
org.javimmutable.collections.hash.collision_map.TreeCollisionMap Maven / Gradle / Ivy
///###////////////////////////////////////////////////////////////////////////
//
// Burton Computer Corporation
// http://www.burton-computer.com
//
// Copyright (c) 2018, 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.collision_map;
import org.javimmutable.collections.Cursor;
import org.javimmutable.collections.Func1;
import org.javimmutable.collections.Holder;
import org.javimmutable.collections.Holders;
import org.javimmutable.collections.JImmutableMap;
import org.javimmutable.collections.SplitableIterator;
import org.javimmutable.collections.common.MutableDelta;
import org.javimmutable.collections.tree.BranchNode;
import org.javimmutable.collections.tree.ComparableComparator;
import org.javimmutable.collections.tree.LeafNode;
import org.javimmutable.collections.tree.Node;
import org.javimmutable.collections.tree.UpdateResult;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import java.util.Comparator;
/**
* Transforms implementation that stores values in Node objects (b-trees trees).
* Usable with keys that implement Comparable. Will fail with any other
* type of key.
*/
@Immutable
public class TreeCollisionMap, V>
implements CollisionMap, K, V>
{
private final Comparator comparator = ComparableComparator.of();
@Nonnull
@Override
public Node update(Node leaf,
@Nonnull K key,
V value,
@Nonnull MutableDelta delta)
{
if (leaf == null) {
delta.add(1);
return new LeafNode<>(key, value);
} else {
return resultForUpdate(leaf, delta, leaf.assign(comparator, key, value));
}
}
@Nonnull
@Override
public Node update(@Nullable Node leaf,
@Nonnull K key,
@Nonnull Func1, V> generator,
@Nonnull MutableDelta delta)
{
if (leaf == null) {
delta.add(1);
return new LeafNode<>(key, generator.apply(Holders.of()));
} else {
return resultForUpdate(leaf, delta, leaf.update(comparator, key, generator));
}
}
@Override
public Node delete(@Nonnull Node leaf,
@Nonnull K key,
@Nonnull MutableDelta delta)
{
final Node newLeaf = leaf.delete(comparator, key);
if (newLeaf == leaf) {
return leaf;
} else {
delta.add(-1);
return newLeaf.isEmpty() ? null : newLeaf.compress();
}
}
@Override
public V getValueOr(@Nonnull Node leaf,
@Nonnull K key,
V defaultValue)
{
return leaf.getValueOr(comparator, key, defaultValue);
}
@Override
public Holder findValue(@Nonnull Node leaf,
@Nonnull K key)
{
return leaf.find(comparator, key);
}
@Override
public Holder> findEntry(@Nonnull Node leaf,
@Nonnull K key)
{
return leaf.findEntry(comparator, key);
}
@Override
public Cursor> cursor(@Nonnull Node leaf)
{
return leaf.cursor();
}
@Override
public SplitableIterator> iterator(@Nonnull Node leaf)
{
return leaf.iterator();
}
private Node resultForUpdate(Node leaf,
@Nonnull MutableDelta delta,
UpdateResult result)
{
switch (result.type) {
case UNCHANGED:
return leaf;
case INPLACE:
delta.add(result.sizeDelta);
return result.newNode;
case SPLIT:
delta.add(result.sizeDelta);
return new BranchNode<>(result.newNode, result.extraNode);
default:
throw new IllegalStateException("unknown UpdateResult.Type value");
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy