private void Compact(CharVector kx, TernaryTree map, char p) { int k; if (p == 0) { return; } if (sc[p] == 0xFFFF) { k = map.Find(kv.Arr, lo[p]); if (k < 0) { k = kx.Alloc(Strlen(kv.Arr, lo[p]) + 1); Strcpy(kx.Arr, k, kv.Arr, lo[p]); map.Insert(kx.Arr, k, (char)k); } lo[p] = (char)k; } else { Compact(kx, map, lo[p]); if (sc[p] != 0) { Compact(kx, map, eq[p]); } Compact(kx, map, hi[p]); } }
private void compact(CharVector kx, TernaryTree map, char p) { int k; if (p == 0) { return; } if (Sc[p] == 0xFFFF) { k = map.Find(Kv.Arr, Lo[p]); if (k < 0) { k = kx.Alloc(Strlen(Kv.Arr, Lo[p]) + 1); Strcpy(kx.Arr, k, Kv.Arr, Lo[p]); map.Insert(kx.Arr, k, (char)k); } Lo[p] = (char)k; } else { compact(kx, map, Lo[p]); if (Sc[p] != 0) { compact(kx, map, Eq[p]); } compact(kx, map, Hi[p]); } }
/** * Each node stores a character (splitchar) which is part of * some Key(s). In a compressed branch (one that only contain * a single string key) the trailer of the key which is not * already in nodes is stored externally in the kv array. * As items are inserted, key substrings decrease. * Some substrings may completely disappear when the whole * branch is totally decompressed. * The tree is traversed to find the key substrings actually * used. In addition, duplicate substrings are removed using * a map (implemented with a TernaryTree!). * */ virtual public void TrimToSize() { // first balance the tree for best performance Balance(); // redimension the node arrays RedimNodeArrays(freenode); // ok, compact kv array CharVector kx = new CharVector(); kx.Alloc(1); TernaryTree map = new TernaryTree(); Compact(kx, map, root); kv = kx; kv.TrimToSize(); }
/// <summary> /// Each node stores a character (splitchar) which is part of /// some Key(s). In a compressed branch (one that only contain /// a single string key) the trailer of the key which is not /// already in nodes is stored externally in the kv array. /// As items are inserted, key substrings decrease. /// Some substrings may completely disappear when the whole /// branch is totally decompressed. /// The tree is traversed to find the key substrings actually /// used. In addition, duplicate substrings are removed using /// a map (implemented with a TernaryTree!). /// </summary> public void TrimToSize() { // first balance the tree for best performance Balance(); // redimension the node arrays redimNodeArrays(Freenode); // ok, compact kv array var kx = new CharVector(); kx.Alloc(1); var map = new TernaryTree(); compact(kx, map, Root); Kv = kx; Kv.TrimToSize(); }
private void Compact(CharVector kx, TernaryTree map, char p) { int k; if (p == 0) return; if (sc[p] == 0xFFFF) { k = map.Find(kv.Arr, lo[p]); if (k < 0) { k = kx.Alloc(Strlen(kv.Arr, lo[p]) + 1); Strcpy(kx.Arr, k, kv.Arr, lo[p]); map.Insert(kx.Arr, k, (char)k); } lo[p] = (char)k; } else { Compact(kx, map, lo[p]); if (sc[p] != 0) Compact(kx, map, eq[p]); Compact(kx, map, hi[p]); } }
/** * The actual insertion function, recursive version. */ private char Insert(char p, char[] key, int start, char val) { int len = Strlen(key, start); if (p == 0) { // this means there is no branch, this node will start a new branch. // Instead of doing that, we store the key somewhere else and create // only one node with a pointer to the key p = freenode++; eq[p] = val; // holds data length++; hi[p] = (char)0; if (len > 0) { sc[p] = (char)0xFFFF; // indicates branch is compressed lo[p] = (char)kv.Alloc(len + 1); // use 'lo' to hold pointer to key Strcpy(kv.Arr, lo[p], key, start); } else { sc[p] = (char)0; lo[p] = (char)0; } return(p); } if (sc[p] == 0xFFFF) { // branch is compressed: need to decompress // this will generate garbage in the external key array // but we can do some garbage collection later char pp = freenode++; lo[pp] = lo[p]; // previous pointer to key eq[pp] = eq[p]; // previous pointer to data lo[p] = (char)0; if (len > 0) { sc[p] = kv[lo[pp]]; eq[p] = pp; lo[pp]++; if (kv[lo[pp]] == 0) { // key completly decompressed leaving garbage in key array lo[pp] = (char)0; sc[pp] = (char)0; hi[pp] = (char)0; } else { sc[pp] = (char)0xFFFF; // we only got first char of key, rest is still there } } else { // In this case we can save a node by swapping the new node // with the compressed node sc[pp] = (char)0xFFFF; hi[p] = pp; sc[p] = (char)0; eq[p] = val; length++; return(p); } } char s = key[start]; if (s < sc[p]) { lo[p] = Insert(lo[p], key, start, val); } else if (s == sc[p]) { if (s != 0) { eq[p] = Insert(eq[p], key, start + 1, val); } else { // key already in tree, overwrite data eq[p] = val; } } else { hi[p] = Insert(hi[p], key, start, val); } return(p); }