Home > OS >  C# get all possible combinations
C# get all possible combinations

Time:12-23

I have a List of Custom Class inside a class

public class CustomImage
{ 
    public CustomImage(Image image,string Hash)
    {
        this.Image = image;
        this.HashCode = Hash;
    }
    public Image Image { get; set; }

    public string HashCode { get; set; }
}

 public class Layer
{
    public List<CustomImage> Images { get; set; }
    
    public Layer(string folderPath)
    {
        //....
    }
    public string HashCode { get; set; }
    //....
}

And inside my Form I have: public List<Layer> Layers = new List<Layer>();

  private Image[] Test()
    {
        Image First = Layers[0].Images[0].Image;

        Image Second = Layers[1].Images[0].Image;

        Image Third = Layers[2].Images[0].Image;

        return new Image[] { First, Second, Third };
    }

and after : Image Third = Layers[2].Images[1].Image;

and after :Image Third = Layers[2].Images[2].Image;

When the Third Image Loop is all done then it will start doing the second one.

Image Second = Layers[1].Images[1].Image; Image Third = Layers[2].Images[0].Image;

Image Second = Layers[1].Images[1].Image; Image Third = Layers[2].Images[1].Image;

It should get 1 image from each layer in Layers List and return an Image List. I couldn't find any solutions for getting all combinations. Can you help me?

What I am aiming for as an image

CodePudding user response:

You can use this method to achieve what you want:

public static IEnumerable<CustomImage[]> GetCombinations(List<Layer> input)
{
    int[] factors = new int[input.Count   1];
    factors[factors.Length - 1] = 1;
    for(int i = factors.Length - 2; i >= 0; i--)
    {
        factors[i] = factors[i 1] * input[i].Images.Count;
    }
    int max = factors[0];
        
    for(int j = 0; j < max; j  )
    {
        CustomImage[] result = new CustomImage[input.Count];
        for(int i = 0; i < input.Count; i  )
        {
            result[i] = input[i].Images[j / factors[i 1] % input[i].Images.Count];
        }
        yield return result;
    }
}

In the first step, we fill an array called factors. The last element of the array will be 1. The array is then filled from behind. Each element is the product of the following element and the amount of images of the layer with the same index. For example, if we have three layers with 2, 2 and 3 images, the element will be from behind:

  • 1 (fixed for the last element)
  • 3*1 = 3 (last layer has three images)
  • 2*3 = 6 (layer before last layer has two images)
  • 2*6 = 12 (layer before that layer has again two images)

The first element of this array is the product of all amounts of images of all layers and thus the amount of combinations.

The we setup a loop which will produce all combinations. We take one image from each layer. The index inside each layer can be calculated by dividing through the next factor and then doing a modulo division through the amount of images of the layer.

Online demo: https://dotnetfiddle.net/jhk18u

CodePudding user response:

@Hebele Hübele, as suggested by @Liam . you need two for loops:

        var imageArray = new Image[1];
        //loop the layers
        foreach (var layer in layers) {
            //and for each layer loop images
            foreach (var imgObject in layer.Images) {
                imageArray.Append(imgObject.Image)
            }
        }
  • Related