Task Get all combinations for array (array is very simple 1,2,3,4,5 - just indexes of lines in table, always in order, so we can use just count-number)
I need result looks like : List of array or List Of List (0:0, 1:1, 2:0,1, 3:3, 4: 0,1,2 ...)
Data state I have rows in table, each row contains 6 columns with decimal number. My target to get sum of numbers from that columns for each selected row which closest to my specific number(test say 600). So I have to select only that rows what have the closet number in sum.
Code
cyclical
Private Function CreateSubsets(Of T)(ByVal originalArray() As T) As List(Of T()) Dim subsets As New List(Of T()) For i As Integer = 0 To originalArray.Length - 1 Dim subsetCount As Integer = subsets.Count subsets.Add(New T() {originalArray(i)}) For j As Integer = 0 To subsetCount - 1 Dim newSubset(subsets(j).Length) As T subsets(j).CopyTo(newSubset, 0) newSubset(newSubset.Length - 1) = originalArray(i) subsets.Add(newSubset) Next Next Return subsets End Function
recursive
Private Function GetAllCombos(Of T)(ByVal list As List(Of T)) As List(Of List(Of T)) Dim result As List(Of List(Of T)) = New List(Of List(Of T)) 'head result.Add(New List(Of T)) result.Last().Add(list(0)) If (list.Count = 1) Then Return result End If 'tail Dim tailCombos = GetAllCombos(list.Skip(1).ToList()) tailCombos.ForEach(Sub(combo) result.Add(New List(Of T)(combo)) combo.Add(list(0)) result.Add(New List(Of T)(combo)) End Sub) Return result End Function
Problems
Get the axception "System.OutOfMemoryException" in both cases. How can I avoid this problem, any solutions? Maybe someone had the same task. I cannot disable limit for large objects - system .NET Framework 4.0 32bit