Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 15th, 2012  |  syntax: None  |  size: 4.32 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Unknown number for loops using Recursion
  2. static void Main(string[] args)
  3.     {
  4.         List<Item> myItem = new List<Item>();
  5.  
  6.         int numberItem1 = 0, numberItem2 = 0;
  7.         foreach (var item in myItem)
  8.         {
  9.             if (item.GetType() == typeof(Item1))
  10.             {
  11.                 numberItem1++;
  12.             }
  13.             else if (item.GetType() == typeof(Item2))
  14.             {
  15.                 numberItem2++;
  16.             }
  17.         }
  18.  
  19.         List<Item> testingItems = new List<Item>();
  20.         //FirstItem
  21.         for (int a = 0; a < numberItem1; a++)
  22.         {
  23.             for (int b = 0; b <= a; b++)
  24.             {
  25.                 testingItems.Add(new Item1 { });                
  26.             }
  27.             //DoTest()
  28.  
  29.             testingItems.Clear();
  30.             //Second Item
  31.             for (int c = 0; c < numberItem2; c++)
  32.             {
  33.                 for (int d = 0; d <= a ; d++)
  34.                 {
  35.                     testingItems.Add(new Item1 { });
  36.                 }
  37.                 for (int e = 0; e <= c; e++)
  38.                 {
  39.                     testingItems.Add(new Item2 { });
  40.                 }
  41.                 //DoTest()
  42.                 testingItems.Clear();
  43.             }
  44.         }
  45.     }
  46.        
  47. IEnumerable<List<Item>> TestLists(List<Item> fullList)
  48. {
  49.     List<Type> types = fullList.Select(i => i.GetType()).Distinct().ToList();
  50.     List<Item> testList = new List<Item> { (Item)Activator.CreateInstance(types[0]) };
  51.     yield return testList;
  52.  
  53.     bool finished = false;
  54.     while (!finished)
  55.     {
  56.         bool incremented = false;
  57.         int i = 0;
  58.         while (i < types.Count && !incremented)
  59.         {
  60.             if (testList.Where(t => t.GetType() == types[i]).Count() <
  61.                 fullList.Where(t => t.GetType() == types[i]).Count())
  62.             {
  63.                 testList.Add((Item)Activator.CreateInstance(types[i]));
  64.                 incremented = true;
  65.             }
  66.             else
  67.             {
  68.                 testList = testList.Where(t => t.GetType() != types[i]).ToList();
  69.                 i++;
  70.             }
  71.         }
  72.         if (incremented)
  73.         {
  74.             yield return testList;
  75.         }
  76.         else
  77.         {
  78.             finished = true;
  79.         }
  80.     }
  81. }
  82.        
  83. foreach (var partList in TestLists(myListToTest))
  84. {
  85.     DoTest(partList);
  86. }
  87.        
  88. void RecursiveTest(List<Item> testingItems, Stack<Type> itemTypes, Stack<int> itemCounts)
  89. {
  90.     if (itemTypes.Count == 0) { return; }
  91.     Type thisType = itemTypes.Pop();
  92.     int thisCount = itemCounts.Pop();
  93.     List<Item> addedItems = new List<Item>();
  94.     for (int i = 0; i <= thisCount; i++)
  95.     {
  96.         if (i > 0)
  97.         {
  98.             Item thisItem = (Item)Activator.CreateInstance(thisType);
  99.             testingItems.Add(thisItem);
  100.             addedItems.Add(thisItem);
  101.         }
  102.  
  103.         if (itemTypes.Count == 0)
  104.         {
  105.             DoTest(testingItems);
  106.         }
  107.         else
  108.         {
  109.             RecursiveTest(testingItems, itemTypes, itemCounts);
  110.         }
  111.     }
  112.     foreach(Item addedItem in addedItems)
  113.     {
  114.         testingItems.Remove(addedItem);
  115.     }
  116.     itemTypes.Push(thisType);
  117.     itemCounts.Push(thisCount);
  118. }
  119.        
  120. List<Item> myItem = new List<Item>();
  121. List<Type> myOrder = new List<Item>();
  122. Dictionary<Type, int> myCount = new Dictionary<Type, int>();
  123. foreach (var item in myItem)
  124. {
  125.  if (myCount.ContainsKey(item.GetType()))
  126.  {
  127.   myCount[item.GetType()]++;
  128.  }
  129.  else
  130.  {
  131.   myOrder.Add(item.GetType());
  132.   myCount.Add(item.GetType(), 1);
  133.  }
  134. }
  135.  
  136. List<Item> testingItems = new List<Item>();
  137. int[] testingCounts = new int[myCount.Count];
  138. while(IncrementCounts(testingCounts, myOrder, myCount)) {
  139.  for(int x=0; x<testingCounts.length; x++) {
  140.   AddInstances( testingItems, myOrder[x], testingCounts[x] );
  141.  }
  142.  // doTest()
  143.  testingItems.Clear();
  144. }
  145.  
  146. // count permutations using the maxima
  147. // EXAMPLE: maxima [2, 2, 2]
  148. //  1,0,0; 2,0,0; 0,1,0; 1,1,0; 2,1,0; 0,2,0; 1,2,0; 2,2,0; 0,0,1; 1,0,1; 2,0,1 etc..
  149. public static bool IncrementCounts(int[] counts, List<Type> order, Dictionary<Type, int> maxima) {
  150.  for(int x=0; x<counts.length; x++) {
  151.   if(counts[x] + 1 <= maxima[order[x]]) {
  152.    counts[x]++;
  153.    return true;
  154.   } else {
  155.    counts[x] = 0;
  156.   }
  157.  }
  158.  return false; // overflow, so we're finished
  159. }    
  160.  
  161. public static void AddIstances(List<Item> list, Type type, int count) {
  162.  for(int x=0; x<count; x++) {
  163.   list.Add( Convert.ChangeType( Activator.CreateInstance(type), type ) );
  164.  }
  165. }