List of usage examples for java.util BitSet BitSet
private BitSet(long[] words)
From source file:com.joliciel.jochre.graphics.LineDefinitionImplTest.java
@Test public void testTrace(@NonStrict final Shape shape) { LineDefinitionImpl lineDef = new LineDefinitionImpl(0, 0); List<Integer> steps = new ArrayList<Integer>(); steps.add(2);/* w ww . j a v a2 s.co m*/ steps.add(3); lineDef.setSteps(steps); new NonStrictExpectations() { { shape.getHeight(); returns(8); shape.getWidth(); returns(8); } }; BitSet bitset = new BitSet(shape.getHeight() * shape.getWidth()); lineDef.trace(bitset, shape, 5, 2, 8, 0); int[] bitsetPixels = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; for (int x = 0; x < 8; x++) for (int y = 0; y < 8; y++) { assertEquals(bitsetPixels[y * 8 + x] == 1, bitset.get(y * 8 + x)); } bitset = new BitSet(shape.getHeight() * shape.getWidth()); lineDef.trace(bitset, shape, 1, 1, 4, 2); int[] bitsetPixels2 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; for (int x = 0; x < 8; x++) for (int y = 0; y < 8; y++) { assertEquals("failure at x=" + x + ",y=" + y, bitsetPixels2[y * 8 + x] == 1, bitset.get(y * 8 + x)); } }
From source file:at.tuwien.mnsa.smssender.SMSPDUConverter.java
/** * Get the PDU encoding for the given message * @param message//from w ww.j ava2s .c om * @param rightshift of the content for concatination * @return */ public SMSPDUConversionResult getContent(String message, int rightshift) { List<Byte> finalized = new LinkedList<>(); BitSet currentWorkingBS = new BitSet(16); int currentShiftpos = 0; boolean currentlyExtended = false; int len = 0; //repeat while there are characters left while (message.length() > 0) { String c = message.substring(0, 1); message = message.substring(1); byte value; //loook up current character if (this.GSM_3GPP_TS_23_038.containsKey(c)) { value = this.GSM_3GPP_TS_23_038.get(c); } else { if (this.GSM_3GPP_TS_23_038_EXTENSION.containsKey(c)) { if (!currentlyExtended) { //extension -> now do the escape character! //do the "new" character the other round message = c + message; currentlyExtended = true; value = this.GSM_3GPP_TS_23_038.get("\u001B"); } else { //we just did the ecsape character, now do the char from //the extended alphabet value = this.GSM_3GPP_TS_23_038_EXTENSION.get(c); currentlyExtended = false; } } else { throw new RuntimeException("Not found: " + c); } } //start at 0x0 if (currentShiftpos == 0) { //add current char beginning at pos 0 addByteToBitset(value, currentWorkingBS, 0, 1, 7); } else { //else start at second byte and do flipping //make place for the right bits of the current char in the front currentWorkingBS = rightShiftBitset(currentWorkingBS, currentShiftpos); //add last X bits in front of the bitset addByteToBitset(value, currentWorkingBS, 0, 8 - currentShiftpos, currentShiftpos); //add the first X bits at the end of the bitset if any if (currentShiftpos < 7) { addByteToBitset(value, currentWorkingBS, 8, 1, 7 - currentShiftpos); } //the first byte of the bitset is now complete! :) byte finalByte = currentWorkingBS.toByteArray()[0]; finalByte = swapEndianFormat(finalByte); finalized.add(finalByte); //shift bitset left by 8 bits since we just finished and exported a byte currentWorkingBS = leftShiftBitset(currentWorkingBS, 8); } currentShiftpos = (currentShiftpos + 1) % 8; len++; //for first character -> just add to the bitset //addByteToBitset(value, bitset, i*7); /*//exchange characters for (int j=0;j<((i%8)*8-(i%8)*7);j++) { boolean cBit = content.get() }*/ } //add last byte (swap back our eagerly shifted byte) if (currentShiftpos == 7) { byte finalByte = 0x00; finalized.add(finalByte); } else if (currentShiftpos != 0) { byte finalByte = (currentWorkingBS.isEmpty()) ? 0x0 : currentWorkingBS.toByteArray()[0]; finalByte = swapEndianFormat(finalByte); //I don't really know why, //but java fills right shifts with 1s //so we have to manually set all new (left) bits to zero for (int i = 0; i < currentShiftpos; i++) { finalByte = (byte) (finalByte >> 1); finalByte = (byte) (finalByte & 0x7F); //unset first bit } //finalByte = (byte) (finalByte & 0x3F); finalized.add(finalByte); } byte[] finalM = ArrayUtils.toPrimitive(finalized.toArray(new Byte[finalized.size()])); Logger.getGlobal().info("1: " + DatatypeConverter.printHexBinary(finalM)); //in case of rightshift for concatenation -> right shift the whole array if (rightshift > 0) { BitSet bs = BitSet.valueOf(finalM); bs = rightShiftBitset(bs, rightshift); finalM = bs.toByteArray(); Logger.getGlobal().info("2: " + DatatypeConverter.printHexBinary(finalM)); } SMSPDUConversionResult res = new SMSPDUConversionResult(finalM, len); return res; }
From source file:Randoms.java
/** Return a random BitSet with "size" bits, each having probability p of being true. */ public synchronized BitSet nextBitSet(int size, double p) { BitSet bs = new BitSet(size); for (int i = 0; i < size; i++) if (nextBoolean(p)) { bs.set(i);/* w ww. j a va2 s .co m*/ } return bs; }
From source file:uniol.apt.extension.ExtendStateFile.java
private BitSet codeStringToCode(String codeString) { BitSet code = new BitSet(codeLength); for (int i = 0; i < codeString.length(); i++) { if (codeString.charAt(i) == '1') { code.set(i);//from w w w . j a v a2 s . com } } return code; }
From source file:com.l2jfree.gameserver.idfactory.BitSetIDFactory.java
public void initialize() { try {// ww w .ja v a 2s . c o m _freeIds = new BitSet(PrimeFinder.nextPrime(100000)); _freeIds.clear(); _freeIdCount = new AtomicInteger(FREE_OBJECT_ID_SIZE); for (int usedObjectId : extractUsedObjectIDTable()) { int objectID = usedObjectId - FIRST_OID; if (objectID < 0) { // auction hack if (usedObjectId == 100100) { continue; } _log.warn("Object ID " + usedObjectId + " in DB is less than minimum ID of " + FIRST_OID); continue; } _freeIds.set(usedObjectId - FIRST_OID); _freeIdCount.decrementAndGet(); } _nextFreeId = new AtomicInteger(_freeIds.nextClearBit(0)); _initialized = true; } catch (Exception e) { _initialized = false; _log.fatal("BitSet ID Factory could not be initialized correctly", e); } }
From source file:nl.matsv.viabackwards.protocol.protocol1_9_4to1_10.chunks.Chunk1_10Type.java
@Override public Chunk read(ByteBuf input, ClientWorld world) throws Exception { int chunkX = input.readInt(); int chunkZ = input.readInt(); boolean groundUp = input.readBoolean(); int primaryBitmask = Type.VAR_INT.read(input); Type.VAR_INT.read(input);/* w w w . j a va 2 s . c o m*/ BitSet usedSections = new BitSet(16); ChunkSection1_10[] sections = new ChunkSection1_10[16]; // Calculate section count from bitmask for (int i = 0; i < 16; i++) { if ((primaryBitmask & (1 << i)) != 0) { usedSections.set(i); } } // Read sections for (int i = 0; i < 16; i++) { if (!usedSections.get(i)) continue; // Section not set ChunkSection1_10 section = new ChunkSection1_10(); sections[i] = section; section.readBlocks(input); section.readBlockLight(input); if (world.getEnvironment() == World.Environment.NORMAL) { section.readSkyLight(input); } } byte[] biomeData = groundUp ? new byte[256] : null; if (groundUp) { input.readBytes(biomeData); } List<CompoundTag> nbtData = Arrays.asList(Type.NBT_ARRAY.read(input)); // Temp patch for plugins that sent wrong too big chunks TODO find the issue in LibsDisguise and PR it. if (input.readableBytes() > 0) { byte[] array = new byte[input.readableBytes()]; input.readBytes(array); if (ViaVersion.getInstance().isDebug()) System.out.println("Found " + array.length + " more bytes than expected while reading the chunk"); } return new Chunk1_10(chunkX, chunkZ, groundUp, primaryBitmask, sections, biomeData, nbtData); }
From source file:org.rifidi.edge.adapter.alien.AlienGPIOService.java
@Override public void setGPO(String readerID, Collection<Integer> ports) throws CannotExecuteException { Alien9800ReaderSession session = getSession(readerID); BitSet bits = new BitSet(8); for (Integer port : ports) { bits.set(port);/*from w ww.ja va 2 s .c o m*/ } logger.info("Setting GPO on " + readerID + " Ports: " + ports); session.setOutputPort(bits); }
From source file:org.kuali.student.enrollment.class2.scheduleofclasses.sort.KSComparatorChain.java
protected void initializeComparatorChain() { BitSet bitSet = new BitSet(comparators.size()); int index = 0; for (KSComparator comparator : comparators) { bitSet.set(index++, comparator.isReverseSort()); }/* w ww. j a v a2 s. com*/ comparatorChain = new ComparatorChain(comparators, bitSet); }
From source file:uk.ac.ebi.atlas.experimentpage.baseline.genedistribution.BarChartTrader.java
protected int countGenesAboveCutoff(Map<FactorGroup, BitSet> geneBitSets, Set<Factor> filterFactors, Set<Factor> selectedFactors) { BitSet expressedGenesBitSet = new BitSet(AVERAGE_GENES_IN_EXPERIMENT); // get the union of genes expressed for all FactorGroups that are in the slice and have been selected for (FactorGroup factorGroup : geneBitSets.keySet()) { boolean factorGroupContainsAllFilterFactors = CollectionUtils.isEmpty(filterFactors) || factorGroup.containsAll(filterFactors); boolean factorGroupOverlapsSelectedFactors = CollectionUtils.isEmpty(selectedFactors) || factorGroup.overlapsWith(selectedFactors); if (factorGroupContainsAllFilterFactors && factorGroupOverlapsSelectedFactors) { expressedGenesBitSet.or(geneBitSets.get(factorGroup)); }//from w w w . j a va2 s .c om } return expressedGenesBitSet.cardinality(); }
From source file:com.joliciel.jochre.graphics.VectorizerImplTest.java
@Test public void testGetLongestLines(@NonStrict final Shape shape) { final int threshold = 100; final int maxLines = 60; final int whiteGapFillFactor = 5; new NonStrictExpectations() { {/*from www .j a v a 2s . c o m*/ shape.getHeight(); returns(8); shape.getWidth(); returns(8); int[] pixels = { 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0 }; for (int x = -1; x <= 8; x++) for (int y = -1; y <= 8; y++) { shape.isPixelBlack(x, y, threshold, whiteGapFillFactor); if (x >= 0 && x < 8 && y >= 0 && y < 8) returns(pixels[y * 8 + x] == 1); else returns(false); } } }; BitSet outline = new BitSet(64); int[] outlinePixels = { 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0 }; for (int x = 0; x < 8; x++) for (int y = 0; y < 8; y++) { outline.set(y * 8 + x, outlinePixels[y * 8 + x] == 1); } VectorizerImpl vectorizer = new VectorizerImpl(); GraphicsServiceInternal graphicsService = new GraphicsServiceImpl(); vectorizer.setGraphicsService(graphicsService); vectorizer.setWhiteGapFillFactor(whiteGapFillFactor); List<LineSegment> lines = vectorizer.getLongestLines(shape, outline, maxLines, threshold); assertEquals(maxLines, lines.size()); }