org.javimmutable.collections.tree.TreeNode Maven / Gradle / Ivy
///###////////////////////////////////////////////////////////////////////////
//
// Burton Computer Corporation
// http://www.burton-computer.com
//
// Copyright (c) 2014, 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.tree;
import org.javimmutable.collections.Cursor;
import org.javimmutable.collections.Cursorable;
import org.javimmutable.collections.Holder;
import org.javimmutable.collections.JImmutableMap;
import org.javimmutable.collections.common.MutableDelta;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import java.util.Collection;
import java.util.Comparator;
/**
* Abstract base class for 2-3 tree nodes. Provides public methods for searching and modifying
* the tree and package private methods used to implement the public methods.
*
* @param
* @param
*/
@Immutable
public abstract class TreeNode
implements Cursorable>
{
public static TreeNode of()
{
return EmptyNode.of();
}
/**
* Return the value matching key or defaultValue if no match is found. Searches this node
* and its appropriate children.
*
* @param comparator
* @param key
* @param defaultValue
* @return
*/
public abstract V getValueOr(Comparator comparator,
K key,
V defaultValue);
/**
* Return a (possibly empty) Holder containing the value matching key. Searches this node
* and its appropriate children.
*
* @param comparator
* @param key
* @return
*/
public abstract Holder find(Comparator comparator,
K key);
/**
* Return a (possibly empty) Holder containing the an Entry matching key. Searches this node
* and its appropriate children.
*
* @param comparator
* @param key
* @return
*/
public abstract Holder> findEntry(Comparator comparator,
K key);
/**
* Adds this node's value and all of its children's value to the collection.
*
* @param collection
*/
public abstract void addEntriesTo(Collection> collection);
/**
* Returns a Cursor visiting all entries in sorted order.
*
* @return
*/
@Nonnull
public abstract Cursor> cursor();
/**
* Assign the specified value to the specified key. Returns a node (possibly this same node)
* reflecting the assignment and updates sizeDelta with the change in size (if any).
*
* @param comparator
* @param key
* @param value
* @param sizeDelta
* @return
*/
public TreeNode assign(Comparator comparator,
K key,
V value,
MutableDelta sizeDelta)
{
UpdateResult result = assignImpl(comparator, key, value);
switch (result.type) {
case UNCHANGED:
return this;
case INPLACE:
sizeDelta.add(result.sizeDelta);
return result.newNode;
case SPLIT:
sizeDelta.add(result.sizeDelta);
return new TwoNode(result.newNode,
result.extraNode,
result.newNode.getMaxKey(),
result.extraNode.getMaxKey());
}
throw new RuntimeException();
}
/**
* Deletes the specified key. Returns a node (possibly this same node)
* reflecting the deletion and updates sizeDelta with the change in size (if any).
*
* @param comparator
* @param key
* @param sizeDelta
* @return
*/
public TreeNode delete(Comparator comparator,
K key,
MutableDelta sizeDelta)
{
DeleteResult result = deleteImpl(comparator, key);
switch (result.type) {
case UNCHANGED:
return this;
default:
sizeDelta.subtract(1);
return result.node;
}
}
/**
* Return true if this node contains no children or value.
*
* @return
*/
public boolean isEmpty()
{
return false;
}
abstract int verifyDepthsMatch();
abstract K getMaxKey();
abstract UpdateResult assignImpl(Comparator comparator,
K key,
V value);
abstract DeleteResult deleteImpl(Comparator comparator,
K key);
abstract DeleteMergeResult leftDeleteMerge(TreeNode node);
abstract DeleteMergeResult rightDeleteMerge(TreeNode node);
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy