Add ArrayStack
This commit is contained in:
parent
bb89ca6bdb
commit
9f179ab32c
15
src/main/java/org/warp/commonutils/type/ArrayStack.java
Normal file
15
src/main/java/org/warp/commonutils/type/ArrayStack.java
Normal file
@ -0,0 +1,15 @@
|
||||
package org.warp.commonutils.type;
|
||||
|
||||
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
public class ArrayStack<T> extends FastUtilStackWrapper<T> {
|
||||
|
||||
public ArrayStack() {
|
||||
super(new ObjectArrayList<T>());
|
||||
}
|
||||
|
||||
public ArrayStack(Collection<T> stack) {
|
||||
super(new ObjectArrayList<T>(stack));
|
||||
}
|
||||
}
|
@ -7,8 +7,8 @@ import org.warp.commonutils.error.IndexOutOfBoundsException;
|
||||
|
||||
public class FastUtilStackSetWrapper<T> implements StackSet<T>, Collection<T> {
|
||||
|
||||
private final AddStrategy addStrategy;
|
||||
private final ObjectLinkedOpenHashSet<T> linkedHashSet;
|
||||
protected final AddStrategy addStrategy;
|
||||
protected final ObjectLinkedOpenHashSet<T> linkedHashSet;
|
||||
|
||||
/**
|
||||
* The first element will be the head
|
||||
@ -133,4 +133,10 @@ public class FastUtilStackSetWrapper<T> implements StackSet<T>, Collection<T> {
|
||||
}
|
||||
return it.next();
|
||||
}
|
||||
|
||||
@SuppressWarnings("MethodDoesntCallSuperMethod")
|
||||
@Override
|
||||
public FastUtilStackSetWrapper<T> clone() {
|
||||
return new FastUtilStackSetWrapper<>(linkedHashSet.clone(), addStrategy);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,123 @@
|
||||
package org.warp.commonutils.type;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.Objects;
|
||||
import java.util.StringJoiner;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class FastUtilStackWrapper<T> implements Stack<T>, Collection<T> {
|
||||
|
||||
private final it.unimi.dsi.fastutil.Stack<T> stack;
|
||||
private final Collection<T> collection;
|
||||
|
||||
public <U extends it.unimi.dsi.fastutil.Stack<T> & Collection<T>> FastUtilStackWrapper(U stack) {
|
||||
this.stack = stack;
|
||||
this.collection = stack;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void push(T o) {
|
||||
this.stack.push(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T pop() {
|
||||
return this.stack.pop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return collection.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return this.stack.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
return collection.contains(o);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Iterator<T> iterator() {
|
||||
return collection.iterator();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Object[] toArray() {
|
||||
return collection.toArray();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public <T1> T1[] toArray(@NotNull T1[] a) {
|
||||
//noinspection SuspiciousToArrayCall
|
||||
return collection.toArray(a);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(T t) {
|
||||
return collection.add(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Object o) {
|
||||
return collection.remove(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAll(@NotNull Collection<?> c) {
|
||||
return collection.containsAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(@NotNull Collection<? extends T> c) {
|
||||
return collection.addAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll(@NotNull Collection<?> c) {
|
||||
return collection.removeAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(@NotNull Collection<?> c) {
|
||||
return collection.retainAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
collection.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
FastUtilStackWrapper<?> that = (FastUtilStackWrapper<?>) o;
|
||||
|
||||
return Objects.equals(stack, that.stack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return stack != null ? stack.hashCode() : 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new StringJoiner(", ", FastUtilStackWrapper.class.getSimpleName() + "[", "]")
|
||||
.add("stack=" + stack)
|
||||
.toString();
|
||||
}
|
||||
}
|
@ -16,4 +16,13 @@ public class HashStackSet<T> extends FastUtilStackSetWrapper<T> {
|
||||
public HashStackSet(AddStrategy addStrategy) {
|
||||
super(new ObjectLinkedOpenHashSet<>(), addStrategy);
|
||||
}
|
||||
|
||||
private HashStackSet(ObjectLinkedOpenHashSet<T> linkedHashSet, AddStrategy addStrategy) {
|
||||
super(linkedHashSet, addStrategy);
|
||||
}
|
||||
|
||||
@Override
|
||||
public HashStackSet<T> clone() {
|
||||
return new HashStackSet<>(super.linkedHashSet.clone(), super.addStrategy);
|
||||
}
|
||||
}
|
||||
|
43
src/main/java/org/warp/commonutils/type/Stack.java
Normal file
43
src/main/java/org/warp/commonutils/type/Stack.java
Normal file
@ -0,0 +1,43 @@
|
||||
package org.warp.commonutils.type;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import org.warp.commonutils.error.IndexOutOfBoundsException;
|
||||
|
||||
/**
|
||||
* A stack
|
||||
*
|
||||
* <p>A stack must provide the classical {@link #push(Object)} and
|
||||
* {@link #pop()} operations, but may be also <em>peekable</em> to some extent: it may provide just the {@link #top()}
|
||||
* function, or even a more powerful {@link #peek(int)} method that provides access to all elements on the stack
|
||||
* (indexed from the top, which has index 0).
|
||||
*/
|
||||
|
||||
public interface Stack<K> extends it.unimi.dsi.fastutil.Stack<K> {
|
||||
|
||||
/**
|
||||
* Pop multiple times
|
||||
* @param count the number of times to pop
|
||||
* @return list of popped elements
|
||||
*/
|
||||
default List<K> pop(int count) {
|
||||
if (count < 0) {
|
||||
throw new IndexOutOfBoundsException(count);
|
||||
}
|
||||
var items = new ArrayList<K>(count);
|
||||
for (int i = 0; i < count; i++) {
|
||||
items.add(this.pop());
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
static <T> Stack<T> create() {
|
||||
return new ArrayStack<>();
|
||||
}
|
||||
|
||||
static <T, U extends Stack<T> & Collection<T>> Stack<T> wrap(U stack) {
|
||||
return new FastUtilStackWrapper<>(stack);
|
||||
}
|
||||
|
||||
}
|
@ -1,11 +1,11 @@
|
||||
package org.warp.commonutils.type;
|
||||
|
||||
import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Predicate;
|
||||
import org.warp.commonutils.error.IndexOutOfBoundsException;
|
||||
|
||||
/**
|
||||
* A stack but with the behavior of a Linked HashSet
|
||||
@ -27,6 +27,22 @@ public interface StackSet<K> {
|
||||
*/
|
||||
boolean push(K o);
|
||||
|
||||
/**
|
||||
* Pop multiple times
|
||||
* @param count the number of times to pop
|
||||
* @return list of popped elements
|
||||
*/
|
||||
default List<K> pop(int count) {
|
||||
if (count < 0) {
|
||||
throw new IndexOutOfBoundsException(count);
|
||||
}
|
||||
var items = new ArrayList<K>(count);
|
||||
for (int i = 0; i < count; i++) {
|
||||
items.add(this.pop());
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pops the top off the stack.
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user