Exemplo n.º 1
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);
        }
Exemplo n.º 2
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);
                }
            }
        }
Exemplo n.º 3
0
        public override bool Set(ref KeyWalker key, ref T value, ref CPNode <T> self, CPMode mode)
        {
            CheckValidity();

            if (key.Left > 0)
            {
                int i = key[0] >> 5;
                if (_children[i] != null)
                {
                    CPSNode <T> child  = _children[i];
                    CPNode <T>  child2 = child;
                    _localCount -= child.LocalCount;
                    bool existed = child.Set(ref key, ref value, ref child2, mode | CPMode.FixedStructure);
                    _localCount += child.LocalCount;
                    Debug.Assert(child == child2);
                    return(existed);
                }
                else
                {
                    if ((mode & CPMode.Create) != (CPMode)0)
                    {
                        _children[i] = new CPSNode <T>(ref key, value);
                        _localCount++;
                    }
                    return(false);
                }
            }
            else
            {
                // key.Left == 0
                if (_zlk == NoZLK)
                {
                    if ((mode & CPMode.Create) != (CPMode)0)
                    {
                        _zlk = value;
                        _localCount++;
                    }
                    return(false);
                }
                else
                {
                    T oldValue = (T)_zlk;
                    if ((mode & CPMode.Set) != (CPMode)0)
                    {
                        _zlk = value;
                    }
                    value = oldValue;
                    return(true);
                }
            }
        }
Exemplo n.º 4
0
        protected TValue GetValue(ref KeyWalker key, ulong keyI)
        {
            CPNode <TValue> head = Head;

            if (head != null)
            {
                TValue value = default(TValue);
                if (head.Set(ref key, ref value, ref head, CPMode.Find))
                {
                    return(value);
                }
            }
            throw new KeyNotFoundException(Localize.Localized("Key not found: ") + keyI.ToString());
        }
Exemplo n.º 5
0
        private void ConvertToBOrSNode(ref CPNode <T> self, int extraCells)
        {
            if (_localCount < 32)
            {
                self = new CPSNode <T>(_localCount + extraCells);
            }
            else
            {
                self = new CPBNode <T>();
            }

            // Scan key-value pairs in this node
            KeyWalker kw = new KeyWalker(new byte[1], 1);

            for (int section = 0; section < 8; section++)
            {
                uint f = _flags[section];
                if (f == 0)
                {
                    continue;
                }
                for (int i = MathEx.FindFirstOne(f); i < 32; i++)
                {
                    if ((f & (1 << i)) != 0)                     // IsPresent(k)
                    {
                        // Get the key and value
                        int k = (section << 5) + i;
                        kw.Buffer[0] = (byte)k;

                        T value = default(T);
                        if (_values != null)
                        {
                            int P = GetValueIndex(k);
                            if (P < _values.Length)
                            {
                                value = _values[P];
                            }
                        }

                        // Assign them to the new node
                        bool existed = self.Set(ref kw, ref value, ref self, CPMode.Create | CPMode.FixedStructure);
                        Debug.Assert(!existed);
                        kw.Reset();
                    }
                }
            }
        }
Exemplo n.º 6
0
        public override bool Set(ref KeyWalker key, ref T value, ref CPNode <T> self, CPMode mode)
        {
            byte k = key.Buffer[key.Offset];

            if (key.Left == 1 && IsPresent(k))
            {
                T   newValue = value;
                int P        = GetValueIndex(k);
                if (P < 0x100 && P < _values.Length)
                {
                    value = _values[P];
                    if ((mode & CPMode.Set) != (CPMode)0)
                    {
                        _values[P] = newValue;
                    }
                }
                else
                {
                    value = default(T);
                    if ((mode & CPMode.Set) != (CPMode)0)
                    {
                        Assign(k, newValue);
                    }
                }
                return(true);
            }
            else if ((mode & CPMode.Create) != (CPMode)0)
            {
                if (key.Left == 1)
                {
                    Assign(k, value);
                    _localCount++;
                }
                else
                {
                    // Must convert back to bitmap or sparse node!
                    ConvertToBOrSNode(ref self, key.Left / 3 + 1);
                    self.Set(ref key, ref value, ref self, mode);
                }
            }
            return(false);
        }
Exemplo n.º 7
0
        public override void AddChild(ref KeyWalker key, CPNode <T> value, ref CPNode <T> self)
        {
            Debug.Assert(key.Left > 0);
            int i = key[0] >> 5;

            if (_children[i] != null)
            {
                CPSNode <T> child  = _children[i];
                CPNode <T>  child2 = child;
                _localCount -= child.LocalCount;
                child.AddChild(ref key, value, ref child2);
                _localCount += child.LocalCount;
                Debug.Assert(child2 == child);
            }
            else
            {
                _children[i] = new CPSNode <T>(ref key, value);
                _localCount++;
            }
        }
Exemplo n.º 8
0
        private void ConvertToSNode(ref CPNode <T> self)
        {
            Debug.Assert(_localCount <= 32);
            int count = 0;             // debug check

            self = new CPSNode <T>(_localCount);
            for (int i = 0; i < _children.Length; i++)
            {
                if (_children[i] != null)
                {
                    count += _children[i].LocalCount;
                    _children[i].MoveAllTo(self);
                }
            }
            if (_zlk != NoZLK)
            {
                KeyWalker kw    = new KeyWalker(EmptyArray <byte> .Value, 0);
                T         value = (T)_zlk;
                G.Verify(!self.Set(ref kw, ref value, ref self, CPMode.Create));
                count++;
            }
            Debug.Assert(count == _localCount);
        }
Exemplo n.º 9
0
 // Returns true if key formerly existed
 public abstract bool Remove(ref KeyWalker key, ref T oldValue, ref CPNode <T> self);
Exemplo n.º 10
0
 // Associates the specified node with a given key. AddChild() requires
 // that the specified key does not exist already.
 public abstract void AddChild(ref KeyWalker key, CPNode <T> value, ref CPNode <T> self);
Exemplo n.º 11
0
 // Returns true if key already existed. Can be used to find rather than
 // create or set a value (mode==JPMode.Find), if the caller just wants
 // the value and not an enumerator. If the key already existed, this
 // method sets value to the original value associated with the key.
 public abstract bool Set(ref KeyWalker key, ref T value, ref CPNode <T> self, CPMode mode);
Exemplo n.º 12
0
 public Entry(CPNode <T> node, int index, int keyOffset)
 {
     Node = node; Index = index; KeyOffset = keyOffset;
 }
Exemplo n.º 13
0
 public override void AddChild(ref KeyWalker key, CPNode <T> value, ref CPNode <T> self)
 {
     // Must convert back to bitmap or sparse node!
     ConvertToBOrSNode(ref self, key.Left / 3 + 1);
     self.AddChild(ref key, value, ref self);
 }