boolean (int value) { assert value > 0; int pos; if (mySetLength == 0 || mySetLength > 0 && Math.abs(mySet[mySetLength - 1]) < value) { pos = -mySetLength-1; // most of the time during bulk indexing we add near the end } else { pos = binarySearch(mySet, 0, mySetLength, value); } if (pos >= 0) { if (mySet[pos] > 0) return false; pos = -pos - 1; // found removed } if (mySetLength == mySet.length) { int nextArraySize = mySet.length < 1024 ? mySet.length << 1 : mySet.length + mySet.length / 5; mySet = Arrays.copyOf(mySet, nextArraySize); } pos = -pos - 1; boolean lengthIsIncreased = pos == mySetLength; // insert at end if (!lengthIsIncreased && Math.abs(mySet[pos]) != value) { // todo we can shift until first removed System.arraycopy(mySet, pos, mySet, pos + 1, mySetLength - pos); lengthIsIncreased = true; } mySet[pos] = value; ++mySize; if (lengthIsIncreased) ++mySetLength; return true; }