Created
January 29, 2016 21:01
-
-
Save cogmission/e7b06f5ab545a80b19be to your computer and use it in GitHub Desktop.
SDR RoaringBitmap wrapper api so far
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.numenta.nupic.research.sdrs; | |
import java.util.Arrays; | |
import java.util.Iterator; | |
import java.util.stream.IntStream; | |
import org.roaringbitmap.RoaringBitmap; | |
public class SDR implements Iterable<Integer> { | |
private RoaringBitmap bits; | |
private int size; | |
private SDR(int width, int... bits) { | |
this.size = width; | |
this.bits = RoaringBitmap.bitmapOf(bits); | |
} | |
/** | |
* Returns an {@code SDR} constructed from <b>sparse</b> indices | |
* ([1,2,7,11] <b><em>NOT</em></b> [0,1,1,0,0,0,0,1,0,0,0,1]) | |
* please be careful as the interface doesn't warn you for misuse | |
* for efficiency sake. | |
* | |
* <b>Warning:</b> The first parameter <b>must</b> be the total length | |
* | |
* @param positions | |
* @return | |
*/ | |
public static SDR fromSparse(int width, int... positions) { | |
return new SDR(width, positions); | |
} | |
/** | |
* Returns a <b><em>sorted</em></b> {@code SDR} constructed from <b>sparse</b> indices | |
* ([7,2,11,1] (unsorted) <b><em>NOT</em></b> [0,1,1,0,0,0,0,1,0,0,0,1]) | |
* please be careful as the interface doesn't warn you for misuse | |
* for efficiency sake. | |
* | |
* * <b>Warning:</b> The first parameter <b>must</b> be the total length | |
* | |
* @param positions | |
* @return | |
*/ | |
public static SDR fromUnsortedSparse(int width, int... positions) { | |
return new SDR(width, Arrays.stream(positions).sorted().toArray()); | |
} | |
/** | |
* Returns an {@code SDR} constructed from <b>dense</b> indices | |
* ([0,1,1,0,0,0,0,1,0,0,0,1] <b><em>NOT</em></b> [1,2,7,11]) | |
* please be careful as the interface doesn't warn you for misuse | |
* for efficiency sake. | |
* @param positions | |
* @return | |
*/ | |
public static SDR fromDense(int... bits) { | |
return new SDR(bits.length, IntStream.range(0, bits.length).filter(i -> bits[i] == 1).toArray()); | |
} | |
/** | |
* Returns the total width of this {@code SDR} (counting zero bits as well). | |
* @return | |
*/ | |
public int size() { | |
return size; | |
} | |
/** | |
* Returns the bit value at the absolute position specified. | |
* | |
* (i.e. for: [1,2,4,7,11] --> getValue(8) == 0 where as, getValue(...) | |
* with 1, 2, 4, 7, or 11 will return a 1). | |
* | |
* @param denseIndex the index for which a value (either 0 or 1) is returned. | |
* @return | |
*/ | |
public int getValue(int denseIndex) { | |
return bits.contains(denseIndex) ? 1 : 0; | |
} | |
/** | |
* Returns the position (sparse index) at the position specified. | |
* (i.e. for: [1,2,4,7,11] --> getPosition(8) throws {@link IndexOutOfBoundsException} | |
* and getPosition() with 0, 1, 2, 3, or 4 will return 1, 2, 4, 7, 11 respectively | |
* | |
* @param position the position of the index to return | |
* @return | |
*/ | |
public int getPosition(int position) { | |
return bits.select(position); | |
} | |
/** | |
* Returns an {@link Iterator} over the bits in this {@link SDR} | |
*/ | |
@Override | |
public Iterator<Integer> iterator() { | |
return bits.iterator(); | |
} | |
/** | |
* @see java.lang.Object#hashCode() | |
*/ | |
@Override | |
public int hashCode() { | |
final int prime = 31; | |
int result = 1; | |
result = prime * result + ((bits == null) ? 0 : bits.hashCode()); | |
return result; | |
} | |
/** | |
* @see java.lang.Object#equals(java.lang.Object) | |
*/ | |
@Override | |
public boolean equals(Object obj) { | |
if(this == obj) | |
return true; | |
if(obj == null) | |
return false; | |
if(getClass() != obj.getClass()) | |
return false; | |
SDR other = (SDR)obj; | |
if(bits == null) { | |
if(other.bits != null) | |
return false; | |
} else if(!bits.equals(other.bits)) | |
return false; | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's the test: