Permalink
Browse files

add support for iterating over NBSI contents sans autoboxing

  • Loading branch information...
1 parent 800d57e commit 39a705928ef9219cf4d7f422dd4d29940eccff79 d committed May 1, 2011
View
6 src/org/cliffc/high_scale_lib/IntIterator.java
@@ -0,0 +1,6 @@
+package org.cliffc.high_scale_lib;
+
+public interface IntIterator {
+ public int next();
+ public boolean hasNext();
+}
View
78 src/org/cliffc/high_scale_lib/NonBlockingSetInt.java
@@ -137,6 +137,8 @@ public void clear ( ) {
;
}
+ public int sizeInBytes() { return _nbsi.sizeInBytes(); }
+
/*****************************************************************
*
* bitwise comparisons optimised for NBSI
@@ -155,6 +157,10 @@ public NonBlockingSetInt or(final NonBlockingSetInt op) {
return res;
}
+// public NonBlockingSetInt not(final NonBlockingSetInt op) {
+//
+// }
+
/** Verbose printout of internal structure for debugging. */
public void print() { _nbsi.print(0); }
@@ -164,38 +170,60 @@ public NonBlockingSetInt or(final NonBlockingSetInt op) {
*/
public Iterator<Integer> iterator( ) { return new iter(); }
- private class iter implements Iterator<Integer> {
- NBSI _nbsi2;
- int _idx = -1;
- int _prev = -1;
- iter() { _nbsi2 = _nbsi; advance(); }
- public boolean hasNext() { return _idx != -2; }
- private void advance() {
+ public IntIterator intIterator() { return new NBSIIntIterator(); }
+
+ private class NBSIIntIterator implements IntIterator {
+
+ NBSI nbsi;
+ int index = -1;
+ int prev = -1;
+
+ NBSIIntIterator() {
+ nbsi = _nbsi;
+ advance();
+ }
+
+ private void advance() {
while( true ) {
- _idx++; // Next index
- while( (_idx>>6) >= _nbsi2._bits.length ) { // Index out of range?
- if( _nbsi2._new == null ) { // New table?
- _idx = -2; // No, so must be all done
- return; //
+ index++; // Next index
+ while( (index>>6) >= nbsi._bits.length ) { // Index out of range?
+ if( nbsi._new == null ) { // New table?
+ index = -2; // No, so must be all done
+ return; //
}
- _nbsi2 = _nbsi2._new; // Carry on, in the new table
+ nbsi = nbsi._new; // Carry on, in the new table
}
- if( _nbsi2.contains(_idx) ) return;
+ if( nbsi.contains(index) ) return;
}
}
- public Integer next() {
- if( _idx == -1 ) throw new NoSuchElementException();
- _prev = _idx;
+ @Override
+ public int next() {
+ if( index == -1 ) throw new NoSuchElementException();
+ prev = index;
advance();
- return _prev;
+ return prev;
}
- public void remove() {
- if( _prev == -1 ) throw new IllegalStateException();
- _nbsi2.remove(_prev);
- _prev = -1;
+
+ @Override
+ public boolean hasNext() {
+ return index != -2;
+ }
+
+ public void remove() {
+ if( prev == -1 ) throw new IllegalStateException();
+ nbsi.remove(prev);
+ prev = -1;
}
}
+ private class iter implements Iterator<Integer> {
+ NBSIIntIterator intIterator;
+ iter() { intIterator = new NBSIIntIterator(); }
+ public boolean hasNext() { return intIterator.hasNext(); }
+ public Integer next() { return intIterator.next(); }
+ public void remove() { intIterator.remove(); }
+ }
+
// --- writeObject -------------------------------------------------------
// Write a NBSI to a stream
private void writeObject(java.io.ObjectOutputStream s) throws IOException {
@@ -300,6 +328,8 @@ private NBSI(NBSI a, NBSI b, Counter ctr, NonBlockingSetInt nonb) {
return;
}
+ // todo - clean this nastiness up
+ // essentially just safely creates new empty buffers for each of the recursive bitsets
if(!has_bits(a)) {
_bits = new long[b._bits.length];
_nbsi64 = new NBSI(null,b._nbsi64,null,null);
@@ -442,6 +472,8 @@ private long safe_read_word(int i, long default_word) {
return word;
}
+ public int sizeInBytes() { return (int)_bits.length; }
+
public int size() { return (int)_size.get(); }
// Must grow the current array to hold an element of size i
@@ -577,5 +609,5 @@ private void print(int d) {
_new.print(d+1);
}
}
- }
+ }
}
View
8 test/src/java/org/cliffc/high_scale_lib/NonBlockingSetIntTest.java
@@ -3,7 +3,6 @@
import junit.framework.TestCase;
import java.io.*;
-import java.util.Arrays;
import java.util.Iterator;
import static org.hamcrest.CoreMatchers.anyOf;
@@ -55,13 +54,6 @@ public void testSetOperations() {
c.and(new NonBlockingSetInt());
new NonBlockingSetInt().and(c);
-
- // todo - test transitive and reciprocal properties of bitset operations are upheld
-
- System.out.println(a);
- System.out.println(b);
- System.out.println(c);
- System.out.println(d);
}
private NonBlockingSetInt _nbsi;

0 comments on commit 39a7059

Please sign in to comment.