Java Code Examples for java.util.BitSet#andNot()

The following examples show how to use java.util.BitSet#andNot() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BlockProcessor.java    From jadx with Apache License 2.0 6 votes vote down vote up
private static void calcImmediateDominators(List<BlockNode> basicBlocks, BlockNode entryBlock) {
	for (BlockNode block : basicBlocks) {
		if (block == entryBlock) {
			continue;
		}
		BlockNode idom;
		List<BlockNode> preds = block.getPredecessors();
		if (preds.size() == 1) {
			idom = preds.get(0);
		} else {
			BitSet bs = new BitSet(block.getDoms().length());
			bs.or(block.getDoms());
			for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1)) {
				BlockNode dom = basicBlocks.get(i);
				bs.andNot(dom.getDoms());
			}
			if (bs.cardinality() != 1) {
				throw new JadxRuntimeException("Can't find immediate dominator for block " + block
						+ " in " + bs + " preds:" + preds);
			}
			idom = basicBlocks.get(bs.nextSetBit(0));
		}
		block.setIDom(idom);
		idom.addDominatesOn(block);
	}
}
 
Example 2
Source File: RegionVersionHolderJUnitTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Return true if bs1 dominates bs2 - meaning that at least all of the bits
 * set in bs2 are set in bs1.
 * @param bs1
 * @param bs2
 * @return
 */
private boolean dominates(BitSet bs1, BitSet bs2) {
  //bs1 dominates bs2 if it has set at least all of the bits in bs1.
  BitSet copy = new BitSet();
  //Make copy a copy of bit set 2
  copy.or(bs2);
  
  //Clear all of the bits in copy that are set in bit set 1
  copy.andNot(bs1);
  
  //If all of the bits have been cleared in copy, that means
  //bit set 1 had at least all of the bits set that were set in
  //bs2
  return copy.isEmpty();
  
}
 
Example 3
Source File: DagAwareYarnTaskScheduler.java    From tez with Apache License 2.0 6 votes vote down vote up
/**
 * Removes a vertex from a RequestPriorityStats.
 *
 * Removing a vertex is more expensive than adding a vertex. The stats contain bitmasks which only store on/off
 * values rather than reference counts. Therefore we must rebuild the descendants bitmasks from the remaining
 * vertices in the request stats. Once the new descendants mask is computed we then need to rebuild the
 * allowedVertices BitSet for all lower priority request stats in case the removal of this vertex unblocks lower
 * priority requests of a descendant vertex.
 *
 * Rebuilding allowedVertices for the lower priorities involves starting with the allowedVertices mask at the
 * current priority then masking off the descendants at each priority level encountered, accumulating the results.
 * Any descendants of a level will be blocked at all lower levels. See the addVertexToRequestStats documentation
 * for details on how vertices map to the descendants and allowedVertices bit masks.
 */
private void removeVertexFromRequestStats(Priority priority, RequestPriorityStats stats, Integer vertexIndexInt) {
  stats.vertexTaskCount.remove(vertexIndexInt);
  int vertexIndex = vertexIndexInt;
  stats.vertices.clear(vertexIndex);

  // Rebuild the descendants BitSet for the remaining vertices at this priority.
  stats.descendants.clear();
  for (Integer vIndex : stats.vertexTaskCount.keySet()) {
    stats.descendants.or(vertexDescendants.get(vIndex));
  }

  // The allowedVertices for all lower priorities need to be recalculated where the vertex descendants at each
  // level are removed from the list of allowed vertices at all subsequent levels.
  Collection<RequestPriorityStats> tailStats = priorityStats.tailMap(priority, false).values();
  if (!tailStats.isEmpty()) {
    BitSet cumulativeAllowed = new BitSet(vertexDescendants.size());
    cumulativeAllowed.or(stats.allowedVertices);
    cumulativeAllowed.andNot(stats.descendants);
    for (RequestPriorityStats s : tailStats) {
      s.allowedVertices.clear();
      s.allowedVertices.or(cumulativeAllowed);
      cumulativeAllowed.andNot(s.descendants);
    }
  }
}
 
Example 4
Source File: Schema.java    From metanome-algorithms with Apache License 2.0 5 votes vote down vote up
@Override
public FunctionalDependency call() throws Exception {
	// Ignore the FD Emptyset --> X, because then everything defines X and this cannot be used for normalization
	if (this.fd.getLhs().cardinality() == 0)
		return null;
	// Violating FDs are all those FDs whose lhs is not a (super-)key of ANY key!
	if (this.keyTree.containsLhsOrSubset(this.fd.getLhs()))
		return null;
	// Ignore FDs that would otherwise split existing foreign keys; splitting a foreign key is possible in theory but defining the foreign key attributes across tables is technically difficult, so we avoid this
	// -> if the rhs+lhs contain the full foreign key, that's fine
	// -> if R\rhs contains the full foreign key, that's also fine
	// -> everything else: not fine
	BitSet generatingSchema = (BitSet) this.fd.getLhs().clone();
	generatingSchema.or(this.fd.getRhs());
	BitSet remainingSchema = (BitSet) this.attributes.clone();
	remainingSchema.andNot(this.fd.getRhs());
	for (Schema referencedSchema : this.referencedSchemata)
		if ((Utils.andNotCount(referencedSchema.getPrimaryKey().getLhs(), generatingSchema) != 0) && (Utils.andNotCount(referencedSchema.getPrimaryKey().getLhs(), remainingSchema) != 0))
			return null;
	
	FunctionalDependency violatingFd = this.fd.clone();
	// Remove the key attributes from the violating FD's rhs, because splitting the schema's key would require finding a new key and this would confuse the user who just defined this key; because the violating FD cannot be a key itself (per definition), the rhs will not be empty afterwards
	// -> if the rhs overlaps with the key, remove that overlap; rhs cannot be empty then, because if it would reference all key attributes, it would have been a key itself and, hence, no violating fd
	// -> if the lhs overlaps with the key, that is ok, because the attributes will stay in that relation and can serve as foreign-key as well
	if (this.primaryKey != null)
		violatingFd.getRhs().andNot(this.primaryKey.getLhs());
	
	return (violatingFd.getRhs().cardinality() > 0) ? violatingFd : null;
}
 
Example 5
Source File: Assignments.java    From bt with Apache License 2.0 5 votes vote down vote up
private boolean hasInterestingPieces(ConnectionKey connectionKey) {
    Optional<Bitfield> peerBitfieldOptional = pieceStatistics.getPeerBitfield(connectionKey);
    if (!peerBitfieldOptional.isPresent()) {
        return false;
    }
    BitSet peerBitfield = peerBitfieldOptional.get().getBitmask();
    BitSet localBitfield = bitfield.getBitmask();
    peerBitfield.andNot(localBitfield);
    return peerBitfield.cardinality() > 0;
}
 
Example 6
Source File: FDTreeElement.java    From metanome-algorithms with Apache License 2.0 5 votes vote down vote up
public void grow(BitSet lhs, FDTree fdTree) {
	// Add specializations of all nodes an mark them as isFD, but if specialization exists, then it is invalid and should not be marked; only add specializations of nodes not marked as isFD!
	BitSet rhs = this.rhsAttributes;
	
	BitSet invalidRhs = (BitSet) rhs.clone();
	invalidRhs.andNot(this.rhsFds);
	
	// Add specializations that are not invalid
	if (invalidRhs.cardinality() > 0) {
		for (int extensionAttr = 0; extensionAttr < this.numAttributes; extensionAttr++) {
			if (lhs.get(extensionAttr) || rhs.get(extensionAttr))
				continue;
			
			lhs.set(extensionAttr);
			fdTree.addFunctionalDependencyIfNotInvalid(lhs, invalidRhs);
			lhs.clear(extensionAttr);
		}
	}
	
	// Traverse children and let them add their specializations
	if (this.children != null) {
		for (int childAttr = 0; childAttr < this.numAttributes; childAttr++) {
			FDTreeElement element = this.children[childAttr];
			if (element != null) {
				lhs.set(childAttr);
				element.grow(lhs, fdTree);
				lhs.clear(childAttr);
			}
		}
	}
}
 
Example 7
Source File: BitSetTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void test_differentSizes() {
    BitSet result = big();
    result.and(small());
    assertEquals("{}", result.toString());
    result = small();
    result.and(big());
    assertEquals("{}", result.toString());

    result = big();
    result.andNot(small());
    assertEquals("{1000}", result.toString());
    result = small();
    result.andNot(big());
    assertEquals("{10}", result.toString());

    assertFalse(big().intersects(small()));
    assertFalse(small().intersects(big()));

    result = big();
    result.or(small());
    assertEquals("{10, 1000}", result.toString());
    result = small();
    result.or(big());
    assertEquals("{10, 1000}", result.toString());

    result = big();
    result.xor(small());
    assertEquals("{10, 1000}", result.toString());
    result = small();
    result.xor(big());
    assertEquals("{10, 1000}", result.toString());
}
 
Example 8
Source File: ArrayPackedSet.java    From JAADAS with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean isSubSet(FlowSet<T> other) {
	if (other == this)
		return true;
	if (sameType(other)) {
          ArrayPackedSet<T> o = (ArrayPackedSet<T>) other;
          
          BitSet tmp = (BitSet) o.bits.clone();
          tmp.andNot(bits);
          return tmp.isEmpty();
	}
	return super.isSubSet(other);
}
 
Example 9
Source File: NaiveFdExtender.java    From metanome-algorithms with Apache License 2.0 5 votes vote down vote up
@Override
public void executeAlgorithm(Map<BitSet, BitSet> fds) {
	System.out.println("Building the FDs' closures ...");
	for (Entry<BitSet, BitSet> entry : fds.entrySet()) {
		BitSet lhs = entry.getKey();
		BitSet rhs = entry.getValue();
		
		// Add the trivial fds to the current fd's rhs for the closure construction (we need to check against ALL the attributes in this fd to build its closure)
		rhs.or(lhs);
		
		// Extend rhs
		long rhsCardinality;
		do {
			rhsCardinality = rhs.cardinality();
			
			for (Entry<BitSet, BitSet> other : fds.entrySet())
				if (Utils.andNotCount(other.getKey(), rhs) == 0)
					rhs.or(other.getValue());
			
			// TODO: when we think of parallelizing the closure calculation
		//	closureFds.entrySet().stream()
		//		.filter(other -> {return Utils.andNotCount(other.getKey(), rhs) == 0;})
		//		.forEach(other -> rhs.and(other.getValue()));
		}
		while (rhsCardinality != rhs.cardinality());
		
		// Remove the trivial fds
		rhs.andNot(lhs);
	}
}
 
Example 10
Source File: Operations.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Returns true if there are dead states reachable from an initial state. */
public static boolean hasDeadStatesFromInitial(Automaton a) {
  BitSet reachableFromInitial = getLiveStatesFromInitial(a);
  BitSet reachableFromAccept = getLiveStatesToAccept(a);
  reachableFromInitial.andNot(reachableFromAccept);
  return reachableFromInitial.isEmpty() == false;
}
 
Example 11
Source File: DefaultMXBeanMappingFactory.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
private static boolean subset(BitSet sub, BitSet sup) {
    BitSet subcopy = (BitSet) sub.clone();
    subcopy.andNot(sup);
    return subcopy.isEmpty();
}
 
Example 12
Source File: Validator.java    From metanome-algorithms with Apache License 2.0 4 votes vote down vote up
public ValidationResult call() throws Exception {
	ValidationResult result = new ValidationResult();
	
	FDTreeElement element = this.elementLhsPair.getElement();
	BitSet lhs = this.elementLhsPair.getLhs();
	BitSet rhs = element.getFds();
	
	int rhsSize = rhs.cardinality();
	if (rhsSize == 0)
		return result;
	result.validations = result.validations + rhsSize;
	
	if (Validator.this.level == 0) {
		// Check if rhs is unique
		for (int rhsAttr = rhs.nextSetBit(0); rhsAttr >= 0; rhsAttr = rhs.nextSetBit(rhsAttr + 1)) {
			if (!Validator.this.plis.get(rhsAttr).isConstant(Validator.this.numRecords)) {
				element.removeFd(rhsAttr);
				result.invalidFDs.add(new FD(lhs, rhsAttr));
			}
			result.intersections++;
		}
	}
	else if (Validator.this.level == 1) {
		// Check if lhs from plis refines rhs
		int lhsAttribute = lhs.nextSetBit(0);
		for (int rhsAttr = rhs.nextSetBit(0); rhsAttr >= 0; rhsAttr = rhs.nextSetBit(rhsAttr + 1)) {
			if (!Validator.this.plis.get(lhsAttribute).refines(Validator.this.compressedRecords, rhsAttr)) {
				element.removeFd(rhsAttr);
				result.invalidFDs.add(new FD(lhs, rhsAttr));
			}
			result.intersections++;
		}
	}
	else {
		// Check if lhs from plis plus remaining inverted plis refines rhs
		int firstLhsAttr = lhs.nextSetBit(0);
		
		lhs.clear(firstLhsAttr);
		BitSet validRhs = Validator.this.plis.get(firstLhsAttr).refines(Validator.this.compressedRecords, lhs, rhs, result.comparisonSuggestions);
		lhs.set(firstLhsAttr);
		
		result.intersections++;
		
		rhs.andNot(validRhs); // Now contains all invalid FDs
		element.setFds(validRhs); // Sets the valid FDs in the FD tree
		
		for (int rhsAttr = rhs.nextSetBit(0); rhsAttr >= 0; rhsAttr = rhs.nextSetBit(rhsAttr + 1))
			result.invalidFDs.add(new FD(lhs, rhsAttr));
	}
	return result;
}
 
Example 13
Source File: MRF.java    From tuffylite with Apache License 2.0 4 votes vote down vote up
public Integer[] getClauseTallies(BitSet world){

		Integer[] rs = new Integer[clauses.size()];

		for(int ct=0;ct<bitmaps_weight.length;ct++){

			rs[ct] = 0;

			double weight = bitmaps_weight[ct];

			BitSet signature = bitmaps_signature[ct];
			BitSet mask = bitmaps_mask[ct];

			// clause: c1 v !c2 v c3   (there are other atoms c4 and c5)
			//		=> 10100 as signature, 11100 as mask
			// given a world T
			// sat = \exists 1 in [ (T ^ 10100) v !(T v 10100) ] ^ 11100

			BitSet TandSIG = (BitSet) world.clone();
			TandSIG.and(signature);

			BitSet notTorSIG = (BitSet) world.clone();
			notTorSIG.or(signature);

			TandSIG.and(mask);
			BitSet tocheck = (BitSet) mask.clone();

			tocheck.andNot(notTorSIG);
			tocheck.or(TandSIG);

			// TODO: change to faster implementation of tally
			if(tocheck.isEmpty()){
				// tocheck == 000000... <--- false
				if(weight > 0){
					rs[ct] ++;
				}

			}else{
				// tocheck != 000000... <--- true
				if(weight < 0){
					rs[ct] ++;
				}
			}
		}

		return rs;
	}
 
Example 14
Source File: DefaultMXBeanMappingFactory.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static boolean subset(BitSet sub, BitSet sup) {
    BitSet subcopy = (BitSet) sub.clone();
    subcopy.andNot(sup);
    return subcopy.isEmpty();
}
 
Example 15
Source File: DefaultMXBeanMappingFactory.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
private static boolean subset(BitSet sub, BitSet sup) {
    BitSet subcopy = (BitSet) sub.clone();
    subcopy.andNot(sup);
    return subcopy.isEmpty();
}
 
Example 16
Source File: TaneAlgorithm.java    From metanome-algorithms with Apache License 2.0 4 votes vote down vote up
/**
 * Computes the dependencies for the current level (level1).
 *
 * @throws AlgorithmExecutionException
 */
private void computeDependencies() throws AlgorithmExecutionException {
    initializeCplusForLevel();

    // iterate through the combinations of the level
    for (BitSet X : level1.keySet()) {
        if (level1.get(X).isValid()) {
            // Build the intersection between X and C_plus(X)
            BitSet C_plus = level1.get(X).getRhsCandidates();
            BitSet intersection = (BitSet) X.clone();
            intersection.and(C_plus);

            // clone of X for usage in the following loop
            BitSet Xclone = (BitSet) X.clone();

            // iterate through all elements (A) of the intersection
            for (int A = intersection.nextSetBit(0); A >= 0; A = intersection.nextSetBit(A + 1)) {
                Xclone.clear(A);

                // check if X\A -> A is valid
                StrippedPartition spXwithoutA = level0.get(Xclone).getPartition();
                StrippedPartition spX = level1.get(X).getPartition();

                if (spX.getError() == spXwithoutA.getError()) {
                    // found Dependency
                    BitSet XwithoutA = (BitSet) Xclone.clone();
                    processFunctionalDependency(XwithoutA, A);

                    // remove A from C_plus(X)
                    level1.get(X).getRhsCandidates().clear(A);

                    // remove all B in R\X from C_plus(X)
                    BitSet RwithoutX = new BitSet();
                    // set to R
                    RwithoutX.set(1, numberAttributes + 1);
                    // remove X
                    RwithoutX.andNot(X);

                    for (int i = RwithoutX.nextSetBit(0); i >= 0; i = RwithoutX.nextSetBit(i + 1)) {
                        level1.get(X).getRhsCandidates().clear(i);
                    }

                }
                Xclone.set(A);
            }
        }
    }
}
 
Example 17
Source File: TaneAlgorithmFilterTreeDirect.java    From metanome-algorithms with Apache License 2.0 4 votes vote down vote up
private void computeDependencies(int l) throws AlgorithmExecutionException {
    initializeCplusForLevel();

    // iterate through the combinations of the level
    for (BitSet X : level1.keySet()) {
        // Build the intersection between X and C_plus(X)
        BitSet C_plus = level1.get(X).getRhsCandidates();
        BitSet intersection = (BitSet) X.clone();
        intersection.and(C_plus);

        // clone of X for usage in the following loop
        BitSet Xclone = (BitSet) X.clone();

        // iterate through all elements (A) of the intersection
        for (int A = intersection.nextSetBit(0); A >= 0; A = intersection.nextSetBit(A + 1)) {
            Xclone.clear(A);

            // check if X\A -> A is valid
            StrippedPartition spXwithoutA = level0.get(Xclone).getPartition();
            StrippedPartition spX = level1.get(X).getPartition();

            if (spX.getError() == spXwithoutA.getError()) {
                // Add dependency to the result receiver as well as to the FDTree (dependencies),
                // which is used to check, whether a more general dependency already exists.
                BitSet XwithoutA = (BitSet) Xclone.clone();
                addDependencyToResultReceiver(XwithoutA, A);
                dependencies.addFunctionalDependency(Xclone, A);


                // remove A from C_plus(X)
                level1.get(X).getRhsCandidates().clear(A);

                // remove all B in R\X from C_plus(X)
                BitSet RwithoutX = new BitSet();
                // set to R
                RwithoutX.set(1, numberAttributes + 1);
                // remove X
                RwithoutX.andNot(X);

                for (int i = RwithoutX.nextSetBit(0); i >= 0; i = RwithoutX.nextSetBit(i + 1)) {
                    level1.get(X).getRhsCandidates().clear(i);
                }

            }
            Xclone.set(A);
        }
    }
}
 
Example 18
Source File: DefaultMXBeanMappingFactory.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
private static boolean subset(BitSet sub, BitSet sup) {
    BitSet subcopy = (BitSet) sub.clone();
    subcopy.andNot(sup);
    return subcopy.isEmpty();
}
 
Example 19
Source File: HotplugDetectionAction.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private static BitSet complement(BitSet first, BitSet second) {
    // Need to clone it so that it doesn't touch original set.
    BitSet clone = (BitSet) first.clone();
    clone.andNot(second);
    return clone;
}
 
Example 20
Source File: MRF.java    From tuffylite with Apache License 2.0 4 votes vote down vote up
public Integer[] getClauseSat(BitSet world){

		Integer[] rs = new Integer[clauses.size()];

		for(int ct=0;ct<bitmaps_weight.length;ct++){

			rs[ct] = 0;

			double weight = bitmaps_weight[ct];

			BitSet signature = bitmaps_signature[ct];
			BitSet mask = bitmaps_mask[ct];

			// clause: c1 v !c2 v c3   (there are other atoms c4 and c5)
			//		=> 10100 as signature, 11100 as mask
			// given a world T
			// sat = \exists 1 in [ (T ^ 10100) v !(T v 10100) ] ^ 11100

			BitSet TandSIG = (BitSet) world.clone();
			TandSIG.and(signature);

			BitSet notTorSIG = (BitSet) world.clone();
			notTorSIG.or(signature);

			TandSIG.and(mask);
			BitSet tocheck = (BitSet) mask.clone();

			tocheck.andNot(notTorSIG);
			tocheck.or(TandSIG);

			// TODO: change to faster implementation of tally
			if(tocheck.isEmpty()){
				// tocheck == 000000... <--- false
				if(weight < 0){
					rs[ct] ++;
				}

			}else{
				// tocheck != 000000... <--- true
				if(weight > 0){
					rs[ct] ++;
				}
			}
		}

		return rs;
	}