private void Add(IList <elementType> key, resultType value, int keyPos) { int size = key.Count - keyPos; if (size > 0) { //add the node needed if nessesary if (!nodes.ContainsKey(key[0])) { DSTNode <elementType, resultType> next = new DSTNode <elementType, resultType>(); next.init(); nodes.Add(key[0], next); } nodes[key[0]].Add(key, value, keyPos++); } else { if (result == null) { result = new List <resultType>(); result.Add(value); count++; } else { throw new ArgumentException("An element with the same key already exists in the DST."); } } }
/// <summary> /// Get the item at an index. /// NOTE: this should be the only way to map a index to an item. /// All operations that are index bound must call this. This insures that there is only one key to item mapping in place. /// </summary> /// <param name="index">The index.</param> /// <param name="count">The count.</param> /// <param name="key">The key.</param> /// <param name="value">The value.</param> /// <returns></returns> private bool ItemAtIndex(int index, int count, ref LinkedList <elementType> key, out resultType value) { int c = count; Dictionary <elementType, DSTNode <elementType, resultType> > .KeyCollection keyList = nodes.Keys; List <elementType> sortedKeyList = new List <elementType>(keyList); //keyList.CopyTo(sortedKeyList, 0); sortedKeyList.Sort(); foreach (elementType keyItem in sortedKeyList) { DSTNode <elementType, resultType> n = nodes[keyItem]; if ((c + n.count) >= index) { if (count == index) { //found a node whose final child is the one. if (result != null) { value = this.result[0]; return(true); } } if (ItemAtIndex(index, c, ref key, out value)) { key.AddFirst(keyItem); return(true); } } c += n.count; } value = default(resultType); return(false); }
public DST() { rootNode = new DSTNode <elementType, resultType>(); rootNode.init(); }