List of usage examples for java.util BitSet get
public boolean get(int bitIndex)
From source file:srebrinb.compress.sevenzip.SevenZFile.java
private void readSubStreamsInfo(final ByteBuffer header, final Archive archive) throws IOException { for (final Folder folder : archive.folders) { folder.numUnpackSubStreams = 1;//from ww w .j ava2 s . co m } int totalUnpackStreams = archive.folders.length; int nid = getUnsignedByte(header); if (nid == NID.kNumUnpackStream) { totalUnpackStreams = 0; for (final Folder folder : archive.folders) { final long numStreams = readUint64(header); folder.numUnpackSubStreams = (int) numStreams; totalUnpackStreams += numStreams; } nid = getUnsignedByte(header); } final SubStreamsInfo subStreamsInfo = new SubStreamsInfo(); subStreamsInfo.unpackSizes = new long[totalUnpackStreams]; subStreamsInfo.hasCrc = new BitSet(totalUnpackStreams); subStreamsInfo.crcs = new long[totalUnpackStreams]; int nextUnpackStream = 0; for (final Folder folder : archive.folders) { if (folder.numUnpackSubStreams == 0) { continue; } long sum = 0; if (nid == NID.kSize) { for (int i = 0; i < folder.numUnpackSubStreams - 1; i++) { final long size = readUint64(header); subStreamsInfo.unpackSizes[nextUnpackStream++] = size; sum += size; } } subStreamsInfo.unpackSizes[nextUnpackStream++] = folder.getUnpackSize() - sum; } if (nid == NID.kSize) { nid = getUnsignedByte(header); } int numDigests = 0; for (final Folder folder : archive.folders) { if (folder.numUnpackSubStreams != 1 || !folder.hasCrc) { numDigests += folder.numUnpackSubStreams; } } if (nid == NID.kCRC) { final BitSet hasMissingCrc = readAllOrBits(header, numDigests); final long[] missingCrcs = new long[numDigests]; for (int i = 0; i < numDigests; i++) { if (hasMissingCrc.get(i)) { missingCrcs[i] = 0xffffFFFFL & header.getInt(); } } int nextCrc = 0; int nextMissingCrc = 0; for (final Folder folder : archive.folders) { if (folder.numUnpackSubStreams == 1 && folder.hasCrc) { subStreamsInfo.hasCrc.set(nextCrc, true); subStreamsInfo.crcs[nextCrc] = folder.crc; ++nextCrc; } else { for (int i = 0; i < folder.numUnpackSubStreams; i++) { subStreamsInfo.hasCrc.set(nextCrc, hasMissingCrc.get(nextMissingCrc)); subStreamsInfo.crcs[nextCrc] = missingCrcs[nextMissingCrc]; ++nextCrc; ++nextMissingCrc; } } } nid = getUnsignedByte(header); } if (nid != NID.kEnd) { throw new IOException("Badly terminated SubStreamsInfo"); } archive.subStreamsInfo = subStreamsInfo; }
From source file:org.apache.hadoop.mapred.split.TestGroupedSplits.java
@Test(timeout = 10000) public void testFormat() throws Exception { JobConf job = new JobConf(defaultConf); Random random = new Random(); long seed = random.nextLong(); LOG.info("seed = " + seed); random.setSeed(seed);/* ww w .j a v a 2s . c o m*/ localFs.delete(workDir, true); FileInputFormat.setInputPaths(job, workDir); final int length = 10000; final int numFiles = 10; createFiles(length, numFiles, random); // create a combined split for the files TextInputFormat wrappedFormat = new TextInputFormat(); wrappedFormat.configure(job); TezGroupedSplitsInputFormat<LongWritable, Text> format = new TezGroupedSplitsInputFormat<LongWritable, Text>(); format.setConf(job); format.setDesiredNumberOfSplits(1); format.setInputFormat(wrappedFormat); LongWritable key = new LongWritable(); Text value = new Text(); for (int i = 0; i < 3; i++) { int numSplits = random.nextInt(length / 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 TezGroupedSplit", TezGroupedSplit.class, split.getClass()); // check the split BitSet bits = new BitSet(length); LOG.debug("split= " + split); RecordReader<LongWritable, Text> reader = format.getRecordReader(split, job, voidReporter); try { int count = 0; while (reader.next(key, value)) { int v = Integer.parseInt(value.toString()); LOG.debug("read " + v); if (bits.get(v)) { LOG.warn("conflict with " + v + " at position " + reader.getPos()); } assertFalse("Key in multiple partitions.", bits.get(v)); bits.set(v); count++; } LOG.info("splits=" + split + " count=" + count); } finally { reader.close(); } assertEquals("Some keys in no partition.", length, bits.cardinality()); } }
From source file:org.apache.poi.ss.format.CellNumberFormatter.java
/** {@inheritDoc} */ public void formatValue(StringBuffer toAppendTo, Object valueObject) { double value = ((Number) valueObject).doubleValue(); value *= scale;/*w ww . j a v a 2 s.c om*/ // the '-' sign goes at the front, always, so we pick it out boolean negative = value < 0; if (negative) value = -value; // Split out the fractional part if we need to print a fraction double fractional = 0; if (slash != null) { if (improperFraction) { fractional = value; value = 0; } else { fractional = value % 1.0; //noinspection SillyAssignment value = (long) value; } } Set<StringMod> mods = new TreeSet<>(); StringBuffer output = new StringBuffer(desc); if (exponent != null) { writeScientific(value, output, mods); } else if (improperFraction) { writeFraction(value, null, fractional, output, mods); } else { StringBuffer result = new StringBuffer(); Formatter f = new Formatter(result); f.format(LOCALE, printfFmt, value); if (numerator == null) { writeFractional(result, output); writeInteger(result, output, integerSpecials, mods, integerCommas); } else { writeFraction(value, result, fractional, output, mods); } } // Now strip out any remaining '#'s and add any pending text ... ListIterator<Special> it = specials.listIterator(); Iterator<StringMod> changes = mods.iterator(); StringMod nextChange = (changes.hasNext() ? changes.next() : null); int adjust = 0; BitSet deletedChars = new BitSet(); // records chars already deleted while (it.hasNext()) { Special s = it.next(); int adjustedPos = s.pos + adjust; if (!deletedChars.get(s.pos) && output.charAt(adjustedPos) == '#') { output.deleteCharAt(adjustedPos); adjust--; deletedChars.set(s.pos); } while (nextChange != null && s == nextChange.special) { int lenBefore = output.length(); int modPos = s.pos + adjust; int posTweak = 0; switch (nextChange.op) { case StringMod.AFTER: // ignore adding a comma after a deleted char (which was a '#') if (nextChange.toAdd.equals(",") && deletedChars.get(s.pos)) break; posTweak = 1; //noinspection fallthrough case StringMod.BEFORE: output.insert(modPos + posTweak, nextChange.toAdd); break; case StringMod.REPLACE: int delPos = s.pos; // delete starting pos in original coordinates if (!nextChange.startInclusive) { delPos++; modPos++; } // Skip over anything already deleted while (deletedChars.get(delPos)) { delPos++; modPos++; } int delEndPos = nextChange.end.pos; // delete end point in original if (nextChange.endInclusive) delEndPos++; int modEndPos = delEndPos + adjust; // delete end point in current if (modPos < modEndPos) { if (nextChange.toAdd == "") output.delete(modPos, modEndPos); else { char fillCh = nextChange.toAdd.charAt(0); for (int i = modPos; i < modEndPos; i++) output.setCharAt(i, fillCh); } deletedChars.set(delPos, delEndPos); } break; default: throw new IllegalStateException("Unknown op: " + nextChange.op); } adjust += output.length() - lenBefore; if (changes.hasNext()) nextChange = changes.next(); else nextChange = null; } } // Finally, add it to the string if (negative) toAppendTo.append('-'); toAppendTo.append(output); }
From source file:bobs.is.compress.sevenzip.SevenZFile.java
private void readSubStreamsInfo(final DataInput header, final Archive archive) throws IOException { for (final Folder folder : archive.folders) { folder.numUnpackSubStreams = 1;// w w w .ja v a 2 s . co m } int totalUnpackStreams = archive.folders.length; int nid = header.readUnsignedByte(); if (nid == NID.kNumUnpackStream) { totalUnpackStreams = 0; for (final Folder folder : archive.folders) { final long numStreams = readUint64(header); folder.numUnpackSubStreams = (int) numStreams; totalUnpackStreams += numStreams; } nid = header.readUnsignedByte(); } final SubStreamsInfo subStreamsInfo = new SubStreamsInfo(); subStreamsInfo.unpackSizes = new long[totalUnpackStreams]; subStreamsInfo.hasCrc = new BitSet(totalUnpackStreams); subStreamsInfo.crcs = new long[totalUnpackStreams]; int nextUnpackStream = 0; for (final Folder folder : archive.folders) { if (folder.numUnpackSubStreams == 0) { continue; } long sum = 0; if (nid == NID.kSize) { for (int i = 0; i < folder.numUnpackSubStreams - 1; i++) { final long size = readUint64(header); subStreamsInfo.unpackSizes[nextUnpackStream++] = size; sum += size; } } subStreamsInfo.unpackSizes[nextUnpackStream++] = folder.getUnpackSize() - sum; } if (nid == NID.kSize) { nid = header.readUnsignedByte(); } int numDigests = 0; for (final Folder folder : archive.folders) { if (folder.numUnpackSubStreams != 1 || !folder.hasCrc) { numDigests += folder.numUnpackSubStreams; } } if (nid == NID.kCRC) { final BitSet hasMissingCrc = readAllOrBits(header, numDigests); final long[] missingCrcs = new long[numDigests]; for (int i = 0; i < numDigests; i++) { if (hasMissingCrc.get(i)) { missingCrcs[i] = 0xffffFFFFL & Integer.reverseBytes(header.readInt()); } } int nextCrc = 0; int nextMissingCrc = 0; for (final Folder folder : archive.folders) { if (folder.numUnpackSubStreams == 1 && folder.hasCrc) { subStreamsInfo.hasCrc.set(nextCrc, true); subStreamsInfo.crcs[nextCrc] = folder.crc; ++nextCrc; } else { for (int i = 0; i < folder.numUnpackSubStreams; i++) { subStreamsInfo.hasCrc.set(nextCrc, hasMissingCrc.get(nextMissingCrc)); subStreamsInfo.crcs[nextCrc] = missingCrcs[nextMissingCrc]; ++nextCrc; ++nextMissingCrc; } } } nid = header.readUnsignedByte(); } if (nid != NID.kEnd) { throw new IOException("Badly terminated SubStreamsInfo"); } archive.subStreamsInfo = subStreamsInfo; }
From source file:org.apache.hadoop.mapred.TestKeyValueTextInputFormat.java
public void testFormat() throws Exception { JobConf job = new JobConf(); Path file = new Path(workDir, "test.txt"); // A reporter that does nothing Reporter reporter = Reporter.NULL;//from w w w .j av a 2s .c om int seed = new Random().nextInt(); LOG.info("seed = " + seed); Random random = new Random(seed); localFs.delete(workDir, true); FileInputFormat.setInputPaths(job, workDir); // for a variety of lengths for (int length = 0; length < MAX_LENGTH; length += random.nextInt(MAX_LENGTH / 10) + 1) { LOG.debug("creating; entries = " + length); // create a file with length entries Writer writer = new OutputStreamWriter(localFs.create(file)); try { for (int i = 0; i < length; i++) { writer.write(Integer.toString(i * 2)); writer.write("\t"); writer.write(Integer.toString(i)); writer.write("\n"); } } finally { writer.close(); } // try splitting the file in a variety of sizes KeyValueTextInputFormat format = new KeyValueTextInputFormat(); format.configure(job); for (int i = 0; i < 3; i++) { int numSplits = random.nextInt(MAX_LENGTH / 20) + 1; LOG.debug("splitting: requesting = " + numSplits); InputSplit[] splits = format.getSplits(job, numSplits); LOG.debug("splitting: got = " + splits.length); // check each split BitSet bits = new BitSet(length); for (int j = 0; j < splits.length; j++) { LOG.debug("split[" + j + "]= " + splits[j]); RecordReader<Text, Text> reader = format.getRecordReader(splits[j], job, reporter); Class readerClass = reader.getClass(); assertEquals("reader class is KeyValueLineRecordReader.", KeyValueLineRecordReader.class, readerClass); Text key = reader.createKey(); Class keyClass = key.getClass(); Text value = reader.createValue(); Class valueClass = value.getClass(); assertEquals("Key class is Text.", Text.class, keyClass); assertEquals("Value class is Text.", Text.class, valueClass); try { int count = 0; while (reader.next(key, value)) { int v = Integer.parseInt(value.toString()); LOG.debug("read " + v); if (bits.get(v)) { LOG.warn("conflict with " + v + " in split " + j + " at position " + reader.getPos()); } assertFalse("Key in multiple partitions.", bits.get(v)); bits.set(v); count++; } LOG.debug("splits[" + j + "]=" + splits[j] + " count=" + count); } finally { reader.close(); } } assertEquals("Some keys in no partition.", length, bits.cardinality()); } } }
From source file:org.jpos.iso.ISOBasePackager.java
/** * @param m/*from w ww. j ava2s .com*/ * the Container of this message * @param b * ISO message image * @return consumed bytes * @exception ISOException */ public int unpack(ISOComponent m, byte[] b) throws ISOException { LogEvent evt = new LogEvent(this, "unpack"); int consumed = 0; try { if (m.getComposite() != m) throw new ISOException("Can't call packager on non Composite"); if (logger != null) // save a few CPU cycle if no logger available evt.addMessage(CardNoEncodeUtil.encodeCardNo((ISOUtil.hexString(b)))); // if ISOMsg and headerLength defined // if (m instanceof ISOMsg /* && ((ISOMsg) m).getHeader()==null */ // && headerLength > 0) { // byte[] h = new byte[headerLength]; // System.arraycopy(b, 0, h, 0, headerLength); // ((ISOMsg) m).setHeader(h); // consumed += headerLength; // } if (!(fld[0] == null) && !(fld[0] instanceof ISOBitMapPackager)) { ISOComponent mti = fld[0].createComponent(0); consumed += fld[0].unpack(mti, b, consumed); m.set(mti); } BitSet bmap = null; int maxField = fld.length; if (emitBitMap()) { ISOBitMap bitmap = new ISOBitMap(-1); consumed += getBitMapfieldPackager().unpack(bitmap, b, consumed); bmap = (BitSet) bitmap.getValue(); if (logger != null) evt.addMessage("<bitmap>" + bmap.toString() + "</bitmap>"); m.set(bitmap); maxField = Math.min(maxField, bmap.size()); } for (int i = getFirstField(); i < maxField; i++) { try { if (bmap == null && fld[i] == null) continue; if (maxField > 128 && i == 65) continue; // ignore extended bitmap if (bmap == null || bmap.get(i)) { if (fld[i] == null) throw new ISOException("field packager '" + i + "' is null"); ISOComponent c = fld[i].createComponent(i); consumed += fld[i].unpack(c, b, consumed); if (logger != null) { String value = c.getValue() + ""; evt.addMessage("<unpack fld=\"" + i + "\" packager=\"" + fld[i].getClass().getName() + "\" name=\"" + fld[i].getDescription() + "\">"); if (c.getValue() instanceof ISOMsg) evt.addMessage(c.getValue()); else if (c.getValue() instanceof byte[]) { evt.addMessage(" <value type='binary'>" + ISOUtil.hexString((byte[]) c.getValue()) + "</value>"); } else { if (i == 2) { value = simpleEncodeCardNo(value); } evt.addMessage(" <value>" + value + "</value>"); } evt.addMessage("</unpack>"); } m.set(c); } } catch (ISOException e) { evt.addMessage("error unpacking field " + i + " consumed=" + consumed); evt.addMessage(e); // jPOS-3 e = new ISOException(String.format("%s (%s) unpacking field=%d, consumed=%d", e.getMessage(), e.getNested().toString(), i, consumed)); throw e; } } if (b.length != consumed) { evt.addMessage("WARNING: unpack len=" + b.length + " consumed=" + consumed); } return consumed; } catch (ISOException e) { evt.addMessage(e); throw e; } catch (Exception e) { evt.addMessage(e); throw new ISOException(e.getMessage() + " consumed=" + consumed); } finally { Logger.log(evt); } }
From source file:juicebox.data.MatrixZoomData.java
/** * Computes eigenvector from Pearson's.// ww w. j a v a2 s . c om * * @param df Expected values, needed to get Pearson's * @param which Which eigenvector; 0 is principal. * @return Eigenvector */ public double[] computeEigenvector(ExpectedValueFunction df, int which) { BasicMatrix pearsons = getPearsons(df); if (pearsons == null) { return null; } int dim = pearsons.getRowDimension(); double[][] data = new double[dim][dim]; BitSet bitSet = new BitSet(dim); for (int i = 0; i < dim; i++) { for (int j = 0; j < dim; j++) { float tmp = pearsons.getEntry(i, j); data[i][j] = tmp; if (data[i][j] != 0 && !Float.isNaN(tmp)) { bitSet.set(i); } } } int[] nonCentromereColumns = new int[bitSet.cardinality()]; int count = 0; for (int i = 0; i < dim; i++) { if (bitSet.get(i)) nonCentromereColumns[count++] = i; } RealMatrix subMatrix = new Array2DRowRealMatrix(data).getSubMatrix(nonCentromereColumns, nonCentromereColumns); RealVector rv = (new EigenDecompositionImpl(subMatrix, 0)).getEigenvector(which); double[] ev = rv.toArray(); int size = pearsons.getColumnDimension(); double[] eigenvector = new double[size]; int num = 0; for (int i = 0; i < size; i++) { if (num < nonCentromereColumns.length && i == nonCentromereColumns[num]) { eigenvector[i] = ev[num]; num++; } else { eigenvector[i] = Double.NaN; } } return eigenvector; }
From source file:org.lockss.crawler.TestNewContentCrawler.java
public void testPassesParamsToUrlCacherDefault() { MockCachedUrlSet cus = (MockCachedUrlSet) mau.getAuCachedUrlSet(); mau.addUrl(startUrl);/*from ww w. j a va2 s. c o m*/ assertTrue(crawler.doCrawl()); MockUrlCacher uc = (MockUrlCacher) mau.makeUrlCacher(startUrl); BitSet fetchFlags = uc.getFetchFlags(); assertFalse(fetchFlags.get(UrlCacher.REFETCH_FLAG)); assertTrue(fetchFlags.get(UrlCacher.CLEAR_DAMAGE_FLAG)); assertTrue(fetchFlags.get(UrlCacher.REFETCH_IF_DAMAGE_FLAG)); }
From source file:org.lockss.crawler.TestNewContentCrawler.java
public void testPassesParamsToUrlCacherParamsNeg() { Properties p = new Properties(); p.setProperty(FollowLinkCrawler.PARAM_REFETCH_IF_DAMAGED, "false"); p.setProperty(FollowLinkCrawler.PARAM_CLEAR_DAMAGE_ON_FETCH, "false"); ConfigurationUtil.addFromProps(p);/* w w w.ja v a 2 s .co m*/ MockCachedUrlSet cus = (MockCachedUrlSet) mau.getAuCachedUrlSet(); mau.addUrl(startUrl); assertTrue(crawler.doCrawl()); MockUrlCacher uc = (MockUrlCacher) mau.makeUrlCacher(startUrl); BitSet fetchFlags = uc.getFetchFlags(); assertFalse(fetchFlags.get(UrlCacher.REFETCH_FLAG)); assertFalse(fetchFlags.get(UrlCacher.CLEAR_DAMAGE_FLAG)); assertFalse(fetchFlags.get(UrlCacher.REFETCH_IF_DAMAGE_FLAG)); }
From source file:org.lockss.crawler.TestNewContentCrawler.java
public void testPassesParamsToUrlCacherParamsPos() { Properties p = new Properties(); p.setProperty(FollowLinkCrawler.PARAM_REFETCH_IF_DAMAGED, "true"); p.setProperty(FollowLinkCrawler.PARAM_CLEAR_DAMAGE_ON_FETCH, "true"); ConfigurationUtil.addFromProps(p);/* www . ja va 2 s.co m*/ MockCachedUrlSet cus = (MockCachedUrlSet) mau.getAuCachedUrlSet(); mau.addUrl(startUrl); assertTrue(crawler.doCrawl()); MockUrlCacher uc = (MockUrlCacher) mau.makeUrlCacher(startUrl); BitSet fetchFlags = uc.getFetchFlags(); assertFalse(fetchFlags.get(UrlCacher.REFETCH_FLAG)); assertTrue(fetchFlags.get(UrlCacher.CLEAR_DAMAGE_FLAG)); assertTrue(fetchFlags.get(UrlCacher.REFETCH_IF_DAMAGE_FLAG)); }