package com.ankurdave.part;

class ArtNode48 extends ArtNode {
    public static int count;

    public ArtNode48() {
        super();
        count++;
    }

    public ArtNode48(final ArtNode48 other) {
        super(other);
        System.arraycopy(other.keys, 0, keys, 0, 256);
        for (int i = 0; i < other.num_children; i++) {
            children[i] = other.children[i];
            children[i].refcount++;
        }
        count++;
    }

    public ArtNode48(final ArtNode16 other) {
        this();
        // ArtNode
        this.num_children = other.num_children;
        this.partial_len = other.partial_len;
        System.arraycopy(other.partial, 0, this.partial, 0,
                         Math.min(MAX_PREFIX_LEN, this.partial_len));

        // ArtNode48 from ArtNode16
        for (int i = 0; i < this.num_children; i++) {
            keys[to_uint(other.keys[i])] = (byte)(i + 1);
            children[i] = other.children[i];
            children[i].refcount++;
        }
    }

    @Override public Node n_clone() {
        return new ArtNode48(this);
    }

    @Override public ChildPtr find_child(byte c) {
        int idx = to_uint(keys[to_uint(c)]);
        if (idx != 0) return new ArrayChildPtr(children, idx - 1);
        return null;
    }

    @Override public Leaf minimum() {
        int idx = 0;
        while (keys[idx] == 0) idx++;
        Node child = children[to_uint(keys[idx]) - 1];
        return Node.minimum(child);
     }

    @Override public void add_child(ChildPtr ref, byte c, Node child, boolean force_clone) {
        if (this.num_children < 28) {
            int pos = this.num_children;

            ArtNode48 target = force_clone ? new ArtNode48(this) : this;

            target.children[pos] = child;
            child.refcount++;
            target.keys[to_uint(c)] = (byte)(pos + 1);
            target.num_children++;

            if (force_clone) {
                // Update the parent pointer to the new node
                ref.change(target);
            }
        } else {
            // Copy the node48 into a new node256
            ArtNode256 result = new ArtNode256(this);
            // Update the parent pointer to the node256
            ref.change(result);
            // Insert the element into the node256 instead
            result.add_child(ref, c, child, false);
        }
    }

    @Override public void iter(IterCallback cb) {
        for (int i = 0; i < 256; i++) {
            int idx = to_uint(keys[i]);
            if (idx != 0) {
                Node.iter(children[idx - 1], cb);
            }
        }
    }

    @Override public int decrement_refcount() {
        if (--this.refcount <= 0) {
            int freed = 0;
            for (int i = 0; i < this.num_children; i++) {
                freed += Node.decrement_refcount(children[i]);
            }
            count--;
            // delete this;
            return freed + 728;
            // object size (8) + refcount (4) +
            // num_children int (4) + partial_len int (4) +
            // pointer to partial array (8) + partial array size (8+4+1*MAX_PREFIX_LEN)
            // pointer to key array (8) + key array size (8+4+1*256) +
            // pointer to children array (8) + children array size (8+4+8*48)
        }
        return 0;
    }

    byte[] keys = new byte[256];
    Node[] children = new Node[48];
}

@@ -1590,17 +1590,17 @@
 ldren %3C 
-2
+4
 8) %7B%0A   

Fix a bug