Permalink
Browse files

experiment with vectormap and pvectormap

  • Loading branch information...
1 parent 3011c7f commit bdf85ec870e77664f975914ad1057c303b5e66a0 @krukow committed Oct 14, 2010
View
@@ -4,5 +4,6 @@
<classpathentry kind="src" path="test"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="lib" path="lib/junit-4.8.2.jar"/>
+ <classpathentry kind="lib" path="lib/jsr166y.jar"/>
<classpathentry kind="output" path="classes"/>
</classpath>
View
Binary file not shown.
@@ -56,6 +56,7 @@ public Object setValue(Object value){
throw new UnsupportedOperationException();
}
+
/*
public boolean equals(Object obj){
@@ -572,5 +572,6 @@ public int count(){
public IPersistentMap meta(){
return _meta;
}
+
}
}
@@ -36,4 +36,5 @@ public V getValue(){
return val();
}
+
}
@@ -385,11 +385,6 @@ public IPersistentCollection persistent() {
}
-static interface Position {
- INode getINode();
- int getIndex();
-}
-
static interface INode extends Serializable {
INode assoc(int shift, int hash, Object key, Object val, Box addedLeaf);
@@ -16,8 +16,12 @@
import java.util.Iterator;
import java.util.List;
import java.util.Stack;
+import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicReference;
+import jsr166y.ForkJoinPool;
+import jsr166y.RecursiveTask;
+
public class PersistentVector<T> extends APersistentVector<T> implements IObj, IEditableCollection<T>{
static class Node implements Serializable {
@@ -94,6 +98,15 @@
this.tail = tail;
}
+public PersistentVector(IPersistentMap meta, int cnt, int shift, Node root, Object[] tail, IFn f) {
+ this._meta = meta();
+ this.cnt = cnt;
+ this.shift = shift;
+ this.tail = mapArray(f,Util.ret1(tail,tail=null));
+ this.root = mapNode(f,Util.ret1(root, root=null), this.shift);
+}
+
+
public TransientVector<T> asTransient(){
return new TransientVector<T>(this);
}
@@ -851,4 +864,85 @@ static public void main(String[] args){
}
// */
+
+public static IPersistentVector vectormap(IFn f, PersistentVector v) {
+ return new PersistentVector(v._meta,v.cnt,v.shift,v.root,Util.ret1(v.tail,v=null),f);
+}
+static final ForkJoinPool mainPool = new ForkJoinPool();
+
+public static IPersistentVector pvectormap(IFn f, PersistentVector v) {
+ Node invoke = mainPool.invoke(new PMapTask(f, v.shift,v.root));
+ return new PersistentVector(v._meta,v.cnt,v.shift,invoke, mapArray(f,v.tail));
+}
+
+static final class PMapTask extends RecursiveTask<Node> {
+
+ private IFn f;
+ private int shift;
+ private Node node;
+
+ public PMapTask(IFn f, int shift, Node node) {
+ this.f = f;
+ this.shift = shift;
+ this.node = node;
+ }
+
+ public Node compute() {
+ if (node == null) {
+ return null;
+ }
+ if (this.shift <= 5) {
+ return mapNode(f,node,shift);
+ }
+
+ PMapTask[] tasks = new PMapTask[node.array.length];
+ shift -= 5;
+ for (int i=0;i<tasks.length;i++) {
+ tasks[i] = new PMapTask(f,shift,(Node) node.array[i]);
+ }
+ invokeAll(tasks);
+ Node[] nodes = new Node[node.array.length];
+ try {
+ for (int i=0;i<tasks.length;i++) {
+ nodes[i] = tasks[i].get();
+ }
+ return new Node(null,nodes);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ throw new RuntimeException(e);
+ } catch (ExecutionException e) {
+ throw new RuntimeException(e);
+ }
+ }
+}
+
+private static Object[] mapArray(IFn f, Object[] arr) {
+ Object[] res = new Object[arr.length];
+ System.arraycopy(arr, 0, res, 0, arr.length);
+ arr = null;
+ try {
+ for(int i=0;i<res.length;i++) {
+ res[i] = f.invoke(res[i]);
+ }
+ return res;
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+}
+
+private static Node mapNode(IFn f, Node node, int level) {
+ if (node == null) {return null;}
+ if (level == 0) {
+ return new Node(null,mapArray(f, Util.ret1(node.array, node=null)));
+ }
+ Object[] newArr = new Object[node.array.length];
+ System.arraycopy(node.array, 0, newArr, 0, node.array.length);
+ node=null;
+ level -= 5;
+ for (int i=0;i<newArr.length;i++) {
+ newArr[i] = mapNode(f,Util.ret1((Node) newArr[i], newArr[i]=null),level);
+ }
+ return new Node(null,newArr);
+}
+
}
@@ -14,6 +14,8 @@
import java.math.BigInteger;
+import com.trifork.clj_ds.PersistentVector.Node;
+
public class Util{
static public boolean equiv(Object k1, Object k2){
if(k1 == k2)
@@ -84,6 +86,12 @@ static public boolean isInteger(Object x){
static public Object ret1(Object ret, Object nil){
return ret;
}
+static public Node ret1(Node ret, Object nil){
+ return ret;
+}
+static public Object[] ret1(Object[] ret, Object nil){
+ return ret;
+}
static public ISeq ret1(ISeq ret, Object nil){
return ret;
@@ -150,7 +150,7 @@ public final void testBadInvocCreateObjectArray() {
*/
@Test
public final void testIterator() {
- PersistentHashMap<Integer, Integer> dsMap = PersistentHashMap.emptyMap();
+ IPersistentMap<Integer, Integer> dsMap = PersistentHashMap.emptyMap();
HashSet<Integer> hs = null;
for (int i = 0; i < 33000; i++) {
hs = new HashSet<Integer>();
@@ -160,7 +160,7 @@ public final void testIterator() {
}
assertEquals(i, hs.size());
Integer o = new Integer(i);
- dsMap = (PersistentHashMap<Integer, Integer>) dsMap.assoc(o, o);
+ dsMap = dsMap.assoc(o, o);
}
}
@@ -0,0 +1,81 @@
+package com.trifork.clj_ds.test;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.junit.Test;
+
+import com.trifork.clj_ds.Cons;
+import com.trifork.clj_ds.MapEntry;
+import com.trifork.clj_ds.PersistentArrayMap;
+import com.trifork.clj_ds.PersistentStructMap;
+import com.trifork.clj_ds.PersistentStructMap.Def;
+
+public class PersistentNonHashMapsTest {
+
+/*
+
+PersistentArrayMap.java
+PersistentStructMap.java
+PersistentTreeMap.java
+
+*/
+ @Test
+ public final void testArrayMap() {
+ PersistentArrayMap<String, Integer> am = PersistentArrayMap.createWithCheck(new Object[]{
+ "1",1,"2",2,"3",3,"4",4,"5",5});
+ List<Map.Entry<String, Integer>> l = new ArrayList<Map.Entry<String,Integer>>();
+ int i=1;
+ for (Map.Entry<String, Integer> e:am) {
+ l.add(e);
+ assertEquals(i, (int) e.getValue());
+ assertEquals(i+"", e.getKey());
+ i += 1;
+ }
+
+ i=3;
+ for (Iterator<Entry<String, Integer>> it = am.iteratorFrom("3");it.hasNext();) {
+ System.out.println(l.get(i));
+ assertEquals(l.get(i++), it.next());
+ }
+ assertEquals(5, i);
+
+ for(Iterator<Entry<String, Integer>> rit = am.reverseIterator();rit.hasNext();) {
+ assertEquals(l.get(--i), rit.next());
+ }
+ assertEquals(0,i);
+
+ }
+
+ @Test
+ public final void testStructMap() {
+ Def def = PersistentStructMap.createSlotMap(
+ new Cons("1", new Cons("2", new Cons("3",null))));
+ PersistentStructMap<String, Integer> sm = PersistentStructMap.construct(def,
+ new Cons(1,new Cons(2,new Cons(3,null))));
+
+ for (int i=4;i<10000;i++) {
+ sm = (PersistentStructMap<String, Integer>) sm.assoc(i+"", i);
+ }
+ List<Map.Entry<String, Integer>> l = new ArrayList<Map.Entry<String,Integer>>();
+ for (Map.Entry<String, Integer> e: sm) {l.add(e);}
+ assertEquals(new MapEntry("1",1),l.get(0));
+ assertEquals(new MapEntry("2",2),l.get(1));
+ assertEquals(new MapEntry("3",3),l.get(2));
+
+ assertEquals(9999,l.size());
+
+ int i=9999;
+ for (Iterator<Entry<String, Integer>> rit = sm.reverseIterator();rit.hasNext();) {
+ assertEquals(l.get(--i),rit.next());
+ }
+ assertEquals(0,i);
+
+
+ }
+}
Oops, something went wrong.

0 comments on commit bdf85ec

Please sign in to comment.