org.javimmutable.collections.hash.hamt.HamtBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javimmutable-collections Show documentation
Show all versions of javimmutable-collections Show documentation
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.
///###////////////////////////////////////////////////////////////////////////
//
// Burton Computer Corporation
// http://www.burton-computer.com
//
// Copyright (c) 2019, 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.hamt;
import org.javimmutable.collections.common.CollisionMap;
import org.javimmutable.collections.list.ListCollisionMap;
import org.javimmutable.collections.tree.TreeCollisionMap;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;
import static org.javimmutable.collections.hash.hamt.HamtBranchNode.*;
@NotThreadSafe
public class HamtBuilder
{
private CollisionMap collisionMap = ListCollisionMap.instance();
private Node root = new Empty<>();
@Nonnull
public HamtNode build()
{
return root.toHamt(collisionMap);
}
public void add(@Nonnull K key,
V value)
{
if (root.isEmpty()) {
collisionMap = selectCollisionMapForKey(key);
root = new Leaf<>(collisionMap, key.hashCode(), key, value);
} else {
root = root.add(collisionMap, key.hashCode(), key, value);
}
}
@Nonnull
public CollisionMap getCollisionMap()
{
return collisionMap;
}
public int size()
{
return root.size();
}
private static CollisionMap selectCollisionMapForKey(@Nonnull K key)
{
if (key instanceof Comparable) {
return TreeCollisionMap.instance();
} else {
return ListCollisionMap.instance();
}
}
private static abstract class Node
{
@Nonnull
abstract Node add(@Nonnull CollisionMap collisionMap,
int hashCode,
@Nonnull K key,
@Nullable V value);
@Nonnull
abstract HamtNode toHamt(@Nonnull CollisionMap collisionMap);
abstract int size();
abstract boolean isEmpty();
}
private static class Empty
extends Node
{
@Nonnull
@Override
Node add(@Nonnull CollisionMap collisionMap,
int hashCode,
@Nonnull K key,
@Nullable V value)
{
return new Leaf<>(collisionMap, hashCode, key, value);
}
@Nonnull
@Override
HamtNode toHamt(@Nonnull CollisionMap collisionMap)
{
return HamtEmptyNode.of();
}
@Override
int size()
{
return 0;
}
@Override
boolean isEmpty()
{
return true;
}
}
private static class Leaf
extends Node
{
private CollisionMap.Node values;
private int hashCode;
private int size;
private Leaf(@Nonnull Leaf other)
{
this.values = other.values;
this.hashCode = other.hashCode >>> SHIFT;
size = other.size;
}
private Leaf(@Nonnull CollisionMap collisionMap,
int hashCode,
@Nonnull K key,
@Nullable V value)
{
this.values = collisionMap.update(collisionMap.emptyNode(), key, value);
this.hashCode = hashCode;
size = 1;
}
@Nonnull
@Override
Node add(@Nonnull CollisionMap collisionMap,
int hashCode,
@Nonnull K key,
@Nullable V value)
{
if (hashCode == this.hashCode) {
values = collisionMap.update(values, key, value);
size = collisionMap.size(values);
return this;
} else {
return new Branch<>(collisionMap, this, hashCode, key, value);
}
}
@Nonnull
@Override
HamtNode toHamt(@Nonnull CollisionMap collisionMap)
{
return new HamtLeafNode<>(hashCode, values);
}
@Override
int size()
{
return size;
}
@Override
boolean isEmpty()
{
return false;
}
}
private static class Branch
extends Node
{
private CollisionMap.Node values;
private Node[] children;
private int size;
private Branch(@Nonnull CollisionMap collisionMap,
@Nonnull Leaf leaf,
int hashCode,
@Nonnull K key,
V value)
{
assert hashCode != leaf.hashCode;
children = new Node[32];
if (leaf.hashCode == 0) {
values = leaf.values;
} else {
values = collisionMap.emptyNode();
children[leaf.hashCode & MASK] = new Leaf<>(leaf);
}
size = leaf.size;
add(collisionMap, hashCode, key, value);
}
@Nonnull
@Override
Node add(@Nonnull CollisionMap collisionMap,
int hashCode,
@Nonnull K key,
@Nullable V value)
{
if (hashCode == 0) {
int oldSize = collisionMap.size(values);
values = collisionMap.update(values, key, value);
size = size - oldSize + collisionMap.size(values);
} else {
final int index = hashCode & MASK;
final Node child = children[index];
if (child == null) {
children[index] = new Leaf<>(collisionMap, hashCode >>> SHIFT, key, value);
size += 1;
} else {
int oldSize = children[index].size();
children[index] = child.add(collisionMap, hashCode >>> SHIFT, key, value);
size = size - oldSize + children[index].size();
}
}
assert invariant(collisionMap);
return this;
}
@Nonnull
@Override
HamtNode toHamt(@Nonnull CollisionMap collisionMap)
{
int count = 0;
for (Node child : children) {
if (child != null) {
count += 1;
}
}
int bitmask = 0;
int bit = 1;
final HamtNode[] nodes = new HamtNode[count];
int index = 0;
for (Node child : children) {
if (child != null) {
final HamtNode hamt = child.toHamt(collisionMap);
nodes[index++] = hamt;
bitmask |= bit;
}
bit <<= 1;
}
return new HamtBranchNode<>(bitmask, values, nodes, size);
}
@Override
int size()
{
return size;
}
@Override
boolean isEmpty()
{
return false;
}
private boolean invariant(@Nonnull CollisionMap collisionMap)
{
final int valuesSize = collisionMap.size(values);
int childCount = 0;
int leafCount = 0;
for (Node child : children) {
if (child != null) {
childCount += 1;
if (child instanceof Leaf) {
leafCount += 1;
}
}
}
return (childCount > 0) && (valuesSize > 0 || childCount > 1 || leafCount == 0);
}
}
}