예제 #1
0
        /// <summary>
        /// Adds a key/value pair to the <see cref="T:System.Collections.Generic.IDictionary`2"/> if the key does not already exist,
        /// or updates a key/value pair if the key already exists.
        /// </summary>
        public TValue AddOrUpdate(TKey key, Converter <TKey, TValue> fnCreate, KeyValueUpdate <TKey, TValue> fnUpdate)
        {
            InsertionInfo ii = new InsertionInfo(fnCreate, fnUpdate);

            AddEntry(key, ref ii);
            return(ii.Value);
        }
예제 #2
0
        /// <summary>
        /// Adds a key/value pair to the  <see cref="T:System.Collections.Generic.IDictionary`2"/> if the key does not already exist.
        /// </summary>
        /// <param name="key">The key of the element to add.</param>
        /// <param name="fnCreate">Constructs a new value for the key.</param>
        /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IDictionary`2"/> is read-only.</exception>
        public TValue GetOrAdd(TKey key, Converter <TKey, TValue> fnCreate)
        {
            InsertionInfo ii = new InsertionInfo(fnCreate, IgnoreUpdate);

            AddEntry(key, ref ii);
            return(ii.Value);
        }
예제 #3
0
파일: BTree.cs 프로젝트: toStars21/Term-4
        private InsertionInfo FindPlace(Node <T> node, T value)
        {
            InsertionInfo info = new InsertionInfo();

            int position = 0;

            for (int i = 0; i < node.Capacity; i++)
            {
                //find first element upper than value
                if (node[position].Value.CompareTo(value) > 0)
                {
                    position = i;
                    break;
                }
                position = i;
            }

            //fill the info about searching
            info.Neighboor = node[position];
            info.Node      = node;
            info.Position  = position;


            Node <T> toDiscover = null;

            //discover left node
            if (node[position].Value.CompareTo(value) > 0)
            {
                toDiscover = node[position].LeftNode;
            }
            //discover right node
            else
            {
                toDiscover = node[position].RightNode;
            }

            if (toDiscover != null)
            {
                info = FindPlace(toDiscover, value);
            }


            return(info);
        }
예제 #4
0
파일: BTree.cs 프로젝트: toStars21/Term-4
        public void Add(T value)
        {
            #region Create Element instanse

            Element <T> element = new Element <T>();
            element.Value = value;

            #endregion



            if (_root.Capacity == 0)
            {
                _root[0] = element;
            }
            else
            {
                InsertionInfo info = FindPlace(_root, value);

                //if the element exist
                //return
                if (info.Neighboor != null && info.Neighboor.Value.CompareTo(value) == 0)
                {
                    return;
                }

                if (info.Node.Capacity == 2 * _t - 1)
                {
                    var node = info.Node;
                    Reallocate(ref node);
                    info = FindPlace(_root, value);
                }

                //we need to reallocate all elements after position
                if (info.Position < info.Node.Capacity)
                {
                    //insert after neighbour
                    //this situation could be just if
                    //the info.Neighbour is alone
                    if (info.Neighboor.Value.CompareTo(value) < 0)
                    {
                        info.Node[info.Position + 1] = element;
                    }
                    //insert before
                    else
                    {
                        for (int i = info.Node.Capacity - 1; i >= info.Position; i--)
                        {
                            Swap(ref info.Node, i, i + 1);
                        }
                        info.Node[info.Position] = element;
                    }
                }
                //the info.Neighbour will always be null
                else
                {
                    info.Node[info.Position] = element;
                }

                //if the amount of value is max
                //need to reallocate the node
                if (info.Node.Capacity == 2 * _t - 1)
                {
                    var needRealocate = info.Node;
                    Reallocate(ref needRealocate);
                }
            }
        }
예제 #5
0
        /// <summary>
        /// Adds an element with the provided key and value to the <see cref="T:System.Collections.Generic.IDictionary`2"/>
        /// by calling the provided factory method to construct the value if the key is not already present in the collection.
        /// </summary>
        public bool TryAdd(TKey key, Converter <TKey, TValue> fnCreate)
        {
            InsertionInfo ii = new InsertionInfo(fnCreate, IgnoreUpdate);

            return(InsertResult.Inserted == AddEntry(key, ref ii));
        }