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); } } }
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); } } }
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(); } } } }
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++; } }
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); }