/// <summary> /// Associates the specified value with the specified key. /// </summary> /// <param name="key">A key to find or create; if key.Offset > 0, bytes /// before that offset are ignored.</param> /// <param name="value">Value to assign to the node, depending on the value /// of mode. On return, value is set to the previous value for the given key.</param> /// <param name="mode">Specifies whether to create an entry if the key is /// not present, and whether to change an existing entry. If mode is Find, /// Set() only retrieves an existing value; it does not change the trie.</param> /// <returns>Returns true if the specified key already existed and false if /// it did not.</returns> protected bool Set(ref KeyWalker key, ref T value, CPMode mode) { if (_head != null) { bool existed = _head.Set(ref key, ref value, ref _head, mode); if (!existed && (mode & CPMode.Create) != (CPMode)0) { _count++; } return(existed); } else if ((mode & CPMode.Create) != (CPMode)0) { Debug.Assert(_count == 0); _head = new CPSNode <T>(ref key, value); _count = 1; } return(false); }
// 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);
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); } } }
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); }