clean up iterator stuff...
authorbdemsky <bdemsky>
Fri, 26 Jun 2009 09:18:18 +0000 (09:18 +0000)
committerbdemsky <bdemsky>
Fri, 26 Jun 2009 09:18:18 +0000 (09:18 +0000)
Robust/src/Benchmarks/SingleTM/Bayes/Data.java
Robust/src/Benchmarks/SingleTM/Bayes/IntList.java
Robust/src/Benchmarks/SingleTM/Bayes/IntListIter.java [new file with mode: 0644]
Robust/src/Benchmarks/SingleTM/Bayes/Learner.java
Robust/src/Benchmarks/SingleTM/Bayes/List.java
Robust/src/Benchmarks/SingleTM/Bayes/ListIter.java [new file with mode: 0644]
Robust/src/Benchmarks/SingleTM/Bayes/Net.java
Robust/src/Benchmarks/SingleTM/Bayes/QuickSort.java [new file with mode: 0644]

index f05d7fdc8035ce3f9ad44eb3c669a32e3475040b..8ffbde1b1ea17a7bcf3acf0dc0a414cb49d4c90c 100644 (file)
@@ -185,11 +185,10 @@ public class Data {
 
           IntList parentIdListPtr = netPtr.net_getParentIdListPtr(id);
           IntListNode it = parentIdListPtr.head;
-          parentIdListPtr.list_iter_reset(it);
 
-          while (parentIdListPtr.list_iter_hasNext(it)) {
+          while (it.nextPtr!=null) {
             it = it.nextPtr;
-            int parentId = parentIdListPtr.list_iter_next(it);
+            int parentId = it.dataPtr;
             if((status = workQueuePtr.queue_push(parentId)) == false) {
               System.out.println("Assert failed: status= "+ status + "should be true");
               System.exit(0);
@@ -228,11 +227,10 @@ public class Data {
         IntList parentIdListPtr = netPtr.net_getParentIdListPtr(v);
         int index = 0;
         IntListNode it = parentIdListPtr.head;
-        parentIdListPtr.list_iter_reset(it);
 
-        while (parentIdListPtr.list_iter_hasNext(it)) {
+        while (it.nextPtr!=null) {
           it = it.nextPtr;
-          int parentId = parentIdListPtr.list_iter_next(it);
+          int parentId = it.dataPtr;
           int value = records[startindex + parentId];
           if(value == DATA_INIT) {
             System.out.println("Assert failed value should be != DATA_INIT");
index 55b54114a04748be31be289127ef5c77ee8ca1c9..907d23d4538486fbddf7be358b09137a99107020 100644 (file)
@@ -67,9 +67,9 @@ public class IntList {
    * =============================================================================
    */
   public void
-    list_iter_reset (IntListNode itPtr)
+    list_iter_reset (IntListIter itPtr)
     {
-      itPtr = head;
+      itPtr.ptr = head;
     }
 
   /* =============================================================================
@@ -77,9 +77,9 @@ public class IntList {
    * =============================================================================
    */
   public boolean
-    list_iter_hasNext (IntListNode itPtr)
+    list_iter_hasNext (IntListIter itPtr)
     {
-      return ((itPtr.nextPtr != null) ? true : false);
+      return (itPtr.ptr.nextPtr != null);
     }
 
 
@@ -88,9 +88,11 @@ public class IntList {
    * =============================================================================
    */
   public int
-    list_iter_next (IntListNode itPtr)
+    list_iter_next (IntListIter itPtr)
     {
-      return itPtr.dataPtr;
+      int val=itPtr.ptr.dataPtr;
+      itPtr.ptr=itPtr.ptr.nextPtr;
+      return val;
     }
 
   /* =============================================================================
diff --git a/Robust/src/Benchmarks/SingleTM/Bayes/IntListIter.java b/Robust/src/Benchmarks/SingleTM/Bayes/IntListIter.java
new file mode 100644 (file)
index 0000000..4d12512
--- /dev/null
@@ -0,0 +1,3 @@
+public class IntListIter {
+  IntListNode ptr;
+}
\ No newline at end of file
index 428ce3abb19df021475750c6d47a13e2227e2295..870b152e9ba6b3a9758b8ddab5b232b1e1387b5c 100644 (file)
@@ -406,11 +406,10 @@ public class Learner {
 
 #ifdef TEST_LEARNER
       ListNode it = learnerPtr.taskListPtr.head;
-      learnerPtr.taskListPtr.list_iter_reset(it);
 
-      while (learnerPtr.taskListPtr.list_iter_hasNext(it)) {
+     while (it.nextPtr!=null) {
         it = it.nextPtr;
-        LearnerTask taskPtr = learnerPtr.taskListPtr.list_iter_next(it);
+        LearnerTask taskPtr = it.dataPtr;
         System.out.println("[task] op= "+ taskPtr.op +" from= "+taskPtr.fromId+" to= " +taskPtr.toId+
            " score= " + taskPtr.score);
       }
@@ -428,10 +427,9 @@ public class Learner {
       LearnerTask taskPtr = null;
 
       ListNode it = taskListPtr.head;
-      taskListPtr.list_iter_reset(it);
-      if (taskListPtr.list_iter_hasNext(it)) {
+      if (it.nextPtr!=null) {
         it = it.nextPtr;
-        taskPtr = taskListPtr.list_iter_next(it); 
+        taskPtr = it.dataPtr;
         boolean status = taskListPtr.list_remove(taskPtr);
         if(status == false) {
           System.out.println("Assert failed: when removing from a list in TMpopTask()");
@@ -458,10 +456,9 @@ public class Learner {
 
       IntList parentIdListPtr = netPtr.net_getParentIdListPtr(id);
       IntListNode it = parentIdListPtr.head;
-      parentIdListPtr.list_iter_reset(it);
-      while (parentIdListPtr.list_iter_hasNext(it)) {
+      while (it.nextPtr!=null) {
         it = it.nextPtr;
-        int parentId = parentIdListPtr.list_iter_next(it);
+        int parentId = it.dataPtr;
         boolean status = parentQueryVectorPtr.vector_pushBack(queries[parentId]);
         if(status == false) {
           System.out.println("Assert failed: unable to pushBack in queue");
@@ -486,11 +483,10 @@ public class Learner {
 
       IntList parentIdListPtr = netPtr.net_getParentIdListPtr(id);
       IntListNode it = parentIdListPtr.head;
-      parentIdListPtr.list_iter_reset(it);
 
-      while (parentIdListPtr.list_iter_hasNext(it)) {
+      while (it.nextPtr!=null) {
         it = it.nextPtr;
-        int parentId = parentIdListPtr.list_iter_next(it);
+        int parentId = it.dataPtr;
         boolean status = parentQueryVectorPtr.vector_pushBack(queries[parentId]);
         if(status == false) {
           System.out.println("Assert failed: unable to pushBack in queue in TMpopulateParentQueryVector()");
@@ -719,11 +715,10 @@ public class Learner {
       {
 
         IntListNode it = parentIdListPtr.head;
-        parentIdListPtr.list_iter_reset(it);
 
-        while( parentIdListPtr.list_iter_hasNext(it)) {
+        while(it.nextPtr!=null) {
           it = it.nextPtr;
-          int parentId = parentIdListPtr.list_iter_next(it);
+          int parentId = it.dataPtr;
           invalidBitmapPtr.bitmap_set(parentId); // invalid since already have edge 
         }
 
index d0fe80a3fc2002db0ce95c7f5e9f8efa7c28bd82..1e102826197f494dd06525aa8c077868b064ed7f 100644 (file)
@@ -90,9 +90,9 @@ public class List {
    * =============================================================================
    */
   public void
-    list_iter_reset (ListNode itPtr)
+    list_iter_reset (ListIter itPtr)
     {
-      itPtr = head;
+      itPtr.ptr = head;
     }
 
   /* =============================================================================
@@ -100,9 +100,9 @@ public class List {
    * =============================================================================
    */
   public boolean
-    list_iter_hasNext (ListNode itPtr)
+    list_iter_hasNext (ListIter itPtr)
     {
-      return ((itPtr.nextPtr != null) ? true : false);
+      return itPtr.ptr.nextPtr != null;
     }
 
   /* =============================================================================
@@ -110,10 +110,11 @@ public class List {
    * =============================================================================
    */
   public LearnerTask
-    list_iter_next (ListNode itPtr)
+    list_iter_next (ListIter itPtr)
     {
-      //itPtr = itPtr.nextPtr;
-      return itPtr.dataPtr;
+      LearnerTask lt=itPtr.ptr.dataPtr;
+      itPtr.ptr=itPtr.ptr.nextPtr;
+      return lt;
     }
 
   /* =============================================================================
diff --git a/Robust/src/Benchmarks/SingleTM/Bayes/ListIter.java b/Robust/src/Benchmarks/SingleTM/Bayes/ListIter.java
new file mode 100644 (file)
index 0000000..0dadc30
--- /dev/null
@@ -0,0 +1,3 @@
+public class ListIter {
+  public ListNode ptr;
+}
\ No newline at end of file
index 4b39edaac3913db2234890705abdd60619606150..817d7b5c947d60e0bfc14514589ace8fe5512f40 100644 (file)
@@ -244,11 +244,10 @@ public class Net {
 
       IntList parentIdListPtr = childNodePtr.parentIdListPtr;
       IntListNode it = parentIdListPtr.head; //intialize iterator
-      parentIdListPtr.list_iter_reset(it);
 
-      while (parentIdListPtr.list_iter_hasNext(it)) {
+      while (it.nextPtr!=null) {
         it = it.nextPtr;
-        int parentId = parentIdListPtr.list_iter_next(it);
+        int parentId = it.dataPtr;
         if (parentId == fromId) {
           return true;
         }
@@ -269,11 +268,10 @@ public class Net {
 
       IntList parentIdListPtr = childNodePtr.parentIdListPtr;
       IntListNode it = parentIdListPtr.head;//initialize iterator
-      parentIdListPtr.list_iter_reset(it);
 
-      while (parentIdListPtr.list_iter_hasNext(it)) {
+      while (it.nextPtr!=null) {
         it = it.nextPtr;
-        int parentId = parentIdListPtr.list_iter_next(it);
+        int parentId = it.dataPtr;
         if (parentId == fromId) {
           return true;
         }
@@ -323,11 +321,10 @@ public class Net {
         NetNode nodePtr = (NetNode) (nodeVectorPtr.vector_at(id));
         IntList childIdListPtr = nodePtr.childIdListPtr;
         IntListNode it = childIdListPtr.head;
-        childIdListPtr.list_iter_reset(it);
 
-        while (childIdListPtr.list_iter_hasNext(it)) {
+        while (it.nextPtr!=null) {
           it = it.nextPtr;
-          int childId = childIdListPtr.list_iter_next(it);
+          int childId = it.dataPtr;
           if (!visitedBitmapPtr.bitmap_isSet(childId)) {
             status = workQueuePtr.queue_push(childId);
             if(status == false) {
@@ -353,11 +350,10 @@ public class Net {
         nodePtr.mark = NET_NODE_MARK_TEST;
         IntList childIdListPtr = nodePtr.childIdListPtr;
         IntListNode it = childIdListPtr.head;
-        childIdListPtr.list_iter_reset(it);
 
-        while (childIdListPtr.list_iter_hasNext(it)) {
+        while (it.nextPtr!=null) {
           it = it.nextPtr;
-          int childId = childIdListPtr.list_iter_next(it);
+          int childId = it.dataPtr;
           NetNode childNodePtr = (NetNode)(nodeVectorPtr.vector_at(childId));
           if (isCycle(nodeVectorPtr, childNodePtr)) {
             return true;
@@ -475,11 +471,10 @@ public class Net {
         NetNode nodePtr = (NetNode)(nodeVectorPtr.vector_at(id));
         IntList parentIdListPtr = nodePtr.parentIdListPtr;
         IntListNode it = parentIdListPtr.head;
-        parentIdListPtr.list_iter_reset(it);
 
-        while (parentIdListPtr.list_iter_hasNext(it)) {
+        while (it.nextPtr!=null) {
           it = it.nextPtr;
-          int parentId = parentIdListPtr.list_iter_next(it);
+          int parentId = it.dataPtr;
           status = ancestorBitmapPtr.bitmap_set(parentId);
           if(status == false) {
             System.out.println("Assert failed: for bitmap_set in net_findAncestors()");
@@ -502,11 +497,10 @@ public class Net {
         NetNode nodePtr = (NetNode)(nodeVectorPtr.vector_at(parentId));
         IntList grandParentIdListPtr = nodePtr.parentIdListPtr;
         IntListNode it = grandParentIdListPtr.head;
-        grandParentIdListPtr.list_iter_reset(it);
 
-        while (grandParentIdListPtr.list_iter_hasNext(it)) {
+        while (it.nextPtr!=null) {
           it = it.nextPtr;
-          int grandParentId = grandParentIdListPtr.list_iter_next(it);
+          int grandParentId = it.dataPtr;
           if (!ancestorBitmapPtr.bitmap_isSet(grandParentId)) {
             if((status = ancestorBitmapPtr.bitmap_set(grandParentId)) == false) {
               System.out.println("Assert failed: for ancestorBitmapPtr bitmap_set in net_findAncestors()");
@@ -550,11 +544,10 @@ public class Net {
         NetNode nodePtr = (NetNode)(nodeVectorPtr.vector_at(id));
         IntList childIdListPtr = nodePtr.childIdListPtr;
         IntListNode it = childIdListPtr.head;
-        childIdListPtr.list_iter_reset(it);
 
-        while (childIdListPtr.list_iter_hasNext(it)) {
+        while (it.nextPtr!=null) {
           it = it.nextPtr;
-          int childId = childIdListPtr.list_iter_next(it);
+          int childId = it.dataPtr;
           if((status = descendantBitmapPtr.bitmap_set(childId)) == false) {
             System.out.println("Assert failed: for descendantBitmapPtr.bitmap_set in net_findDescendants()");
             System.exit(0);
@@ -578,11 +571,10 @@ public class Net {
         NetNode nodePtr = (NetNode)(nodeVectorPtr.vector_at(childId));
         IntList grandChildIdListPtr = nodePtr.childIdListPtr;
         IntListNode it = grandChildIdListPtr.head;
-        grandChildIdListPtr.list_iter_reset(it);
 
-        while (grandChildIdListPtr.list_iter_hasNext(it)) {
+        while (it.nextPtr!=null) {
           it = it.nextPtr;
-          int grandChildId = grandChildIdListPtr.list_iter_next(it);
+          int grandChildId = it.dataPtr;
           if (!descendantBitmapPtr.bitmap_isSet(grandChildId)) {
             if((status = descendantBitmapPtr.bitmap_set(grandChildId)) == false) {
               System.out.println("Assert failed: for descendantBitmapPtr.bitmap_set in net_findDescendants()");
diff --git a/Robust/src/Benchmarks/SingleTM/Bayes/QuickSort.java b/Robust/src/Benchmarks/SingleTM/Bayes/QuickSort.java
new file mode 100644 (file)
index 0000000..51f20a7
--- /dev/null
@@ -0,0 +1,184 @@
+public class QuickSort {
+  public  QuickSort() {
+
+  }
+
+  /**
+   * Sort an array of Objects into ascending order. The sort algorithm is an optimised
+   * quicksort, as described in Jon L. Bentley and M. Douglas McIlroy's
+   * "Engineering a Sort Function", Software-Practice and Experience, Vol.
+   * 23(11) P. 1249-1265 (November 1993). This algorithm gives nlog(n)
+   * performance on many arrays that would take quadratic time with a standard
+   * quicksort.
+   *
+   * @param a the array to sort
+   */
+  public void sort(Object[] a)
+  {
+    qsort(a, 0, a.length);
+  }
+
+  public void sort(Object[] a, int fromIndex, int toIndex)
+  {
+    qsort(a, fromIndex, toIndex);
+  }
+
+  private int med3(int a, int b, int c, Object[]d)
+  {
+    if(less(d[a], d[b])) {
+      if(less(d[b], d[c])) {
+        return b;
+      } else {
+        if(less(d[a], d[c])) 
+          return c;
+        else
+          return a;
+      }
+    } else {
+      if(less(d[c], d[b])) {
+        return b;
+      } else {
+        if(less(d[c], d[a])) 
+          return c;
+        else
+          return a;
+      }
+    }
+  }
+
+  private void swap(int i, int j, Object[] a)
+  {
+    Object c = a[i];
+    a[i] = a[j];
+    a[j] = c;
+  }
+
+  private void qsort(Object[] a, int start, int n)
+  {
+    // use an insertion sort on small arrays
+    if (n <= 7)
+    {
+      for (int i = start + 1; i < start + n; i++)
+        for (int j = i; j > 0 && less(a[j], a[j - 1]); j--)
+          swap(j, j - 1, a);
+      return;
+    }
+
+    int pm = n / 2;             // small arrays, middle element
+    if (n > 7)
+    {
+      int pl = start;
+      int pn = start + n - 1;
+
+      if (n > 40)
+      {                     // big arrays, pseudomedian of 9
+        int s = n / 8;
+        pl = med3(pl, pl + s, pl + 2 * s, a);
+        pm = med3(pm - s, pm, pm + s, a);
+        pn = med3(pn - 2 * s, pn - s, pn, a);
+      }
+      pm = med3(pl, pm, pn, a);       // mid-size, med of 3
+    }
+
+    int pa, pb, pc, pd, pv;
+    int r;
+
+    pv = start;
+    swap(pv, pm, a);
+    pa = pb = start;
+    pc = pd = start + n - 1;
+
+    while(true)
+    {
+      while (pb <= pc && (r = diff(a[pb],a[pv])) <= 0)
+      {
+        if (r == 0)
+        {
+          swap(pa, pb, a);
+          pa++;
+        }
+        pb++;
+      }
+      while (pc >= pb && (r = diff(a[pc],a[pv])) >= 0)
+      {
+        if (r == 0)
+        {
+          swap(pc, pd, a);
+          pd--;
+        }
+        pc--;
+      }
+      if (pb > pc)
+        break;
+      swap(pb, pc, a);
+      pb++;
+      pc--;
+    }
+    int pn = start + n;
+    int s;
+    s = Math.imin(pa - start, pb - pa);
+    vecswap(start, pb - s, s, a);
+    s = Math.imin(pd - pc, pn - pd - 1);
+    vecswap(pb, pn - s, s, a);
+    if ((s = pb - pa) > 1)
+      qsort(a, start, s);
+    if ((s = pd - pc) > 1)
+      qsort(a, pn - s, s);
+  }
+
+  private void vecswap(int i, int j, int n, Object[] a)
+  {
+    for (; n > 0; i++, j++, n--)
+      swap(i, j, a);
+  }
+
+  /* ===========================================
+   * compareQuery
+   * -- Want smallest ID first
+   * -- For vector_sort
+   * ===========================================
+   */
+  public boolean less(Object x, Object y) {
+    Query aQueryPtr = (Query) x;
+    Query bQueryPtr = (Query) y;
+    if(aQueryPtr.index < bQueryPtr.index)
+      return true;
+    return false;
+  }
+
+  public int diff(Object x, Object y) {
+    Query aQueryPtr = (Query) x;
+    Query bQueryPtr = (Query) y;
+    return (aQueryPtr.index - bQueryPtr.index);
+  }
+
+  /**
+   * main to test quick sort 
+   **/
+  /*
+  public static void main(String[] args) {
+    QuickSort qs = new QuickSort();
+    Query[] queries = new Query[10];
+
+    Random r = new Random();
+    r.random_alloc();
+    r.random_seed(0);
+
+    for(int i = 0; i<10; i++) {
+      queries[i] = new Query();
+      queries[i].index = (int) (r.random_generate() % 100);
+      queries[i].value = -1;
+    }
+
+    System.out.println("Before sorting");
+    for(int i = 0; i<10; i++)
+      System.out.println("queries["+i+"]= "+ queries[i]);
+
+    qs.sort(queries);
+
+    System.out.println("After sorting");
+    for(int i = 0; i<10; i++)
+      System.out.println("queries["+i+"]= "+ queries[i]);
+  }
+  */
+}