org.javimmutable.collections.inorder.JImmutableInsertOrderMap 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) 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.inorder;
import org.javimmutable.collections.Cursor;
import org.javimmutable.collections.Func1;
import org.javimmutable.collections.Holder;
import org.javimmutable.collections.Holders;
import org.javimmutable.collections.JImmutableArray;
import org.javimmutable.collections.JImmutableMap;
import org.javimmutable.collections.MapEntry;
import org.javimmutable.collections.array.trie32.TrieArray;
import org.javimmutable.collections.common.AbstractJImmutableMap;
import org.javimmutable.collections.cursors.TransformCursor;
import org.javimmutable.collections.hash.JImmutableHashMap;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
/**
* JImmutableMap implementation that allows iteration over members in the order in which they
* were inserted into the map. Maintains two parallel data structures, one for sorting and
* the other for storing entries. Gets are approximately as fast as hash map gets but updates
* are significantly slower. Iteration is comparable to sorted map iteration.
*
* Use a hash or tree map whenever possible but this class performs well enough for most cases
* where insertion order is important to an algorithm.
*
* @param
* @param
*/
@Immutable
public class JImmutableInsertOrderMap
extends AbstractJImmutableMap
{
@SuppressWarnings("unchecked")
public static final JImmutableInsertOrderMap EMPTY = new JImmutableInsertOrderMap(TrieArray.of(), JImmutableHashMap.of(), 1);
private final JImmutableArray> sortedNodes;
private final JImmutableMap> hashedNodes;
private final int nextIndex;
private JImmutableInsertOrderMap(JImmutableArray> sortedNodes,
JImmutableMap> hashedNodes,
int nextIndex)
{
assert sortedNodes.size() == hashedNodes.size();
this.sortedNodes = sortedNodes;
this.hashedNodes = hashedNodes;
this.nextIndex = nextIndex;
}
@SuppressWarnings("unchecked")
public static JImmutableInsertOrderMap of()
{
return (JImmutableInsertOrderMap)EMPTY;
}
@Override
public V getValueOr(K key,
V defaultValue)
{
final Node current = hashedNodes.get(key);
return (current != null) ? current.getValue() : defaultValue;
}
@Nonnull
@Override
public Holder find(@Nonnull K key)
{
final Node current = hashedNodes.get(key);
return (current != null) ? current : Holders.of();
}
@Nonnull
@Override
public Holder> findEntry(@Nonnull K key)
{
final Node current = hashedNodes.get(key);
return (current != null) ? Holders.>of(current) : Holders.>of();
}
@Nonnull
@Override
public JImmutableInsertOrderMap assign(@Nonnull K key,
V value)
{
final Node current = hashedNodes.get(key);
if (current == null) {
final Node newNode = new Node(key, value, nextIndex);
return new JImmutableInsertOrderMap(sortedNodes.assign(newNode.index, newNode),
hashedNodes.assign(key, newNode),
nextIndex + 1);
} else if (current.getValue() == value) {
return this;
} else {
final Node newNode = current.withValue(value);
return new JImmutableInsertOrderMap(sortedNodes.assign(newNode.index, newNode),
hashedNodes.assign(key, newNode),
nextIndex);
}
}
@Nonnull
@Override
public JImmutableInsertOrderMap delete(@Nonnull K key)
{
final Node current = hashedNodes.get(key);
if (current != null) {
return new JImmutableInsertOrderMap(sortedNodes.delete(current.index),
hashedNodes.delete(key),
nextIndex);
} else {
return this;
}
}
@Override
public int size()
{
return hashedNodes.size();
}
@Nonnull
@Override
public JImmutableInsertOrderMap deleteAll()
{
return of();
}
@Override
@Nonnull
public Cursor> cursor()
{
return TransformCursor.of(sortedNodes.valuesCursor(), new Func1, Entry>()
{
@Override
public Entry apply(Node node)
{
return node;
}
});
}
/**
* An Entry implementation that also stores the sortedKeys index corresponding to this node's key.
*
* @param
* @param
*/
@Immutable
private static class Node
extends MapEntry
implements Holder
{
private final int index;
private Node(K key,
V value,
int index)
{
super(key, value);
this.index = index;
}
@Override
public boolean isEmpty()
{
return false;
}
@Override
public boolean isFilled()
{
return true;
}
@Override
public V getValueOrNull()
{
return value;
}
@Override
public V getValueOr(V defaultValue)
{
return value;
}
private Node withValue(V value)
{
return new Node(key, value, index);
}
}
}