3131package com .rabbitmq .utility ;
3232
3333/**
34- * A class for allocating integer IDs in a given range.
35- */
34+ * A class for allocating integer IDs in a given range.
35+ */
3636public class IntAllocator {
3737
3838 // Invariant: Sorted in order of first element. Non-overlapping, non-adjacent.
39- // This could really use being a balanced binary tree. However for normal usages
39+ // This could really use being a balanced binary tree. However for normal usages
4040 // it doesn't actually matter.
4141 private IntervalList base ;
4242
4343 private final int [] unsorted ;
4444 private int unsortedCount = 0 ;
4545
46- /**
46+ /**
4747 * A class representing an inclusive interval from start to end.
4848 */
4949 private static class IntervalList {
@@ -56,14 +56,14 @@ private static class IntervalList{
5656 int end ;
5757 IntervalList next ;
5858
59- int length (){ return end - start + 1 ; }
59+ int length (){ return end - start + 1 ; }
6060 }
6161
6262 /** Destructively merge two IntervalLists.
6363 * Invariant: None of the Intervals in the two lists may overlap
6464 * intervals in this list.
6565 */
66- public static IntervalList merge (IntervalList x , IntervalList y ){
66+ public static IntervalList merge (IntervalList x , IntervalList y ){
6767 if (x == null ) return y ;
6868 if (y == null ) return x ;
6969
@@ -112,7 +112,7 @@ public static IntervalList fromArray(int[] xs, int length){
112112 }
113113
114114
115- /**
115+ /**
116116 * Creates an IntAllocator allocating integer IDs within the inclusive range [start, end]
117117 */
118118 public IntAllocator (int start , int end ){
@@ -125,26 +125,26 @@ public IntAllocator(int start, int end){
125125
126126 /**
127127 * Allocate a fresh integer from the range, or return -1 if no more integers
128- * are available. This operation is guaranteed to run in O(1)
128+ * are available. This operation is guaranteed to run in O(1)
129129 */
130130 public int allocate (){
131131 if (unsortedCount > 0 ){
132132 return unsorted [--unsortedCount ];
133133 } else if (base != null ){
134134 IntervalList source = base ;
135135 if (base .length () == 1 ) base = base .next ;
136- return source .start ++;
136+ return source .start ++;
137137 } else {
138138 return -1 ;
139139 }
140140 }
141141
142- /**
143- * Make the provided integer available for allocation again. This operation
144- * runs in amortized O(sqrt(range size)) time: About every sqrt(range size)
145- * operations will take O(range_size + number of intervals) to complete and
142+ /**
143+ * Make the provided integer available for allocation again. This operation
144+ * runs in amortized O(sqrt(range size)) time: About every sqrt(range size)
145+ * operations will take O(range_size + number of intervals) to complete and
146146 * the rest run in constant time.
147- *
147+ *
148148 * No error checking is performed, so if you double free or free an integer
149149 * that was not originally allocated the results are undefined. Sorry.
150150 */
@@ -155,13 +155,13 @@ public void free(int id){
155155 unsorted [unsortedCount ++] = id ;
156156 }
157157
158- /**
159- * Attempt to reserve the provided ID as if it had been allocated. Returns true
160- * if it is available, false otherwise.
158+ /**
159+ * Attempt to reserve the provided ID as if it had been allocated. Returns true
160+ * if it is available, false otherwise.
161161 *
162162 * This operation runs in O(id) in the worst case scenario, though it can usually
163163 * be expected to perform better than that unless a great deal of fragmentation
164- * has occurred.
164+ * has occurred.
165165 */
166166 public boolean reserve (int id ){
167167 flush ();
@@ -174,13 +174,13 @@ public boolean reserve(int id){
174174
175175 if (current == null ) return false ;
176176 if (current .start > id ) return false ;
177-
177+
178178 if (current .end == id )
179179 current .end --;
180180 else if (current .start == id )
181181 current .start ++;
182182 else {
183- // The ID is in the middle of this interval.
183+ // The ID is in the middle of this interval.
184184 // We need to split the interval into two.
185185 IntervalList rest = new IntervalList (id + 1 , current .end );
186186 current .end = id - 1 ;
@@ -193,9 +193,9 @@ else if(current.start == id)
193193
194194 public void flush (){
195195 if (unsortedCount == 0 ) return ;
196-
196+
197197 base = merge (base , fromArray (unsorted , unsortedCount ));
198- unsortedCount = 0 ;
198+ unsortedCount = 0 ;
199199 }
200200
201201 @ Override public String toString (){
@@ -207,8 +207,8 @@ public void flush(){
207207 IntervalList it = base ;
208208 while (it != null ){
209209 builder .append (it .start ).append (".." ).append (it .end );
210- if (it .next != null ) builder .append (", " );
211- it = it .next ;
210+ if (it .next != null ) builder .append (", " );
211+ it = it .next ;
212212 }
213213 builder .append ("]" );
214214
0 commit comments