Returns an array containing all permutations of a defined bitset with {@code bits} set and
maximum length of {@code length}.
771768
public static final long[] getAllPermutations(int bits, int length) {
    long min = allOnes(bits);
    long permutations = CombinatoricUtils.combinations(length, bits);
    long[] retVal = new long[(int) permutations];

    long val = min;
    for (int idx = 0; idx < permutations; idx++) {
      retVal[idx] = val;
      val = nextPermutation(val);
    }
    return retVal;
  }
613718
public Bits andSet(Bits xs) {
        Assert.check(currentState != BitsState.UNKNOWN);
        internalAndSet(xs);
        currentState = BitsState.NORMAL;
        return this;
    }
689855
def each_bit
      return enum_for(:each_bit) unless block_given?
      index = 0
      headbits = (self.bytesize - 1) * 8
      skipbits = 8 - @bits
      @data.each_byte do |byte|
        loop do
          break if index == @bitsize
          if index >= headbits
            bit = (byte >> (7 - ((index + skipbits) & 7))) & 1
          else
            bit = (byte >> (7 - (index & 7))) & 1
          end
          yield bit
          index += 1
          break if (index & 7) == 0
        end
      end
      return self
    end
1081349
public static final BitSet toBitSet(final int size, final long value) {
    BitSet bits = new BitSet(size);
    int idx = 0;
    long tmp = value;
    while (tmp != 0L) {
      if (tmp % 2L != 0L) {
        bits.set(idx);
      }
      ++idx;
      tmp = tmp >>> 1;
    }
    return bits;
  }
1290513
private static Set<Integer> setForRange(int max) {
    Set<Integer> set = Sets.newLinkedHashSet();
    for (int i = 0; i < max; i++) {
      set.add(i);
    }
    return set;
  }
278672
public Bits diffSet(Bits xs) {
        Assert.check(currentState != BitsState.UNKNOWN);
        for (int i = 0; i < bits.length; i++) {
            if (i < xs.bits.length) {
                bits[i] = bits[i] & ~xs.bits[i];
            }
        }
        currentState = BitsState.NORMAL;
        return this;
    }
1135900
public static BitArray allocate(long bits) {
    if (!(bits > 0 & (bits & (bits - 1)) == 0))
      throw new IllegalArgumentException("size must be a power of 2");
    return new BitArray(UnsafeHeapBytes.allocate(Math.max(bits / 8 + 8, 8)), bits);
  }
35695
private String[] getEquivalents(String segment) {
        Set<String> result = new HashSet<String>();
        Set<String> basic = getEquivalents2(segment);
        Set<String> permutations = new HashSet<String>();

        // now get all the permutations
        // add only the ones that are canonically equivalent
        // TODO: optimize by not permuting any class zero.
        Iterator<String> it = basic.iterator();
        while (it.hasNext()) {
            String item = it.next();
            permutations.clear();
            permute(item, SKIP_ZEROS, permutations);
            Iterator<String> it2 = permutations.iterator();
            while (it2.hasNext()) {
                String possible = it2.next();

/*
                String attempt = Normalizer.normalize(possible, Normalizer.DECOMP, 0);
                if (attempt.equals(segment)) {
*/
                if (Normalizer.compare(possible, segment,0)==0) {

                    if (PROGRESS) System.out.println("Adding Permutation: " + Utility.hex(possible));
                    result.add(possible);

                } else {
                    if (PROGRESS) System.out.println("-Skipping Permutation: " + Utility.hex(possible));
                }
            }
        }

        // convert into a String[] to clean up storage
        String[] finalResult = new String[result.size()];
        result.toArray(finalResult);
        return finalResult;
    }
1109600
public static <T> Set<Set<T>> combinations(Set<T> elements, int subsetSize) {
        return combinationsStream(elements, subsetSize).collect(Collectors.toSet());
    }
888060
List<Long> getPositionList() {
		List<Long> ret = new ArrayList<Long>();

		ret.add(-1L);
		/*
		 * This -1 is pointing to the previous position of the first valid
		 * position of the bit vector, which starts at index 0. Since the zeroth
		 * occurrence of a bit is undefined, the first occurrence can be at
		 * position 0, or later.
		 */

		long count = 0;
		for (long index = 0; index < this.bitVector.size(); index++) {
			if (this.bitVector.getBit(index) == this.bit) {
				count++;
			}
			if (count >= this.blockSize) {
				count = 0;
				ret.add(index);
			}
		}
		return ret;
	}
1135952
public static int cardinality(long[] bits) {
    int card = 0;
    for (int i = bits.length - 1; i >= 0; i--) {
      long a = bits[i];
      if (a == 0)
        continue;
      if (a == -1) {
        card += 64;
        continue;
      }
      // Successively collapse alternating bit groups into a sum.
      a = ((a >> 1) & 0x5555555555555555L) + (a & 0x5555555555555555L);
      a = ((a >> 2) & 0x3333333333333333L) + (a & 0x3333333333333333L);
      int b = (int) ((a >>> 32) + a);
      b = ((b >> 4) & 0x0f0f0f0f) + (b & 0x0f0f0f0f);
      b = ((b >> 8) & 0x00ff00ff) + (b & 0x00ff00ff);
      card += ((b >> 16) & 0x0000ffff) + (b & 0x0000ffff);
    }
    return card;
  }
998254
public static <T> Stream<Set<T>> combinationsStream(Set<T> elements, int subsetSize) {
        if (subsetSize == 0) {
            return Stream.of(new HashSet<>());
        }
        else if (subsetSize <= elements.size()) {
            Set<T> remainingElements = elements;

            Iterator<T> it = remainingElements.iterator();
            T X = it.next();
            it.remove();

            Stream<Set<T>> combinations = Stream.concat(
                    combinationsStream(remainingElements, subsetSize), //exclude X
                    combinationsStream(remainingElements, subsetSize-1).map(s -> {s.add(X); return s;}) //include X
            );

            remainingElements.add(X);

            return combinations;
        }
        else {
            return Stream.empty();
        }
    }
471100
public static Collection<Integer> ofSetBits(long i) {
    	final Collection<Integer> c = new LinkedList<Integer>();
        int k = 0;
        while (i != 0) {
            if ((i & 1L) != 0) {
                c.add(k);
            }
            k++;
            i >>>= 1;
        }
        return c;
    }
1160295
public int packedBits() {
    int bits = templateNestDepth;
    bits = (bits << N_URI_TYPE_BITS) | uriType.ordinal();
    bits = (bits << N_URI_PART_BITS) | uriPart.ordinal();
    bits = (bits << N_JS_SLASH_BITS) | slashType.ordinal();
    bits = (bits << N_DELIM_BITS) | delimType.ordinal();
    bits = (bits << N_ATTR_BITS) | attrType.ordinal();
    bits = (bits << N_ELEMENT_BITS) | elType.ordinal();
    bits = (bits << N_STATE_BITS) | state.ordinal();
    return bits;
  }
854097
@SuppressWarnings("unchecked")
    public T[] nextPermutationAsArray()
    {
        T[] permutation = (T[]) Array.newInstance(elements.getClass().getComponentType(),
                                                  permutationIndices.length);
        return nextPermutationAsArray(permutation);
    }
335344
public static int[] randomPermutation(int size) {
        Random r = new Random();
        int[] result = new int[size];

        for (int j = 0; j < size; j++) {
            result[j] = j;
        }
        
        for (int j = size - 1; j > 0; j--) {
            int k = r.nextInt(j);
            int temp = result[j];
            result[j] = result[k];
            result[k] = temp;
        }

        return result;
    }
213139
public static int maxbits(int[] i, int pos, int length) {
        int mask = 0;
        for (int k = pos; k < pos + length; ++k)
            mask |= i[k];
        return bits(mask);
    }