Esempio n. 1
0
        private SnugArray(Dictionary<TimeSpan, ParentIDCount> EntryArrayOfStuff, TimeSpan MaxValue, List<Dictionary<string, int>> MyListOfDicts)
        //private SnugArray(List<TimeSpanWithID> ConstrainedElements,List<TimeSpanWithID> EntryArrayOfStuff, TimeSpan MaxValue,List<Dictionary<string, int>> MyListOfDicts)
        //private SnugArray(List<TimeSpanWithID> ConstrainedElements,List<TimeSpanWithID> EntryArrayOfStuff, TimeSpan MaxValue,Dictionary<string, int> MyDict,List<Dictionary<string, int>> MyListOfDicts)
        {

            EntryArrayOfStuff = new Dictionary<TimeSpan, ParentIDCount>(EntryArrayOfStuff);
            ArrayOfStuff = EntryArrayOfStuff;
            int i=0;
            maxTimeSpan = MaxValue;
            List<TimeSpan> EventKeys = EntryArrayOfStuff.Keys.ToList();


            List<TimeSpan> SmallerElements = new List<TimeSpan>();
            i = 0;
            int MyArrayCount = EventKeys.Count;
            ListOfDictionaryOfID_Count = MyListOfDicts;
            for (; i < MyArrayCount; i++)//loop gets element only smaller or equal to max size
            {
                ParentIDCount MyEntry = EntryArrayOfStuff[EventKeys[i]];
                if ((MyEntry.Item2 <= MaxValue)&&(MyEntry.Item1 >0))//checks if smaller than max value
                {
                    SmallerElements.Add(EventKeys[i]); 
                }
            }
            int IncludeOrDontIncludeLastElement = 0;
            if (SmallerElements.Count > 1)
            {
                int counter = SmallerElements.Count;
                TimeSpan TotalSumOfTimeSoFar = new TimeSpan(0);
                for (; counter > 0;counter-- )//including the index 0 in for loop means index is small enough to include all indexes
                {
                    TimeSpan KeyToLastElement = SmallerElements[counter - 1];
                    ParentIDCount CurrentParentIDCOunt = EntryArrayOfStuff[KeyToLastElement];
                    TotalSumOfTimeSoFar += TimeSpan.FromTicks(CurrentParentIDCOunt.Item2.Ticks * CurrentParentIDCOunt.Item1);
                    TimeSpan TimeSpanDiff = MaxValue - TotalSumOfTimeSoFar;
                    if (TimeSpanDiff >= EntryArrayOfStuff[SmallerElements[0]].Item2)
                    {
                        ++IncludeOrDontIncludeLastElement;
                    }
                    else 
                    {
                        break;
                    }
                }
            }

            TopElements = SmallerElements.ToArray();
            //int LengthOfSubElmentArray = TopElements.Length;
            

            i = 0;

            TimeSpan[] MyFittableElemtsHolder = new TimeSpan[SmallerElements.Count];//Array holds the current array of subelements for future reasons
            List<TimeSpan> MyFittableElementsHolderList = new List<TimeSpan>();//This is also holds a curent array of sub elements.
            SmallerElements.CopyTo(MyFittableElemtsHolder, 0);
            MyFittableElementsHolderList = MyFittableElemtsHolder.ToList();
            MyArrayCount = SmallerElements.Count - IncludeOrDontIncludeLastElement;
            SubSnugArrayElements = new SnugArray[MyArrayCount];
            Tuple<Dictionary<TimeSpan, ParentIDCount>, TimeSpan, List<Dictionary<string, int>>>[] PreppedDataArray = new Tuple<Dictionary<TimeSpan, ParentIDCount>, TimeSpan, List<Dictionary<string, int>>>[MyArrayCount];
            
            MyDict = EntryArrayOfStuff;

            Dictionary<TimeSpan, ParentIDCount> EntryArrayOfStuff_Cpy = new Dictionary<TimeSpan, ParentIDCount>(EntryArrayOfStuff);




#if enableMultithreading

            int j = 0;
            for (; j < MyArrayCount; j++)//prepares data for multithreading breaks dependent references
            {
                
                ParentIDCount DictEntry = EntryArrayOfStuff_Cpy[TopElements[j]];
                DictEntry = DictEntry.CreateCopy;
                --(DictEntry.Item1);
                EntryArrayOfStuff_Cpy[TopElements[j]] = DictEntry;

                Tuple<Dictionary<TimeSpan, ParentIDCount>, TimeSpan, List<Dictionary<string, int>>> PreppedData = new Tuple<Dictionary<TimeSpan, ParentIDCount>, TimeSpan, List<Dictionary<string, int>>>(EntryArrayOfStuff_Cpy, (MaxValue - DictEntry.Item2), ListOfDictionaryOfID_Count);//creates new snug array with elements that are smaller
                PreppedDataArray[j] = PreppedData;
                EntryArrayOfStuff_Cpy.Remove(TopElements[j]);
            }





            Parallel.For(0, MyArrayCount, k =>//makes parallel calls
            {
                
                if (k > 0)
                {
                    EntryArrayOfStuff_Cpy.Remove(TopElements[j - 1]);
                }


                Tuple<Dictionary<TimeSpan, ParentIDCount>, TimeSpan, List<Dictionary<string, int>>> PreppedData0 = PreppedDataArray[k];
                SubSnugArrayElements[k] = new SnugArray(PreppedData0.Item1, PreppedData0.Item2, PreppedData0.Item3);//creates graph on current node

            }

);

#else
            for (; i < MyArrayCount; i++)
            {
               ParentIDCount DictEntry = EntryArrayOfStuff_Cpy[TopElements[i]];
                DictEntry = DictEntry.CreateCopy;
                --(DictEntry.Item1);
                EntryArrayOfStuff_Cpy[TopElements[i]] = DictEntry;
                SubSnugArrayElements[i] = new SnugArray(EntryArrayOfStuff_Cpy, (MaxValue - DictEntry.Item2), ListOfDictionaryOfID_Count);//creates new graph for current node
                EntryArrayOfStuff_Cpy.Remove(TopElements[i]);//removes current node from graph
            }
#endif

            


































 
  




        
        }