All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.javimmutable.collections.hash.HashInteriorNode Maven / Gradle / Ivy

Go to download

Library providing immutable/persistent collection classes for Java. While collections are immutable they provide methods for adding and removing values by creating new modified copies of themselves. Each copy shares almost all of its structure with other copies to minimize memory consumption.

There is a newer version: 3.2.1
Show newest version
///###////////////////////////////////////////////////////////////////////////
//
// Burton Computer Corporation
// http://www.burton-computer.com
//
// Copyright (c) 2013, Burton Computer Corporation
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//     Redistributions of source code must retain the above copyright
//     notice, this list of conditions and the following disclaimer.
//
//     Redistributions in binary form must reproduce the above copyright
//     notice, this list of conditions and the following disclaimer in
//     the documentation and/or other materials provided with the
//     distribution.
//
//     Neither the name of the Burton Computer Corporation nor the names
//     of its contributors may be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package org.javimmutable.collections.hash;

import org.javimmutable.collections.Cursor;
import org.javimmutable.collections.Holder;
import org.javimmutable.collections.Holders;
import org.javimmutable.collections.JImmutableMap;
import org.javimmutable.collections.array.bit32.Bit32Array;
import org.javimmutable.collections.common.MutableDelta;
import org.javimmutable.collections.cursors.LazyCursor;
import org.javimmutable.collections.cursors.MultiCursor;
import org.javimmutable.collections.cursors.MultiTransformCursor;
import org.javimmutable.collections.cursors.TransformCursor;

/**
 * Full HashTrieNode implementation for up to 32 subbranches and 32 values.  Uses Bit32Arrays for branches
 * and values to minimize memory consumption.
 *
 * @param 
 * @param 
 */
public class HashInteriorNode
        extends AbstractHashTrieNode
{
    private final Bit32Array> branches;
    private final Bit32Array> values;

    public HashInteriorNode()
    {
        this(Bit32Array.>of(), Bit32Array.>of());
    }

    public HashInteriorNode(Bit32Array> values)
    {
        this(Bit32Array.>of(), values);
    }

    public HashInteriorNode(int branchIndex,
                            int valueIndex,
                            HashTrieValue value)
    {
        if (branchIndex == 0) {
            this.branches = Bit32Array.of();
            this.values = Bit32Array.>of().assign(valueIndex, value);
        } else {
            this.branches = Bit32Array.>of().assign(branchIndex & 0x1f, new HashQuickNode(branchIndex >>> 5, valueIndex, value));
            this.values = Bit32Array.of();
        }
    }

    public HashInteriorNode(Bit32Array> branches,
                            Bit32Array> values)
    {
        this.branches = branches;
        this.values = values;
    }

    @Override
    public Holder get(int branchIndex,
                         int valueIndex,
                         K key)
    {
        HashTrieValue value = getTrieValue(branchIndex, valueIndex);
        return value != null ? value.getValueForKey(key) : Holders.of();
    }

    @Override
    public JImmutableMap.Entry getEntry(int branchIndex,
                                              int valueIndex,
                                              K key)
    {
        HashTrieValue value = getTrieValue(branchIndex, valueIndex);
        return value != null ? value.getEntryForKey(key) : null;
    }

    @Override
    public HashTrieValue getTrieValue(int branchIndex,
                                            int valueIndex)
    {
        if (branchIndex == 0) {
            return values.get(valueIndex).getValueOrNull();
        } else {
            HashTrieNode branch = branches.get(branchIndex & 0x1f).getValueOrNull();
            return (branch != null) ? branch.getTrieValue(branchIndex >>> 5, valueIndex) : null;
        }
    }

    @Override
    public HashTrieNode assign(int branchIndex,
                                     int valueIndex,
                                     K key,
                                     V value,
                                     MutableDelta sizeDelta)
    {
        final Bit32Array> branches = this.branches;
        final Bit32Array> values = this.values;
        if (branchIndex == 0) {
            HashTrieValue valueNode = values.get(valueIndex).getValueOrNull();
            if (valueNode != null) {
                HashTrieValue newValueNode = valueNode.setValueForKey(key, value, sizeDelta);
                return (newValueNode == valueNode) ? this : new HashInteriorNode(branches, values.assign(valueIndex, newValueNode));
            } else {
                sizeDelta.add(1);
                return new HashInteriorNode(branches, values.assign(valueIndex, new HashTrieSingleValue(key, value)));
            }
        } else {
            final int childIndex = branchIndex & 0x1f;
            HashTrieNode child = branches.get(childIndex).getValueOrNull();
            HashTrieNode newChild;
            if (child == null) {
                sizeDelta.add(1);
                newChild = new HashQuickNode(branchIndex >>> 5, valueIndex, new HashTrieSingleValue(key, value));
            } else {
                newChild = child.assign(branchIndex >>> 5, valueIndex, key, value, sizeDelta);
            }
            return (newChild == child) ? this : new HashInteriorNode(branches.assign(childIndex, newChild), values);
        }
    }

    @Override
    public HashTrieNode delete(int branchIndex,
                                     int valueIndex,
                                     K key,
                                     MutableDelta sizeDelta)
    {
        final Bit32Array> branches = this.branches;
        final Bit32Array> values = this.values;
        if (branchIndex == 0) {
            final HashTrieValue currentValue = values.get(valueIndex).getValueOrNull();
            if (currentValue == null) {
                return this;
            } else {
                HashTrieValue newValue = currentValue.deleteValueForKey(key, sizeDelta);
                if (newValue == currentValue) {
                    return this;
                } else if (newValue == null) {
                    return deleteConsolidationImpl(branches, values.delete(valueIndex));
                } else {
                    return new HashInteriorNode(branches, values.assign(valueIndex, newValue));
                }
            }
        } else {
            final int nodeIndex = branchIndex & 0x1f;
            HashTrieNode currentNode = branches.get(nodeIndex).getValueOrNull();
            if (currentNode == null) {
                return this;
            } else {
                final HashTrieNode newNode = currentNode.delete(branchIndex >>> 5, valueIndex, key, sizeDelta);
                if (newNode == currentNode) {
                    return this;
                } else if (newNode.shallowSize() == 0) {
                    return deleteConsolidationImpl(branches.delete(nodeIndex), values);
                } else {
                    return new HashInteriorNode(branches.assign(nodeIndex, newNode), values);
                }
            }
        }
    }

    @Override
    public Cursor> cursor()
    {
        final Cursor> valuesCursor = TransformCursor.ofValues(LazyCursor.of(values));
        final Cursor> branchesCursor = MultiTransformCursor.of(TransformCursor.ofValues(LazyCursor.of(branches)), HashTrieNodeToValueCursorFunc.of());
        return MultiCursor.of(valuesCursor, branchesCursor);
    }

    @Override
    public int shallowSize()
    {
        return values.size() + branches.size();
    }

    @Override
    public int deepSize()
    {
        int total = 0;
        for (JImmutableMap.Entry> branch : branches) {
            total += branch.getValue().deepSize();
        }
        for (JImmutableMap.Entry> value : values) {
            total += value.getValue().size();
        }
        return total;
    }

    @Override
    public JImmutableMap getNodeTypeCounts(JImmutableMap map)
    {
        map = super.getNodeTypeCounts(map);
        for (JImmutableMap.Entry> branch : branches) {
            map = branch.getValue().getNodeTypeCounts(map);
        }
        return map;
    }

    private HashTrieNode deleteConsolidationImpl(Bit32Array> branches,
                                                       Bit32Array> values)
    {
        if (branches.size() == 0) {
            switch (values.size()) {
            case 0:
                return HashEmptyNode.of();
            case 1:
                final int remainingIndex = values.firstIndex();
                return new HashQuickNode(0, remainingIndex, values.get(remainingIndex).getValue());
            }
        }
        return new HashInteriorNode(branches, values);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy