List of usage examples for java.util BitSet get
public boolean get(int bitIndex)
From source file:at.tuwien.mnsa.smssender.SMSPDUConverter.java
/** * Right shift a bitset/*from w ww. j a v a 2s . c om*/ * @param bitset * @param positions * @return */ private BitSet rightShiftBitset(BitSet bitset, int positions) { for (int j = 0; j < positions; j++) { for (int i = bitset.length(); i > 0; i--) { bitset.set(i, bitset.get(i - 1)); } bitset.set(j, false); } return bitset; }
From source file:mastodon.algorithms.SABisectionAlgorithm.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;/*from w w w . ja v a2 s .c o m*/ } 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(); }
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);//from ww w . j av a2 s . c om 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.apache.metron.stellar.common.utils.hashing.tlsh.TLSHHasher.java
public Map<String, String> bin(String hash) throws DecoderException { Random r = new Random(0); byte[] h = Hex.decodeHex(hash.substring(2 * checksumOption.getChecksumLength()).toCharArray()); BitSet vector = BitSet.valueOf(h); int n = vector.length(); Map<String, String> ret = new HashMap<>(); boolean singleHash = hashes.size() == 1; for (int numHashes : hashes) { BitSet projection = new BitSet(); for (int i = 0; i < numHashes; ++i) { int index = r.nextInt(n); projection.set(i, vector.get(index)); }/*from w w w . j av a 2 s .com*/ String outputHash = numHashes + Hex.encodeHexString(projection.toByteArray()); if (singleHash) { ret.put(TLSH_BIN_KEY, outputHash); } else { ret.put(TLSH_BIN_KEY + "_" + numHashes, outputHash); } } return ret; }
From source file:mastodon.algorithms.SALinearAlgorithm.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;/*ww w. j ava 2 s .c o m*/ } if (numberToSet > (bts.getTaxaCount() - currPrunedSpeciesCount)) { numberToSet = bts.getTaxaCount() - currPrunedSpeciesCount; } //if we are pruning by one more species now, clear one species less from the pruning list this time if (currPruning.cardinality() < currPrunedSpeciesCount) { numberToClear = numberToSet - 1; } else { 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(); }
From source file:net.solarnetwork.node.control.sma.pcm.ModbusPCMController.java
/** * Get the status value of the PCM, as an Integer. * // w w w . j a va 2 s. c om * <p> * This returns the overall vale of the PCM, as an integer between 0 and 15. * A value of 0 represent a 0% output setting, while 15 represents 100%. * </p> * * @return an integer between 0 and 15 */ private Integer integerValueForBitSet(BitSet bits) { return ((bits.get(0) ? 1 : 0) | ((bits.get(1) ? 1 : 0) << 1) | ((bits.get(2) ? 1 : 0) << 2) | ((bits.get(3) ? 1 : 0) << 3)); }
From source file:org.jax.bham.test.HaplotypeAssociationTestGraphPanel.java
private List<JComponent> createContextMenuItems() { List<JComponent> menuItems = new ArrayList<JComponent>(); // TODO is getSelectedIntervals thread safe here? probably not int[] chromosomes = this.getSelectedChromosomes(); if (this.lastClickedIntervalIndex >= 0 && chromosomes.length == 1) { RealValuedBasePairInterval[] selectedIntervals = this.chromosomeToNegLogValueMap.get(chromosomes[0]); RealValuedBasePairInterval selectedInterval = selectedIntervals[this.lastClickedIntervalIndex]; menuItems.add(new JMenuItem(new GoToMouseIntervalInCGDSnpDatabaseAction(selectedInterval, BhamApplication.getInstance().getBhamFrame()))); menuItems.add(new JMenuItem(new GoToMouseIntervalInCGDGBrowseAction(selectedInterval, BhamApplication.getInstance().getBhamFrame()))); menuItems.add(new JMenuItem(new GoToMouseIntervalInUCSCBrowserAction(selectedInterval, BhamApplication.getInstance().getBhamFrame()))); menuItems.add(new JSeparator()); if (selectedInterval instanceof CompositeRealValuedBasePairInterval) { CompositeRealValuedBasePairInterval selectedCompositeInterval = (CompositeRealValuedBasePairInterval) selectedInterval; if (selectedCompositeInterval.getDelegateInterval() instanceof BinaryStrainPartition) { BinaryStrainPartition partition = (BinaryStrainPartition) selectedCompositeInterval .getDelegateInterval(); Set<String> strains = this.testToPlot.getCommonStrains(); String[] sortedStrains = strains.toArray(new String[strains.size()]); Arrays.sort(sortedStrains); Set<String> inStrains = new HashSet<String>(); Set<String> outStrains = new HashSet<String>(); Map<String, Set<String>> strainGroups = new HashMap<String, Set<String>>(); strainGroups.put("Strains Inside Haplotype Block", inStrains); strainGroups.put("Strains Outside Haplotype Block", outStrains); BitSet bits = partition.getStrainBitSet(); for (int i = 0; i < sortedStrains.length; i++) { if (bits.get(i)) { inStrains.add(sortedStrains[i]); } else { outStrains.add(sortedStrains[i]); }//from www . j av a 2 s . c o m } menuItems.add(new JMenuItem(new ShowPhenotypeEffectPlotAction( this.testToPlot.getPhenotypeDataSource(), strainGroups))); menuItems.add(new JSeparator()); } } } return menuItems; }
From source file:org.apache.eagle.alert.engine.serialization.PartitionedEventSerializerTest.java
@Test public void testBitSet() { BitSet bitSet = new BitSet(); bitSet.set(0, true); // 1 bitSet.set(1, false); // 0 bitSet.set(2, true); // 1 LOG.info("Bit Set Size: {}", bitSet.size()); LOG.info("Bit Set Byte[]: {}", bitSet.toByteArray()); LOG.info("Bit Set Byte[]: {}", bitSet.toLongArray()); LOG.info("BitSet[0]: {}", bitSet.get(0)); LOG.info("BitSet[1]: {}", bitSet.get(1)); LOG.info("BitSet[1]: {}", bitSet.get(2)); byte[] bytes = bitSet.toByteArray(); BitSet bitSet2 = BitSet.valueOf(bytes); LOG.info("Bit Set Size: {}", bitSet2.size()); LOG.info("Bit Set Byte[]: {}", bitSet2.toByteArray()); LOG.info("Bit Set Byte[]: {}", bitSet2.toLongArray()); LOG.info("BitSet[0]: {}", bitSet2.get(0)); LOG.info("BitSet[1]: {}", bitSet2.get(1)); LOG.info("BitSet[1]: {}", bitSet2.get(2)); BitSet bitSet3 = new BitSet(); bitSet3.set(0, true);//from w w w . j ava 2s .co m Assert.assertEquals(1, bitSet3.length()); BitSet bitSet4 = new BitSet(); bitSet4.set(0, false); Assert.assertEquals(0, bitSet4.length()); Assert.assertFalse(bitSet4.get(1)); Assert.assertFalse(bitSet4.get(2)); }
From source file:org.apache.lucene.index.collocations.CollocationExtractor.java
private void populateHashMapWithPhraseTerms(Term term, int numDocsForTerm, int totalNumDocs, HashMap<String, CollocationScorer> phraseTerms, BitSet termPos, String[] terms, int j, boolean[] matchFound, int startpos, int endpos) throws IOException { for (int prevpos = startpos; (prevpos <= endpos) && (!matchFound[j]); prevpos++) { if (termPos.get(prevpos)) { // Add term to hashmap containing co-occurrence // counts for this term CollocationScorer pt = (CollocationScorer) phraseTerms.get(terms[j]); if (pt == null) { // TermEnum otherTe = reader.terms(new Term(fieldName, terms[j])); Term otherTe = new Term(this.fieldName, terms[j]); int numDocsForOtherTerm = Math.min(this.reader.docFreq(otherTe), maxNumDocsToAnalyze); float otherPercent = (float) numDocsForOtherTerm / (float) totalNumDocs; // check other term is not too rare or frequent if (otherPercent < minTermPopularity) { System.out.println(term.text() + " not popular enough " + otherPercent); matchFound[j] = true; continue; }/* ww w.ja v a2 s . co m*/ if (otherPercent > maxTermPopularity) { System.out.println(term.text() + " too popular " + otherPercent); matchFound[j] = true; continue; } // public CollocationScorer(String term, String coincidentalTerm, int termADocFreq, int termBDocFreq) pt = new CollocationScorer(term.text(), terms[j], numDocsForTerm, numDocsForOtherTerm, 0, 0); phraseTerms.put(pt.coincidentalTerm, pt); } pt.incCoIncidenceDocCount(); matchFound[j] = true; } } }
From source file:dk.alexandra.fresco.demo.PrivateSetDemo.java
/** * This is where the actual computation is defined. The method builds up a * protocol that does one evaluation of an AES block encryption for each * of the provided input integers. This//from w w w. j a va 2 s . c o m * involves protocols for 'closing' the plaintexts and keys, i.e., converting * them from something that one of the players knows to secret values. It * also involves a protocol for AES that works on secret values, and * protocols for opening up the resulting ciphertext. * * The final protocol is build from smaller protocols using the * ParallelProtocolProducer and SequentialProtocolProducer. The open and * closed values (OBool and SBool) are used to 'glue' the subprotocols * together. * */ //May cause problems if more than 2 parties and if both insets are not of //Equal length @Override public ProtocolProducer prepareApplication(ProtocolFactory factory) { if (!(factory instanceof BasicLogicFactory)) { throw new MPCException(factory.getClass().getSimpleName() + " is not a BasicLogicFactory. This Private Set demo requires a protocol suite that implements the BasicLogicFactory."); } BasicLogicFactory boolFactory = (BasicLogicFactory) factory; OBool[] key2Open = new OBool[BLOCK_SIZE]; OBool[] key1Open = new OBool[BLOCK_SIZE]; for (int i = 0; i < BLOCK_SIZE; i++) { key1Open[i] = boolFactory.getOBool(); key2Open[i] = boolFactory.getOBool(); if (this.id == 1) { key1Open[i].setValue(this.inKey[i]); } else if (this.id == 2) { key2Open[i].setValue(this.inKey[i]); } else { // OK, there might be more players, but they don't have input. } } // Establish some secure values. SBool[] key1Closed = boolFactory.getSBools(BLOCK_SIZE); SBool[] key2Closed = boolFactory.getSBools(BLOCK_SIZE); // Build protocol where player 1 closes his key. ProtocolProducer[] closeKey1Bits = new ProtocolProducer[BLOCK_SIZE]; for (int i = 0; i < BLOCK_SIZE; i++) { closeKey1Bits[i] = boolFactory.getCloseProtocol(1, key1Open[i], key1Closed[i]); } ProtocolProducer closeKey1 = new ParallelProtocolProducer(closeKey1Bits); // Build protocol where player 2 closes his key. ProtocolProducer[] closeKey2Bits = new ProtocolProducer[BLOCK_SIZE]; for (int i = 0; i < BLOCK_SIZE; i++) { closeKey2Bits[i] = boolFactory.getCloseProtocol(2, key2Open[i], key2Closed[i]); } ProtocolProducer closeKey2 = new ParallelProtocolProducer(closeKey2Bits); // We can close both keys in parallel. ProtocolProducer closeKeys = new ParallelProtocolProducer(closeKey1, closeKey2); // XOR the keys together. SBool[] combinedKey = boolFactory.getSBools(BLOCK_SIZE); ProtocolProducer[] combineKeyBits = new ProtocolProducer[BLOCK_SIZE]; for (int i = 0; i < BLOCK_SIZE; i++) { combineKeyBits[i] = boolFactory.getXorProtocol(key1Closed[i], key2Closed[i], combinedKey[i]); } ProtocolProducer combineKeys = new ParallelProtocolProducer(combineKeyBits); // Initialize various arrays OBool[][] inputsOpen = new OBool[2 * this.inSet.length][BLOCK_SIZE]; //Arrays of open inputs SBool[][] inputsClosed = new SBool[this.inSet.length * 2][BLOCK_SIZE]; //Arrays of closed inputs SBool[][] outClosed = new SBool[this.inSet.length * 2][BLOCK_SIZE]; //Arrays of closed result this.result = new OBool[this.inSet.length * 2][]; //Arrays of resulting ciphertexts for (int i = 0; i < this.inSet.length * 2; i++) { inputsOpen[i] = boolFactory.getOBools(BLOCK_SIZE); inputsClosed[i] = boolFactory.getSBools(BLOCK_SIZE); outClosed[i] = boolFactory.getSBools(BLOCK_SIZE); this.result[i] = boolFactory.getOBools(BLOCK_SIZE); } // Handle input lists for (int i = 0; i < this.inSet.length; i++) { if (this.id != 1 && this.id != 2) { continue; } int offset = this.inSet.length; BitSet bits = ByteArithmetic.intToBitSet(this.inSet[i]); for (int j = 0; j < BLOCK_SIZE; j++) { if (this.id == 1) { inputsOpen[i][j].setValue(bits.get(BLOCK_SIZE - 1 - j)); } else { inputsOpen[i + offset][j].setValue(bits.get(BLOCK_SIZE - 1 - j)); } } } // Build protocols where the inputs are closed. ProtocolProducer[][] closeInputBits = new ProtocolProducer[this.inSet.length * 2][BLOCK_SIZE]; for (int j = 0; j < this.inSet.length; j++) { for (int i = 0; i < BLOCK_SIZE; i++) { closeInputBits[j][i] = boolFactory.getCloseProtocol(1, inputsOpen[j][i], inputsClosed[j][i]); closeInputBits[j + this.inSet.length][i] = boolFactory.getCloseProtocol(2, inputsOpen[j + this.inSet.length][i], inputsClosed[j + this.inSet.length][i]); } } //Build the 2*list AES protocols and put the closing protocols into a single producer ProtocolProducer[] tmp = new ProtocolProducer[this.inSet.length * 2]; //Each protocolproducer closes an input bit string ProtocolProducer[] aesProtocols = new ProtocolProducer[this.inSet.length * 2]; for (int i = 0; i < this.inSet.length * 2; i++) { tmp[i] = new ParallelProtocolProducer(closeInputBits[i]); aesProtocols[i] = new BristolCryptoFactory(boolFactory).getAesProtocol(inputsClosed[i], combinedKey, outClosed[i]); } ProtocolProducer closeInputs = new ParallelProtocolProducer(tmp); ProtocolProducer compute = new ParallelProtocolProducer(aesProtocols); // Construct protocol for opening up the result. Protocol[][] opens = new Protocol[this.inSet.length * 2][BLOCK_SIZE]; ProtocolProducer[] openInputs = new ProtocolProducer[this.inSet.length * 2]; for (int j = 0; j < this.inSet.length * 2; j++) { for (int i = 0; i < BLOCK_SIZE; i++) { opens[j][i] = boolFactory.getOpenProtocol(outClosed[j][i], result[j][i]); } openInputs[j] = new ParallelProtocolProducer(opens[j]); } ProtocolProducer openCipher = new ParallelProtocolProducer(openInputs); // First we close key and plaintext, then we do the AES, then we open the resulting ciphertexts. ProtocolProducer finalProtocol = new SequentialProtocolProducer(closeKeys, combineKeys, closeInputs, compute, openCipher); return finalProtocol; }