fix problem with extending sorted set by one and attempting to increase random access capacity in case of value removal

This commit is contained in:
Maxim.Mossienko
2014-06-05 23:11:03 +02:00
parent aa0e217bb9
commit e4dbc27d73
2 changed files with 20 additions and 14 deletions
@@ -78,10 +78,10 @@ public class ChangeBufferingList implements Cloneable {
}
public void remove(int value) {
ensureCapacity(1);
if (DEBUG) checkSet.remove(value);
RandomAccessIntContainer intContainer = randomAccessContainer;
if (intContainer == null) {
ensureCapacity(1);
addChange(-value);
}
else {
@@ -197,22 +197,25 @@ public class ChangeBufferingList implements Cloneable {
if (changes == null) {
changes = new int[Math.max(3, diff)];
} else if (length + diff > changes.length) {
int nextArraySize = Math.min(
Math.max(changes.length < 1024 ? changes.length << 1 : changes.length + changes.length / 5, length + diff),
MAX_FILES
);
int[] newChanges = new int[nextArraySize];
int[] newChanges = new int[calcNextArraySize(changes.length, length + diff)];
System.arraycopy(changes, 0, newChanges, 0, length);
changes = newChanges;
}
}
static int calcNextArraySize(int currentSize, int wantedSize) {
return Math.min(
Math.max(currentSize < 1024 ? currentSize << 1 : currentSize + currentSize / 5, wantedSize),
MAX_FILES
);
}
public boolean isEmpty() {
if (randomAccessContainer == null) {
if (changes == null) return true;
if (removals == 0) return length == 0;
}
// todo we can
// todo we can calculate isEmpty in more cases (without container)
RandomAccessIntContainer intContainer = getRandomAccessContainer();
return intContainer.size() == 0;
}
@@ -200,16 +200,19 @@ public class SortedIdSet implements Cloneable, RandomAccessIntContainer {
}
public RandomAccessIntContainer ensureContainerCapacity(int count) {
if (mySize + count > ChangeBufferingList.MAX_FILES) {
int newSize = mySetLength + count;
if (newSize < mySet.length) return this;
if (newSize > ChangeBufferingList.MAX_FILES) {
return new IdBitSet(this, count);
}
int newSize = mySetLength + count;
newSize = ChangeBufferingList.calcNextArraySize(mySet.length, newSize);
assert newSize < Short.MAX_VALUE;
if (newSize >= mySet.length) {
int[] newSet = new int[newSize]; // todo slightly increase size and compact
System.arraycopy(mySet, 0, newSet, 0, mySetLength);
mySet = newSet;
}
int[] newSet = new int[newSize]; // todo slightly increase size and compact
System.arraycopy(mySet, 0, newSet, 0, mySetLength);
mySet = newSet;
return this;
}