All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.github.methylene.lists.ComparableList Maven / Gradle / Ivy
package com.github.methylene.lists;
import static com.github.methylene.lists.ListBuilder.DEFAULT_INITIAL_CAPACITY;
import static com.github.methylene.lists.ListBuilder.ensureCapacity;
import static com.github.methylene.sym.Rankings.apply;
import static com.github.methylene.sym.Rankings.nextOffset;
import static com.github.methylene.sym.Rankings.sort;
import static java.util.Arrays.binarySearch;
import static java.util.Arrays.copyOf;
import com.github.methylene.sym.Permutation;
import com.github.methylene.sym.Util;
import java.io.Serializable;
import java.util.*;
/**
* Comparable based lookup list.
*/
public final class ComparableList extends LookupList implements RandomAccess, Serializable {
private static final long serialVersionUID = 1L;
private final Comparable[] sorted;
private final boolean unique;
private final boolean ordered;
private ComparableList(Comparable[] sorted, boolean ordered, Permutation sort, Permutation unsort) {
super(sort, unsort);
this.sorted = sorted;
this.ordered = ordered;
this.unique = Util.isUnique(sorted);
}
@SuppressWarnings("unchecked")
static ComparableList createNewList(Comparable[] a, Permutation sort) {
Comparable[] applied = sort.apply(a);
Comparable[] sorted = applied == a ? Arrays.copyOf(a, a.length) : applied;
return new ComparableList(sorted, Util.isSorted(a), sort, sort.invert());
}
public static ComparableList createNewList(Comparable[] a) {
return createNewList(a, Permutation.sort(a));
}
@Override
public int indexOf(Object el) {
int i = Arrays.binarySearch(sorted, el);
return i < 0 ? -1 : unsort.apply(i);
}
@Override
public int lastIndexOf(Object el) {
Comparable comparable = (Comparable) el;
int start = Arrays.binarySearch(sorted, comparable);
if (start < 0) {return -1;}
int direction = start > 0 && Objects.equals(sorted[start - 1], comparable) ? -1 : 1;
int peek = start + direction;
while (peek >= 0 && peek < sorted.length && Objects.equals(sorted[peek], comparable)) {
start = peek;
peek += direction;
}
return unsort.apply(start);
}
@Override
public boolean contains(Object el) {
return indexOf(el) >= 0;
}
@Override
@SuppressWarnings("unchecked")
public E get(int i) {
return (E) sorted[sort.apply(i)];
}
@Override
public int size() {
return sorted.length;
}
@Override
public int[] indexOf(E el, int size) {
final int idx = binarySearch(sorted, el);
if (idx < 0 || size == 0)
return EMPTY_INT_ARRAY;
int[] builder = new int[size < 0 ? DEFAULT_INITIAL_CAPACITY : size];
int offset = 0;
int i = 0;
do {
builder = ensureCapacity(builder, i + 1);
builder[i++] = unsort.apply(idx + offset);
} while ((offset = nextOffset(idx, offset, sorted)) != 0 && (size < 0 || i < size));
return i == size ? builder : Arrays.copyOf(builder, i);
}
@Override
public Map group() {
return Group.group(sorted, unsort);
}
@Override
@SuppressWarnings("unchecked")
public List sort() {
if (ordered)
return this;
ArrayList result = new ArrayList(sorted.length);
for (Comparable el : sorted)
result.add((E) el);
return result;
}
@Override
@SuppressWarnings("unchecked")
public List sortUnique() {
if (unique)
return sort();
ArrayList result = new ArrayList(sorted.length);
E previous = (E) sorted[0];
for (Comparable el : sorted) {
if (!el.equals(previous)) {
result.add(previous);
previous = (E) el;
}
}
result.add(previous);
return result;
}
@Override
@SuppressWarnings("unchecked")
public ComparableList shuffle(Permutation p) {
if (unique) {
Permutation punsort = p.compose(unsort);
return new ComparableList(sorted, punsort.sorts(sorted), punsort.invert(), punsort);
} else {
Comparable[] a = p.compose(super.unsort).apply(sorted);
return createNewList(a, Permutation.sort(a));
}
}
@Override
public boolean isUnique() {
return unique;
}
@Override
public boolean isSorted() {
return ordered;
}
/**
* Convenience list builder
*/
public static final class Builder extends ListBuilder {
private Comparable[] contents;
Builder(int initialCapacity) {
this.contents = new Comparable[checkPositive(initialCapacity)];
}
Builder() {
this(DEFAULT_INITIAL_CAPACITY);
}
@Override
public LookupList build() {
Comparable[] a = Arrays.copyOf(contents, size);
return createNewList(a, Permutation.sort(a));
}
@Override
protected void ensureCapacity(int minCapacity) {
if (minCapacity > contents.length)
contents = ensureCapacity(contents, minCapacity);
}
@Override
public Builder add(E element) {
ensureCapacity(size + 1);
contents[size++] = element;
return this;
}
@Override public ListBuilder addAll(E... elements) {
ensureCapacity(size + elements.length);
System.arraycopy(elements, 0, contents, size, elements.length);
incrementSize(elements.length);
return this;
}
}
}