From: bdemsky Date: Fri, 26 Jun 2009 09:18:18 +0000 (+0000) Subject: clean up iterator stuff... X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=61b1d3bb1b206a536d358785ab8ea3455571a3c1;p=IRC.git clean up iterator stuff... --- diff --git a/Robust/src/Benchmarks/SingleTM/Bayes/Data.java b/Robust/src/Benchmarks/SingleTM/Bayes/Data.java index f05d7fdc..8ffbde1b 100644 --- a/Robust/src/Benchmarks/SingleTM/Bayes/Data.java +++ b/Robust/src/Benchmarks/SingleTM/Bayes/Data.java @@ -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"); diff --git a/Robust/src/Benchmarks/SingleTM/Bayes/IntList.java b/Robust/src/Benchmarks/SingleTM/Bayes/IntList.java index 55b54114..907d23d4 100644 --- a/Robust/src/Benchmarks/SingleTM/Bayes/IntList.java +++ b/Robust/src/Benchmarks/SingleTM/Bayes/IntList.java @@ -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 index 00000000..4d125121 --- /dev/null +++ b/Robust/src/Benchmarks/SingleTM/Bayes/IntListIter.java @@ -0,0 +1,3 @@ +public class IntListIter { + IntListNode ptr; +} \ No newline at end of file diff --git a/Robust/src/Benchmarks/SingleTM/Bayes/Learner.java b/Robust/src/Benchmarks/SingleTM/Bayes/Learner.java index 428ce3ab..870b152e 100644 --- a/Robust/src/Benchmarks/SingleTM/Bayes/Learner.java +++ b/Robust/src/Benchmarks/SingleTM/Bayes/Learner.java @@ -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 } diff --git a/Robust/src/Benchmarks/SingleTM/Bayes/List.java b/Robust/src/Benchmarks/SingleTM/Bayes/List.java index d0fe80a3..1e102826 100644 --- a/Robust/src/Benchmarks/SingleTM/Bayes/List.java +++ b/Robust/src/Benchmarks/SingleTM/Bayes/List.java @@ -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 index 00000000..0dadc30c --- /dev/null +++ b/Robust/src/Benchmarks/SingleTM/Bayes/ListIter.java @@ -0,0 +1,3 @@ +public class ListIter { + public ListNode ptr; +} \ No newline at end of file diff --git a/Robust/src/Benchmarks/SingleTM/Bayes/Net.java b/Robust/src/Benchmarks/SingleTM/Bayes/Net.java index 4b39edaa..817d7b5c 100644 --- a/Robust/src/Benchmarks/SingleTM/Bayes/Net.java +++ b/Robust/src/Benchmarks/SingleTM/Bayes/Net.java @@ -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 index 00000000..51f20a70 --- /dev/null +++ b/Robust/src/Benchmarks/SingleTM/Bayes/QuickSort.java @@ -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]); + } + */ +}