Bladeren bron

comparator

Ranides Atterwim 10 jaren geleden
bovenliggende
commit
304cd988df

+ 115 - 0
assira/src/main/java/net/ranides/assira/test/TComparator.java

@@ -0,0 +1,115 @@
+
+package net.ranides.assira.test;
+
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import net.ranides.assira.collection.HashComparator;
+import net.ranides.assira.test.TMap.TItem;
+import net.ranides.assira.test.TMap.TItems;
+
+/**
+ *
+ * @author msieron
+ */
+public class TComparator<K> implements HashComparator<K> {
+	
+	private final Map<K, HGRecord> records = new LinkedHashMap<>();
+
+	public TComparator(TItems<K,?> items) {
+        int i=0;
+		for(TItem<K,?> item : items) {
+			records.put(item.key(), new HGRecord(i, i));
+            i++;
+		}
+	}
+	
+	public TComparator<K> hash(int... hashes) {
+		int mi = (int)Math.ceil((double)records.size() / (double)hashes.length);
+		int i=0;
+		for(HGRecord r : records.values()) {
+			r.hash = hashes[i/mi];
+			i++;
+		}
+		return this;
+	}
+	
+	public TComparator<K> hash(int h, K key) {
+		records.get(key).hash = h;
+		return this;
+	}
+	
+	public TComparator<K> order(int... values) {
+		int mi = (int)Math.ceil((double)records.size() / (double)values.length);
+		int i=0;
+		for(HGRecord r : records.values()) {
+			r.order = values[i/mi];
+			i++;
+		}
+		return this;
+	}
+	
+	public TComparator<K> order(int v, K key) {
+		records.get(key).order = v;
+		return this;
+	}
+    
+    /**
+     * @see TMap#urange(int, int) 
+     * @return 
+     */
+    public TComparator<K> uorder() {
+        int i=0;
+        Iterator<HGRecord> iterator = records.values().iterator();
+        while(true) {
+            if(!iterator.hasNext()) { break; }
+            iterator.next().set(2*i, i);
+            
+            if(!iterator.hasNext()) { break; }
+            iterator.next().set(2*i, i);    // full collision
+            
+            if(!iterator.hasNext()) { break; }
+            iterator.next().set(2*i, i+1);  // hash collision
+            
+            i++;
+        }
+        return this;
+    }
+	
+	@Override
+	public int hashCode(K object) {
+		return records.get(object).hash;
+	}
+    
+    public int[] hashCodes(TItems<K,?> items) {
+        int[] array = new int[items.data.size()];
+        for(int i=0; i<array.length; i++) {
+            array[i] = hashCode(items.at(i).key());
+        }
+		return array;
+	}
+
+	@Override
+	public int compare(K o1, K o2) {
+		return records.get(o1).order - records.get(o2).order;
+	}
+	
+	private static final class HGRecord {
+		
+		public int hash;
+		
+		public int order;
+
+		public HGRecord(int hash, int order) {
+            set(hash, order);
+		}
+        
+        public HGRecord set(int hash, int order) {
+            this.hash = hash;
+            this.order = order;
+            return this;
+        }
+
+	}
+
+}

+ 32 - 55
assira/src/test/java/net/ranides/assira/collection/mockup/TMap.java

@@ -4,7 +4,7 @@
  * @license WTFPL
  * @url http://ranides.net/projects/???
  */
-package net.ranides.assira.collection.mockup;
+package net.ranides.assira.test;
 
 import java.util.AbstractMap;
 import java.util.ArrayList;
@@ -56,18 +56,6 @@ public abstract class TMap<K,V> {
             return new AbstractMap.SimpleImmutableEntry<>(key, value);
         }
         
-        public TItem<K,V> next() {
-            return new TItem<>(that, that.nextKey(key), that.nextValue(value));
-        }
-		
-		public TItem<K,V> next(int count) {
-			TItem<K,V> item = this;
-			for(int i=0; i<count; i++) {
-				item = item.next();
-			}
-			return item;
-		}
-		
 		public Map<K,V> into(Map<K,V> target) {
 			target.put(key(), value());
 			return target;
@@ -126,18 +114,6 @@ public abstract class TMap<K,V> {
             return array;
         }
         
-        public TItems<K,V> next() {
-            return next(1);
-        }
-		
-		public TItems<K,V> next(int count) {
-			TItems<K,V> items = new TItems<K,V>(that);
-            for(int i=0, n=data.size(); i<n; i++) {
-                items.data.add(data.get(i).next(count));
-            }
-            return items;
-		}
-		
 		public TItems<K,V> sort(Comparator<K> cmp) {
 			TItems<K,V> items = new TItems<>(that);
 			items.data.addAll(data);
@@ -162,24 +138,16 @@ public abstract class TMap<K,V> {
             data.set(bi, a);
         }
         
-        public TItems<K,V> item(TItem<K,V> item) {
-            data.add(item);
-            return this;
-        }
+        public TItems<K,V> item(int item) {
+			data.add(that.item(item,0));
+			return this;
+		}
         
-        public TItems<K,V> item(TItems<K,V> items) {
-            data.addAll(items.data);
-            return this;
-        }
-		
-		public TItems<K,V> item(int item) {
-			return TItems.this.item(that.item(item));
+        public TItems<K,V> item(int item, int n) {
+            data.add(that.item(item,n));
+			return this;
 		}
 		
-		public TItems<K,V> item(int item, int next) {
-			return TItems.this.item(that.item(item).next(next));
-		}
-
         @Override
         public Iterator<TItem<K,V>> iterator() {
             return data.iterator();
@@ -205,20 +173,24 @@ public abstract class TMap<K,V> {
         
     }
     
-    protected abstract K key(int index);
+    protected abstract K key(int index, int n);
     
-    protected abstract V value(int index);
+    protected abstract V value(int index, int n);
     
-    protected abstract K nextKey(K key);
+    protected K key(int index) {
+        return key(index, 0);
+    }
     
-    protected abstract V nextValue(V value);
+    protected V value(int index) {
+        return value(index, 0);
+    }
     
     public TItem<K,V> item(int index) {
-        return new TItem<>(this, key(index), value(index));
+        return item(index, 0);
     }
-	
-	public TItem<K,V> item(int index, int next) {
-        return item(index).next(next);
+    
+    public TItem<K,V> item(int index, int n) {
+        return new TItem<>(this, key(index, n), value(index, n));
     }
 	
     public TItems<K,V> list(int... indexes) {
@@ -240,14 +212,19 @@ public abstract class TMap<K,V> {
 	public TItems<K,V> range(int count) {
 		return range(0,count);
 	}
-    
+        
     public TItems<K,V> urange(int begin, int end) {
-        TItems<K,V> items = range(begin, end);
-        return new TItems<>(this).item(items).item(items).item(items.next()).shuffle(777);
+        TItems<K,V> ret = new TItems<>(this);
+        for(int i=begin; i<end; i++) {
+            ret.data.add(item(i,0));
+            ret.data.add(item(i,0));
+            ret.data.add(item(i,1));
+        }
+        return ret;
+    }
+    
+    public TItems<K,V> urange(int count) {
+        return urange(0,count);
     }
-	
-	public TItems<K,V> urange(int count) {
-		return urange(0,count);
-	}
 
 }

+ 1 - 1
assira/src/test/java/net/ranides/assira/collection/lookups/AVLTreeLookupTest.java

@@ -8,7 +8,7 @@ package net.ranides.assira.collection.lookups;
 
 import java.util.Map;
 import java.util.SortedMap;
-import net.ranides.assira.collection.mockup.TMap;
+import net.ranides.assira.test.TMap;
 import net.ranides.assira.collection.mockup.TPoint;
 import static net.ranides.assira.collection.mockup.TPointUtils.*;
 import net.ranides.assira.collection.suite.CollectionSuite;

+ 0 - 77
assira/src/test/java/net/ranides/assira/collection/mockup/TComparator.java

@@ -1,77 +0,0 @@
-
-package net.ranides.assira.collection.mockup;
-
-import java.util.LinkedHashMap;
-import java.util.Map;
-import net.ranides.assira.collection.HashComparator;
-import net.ranides.assira.collection.mockup.TMap.TItem;
-import net.ranides.assira.collection.mockup.TMap.TItems;
-
-/**
- *
- * @author msieron
- */
-public class TComparator<T> implements HashComparator<T> {
-	
-	private final Map<T, HGRecord> records = new LinkedHashMap<>();
-
-	public TComparator(TItems<T,?> items) {
-		for(TItem<T,?> item : items) {
-			records.put(item.key(), new HGRecord(item.hashCode(), item.hashCode()));
-		}
-	}
-	
-	public TComparator<T> hash(int... hashes) {
-		int mi = (int)Math.ceil((double)records.size() / (double)hashes.length);
-		int i=0;
-		for(HGRecord r : records.values()) {
-			r.hash = hashes[i/mi];
-			i++;
-		}
-		return this;
-	}
-	
-	public TComparator<T> hash(int h, T key) {
-		records.get(key).hash = h;
-		return this;
-	}
-	
-	public TComparator<T> order(int... values) {
-		int mi = (int)Math.ceil((double)records.size() / (double)values.length);
-		int i=0;
-		for(HGRecord r : records.values()) {
-			r.order = values[i/mi];
-			i++;
-		}
-		return this;
-	}
-	
-	public TComparator<T> order(int v, T key) {
-		records.get(key).order = v;
-		return this;
-	}
-	
-	@Override
-	public int hashCode(T object) {
-		return records.get(object).hash;
-	}
-
-	@Override
-	public int compare(T o1, T o2) {
-		return records.get(o1).order - records.get(o2).order;
-	}
-	
-	private static final class HGRecord {
-		
-		public int hash;
-		
-		public int order;
-
-		public HGRecord(int hash, int order) {
-			this.hash = hash;
-			this.order = order;
-		}
-
-	}
-
-}

+ 5 - 14
assira/src/test/java/net/ranides/assira/collection/mockup/TPoint.java

@@ -6,6 +6,7 @@
  */
 package net.ranides.assira.collection.mockup;
 
+import net.ranides.assira.test.TMap;
 import java.io.Serializable;
 import net.ranides.assira.collection.HashComparator;
 import net.ranides.assira.generic.HashUtils;
@@ -97,23 +98,13 @@ public class TPoint implements Serializable {
     public static final TMap<TPoint, Integer> STD_INT_MAP = new TMap<TPoint, Integer>(){
 
         @Override
-        protected TPoint key(int index) {
-            return new TPoint(index, index, 10*index);
+        protected TPoint key(int index, int n) {
+            return new TPoint(index, index, 10*index+n);
         }
 
         @Override
-        protected Integer value(int index) {
-            return 10*index;
-        }
-
-        @Override
-        protected TPoint nextKey(TPoint key) {
-            return new TPoint(key.x, key.y, key.z+1);
-        }
-
-        @Override
-        protected Integer nextValue(Integer value) {
-            return value + 1;
+        protected Integer value(int index, int n) {
+            return 10 * index + n;
         }
 
     };

+ 5 - 5
assira/src/test/java/net/ranides/assira/collection/suite/AHashLookupTester.java

@@ -8,7 +8,7 @@ package net.ranides.assira.collection.suite;
 
 import javax.annotation.Resource;
 import net.ranides.assira.collection.lookups.AHashLookup;
-import net.ranides.assira.collection.mockup.TMap;
+import net.ranides.assira.test.TMap;
 import net.ranides.assira.junit.QAssert;
 import org.junit.Test;
 
@@ -23,9 +23,9 @@ public final class AHashLookupTester<K> {
 
     @Test
     public void basicTrim(AHashLookup<K> target) {
-        $map.urange(0, 1024).into(target);
+        $map.range(1024).into(target);
         target.clear();
-        $map.urange(0, 128).into(target);
+        $map.range(128).into(target);
         target.trim();
     }
     
@@ -42,9 +42,9 @@ public final class AHashLookupTester<K> {
     }
     
     private void basicTrim(AHashLookup<K> target, int put1st, int put2nd, int n) {
-        $map.urange(0, put1st).into(target);
+        $map.range(put1st).into(target);
         target.clear();
-        $map.urange(0, put2nd).into(target);
+        $map.range(put2nd).into(target);
         target.trim(n);
     }
     

+ 5 - 5
assira/src/test/java/net/ranides/assira/collection/suite/AHashMapTester.java

@@ -8,7 +8,7 @@ package net.ranides.assira.collection.suite;
 
 import javax.annotation.Resource;
 import net.ranides.assira.collection.maps.AHashMap;
-import net.ranides.assira.collection.mockup.TMap;
+import net.ranides.assira.test.TMap;
 import net.ranides.assira.junit.QAssert;
 import org.junit.Test;
 
@@ -23,9 +23,9 @@ public final class AHashMapTester<K> {
     
     @Test
     public void basicTrim(AHashMap<K, Integer> target) {
-        $map.urange(0, 1024).into(target);
+        $map.range(1024).into(target);
         target.clear();
-        $map.urange(0, 128).into(target);
+        $map.range(128).into(target);
         target.trim();
     }
     
@@ -42,9 +42,9 @@ public final class AHashMapTester<K> {
     }
     
     private void basicTrim(AHashMap<K, Integer> target, int put1st, int put2nd, int n) {
-        $map.urange(0, put1st).into(target);
+        $map.range(put1st).into(target);
         target.clear();
-        $map.urange(0, put2nd).into(target);
+        $map.range(put2nd).into(target);
         target.trim(n);
     }
     

+ 3 - 3
assira/src/test/java/net/ranides/assira/collection/suite/LookupTester.java

@@ -14,9 +14,9 @@ import net.ranides.assira.collection.HashComparator;
 import net.ranides.assira.collection.IntCollection;
 import net.ranides.assira.collection.iterators.IntIterator;
 import net.ranides.assira.collection.lookups.Lookup;
-import net.ranides.assira.collection.mockup.TMap;
-import net.ranides.assira.collection.mockup.TMap.TItem;
-import net.ranides.assira.collection.mockup.TMap.TItems;
+import net.ranides.assira.test.TMap;
+import net.ranides.assira.test.TMap.TItem;
+import net.ranides.assira.test.TMap.TItems;
 import net.ranides.assira.junit.QAssert;
 import static org.junit.Assert.*;
 import org.junit.Test;

+ 74 - 0
assira/src/test/java/net/ranides/assira/test/TComparatorTest.java

@@ -0,0 +1,74 @@
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/???
+ */
+package net.ranides.assira.test;
+
+import java.util.Arrays;
+import java.util.TreeMap;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public class TComparatorTest {
+    
+    
+    private static final TMap<String, String> SSMAP = new TMap<String, String>(){
+
+        @Override
+        protected String key(int index, int n) {
+            assert n==0;
+            return "K"+index;
+        }
+
+        @Override
+        protected String value(int index, int n) {
+            assert n==0;
+            return "V"+index;
+        }
+
+    };
+        
+    @Test
+    public void testF1() {
+        TComparator<String> cmp = new TComparator<>(SSMAP.range(8));
+        TreeMap<String, String> map = new TreeMap<>(cmp);
+        SSMAP.range(8).into(map);
+        assertEquals(8, map.size());
+        assertEquals("{K0=V0, K1=V1, K2=V2, K3=V3, K4=V4, K5=V5, K6=V6, K7=V7}", map.toString());
+    }
+    
+
+    @Test
+    public void testF2() {
+        TComparator<String> cmp = new TComparator<>(SSMAP.range(8));
+        cmp.hash(1,2).order(7,6,5,4);
+        /*
+            value 0 1 2 3 4 5 6 7
+            hash  1 1 1 1 2 2 2 2
+            order 7 7 6 6 5 5 4 4
+        */
+        
+        assertEquals(1, cmp.hashCode(SSMAP.item(0).key()) );
+        assertEquals(1, cmp.hashCode(SSMAP.item(1).key()) );
+        assertEquals(1, cmp.hashCode(SSMAP.item(2).key()) );
+        assertEquals(1, cmp.hashCode(SSMAP.item(3).key()) );
+        assertEquals(2, cmp.hashCode(SSMAP.item(4).key()) );
+        assertEquals(2, cmp.hashCode(SSMAP.item(5).key()) );
+        assertEquals(2, cmp.hashCode(SSMAP.item(6).key()) );
+        assertEquals(2, cmp.hashCode(SSMAP.item(7).key()) );
+        
+        assertArrayEquals(new int[]{1,1,1,1,2,2,2,2}, cmp.hashCodes(SSMAP.range(8)));
+        
+        TreeMap<String, String> map = new TreeMap<>(cmp);
+        SSMAP.range(8).into(map);
+        assertEquals(4, map.size());
+        assertEquals("[V7, V5, V3, V1]", map.values().toString());
+    }
+    
+}