List of usage examples for java.util BitSet get
public boolean get(int bitIndex)
From source file:au.org.ala.delta.intkey.WriteOnceIntkeyItemsFile.java
public void writeAttributeBits(int charNumber, List<BitSet> attributes, int numBits) { int record = updateCharacterIndex(charNumber); // Merge the list into a single BitSet. BitSet master = new BitSet(); int offset = 0; for (BitSet set : attributes) { for (int i = 0; i < numBits; i++) { if (set.get(i)) { master.set(i + offset);/*from w w w. ja v a 2 s .c om*/ } } offset += numBits; } List<Integer> values = bitSetToInts(master, numBits * attributes.size()); writeToRecord(record, values); }
From source file:org.apache.hadoop.mapred.TestCombineSequenceFileInputFormat.java
@Test(timeout = 10000) public void testFormat() throws Exception { JobConf job = new JobConf(conf); Reporter reporter = Reporter.NULL;/* ww w. j a va 2 s. c o m*/ Random random = new Random(); long seed = random.nextLong(); LOG.info("seed = " + seed); random.setSeed(seed); localFs.delete(workDir, true); FileInputFormat.setInputPaths(job, workDir); final int length = 10000; final int numFiles = 10; // create a file with various lengths createFiles(length, numFiles, random); // create a combine split for the files InputFormat<IntWritable, BytesWritable> format = new CombineSequenceFileInputFormat<IntWritable, BytesWritable>(); IntWritable key = new IntWritable(); BytesWritable value = new BytesWritable(); for (int i = 0; i < 3; i++) { int numSplits = random.nextInt(length / (SequenceFile.SYNC_INTERVAL / 20)) + 1; LOG.info("splitting: requesting = " + numSplits); InputSplit[] splits = format.getSplits(job, numSplits); LOG.info("splitting: got = " + splits.length); // we should have a single split as the length is comfortably smaller than // the block size assertEquals("We got more than one splits!", 1, splits.length); InputSplit split = splits[0]; assertEquals("It should be CombineFileSplit", CombineFileSplit.class, split.getClass()); // check each split BitSet bits = new BitSet(length); RecordReader<IntWritable, BytesWritable> reader = format.getRecordReader(split, job, reporter); try { while (reader.next(key, value)) { assertFalse("Key in multiple partitions.", bits.get(key.get())); bits.set(key.get()); } } finally { reader.close(); } assertEquals("Some keys in no partition.", length, bits.cardinality()); } }
From source file:hivemall.dataset.LogisticRegressionDataGeneratorUDTF.java
private void generateSparseData() throws HiveException { float label = rnd1.nextFloat(); float sign = (label <= prob_one) ? 1.f : 0.f; labels[position] = classification ? sign : label; String[] features = featuresArray[position]; assert (features != null); final BitSet used = new BitSet(n_dimensions); int searchClearBitsFrom = 0; for (int i = 0, retry = 0; i < n_features; i++) { int f = rnd2.nextInt(n_dimensions); if (used.get(f)) { if (retry < 3) { --i;/* w ww . ja v a2s.co m*/ ++retry; continue; } searchClearBitsFrom = used.nextClearBit(searchClearBitsFrom); f = searchClearBitsFrom; } used.set(f); float w = (float) rnd2.nextGaussian() + (sign * eps); String y = f + ":" + w; features[i] = y; retry = 0; } if (sort) { Arrays.sort(features, new Comparator<String>() { @Override public int compare(String o1, String o2) { int i1 = Integer.parseInt(o1.split(":")[0]); int i2 = Integer.parseInt(o2.split(":")[0]); return Primitives.compare(i1, i2); } }); } }
From source file:com.joliciel.jochre.graphics.ShapeFillerImplTest.java
@Test public void testFillShape(@NonStrict final JochreImage jochreImage) throws Exception { new NonStrictExpectations() { {//from w w w. jav a 2s . co m jochreImage.normalize(255); returns(255); jochreImage.normalize(0); returns(0); } }; for (int i = 0; i <= 1; i++) { String imageName = ""; int fillFactor = 0; if (i == 0) { imageName = "AlephWithHoles.png"; fillFactor = 5; } else if (i == 1) { imageName = "TesWithHoles.png"; fillFactor = 5; } else { imageName = "AlephNoHoles.png"; fillFactor = 1; } LOG.debug(imageName); InputStream imageFileStream = getClass() .getResourceAsStream("/com/joliciel/jochre/test/resources/" + imageName); assertNotNull(imageFileStream); BufferedImage image = ImageIO.read(imageFileStream); ShapeInternal shape = new ShapeImpl(); shape.setJochreImage(jochreImage); shape.setImage(image); shape.setTop(0); shape.setLeft(0); shape.setRight(image.getWidth() - 1); shape.setBottom(image.getHeight() - 1); ShapeFillerImpl shapeFiller = new ShapeFillerImpl(); BitSet bitset = shapeFiller.fillShape(shape, 100, fillFactor); for (int y = 0; y < shape.getHeight(); y++) { StringBuilder line = new StringBuilder(); for (int x = 0; x < shape.getWidth(); x++) { if (bitset.get(y * shape.getWidth() + x)) line.append("x"); else line.append("o"); } LOG.debug(line.toString()); } } }
From source file:com.joliciel.jochre.graphics.ShapeFillerImplTest.java
@Test public void testFillBitSet(@NonStrict final Shape shape) { final int threshold = 100; final int width = 8; final int height = 8; new NonStrictExpectations() { {/* w w w . jav a 2 s.c o m*/ shape.getHeight(); returns(height); shape.getWidth(); returns(width); int[] pixels = { 0, 1, 1, 0, 0, 1, 1, 1, // row 0 0, 1, 0, 1, 0, 1, 0, 1, // row 1 0, 0, 1, 1, 0, 0, 1, 1, // row 2 0, 0, 1, 1, 0, 1, 1, 0, // row 3 0, 0, 0, 1, 0, 1, 1, 0, // row 4 0, 0, 0, 1, 1, 1, 0, 0, // row 5 0, 0, 1, 0, 1, 0, 0, 0, // row 6 1, 1, 0, 1, 1, 0, 0, 0, // row 7 }; BitSet bitset = new BitSet(height * width); for (int x = -1; x <= width; x++) for (int y = -1; y <= height; y++) { if (x >= 0 && x < width && y >= 0 && y < height && pixels[y * width + x] == 1) bitset.set(y * width + x); shape.isPixelBlack(x, y, threshold); if (x >= 0 && x < width && y >= 0 && y < height) returns(pixels[y * width + x] == 1); else returns(false); if (x >= 0 && x < width && y >= 0 && y < height) { shape.getAbsolutePixel(x, y); if (pixels[y * width + x] == 1) returns(0); else returns(255); } } shape.getBlackAndWhiteBitSet(threshold); returns(bitset); } }; ShapeFillerImpl shapeFiller = new ShapeFillerImpl(); BitSet filledBitSet = shapeFiller.fillBitSet(shape, shape.getBlackAndWhiteBitSet(threshold), 5); for (int y = 0; y < height; y++) { StringBuilder line = new StringBuilder(); for (int x = 0; x < width; x++) { if (filledBitSet.get(y * width + x)) line.append("x"); else line.append("o"); } LOG.debug(line.toString()); } }
From source file:org.apache.hadoop.record.TestRecordWritable.java
public void testFormat() throws Exception { JobConf job = new JobConf(conf); FileSystem fs = FileSystem.getLocal(conf); Path dir = new Path(System.getProperty("test.build.data", ".") + "/mapred"); Path file = new Path(dir, "test.seq"); int seed = new Random().nextInt(); //LOG.info("seed = "+seed); Random random = new Random(seed); fs.delete(dir, true);//from w w w . j ava2 s . c om FileInputFormat.setInputPaths(job, dir); // for a variety of lengths for (int length = 0; length < MAX_LENGTH; length += random.nextInt(MAX_LENGTH / 10) + 1) { // create a file with length entries SequenceFile.Writer writer = new SequenceFile.Writer(fs, conf, file, RecInt.class, RecBuffer.class); try { for (int i = 0; i < length; i++) { RecInt key = new RecInt(); key.setData(i); byte[] data = new byte[random.nextInt(10)]; random.nextBytes(data); RecBuffer value = new RecBuffer(); value.setData(new Buffer(data)); writer.append(key, value); } } finally { writer.close(); } // try splitting the file in a variety of sizes InputFormat<RecInt, RecBuffer> format = new SequenceFileInputFormat<RecInt, RecBuffer>(); RecInt key = new RecInt(); RecBuffer value = new RecBuffer(); for (int i = 0; i < 3; i++) { int numSplits = random.nextInt(MAX_LENGTH / (SequenceFile.SYNC_INTERVAL / 20)) + 1; InputSplit[] splits = format.getSplits(job, numSplits); // check each split BitSet bits = new BitSet(length); for (int j = 0; j < splits.length; j++) { RecordReader<RecInt, RecBuffer> reader = format.getRecordReader(splits[j], job, Reporter.NULL); try { int count = 0; while (reader.next(key, value)) { assertFalse("Key in multiple partitions.", bits.get(key.getData())); bits.set(key.getData()); count++; } } finally { reader.close(); } } assertEquals("Some keys in no partition.", length, bits.cardinality()); } } }
From source file:org.apache.kylin.cube.cuboid.algorithm.generic.GeneticAlgorithm.java
protected Population initRandomPopulation(BitsChromosomeHelper helper) { List<Chromosome> chromosomeList = Lists.newArrayListWithCapacity(populationSize); while (chromosomeList.size() < populationSize) { BitSet bitSetForSelection = new BitSet(helper.getLength()); //Initialize selection genes double totalSpace = 0; while (totalSpace < helper.spaceLimit) { int j = org.apache.commons.math3.genetics.GeneticAlgorithm.getRandomGenerator() .nextInt(helper.getLength()); if (!bitSetForSelection.get(j)) { totalSpace += helper.getCuboidSizeByBitIndex(j); bitSetForSelection.set(j); }/*from w w w . j a v a 2 s .c o m*/ } Chromosome chromosome = new BitsChromosome(bitSetForSelection, benefitPolicy.getInstance(), helper); chromosomeList.add(chromosome); } return new ElitisticListPopulation(chromosomeList, maxPopulationSize, 0.8); }
From source file:org.osgp.adapter.protocol.dlms.domain.commands.SetAlarmNotificationsCommandExecutor.java
public AlarmNotifications alarmNotifications(final long alarmFilterLongValue) { final BitSet bitSet = BitSet.valueOf(new long[] { alarmFilterLongValue }); final Set<AlarmNotification> notifications = new TreeSet<>(); final AlarmType[] alarmTypes = AlarmType.values(); for (final AlarmType alarmType : alarmTypes) { final boolean enabled = bitSet.get(alarmRegisterBitIndexPerAlarmType.get(alarmType)); notifications.add(new AlarmNotification(alarmType, enabled)); }// w w w. j av a 2 s . c o m return new AlarmNotifications(notifications); }
From source file:UrlEncoder.java
/** * {@inheritDoc}/*from w w w . j av a 2s . c o m*/ */ public String encode(String text) { if (text == null) return null; if (text.length() == 0) return text; final BitSet safeChars = isSlashEncoded() ? RFC2396_UNRESERVED_CHARACTERS : RFC2396_UNRESERVED_WITH_SLASH_CHARACTERS; final StringBuilder result = new StringBuilder(); final CharacterIterator iter = new StringCharacterIterator(text); for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) { if (safeChars.get(c)) { // Safe character, so just pass through ... result.append(c); } else { // The character is not a safe character, and must be escaped ... result.append(ESCAPE_CHARACTER); result.append(Character.toLowerCase(Character.forDigit(c / 16, 16))); result.append(Character.toLowerCase(Character.forDigit(c % 16, 16))); } } return result.toString(); }
From source file:mastodon.algorithms.MHBisectionAlgorithm.java
protected void tryPruning() { //choose the number of species in list to perturb based on a Poisson distributions with rate equal to variable "mean" above int numberToSet = 0; int numberToClear = 0; while (numberToSet < 1 || numberToSet > currPrunedSpeciesCount) { numberToSet = pd.sample() + 1;//www.j a va 2 s . c om } if (numberToSet > (bts.getTaxaCount() - currPrunedSpeciesCount)) { numberToSet = bts.getTaxaCount() - currPrunedSpeciesCount; } numberToClear = numberToSet; BitSet bitsToSet = new BitSet(); BitSet bitsToClear = new BitSet(); for (int e = 0; e < numberToSet; e++) { int choice = 0; while (true) { choice = (int) (Random.nextDouble() * bts.getTaxaCount()); if (!currPruning.get(choice) && !bitsToSet.get(choice)) { break; } } bitsToSet.set(choice); } for (int e = 0; e < numberToClear; e++) { int choice = 0; while (true) { choice = (int) (Random.nextDouble() * bts.getTaxaCount()); if (currPruning.get(choice) && !bitsToClear.get(choice)) { break; } } bitsToClear.set(choice); } currPruning.or(bitsToSet); currPruning.xor(bitsToClear); currScore = bts.pruneFast(currPruning); bts.unPrune(); }