下面列出了java.util.BitSet#size ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
*
* @param Molecule1
* @param Molecule2
* @return <B>Distance (Dissimilarity)<U> Soergel</U></B>
* <B>1-c/[a+b-c]=[a+b-2*c]/[a+b-c]</B>
* @throws Exception
*/
public static double getSoergelDistance(BitSet Molecule1, BitSet Molecule2) throws Exception {
A = (BitSet) Molecule1.clone();
B = (BitSet) Molecule2.clone();
float _bitset1_cardinality = A.cardinality();
float _bitset2_cardinality = B.cardinality();
if (A.size() != B.size()) {
throw new Exception("Bisets must have the same bit length");
}
BitSet one_and_two = (BitSet) A.clone();
one_and_two.and(B);
float _common_bit_count = one_and_two.cardinality();
double Val = (_bitset1_cardinality + _bitset2_cardinality - _common_bit_count) / _common_bit_count;
return Val;
}
/**
* 由bitset生成对应的bytebuf
* @param bitSet
* @param bitSetSize bitset实际大小
* @return bytebuf
*/
private ByteBuf bitSetToResult(BitSet bitSet, int bitSetSize) {
// 测试
// if (bitSet.size() != bitSetSize) {
// LOG.debug("bitSet.size() = " + bitSet.size());
// LOG.debug("bitSetSize = " + bitSetSize);
// }
// 计算ret所需大小
int retSize = (bitSet.size() / 8) + Integer.BYTES;
LOG.debug("retSize = " + retSize);
// 分配bytebuf
ByteBuf ret = PooledByteBufAllocator.DEFAULT.buffer(retSize);
// 先将bytebuf填充满
ret.writerIndex(retSize);
// 将bitset中的数据映射到bytebuf中
for (int i = 0; i < bitSet.size(); i++) {
int index = i / 8;
int offset = 7 - i % 8;
byte b = ret.getByte(index);
b |= (bitSet.get(i) ? 1 : 0) << offset;
ret.setByte(index, b);
}
// 将bitset大小信息添加到最后
ret.setInt((bitSet.size() / 8), bitSetSize);
return ret;
}
/**
*
* @param startValues fixed parameter values (if no parameters are fixed this is completely ignored)
* @param transforms Array of ParameterLimitsTransform (which can be the NullTransform which does NOT transform the parameter) which transform
* a constrained function parameter (e.g. must be between -1 and 1) to a unconstrained fit parameter.
* @param fixed BitSet with an element set to <b>true</b> if that parameter is fixed
*/
public UncoupledParameterTransforms(DoubleArray startValues, ParameterLimitsTransform[] transforms, BitSet fixed) {
ArgChecker.notNull(startValues, "null start values");
ArgChecker.notEmpty(transforms, "must specify transforms");
ArgChecker.notNull(fixed, "must specify what is fixed (even if none)");
_nMP = startValues.size();
ArgChecker.isTrue(_nMP == transforms.length, "Have {}-dimensional start value but {} transforms", _nMP, transforms.length);
_freeParameters = new boolean[_nMP];
for (int i = 0; i < _nMP; i++) {
if (i < fixed.size()) {
_freeParameters[i] = !fixed.get(i);
} else {
_freeParameters[i] = true;
}
}
int count = fixed.cardinality();
ArgChecker.isTrue(count < _nMP, "all parameters are fixed");
_nFP = _nMP - count;
_startValues = startValues;
_transforms = transforms;
}
/**
* 求素数 有无限个。一个大于1的自然数,如果除了1和它本身外,不能被其他自然数整除(除0以外)的数称之为素数(质数) 否则称为合数
*/
public static void computePrime() {
BitSet sieve = new BitSet(1024);
int size = sieve.size();
for (int i = 2; i < size; i++)
sieve.set(i);
int finalBit = (int) Math.sqrt(sieve.size());
for (int i = 2; i < finalBit; i++)
if (sieve.get(i))
for (int j = 2 * i; j < size; j += i)
sieve.clear(j);
int counter = 0;
for (int i = 1; i < size; i++) {
if (sieve.get(i)) {
System.out.printf("%5d", i);
if (++counter % 15 == 0)
System.out.println();
}
}
System.out.println();
}
/**
*
* @param Molecule1 BitSet
* @param Molecule2 BitSet
* <B>Note:</B> Can be Normalized to the range 1 to 0 if all attributes are
* normalized to this range and the result is divided in N
* @return <B>Distance (Dissimilarity) <U>Euclidean</U></B>
* <B>sqrt(a+b-2c)</B>
* <B>sqrt(OnA+OnB-2*c)</B>
* @throws java.lang.Exception
*/
public static double getEuclideanDistance(BitSet Molecule1, BitSet Molecule2) throws Exception {
A = (BitSet) Molecule1.clone();
B = (BitSet) Molecule2.clone();
float _bitset1_cardinality = A.cardinality();
float _bitset2_cardinality = B.cardinality();
if (A.size() != B.size()) {
throw new Exception("Bisets must have the same bit length");
}
BitSet one_and_two = (BitSet) A.clone();
one_and_two.and(B);
float _common_bit_count = one_and_two.cardinality();
double Val = sqrt((_bitset1_cardinality + _bitset2_cardinality - _common_bit_count) - _common_bit_count);
return Val;
}
protected static byte[] a(BitSet bitset)
{
byte abyte0[] = new byte[bitset.size() / 8];
int i = 0;
while (i < bitset.size())
{
int j = i / 8;
int k = 7 - i % 8;
byte byte0 = abyte0[j];
int l;
if (bitset.get(i))
{
l = 1;
} else
{
l = 0;
}
abyte0[j] = (byte)(byte0 | l << k);
i++;
}
return abyte0;
}
public int findStartIdx(BitSet blocksMap, int requiredBlocks) {
int startIdx = -1, blocks = 0, idx = 0;
for (idx = 0; idx < blocksMap.size(); ++idx) {
if (!blocksMap.get(idx)) {
blocks++;
} else {
blocks = 0;
}
if (blocks == requiredBlocks) {
break;
}
}
if (blocks == requiredBlocks) {
startIdx = idx - blocks + 1;
}
return startIdx;
}
public void reset(long[] ticks, long[] locations, long[] elevations, BitSet values, int size) throws IOException {
int n = ticks.length;
if (n != values.size() || (null != locations && n != locations.length) || (null != elevations && n != elevations.length)) {
throw new IOException("Incoherent array lengths");
}
this.ticks = ticks;
this.locations = locations;
this.elevations = elevations;
this.doubleValues = null;
this.longValues = null;
this.booleanValues = values;
this.stringValues = null;
this.sorted = false;
this.values = size;
this.sizehint = n;
this.type = TYPE.BOOLEAN;
}
/**
*
* @param Molecule1
* @param Molecule2
* @return <B>Similarity <U>Cosine,Ochiai,Carbo</U></B>
* <B>c/sqrt(a*b)</B>
* @throws Exception
*/
public static synchronized double getCosineSimilarity(BitSet Molecule1, BitSet Molecule2) throws Exception {
BitSet bitset1 = (BitSet) Molecule1.clone();
BitSet bitset2 = (BitSet) Molecule2.clone();
float _bitset1_cardinality = bitset1.cardinality();
float _bitset2_cardinality = bitset2.cardinality();
if (bitset1.size() != bitset2.size()) {
throw new Exception("Bisets must have the same bit length");
}
BitSet one_and_two = (BitSet) bitset1.clone();
one_and_two.and(bitset2);
float _common_bit_count = one_and_two.cardinality();
return _common_bit_count / (sqrt(_bitset1_cardinality * _bitset2_cardinality));
}
/**
*
* @param Molecule1 BitSet
* @param Molecule2 BitSet
* @return <B>Similarity <U>Tanimoto, Jaccard</U> </B>
* <B>c/(a+b-c)></B>
* @throws java.lang.Exception
*/
public static synchronized float getTanimotoSimilarity(BitSet Molecule1, BitSet Molecule2) throws Exception {
BitSet bitset1 = (BitSet) Molecule1.clone();
BitSet bitset2 = (BitSet) Molecule2.clone();
float _bitset1_cardinality = bitset1.cardinality();
float _bitset2_cardinality = bitset2.cardinality();
// System.out.println("bitset1: "+ bitset1.size() + " " + " bitset2" + bitset2.size());
if (bitset1.size() != bitset2.size()) {
throw new Exception("BitSets must have the same bit length");
}
BitSet one_and_two = (BitSet) bitset1.clone();
one_and_two.and(bitset2);
float _common_bit_count = one_and_two.cardinality();
return _common_bit_count / (_bitset1_cardinality + _bitset2_cardinality - _common_bit_count);
}
public FindPartitionConditions(BitSet dirs, RexBuilder builder) {
// go deep
super(true);
this.dirs = dirs;
this.builder = builder;
this.referencedDirs = new BitSet(dirs.size());
}
public byte[] toByteArray(BitSet bs) {
if (bs.size() == 0) {
return new byte[0];
}
// Find highest bit
int hiBit = -1;
for (int i = 0; i < bs.size(); i++) {
if (bs.get(i)) {
hiBit = i;
}
}
int n = (hiBit + 8) / 8;
byte[] bytes = new byte[n];
if (n == 0) {
return bytes;
}
Arrays.fill(bytes, (byte) 0);
for (int i = 0; i < n * 8; i++) {
if (bs.get(i)) {
setBit(i, bytes);
}
}
return bytes;
}
public static byte[] toByteArray_bak(BitSet bitSet) {
ByteArrayOutputStream baos = new ByteArrayOutputStream(bitSet.size());
try {
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(bitSet);
} catch (IOException ex) {
ex.printStackTrace();
}
return baos.toByteArray();
}
/**
* Returns <code>true</code> iff any bit in the given set is
* <code>true</code>.
*/
private boolean anyValueSet(BitSet set) {
for (int i = 0; i < set.size(); i++) {
if (set.get(i)) {
return true;
}
}
return false;
}
/**
*
* @param molSet
* @throws CDKException
*/
private static IPatternFingerprinter getSumOfFingerprints(IAtomContainerSet molSet) throws CDKException, Exception {
FingerprintGenerator molFingerprint = new FingerprintGenerator();
IPatternFingerprinter fp = new PatternFingerprinter(getFingerprinterSize());
for (IAtomContainer mol : molSet.atomContainers()) {
BitSet booleanArray = molFingerprint.getFingerprint(mol);
for (int i = 0; i < booleanArray.size(); i++) {
if (booleanArray.get(i)) {
fp.add(new Feature(valueOf(i), 1.0));
}
}
}
return fp;
}
private static <T> List <T> generateOne(List <T> elements, BitSet bs) {
List <T> entry = new ArrayList <T> (bs.size());
for (int i = 0; i < bs.size(); ++i) {
if (bs.get(i)) {
entry.add(elements.get(i));
}
}
return entry;
}
protected static Date firstDate(DatePattern dp, int dayCode) {
if (dp == null) return null;
BitSet weekCode = dp.getPatternBitSet();
if (weekCode.isEmpty()) return null;
Calendar cal = Calendar.getInstance(Locale.US); cal.setLenient(true);
Date dpFirstDate = DateUtils.getDate(1, dp.getSession().getPatternStartMonth(), dp.getSession().getSessionStartYear());
cal.setTime(dpFirstDate);
int idx = weekCode.nextSetBit(0);
cal.add(Calendar.DAY_OF_YEAR, idx);
while (idx < weekCode.size()) {
if (weekCode.get(idx)) {
int dow = cal.get(Calendar.DAY_OF_WEEK);
switch (dow) {
case Calendar.MONDAY:
if ((dayCode & DayCode.MON.getCode()) != 0) return cal.getTime();
break;
case Calendar.TUESDAY:
if ((dayCode & DayCode.TUE.getCode()) != 0) return cal.getTime();
break;
case Calendar.WEDNESDAY:
if ((dayCode & DayCode.WED.getCode()) != 0) return cal.getTime();
break;
case Calendar.THURSDAY:
if ((dayCode & DayCode.THU.getCode()) != 0) return cal.getTime();
break;
case Calendar.FRIDAY:
if ((dayCode & DayCode.FRI.getCode()) != 0) return cal.getTime();
break;
case Calendar.SATURDAY:
if ((dayCode & DayCode.SAT.getCode()) != 0) return cal.getTime();
break;
case Calendar.SUNDAY:
if ((dayCode & DayCode.SUN.getCode()) != 0) return cal.getTime();
break;
}
}
cal.add(Calendar.DAY_OF_YEAR, 1); idx++;
}
return null;
}
private <V extends Number> ClusterAffinityDistribution<V> evaluateSimilarity(ValueFactory<V> valueFactory, ComparisonMeasure comparisonMeasure, List<ClusteringField> clusteringFields, List<FieldValue> values){
ClusteringModel clusteringModel = getModel();
List<Cluster> clusters = clusteringModel.getClusters();
ClusterAffinityDistribution<V> result = createClusterAffinityDistribution(Classification.Type.SIMILARITY, clusters);
BitSet flags = MeasureUtil.toBitSet(values);
for(Cluster cluster : clusters){
BitSet clusterFlags = CacheUtil.getValue(cluster, ClusteringModelEvaluator.clusterFlagCache);
if(flags.size() != clusterFlags.size()){
throw new InvalidElementException(cluster);
}
Value<V> similarity = MeasureUtil.evaluateSimilarity(valueFactory, comparisonMeasure, clusteringFields, flags, clusterFlags);
result.put(cluster, similarity);
}
return result;
}
public FindPartitionConditions(BitSet dirs) {
// go deep
super(true);
this.dirs = dirs;
this.referencedDirs = new BitSet(dirs.size());
}
private synchronized void set(BitSet bitset) {
arrayFingerprint = new boolean[bitset.size()];
for (int i = 0; i < bitset.length(); i++) {
arrayFingerprint[i] = (bitset.get(i));
}
}