Java Code Examples for org.roaringbitmap.RoaringBitmap#add()

The following examples show how to use org.roaringbitmap.RoaringBitmap#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RoaringBitmapBenchmark.java    From RoaringBitmap with Apache License 2.0 6 votes vote down vote up
@Setup
public void setup() {
  bitmap1 = new RoaringBitmap();
  bitmap2 = new RoaringBitmap();
  int k = 1 << 16;
  int i = 0;
  for (; i < 10000; ++i) {
    bitmap1.add(i * k);
  }
  for (; i < 10050; ++i) {
    bitmap2.add(i * k);
    bitmap1.add(i * k + 13);
  }
  for (; i < 20000; ++i) {
    bitmap2.add(i * k);
  }
  bitmap1.add(i * k);
}
 
Example 2
Source File: SmileExtUtils.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
/**
 * @param opt command separated list of Q and C. Q for {@link NumericAttribute}, C for
 *        {@link NominalAttribute}.
 */
@Nonnull
public static RoaringBitmap resolveAttributes(@Nullable final String opt)
        throws UDFArgumentException {
    final RoaringBitmap attr = new RoaringBitmap();
    if (opt == null) {
        return attr;
    }
    final String[] opts = opt.split(",");
    final int size = opts.length;
    for (int i = 0; i < size; i++) {
        final String type = opts[i];
        if ("Q".equals(type)) {
            continue;
        } else if ("C".equals(type)) {
            attr.add(i);
        } else {
            throw new UDFArgumentException("Unsupported attribute type: " + type);
        }
    }
    return attr;
}
 
Example 3
Source File: InlinkOverlapEntityEntitySimilarity.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
private void setupEntities(Entities entities) throws Exception {
  if (entities.size() == 0) {
    logger.debug("Skipping initialization of InlinkEntityEntitySimilarity for " + entities.size() + " entities");
    return;
  }

  logger.debug("Initializing InlinkEntityEntitySimilarity for " + entities.size() + " entities");

  entity2vector = new TIntObjectHashMap<>();

  TIntObjectHashMap<int[]> entityInlinks = DataAccess.getInlinkNeighbors(entities);

  for (TIntObjectIterator<int[]> itr = entityInlinks.iterator(); itr.hasNext(); ) {
    itr.advance();
    int entity = itr.key();
    int[] inLinks = itr.value();

    RoaringBitmap bs = new RoaringBitmap();
    for (int l : inLinks) {
      bs.add(l);
    }
    entity2vector.put(entity, bs);
  }

  logger.debug("Done initializing InlinkEntityEntitySimilarity");
}
 
Example 4
Source File: SmileExtUtils.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
@Nonnull
public static RoaringBitmap convertAttributeTypes(
        @Nonnull final smile.data.Attribute[] original) {
    final int size = original.length;
    final RoaringBitmap nominalAttrs = new RoaringBitmap();
    for (int i = 0; i < size; i++) {
        smile.data.Attribute o = original[i];
        switch (o.type) {
            case NOMINAL: {
                nominalAttrs.add(i);
                break;
            }
            case NUMERIC: {
                break;
            }
            default:
                throw new UnsupportedOperationException("Unsupported type: " + o.type);
        }
    }
    return nominalAttrs;
}
 
Example 5
Source File: RoaringBitmapBenchmark.java    From RoaringBitmap with Apache License 2.0 6 votes vote down vote up
@Setup
public void setup() {
  bitmap1 = new RoaringBitmap();
  bitmap2 = new RoaringBitmap();
  int k = 1 << 16;
  int i = 0;
  for (; i < 10000; ++i) {
    bitmap1.add(i * k);
  }
  for (; i < 10050; ++i) {
    bitmap2.add(i * k);
    bitmap1.add(i * k + 13);
  }
  for (; i < 20000; ++i) {
    bitmap2.add(i * k);
  }
  bitmap1.add(i * k);
}
 
Example 6
Source File: CompressionResults.java    From RoaringBitmap with Apache License 2.0 6 votes vote down vote up
public static void testSuperSparse() {
  System.out.println("Sparse case... universe = [0,"+universe_size+")");
  RoaringBitmap r = new RoaringBitmap();
  int howmany = 100;
  int gap = universe_size / howmany;
  System.out.println("Adding "+howmany+" values separated by gaps of "+gap+ "...");
  System.out.println("As a bitmap it would look like 1000...001000... ");
  for (int i = 1; i < howmany; i++) {
    r.add(i * gap);
  }
  System.out.println("Bits used per value = "+F.format(r.getSizeInBytes()*8.0/howmany));
  r.runOptimize();
  System.out.println("Bits used per value after run optimize = "+F.format(r.getSizeInBytes()*8.0/howmany));
  System.out.println("An uncompressed bitset might use "+F.format(universe_size*1.0/howmany)+" bits per value set");
  System.out.println();

}
 
Example 7
Source File: Basic.java    From RoaringBitmap with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
      RoaringBitmap rr = RoaringBitmap.bitmapOf(1,2,3,1000);
      RoaringBitmap rr2 = new RoaringBitmap();
      rr2.add(4000L,4255L);

      RoaringBitmap rror = RoaringBitmap.or(rr, rr2);// new bitmap
      rr.or(rr2); //in-place computation
      boolean equals = rror.equals(rr);// true
      if(!equals) throw new RuntimeException("bug");
      // number of values stored?
      long cardinality = rr.getLongCardinality();
      System.out.println(cardinality);
      // a "forEach" is faster than this loop, but a loop is possible:
      for(int i : rr) {
        System.out.println(i);
      }
}
 
Example 8
Source File: TestRoaring64NavigableMap.java    From RoaringBitmap with Apache License 2.0 6 votes vote down vote up
@Disabled(".add have a different meaning between Roaring64NavigableMap and RoaringBitmap")
@Test
public void testDefaultBehaviorLikeRoaring_MinusOneAsInt() {
  Roaring64NavigableMap longBitmap = newDefaultCtor();
  RoaringBitmap bitmap = new RoaringBitmap();

  longBitmap.addLong(-1);
  bitmap.add(-1);

  // Ok as -1 === -1
  assertEquals(bitmap.select(0), longBitmap.select(0));

  // But RoaringBitmap.select has to be interpreted as an unsigned integer: not as an unsigned
  // long
  assertEquals(Util.toUnsignedLong(bitmap.select(0)), longBitmap.select(0));
}
 
Example 9
Source File: TestRoaring64NavigableMap.java    From RoaringBitmap with Apache License 2.0 6 votes vote down vote up
@Test
public void testDefaultBehaviorLikeRoaring() {
  Roaring64NavigableMap longBitmap = newDefaultCtor();
  RoaringBitmap bitmap = new RoaringBitmap();

  longBitmap.addLong(-1);
  bitmap.add(-1);

  longBitmap.addLong(1);
  bitmap.add(1);

  int[] bitmapAsIntArray = bitmap.toArray();

  long[] longBitmapAsArray = longBitmap.toArray();

  // The array seems equivalent, but beware one represents unsigned integers while the others
  // holds unsigned longs: -1 have a different meaning
  assertArrayEquals(bitmapAsIntArray, Ints.toArray(Longs.asList(longBitmapAsArray)));
  assertArrayEquals(Longs.toArray(Ints.asList(bitmapAsIntArray)), longBitmapAsArray);

  long[] bitmapAsLongArray = new long[bitmapAsIntArray.length];
  for (int i = 0; i < bitmapAsIntArray.length; i++) {
    bitmapAsLongArray[i] = Util.toUnsignedLong(bitmapAsIntArray[i]);
  }
}
 
Example 10
Source File: TestRoaring64NavigableMap.java    From RoaringBitmap with Apache License 2.0 5 votes vote down vote up
@Test
public void testRoaringBitmap_SelectAboveIntegerMaxValuePlusOne() {
  RoaringBitmap map = new RoaringBitmap();

  long maxForRoaringBitmap = Util.toUnsignedLong(-1) + 1;
  map.add(0L, maxForRoaringBitmap);

  assertEquals(maxForRoaringBitmap, map.getLongCardinality());
  assertEquals(-1, map.select(-1));
}
 
Example 11
Source File: TestRoaring64NavigableMap.java    From RoaringBitmap with Apache License 2.0 5 votes vote down vote up
@Test
public void testRoaringBitmap_SelectAboveIntegerMaxValue() {
  RoaringBitmap map = new RoaringBitmap();

  long maxForRoaringBitmap = Util.toUnsignedLong(-1) + 1;
  map.add(0L, maxForRoaringBitmap);

  assertEquals(maxForRoaringBitmap, map.getLongCardinality());
  assertEquals(-1, map.select(-1));
}
 
Example 12
Source File: CompressionResults.java    From RoaringBitmap with Apache License 2.0 5 votes vote down vote up
public static void testAlternating() {
  System.out.println("Alternating case... universe = [0,"+universe_size+")");
  RoaringBitmap r = new RoaringBitmap();
  for (int i = 1; i < universe_size; i++) {
    if(i%2 == 0)
      r.add(i);
  }
  System.out.println("Adding all even values in the universe");
  System.out.println("As a bitmap it would look like 01010101... ");
  System.out.println("Bits used per value = "+F.format(r.getSizeInBytes()*8.0/r.getCardinality()));
  r.runOptimize();
  System.out.println("Bits used per value after run optimize = "+F.format(r.getSizeInBytes()*8.0/r.getCardinality()));
  System.out.println("An uncompressed bitset might use "+F.format(universe_size*1.0/r.getCardinality())+" bits per value set");
  System.out.println();
}
 
Example 13
Source File: RoaringBitmapBenchmark.java    From RoaringBitmap with Apache License 2.0 5 votes vote down vote up
@Setup
public void setup() {
  bitmap1 = new RoaringBitmap();
  bitmap2 = new RoaringBitmap();
  int k = 1 << 16;
  for (int i = 0; i < 10000; ++i) {
    bitmap1.add(i * k);
    bitmap2.add(i * k);
  }
}
 
Example 14
Source File: RoaringBitmapBenchmark.java    From RoaringBitmap with Apache License 2.0 5 votes vote down vote up
@Setup
public void setup() {
  bitmap1 = new RoaringBitmap();
  bitmap2 = new RoaringBitmap();
  int k = 1 << 16;
  for (int i = 0; i < 10000; ++i) {
    bitmap1.add(i * k);
    bitmap2.add(i * k);
  }
}
 
Example 15
Source File: LSH.java    From ache with Apache License 2.0 5 votes vote down vote up
@Override
public void insertToBand(int band, int[] hashes, int id) {
    String hexkey = toHex(band, hashes);
    RoaringBitmap idsBitmap = maps.get(hexkey);
    if (idsBitmap == null) {
        idsBitmap = RoaringBitmap.bitmapOf(id);
        maps.put(hexkey, idsBitmap);
    } else {
        idsBitmap.add(id);
    }
}
 
Example 16
Source File: BitmapBackedSelection.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
private RoaringBitmap toBitmap(Selection otherSelection) {
  if (otherSelection instanceof BitmapBackedSelection) {
    return ((BitmapBackedSelection) otherSelection).bitmap.clone();
  }
  RoaringBitmap bits = new RoaringBitmap();
  for (int i : otherSelection) {
    bits.add(i);
  }
  return bits;
}
 
Example 17
Source File: BitmapBackedSelection.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
private RoaringBitmap toBitmap(Selection otherSelection) {
  if (otherSelection instanceof BitmapBackedSelection) {
    return ((BitmapBackedSelection) otherSelection).bitmap.clone();
  }
  RoaringBitmap bits = new RoaringBitmap();
  for (int i : otherSelection) {
    bits.add(i);
  }
  return bits;
}
 
Example 18
Source File: RoaringBitmapBenchmark.java    From RoaringBitmap with Apache License 2.0 5 votes vote down vote up
@Setup
public void setup() {
  bitmap1 = new RoaringBitmap();
  bitmap2 = new RoaringBitmap();
  int k = 1 << 16;
  for (int i = 0; i < 10000; ++i) {
    bitmap1.add(2 * i * k);
    bitmap2.add(2 * i * k + 1);
  }
}
 
Example 19
Source File: RoaringBitmapBenchmark.java    From RoaringBitmap with Apache License 2.0 5 votes vote down vote up
@Setup
public void setup() {
  bitmap1 = new RoaringBitmap();
  bitmap2 = new RoaringBitmap();
  int k = 1 << 16;
  for (int i = 0; i < 10000; ++i) {
    bitmap1.add(2 * i * k);
    bitmap2.add(2 * i * k + 1);
  }
}
 
Example 20
Source File: DecisionTreeTest.java    From incubator-hivemall with Apache License 2.0 4 votes vote down vote up
@Test
public void testTitanicPruning() throws IOException, ParseException {
    String datasetUrl =
            "https://gist.githubusercontent.com/myui/7cd82c443db84ba7e7add1523d0247a9/raw/f2d3e3051b0292577e8c01a1759edabaa95c5781/titanic_train.tsv";

    URL url = new URL(datasetUrl);
    InputStream is = new BufferedInputStream(url.openStream());

    DelimitedTextParser parser = new DelimitedTextParser();
    parser.setColumnNames(true);
    parser.setDelimiter(",");
    parser.setResponseIndex(new NominalAttribute("survived"), 0);

    AttributeDataset train = parser.parse("titanic train", is);
    double[][] x_ = train.toArray(new double[train.size()][]);
    int[] y = train.toArray(new int[train.size()]);

    // pclass, name, sex, age, sibsp, parch, ticket, fare, cabin, embarked
    // C,C,C,Q,Q,Q,C,Q,C,C
    RoaringBitmap nominalAttrs = new RoaringBitmap();
    nominalAttrs.add(0);
    nominalAttrs.add(1);
    nominalAttrs.add(2);
    nominalAttrs.add(6);
    nominalAttrs.add(8);
    nominalAttrs.add(9);

    int columns = x_[0].length;
    Matrix x = new RowMajorDenseMatrix2d(x_, columns);
    int numVars = (int) Math.ceil(Math.sqrt(columns));
    int maxDepth = Integer.MAX_VALUE;
    int maxLeafs = Integer.MAX_VALUE;
    int minSplits = 2;
    int minLeafSize = 1;
    int[] samples = null;
    PRNG rand = RandomNumberGeneratorFactory.createPRNG(43L);

    final String[] featureNames = new String[] {"pclass", "name", "sex", "age", "sibsp",
            "parch", "ticket", "fare", "cabin", "embarked"};
    final String[] classNames = new String[] {"yes", "no"};
    DecisionTree tree = new DecisionTree(nominalAttrs, x, y, numVars, maxDepth, maxLeafs,
        minSplits, minLeafSize, samples, SplitRule.GINI, rand) {
        @Override
        public String toString() {
            return predictJsCodegen(featureNames, classNames);
        }
    };
    tree.toString();
}