aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gtPlusPlus/api/objects/data
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/gtPlusPlus/api/objects/data')
-rw-r--r--src/main/java/gtPlusPlus/api/objects/data/AutoMap.java345
-rw-r--r--src/main/java/gtPlusPlus/api/objects/data/ConcurrentHashSet.java18
-rw-r--r--src/main/java/gtPlusPlus/api/objects/data/ConcurrentSet.java53
-rw-r--r--src/main/java/gtPlusPlus/api/objects/data/FlexiblePair.java39
-rw-r--r--src/main/java/gtPlusPlus/api/objects/data/ObjMap.java285
-rw-r--r--src/main/java/gtPlusPlus/api/objects/data/Pair.java35
-rw-r--r--src/main/java/gtPlusPlus/api/objects/data/Quad.java45
-rw-r--r--src/main/java/gtPlusPlus/api/objects/data/ReverseAutoMap.java175
-rw-r--r--src/main/java/gtPlusPlus/api/objects/data/Triplet.java27
-rw-r--r--src/main/java/gtPlusPlus/api/objects/data/TypeCounter.java178
-rw-r--r--src/main/java/gtPlusPlus/api/objects/data/WeightedCollection.java102
-rw-r--r--src/main/java/gtPlusPlus/api/objects/data/weakref/WeakAutoMap.java12
12 files changed, 1314 insertions, 0 deletions
diff --git a/src/main/java/gtPlusPlus/api/objects/data/AutoMap.java b/src/main/java/gtPlusPlus/api/objects/data/AutoMap.java
new file mode 100644
index 0000000000..e04f1af03a
--- /dev/null
+++ b/src/main/java/gtPlusPlus/api/objects/data/AutoMap.java
@@ -0,0 +1,345 @@
+package gtPlusPlus.api.objects.data;
+
+import java.io.Serializable;
+import java.util.*;
+
+public class AutoMap<V> implements Iterable<V>, Cloneable, Serializable, Collection<V>, Queue<V>, List<V> {
+
+ /**
+ * The Internal Map
+ */
+ protected final Map<Integer, V> mInternalMap;
+ protected final Map<String, Integer> mInternalNameMap;
+
+ /**
+ * The Internal ID
+ */
+ private int mInternalID = 0;
+ private static final long serialVersionUID = 3771412318075131790L;
+
+
+ public AutoMap() {
+ this(new LinkedHashMap<Integer, V>());
+ }
+
+ public Map<Integer, V> getMap(){
+ return mInternalMap;
+ }
+
+ public AutoMap(Map<Integer, V> defaultMapType) {
+ mInternalMap = defaultMapType;
+ mInternalNameMap = new LinkedHashMap<String, Integer>();
+ }
+
+ /**
+ * Generates an AutoMap from the List.
+ * @param aList - Data to be inserted into the AutoMap.
+ */
+ public AutoMap(List<V> aList) {
+ mInternalMap = new LinkedHashMap<Integer, V>();
+ mInternalNameMap = new LinkedHashMap<String, Integer>();
+ if (aList != null && aList.size() > 0) {
+ for (V obj : aList) {
+ add(obj);
+ }
+ }
+ }
+ /**
+ * Generates an AutoMap from a Set.
+ * @param aList - Data to be inserted into the AutoMap.
+ */
+ public AutoMap(Set<V> aList) {
+ mInternalMap = new LinkedHashMap<Integer, V>();
+ mInternalNameMap = new LinkedHashMap<String, Integer>();
+ if (aList != null && aList.size() > 0) {
+ for (V obj : aList) {
+ add(obj);
+ }
+ }
+ }
+ /**
+ * Generates an AutoMap from a Collection.
+ * @param aList - Data to be inserted into the AutoMap.
+ */
+ public AutoMap(Collection<V> aList) {
+ mInternalMap = new LinkedHashMap<Integer, V>();
+ mInternalNameMap = new LinkedHashMap<String, Integer>();
+ if (aList != null && aList.size() > 0) {
+ for (V obj : aList) {
+ add(obj);
+ }
+ }
+ }
+
+ /**
+ * Generates an AutoMap from a Array.
+ * @param aArray - Data to be inserted into the AutoMap.
+ */
+ public AutoMap(V[] aArray) {
+ mInternalMap = new LinkedHashMap<Integer, V>();
+ mInternalNameMap = new LinkedHashMap<String, Integer>();
+ if (aArray != null && aArray.length > 0) {
+ for (V obj : aArray) {
+ add(obj);
+ }
+ }
+ }
+
+ @Override
+ public Iterator<V> iterator() {
+ return values().iterator();
+ }
+
+ public synchronized boolean setValue(V object){
+ int mOriginalID = this.mInternalID;
+ put(object);
+ if (this.mInternalMap.get(mOriginalID).equals(object) || mOriginalID > this.mInternalID){
+ return true;
+ }
+ else {
+ return false;
+ }
+ }
+
+ public synchronized V put(V object){
+ return set(object);
+ }
+
+ public synchronized boolean add(V object){
+ return set(object) != null;
+ }
+
+ public synchronized V set(V object){
+ if (object == null) {
+ return null;
+ }
+ mInternalNameMap.put(""+object.hashCode(), (mInternalID+1));
+ return mInternalMap.put(mInternalID++, object);
+ }
+
+ public synchronized V get(int id){
+ return mInternalMap.get(id);
+ }
+
+ public synchronized Collection<V> values(){
+ return mInternalMap.values();
+ }
+
+ public synchronized int size(){
+ return mInternalMap.size();
+ }
+
+ public synchronized int hashCode(){
+ return mInternalMap.hashCode();
+ }
+
+ public synchronized boolean containsKey(int key){
+ return mInternalMap.containsKey(key);
+ }
+
+ public synchronized boolean containsValue(V value){
+ return mInternalMap.containsValue(value);
+ }
+
+ public synchronized boolean isEmpty(){
+ return mInternalMap.isEmpty();
+ }
+
+ public synchronized void clear(){
+ this.mInternalID = 0;
+ this.mInternalMap.clear();
+ this.mInternalNameMap.clear();
+ return;
+ }
+
+ @SuppressWarnings("unchecked")
+ public V[] toArray() {
+ V[] toR = (V[]) java.lang.reflect.Array.newInstance(mInternalMap.get(0).getClass(), mInternalMap.size());
+ for (int i = 0; i < mInternalMap.size(); i++) {
+ toR[i] = mInternalMap.get(i);
+ }
+ return toR;
+ }
+
+ public synchronized final int getInternalID() {
+ return mInternalID;
+ }
+
+ public synchronized final boolean remove(Object value) {
+ value.getClass();
+ if (this.mInternalMap.containsValue(value)) {
+ return this.mInternalMap.remove(mInternalNameMap.get(""+value.hashCode()), value);
+ }
+ return false;
+ }
+
+ @Override
+ public boolean contains(Object o) {
+ for (V g : this.mInternalMap.values()) {
+ if (g.equals(o)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public <V> V[] toArray(V[] a) {
+ return (V[]) toArray();
+ }
+
+ @Override
+ public boolean containsAll(Collection<?> c) {
+ boolean aTrue = true;
+ for (Object g : c) {
+ if (!this.contains(g)) {
+ aTrue = false;
+ }
+ }
+ return aTrue;
+ }
+
+ @Override
+ public boolean addAll(Collection<? extends V> c) {
+ boolean aTrue = true;
+ for (V g : c) {
+ if (!this.add(g)) {
+ aTrue = false;
+ }
+ }
+ return aTrue;
+ }
+
+ @Override
+ public boolean removeAll(Collection<?> c) {
+ boolean aTrue = true;
+ for (Object g : c) {
+ if (!this.remove(g)) {
+ aTrue = false;
+ }
+ }
+ return aTrue;
+ }
+
+ @Override
+ public boolean retainAll(Collection<?> c) {
+ AutoMap<?> aTempAllocation = new AutoMap<Object>();
+ boolean aTrue = false;
+ aTempAllocation = this;
+ aTempAllocation.removeAll(c);
+ aTempAllocation.clear();
+ aTrue = aTempAllocation.isEmpty();
+ aTempAllocation.clear();
+ return aTrue;
+ }
+
+ @Override
+ public boolean offer(V e) {
+ return add(e);
+ }
+
+ @Override
+ public V remove() {
+ V y = this.get(0);
+ if (remove(y))
+ return y;
+ else
+ return null;
+ }
+
+ @Override
+ public V poll() {
+ if (this.mInternalMap.isEmpty()) {
+ return null;
+ }
+ return remove();
+ }
+
+ @Override
+ public V element() {
+ if (this.mInternalMap.isEmpty()) {
+ return null;
+ }
+ return this.get(0);
+ }
+
+ @Override
+ public V peek() {
+ return element();
+ }
+
+ @Override
+ public boolean addAll(int index, Collection<? extends V> c) {
+ for (V y : c) {
+ add(y);
+ }
+ return true;
+ }
+
+ @Override
+ public V set(int index, V element) {
+ return mInternalMap.put(index, element);
+ }
+
+ @Override
+ public void add(int index, V element) {
+ add(element);
+ }
+
+ @Override
+ public V remove(int index) {
+ V h = mInternalMap.get(index);
+ set(index, null);
+ return h;
+ }
+
+ @Override
+ public int indexOf(Object o) {
+ int aCount = 0;
+ for (V of : mInternalMap.values()) {
+ if (of != o) {
+ aCount++;
+ continue;
+ }
+ else {
+ return aCount;
+ }
+ }
+ return -1;
+ }
+
+ @Override
+ public int lastIndexOf(Object o) {
+ //TODO
+ return indexOf(o);
+ }
+
+ @Override
+ public ListIterator<V> listIterator() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public ListIterator<V> listIterator(int index) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public List<V> subList(int fromIndex, int toIndex) {
+ AutoMap<V> aNewSubList = new AutoMap<V>();
+ for (int slot=fromIndex; slot<=toIndex; slot++) {
+ V obj = mInternalMap.get(slot);
+ if (obj == null) {
+ continue;
+ }
+ else {
+ aNewSubList.put(obj);
+ }
+ }
+ return aNewSubList;
+ }
+
+}
diff --git a/src/main/java/gtPlusPlus/api/objects/data/ConcurrentHashSet.java b/src/main/java/gtPlusPlus/api/objects/data/ConcurrentHashSet.java
new file mode 100644
index 0000000000..991908e402
--- /dev/null
+++ b/src/main/java/gtPlusPlus/api/objects/data/ConcurrentHashSet.java
@@ -0,0 +1,18 @@
+package gtPlusPlus.api.objects.data;
+
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+public class ConcurrentHashSet<V> extends ConcurrentSet<V> {
+
+ private static final long serialVersionUID = -1293478938482781728L;
+
+ public ConcurrentHashSet() {
+ this(new ConcurrentHashMap<Integer, V>());
+ }
+
+ public ConcurrentHashSet(ConcurrentMap<Integer, V> defaultMapType) {
+ super(defaultMapType);
+ }
+
+}
diff --git a/src/main/java/gtPlusPlus/api/objects/data/ConcurrentSet.java b/src/main/java/gtPlusPlus/api/objects/data/ConcurrentSet.java
new file mode 100644
index 0000000000..1d3ffc1c01
--- /dev/null
+++ b/src/main/java/gtPlusPlus/api/objects/data/ConcurrentSet.java
@@ -0,0 +1,53 @@
+package gtPlusPlus.api.objects.data;
+
+import java.io.Serializable;
+import java.util.AbstractSet;
+import java.util.Iterator;
+import java.util.concurrent.ConcurrentMap;
+
+public abstract class ConcurrentSet<E> extends AbstractSet<E> implements Serializable {
+
+ private static final long serialVersionUID = -6761513279741915432L;
+
+ private final ConcurrentMap<Integer, E> mInternalMap;
+
+ private int mInternalID = 0;
+
+ /**
+ * Creates a new instance which wraps the specified {@code map}.
+ */
+ public ConcurrentSet(ConcurrentMap<Integer, E> aMap) {
+ mInternalMap = aMap;
+ }
+
+ @Override
+ public int size() {
+ return mInternalMap.size();
+ }
+
+ @Override
+ public boolean contains(Object o) {
+ return mInternalMap.containsKey(o);
+ }
+
+ @Override
+ public boolean add(E o) {
+ return mInternalMap.putIfAbsent(mInternalID++, o) == null;
+ }
+
+ @Override
+ public boolean remove(Object o) {
+ return mInternalMap.remove(o) != null;
+ }
+
+ @Override
+ public void clear() {
+ this.mInternalID = 0;
+ mInternalMap.clear();
+ }
+
+ @Override
+ public Iterator<E> iterator() {
+ return mInternalMap.values().iterator();
+ }
+}
diff --git a/src/main/java/gtPlusPlus/api/objects/data/FlexiblePair.java b/src/main/java/gtPlusPlus/api/objects/data/FlexiblePair.java
new file mode 100644
index 0000000000..64f57b4e5a
--- /dev/null
+++ b/src/main/java/gtPlusPlus/api/objects/data/FlexiblePair.java
@@ -0,0 +1,39 @@
+package gtPlusPlus.api.objects.data;
+
+import java.io.Serializable;
+
+import com.google.common.base.Objects;
+
+public class FlexiblePair<K,V> implements Serializable {
+
+ /**
+ * SVUID
+ */
+ private static final long serialVersionUID = 1250550491092812443L;
+ private final K key;
+ private V value;
+
+ public FlexiblePair(final K key, final V value){
+ this.key = key;
+ this.value = value;
+ }
+
+ final public K getKey(){
+ return this.key;
+ }
+
+ final public V getValue(){
+ return this.value;
+ }
+
+ final public void setValue(V aObj) {
+ value = aObj;
+ }
+
+ @Override
+ public int hashCode() {
+ Integer aCode = Objects.hashCode(getKey(), getValue());
+ return aCode != null ? aCode : super.hashCode();
+ }
+
+} \ No newline at end of file
diff --git a/src/main/java/gtPlusPlus/api/objects/data/ObjMap.java b/src/main/java/gtPlusPlus/api/objects/data/ObjMap.java
new file mode 100644
index 0000000000..49dd70d2b8
--- /dev/null
+++ b/src/main/java/gtPlusPlus/api/objects/data/ObjMap.java
@@ -0,0 +1,285 @@
+package gtPlusPlus.api.objects.data;
+
+import java.util.Arrays;
+
+/**
+ * Object-2-object map based on IntIntMap4a
+ */
+public class ObjMap<K, V>
+{
+ private static final Object FREE_KEY = new Object();
+ private static final Object REMOVED_KEY = new Object();
+
+ /** Keys and values */
+ private Object[] m_data;
+
+ /** Value for the null key (if inserted into a map) */
+ private Object m_nullValue;
+ private boolean m_hasNull;
+
+ /** Fill factor, must be between (0 and 1) */
+ private final float m_fillFactor;
+ /** We will resize a map once it reaches this size */
+ private int m_threshold;
+ /** Current map size */
+ private int m_size;
+ /** Mask to calculate the original position */
+ private int m_mask;
+ /** Mask to wrap the actual array pointer */
+ private int m_mask2;
+
+ public ObjMap( final int size, final float fillFactor )
+ {
+ if ( fillFactor <= 0 || fillFactor >= 1 )
+ throw new IllegalArgumentException( "FillFactor must be in (0, 1)" );
+ if ( size <= 0 )
+ throw new IllegalArgumentException( "Size must be positive!" );
+ final int capacity = arraySize(size, fillFactor);
+ m_mask = capacity - 1;
+ m_mask2 = capacity * 2 - 1;
+ m_fillFactor = fillFactor;
+
+ m_data = new Object[capacity * 2];
+ Arrays.fill( m_data, FREE_KEY );
+
+ m_threshold = (int) (capacity * fillFactor);
+ }
+
+ @SuppressWarnings("unchecked")
+ public V get( final K key )
+ {
+ if ( key == null )
+ return (V) m_nullValue; //we null it on remove, so safe not to check a flag here
+
+ int ptr = (key.hashCode() & m_mask) << 1;
+ Object k = m_data[ ptr ];
+
+ if ( k == FREE_KEY )
+ return null; //end of chain already
+ if ( k.equals( key ) ) //we check FREE and REMOVED prior to this call
+ return (V) m_data[ ptr + 1 ];
+ while ( true )
+ {
+ ptr = (ptr + 2) & m_mask2; //that's next index
+ k = m_data[ ptr ];
+ if ( k == FREE_KEY )
+ return null;
+ if ( k.equals( key ) )
+ return (V) m_data[ ptr + 1 ];
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ public V put( final K key, final V value )
+ {
+ if ( key == null )
+ return insertNullKey(value);
+
+ int ptr = getStartIndex(key) << 1;
+ Object k = m_data[ptr];
+
+ if ( k == FREE_KEY ) //end of chain already
+ {
+ m_data[ ptr ] = key;
+ m_data[ ptr + 1 ] = value;
+ if ( m_size >= m_threshold )
+ rehash( m_data.length * 2 ); //size is set inside
+ else
+ ++m_size;
+ return null;
+ }
+ else if ( k.equals( key ) ) //we check FREE and REMOVED prior to this call
+ {
+ final Object ret = m_data[ ptr + 1 ];
+ m_data[ ptr + 1 ] = value;
+ return (V) ret;
+ }
+
+ int firstRemoved = -1;
+ if ( k == REMOVED_KEY )
+ firstRemoved = ptr; //we may find a key later
+
+ while ( true )
+ {
+ ptr = ( ptr + 2 ) & m_mask2; //that's next index calculation
+ k = m_data[ ptr ];
+ if ( k == FREE_KEY )
+ {
+ if ( firstRemoved != -1 )
+ ptr = firstRemoved;
+ m_data[ ptr ] = key;
+ m_data[ ptr + 1 ] = value;
+ if ( m_size >= m_threshold )
+ rehash( m_data.length * 2 ); //size is set inside
+ else
+ ++m_size;
+ return null;
+ }
+ else if ( k.equals( key ) )
+ {
+ final Object ret = m_data[ ptr + 1 ];
+ m_data[ ptr + 1 ] = value;
+ return (V) ret;
+ }
+ else if ( k == REMOVED_KEY )
+ {
+ if ( firstRemoved == -1 )
+ firstRemoved = ptr;
+ }
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ public V remove( final K key )
+ {
+ if ( key == null )
+ return removeNullKey();
+
+ int ptr = getStartIndex(key) << 1;
+ Object k = m_data[ ptr ];
+ if ( k == FREE_KEY )
+ return null; //end of chain already
+ else if ( k.equals( key ) ) //we check FREE and REMOVED prior to this call
+ {
+ --m_size;
+ if ( m_data[ ( ptr + 2 ) & m_mask2 ] == FREE_KEY )
+ m_data[ ptr ] = FREE_KEY;
+ else
+ m_data[ ptr ] = REMOVED_KEY;
+ final V ret = (V) m_data[ ptr + 1 ];
+ m_data[ ptr + 1 ] = null;
+ return ret;
+ }
+ while ( true )
+ {
+ ptr = ( ptr + 2 ) & m_mask2; //that's next index calculation
+ k = m_data[ ptr ];
+ if ( k == FREE_KEY )
+ return null;
+ else if ( k.equals( key ) )
+ {
+ --m_size;
+ if ( m_data[ ( ptr + 2 ) & m_mask2 ] == FREE_KEY )
+ m_data[ ptr ] = FREE_KEY;
+ else
+ m_data[ ptr ] = REMOVED_KEY;
+ final V ret = (V) m_data[ ptr + 1 ];
+ m_data[ ptr + 1 ] = null;
+ return ret;
+ }
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private V insertNullKey(final V value)
+ {
+ if ( m_hasNull )
+ {
+ final Object ret = m_nullValue;
+ m_nullValue = value;
+ return (V) ret;
+ }
+ else
+ {
+ m_nullValue = value;
+ ++m_size;
+ return null;
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private V removeNullKey()
+ {
+ if ( m_hasNull )
+ {
+ final Object ret = m_nullValue;
+ m_nullValue = null;
+ m_hasNull = false;
+ --m_size;
+ return (V) ret;
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ public int size()
+ {
+ return m_size;
+ }
+
+ @SuppressWarnings("unchecked")
+ private void rehash( final int newCapacity )
+ {
+ m_threshold = (int) (newCapacity/2 * m_fillFactor);
+ m_mask = newCapacity/2 - 1;
+ m_mask2 = newCapacity - 1;
+
+ final int oldCapacity = m_data.length;
+ final Object[] oldData = m_data;
+
+ m_data = new Object[ newCapacity ];
+ Arrays.fill( m_data, FREE_KEY );
+
+ m_size = m_hasNull ? 1 : 0;
+
+ for ( int i = 0; i < oldCapacity; i += 2 ) {
+ final Object oldKey = oldData[ i ];
+ if( oldKey != FREE_KEY && oldKey != REMOVED_KEY )
+ put( (K)oldKey, (V)oldData[ i + 1 ]);
+ }
+ }
+
+ public int getStartIndex( final Object key )
+ {
+ //key is not null here
+ return key.hashCode() & m_mask;
+ }
+
+ public Object[] values() {
+ return m_data;
+ }
+
+ /** Taken from FastUtil implementation */
+
+ /** Return the least power of two greater than or equal to the specified value.
+ *
+ * <p>Note that this function will return 1 when the argument is 0.
+ *
+ * @param x a long integer smaller than or equal to 2<sup>62</sup>.
+ * @return the least power of two greater than or equal to the specified value.
+ */
+ public static long nextPowerOfTwo( long x ) {
+ if ( x == 0 ) return 1;
+ x--;
+ x |= x >> 1;
+ x |= x >> 2;
+ x |= x >> 4;
+ x |= x >> 8;
+ x |= x >> 16;
+ return ( x | x >> 32 ) + 1;
+ }
+
+ /** Returns the least power of two smaller than or equal to 2<sup>30</sup> and larger than or equal to <code>Math.ceil( expected / f )</code>.
+ *
+ * @param expected the expected number of elements in a hash table.
+ * @param f the load factor.
+ * @return the minimum possible size for a backing array.
+ * @throws IllegalArgumentException if the necessary size is larger than 2<sup>30</sup>.
+ */
+ public static int arraySize( final int expected, final float f ) {
+ final long s = Math.max( 2, nextPowerOfTwo( (long)Math.ceil( expected / f ) ) );
+ if ( s > (1 << 30) ) throw new IllegalArgumentException( "Too large (" + expected + " expected elements with load factor " + f + ")" );
+ return (int)s;
+ }
+
+ //taken from FastUtil
+ private static final int INT_PHI = 0x9E3779B9;
+
+ public static int phiMix( final int x ) {
+ final int h = x * INT_PHI;
+ return h ^ (h >> 16);
+}
+
+}
diff --git a/src/main/java/gtPlusPlus/api/objects/data/Pair.java b/src/main/java/gtPlusPlus/api/objects/data/Pair.java
new file mode 100644
index 0000000000..e1d23e6b43
--- /dev/null
+++ b/src/main/java/gtPlusPlus/api/objects/data/Pair.java
@@ -0,0 +1,35 @@
+package gtPlusPlus.api.objects.data;
+
+import java.io.Serializable;
+
+import com.google.common.base.Objects;
+
+public class Pair<K,V> implements Serializable {
+
+ /**
+ * SVUID
+ */
+ private static final long serialVersionUID = 1250550491092812443L;
+ private final K key;
+ private final V value;
+
+ public Pair(final K key, final V value){
+ this.key = key;
+ this.value = value;
+ }
+
+ final public K getKey(){
+ return this.key;
+ }
+
+ final public V getValue(){
+ return this.value;
+ }
+
+ @Override
+ public int hashCode() {
+ Integer aCode = Objects.hashCode(getKey(), getValue());
+ return aCode != null ? aCode : super.hashCode();
+ }
+
+} \ No newline at end of file
diff --git a/src/main/java/gtPlusPlus/api/objects/data/Quad.java b/src/main/java/gtPlusPlus/api/objects/data/Quad.java
new file mode 100644
index 0000000000..fa2e52951e
--- /dev/null
+++ b/src/main/java/gtPlusPlus/api/objects/data/Quad.java
@@ -0,0 +1,45 @@
+package gtPlusPlus.api.objects.data;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class Quad<K,V,C,R> {
+
+ private final K key;
+ private final V value;
+ private final C value2;
+ private final R value3;
+
+ public Quad(final K key, final V value, final C value2, final R value3){
+ this.key = key;
+ this.value = value;
+ this.value2 = value2;
+ this.value3 = value3;
+ }
+
+ final public K getKey(){
+ return this.key;
+ }
+
+ final public V getValue_1(){
+ return this.value;
+ }
+
+ final public C getValue_2(){
+ return this.value2;
+ }
+
+ final public R getValue_3(){
+ return this.value3;
+ }
+
+ public final List values() {
+ List<Object> aVals = new ArrayList<Object>();
+ aVals.add(key);
+ aVals.add(value);
+ aVals.add(value2);
+ aVals.add(value3);
+ return aVals;
+ }
+
+} \ No newline at end of file
diff --git a/src/main/java/gtPlusPlus/api/objects/data/ReverseAutoMap.java b/src/main/java/gtPlusPlus/api/objects/data/ReverseAutoMap.java
new file mode 100644
index 0000000000..72ec0bc293
--- /dev/null
+++ b/src/main/java/gtPlusPlus/api/objects/data/ReverseAutoMap.java
@@ -0,0 +1,175 @@
+package gtPlusPlus.api.objects.data;
+
+import java.util.*;
+
+public class ReverseAutoMap<N> extends AutoMap<N> {
+
+ /**
+ * The Internal Map
+ */
+ private Map<N, Integer> mInternalMapReverseLookup = new HashMap<N, Integer>();
+
+ /**
+ * The Internal ID
+ */
+ private int mInternalID = 0;
+ private static final long serialVersionUID = 3771412318075131790L;
+
+ @Override
+ public Iterator<N> iterator() {
+ return values().iterator();
+ }
+
+ public synchronized boolean setValue(N object){
+ int mOriginalID = this.mInternalID;
+ put(object);
+ if (this.mInternalMap.get(mOriginalID).equals(object) || mOriginalID > this.mInternalID){
+ return true;
+ }
+ else {
+ return false;
+ }
+ }
+
+ @Override
+ public synchronized N put(N object){
+ return set(object);
+ }
+
+ @Override
+ public synchronized N set(N object){
+ int newID = getNextFreeMapID();
+ mInternalMapReverseLookup.put(object, newID);
+ return mInternalMap.put(newID, object);
+ }
+
+ public synchronized int putToInternalMap(N object){
+ return setInternalMap(object);
+ }
+
+ public synchronized int setInternalMap(N object){
+ int newID = getNextFreeMapID();
+ mInternalMap.put(newID, object);
+ mInternalMapReverseLookup.put(object, newID);
+ return newID;
+ }
+
+ public synchronized boolean injectCleanDataToAutoMap(Integer g, N object){
+ if (!mInternalMap.containsKey(g) && !mInternalMapReverseLookup.containsKey(object)) {
+ int a1 = 0, a2 = 0, a11 = 0, a22 = 0;
+ a1 = mInternalMap.size();
+ a2 = mInternalMapReverseLookup.size();
+ a11 = a1;
+ a22 = a2;
+ mInternalMap.put(g, object);
+ a1 = mInternalMap.size();
+ mInternalMapReverseLookup.put(object, g);
+ a2 = mInternalMapReverseLookup.size();
+ if (a1 > a11 && a2 > a22)
+ return true;
+ }
+ return false;
+ }
+
+ public synchronized boolean injectDataToAutoMap(Integer g, N object){
+ int a1 = 0, a2 = 0, a11 = 0, a22 = 0;
+ a1 = mInternalMap.size();
+ a2 = mInternalMapReverseLookup.size();
+ a11 = a1;
+ a22 = a2;
+ mInternalMap.put(g, object);
+ a1 = mInternalMap.size();
+ mInternalMapReverseLookup.put(object, g);
+ a2 = mInternalMapReverseLookup.size();
+ if (a1 > a11 && a2 > a22)
+ return true;
+ return false;
+ }
+
+ private boolean raiseInternalID() {
+ int mOld = mInternalID;
+ mInternalID++;
+ return mInternalID > mOld;
+ }
+
+ public synchronized int getNextFreeMapID() {
+ if (raiseInternalID()) {
+ return mInternalID;
+ }
+ return Short.MIN_VALUE;
+ }
+
+ @Override
+ public synchronized N get(int id){
+ return mInternalMap.get(id);
+ }
+
+ public synchronized int get(N key) {
+ return mInternalMapReverseLookup.get(key);
+ }
+
+ @Override
+ public synchronized Collection<N> values(){
+ return mInternalMap.values();
+ }
+
+ public synchronized Collection<Integer> keys(){
+ return mInternalMapReverseLookup.values();
+ }
+
+ @Override
+ public synchronized int size(){
+ return mInternalMap.size();
+ }
+
+ @Override
+ public synchronized int hashCode(){
+ return mInternalMap.hashCode()+mInternalMapReverseLookup.hashCode();
+ }
+
+ @Override
+ public synchronized boolean containsKey(int key){
+ return mInternalMap.containsKey(key);
+ }
+
+ @Override
+ public synchronized boolean containsValue(N value){
+ return mInternalMap.containsValue(value);
+ }
+
+ public synchronized boolean containsKey(N key){
+ return mInternalMapReverseLookup.containsKey(key);
+ }
+
+ public synchronized boolean containsValue(int value){
+ return mInternalMapReverseLookup.containsValue(value);
+ }
+
+ @Override
+ public synchronized boolean isEmpty(){
+ return mInternalMap.isEmpty() && mInternalMapReverseLookup.isEmpty();
+ }
+
+ @Override
+ public synchronized void clear(){
+ this.mInternalID = 0;
+ this.mInternalMap.clear();
+ this.mInternalMapReverseLookup.clear();
+ return;
+ }
+
+ @Override
+ public synchronized N[] toArray() {
+ Collection<N> col = this.mInternalMap.values();
+ @SuppressWarnings("unchecked")
+ N[] val = (N[]) col.toArray();
+ return val;
+ }
+
+ public synchronized Integer[] toArrayInternalMap() {
+ Collection<Integer> col = this.mInternalMapReverseLookup.values();
+ Integer[] val = col.toArray(new Integer[col.size()]);
+ return val;
+ }
+
+}
diff --git a/src/main/java/gtPlusPlus/api/objects/data/Triplet.java b/src/main/java/gtPlusPlus/api/objects/data/Triplet.java
new file mode 100644
index 0000000000..affb03d868
--- /dev/null
+++ b/src/main/java/gtPlusPlus/api/objects/data/Triplet.java
@@ -0,0 +1,27 @@
+package gtPlusPlus.api.objects.data;
+
+public class Triplet<K,V,C> {
+
+ private final K key;
+ private final V value;
+ private final C count;
+
+ public Triplet(final K key, final V value, final C value2){
+ this.key = key;
+ this.value = value;
+ this.count = value2;
+ }
+
+ final public K getValue_1(){
+ return this.key;
+ }
+
+ final public V getValue_2(){
+ return this.value;
+ }
+
+ final public C getValue_3(){
+ return this.count;
+ }
+
+} \ No newline at end of file
diff --git a/src/main/java/gtPlusPlus/api/objects/data/TypeCounter.java b/src/main/java/gtPlusPlus/api/objects/data/TypeCounter.java
new file mode 100644
index 0000000000..601a51392f
--- /dev/null
+++ b/src/main/java/gtPlusPlus/api/objects/data/TypeCounter.java
@@ -0,0 +1,178 @@
+package gtPlusPlus.api.objects.data;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+
+import gtPlusPlus.api.objects.Logger;
+
+
+public class TypeCounter<V> implements Set<V> {
+
+ private Map<String, InternalTypeCounterObject<V>> mInternalMap = new LinkedHashMap<String, InternalTypeCounterObject<V>>();
+ private String mHighestValueKey;
+ private int mHighestValue = 0;
+ private final Class mClass;
+
+ public TypeCounter(Class o) {
+ Logger.WARNING("Created new TypeCounter for "+o.getName());
+ mClass = o;
+ }
+
+ public static class InternalTypeCounterObject<Z> {
+ private final Z mObject;
+ private int mCounter = 0;
+
+ public InternalTypeCounterObject(Z o) {
+ mObject = o;
+ }
+
+ public String hash() {
+ return String.valueOf(mObject.hashCode());
+ }
+
+ public Z get() {
+ return mObject;
+ }
+
+ public void add() {
+ mCounter++;
+ }
+
+ public int count() {
+ return mCounter;
+ }
+
+ }
+
+ public boolean add(V arg0) {
+ return add(arg0, null);
+ }
+
+ public boolean add(V arg0, String aKeyName) {
+ String aKey = aKeyName != null ? aKeyName : arg0.toString();
+ InternalTypeCounterObject<V> aValue = mInternalMap.get(aKey);
+ if (aValue == null) {
+ aValue = new InternalTypeCounterObject<V>((V) arg0);
+ Logger.WARNING("Adding new key to map: "+aKey);
+ }
+ aValue.add();
+ int a = aValue.count();
+ if (a > mHighestValue) {
+ mHighestValue = a;
+ mHighestValueKey = aKey;
+ Logger.WARNING("New Highest Count - "+aKey+":"+a);
+ }
+ mInternalMap.put(aKey, aValue);
+ Logger.WARNING(aKey+":"+a);
+ return true;
+ }
+
+ @Override
+ public boolean addAll(Collection arg0) {
+ boolean aReturn = true;
+ for (Object o : arg0) {
+ if (mClass.isInstance(o)) {
+ V j = (V) o;
+ boolean b = add(j);
+ if (!b) {
+ aReturn = false;
+ }
+ }
+ }
+ return aReturn;
+ }
+
+ @Override
+ public void clear() {
+ mInternalMap.clear();
+ }
+
+ @Override
+ public boolean contains(Object arg0) {
+ return mInternalMap.containsKey(arg0.toString());
+ }
+
+ @Override
+ public boolean containsAll(Collection arg0) {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return mInternalMap.isEmpty();
+ }
+
+ @Override
+ public Iterator iterator() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public boolean remove(Object arg0) {
+ InternalTypeCounterObject<V> aValue = mInternalMap.remove(arg0.toString());
+ if (aValue != null) {
+ return true;
+ }
+ else {
+ return false;
+ }
+ }
+
+ @Override
+ public boolean removeAll(Collection arg0) {
+ boolean aReturn = true;
+ for (Object o : arg0) {
+ boolean a = remove(o);
+ if (!a) {
+ aReturn = false;
+ }
+ }
+ return aReturn;
+ }
+
+ @Override
+ public boolean retainAll(Collection arg0) {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ @Override
+ public int size() {
+ return this.mInternalMap.size();
+ }
+
+ @Override
+ public Object[] toArray() {
+ Object[] aArray = new Object[this.mInternalMap.size()];
+ int aPos = 0;
+ for (String k : this.mInternalMap.keySet()) {
+ if (k != null) {
+ InternalTypeCounterObject<V> aVal = this.mInternalMap.get(k);
+ aArray[aPos++] = new Pair<String, InternalTypeCounterObject<V>>(k, aVal);
+ }
+ }
+ return aArray;
+ }
+
+ @Override
+ public V[] toArray(Object[] a) {
+ Object[] aArray = new Object[a.length];
+ int aPos = 0;
+ for (Object k : a) {
+ if (k != null) {
+ aArray[aPos++] = k;
+ }
+ }
+ return (V[]) aArray;
+ }
+
+ public V getResults() {
+ InternalTypeCounterObject<V> x = mInternalMap.get(mHighestValueKey);
+ return x.get();
+ }
+}
diff --git a/src/main/java/gtPlusPlus/api/objects/data/WeightedCollection.java b/src/main/java/gtPlusPlus/api/objects/data/WeightedCollection.java
new file mode 100644
index 0000000000..f9966474b0
--- /dev/null
+++ b/src/main/java/gtPlusPlus/api/objects/data/WeightedCollection.java
@@ -0,0 +1,102 @@
+package gtPlusPlus.api.objects.data;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.NavigableMap;
+import java.util.Random;
+import java.util.Set;
+import java.util.TreeMap;
+
+import gtPlusPlus.api.objects.random.XSTR;
+
+public class WeightedCollection<E> implements Map<Integer, E> {
+
+ private NavigableMap<Integer, E> map = new TreeMap<Integer, E>();
+ private Random random;
+ private int total = 0;
+
+ public WeightedCollection() {
+ this(new XSTR());
+ }
+
+ public WeightedCollection(Random random) {
+ this.random = random;
+ }
+
+ public E add(int weight, E object) {
+ if (weight <= 0) return null;
+ total += weight;
+ return map.put(total, object);
+ }
+
+ private E next() {
+ int value = random.nextInt(total) + 1; // Can also use floating-point weights
+ return map.ceilingEntry(value).getValue();
+ }
+
+ @Override
+ public int size() {
+ return map.size();
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return map.isEmpty();
+ }
+
+ @Override
+ public boolean containsKey(Object key) {
+ return map.containsKey(key);
+ }
+
+ @Override
+ public boolean containsValue(Object value) {
+ return map.containsValue(value);
+ }
+
+ public E get() {
+ return next();
+ }
+
+ @Override
+ public E get(Object key) {
+ return next();
+ }
+
+ @Override
+ public void putAll(Map m) {
+ map.putAll(m);
+ }
+
+ @Override
+ public void clear() {
+ map.clear();
+ this.total = 0;
+ }
+
+ @Override
+ public Set keySet() {
+ return map.keySet();
+ }
+
+ @Override
+ public Collection values() {
+ return map.values();
+ }
+
+ @Override
+ public Set entrySet() {
+ return map.entrySet();
+ }
+
+ @Override
+ public E put(Integer key, E value) {
+ return add(key, value);
+ }
+
+ @Override
+ public E remove(Object key) {
+ return map.remove(key);
+ }
+
+} \ No newline at end of file
diff --git a/src/main/java/gtPlusPlus/api/objects/data/weakref/WeakAutoMap.java b/src/main/java/gtPlusPlus/api/objects/data/weakref/WeakAutoMap.java
new file mode 100644
index 0000000000..6c55822ca9
--- /dev/null
+++ b/src/main/java/gtPlusPlus/api/objects/data/weakref/WeakAutoMap.java
@@ -0,0 +1,12 @@
+package gtPlusPlus.api.objects.data.weakref;
+
+import java.util.WeakHashMap;
+
+import gtPlusPlus.api.objects.data.AutoMap;
+
+public class WeakAutoMap<T> extends AutoMap<T> {
+ private static final long serialVersionUID = 8328345351801363386L;
+ public WeakAutoMap() {
+ super(new WeakHashMap<Integer, T>());
+ }
+}