
org.javersion.util.PersistentTreeSet Maven / Gradle / Ivy
/*
* Copyright 2013 Samppa Saarela
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.javersion.util;
import static com.google.common.collect.Iterables.transform;
import static java.util.Arrays.asList;
import static java.util.Spliterators.emptySpliterator;
import static org.javersion.util.AbstractRedBlackTree.Color.RED;
import java.util.*;
import java.util.stream.Collectors;
import javax.annotation.concurrent.Immutable;
import org.javersion.util.PersistentTreeSet.Node;
import com.google.common.base.Function;
import com.google.common.collect.Iterators;
@Immutable
public class PersistentTreeSet extends AbstractRedBlackTree, PersistentTreeSet> implements PersistentSet {
@SuppressWarnings("rawtypes")
private static final PersistentTreeSet EMPTY = new PersistentTreeSet();
@SuppressWarnings("rawtypes")
private static final Function GET_ELEMENT = input -> input != null ? ((Node)input).getKey() : null;
@SuppressWarnings("unchecked")
public static PersistentTreeSet empty() {
return EMPTY;
}
public static PersistentTreeSet empty(Comparator super E> comparator) {
return new PersistentTreeSet<>(comparator);
}
public static > PersistentTreeSet of(E... elements) {
return new PersistentTreeSet().conjAll(asList(elements));
}
public static PersistentTreeSet of(Comparator super E> comparator, E... elements) {
return new PersistentTreeSet<>(comparator).conjAll(asList(elements));
}
private final Node root;
private final int size;
private PersistentTreeSet() {
super();
root = null;
size = 0;
}
private PersistentTreeSet(Comparator super E> comparator) {
super(comparator);
root = null;
size = 0;
}
private PersistentTreeSet(Comparator super E> comparator, Node root, int size) {
super(comparator);
this.root = root;
this.size = size;
}
public int size() {
return size;
}
@Override
public boolean contains(Object key) {
return find(root, key) != null;
}
Node root() {
return root;
}
@Override
public Set asSet() {
return new ImmutableSet<>(this);
}
@Override
public PersistentTreeSet conj(E value) {
UpdateContext> context = new UpdateContext<>(1);
return doAdd(context, root, new Node<>(context, value, RED));
}
@Override
public PersistentTreeSet conjAll(Collection extends E> coll) {
final UpdateContext> context = new UpdateContext<>(32);
return doAddAll(context, root, transform(coll, new EntryToNode<>(context)));
}
@Override
public PersistentTreeSet disj(Object keyObj) {
return doRemove(new UpdateContext<>(1), root, keyObj);
}
@Override
public MutableSet toMutableSet() {
// TODO
return null;
}
@SuppressWarnings("unchecked")
public Iterator iterator() {
return Iterators.transform(doIterator(root, true), GET_ELEMENT);
}
@Override
public Spliterator spliterator() {
if (root != null) {
return new ElementSpliterator(root, size, comparator);
} else {
return emptySpliterator();
}
}
@SuppressWarnings("unchecked")
@Override
protected PersistentTreeSet doReturn(Comparator super E> comparator, Node newRoot, int newSize) {
if (newRoot == root) {
return this;
} else if (newRoot == null) {
return EMPTY;
}
return new PersistentTreeSet<>(comparator, newRoot, newSize);
}
public String toString() {
return stream().map(Objects::toString).collect(Collectors.joining(", ", "[", "]"));
}
private static final class EntryToNode implements Function> {
private final UpdateContext> context;
private EntryToNode(UpdateContext> context) {
this.context = context;
}
@Override
public Node apply(E input) {
return new Node(context, input, RED);
}
}
static class Node extends AbstractRedBlackTree.Node> {
public Node(UpdateContext super Node> context, E key, Color color) {
this(context, key, color, null, null);
}
public Node(UpdateContext super Node> context, E key, Color color, Node left, Node right) {
super(context, key, color, left, right);
}
public E getKey() {
return key;
}
@Override
public Node self() {
return this;
}
@Override
protected Node cloneWith(UpdateContext super Node> currentContext) {
return new Node(currentContext, key, color, left, right);
}
@Override
protected Node replaceWith(UpdateContext super Node> currentContext, Node node) {
return this;
}
}
static class ElementSpliterator extends RBSpliterator> {
private final Comparator super E> comparator;
public ElementSpliterator(Node root, int size, Comparator super E> comparator) {
super(root, size, SORTED | DISTINCT | IMMUTABLE);
this.comparator = comparator;
}
protected ElementSpliterator(int sizeEstimate, Comparator super E> comparator) {
super(sizeEstimate, SORTED | DISTINCT | IMMUTABLE);
this.comparator = comparator;
}
@Override
protected RBSpliterator> newSpliterator(int sizeEstimate) {
return new ElementSpliterator(sizeEstimate, comparator);
}
@Override
protected E apply(Node node) {
return node.key;
}
@Override
public Comparator super E> getComparator() {
return comparator;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy