java.util.BitSet.cardinality()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(9.7k)|赞(0)|评价(0)|浏览(174)

本文整理了Java中java.util.BitSet.cardinality()方法的一些代码示例,展示了BitSet.cardinality()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BitSet.cardinality()方法的具体详情如下:
包路径:java.util.BitSet
类名称:BitSet
方法名:cardinality

BitSet.cardinality介绍

[英]Returns the number of bits that are true in this BitSet.
[中]返回此位集中为真的位数。

代码示例

代码示例来源:origin: skylot/jadx

  1. public static List<BlockNode> bitSetToBlocks(MethodNode mth, BitSet bs) {
  2. int size = bs.cardinality();
  3. if (size == 0) {
  4. return Collections.emptyList();
  5. }
  6. List<BlockNode> blocks = new ArrayList<>(size);
  7. for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1)) {
  8. BlockNode block = mth.getBasicBlocks().get(i);
  9. blocks.add(block);
  10. }
  11. return blocks;
  12. }

代码示例来源:origin: mpetazzoni/ttorrent

  1. public PieceStorageImpl(TorrentByteStorage fileCollectionStorage,
  2. BitSet availablePieces,
  3. int piecesCount,
  4. int pieceSize) {
  5. this.fileCollectionStorage = fileCollectionStorage;
  6. this.readWriteLock = new ReentrantReadWriteLock();
  7. this.piecesCount = piecesCount;
  8. this.pieceSize = pieceSize;
  9. BitSet bitSet = new BitSet(piecesCount);
  10. bitSet.or(availablePieces);
  11. if (bitSet.cardinality() != piecesCount) {
  12. this.availablePieces = bitSet;
  13. }
  14. isOpen = false;
  15. }

代码示例来源:origin: google/guava

  1. static CharMatcher from(BitSet chars, String description) {
  2. // Compute the filter.
  3. long filter = 0;
  4. int size = chars.cardinality();
  5. boolean containsZero = chars.get(0);
  6. // Compute the hash table.
  7. char[] table = new char[chooseTableSize(size)];
  8. int mask = table.length - 1;
  9. for (int c = chars.nextSetBit(0); c != -1; c = chars.nextSetBit(c + 1)) {
  10. // Compute the filter at the same time.
  11. filter |= 1L << c;
  12. int index = smear(c) & mask;
  13. while (true) {
  14. // Check for empty.
  15. if (table[index] == 0) {
  16. table[index] = (char) c;
  17. break;
  18. }
  19. // Linear probing.
  20. index = (index + 1) & mask;
  21. }
  22. }
  23. return new SmallCharMatcher(table, filter, containsZero, description);
  24. }

代码示例来源:origin: apache/ignite

  1. for (int j = 0; j < selectedNodesCnt; j++)
  2. if ((mask & (1 << j)) != 0) {
  3. boolean connected = connections[indexes[i]].get(indexes[j]);
  4. BitSet resSet = new BitSet(selectedNodesCnt);
  5. int idx = indexes[i];
  6. assert selectedSet.get(idx)
  7. : "Result contains node which is not presented in income set [nodeIdx" + idx + ", set=" + selectedSet + "]";
  8. resSet.set(idx);
  9. assert resSet.cardinality() > 0
  10. : "No nodes selected as fully connected component [set=" + selectedSet + "]";

代码示例来源:origin: stackoverflow.com

  1. BitSet temp2 = new BitSet();
  2. temp2.set(j);
  3. bitMap.put(temp1, temp2);
  4. } else {
  5. bitMap.get(temp1).set(j);
  6. BitSet resultGetter = new BitSet(params[this.L]);
  7. resultGetter.set(0, params[this.L] + 1, true);
  8. Iterator<BitSet> itr = bitMap.keySet().iterator();
  9. BitSet temp3 = new BitSet();
  10. if (bitMap.get(temp3).cardinality() == params[this.N]) {
  11. if (temp3.cardinality() <= resultGetter.cardinality()) {
  12. resultGetter = temp3;
  13. if (resultGetter.cardinality() > params[this.L])
  14. retVal = -1;
  15. else
  16. retVal = resultGetter.cardinality();

代码示例来源:origin: apache/incubator-druid

  1. private static <T> List<T> filterByBitset(List<T> list, BitSet bitSet)
  2. {
  3. final ArrayList<T> outList = new ArrayList<>(bitSet.cardinality());
  4. for (int i = 0; i < list.size(); ++i) {
  5. if (bitSet.get(i)) {
  6. outList.add(list.get(i));
  7. }
  8. }
  9. return outList;
  10. }

代码示例来源:origin: apache/flink

  1. @Override
  2. public void invoke(Integer value) throws Exception {
  3. numElements++;
  4. if (duplicateChecker.get(value)) {
  5. throw new Exception("Received a duplicate: " + value);
  6. }
  7. duplicateChecker.set(value);
  8. if (numElements == numElementsTotal) {
  9. // validate
  10. if (duplicateChecker.cardinality() != numElementsTotal) {
  11. throw new Exception("Duplicate checker has wrong cardinality");
  12. }
  13. else if (duplicateChecker.nextClearBit(0) != numElementsTotal) {
  14. throw new Exception("Received sparse sequence");
  15. }
  16. else {
  17. throw new SuccessException();
  18. }
  19. }
  20. }

代码示例来源:origin: mpetazzoni/ttorrent

  1. /**
  2. * Piece download completion handler.
  3. * <p/>
  4. * <p>
  5. * If the complete piece downloaded is valid, we can record in the torrent
  6. * completedPieces bit field that we know have this piece.
  7. * </p>
  8. *
  9. * @param peer The peer we got this piece from.
  10. * @param piece The piece in question.
  11. */
  12. @Override
  13. public void handlePieceCompleted(SharingPeer peer,
  14. Piece piece) throws IOException {
  15. // Regardless of validity, record the number of bytes downloaded and
  16. // mark the piece as not requested anymore
  17. myTorrentStatistic.addDownloaded(piece.size());
  18. this.requestedPieces.set(piece.getIndex(), false);
  19. logger.trace("We now have {} piece(s) and {} outstanding request(s): {}",
  20. new Object[]{
  21. this.completedPieces.cardinality(),
  22. this.requestedPieces.cardinality(),
  23. this.requestedPieces
  24. });
  25. }

代码示例来源:origin: mpetazzoni/ttorrent

  1. BitSet availablePieces = peer.getAvailablePieces();
  2. for (int i = availablePieces.nextSetBit(0); i >= 0;
  3. i = availablePieces.nextSetBit(i + 1)) {
  4. this.pieces[i].noLongerAt(peer);
  5. if (requested != null) {
  6. for (Piece piece : requested) {
  7. this.requestedPieces.set(piece.getIndex(), false);
  8. new Object[]{
  9. peer,
  10. availablePieces.cardinality(),
  11. this.completedPieces.cardinality(),
  12. this.pieces.length
  13. });
  14. logger.trace("We now have {} piece(s) and {} outstanding request(s): {}",
  15. new Object[]{
  16. this.completedPieces.cardinality(),
  17. this.requestedPieces.cardinality(),
  18. this.requestedPieces
  19. });

代码示例来源:origin: skylot/jadx

  1. public static BlockNode getPathCross(MethodNode mth, BlockNode b1, BlockNode b2) {
  2. if (b1 == null || b2 == null) {
  3. return null;
  4. }
  5. BitSet b = new BitSet();
  6. b.or(b1.getDomFrontier());
  7. b.and(b2.getDomFrontier());
  8. b.clear(b1.getId());
  9. b.clear(b2.getId());
  10. if (b.cardinality() == 1) {
  11. BlockNode end = mth.getBasicBlocks().get(b.nextSetBit(0));
  12. if (isPathExists(b1, end) && isPathExists(b2, end)) {
  13. return end;
  14. }
  15. }
  16. if (isPathExists(b1, b2)) {
  17. return b2;
  18. }
  19. if (isPathExists(b2, b1)) {
  20. return b1;
  21. }
  22. return null;
  23. }

代码示例来源:origin: apache/ignite

  1. BitSet canUse = new BitSet(selectedNodesCnt);
  2. for (int i = 0; i < selectedNodesCnt; i++)
  3. canUse.set(i);
  4. if (bestRes != null && canUse.cardinality() <= bestRes.cardinality())
  5. break;
  6. BitSet currRes = new BitSet(selectedNodesCnt);
  7. currRes.set(pickedIdx);
  8. canUse.set(pickedIdx, false);
  9. if (bestRes == null || currRes.cardinality() > bestRes.cardinality())
  10. bestRes = currRes;
  11. if (!bestRes.get(nodeIdx) && joinNode(bestRes, nodeIdx, nodeIndexes))
  12. bestRes.set(nodeIdx);
  13. BitSet reindexedBestRes = new BitSet(totalNodesCnt);
  14. Iterator<Integer> it = new BitSetIterator(bestRes);
  15. while (it.hasNext())

代码示例来源:origin: apache/ignite

  1. /**
  2. *
  3. */
  4. @Test
  5. public void testFind() {
  6. BitSet[] matrix = provider.provide();
  7. int nodes = matrix.length;
  8. BitSet all = new BitSet(nodes);
  9. for (int i = 0; i < nodes; i++)
  10. all.set(i);
  11. FullyConnectedComponentSearcher searcher = new FullyConnectedComponentSearcher(matrix);
  12. BitSet res = searcher.findLargest(all);
  13. int size = res.cardinality();
  14. Assert.assertTrue("Actual = " + size + ", Expected = " + minAcceptableRes,
  15. size >= minAcceptableRes);
  16. }

代码示例来源:origin: linkedin/cruise-control

  1. /**
  2. * @return the number of windows with extrapolations.
  3. */
  4. public synchronized int numWindowsWithExtrapolation() {
  5. int currentIdx = arrayIndex(currentWindowIndex());
  6. int numExtrapolationAdjustment = _extrapolations.get(currentIdx) ? 1 : 0;
  7. return _extrapolations.cardinality() - numExtrapolationAdjustment;
  8. }

代码示例来源:origin: mpetazzoni/ttorrent

  1. /**
  2. * Mark a piece as completed, decrementing the piece size in bytes from our
  3. * left bytes to download counter.
  4. */
  5. public synchronized void markCompleted(Piece piece) {
  6. if (this.completedPieces.get(piece.getIndex())) {
  7. return;
  8. }
  9. // A completed piece means that's that much data left to download for
  10. // this torrent.
  11. myTorrentStatistic.addLeft(-piece.size());
  12. this.completedPieces.set(piece.getIndex());
  13. if (completedPieces.cardinality() == getPiecesCount()) {
  14. logger.info("all pieces are received for torrent {}. Validating...", this);
  15. }
  16. }

代码示例来源:origin: prestodb/presto

  1. static CharMatcher from(BitSet chars, String description) {
  2. // Compute the filter.
  3. long filter = 0;
  4. int size = chars.cardinality();
  5. boolean containsZero = chars.get(0);
  6. // Compute the hash table.
  7. char[] table = new char[chooseTableSize(size)];
  8. int mask = table.length - 1;
  9. for (int c = chars.nextSetBit(0); c != -1; c = chars.nextSetBit(c + 1)) {
  10. // Compute the filter at the same time.
  11. filter |= 1L << c;
  12. int index = smear(c) & mask;
  13. while (true) {
  14. // Check for empty.
  15. if (table[index] == 0) {
  16. table[index] = (char) c;
  17. break;
  18. }
  19. // Linear probing.
  20. index = (index + 1) & mask;
  21. }
  22. }
  23. return new SmallCharMatcher(table, filter, containsZero, description);
  24. }

代码示例来源:origin: mpetazzoni/ttorrent

  1. /**
  2. * Peer choked handler.
  3. * <p/>
  4. * <p>
  5. * When a peer chokes, the requests made to it are canceled and we need to
  6. * mark the eventually piece we requested from it as available again for
  7. * download tentative from another peer.
  8. * </p>
  9. *
  10. * @param peer The peer that choked.
  11. */
  12. @Override
  13. public synchronized void handlePeerChoked(SharingPeer peer) {
  14. Set<Piece> pieces = peer.getRequestedPieces();
  15. if (pieces.size() > 0) {
  16. for (Piece piece : pieces) {
  17. this.requestedPieces.set(piece.getIndex(), false);
  18. }
  19. }
  20. logger.trace("Peer {} choked, we now have {} outstanding " +
  21. "request(s): {}.",
  22. new Object[]{
  23. peer,
  24. this.requestedPieces.cardinality(),
  25. this.requestedPieces
  26. });
  27. }

代码示例来源:origin: google/guava

  1. final BitSet table = new BitSet();
  2. setBits(table);
  3. int totalCharacters = table.cardinality();
  4. if (totalCharacters * 2 <= DISTINCT_CHARS) {
  5. return precomputedPositive(totalCharacters, table, toString());

代码示例来源:origin: apache/kylin

  1. public ImmutableBitSet(BitSet set) {
  2. this.set = (BitSet) set.clone();
  3. this.arr = new int[set.cardinality()];
  4. int j = 0;
  5. for (int i = set.nextSetBit(0); i >= 0; i = set.nextSetBit(i + 1)) {
  6. arr[j++] = i;
  7. }
  8. }

代码示例来源:origin: skylot/jadx

  1. if (s.getIDom() != block) {
  2. if (domFrontier == null) {
  3. domFrontier = new BitSet(blocks.size());
  4. domFrontier.set(s.getId());
  5. for (int p = frontier.nextSetBit(0); p >= 0; p = frontier.nextSetBit(p + 1)) {
  6. if (blocks.get(p).getIDom() != block) {
  7. if (domFrontier == null) {
  8. domFrontier = new BitSet(blocks.size());
  9. domFrontier.set(p);
  10. if (domFrontier == null || domFrontier.cardinality() == 0) {
  11. domFrontier = EMPTY;

代码示例来源:origin: apache/hive

  1. BitSet bs = new BitSet(numBuckets);
  2. bs.clear();
  3. PrimitiveObjectInspector bucketOI = (PrimitiveObjectInspector)bucketField.getFieldObjectInspector();
  4. ObjectInspectorUtils.getBucketNumber(convCols, new ObjectInspector[]{constOI}, numBuckets) :
  5. ObjectInspectorUtils.getBucketNumberOld(convCols, new ObjectInspector[]{constOI}, numBuckets);
  6. bs.set(n);
  7. if (bucketingVersion == 1 && ctxt.isCompat()) {
  8. int h = ObjectInspectorUtils.getBucketHashCodeOld(convCols, new ObjectInspector[]{constOI});
  9. bs.set(n);
  10. if (bs.cardinality() < numBuckets) {

相关文章