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
            BigRational insertSortKey = index > 0 ?
                                        (List[index - 1].SortKey + List[index].SortKey) / 2 :
                                        List[index].SortKey - 1;


            // 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 ImmutableDictionaryListPair <TKey, TValue> Add(KeyValuePair <TKey, TValue> pair)
        {
            var endNode = List.Any() ? List[List.Count - 1] : null;

            // Create the list of nodes for the internal list
            var node = new ObservableDictionaryNode <TKey, TValue>(pair, endNode);

            // create the new immutable Dictionary/List pair
            return(new ImmutableDictionaryListPair <TKey, TValue>(Dictionary.Add(pair.Key, node), List.Add(node)));
        }
        /// <summary>
        /// Gets the index of the key passed in. If the index can't be determined then -1 is returned.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private int GetIndex(ObservableDictionaryNode <TKey, TValue> node)
        {
            int matchIndex = _indexFinder.GetMatchIndex(List.Count, node.SortKey, index => List[index].SortKey);

            if (matchIndex < 0 && !node.Equals(List[matchIndex]))
            {
                throw new InvalidOperationException("Theres a bug in the code that finds the dictionary index");
            }

            return(matchIndex);
        }
Ejemplo n.º 4
0
        public override TValue RetrieveOrAdd(TKey key, Func <TKey, TValue> getValue)
        {
            ObservableDictionaryNode <TKey, TValue> internalNode = null;
            // Make this nullable so it throws an exception if there's a bug in this code
            KeyValuePair <TKey, TValue>?newPair = null;

            if (DoTestReadWriteNotify(
                    // Test if already exists, continue if it doesn't
                    () => !_internalCollection.Dictionary.TryGetValue(key, out internalNode),
                    // create new node, similar to add
                    () => _sorter.GetInsertIndex(_internalCollection.Count, key, i => _internalCollection.List[i].Key),
                    (index) => _internalCollection.Insert(index, (newPair = KeyValuePair.Create(key, getValue(key))).Value),
                    (index) => new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, newPair, index)
                    ))
            {
                // new one created
                return(newPair.Value.Value);
            }
            else
            {
                return(internalNode.Value);
            }
        }
 public ObservableDictionaryNode(KeyValuePair <TKey, TValue> pair, ObservableDictionaryNode <TKey, TValue> before)
     : this(pair, before != null ? before.SortKey + 1 : 0)
 {
 }
        public ImmutableDictionaryListPair <TKey, TValue> ReplaceItem(int index, KeyValuePair <TKey, TValue> pair)
        {
            ObservableDictionaryNode <TKey, TValue> node = new ObservableDictionaryNode <TKey, TValue>(pair, List[index].SortKey);

            return(new ImmutableDictionaryListPair <TKey, TValue>(Dictionary.SetItem(node.Key, node), List.SetItem(index, node)));
        }
        public ImmutableDictionaryListPair <TKey, TValue> RemoveAt(int index)
        {
            ObservableDictionaryNode <TKey, TValue> nodeToRemove = List[index];

            return(new ImmutableDictionaryListPair <TKey, TValue>(Dictionary.Remove(nodeToRemove.Key), List.RemoveAt(index)));
        }