forked from Speiger/Primitive-Collections
Added TrimAndClear function into ITrimmable
This commit is contained in:
parent
13b2c727fc
commit
998272c8d5
|
@ -921,6 +921,20 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trims the collection down to the requested size and clears all elements while doing so
|
||||
* @param size the amount of elements that should be allowed
|
||||
* @note this will enforce minimum size of the collection itself
|
||||
*/
|
||||
@Override
|
||||
public void clearAndTrim(int size) {
|
||||
if(data.length <= size) {
|
||||
clear();
|
||||
return;
|
||||
}
|
||||
data = size == 0 ? EMPTY_KEY_ARRAY : NEW_KEY_ARRAY(size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Increases the capacity of this implementation instance, if necessary,
|
||||
* to ensure that it can hold at least the number of elements specified by
|
||||
|
|
|
@ -237,6 +237,22 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_G
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trims the collection down to the requested size and clears all elements while doing so
|
||||
* @param size the amount of elements that should be allowed
|
||||
* @note this will enforce minimum size of the collection itself
|
||||
*/
|
||||
@Override
|
||||
public void clearAndTrim(int size) {
|
||||
int newSize = Math.max(MIN_CAPACITY, size);
|
||||
if(array.length <= newSize) {
|
||||
clear();
|
||||
return;
|
||||
}
|
||||
first = last = 0;
|
||||
array = NEW_KEY_ARRAY(newSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] input) {
|
||||
if(input == null || input.length < size()) input = NEW_KEY_ARRAY(size());
|
||||
|
|
|
@ -501,6 +501,21 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY
|
|||
firstIndex = lastIndex = -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearAndTrim(int size) {
|
||||
if(nullIndex <= size) {
|
||||
clear();
|
||||
return;
|
||||
}
|
||||
nullIndex = size;
|
||||
mask = nullIndex - 1;
|
||||
maxFill = Math.min((int)Math.ceil(nullIndex * loadFactor), nullIndex - 1);
|
||||
keys = NEW_KEY_ARRAY(nullIndex + 1);
|
||||
links = new long[nullIndex + 1];
|
||||
firstIndex = -1;
|
||||
lastIndex = -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LIST_ITERATOR KEY_GENERIC_TYPE iterator() {
|
||||
return new SetIterator();
|
||||
|
|
|
@ -473,6 +473,21 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE
|
|||
firstIndex = lastIndex = -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearAndTrim(int size) {
|
||||
if(nullIndex <= size) {
|
||||
clear();
|
||||
return;
|
||||
}
|
||||
nullIndex = size;
|
||||
mask = nullIndex - 1;
|
||||
maxFill = Math.min((int)Math.ceil(nullIndex * loadFactor), nullIndex - 1);
|
||||
keys = NEW_KEY_ARRAY(nullIndex + 1);
|
||||
links = new long[nullIndex + 1];
|
||||
firstIndex = -1;
|
||||
lastIndex = -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LIST_ITERATOR KEY_GENERIC_TYPE iterator() {
|
||||
return new SetIterator();
|
||||
|
|
|
@ -348,6 +348,18 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T
|
|||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearAndTrim(int size) {
|
||||
if(nullIndex <= size) {
|
||||
clear();
|
||||
return;
|
||||
}
|
||||
nullIndex = size;
|
||||
mask = nullIndex - 1;
|
||||
maxFill = Math.min((int)Math.ceil(nullIndex * loadFactor), nullIndex - 1);
|
||||
keys = NEW_KEY_ARRAY(nullIndex + 1);
|
||||
}
|
||||
|
||||
private void ensureCapacity(int newCapacity) {
|
||||
int size = HashUtil.arraySize(newCapacity, loadFactor);
|
||||
if(size > nullIndex) rehash(size);
|
||||
|
|
|
@ -306,6 +306,18 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp
|
|||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearAndTrim(int size) {
|
||||
if(nullIndex <= size) {
|
||||
clear();
|
||||
return;
|
||||
}
|
||||
nullIndex = size;
|
||||
mask = nullIndex - 1;
|
||||
maxFill = Math.min((int)Math.ceil(nullIndex * loadFactor), nullIndex - 1);
|
||||
keys = NEW_KEY_ARRAY(nullIndex + 1);
|
||||
}
|
||||
|
||||
private void ensureCapacity(int newCapacity) {
|
||||
int size = HashUtil.arraySize(newCapacity, loadFactor);
|
||||
if(size > nullIndex) rehash(size);
|
||||
|
|
|
@ -146,6 +146,9 @@ public class LISTS
|
|||
@Override
|
||||
public boolean trim(int size) { synchronized(mutex) { return l.trim(size); } }
|
||||
|
||||
@Override
|
||||
public void clearAndTrim(int size) { synchronized(mutex) { l.clearAndTrim(size); } }
|
||||
|
||||
@Override
|
||||
public KEY_TYPE[] elements() { synchronized(mutex) { return l.elements(); } }
|
||||
|
||||
|
|
|
@ -358,6 +358,9 @@ public class SETS
|
|||
|
||||
@Override
|
||||
public boolean trim(int size) { synchronized(mutex) { return trim.trim(size); } }
|
||||
|
||||
@Override
|
||||
public void clearAndTrim(int size) { synchronized(mutex) { trim.clearAndTrim(size); } }
|
||||
}
|
||||
|
||||
private static class SynchronizedNavigableSet KEY_GENERIC_TYPE extends SynchronizedSortedSet KEY_GENERIC_TYPE implements NAVIGABLE_SET KEY_GENERIC_TYPE
|
||||
|
@ -450,6 +453,9 @@ public class SETS
|
|||
|
||||
@Override
|
||||
public boolean trim(int size) { synchronized(mutex) { return trim.trim(size); } }
|
||||
|
||||
@Override
|
||||
public void clearAndTrim(int size) { synchronized(mutex) { trim.clearAndTrim(size); } }
|
||||
}
|
||||
|
||||
private static class SynchronizedSortedSet KEY_GENERIC_TYPE extends SynchronizedSet KEY_GENERIC_TYPE implements SORTED_SET KEY_GENERIC_TYPE
|
||||
|
@ -525,6 +531,9 @@ public class SETS
|
|||
|
||||
@Override
|
||||
public boolean trim(int size) { synchronized(mutex) { return trim.trim(size); } }
|
||||
|
||||
@Override
|
||||
public void clearAndTrim(int size) { synchronized(mutex) { trim.clearAndTrim(size); } }
|
||||
}
|
||||
|
||||
private static class SynchronizedSet KEY_GENERIC_TYPE extends SynchronizedCollection KEY_GENERIC_TYPE implements SET KEY_GENERIC_TYPE
|
||||
|
|
|
@ -20,4 +20,18 @@ public interface ITrimmable
|
|||
* @return if the internal array has been trimmed.
|
||||
*/
|
||||
public boolean trim(int size);
|
||||
|
||||
/**
|
||||
* Trims the collection down to the original size and clears all present elements with it.
|
||||
*/
|
||||
public default void clearAndTrim() {
|
||||
clearAndTrim(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Trims the collection down to the requested size and clears all elements while doing so
|
||||
* @param size the amount of elements that should be allowed
|
||||
* @note this will enforce minimum size of the collection itself
|
||||
*/
|
||||
public void clearAndTrim(int size);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue