List of usage examples for java.util BitSet set
public void set(int bitIndex)
From source file:com.joliciel.jochre.graphics.ShapeFillerImpl.java
BitSet fillBitSet(Shape shape, BitSet bitset, int neighbourBirthCount) { BitSet newBitSet = new BitSet(bitset.size()); int baseIndex = 0; for (int y = 0; y < shape.getHeight(); y++) { for (int x = 0; x < shape.getWidth(); x++) { int index = baseIndex + x; if (bitset.get(index)) newBitSet.set(index); else { int surroundingCount = 0; if (y > 0) { if (x > 0) surroundingCount += bitset.get(index - (shape.getWidth() + 1)) ? 1 : 0; surroundingCount += bitset.get(index - (shape.getWidth())) ? 1 : 0; if (x < shape.getWidth() - 1) surroundingCount += bitset.get(index - (shape.getWidth() - 1)) ? 1 : 0; }/*from w w w. ja v a 2 s .c o m*/ if (x > 0) surroundingCount += bitset.get(index - 1) ? 1 : 0; if (x < shape.getWidth() - 1) surroundingCount += bitset.get(index + 1) ? 1 : 0; if (y < shape.getHeight() - 1) { if (x > 0) surroundingCount += bitset.get(index + (shape.getWidth() - 1)) ? 1 : 0; surroundingCount += bitset.get(index + (shape.getWidth())) ? 1 : 0; if (x < shape.getWidth() - 1) surroundingCount += bitset.get(index + (shape.getWidth() + 1)) ? 1 : 0; } // if at least NEIGHBOUR_COUNT_BIRTH out of 8 surrounding pixels are on, // assume this one should be on if (surroundingCount >= NEIGHBOUR_COUNT_BIRTH) newBitSet.set(index); } } baseIndex += shape.getWidth(); } return newBitSet; }
From source file:org.apache.pig.tools.pigstats.mapreduce.MRScriptState.java
public String getPigFeature(MapReduceOper mro) { if (featureMap == null) { featureMap = new HashMap<MapReduceOper, String>(); }// w w w. j a v a2 s . c o m String retStr = featureMap.get(mro); if (retStr == null) { BitSet feature = new BitSet(); feature.clear(); if (mro.isSkewedJoin()) { feature.set(PIG_FEATURE.SKEWED_JOIN.ordinal()); } if (mro.isGlobalSort()) { feature.set(PIG_FEATURE.ORDER_BY.ordinal()); } if (mro.isSampler()) { feature.set(PIG_FEATURE.SAMPLER.ordinal()); } if (mro.isIndexer()) { feature.set(PIG_FEATURE.INDEXER.ordinal()); } if (mro.isCogroup()) { feature.set(PIG_FEATURE.COGROUP.ordinal()); } if (mro.isGroupBy()) { feature.set(PIG_FEATURE.GROUP_BY.ordinal()); } if (mro.isRegularJoin()) { feature.set(PIG_FEATURE.HASH_JOIN.ordinal()); } if (mro.needsDistinctCombiner()) { feature.set(PIG_FEATURE.DISTINCT.ordinal()); } if (!mro.combinePlan.isEmpty()) { feature.set(PIG_FEATURE.COMBINER.ordinal()); } if (mro instanceof NativeMapReduceOper) { feature.set(PIG_FEATURE.NATIVE.ordinal()); } else {// if it is NATIVE MR , don't explore its plans try { new FeatureVisitor(mro.mapPlan, feature).visit(); if (mro.reducePlan.isEmpty()) { feature.set(PIG_FEATURE.MAP_ONLY.ordinal()); } else { new FeatureVisitor(mro.reducePlan, feature).visit(); } } catch (VisitorException e) { LOG.warn("Feature visitor failed", e); } } StringBuilder sb = new StringBuilder(); for (int i = feature.nextSetBit(0); i >= 0; i = feature.nextSetBit(i + 1)) { if (sb.length() > 0) sb.append(","); sb.append(PIG_FEATURE.values()[i].name()); } retStr = sb.toString(); featureMap.put(mro, retStr); } return retStr; }
From source file:au.org.ala.delta.translation.intkey.IntkeyCharactersFileWriter.java
protected void writeCharacterMask() { List<Boolean> includedCharacters = _encoder.encodeCharacterMasks(_dataSet, true); BitSet charMask = new BitSet(); for (int i = 0; i < includedCharacters.size(); i++) { if (includedCharacters.get(i)) { charMask.set(i); }//from ww w . java 2 s. c o m } _charsFile.writeCharacterMask(_dataSet.getNumberOfCharacters(), charMask); }
From source file:org.lockss.plugin.springer.api.SpringerApiCrawlSeed.java
/** * <p>/* w w w.j av a2s. co m*/ * Makes a URL fetcher for the given API request, that will parse the result * using the given {@link SpringerApiPamLinkExtractor} instance. * </p> * * @param ple * A {@link SpringerApiPamLinkExtractor} instance to parse the API * response with. * @param url * A query URL. * @return A URL fetcher for the given query URL. * @since 1.67.5 */ protected UrlFetcher makeApiUrlFetcher(final SpringerApiPamLinkExtractor ple, final String url, final String loggerUrl) { // Make a URL fetcher UrlFetcher uf = facade.makeUrlFetcher(url); // Set refetch flag BitSet permFetchFlags = uf.getFetchFlags(); permFetchFlags.set(UrlCacher.REFETCH_FLAG); uf.setFetchFlags(permFetchFlags); // Set custom crawl rate limiter uf.setCrawlRateLimiter(API_CRAWL_RATE_LIMITER); // Set custom URL consumer factory uf.setUrlConsumerFactory(new UrlConsumerFactory() { @Override public UrlConsumer createUrlConsumer(CrawlerFacade ucfFacade, FetchedUrlData ucfFud) { // Make custom URL consumer return new SimpleUrlConsumer(ucfFacade, ucfFud) { @Override public void consume() throws IOException { // Apply link extractor to URL and output results into a list final List<String> partial = new ArrayList<String>(); try { ple.extractUrls(au, fud.input, AuUtil.getCharsetOrDefault(fud.headers), // FIXME loggerUrl, // rather than fud.origUrl new Callback() { @Override public void foundLink(String url) { partial.add(url); } }); } catch (IOException ioe) { log.debug2("Link extractor threw", ioe); throw new IOException("Error while parsing PAM response for " + url, ioe); } finally { // Logging log.debug2(String.format("Step ending with %d URLs", partial.size())); if (log.isDebug3()) { log.debug3("URLs from step: " + partial.toString()); } // Output accumulated URLs to start URL list urlList.addAll(partial); } } }; } }); return uf; }
From source file:org.lockss.plugin.springer.link.BaseSpringerLinkCrawlSeed.java
/** * <p>// w w w. j av a 2 s . c o m * Makes a URL fetcher for the given API request, that will parse the result * using the given {@link SpringerLinkPamLinkExtractor} instance. * </p> * * @param ple * A {@link SpringerLinkPamLinkExtractor} instance to parse the API * response with. * @param url * A query URL. * @return A URL fetcher for the given query URL. * @since 1.67.5 */ protected UrlFetcher makeApiUrlFetcher(final SpringerLinkPamLinkExtractor ple, final String url, final String loggerUrl) { // Make a URL fetcher UrlFetcher uf = facade.makeUrlFetcher(url); // Set refetch flag BitSet permFetchFlags = uf.getFetchFlags(); permFetchFlags.set(UrlCacher.REFETCH_FLAG); uf.setFetchFlags(permFetchFlags); // Set custom crawl rate limiter uf.setCrawlRateLimiter(API_CRAWL_RATE_LIMITER); // Set custom URL consumer factory uf.setUrlConsumerFactory(new UrlConsumerFactory() { @Override public UrlConsumer createUrlConsumer(CrawlerFacade ucfFacade, FetchedUrlData ucfFud) { // Make custom URL consumer return new SimpleUrlConsumer(ucfFacade, ucfFud) { @Override public void consume() throws IOException { // Apply link extractor to URL and output results into a list final Set<String> partial = new HashSet<String>(); try { String au_cset = AuUtil.getCharsetOrDefault(fud.headers); String cset = CharsetUtil.guessCharsetFromStream(fud.input, au_cset); //FIXME 1.69 // Once guessCharsetFromStream correctly uses the hint instead of returning null // this local bit won't be needed. if (cset == null) { cset = au_cset; } // ple.extractUrls(au, fud.input, cset, loggerUrl, // rather than fud.origUrl new Callback() { @Override public void foundLink(String url) { partial.add(url); } }); } catch (IOException ioe) { log.debug2("Link extractor threw", ioe); throw new IOException("Error while parsing PAM response for " + loggerUrl, ioe); } finally { // Logging log.debug2(String.format("Step ending with %d URLs", partial.size())); if (log.isDebug3()) { log.debug3("URLs from step: " + partial.toString()); } // Output accumulated URLs to start URL list urlList.addAll(convertDoisToUrls(partial)); } } }; } }); return uf; }
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;//from w ww.jav a2 s .co 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: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() { {/*from ww w . j a va2s . co 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.tez.dag.app.dag.impl.DAGSchedulerNaturalOrderControlled.java
private void taskAttemptSeen(String vertexName, TezTaskAttemptID taskAttemptID) { BitSet scheduledTasks = vertexScheduledTasks.get(vertexName); if (scheduledTasks == null) { scheduledTasks = new BitSet(); vertexScheduledTasks.put(vertexName, scheduledTasks); }/*from w w w.ja v a 2s. c o m*/ if (taskAttemptID != null) { // null for 0 task vertices scheduledTasks.set(taskAttemptID.getTaskID().getId()); } }
From source file:edu.udo.scaffoldhunter.model.db.StringProperty.java
/** * Returns a new {@link BitSet} of the BitFingerprint * //w w w .ja v a2 s .c om * @return the fingerprint ({@link BitSet}) */ public BitSet getBitFingerprintBitSet() { checkBitFingerprint(); if (bitsCache != null) { return (BitSet) bitsCache.clone(); } BitSet bits = new BitSet(); byte[] bitFingerprint; bitFingerprint = Base64.decodeBase64(value); short length = bitFingerprintToLength(bitFingerprint); for (int i = 0; i < length; i++) { if ((bitFingerprint[i / sizeofbyte + lengthbytes] & (1 << (i % sizeofbyte))) > 0) { bits.set(i); } } bitsCache = (BitSet) bits.clone(); lengthCache = length; return bits; }
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 av a 2s .c o m*/ 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()); } } }