示例#1
0
  /**
   * Remove the last element in the vector.
   *
   * @return element removed
   */
  public E pop_back() {
    Descriptor<E> desc;
    Descriptor<E> newd;
    E elem;
    do {
      desc = descriptor.get();
      desc.completeWrite();

      int pos = desc.size + FIRST_BUCKET_SIZE - 1;
      int bucketInd =
          Integer.numberOfLeadingZeros(FIRST_BUCKET_SIZE) - Integer.numberOfLeadingZeros(pos);
      int idx = Integer.highestOneBit(pos) ^ pos;
      elem = buckets.get(bucketInd).get(idx);
      newd = new Descriptor<E>(desc.size - 1, null);
    } while (!descriptor.compareAndSet(desc, newd));

    return elem;
  }
示例#2
0
  /**
   * add e at the end of vector.
   *
   * @param e element added
   */
  public void push_back(E e) {
    Descriptor<E> desc;
    Descriptor<E> newd;
    do {
      desc = descriptor.get();
      desc.completeWrite();

      int pos = desc.size + FIRST_BUCKET_SIZE;
      int zeroNumPos = Integer.numberOfLeadingZeros(pos);
      int bucketInd = zeroNumFirst - zeroNumPos;
      if (buckets.get(bucketInd) == null) {
        int newLen = 2 * buckets.get(bucketInd - 1).length();
        if (debug) System.out.println("New Length is:" + newLen);
        buckets.compareAndSet(bucketInd, null, new AtomicReferenceArray<E>(newLen));
      }

      int idx = (0x80000000 >>> zeroNumPos) ^ pos;
      newd =
          new Descriptor<E>(
              desc.size + 1, new WriteDescriptor<E>(buckets.get(bucketInd), idx, null, e));
    } while (!descriptor.compareAndSet(desc, newd));
    descriptor.get().completeWrite();
  }