List of usage examples for java.util BitSet get
public boolean get(int bitIndex)
From source file:com.joliciel.jochre.graphics.VectorizerImpl.java
/** * Find n longest lines within shape which connect two points in the outline * @param outline//from w w w . java 2 s . co m * @param maxLines * @return */ List<LineSegment> getLongestLines(Shape shape, BitSet outline, int maxLines, int threshold) { TreeSet<LineSegment> lineSegmentSet = new TreeSet<LineSegment>(); int outlineCardinality = outline.cardinality(); int samplingInterval = outlineCardinality / 100; if (samplingInterval == 0) samplingInterval = 1; int samplingIndex = 0; for (int y = 0; y < shape.getHeight(); y++) { for (int x = 0; x < shape.getWidth(); x++) { if (outline.get(y * shape.getWidth() + x)) { // this pixel is part of the outline if (samplingIndex == 0) { lineSegmentSet.addAll(this.getLinesToEdge(shape, x, y, threshold)); } samplingIndex++; if (samplingIndex == samplingInterval) samplingIndex = 0; } } } int i = 0; List<LineSegment> lineSegments = new ArrayList<LineSegment>(); for (LineSegment lineSegment : lineSegmentSet) { lineSegments.add(lineSegment); i++; if (i >= maxLines) break; } if (LOG.isDebugEnabled()) { i = 0; for (LineSegment lineSegment : lineSegments) { double slope = (double) (lineSegment.getEndY() - lineSegment.getStartY()) / (double) (lineSegment.getEndX() - lineSegment.getStartX()); LOG.debug("Line " + i++ + "(" + lineSegment.getStartX() + "," + lineSegment.getStartY() + ") " + "(" + lineSegment.getEndX() + "," + lineSegment.getEndY() + "). Length = " + lineSegment.getLength() + ", Slope = " + slope); } } return lineSegments; }
From source file:com.opengamma.analytics.math.minimization.UncoupledParameterTransforms.java
/** * * @param startValues fixed parameter values (if no parameters are fixed this is completely ignored) * @param transforms Array of ParameterLimitsTransform (which can be the NullTransform which does NOT transform the parameter) which transform * a constrained function parameter (e.g. must be between -1 and 1) to a unconstrained fit parameter. * @param fixed BitSet with an element set to <b>true</b> if that parameter is fixed *//* w ww .ja v a 2 s .c o m*/ public UncoupledParameterTransforms(final DoubleMatrix1D startValues, final ParameterLimitsTransform[] transforms, final BitSet fixed) { ArgumentChecker.notNull(startValues, "null start values"); ArgumentChecker.notEmpty(transforms, "must specify transforms"); ArgumentChecker.notNull(fixed, "must specify what is fixed (even if none)"); _nMP = startValues.getNumberOfElements(); ArgumentChecker.isTrue(_nMP == transforms.length, "Have {}-dimensional start value but {} transforms", _nMP, transforms.length); _freeParameters = new boolean[_nMP]; for (int i = 0; i < _nMP; i++) { if (i < fixed.size()) { _freeParameters[i] = !fixed.get(i); } else { _freeParameters[i] = true; } } final int count = fixed.cardinality(); ArgumentChecker.isTrue(count < _nMP, "all parameters are fixed"); _nFP = _nMP - count; _startValues = startValues; _transforms = transforms; }
From source file:org.apache.hadoop.hive.serde2.compression.SnappyCompDe.java
/** * Decompress a set of columns from a ByteBuffer and update the position of the buffer. * * @param input A ByteBuffer with `position` indicating the starting point of the compressed chunk. * @param chunkSize The length of the compressed chunk to be decompressed from the input buffer. * * @return The set of columns./*from w w w . j a v a 2 s. c o m*/ */ @Override public ColumnBuffer[] decompress(ByteBuffer input, int chunkSize) { int startPos = input.position(); try { // Read the footer. int footerSize = input.getInt(startPos + chunkSize - 4); Iterator<Integer> compressedSize = Arrays .asList(ArrayUtils.toObject(Snappy.uncompressIntArray(input.array(), input.arrayOffset() + startPos + chunkSize - Integer.SIZE / Byte.SIZE - footerSize, footerSize))) .iterator(); // Read the header. int[] dataType = readIntegers(compressedSize.next(), input); int numOfCols = dataType.length; // Read the columns. ColumnBuffer[] outputCols = new ColumnBuffer[numOfCols]; for (int colNum = 0; colNum < numOfCols; colNum++) { byte[] nulls = readBytes(compressedSize.next(), input); switch (TTypeId.findByValue(dataType[colNum])) { case BOOLEAN_TYPE: { int numRows = input.getInt(); byte[] vals = readBytes(compressedSize.next(), input); BitSet bsBools = BitSet.valueOf(vals); boolean[] bools = new boolean[numRows]; for (int rowNum = 0; rowNum < numRows; rowNum++) { bools[rowNum] = bsBools.get(rowNum); } TBoolColumn column = new TBoolColumn(Arrays.asList(ArrayUtils.toObject(bools)), ByteBuffer.wrap(nulls)); outputCols[colNum] = new ColumnBuffer(TColumn.boolVal(column)); break; } case TINYINT_TYPE: { byte[] vals = readBytes(compressedSize.next(), input); TByteColumn column = new TByteColumn(Arrays.asList(ArrayUtils.toObject(vals)), ByteBuffer.wrap(nulls)); outputCols[colNum] = new ColumnBuffer(TColumn.byteVal(column)); break; } case SMALLINT_TYPE: { short[] vals = readShorts(compressedSize.next(), input); TI16Column column = new TI16Column(Arrays.asList(ArrayUtils.toObject(vals)), ByteBuffer.wrap(nulls)); outputCols[colNum] = new ColumnBuffer(TColumn.i16Val(column)); break; } case INT_TYPE: { int[] vals = readIntegers(compressedSize.next(), input); TI32Column column = new TI32Column(Arrays.asList(ArrayUtils.toObject(vals)), ByteBuffer.wrap(nulls)); outputCols[colNum] = new ColumnBuffer(TColumn.i32Val(column)); break; } case BIGINT_TYPE: { long[] vals = readLongs(compressedSize.next(), input); TI64Column column = new TI64Column(Arrays.asList(ArrayUtils.toObject(vals)), ByteBuffer.wrap(nulls)); outputCols[colNum] = new ColumnBuffer(TColumn.i64Val(column)); break; } case DOUBLE_TYPE: { double[] vals = readDoubles(compressedSize.next(), input); TDoubleColumn column = new TDoubleColumn(Arrays.asList(ArrayUtils.toObject(vals)), ByteBuffer.wrap(nulls)); outputCols[colNum] = new ColumnBuffer(TColumn.doubleVal(column)); break; } case BINARY_TYPE: { int[] rowSize = readIntegers(compressedSize.next(), input); ByteBuffer flattenedData = ByteBuffer.wrap(readBytes(compressedSize.next(), input)); ByteBuffer[] vals = new ByteBuffer[rowSize.length]; for (int rowNum = 0; rowNum < rowSize.length; rowNum++) { vals[rowNum] = ByteBuffer.wrap(flattenedData.array(), flattenedData.position(), rowSize[rowNum]); flattenedData.position(flattenedData.position() + rowSize[rowNum]); } TBinaryColumn column = new TBinaryColumn(Arrays.asList(vals), ByteBuffer.wrap(nulls)); outputCols[colNum] = new ColumnBuffer(TColumn.binaryVal(column)); break; } case STRING_TYPE: { int[] rowSize = readIntegers(compressedSize.next(), input); ByteBuffer flattenedData = ByteBuffer.wrap(readBytes(compressedSize.next(), input)); String[] vals = new String[rowSize.length]; for (int rowNum = 0; rowNum < rowSize.length; rowNum++) { vals[rowNum] = new String(flattenedData.array(), flattenedData.position(), rowSize[rowNum], StandardCharsets.UTF_8); flattenedData.position(flattenedData.position() + rowSize[rowNum]); } TStringColumn column = new TStringColumn(Arrays.asList(vals), ByteBuffer.wrap(nulls)); outputCols[colNum] = new ColumnBuffer(TColumn.stringVal(column)); break; } default: throw new IllegalStateException( "Unrecognized column type: " + TTypeId.findByValue(dataType[colNum])); } } input.position(startPos + chunkSize); return outputCols; } catch (IOException e) { e.printStackTrace(); return (ColumnBuffer[]) null; } }
From source file:edu.udo.scaffoldhunter.model.db.StringProperty.java
/** * This will generate a {@link PropertyType}.BitFingerprint. * /*from ww w. ja v a 2s . c o m*/ * Precondition: Only for PropertyType BitFingerprint! * * @param bits * the bits in form of a {@link BitSet} * @param length * the length as a {@link BitSet} does not save the exact length. */ public void setBitFingerprint(BitSet bits, short length) { checkBitFingerprint(); Preconditions.checkArgument(length < Math.pow(2, 16), "length exceeds range"); Preconditions.checkArgument(length > 0, "A length of zero is not supported"); // + 1 not full block + 2 size block byte[] bitFingerprint = new byte[length / sizeofbyte + ((length % sizeofbyte) > 0 ? 1 : 0) + lengthbytes]; // convert short to two bytes lenghtToBitFingerprint(length, bitFingerprint); // bits.lenght is faster because its the position of the highest on bit for (int i = 0; i < bits.length(); i++) { if (bits.get(i)) { // + 1 because the first char contains the size bitFingerprint[i / sizeofbyte + lengthbytes] |= 1 << (i % sizeofbyte); } } value = new String(Base64.encodeBase64String(bitFingerprint)); lengthCache = length; bitsCache = (BitSet) bits.clone(); }
From source file:org.apache.openjpa.kernel.AttachManager.java
private Object handleCascade(Object toAttach, OpenJPAStateManager owner) { StateManagerImpl sm = _broker.getStateManagerImpl(toAttach, true); BitSet loaded = sm.getLoaded(); FieldMetaData[] fmds = sm.getMetaData().getDefinedFields(); for (FieldMetaData fmd : fmds) { if (fmd.getElement().getCascadeAttach() == ValueMetaData.CASCADE_IMMEDIATE) { FieldMetaData[] inverseFieldMappings = fmd.getInverseMetaDatas(); if (inverseFieldMappings.length != 0) { _visitedNodes.add(sm);/*w w w . ja v a 2 s. co m*/ // Only try to attach this field is it is loaded if (loaded.get(fmd.getIndex())) { getStrategy(toAttach).attachField(this, toAttach, sm, fmd, true); } } } } return toAttach; }
From source file:org.apache.asterix.external.library.ClassAdParser.java
public static int checkOptionalConstraints(ARecordType recType, BitSet nulls) { for (int i = 0; i < recType.getFieldTypes().length; i++) { if (nulls.get(i) == false) { IAType type = recType.getFieldTypes()[i]; if (type.getTypeTag() != ATypeTag.MISSING && type.getTypeTag() != ATypeTag.UNION) { return i; }//from w w w . ja v a2 s . c om if (type.getTypeTag() == ATypeTag.UNION) { // union AUnionType unionType = (AUnionType) type; if (!unionType.isUnknownableType()) { return i; } } } } return -1; }
From source file:org.apache.hadoop.mapred.TestTextInputFormat.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;/* ww w . j a va 2 s . c o m*/ 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)); writer.write("\n"); } } finally { writer.close(); } // try splitting the file in a variety of sizes TextInputFormat format = new TextInputFormat(); format.configure(job); LongWritable key = new LongWritable(); Text value = new Text(); 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); if (length == 0) { assertEquals("Files of length 0 are not returned from FileInputFormat.getSplits().", 1, splits.length); assertEquals("Empty file length == 0", 0, splits[0].getLength()); } // check each split BitSet bits = new BitSet(length); for (int j = 0; j < splits.length; j++) { LOG.debug("split[" + j + "]= " + splits[j]); RecordReader<LongWritable, Text> reader = format.getRecordReader(splits[j], job, reporter); 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:com.moadbus.banking.iso.core.protocol.MessageFactory.java
/** * Creates a new message instance from the buffer, which must contain a * valid ISO8583 message. If the factory is set to use binary messages then * it will try to parse a binary message. * * @param buf/* ww w . j ava 2 s . c o m*/ * The byte buffer containing the message. Must not include the * length header. * @param isoHeaderLength * The expected length of the ISO header, after which the message * type and the rest of the message must come. */ public IsoMessage parseMessage(byte[] buf, int isoHeaderLength) throws ParseException { log.debug(" Message length =" + buf.length); IsoMessage m = new IsoMessage(isoHeaderLength > 0 ? new String(buf, 0, isoHeaderLength) : null); // TODO it only parses ASCII messages for now int type = 0; if (useBinary) { type = ((buf[isoHeaderLength] & 0xff) << 8) | (buf[isoHeaderLength + 1] & 0xff); } else { type = ((buf[isoHeaderLength] - 48) << 12) | ((buf[isoHeaderLength + 1] - 48) << 8) | ((buf[isoHeaderLength + 2] - 48) << 4) | (buf[isoHeaderLength + 3] - 48); } m.setType(type); // Parse the bitmap (primary first) BitSet bs = new BitSet(64); int pos = 0; if (useBinary) { for (int i = isoHeaderLength + 2; i < isoHeaderLength + 10; i++) { int bit = 128; for (int b = 0; b < 8; b++) { bs.set(pos++, (buf[i] & bit) != 0); bit >>= 1; } } // Check for secondary bitmap and parse if necessary if (bs.get(0)) { for (int i = isoHeaderLength + 10; i < isoHeaderLength + 18; i++) { int bit = 128; for (int b = 0; b < 8; b++) { bs.set(pos++, (buf[i] & bit) != 0); bit >>= 1; } } pos = 18 + isoHeaderLength; } else { pos = 10 + isoHeaderLength; } } else { for (int i = isoHeaderLength + 4; i < isoHeaderLength + 20; i++) { int hex = Integer.parseInt(new String(buf, i, 1), 16); bs.set(pos++, (hex & 8) > 0); bs.set(pos++, (hex & 4) > 0); bs.set(pos++, (hex & 2) > 0); bs.set(pos++, (hex & 1) > 0); } // Check for secondary bitmap and parse it if necessary if (bs.get(0)) { for (int i = isoHeaderLength + 20; i < isoHeaderLength + 36; i++) { int hex = Integer.parseInt(new String(buf, i, 1), 16); bs.set(pos++, (hex & 8) > 0); bs.set(pos++, (hex & 4) > 0); bs.set(pos++, (hex & 2) > 0); bs.set(pos++, (hex & 1) > 0); } pos = 36 + isoHeaderLength; } else { pos = 20 + isoHeaderLength; } } // Parse each field Map<Integer, FieldParseInfo> parseGuide = parseMap.get(type); List<Integer> index = parseOrder.get(type); log.debug(" Parsing bit "); log.debug(" Total index =" + index.size()); for (Integer i : index) { FieldParseInfo fpi = parseGuide.get(i); if (i == 124) { if (1 == 1) ; } log.debug((i) + ","); if (bs.get(i - 1)) { IsoValue val = useBinary ? fpi.parseBinary(buf, pos) : fpi.parse(buf, pos); log.debug("bit [" + i + "] len=" + val.getLength() + " val=" + val); m.setField(i, val); if (useBinary && !(val.getType() == IsoType.ALPHA || val.getType() == IsoType.LLVAR || val.getType() == IsoType.LLLVAR)) { pos += (val.getLength() / 2) + (val.getLength() % 2); } else { pos += val.getLength(); } if (val.getType() == IsoType.LLVAR) { pos += useBinary ? 1 : 2; } else if (val.getType() == IsoType.LLLVAR) { pos += useBinary ? 2 : 3; } } } log.debug("...done"); return m; }
From source file:org.lockss.util.NumberUtil.java
/** * Increment an alphabetical string by the given delta, considering the string * to represent a value in base-26 using the characters a-z or A-Z - the * string is treated case-insensitively. For example, with a delta of 1: * <ul>//from w w w. java 2s . com * <li>aaa to aab</li> * <li>aaz to aba</li> * <li>zzy to zzz</li> * <li>zyz to zza</li> * </ul> * <p> * Note that after 'z' comes 'ba', because 'a' corresponds to 0 and is so * every number is implicitly preceded by 'a'. This may not be what is desired * for some sequences, in which case this may need to be adapted. * <p> * The string is lower cased before the increment is applied, and then each * character position that was upper case in the original string is upper * cased before returning. An exception will be thrown if any character is * outside of a-z after lower casing. If the value limit for the string length * is reached, the returned string will be longer; the extra characters will * be lower cased, which may be unwanted. * * @param s a purely alphabetical string * @return a string incremented according to base-26 * @throws NumberFormatException if the string contains inappropriate characters or cannot be incremented */ public static String incrementBase26String(String s, int delta) throws NumberFormatException { // Track the case of each character, so we can reset them before returning BitSet cases = new BitSet(); for (int i = 0; i < s.length(); i++) { cases.set(i, Character.isUpperCase(s.charAt(i))); } // Convert, increment, convert back String res = toBase26(fromBase26(s.toLowerCase()) + delta); // Pad the string to the correct length with 'a' res = StringUtils.leftPad(res, s.length(), 'a'); // Re-case the chars - using an offset in case the new string is longer char[] carr = res.toCharArray(); int offset = carr.length - s.length(); for (int pos = 0; pos < s.length(); pos++) { if (cases.get(pos)) carr[offset + pos] = Character.toUpperCase(carr[offset + pos]); } return new String(carr); }
From source file:org.onosproject.tetopology.management.impl.TeTopologyManager.java
@Override public void updateTeTopology(TeTopology teTopology) { TeTopologyKey newKey = null;//from www. j a v a2 s .c om try { newKey = newTeTopologyKey(teTopology); } catch (ApplicationException e) { log.error("Ignoring the non-TE topology"); return; } // TE topology is updated here from other APP or NBI, the flag // BIT_CUSTOMIZED or BIT_MERGED should be set. BitSet flags = teTopology.flags(); if (flags == null || !(flags.get(TeTopology.BIT_CUSTOMIZED) || flags.get(TeTopology.BIT_MERGED))) { log.error("TE topology flags {} are not set properly", flags); return; } if (newKey != null) { DefaultTeTopology newTopology = new DefaultTeTopology( newKey == null ? teTopology.teTopologyId() : newKey, teTopology.teNodes(), teTopology.teLinks(), teTopology.teTopologyIdStringValue(), new CommonTopologyData(teTopology)); // Update with new data store.updateTeTopology(newTopology); } else { store.updateTeTopology(teTopology); } }