Ejemplo n.º 1
0
        private void Rehash()
        {
            PagedGrowableWriter oldTable = table;

            table = new PagedGrowableWriter(2 * oldTable.Size(), 1 << 30, PackedInts.BitsRequired(count), PackedInts.COMPACT);
            mask  = table.Size() - 1;
            for (long idx = 0; idx < oldTable.Size(); idx++)
            {
                long address = oldTable.Get(idx);
                if (address != 0)
                {
                    AddNew(address);
                }
            }
        }
Ejemplo n.º 2
0
        public long Add(Builder <T> .UnCompiledNode <T> nodeIn)
        {
            //System.out.println("hash: add count=" + count + " vs " + table.size() + " mask=" + mask);
            long h   = Hash(nodeIn);
            long pos = h & mask;
            int  c   = 0;

            while (true)
            {
                long v = table.Get(pos);
                if (v == 0)
                {
                    // freeze & add
                    long node = fst.AddNode(nodeIn);
                    //System.out.println("  now freeze node=" + node);
                    long hashNode = Hash(node);
                    Debug.Assert(hashNode == h, "frozenHash=" + hashNode + " vs h=" + h);
                    count++;
                    table.Set(pos, node);
                    // Rehash at 2/3 occupancy:
                    if (count > 2 * table.Size() / 3)
                    {
                        Rehash();
                    }
                    return(node);
                }
                else if (NodesEqual(nodeIn, v))
                {
                    // same node is already here
                    return(v);
                }

                // quadratic probe
                pos = (pos + (++c)) & mask;
            }
        }