예제 #1
0
    AvlNode<E> remove(Comparator<? super E> comparator, @Nullable E e, int count, int[] result) {
      int cmp = comparator.compare(e, elem);
      if (cmp < 0) {
        AvlNode<E> initLeft = left;
        if (initLeft == null) {
          result[0] = 0;
          return this;
        }

        left = initLeft.remove(comparator, e, count, result);

        if (result[0] > 0) {
          if (count >= result[0]) {
            this.distinctElements--;
            this.totalCount -= result[0];
          } else {
            this.totalCount -= count;
          }
        }
        return (result[0] == 0) ? this : rebalance();
      } else if (cmp > 0) {
        AvlNode<E> initRight = right;
        if (initRight == null) {
          result[0] = 0;
          return this;
        }

        right = initRight.remove(comparator, e, count, result);

        if (result[0] > 0) {
          if (count >= result[0]) {
            this.distinctElements--;
            this.totalCount -= result[0];
          } else {
            this.totalCount -= count;
          }
        }
        return rebalance();
      }

      // removing count from me!
      result[0] = elemCount;
      if (count >= elemCount) {
        return deleteMe();
      } else {
        this.elemCount -= count;
        this.totalCount -= count;
        return this;
      }
    }
예제 #2
0
 @CanIgnoreReturnValue
 @Override
 public int remove(@Nullable Object element, int occurrences) {
   checkNonnegative(occurrences, "occurrences");
   if (occurrences == 0) {
     return count(element);
   }
   AvlNode<E> root = rootReference.get();
   int[] result = new int[1]; // used as a mutable int reference to hold result
   AvlNode<E> newRoot;
   try {
     @SuppressWarnings("unchecked")
     E e = (E) element;
     if (!range.contains(e) || root == null) {
       return 0;
     }
     newRoot = root.remove(comparator(), e, occurrences, result);
   } catch (ClassCastException e) {
     return 0;
   } catch (NullPointerException e) {
     return 0;
   }
   rootReference.checkAndSet(root, newRoot);
   return result[0];
 }