public class IntAllocator extends Object
A class for allocating integers from a given range that uses a
BitSetrepresentation of the free integers.
Concurrency Semantics:This class is not thread safe.
This was originally an ordered chain of non-overlapping Intervals, together with a fixed size array cache for freed integers.
allocate()is slightly slower than O(1) and in the worst case could be O(N), the use of a "
lastIndex" field for starting the next scan for free integers means this is negligible.
The data representation overhead is O(N) where N is the size of the allocation range. One
longis used for every 64 integers in the range.
Very little Object creation and destruction occurs in use.
Constructors Constructor Description
IntAllocator(int bottom, int top)Creates an IntAllocator allocating integer IDs within the inclusive range [
All Methods Instance Methods Concrete Methods Modifier and Type Method Description
allocate()Allocate an unallocated integer from the range, or return -1 if no more integers are available.
free(int reservation)Make the provided integer available for allocation again.
reserve(int reservation)Attempt to reserve the provided ID as if it had been allocated.
public int allocate()Allocate an unallocated integer from the range, or return -1 if no more integers are available.
- the allocated integer, or -1
public void free(int reservation)Make the provided integer available for allocation again. This operation runs in O(1) time. No error checking is performed, so if you double free or free an integer that was not originally allocated the results are undefined.
reservation- the previously allocated integer to free
public boolean reserve(int reservation)Attempt to reserve the provided ID as if it had been allocated. Returns true if it is available, false otherwise. This operation runs in O(1) time.
reservation- the integer to be allocated, if possible
falseif already allocated