下面列出了java.util.BitSet#clone ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Checks if clade i contains clade j.
*
* @param i - the parent clade
* @param j - the child clade
* @return true, if i contains j
*/
protected boolean containsClade(BitSet i, BitSet j) {
BitSet tmpI = (BitSet) i.clone();
// just set the bits which are either in j but not in i or in i but not
// in j
tmpI.xor(j);
int numberOfBitsInEither = tmpI.cardinality();
// which bits are just in i
tmpI.and(i);
int numberIfBitJustInContaining = tmpI.cardinality();
// if the number of bits just in i is equal to the number of bits just
// in one of i or j
// then i contains j
return numberOfBitsInEither == numberIfBitJustInContaining;
}
/**
* remove OzoneAcl from existing list of OzoneAcls.
* @param existingAcls
* @param acl
* @return true if current OzoneAcls are changed, false otherwise.
*/
public static boolean removeAcl(List<OzoneAcl> existingAcls, OzoneAcl acl) {
if (existingAcls == null || existingAcls.isEmpty() || acl == null) {
return false;
}
for (OzoneAcl a: existingAcls) {
if (a.getName().equals(acl.getName()) &&
a.getType().equals(acl.getType()) &&
a.getAclScope().equals(acl.getAclScope())) {
BitSet current = a.getAclBitSet();
BitSet original = (BitSet) current.clone();
current.andNot(acl.getAclBitSet());
if (current.equals(original)) {
return false;
}
if (current.isEmpty()) {
existingAcls.remove(a);
}
return true;
}
}
return false;
}
/**
* Test if set sourceBitSet is contained in set targetBitSet.
*
* @param sourceBitSet a bitSet
* @param targetBitSet a bitSet
* @return true if sourceBitSet is contained in targetBitSet
*/
private boolean isContainedIn(BitSet sourceBitSet, BitSet targetBitSet) {
boolean result = false;
if (sourceBitSet.isEmpty()) {
return true;
}
BitSet setA = (BitSet) sourceBitSet.clone();
setA.and(targetBitSet);
if (setA.equals(sourceBitSet)) {
result = true;
}
return result;
}
/**
* Checks whether all subsets of X (with length of X - 1) are part of the last level.
* Only if this check return true X is added to the new level.
*
* @param X
* @return
*/
private boolean checkSubsets(BitSet X) {
boolean xIsValid = true;
// clone of X for usage in the following loop
BitSet Xclone = (BitSet) X.clone();
for (int l = X.nextSetBit(0); l >= 0; l = X.nextSetBit(l + 1)) {
Xclone.clear(l);
if (!level0.containsKey(Xclone)) {
xIsValid = false;
break;
}
Xclone.set(l);
}
return xIsValid;
}
private boolean checkJoinCondition(BitSet p, BitSet q) {
if (p.length() >= q.length()) {
return false;
}
BitSet intersection = (BitSet) p.clone();
intersection.and(q);
return p.cardinality() == intersection.cardinality() &&
q.cardinality() == intersection.cardinality();
}
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);
}
}
}
}
/** Returns a BitSet that is the union of the given BitSets. Does not modify
* any of the inputs. */
public static BitSet union(BitSet set0, BitSet... sets) {
final BitSet s = (BitSet) set0.clone();
for (BitSet set : sets) {
s.or(set);
}
return s;
}
public void updatePositiveCover(FDList nonFds) {
/* if (nonFds.isEmpty())
return;
// Sort the negative cover
Logger.getInstance().writeln("Sorting FD-violations ...");
Collections.sort(nonFds, new Comparator<BitSet>() {
@Override
public int compare(BitSet o1, BitSet o2) {
return (int)(o1.cardinality() - o2.cardinality());
}
});
*/ // THE SORTING IS NOT NEEDED AS THE UCCSet SORTS THE NONUCCS BY LEVEL ALREADY
de.metanome.algorithms.cfdfinder.utils.Logger.getInstance().writeln("Inducing FD candidates ...");
for (int i = nonFds.getFdLevels().size() - 1; i >= 0; i--) {
if (i >= nonFds.getFdLevels().size()) // If this level has been trimmed during iteration
continue;
List<BitSet> nonFdLevel = nonFds.getFdLevels().get(i);
for (BitSet lhs : nonFdLevel) {
BitSet fullRhs = (BitSet) lhs.clone();
fullRhs.flip(0, this.posCover.getNumAttributes());
for (int rhs = fullRhs.nextSetBit(0); rhs >= 0; rhs = fullRhs.nextSetBit(rhs + 1))
this.specializePositiveCover(lhs, rhs, nonFds);
}
nonFdLevel.clear();
}
}
private BitSet extendWith(BitSet ucc, int extensionAttr) {
if (ucc.get(extensionAttr))
return null;
BitSet childUCC = (BitSet) ucc.clone();
childUCC.set(extensionAttr);
if (this.posCover.containsUCCOrGeneralization(childUCC))
return null;
// if (this.negCover.containsUCCOrSpecialization(childUCC)) // TODO: May be needed?
// return null;
return childUCC;
}
public static List<BitSet> generateLhsSubsets(BitSet lhs) {
List<BitSet> results = new LinkedList<>();
for (int i = lhs.nextSetBit(0); i >= 0; i = lhs.nextSetBit(i + 1)) {
BitSet subset = (BitSet) lhs.clone();
subset.flip(i);
if (subset.cardinality() > 0) {
results.add(subset);
}
}
return results;
}
/** Removes specified amount of bits from the set.
*/
private static BitSet removeAt (BitSet b, int at, int len, int newSize) {
BitSet clone = (BitSet)b.clone ();
int max = b.length ();
while (at < max) {
clone.set (at, b.get (at + len));
at++;
}
clone.set (newSize, b.size (), false);
return clone;
}
/**
* Returns {@code true} if this set contains all the elements of the given collection.
*
* @param c the collection to be checked for containment in this set.
* @return {@code true} if this set contains all elements of the given collection.
*/
@Override
public boolean containsAll(final Collection<?> c) {
if (c instanceof CodeListSet) {
final CodeListSet<?> o = (CodeListSet<?>) c;
if (elementType == o.elementType) {
if (values == (values | o.values)) {
/*
* Code below this point checks for the rare cases
* where there is more than 64 code list elements.
*/
final BitSet s = supplementary;
final BitSet os = o.supplementary;
if (( s == null || s.isEmpty()) &&
(os == null || os.isEmpty()))
{
return true;
}
if (s != null && os != null) {
final BitSet tmp = (BitSet) os.clone();
tmp.andNot(s);
return tmp.isEmpty();
}
}
}
return false;
}
return super.containsAll(c);
}
private static boolean subset(BitSet sub, BitSet sup) {
BitSet subcopy = (BitSet) sub.clone();
subcopy.andNot(sup);
return subcopy.isEmpty();
}
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 fd to FDTree. Filter the tree at the end of the algorithm.
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);
}
}
}
private static boolean subset(BitSet sub, BitSet sup) {
BitSet subcopy = (BitSet) sub.clone();
subcopy.andNot(sup);
return subcopy.isEmpty();
}
private static boolean subset(BitSet sub, BitSet sup) {
BitSet subcopy = (BitSet) sub.clone();
subcopy.andNot(sup);
return subcopy.isEmpty();
}
private static boolean subset(BitSet sub, BitSet sup) {
BitSet subcopy = (BitSet) sub.clone();
subcopy.andNot(sup);
return subcopy.isEmpty();
}
public static int andNotCount(BitSet base, BitSet not) {
BitSet andNotSet = (BitSet) base.clone();
andNotSet.andNot(not);
return andNotSet.cardinality();
}
public Subset(BitSet subset, double merit){
this.subset =(BitSet)subset.clone ();
this.merit =merit;
}
/**
* Get prefix of BitSet by copying it and removing the last Bit.
*
* @param bitset
* @return
*/
private BitSet getPrefix(BitSet bitset) {
BitSet prefix = (BitSet) bitset.clone();
prefix.clear(getLastSetBitIndex(prefix));
return prefix;
}