|
| 1 | +// This source code is dual-licensed under the Apache License, version |
| 2 | +// 2.0, and the Mozilla Public License, version 1.1. |
| 3 | +// |
| 4 | +// The APL v2.0: |
| 5 | +// |
| 6 | +//--------------------------------------------------------------------------- |
| 7 | +// Copyright (C) 2007-2009 LShift Ltd., Cohesive Financial |
| 8 | +// Technologies LLC., and Rabbit Technologies Ltd. |
| 9 | +// |
| 10 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 11 | +// you may not use this file except in compliance with the License. |
| 12 | +// You may obtain a copy of the License at |
| 13 | +// |
| 14 | +// http://www.Apache.Org/licenses/LICENSE-2.0 |
| 15 | +// |
| 16 | +// Unless required by applicable law or agreed to in writing, software |
| 17 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | +// See the License for the specific language governing permissions and |
| 20 | +// limitations under the License. |
| 21 | +//--------------------------------------------------------------------------- |
| 22 | +// |
| 23 | +// The MPL v1.1: |
| 24 | +// |
| 25 | +//--------------------------------------------------------------------------- |
| 26 | +// The contents of this file are subject to the Mozilla Public License |
| 27 | +// Version 1.1 (the "License"); you may not use this file except in |
| 28 | +// compliance with the License. You may obtain a copy of the License at |
| 29 | +// http://www.Rabbitmq.Com/mpl.Html |
| 30 | +// |
| 31 | +// Software distributed under the License is distributed on an "AS IS" |
| 32 | +// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
| 33 | +// License for the specific language governing rights and limitations |
| 34 | +// under the License. |
| 35 | +// |
| 36 | +// The Original Code is The RabbitMQ .NET Client. |
| 37 | +// |
| 38 | +// The Initial Developers of the Original Code are LShift Ltd, |
| 39 | +// Cohesive Financial Technologies LLC, and Rabbit Technologies Ltd. |
| 40 | +// |
| 41 | +// Portions created before 22-Nov-2008 00:00:00 GMT by LShift Ltd, |
| 42 | +// Cohesive Financial Technologies LLC, or Rabbit Technologies Ltd |
| 43 | +// are Copyright (C) 2007-2008 LShift Ltd, Cohesive Financial |
| 44 | +// Technologies LLC, and Rabbit Technologies Ltd. |
| 45 | +// |
| 46 | +// Portions created by LShift Ltd are Copyright (C) 2007-2009 LShift |
| 47 | +// Ltd. Portions created by Cohesive Financial Technologies LLC are |
| 48 | +// Copyright (C) 2007-2009 Cohesive Financial Technologies |
| 49 | +// LLC. Portions created by Rabbit Technologies Ltd are Copyright |
| 50 | +// (C) 2007-2009 Rabbit Technologies Ltd. |
| 51 | +// |
| 52 | +// All Rights Reserved. |
| 53 | +// |
| 54 | +// Contributor(s): ______________________________________. |
| 55 | +// |
| 56 | +//--------------------------------------------------------------------------- |
| 57 | + |
| 58 | +using System; |
| 59 | +using System.Collections.Generic; |
| 60 | +using System.Text; |
| 61 | +using System.Diagnostics; |
| 62 | + |
| 63 | +namespace RabbitMQ.Util |
| 64 | +{ |
| 65 | + |
| 66 | + |
| 67 | + /** |
| 68 | + * A class for allocating integer IDs in a given range. |
| 69 | + */ |
| 70 | + public class IntAllocator{ |
| 71 | + |
| 72 | + private IntervalList Base; |
| 73 | + |
| 74 | + private readonly int[] unsorted; |
| 75 | + private int unsortedCount = 0; |
| 76 | + |
| 77 | + /** |
| 78 | + * A class representing a list of inclusive intervals |
| 79 | + */ |
| 80 | + public class IntervalList{ |
| 81 | + public IntervalList(int start, int end){ |
| 82 | + this.Start = start; |
| 83 | + this.End = end; |
| 84 | + } |
| 85 | + |
| 86 | + public int Start; |
| 87 | + public int End; |
| 88 | + |
| 89 | + // Invariant: If Next != Null then Next.Start > this.End + 1 |
| 90 | + public IntervalList Next; |
| 91 | + |
| 92 | + // Destructively merge two IntervalLists. |
| 93 | + // Invariant: None of the Intervals in the two lists may overlap |
| 94 | + // intervals in this list. |
| 95 | + public static IntervalList Merge(IntervalList x, IntervalList y) |
| 96 | + { |
| 97 | + if(x == null) return y; |
| 98 | + if(y == null) return x; |
| 99 | + |
| 100 | + if(x.End > y.Start) return Merge(y, x); |
| 101 | + |
| 102 | + Debug.Assert(x.End != y.Start); |
| 103 | + |
| 104 | + // We now have x, y non-null and x.End < y.Start. |
| 105 | + |
| 106 | + if(y.Start == x.End + 1) |
| 107 | + { |
| 108 | + // The two intervals adjoin. Merge them into one and then |
| 109 | + // merge the tails. |
| 110 | + x.End = y.End; |
| 111 | + x.Next = Merge(x.Next, y.Next); |
| 112 | + return x; |
| 113 | + } |
| 114 | + |
| 115 | + // y belongs in the tail of x. |
| 116 | + |
| 117 | + x.Next = Merge(y, x.Next); |
| 118 | + return x; |
| 119 | + } |
| 120 | + |
| 121 | + public static IntervalList FromArray(int[] xs, int length) |
| 122 | + { |
| 123 | + Array.Sort(xs); |
| 124 | + |
| 125 | + IntervalList result = null; |
| 126 | + IntervalList current = null; |
| 127 | + |
| 128 | + int i = 0; |
| 129 | + while(i < length){ |
| 130 | + int start = i; |
| 131 | + while((i < length - 1) && (xs[i + 1] == xs[i] + 1)) |
| 132 | + i++; |
| 133 | + |
| 134 | + IntervalList interval = new IntervalList(start, i); |
| 135 | + |
| 136 | + if(result == null) |
| 137 | + { |
| 138 | + result = interval; |
| 139 | + current = interval; |
| 140 | + } |
| 141 | + else |
| 142 | + { |
| 143 | + current.Next = interval; |
| 144 | + current = interval; |
| 145 | + } |
| 146 | + i++; |
| 147 | + } |
| 148 | + return result; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Creates an IntAllocator allocating integer IDs within the inclusive range [start, end] |
| 154 | + */ |
| 155 | + public IntAllocator(int start, int end) |
| 156 | + { |
| 157 | + if(start > end) throw new ArgumentException("illegal range [" + start +", " + end + "]"); |
| 158 | + |
| 159 | + // Fairly arbitrary heuristic for a good size for the unsorted set. |
| 160 | + unsorted = new int[Math.Max(32, (int)Math.Sqrt(end - start))]; |
| 161 | + Base = new IntervalList(start, end); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Allocate a fresh integer from the range, or return -1 if no more integers |
| 166 | + * are available. This operation is guaranteed to run in O(1) |
| 167 | + */ |
| 168 | + public int Allocate() |
| 169 | + { |
| 170 | + if(unsortedCount > 0){ |
| 171 | + return unsorted[--unsortedCount]; |
| 172 | + } else if (Base != null) { |
| 173 | + int result = Base.Start++; |
| 174 | + if(Base.Start == Base.End) Base = Base.Next; |
| 175 | + return result; |
| 176 | + } else { |
| 177 | + return -1; |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + private void Flush() |
| 182 | + { |
| 183 | + if(unsortedCount > 0) |
| 184 | + { |
| 185 | + Base = IntervalList.Merge(Base, IntervalList.FromArray(unsorted, unsortedCount)); |
| 186 | + unsortedCount = 0; |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + |
| 191 | + /** |
| 192 | + * Make the provided integer available for allocation again. This operation |
| 193 | + * runs in amortized O(sqrt(range size)) time: About every sqrt(range size) |
| 194 | + * operations will take O(range_size + number of intervals) to complete and |
| 195 | + * the rest run in constant time. |
| 196 | + * |
| 197 | + * No error checking is performed, so if you double Free or Free an integer |
| 198 | + * that was not originally Allocated the results are undefined. Sorry. |
| 199 | + */ |
| 200 | + public void Free(int id) |
| 201 | + { |
| 202 | + if(unsortedCount >= unsorted.Length) |
| 203 | + { |
| 204 | + Flush(); |
| 205 | + } |
| 206 | + unsorted[unsortedCount++] = id; |
| 207 | + } |
| 208 | + |
| 209 | + public bool Reserve(int id) |
| 210 | + { |
| 211 | + // We always flush before reserving because the only way to determine |
| 212 | + // if an ID is in the unsorted array is through a linear scan. This leads |
| 213 | + // us to the potentially expensive situation where there is a large unsorted |
| 214 | + // array and we reserve several IDs, incurring the cost of the scan each time. |
| 215 | + // Flushing makes sure the array is always empty and does no additional work if |
| 216 | + // reserve is called twice. |
| 217 | + Flush(); |
| 218 | + |
| 219 | + IntervalList current = Base; |
| 220 | + |
| 221 | + while(current != null) |
| 222 | + { |
| 223 | + if(current.End < id) |
| 224 | + { |
| 225 | + current = current.Next; |
| 226 | + continue; |
| 227 | + } |
| 228 | + else if(current.Start > id) |
| 229 | + { |
| 230 | + return false; |
| 231 | + } |
| 232 | + else if(current.End == id) |
| 233 | + { |
| 234 | + current.End--; |
| 235 | + } |
| 236 | + else if(current.Start == id) |
| 237 | + { |
| 238 | + current.Start++; |
| 239 | + } |
| 240 | + else |
| 241 | + { |
| 242 | + // The ID is in the middle of this interval. |
| 243 | + // We need to split the interval into two. |
| 244 | + IntervalList rest = new IntervalList(id + 1, current.End); |
| 245 | + current.End = id - 1; |
| 246 | + rest.Next = current.Next; |
| 247 | + current.Next = rest; |
| 248 | + } |
| 249 | + return true; |
| 250 | + } |
| 251 | + return false; |
| 252 | + } |
| 253 | + |
| 254 | + } |
| 255 | +} |
0 commit comments