public ImmutableDictionaryListPair <TKey, TValue> Insert(int index, KeyValuePair <TKey, TValue> pair)
        {
            if (index == Count)
            {
                return(Add(pair));
            }
            else if (index > Count || index < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(index), index, $"Needs to be greated than -1 and less than the count of {Count}");
            }

            // Need to find the node position numbers either side, take the average, and that is the new sort key
            BigRationalOld insertSortKey = index > 0 ?
                                           (List[index - 1].SortKey + List[index].SortKey) / (BigRationalOld)2 :
                                           List[index].SortKey - BigRationalOld.One;


            // Create the list of nodes for the internal list
            var node = new ObservableDictionaryNode <TKey, TValue>(pair, insertSortKey);
            // Double update the lists inserting the new node and replacing the after node with a modified version
            var newList       = List.Insert(index, node);
            var newDictionary = Dictionary.Add(node.Key, node);

            // Create the new collection
            return(new ImmutableDictionaryListPair <TKey, TValue>(newDictionary, newList));
        }
        public void TestIteratingOnInsertionIndexUpDirectionAlternate1()
        {
            // If iterations is set to 10_000 it takes 23 minutes to run this test

            List <BigRationalOld> testSetLower = new List <BigRationalOld>(_count);
            List <BigRationalOld> testSetUpper = new List <BigRationalOld>(_count);

            for (int i = 0; i < _count; ++i)
            {
                testSetLower.Add(i - _count / 2);
                testSetUpper.Add(i + 1 - _count / 2);
            }
            for (int iteration = 0; iteration < _iterations; ++iteration)
            {
                for (int i = 0; i < _count; ++i)
                {
                    // Get pairs of number, take half way between them, and check it
                    // is less than the high end and greater than the lower end
                    BigRationalOld newValue = (testSetLower[i] + testSetUpper[i]) / 2;
                    Assert.IsTrue(newValue > testSetLower[i]);
                    Assert.IsTrue(newValue < testSetUpper[i]);
                    testSetLower[i] = newValue;
                }
            }
        }
Example #3
0
 public ObservableDictionaryNode(KeyValuePair <TKey, TValue> pair, BigRationalOld position)
 {
     KeyValuePair = pair;
     SortKey      = position;
 }