Пример #1
0
  public static Entry[] andNot(Entry[] a, Entry[] b) {
    if (Debug.ENABLED) {
      checkInvariants(a);
      checkInvariants(b);
    }

    if (Debug.ENABLED) { // Should not call for nothing
      boolean empty = true;

      for (int i = b.length - 1; i >= 0; i--) {
        if (b[i] != null) {
          Debug.assertion(b[i].Value != 0);
          empty = false;
        }
      }

      Debug.assertion(!empty);
    }

    Entry[] result = PlatformAdapter.createBitsArray(a.length);

    for (int i = a.length - 1; i >= 0; i--) if (a[i] != null) result[i] = andNot(a[i], b);

    return result;
  }
Пример #2
0
  public static boolean tryToSet(Entry[] sparse, int index) {
    if (Debug.ENABLED) Debug.assertion(index >= 0);

    if (Debug.ENABLED) checkInvariants(sparse);

    int intIndex = intIndex(index);
    int foldedIntIndex = intIndex & (sparse.length - 1);

    for (int i = SparseArrayHelper.attemptsStart(sparse.length); i >= 0; i--) {
      Entry current = sparse[foldedIntIndex];

      if (current != null && current.IntIndex == intIndex) {
        current.Value |= maskArray(index);
        return true;
      }

      if (current == null) {
        sparse[foldedIntIndex] = PlatformAdapter.createBitsEntry(intIndex, maskArray(index));
        return true;
      }

      foldedIntIndex = (foldedIntIndex + 1) & (sparse.length - 1);
    }

    return false;
  }
Пример #3
0
  public static final Entry[] set(Entry[] sparse, int index) {
    if (sparse == null)
      sparse = PlatformAdapter.createBitsArray(Bits.SPARSE_BITSET_DEFAULT_CAPACITY);

    while (!tryToSet(sparse, index)) sparse = reindex(sparse);

    return sparse;
  }
Пример #4
0
  private static final Entry[] reindex(Entry[] sparse) {
    Entry[] old = sparse;

    for (; ; ) {
      sparse = PlatformAdapter.createBitsArray(sparse.length << SparseArrayHelper.TIMES_TWO_SHIFT);

      if (reindex(old, sparse)) break;
    }

    return sparse;
  }
Пример #5
0
  private static Entry andNot(Entry entry, Entry[] mask) {
    int foldedIntIndex = entry.IntIndex & (mask.length - 1);

    for (int i = SparseArrayHelper.attemptsStart(mask.length); i >= 0; i--) {
      Entry current = mask[foldedIntIndex];

      if (current != null && current.IntIndex == entry.IntIndex)
        return PlatformAdapter.createBitsEntry(entry.IntIndex, entry.Value & ~current.Value);

      if (current == null) return entry;

      foldedIntIndex = (foldedIntIndex + 1) & (mask.length - 1);
    }

    return entry;
  }
Пример #6
0
  private static boolean mergeByCopy(Entry[] sparse, Entry entry) {
    int foldedIntIndex = entry.IntIndex & (sparse.length - 1);

    for (int i = SparseArrayHelper.attemptsStart(sparse.length); i >= 0; i--) {
      Entry current = sparse[foldedIntIndex];

      if (current != null && current.IntIndex == entry.IntIndex) {
        sparse[foldedIntIndex] =
            PlatformAdapter.createBitsEntry(current.IntIndex, current.Value | entry.Value);
        return true;
      }

      if (current == null) {
        sparse[foldedIntIndex] = entry;
        return true;
      }

      foldedIntIndex = (foldedIntIndex + 1) & (sparse.length - 1);
    }

    return false;
  }