|
|
@@ -0,0 +1,172 @@
|
|
|
+/*
|
|
|
+ * @author Ranides Atterwim <ranides@gmail.com>
|
|
|
+ * @copyright Ranides Atterwim
|
|
|
+ * @license WTFPL
|
|
|
+ * @url http://ranides.net/projects/???
|
|
|
+ */
|
|
|
+package net.ranides.assira.collection.mockup;
|
|
|
+
|
|
|
+import java.util.AbstractMap;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Comparator;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Map.Entry;
|
|
|
+import java.util.Random;
|
|
|
+import java.util.TreeMap;
|
|
|
+import net.ranides.assira.collection.arrays.ArrayAllocator;
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @author Ranides Atterwim <ranides@gmail.com>
|
|
|
+ */
|
|
|
+public abstract class TMap<K,V> {
|
|
|
+
|
|
|
+ public final class TItem {
|
|
|
+
|
|
|
+ private final K key;
|
|
|
+
|
|
|
+ private final V value;
|
|
|
+
|
|
|
+ protected TItem(K key, V value) {
|
|
|
+ this.key = key;
|
|
|
+ this.value = value;
|
|
|
+ }
|
|
|
+
|
|
|
+ public K key() {
|
|
|
+ return key;
|
|
|
+ }
|
|
|
+
|
|
|
+ public V value() {
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Entry<K,V> entry() {
|
|
|
+ return new AbstractMap.SimpleImmutableEntry<>(key, value);
|
|
|
+ }
|
|
|
+
|
|
|
+ public TItem next() {
|
|
|
+ return new TItem(nextKey(key), nextValue(value));
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public final class TItems implements Iterable<TItem> {
|
|
|
+
|
|
|
+ private final List<TItem> data = new ArrayList<>();
|
|
|
+
|
|
|
+ public K[] keys() {
|
|
|
+ K[] array = ArrayAllocator.allocate(key(0).getClass(), data.size());
|
|
|
+ for(int i=0,n=data.size(); i<n; i++) {
|
|
|
+ array[i] = data.get(i).key();
|
|
|
+ }
|
|
|
+ return array;
|
|
|
+ }
|
|
|
+
|
|
|
+ public V[] values() {
|
|
|
+ V[] array = ArrayAllocator.allocate(value(0).getClass(), data.size());
|
|
|
+ for(int i=0,n=data.size(); i<n; i++) {
|
|
|
+ array[i] = data.get(i).value();
|
|
|
+ }
|
|
|
+ return array;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Entry<K,V>[] entries() {
|
|
|
+ Entry<K,V> array[] = new Entry[data.size()];
|
|
|
+ for(int i=0, n=data.size(); i<n; i++) {
|
|
|
+ array[i] = data.get(i).entry();
|
|
|
+ }
|
|
|
+ return array;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Map<K,V> map(Comparator<K> cmp) {
|
|
|
+ Map<K, V> target = new TreeMap<>(cmp);
|
|
|
+ for (int i = 0, n=data.size(); i<n; i++) {
|
|
|
+ target.put(data.get(i).key(), data.get(i).value());
|
|
|
+ }
|
|
|
+ return target;
|
|
|
+ }
|
|
|
+
|
|
|
+ public TItems next() {
|
|
|
+ TItems items = new TItems();
|
|
|
+ for(int i=0, n=data.size(); i<n; i++) {
|
|
|
+ items.data.add(data.get(i).next());
|
|
|
+ }
|
|
|
+ return items;
|
|
|
+ }
|
|
|
+
|
|
|
+ public TItems shuffle(long seed) {
|
|
|
+ int n = data.size();
|
|
|
+ Random random = new Random(seed);
|
|
|
+ for(int i=0; i<n; i++) {
|
|
|
+ int a = random.nextInt(n);
|
|
|
+ int b = random.nextInt(n);
|
|
|
+ swap(a, b);
|
|
|
+ }
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void swap(int ai, int bi) {
|
|
|
+ TItem a = data.get(ai);
|
|
|
+ data.set(ai, data.get(bi));
|
|
|
+ data.set(bi, a);
|
|
|
+ }
|
|
|
+
|
|
|
+ public TItems add(TItem item) {
|
|
|
+ data.add(item);
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public TItems add(TItems items) {
|
|
|
+ data.addAll(items.data);
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Iterator<TItem> iterator() {
|
|
|
+ return data.iterator();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ protected abstract K key(int index);
|
|
|
+
|
|
|
+ protected abstract V value(int index);
|
|
|
+
|
|
|
+ protected abstract K nextKey(K key);
|
|
|
+
|
|
|
+ protected abstract V nextValue(V value);
|
|
|
+
|
|
|
+ public TItem item(int index) {
|
|
|
+ return new TItem(key(index), value(index));
|
|
|
+ }
|
|
|
+
|
|
|
+ public TItems list(int... indexes) {
|
|
|
+ TItems ret = new TItems();
|
|
|
+ for(int i=0; i<indexes.length; i++) {
|
|
|
+ ret.data.add(item(indexes[i]));
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ public TItems range(int begin, int end) {
|
|
|
+ TItems ret = new TItems();
|
|
|
+ for(int i=begin; i<end; i++) {
|
|
|
+ ret.data.add(item(i));
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ public TItems urange(int begin, int end) {
|
|
|
+ TItems items = range(begin, end);
|
|
|
+ return new TItems().add(items).add(items).add(items.next()).shuffle(777);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void put(Map<K,V> target, TItems items) {
|
|
|
+ for(TItem item : items) {
|
|
|
+ target.put(item.key(), item.value());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|