Skip to content

Commit

Permalink
Merging bug22334 inonto default
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Sackman committed Feb 10, 2010
2 parents 5638066 + 1e11502 commit d2fdef3
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions projects/client/RabbitMQ.Client/src/util/IntAllocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,22 @@ namespace RabbitMQ.Util


/**
* A class for allocating integer IDs in a given range.
*/
* A class for allocating integer IDs in a given range.
*/
public class IntAllocator{

private IntervalList Base;

private readonly int[] unsorted;
private int unsortedCount = 0;

/**
/**
* A class representing a list of inclusive intervals
*/
public class IntervalList{
public IntervalList(int start, int end){
this.Start = start;
this.End = end;
this.End = end;
}

public int Start;
Expand All @@ -93,13 +93,13 @@ public IntervalList(int start, int end){
// Invariant: None of the Intervals in the two lists may overlap
// intervals in this list.
public static IntervalList Merge(IntervalList x, IntervalList y)
{
{
if(x == null) return y;
if(y == null) return x;

if(x.End > y.Start) return Merge(y, x);

Debug.Assert(x.End != y.Start);
Debug.Assert(x.End != y.Start);

// We now have x, y non-null and x.End < y.Start.

Expand Down Expand Up @@ -138,7 +138,7 @@ public static IntervalList FromArray(int[] xs, int length)
result = interval;
current = interval;
}
else
else
{
current.Next = interval;
current = interval;
Expand All @@ -149,7 +149,7 @@ public static IntervalList FromArray(int[] xs, int length)
}
}

/**
/**
* Creates an IntAllocator allocating integer IDs within the inclusive range [start, end]
*/
public IntAllocator(int start, int end)
Expand All @@ -163,7 +163,7 @@ public IntAllocator(int start, int end)

/**
* 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)
* are available. This operation is guaranteed to run in O(1)
*/
public int Allocate()
{
Expand All @@ -188,12 +188,12 @@ private void Flush()
}


/**
* 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
/**
* 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.
*/
Expand All @@ -211,7 +211,7 @@ public bool Reserve(int id)
// We always flush before reserving because the only way to determine
// if an ID is in the unsorted array is through a linear scan. This leads
// us to the potentially expensive situation where there is a large unsorted
// array and we reserve several IDs, incurring the cost of the scan each time.
// array and we reserve several IDs, incurring the cost of the scan each time.
// Flushing makes sure the array is always empty and does no additional work if
// reserve is called twice.
Flush();
Expand All @@ -228,18 +228,18 @@ public bool Reserve(int id)
else if(current.Start > id)
{
return false;
}
}
else if(current.End == id)
{
current.End--;
}
}
else if(current.Start == id)
{
current.Start++;
}
else
}
else
{
// The ID is in the middle of this interval.
// The ID is in the middle of this interval.
// We need to split the interval into two.
IntervalList rest = new IntervalList(id + 1, current.End);
current.End = id - 1;
Expand Down

0 comments on commit d2fdef3

Please sign in to comment.