alloc() public method

public alloc ( int size ) : int
size int
return int
Exemplo n.º 1
0
        private void compact(CharVector kx, TernaryTree map, char p)
        {
            int k;

            if (p == 0)
            {
                return;
            }
            if (sc[p] == 0xFFFF)
            {
                k = map.find(kv.Array, lo[p]);
                if (k < 0)
                {
                    k = kx.alloc(strlen(kv.Array, lo[p]) + 1);
                    strcpy(kx.Array, k, kv.Array, lo[p]);
                    map.insert(kx.Array, 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]);
            }
        }
Exemplo n.º 2
0
        /// <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 virtual 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();
        }
Exemplo n.º 3
0
        /// <summary>
        /// The actual insertion function, recursive version.
        /// </summary>
        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.Array, 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.get(lo[pp]);
                    eq[p] = pp;
                    lo[pp]++;
                    if (kv.get(lo[pp]) == 0)
                    {
                        // key completly decompressed leaving garbage in key array
                        lo[pp] = (char)0;
                        sc[pp] = (char)0;
                        hi[pp] = (char)0;
                    }
                    else
                    {
                        // we only got first char of key, rest is still there
                        sc[pp] = (char)0xFFFF;
                    }
                }
                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);
        }