コード例 #1
0
 internal new int DoSingleOperation(ref AListSingleOperation <K, KeyValuePair <K, V> > op)
 {
     op.CompareKeys  = _compareKeys;
     op.CompareToKey = CompareToKey;
     op.Key          = op.Item.Key;
     return(base.DoSingleOperation(ref op));
 }
コード例 #2
0
        public void Add(KeyValuePair <K, V> item)
        {
            var op = new AListSingleOperation <K, KeyValuePair <K, V> >();

            op.Mode = AListOperation.AddOrThrow;
            op.Item = item;
            DoSingleOperation(ref op);
        }
コード例 #3
0
        public bool TryGetValue(K key, out V value)
        {
            var op = new AListSingleOperation <K, KeyValuePair <K, V> >();

            op.Key = key;
            OrganizedRetrieve(ref op);
            value = op.Item.Value;
            return(op.Found);
        }
コード例 #4
0
        /// <inheritdoc cref="FindLowerBound(T)"/>
        public int FindLowerBound(K key, out bool found)
        {
            var op = new AListSingleOperation <K, KeyValuePair <K, V> >();

            op.Key = key;
            OrganizedRetrieve(ref op);
            found = op.Found;
            return((int)op.BaseIndex);
        }
コード例 #5
0
        public bool Remove(KeyValuePair <K, V> item)
        {
            var op = new AListSingleOperation <K, KeyValuePair <K, V> >();

            op.Mode = AListOperation.Remove;
            op.Item = item;
            op.RequireExactMatch = true;
            return(DoSingleOperation(ref op) < 0);
        }
コード例 #6
0
ファイル: BMultiMap.cs プロジェクト: jonathanvdc/Loyc
        /// <summary>Finds out whether the specified key is present.</summary>
        /// <param name="key">Key to search for</param>
        /// <returns>Returns true if the dictionary contains at least one key-
        /// value pair in which the key compares equal to the specified key.</returns>
        public bool ContainsKey(K key)
        {
            var op = new AListSingleOperation <KeyValuePair <K, V>, KeyValuePair <K, V> >();

            op.CompareToKey = op.CompareKeys = CompareKeysOnly;
            op.Key          = new KeyValuePair <K, V>(key, default(V));
            OrganizedRetrieve(ref op);
            return(op.Found);
        }
コード例 #7
0
ファイル: BMultiMap.cs プロジェクト: jonathanvdc/Loyc
        /// <summary>Removes one pair from the collection that matches the specified key.</summary>
        /// <returns>True if a pair was removed, or false if the key was not found.</returns>
        public bool RemoveAny(K key)
        {
            var op = new AListSingleOperation <KeyValuePair <K, V>, KeyValuePair <K, V> >();

            op.Mode         = AListOperation.Remove;
            op.CompareToKey = op.CompareKeys = CompareKeysOnly;
            op.Key          = op.Item = new KeyValuePair <K, V>(key, default(V));
            return(DoSingleOperation(ref op) < 0);
        }
コード例 #8
0
        /// <summary>Replaces the value associated with a specified key, if it already exists in the dictionary.</summary>
        /// <param name="key">Key to replace. If this parameter is passed by reference and a matching pair existed, this method sets it to the old key instance.</param>
        /// <param name="value">New value to associate with the key. If this parameter is passed by reference and a matching pair existed, this method sets it to the old value.</param>
        /// <returns>True if the key was found and the pair was replaced, false if it was not found.</returns>
        /// <remarks>
        /// This method has no effect if the key was not already present.
        /// </remarks>
        public bool ReplaceIfPresent(K key, V value)
        {
            var op = new AListSingleOperation <K, KeyValuePair <K, V> >();

            op.Mode = AListOperation.ReplaceIfPresent;
            op.Item = new KeyValuePair <K, V>(key, value);
            DoSingleOperation(ref op);
            return(op.Found);
        }
コード例 #9
0
ファイル: BMultiMap.cs プロジェクト: jonathanvdc/Loyc
        /// <summary>Finds the index of the first item in the list whose key is
        /// greater than the specified key.</summary>
        /// <param name="key">The key to find. If passed by reference, when this
        /// method returns, item is set to the next greater item than the item you
        /// searched for, or left unchanged if there is no greater item.</param>
        /// <param name="index">The index of the next greater item that was found,
        /// or Count if the given item is greater than all items in the list.</param>
        /// <returns></returns>
        public int FindUpperBound(K key)
        {
            var op = new AListSingleOperation <KeyValuePair <K, V>, KeyValuePair <K, V> >();

            op.CompareKeys = op.CompareToKey = UpperBoundCompare;
            op.Key         = new KeyValuePair <K, V>(key, default(V));
            OrganizedRetrieve(ref op);
            return((int)op.BaseIndex);
        }
コード例 #10
0
        public bool Remove(K key)
        {
            var op = new AListSingleOperation <K, KeyValuePair <K, V> >();

            op.Mode         = AListOperation.Remove;
            op.Key          = key;
            op.CompareKeys  = _compareKeys;
            op.CompareToKey = CompareToKey;
            return(base.DoSingleOperation(ref op) < 0);
        }
コード例 #11
0
        public void Add(T item)
        {
            AListSingleOperation <T, T> op = new AListSingleOperation <T, T>();

            op.Mode         = AListOperation.Add;
            op.CompareKeys  = _compareItems;
            op.CompareToKey = _compareItems;
            op.Key          = op.Item = item;
            DoSingleOperation(ref op);
        }
コード例 #12
0
        /// <inheritdoc cref="Do(AListOperation, T)"/>
        public int Do(AListOperation mode, T item)
        {
            AListSingleOperation <T, T> op = new AListSingleOperation <T, T>();

            op.Mode         = mode;
            op.CompareKeys  = _compareItems;
            op.CompareToKey = _compareItems;
            op.Key          = op.Item = item;
            return(DoSingleOperation(ref op));
        }
コード例 #13
0
        V ITryGet <K, V> .TryGet(K key, out bool fail)       // enable TryGet extension methods (in TryGetExt)
        {
            var op = new AListSingleOperation <K, KeyValuePair <K, V> >()
            {
                Key = key
            };

            OrganizedRetrieve(ref op);
            fail = !op.Found;
            return(op.Item.Value);
        }
コード例 #14
0
        /// <summary>Finds the index of the first item in the list that is greater
        /// than the specified item.</summary>
        /// <param name="item">The item to find. If passed by reference, when this
        /// method returns, item is set to the next greater item than the item you
        /// searched for, or left unchanged if there is no greater item.</param>
        /// <param name="index">The index of the next greater item that was found,
        /// or Count if the given item is greater than all items in the list.</param>
        /// <returns></returns>
        public int FindUpperBound(K key)
        {
            var op = new AListSingleOperation <K, KeyValuePair <K, V> >();
            // When searchKey==candidate, act like searchKey>candidate.
            Func <K, K, int> upperBoundCmp = (candidate, searchKey) => - (_compareKeys(searchKey, candidate) | 1);

            op.Key          = key;
            op.CompareKeys  = upperBoundCmp;
            op.CompareToKey = (pair, k) => upperBoundCmp(pair.Key, k);
            base.OrganizedRetrieve(ref op);
            return((int)op.BaseIndex);
        }
コード例 #15
0
        /// <inheritdoc cref="FindLowerBound(T)"/>
        public int FindLowerBound(T item, out bool found)
        {
            var op = new AListSingleOperation <T, T>();

            op.CompareKeys  = _compareItems;
            op.CompareToKey = _compareItems;
            op.Key          = item;
            op.LowerBound   = true;
            OrganizedRetrieve(ref op);
            found = op.Found;
            return((int)op.BaseIndex);
        }
コード例 #16
0
        /// <summary>Finds the index of the first item in the list that is greater
        /// than the specified item.</summary>
        /// <param name="item">The item to find. If passed by reference, when this
        /// method returns, item is set to the next greater item than the item you
        /// searched for, or left unchanged if there is no greater item.</param>
        /// <returns>The index of the next greater item that was found,
        /// or Count if the given item is greater than all items in the list.</returns>
        public int FindUpperBound(T item)
        {
            var op = new AListSingleOperation <T, T>();
            // When searchKey==candidate, act like searchKey>candidate.
            Func <T, T, int> upperBoundCmp = (candidate, searchKey) => - (_compareItems(searchKey, candidate) | 1);

            op.CompareKeys  = upperBoundCmp;
            op.CompareToKey = upperBoundCmp;
            op.Key          = item;
            OrganizedRetrieve(ref op);
            return((int)op.BaseIndex);
        }
コード例 #17
0
 public V this[K key, V defaultValue]
 {
     get {
         var op = new AListSingleOperation <K, KeyValuePair <K, V> >();
         op.Key = key;
         OrganizedRetrieve(ref op);
         if (!op.Found)
         {
             return(defaultValue);
         }
         return(op.Item.Value);
     }
 }
コード例 #18
0
        /// <summary>TODO: TEST THIS!!</summary>
        public Maybe <V> GetAndRemove(K key)
        {
            var op = new AListSingleOperation <K, KeyValuePair <K, V> >();

            op.Mode         = AListOperation.Remove;
            op.Key          = key;
            op.CompareKeys  = _compareKeys;
            op.CompareToKey = CompareToKey;
            if (base.DoSingleOperation(ref op) < 0)
            {
                return(op.Item.Value);                // TODO: TEST THIS!!
            }
            return(Maybe <V> .NoValue);
        }
コード例 #19
0
        /// <inheritdoc cref="ReplaceIfPresent(K,V)"/>
        public bool ReplaceIfPresent(ref K key, ref V value)
        {
            var op = new AListSingleOperation <K, KeyValuePair <K, V> >();

            op.Mode = AListOperation.ReplaceIfPresent;
            op.Item = new KeyValuePair <K, V>(key, value);
            DoSingleOperation(ref op);
            if (op.Found)
            {
                key   = op.Item.Key;
                value = op.Item.Value;
            }
            return(op.Found);
        }
コード例 #20
0
        /// <inheritdoc cref="SetAndGetOldValue(K,V)"/>
        public bool SetAndGetOldValue(ref K key, ref V value)
        {
            var op = new AListSingleOperation <K, KeyValuePair <K, V> >();

            op.Mode = AListOperation.AddOrReplace;
            op.Item = new KeyValuePair <K, V>(key, value);
            DoSingleOperation(ref op);
            if (op.Found)
            {
                key   = op.Item.Key;
                value = op.Item.Value;
            }
            return(!op.Found);
        }
コード例 #21
0
ファイル: BMultiMap.cs プロジェクト: jonathanvdc/Loyc
        public int FindLowerBound(ref K key, out V value, out bool found)
        {
            var op = new AListSingleOperation <KeyValuePair <K, V>, KeyValuePair <K, V> >();

            op.CompareKeys = op.CompareToKey = CompareKeysOnly;
            op.Key         = new KeyValuePair <K, V>(key, default(V));
            op.LowerBound  = true;
            OrganizedRetrieve(ref op);
            if (found = op.Found)
            {
                key = op.Item.Key;
            }
            value = op.Item.Value;
            return((int)op.BaseIndex);
        }
コード例 #22
0
        /// <summary>Finds the lowest index of an item that is equal to or greater than the specified item.</summary>
        /// <param name="item">Item to find.</param>
        /// <returns>The lower-bound index, or Count if the item is greater than all items in the list.</returns>
        public int IndexOf(T item)
        {
            var op = new AListSingleOperation <T, T>();

            op.CompareKeys  = _compareItems;
            op.CompareToKey = _compareItems;
            op.Key          = item;
            op.LowerBound   = true;
            OrganizedRetrieve(ref op);
            if (!op.Found)
            {
                return(-1);
            }
            return((int)op.BaseIndex);
        }
コード例 #23
0
        public int IndexOf(KeyValuePair <K, V> item)
        {
            var op = new AListSingleOperation <K, KeyValuePair <K, V> >();

            op.Item = item;
            op.Key  = item.Key;
            op.RequireExactMatch = true;
            OrganizedRetrieve(ref op);
            if (!op.Found)
            {
                return(-1);
            }
            Debug.Assert(item.Equals(op.Item));
            return((int)op.BaseIndex);
        }
コード例 #24
0
 public V this[K key]
 {
     get {
         if (!TryGetValue(key, out V value))
         {
             throw new KeyNotFoundException();
         }
         return(value);
     }
     set {
         var op = new AListSingleOperation <K, KeyValuePair <K, V> >();
         op.Mode = AListOperation.AddOrReplace;
         op.Item = new KeyValuePair <K, V>(key, value);
         DoSingleOperation(ref op);
     }
 }
コード例 #25
0
        /// <inheritdoc cref="FindLowerBound(T)"/>
        public int FindLowerBound(ref K key, out bool found)
        {
            var op = new AListSingleOperation <K, KeyValuePair <K, V> >();

            op.Key = key;
            OrganizedRetrieve(ref op);
            if (found = op.Found)
            {
                key = op.Item.Key;
            }
            else if ((int)op.BaseIndex < Count)
            {
                key = this[(int)op.BaseIndex].Key;
            }
            return((int)op.BaseIndex);
        }
コード例 #26
0
        // TODO: TEST THIS!
        public int AddRange(IEnumerable <KeyValuePair <K, V> > e, DictEditMode mode)
        {
            var op = new AListSingleOperation <K, KeyValuePair <K, V> >();

            op.Mode = (AListOperation)((int)mode & 3);
            int numNewPairs = 0;

            foreach (var pair in e)
            {
                op.Item = pair;
                DoSingleOperation(ref op);
                if (!op.Found)
                {
                    numNewPairs++;
                }
            }
            return(numNewPairs);
        }
コード例 #27
0
        public bool GetAndEdit(ref K key, ref V value, DictEditMode mode)
        {
            var op = new AListSingleOperation <K, KeyValuePair <K, V> >();

            op.Mode = (AListOperation)((int)mode & 3);
            op.Item = new KeyValuePair <K, V>(key, value);
            DoSingleOperation(ref op);
            if (op.Found)
            {
                key   = op.Item.Key;
                value = op.Item.Value;
                return(true);
            }
            else
            {
                return(false);
            }
        }
コード例 #28
0
        /// <inheritdoc cref="FindLowerBound(T)"/>
        public int FindLowerBound(ref T item, out bool found)
        {
            var op = new AListSingleOperation <T, T>();

            op.CompareKeys  = _compareItems;
            op.CompareToKey = _compareItems;
            op.Key          = item;
            op.LowerBound   = true;
            OrganizedRetrieve(ref op);
            if (found = op.Found)
            {
                item = op.Item;
            }
            else if ((int)op.BaseIndex < Count)
            {
                item = this[(int)op.BaseIndex];
            }
            return((int)op.BaseIndex);
        }
コード例 #29
0
        /// <summary>Performs the same operation for each item in a series.
        /// Equivalent to calling <see cref="Do(AListOperation,T)"/> on each item.</summary>
        /// <param name="mode">Indicates the operation to perform.</param>
        /// <param name="e">A list of items to act upon.</param>
        /// <returns>Returns the change in Count: positive if items were added,
        /// negative if items were removed, and 0 if all items were unchanged or
        /// replaced.</returns>
        public int DoRange(AListOperation mode, IEnumerable <T> e)
        {
            AListSingleOperation <T, T> op = new AListSingleOperation <T, T>();

            op.Mode         = mode;
            op.CompareKeys  = _compareItems;
            op.CompareToKey = _compareItems;

            int delta = 0;

            foreach (T item in e)
            {
                op.Key = op.Item = item;
                // Some fields must be cleared before each operation
                op.BaseIndex        = 0;
                op.Found            = false;
                op.AggregateChanged = 0;
                delta += DoSingleOperation(ref op);
                Debug.Assert(op.Mode == mode);
            }
            return(delta);
        }
コード例 #30
0
 internal new void OrganizedRetrieve(ref AListSingleOperation <K, KeyValuePair <K, V> > op)
 {
     op.CompareKeys  = _compareKeys;
     op.CompareToKey = CompareToKey;
     base.OrganizedRetrieve(ref op);
 }