Exemple #1
0
        public bool Find(byte[] key, out Enumerator e)
        {
            KeyWalker kw = new KeyWalker(key, key.Length);

            e = new Enumerator(this);
            return(base.Find(ref kw, e));
        }
Exemple #2
0
        public bool Find(int key, out IntEnumerator e)
        {
            KeyWalker kw = Encode(key);

            e = new IntEnumerator(this);
            return(base.Find(ref kw, e));
        }
Exemple #3
0
        /// <summary>Adds the specified key-value pair only if the specified key is
        /// not already present in the trie.</summary>
        /// <param name="key">An array that contains the key to find. The offset
        /// and length parameters specify a substring of this array to use as the key.</param>
        /// <param name="value">On entry, value specifies the value to associate
        /// with the specified key, but if the key already exists, value is changed
        /// to the value associated with the existing key.</param>
        /// <returns>Returns true if the key-value pair was added or false if
        /// the key already existed. In the false case, the trie is not modified.</returns>
        public bool TryAdd(byte[] key, int offset, int length, ref TValue value)
        {
            KeyWalker kw = new KeyWalker(key, offset, length);

            Check(ref kw, "TryAdd");
            return(!base.Set(ref kw, ref value, CPMode.Create));
        }
Exemple #4
0
        /// <summary>Removes the specified key and associated value, returning true
        /// if the entry was found and removed.</summary>
        /// <param name="key">An array that contains the key to find. The offset
        /// and length parameters specify a substring of this array to use as the key.</param>
        /// <param name="oldValue">If the key is found, the associated value is
        /// assigned to this parameter. Otherwise, this parameter is not changed.</param>
        public bool Remove(byte[] key, int offset, int length, ref TValue oldValue)
        {
            KeyWalker kw = new KeyWalker(key, offset, length);

            Check(ref kw, "Remove");
            return(base.Remove(ref kw, ref oldValue));
        }
Exemple #5
0
        /// <summary>Finds the specified key and gets its associated value,
        /// returning true if the key was found.</summary>
        public bool TryGetValue(byte[] key, out TValue value)
        {
            KeyWalker kw = new KeyWalker(key, 0, key.Length);

            value = default(TValue);
            return(base.Find(ref kw, ref value));
        }
Exemple #6
0
        /// <summary>Searches for the specified key, returning true if it is
        /// present in the trie.</summary>
        public bool ContainsKey(byte[] key)
        {
            KeyWalker kw    = new KeyWalker(key, key.Length);
            TValue    value = default(TValue);

            return(base.Find(ref kw, ref value));
        }
Exemple #7
0
        public bool Find(ulong key, out LongEnumerator e)
        {
            KeyWalker kw = Encode(key);

            e = new LongEnumerator(this);
            return(base.Find(ref kw, e));
        }
Exemple #8
0
        public bool TryGetValue(ulong key, out TValue value)
        {
            KeyWalker kw = Encode(key);

            value = default(TValue);
            return(base.Find(ref kw, ref value));
        }
Exemple #9
0
        public override bool Find(ref KeyWalker key, CPEnumerator <T> e)
        {
            if (key.Left == 0)
            {
                MoveFirst(e);
                return(false);
            }
            byte k = key.Buffer[key.Offset];

            if (key.Left == 1 && IsPresent(k))
            {
                e.Stack.Add(new CPEnumerator <T> .Entry(this, k, e.Key.Offset));
                ExtractCurrent(e, k);
                return(true);
            }
            else
            {
                int nextK = FindNextInUse(k);
                if (nextK >= 0)
                {
                    e.Stack.Add(new CPEnumerator <T> .Entry(this, nextK, e.Key.Offset));
                    ExtractCurrent(e, (byte)nextK);
                }
                else
                {                       // code duplicated from CPSNode
                    if (!e.Stack.IsEmpty)
                    {
                        e.Key.Reset(e.Stack.Last.KeyOffset);
                        e.MoveNext();
                    }
                }
                return(false);
            }
        }
Exemple #10
0
        public override bool Remove(ref KeyWalker key, ref T oldValue, ref CPNode <T> self)
        {
            if (key.Left != 1)
            {
                return(false);
            }
            byte k = key.Buffer[key.Offset];

            if (!IsPresent(k))
            {
                return(false);
            }

            if (_values != null)
            {
                int P = GetValueIndex(k);
                if (P < _values.Length)
                {
                    oldValue = _values[P];
                    FreeValueSlot(P);
                }
            }
            else
            {
                Debug.Assert(_indices == null);
            }

            _localCount--;
            _flags[k >> 5] &= ~(1u << (k & 0x1F));
            if (_localCount < 24 && (_valueCount > 0 || _localCount < 12))
            {
                ConvertToBOrSNode(ref self, 0);
            }
            return(true);
        }
Exemple #11
0
 /// <summary>Finds the specified key and returns its associated value. If
 /// the key did not exist, TryGetValue returns defaultValue instead.</summary>
 public TValue this[byte[] key, TValue defaultValue]
 {
     get {
         KeyWalker kw = new KeyWalker(key, 0, key.Length);
         base.Find(ref kw, ref defaultValue);
         return(defaultValue);
     }
 }
Exemple #12
0
        public bool TryGetValue(byte[] key, int offset, int length, out TValue value)
        {
            KeyWalker kw = new KeyWalker(key, offset, length);

            Check(ref kw, "TryGetValue");
            value = default(TValue);
            return(base.Find(ref kw, ref value));
        }
Exemple #13
0
        public LongEnumerator FindAtLeast(ulong key)
        {
            KeyWalker      kw = Encode(key);
            LongEnumerator e  = new LongEnumerator(this);

            base.Find(ref kw, e);
            return(e);
        }
Exemple #14
0
        public Enumerator FindAtLeast(byte[] key)
        {
            KeyWalker  kw = new KeyWalker(key, key.Length);
            Enumerator e  = new Enumerator(this);

            base.Find(ref kw, e);
            return(e);
        }
Exemple #15
0
        public IntEnumerator FindAtLeast(int key)
        {
            KeyWalker     kw = Encode(key);
            IntEnumerator e  = new IntEnumerator(this);

            base.Find(ref kw, e);
            return(e);
        }
Exemple #16
0
 public TValue this[ulong key, TValue defaultValue]
 {
     get {
         KeyWalker kw = Encode(key);
         base.Find(ref kw, ref defaultValue);
         return(defaultValue);
     }
 }
Exemple #17
0
        public bool Find(byte[] key, int offset, int length, out Enumerator e)
        {
            KeyWalker kw = new KeyWalker(key, offset, length);

            Check(ref kw, "Find");
            e = new Enumerator(this);
            return(base.Find(ref kw, e));
        }
Exemple #18
0
        /// <summary>Adds the specified key-value pair to the trie, throwing an
        /// exception if the key is already present.</summary>
        public void Add(byte[] key, TValue value)
        {
            KeyWalker kw = new KeyWalker(key, key.Length);

            if (base.Set(ref kw, ref value, CPMode.Create))
            {
                CheckParam.ThrowBadArgument(nameof(key), "Key already exists: {0}", key);
            }
        }
Exemple #19
0
        /// <summary>Searches for the specified key, returning true if it is
        /// present in the trie.</summary>
        /// <param name="key">An array that contains the key to find. The offset
        /// and length parameters specify a substring of this array to use as the key.</param>
        public bool ContainsKey(byte[] key, int offset, int length)
        {
            KeyWalker kw = new KeyWalker(key, offset, length);

            Check(ref kw, "ContainsKey");
            TValue value = default(TValue);

            return(base.Find(ref kw, ref value));
        }
Exemple #20
0
 public TValue this[byte[] key, int offset, int length, TValue defaultValue]
 {
     get {
         KeyWalker kw = new KeyWalker(key, offset, length);
         Check(ref kw, "TryGetValue");
         base.Find(ref kw, ref defaultValue);
         return(defaultValue);
     }
 }
Exemple #21
0
        /// <summary>Adds the specified key-value pair to the trie, throwing an
        /// exception if the key is already present.</summary>
        public void Add(byte[] key, TValue value)
        {
            KeyWalker kw = new KeyWalker(key, key.Length);

            if (base.Set(ref kw, ref value, CPMode.Create))
            {
                throw new ArgumentException(Localize.Localized("Key already exists: ") + key);
            }
        }
Exemple #22
0
        /// <summary>Adds the specified key-value pair to the trie, throwing an
        /// exception if the key is already present.</summary>
        /// <param name="key">An array that contains the key to add. The offset
        /// and length parameters specify a substring of this array to use as the key.</param>
        public void Add(byte[] key, int offset, int length, TValue value)
        {
            KeyWalker kw = new KeyWalker(key, offset, length);

            Check(ref kw, "Add");
            if (base.Set(ref kw, ref value, CPMode.Create))
            {
                throw new ArgumentException(Localize.Localized("Key already exists: ") + key);
            }
        }
Exemple #23
0
 public TValue this[ulong key]
 {
     get {
         KeyWalker kw = Encode(key);
         return(GetValue(ref kw, key));
     }
     set {
         KeyWalker kw = Encode(key);
         base.Set(ref kw, ref value, CPMode.Set | CPMode.Create);
     }
 }
Exemple #24
0
        public override bool Remove(ref KeyWalker key, ref T oldValue, ref CPNode <T> self)
        {
            CheckValidity();

            if (key.Left > 0)
            {
                int i = key[0] >> 5;
                if (_children[i] != null)
                {
                    CPSNode <T> child       = _children[i];
                    CPNode <T>  child2      = child;
                    int         childLCount = child.LocalCount;
                    if (child.Remove(ref key, ref oldValue, ref child2))
                    {
                        _localCount -= childLCount;
                        if (child2 == null)
                        {
                            _children[i] = null;
                        }
                        else
                        {
                            _localCount += child.LocalCount;
                            Debug.Assert(child == child2);
                        }
                        if (_localCount < 24)
                        {
                            ConvertToSNode(ref self);
                        }
                        return(true);
                    }
                    Debug.Assert(child == _children[i]);
                }
                return(false);
            }
            else
            {
                // key.Left == 0
                if (_zlk == NoZLK)
                {
                    return(false);
                }
                else
                {
                    oldValue = (T)_zlk;
                    _zlk     = NoZLK;
                    _localCount--;
                    if (IsEmpty)
                    {
                        self = null;
                    }
                    return(true);
                }
            }
        }
Exemple #25
0
 private void Check(ref KeyWalker kw, string operation)
 {
     if ((kw.Offset | kw.Left) < 0)
     {
         throw new ArgumentException(operation + ": " + Localize.Localized("offset or length are negative"));
     }
     if (kw.Offset + kw.Left > kw.Buffer.Length)
     {
         throw new ArgumentException(operation + ": " + Localize.Localized("offset+length exceeds buffer length"));
     }
 }
Exemple #26
0
        public bool Contains(KeyValuePair <byte[], TValue> item)
        {
            KeyWalker kw    = new KeyWalker(item.Key, item.Key.Length);
            TValue    value = default(TValue);

            if (base.Find(ref kw, ref value))
            {
                return(DefaultComparer.Compare(value, item.Value) == 0);
            }
            return(false);
        }
Exemple #27
0
 private void Check(ref KeyWalker kw, string operation)
 {
     if ((kw.Offset | kw.Left) < 0)
     {
         CheckParam.ThrowOutOfRange(kw.Offset < 0 ? "offset" : "length", "{0}: offset or length are negative", operation);
     }
     if (kw.Offset + kw.Left > kw.Buffer.Length)
     {
         throw new ArgumentException("{0}: offset+length exceeds buffer length".Localized(operation));
     }
 }
Exemple #28
0
        public bool Remove(KeyValuePair <long, TValue> item)
        {
            KeyWalker kw    = Encode(item.Key);
            KeyWalker kw2   = kw;
            TValue    value = default(TValue);

            if (Find(ref kw, ref value) && DefaultComparer.Compare(value, item.Value) == 0)
            {
                return(Remove(ref kw2, ref value));
            }
            return(false);
        }
Exemple #29
0
        public LongEnumerator FindExact(ulong key)
        {
            KeyWalker      kw = Encode(key);
            LongEnumerator e  = new LongEnumerator(this);

            if (!base.Find(ref kw, e))
            {
                return(null);
            }
            Debug.Assert(e.IsValid);
            return(e);
        }
Exemple #30
0
        public Enumerator FindExact(byte[] key)
        {
            KeyWalker  kw = new KeyWalker(key, key.Length);
            Enumerator e  = new Enumerator(this);

            if (!base.Find(ref kw, e))
            {
                return(null);
            }
            Debug.Assert(e.IsValid);
            return(e);
        }