com.rabbitmq.utility
Class IntAllocator

java.lang.Object
  extended by com.rabbitmq.utility.IntAllocator

public class IntAllocator
extends java.lang.Object

A class for allocating integer IDs in a given range.


Constructor Summary
IntAllocator(int start, int end)
          Creates an IntAllocator allocating integer IDs within the inclusive range [start, end]
 
Method Summary
 int allocate()
          Allocate a fresh integer from the range, or return -1 if no more integers are available.
 void flush()
           
 void free(int id)
          Make the provided integer available for allocation again.
static com.rabbitmq.utility.IntAllocator.IntervalList fromArray(int[] xs, int length)
           
static com.rabbitmq.utility.IntAllocator.IntervalList merge(com.rabbitmq.utility.IntAllocator.IntervalList x, com.rabbitmq.utility.IntAllocator.IntervalList y)
          Destructively merge two IntervalLists.
 boolean reserve(int id)
          Attempt to reserve the provided ID as if it had been allocated.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

IntAllocator

public IntAllocator(int start,
                    int end)
Creates an IntAllocator allocating integer IDs within the inclusive range [start, end]

Method Detail

merge

public static com.rabbitmq.utility.IntAllocator.IntervalList merge(com.rabbitmq.utility.IntAllocator.IntervalList x,
                                                                   com.rabbitmq.utility.IntAllocator.IntervalList y)
Destructively merge two IntervalLists. Invariant: None of the Intervals in the two lists may overlap intervals in this list.


fromArray

public static com.rabbitmq.utility.IntAllocator.IntervalList fromArray(int[] xs,
                                                                       int length)

allocate

public int allocate()
Allocate a fresh integer from the range, or return -1 if no more integers are available. This operation is guaranteed to run in O(1)


free

public void free(int id)
Make the provided integer available for allocation again. This operation runs in amortized O(sqrt(range size)) time: About every sqrt(range size) operations will take O(range_size + number of intervals) to complete and the rest run in constant time. No error checking is performed, so if you double free or free an integer that was not originally allocated the results are undefined. Sorry.


reserve

public boolean reserve(int id)
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(id) in the worst case scenario, though it can usually be expected to perform better than that unless a great deal of fragmentation has occurred.


flush

public void flush()

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object