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 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);
        }
Example #4
0
        /// <summary>
        /// Retrives a value for the key passed in if it exists, else
        /// adds the new value passed in.
        /// </summary>
        /// <param name="key">
        /// The object to use as the key of the element to retrieve or add.
        /// </param>
        /// <param name="getValue">
        /// The object factory to use if key doesn't already exist
        /// </param>
        /// <returns></returns>
        public virtual 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 the 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
                    () => _internalCollection.Count,
                    (index) => _internalCollection.Add((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 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)));
        }
 public ObservableDictionaryNode(KeyValuePair <TKey, TValue> pair, ObservableDictionaryNode <TKey, TValue> before)
     : this(pair, before != null ? before.SortKey + 1 : 0)
 {
 }
Example #8
0
 public ObservableDictionaryNode(KeyValuePair <TKey, TValue> pair, ObservableDictionaryNode <TKey, TValue> before)
     : this(pair, before != null ? before.SortKey + BigRationalOld.One: BigRationalOld.Zero)
 {
 }