let MinHeap self trim on empty; cleanup

httptests
UbitUmarov 2018-01-24 04:48:10 +00:00
parent e7b834874e
commit 5a246026a0
1 changed files with 80 additions and 70 deletions

View File

@ -45,8 +45,8 @@ namespace OpenSim.Framework
internal void Clear() internal void Clear()
{ {
this.index = -1; index = -1;
this.heap = null; heap = null;
} }
} }
@ -55,23 +55,26 @@ namespace OpenSim.Framework
internal T value; internal T value;
internal Handle handle; internal Handle handle;
internal HeapItem(T value, Handle handle) internal HeapItem(T _value, Handle _handle)
{ {
this.value = value; value = _value;
this.handle = handle; handle = _handle;
} }
internal void Clear() internal void Clear()
{ {
if (this.handle != null) if (handle != null)
this.handle.Clear(); {
ClearRef(); handle.Clear();
handle = null;
}
value = default(T);
} }
internal void ClearRef() internal void ClearRef()
{ {
this.value = default(T); value = default(T);
this.handle = null; handle = null;
} }
} }
@ -81,6 +84,7 @@ namespace OpenSim.Framework
private int size; private int size;
private object sync_root; private object sync_root;
private int version; private int version;
private int capacity;
private Comparison<T> comparison; private Comparison<T> comparison;
@ -90,14 +94,15 @@ namespace OpenSim.Framework
public MinHeap(int capacity, IComparer<T> comparer) : public MinHeap(int capacity, IComparer<T> comparer) :
this(capacity, new Comparison<T>(comparer.Compare)) { } this(capacity, new Comparison<T>(comparer.Compare)) { }
public MinHeap(Comparison<T> comparison) : this(DEFAULT_CAPACITY, comparison) { } public MinHeap(Comparison<T> comparison) : this(DEFAULT_CAPACITY, comparison) { }
public MinHeap(int capacity, Comparison<T> comparison) public MinHeap(int _capacity, Comparison<T> _comparison)
{ {
this.items = new HeapItem[capacity]; capacity = _capacity;
this.comparison = comparison; items = new HeapItem[capacity];
this.size = this.version = 0; comparison = _comparison;
size = version = 0;
} }
public int Count { get { return this.size; } } public int Count { get { return size; } }
public bool IsReadOnly { get { return false; } } public bool IsReadOnly { get { return false; } }
@ -108,15 +113,16 @@ namespace OpenSim.Framework
get get
{ {
Handle handle = ValidateThisHandle(key); Handle handle = ValidateThisHandle(key);
return this.items[handle.index].value; return items[handle.index].value;
} }
set set
{ {
Handle handle = ValidateThisHandle(key); Handle handle = ValidateThisHandle(key);
this.items[handle.index].value = value; int indx = handle.index;
if (!BubbleUp(handle.index)) items[indx].value = value;
BubbleDown(handle.index); if (!BubbleUp(indx))
BubbleDown(indx);
} }
} }
@ -124,9 +130,9 @@ namespace OpenSim.Framework
{ {
get get
{ {
if (this.sync_root == null) if (sync_root == null)
Interlocked.CompareExchange<object>(ref this.sync_root, new object(), null); Interlocked.CompareExchange<object>(ref sync_root, new object(), null);
return this.sync_root; return sync_root;
} }
} }
@ -152,27 +158,27 @@ namespace OpenSim.Framework
private void Set(HeapItem item, int index) private void Set(HeapItem item, int index)
{ {
this.items[index] = item; items[index] = item;
if (item.handle != null) if (item.handle != null)
item.handle.index = index; item.handle.index = index;
} }
private bool BubbleUp(int index) private bool BubbleUp(int index)
{ {
HeapItem item = this.items[index]; HeapItem item = items[index];
int current, parent; int current, parent;
for (current = index, parent = (current - 1) / 2; for (current = index, parent = (current - 1) / 2;
(current > 0) && (this.comparison(this.items[parent].value, item.value)) > 0; (current > 0) && (comparison(items[parent].value, item.value)) > 0;
current = parent, parent = (current - 1) / 2) current = parent, parent = (current - 1) / 2)
{ {
Set(this.items[parent], current); Set(items[parent], current);
} }
if (current != index) if (current != index)
{ {
Set(item, current); Set(item, current);
++this.version; ++version;
return true; return true;
} }
return false; return false;
@ -180,24 +186,24 @@ namespace OpenSim.Framework
private void BubbleDown(int index) private void BubbleDown(int index)
{ {
HeapItem item = this.items[index]; HeapItem item = items[index];
int current, child; int current, child;
for (current = index, child = (2 * current) + 1; for (current = index, child = (2 * current) + 1;
current < this.size / 2; current < size / 2;
current = child, child = (2 * current) + 1) current = child, child = (2 * current) + 1)
{ {
if ((child < this.size - 1) && this.comparison(this.items[child].value, this.items[child + 1].value) > 0) if ((child < size - 1) && comparison(items[child].value, items[child + 1].value) > 0)
++child; ++child;
if (this.comparison(this.items[child].value, item.value) >= 0) if (comparison(items[child].value, item.value) >= 0)
break; break;
Set(this.items[child], current); Set(items[child], current);
} }
if (current != index) if (current != index)
{ {
Set(item, current); Set(item, current);
++this.version; ++version;
} }
} }
@ -206,7 +212,7 @@ namespace OpenSim.Framework
Handle handle = ValidateHandle(key); Handle handle = ValidateHandle(key);
if (handle.index > -1) if (handle.index > -1)
{ {
value = this.items[handle.index].value; value = items[handle.index].value;
return true; return true;
} }
value = default(T); value = default(T);
@ -228,12 +234,12 @@ namespace OpenSim.Framework
public void Add(T value, IHandle ihandle) public void Add(T value, IHandle ihandle)
{ {
if (this.size == this.items.Length) if (size == items.Length)
{ {
int capacity = (int)((this.items.Length * 200L) / 100L); int capacity = (int)((items.Length * 200L) / 100L);
if (capacity < (this.items.Length + DEFAULT_CAPACITY)) if (capacity < (items.Length + DEFAULT_CAPACITY))
capacity = this.items.Length + DEFAULT_CAPACITY; capacity = items.Length + DEFAULT_CAPACITY;
Array.Resize<HeapItem>(ref this.items, capacity); Array.Resize<HeapItem>(ref items, capacity);
} }
Handle handle = null; Handle handle = null;
@ -245,8 +251,8 @@ namespace OpenSim.Framework
HeapItem item = new MinHeap<T>.HeapItem(value, handle); HeapItem item = new MinHeap<T>.HeapItem(value, handle);
Set(item, this.size); Set(item, size);
BubbleUp(this.size++); BubbleUp(size++);
} }
public void Add(T value) public void Add(T value)
@ -256,50 +262,54 @@ namespace OpenSim.Framework
public T Min() public T Min()
{ {
if (this.size == 0) if (size == 0)
throw new InvalidOperationException("Heap is empty"); throw new InvalidOperationException("Heap is empty");
return this.items[0].value; return items[0].value;
} }
public void Clear() public void Clear()
{ {
for (int index = 0; index < this.size; ++index) for (int index = 0; index < size; ++index)
this.items[index].Clear(); items[index].Clear();
this.size = 0; size = 0;
++this.version; if(items.Length > capacity)
items = new HeapItem[capacity];
++version;
} }
public void TrimExcess() public void TrimExcess()
{ {
int length = (int)(this.items.Length * 0.9); int length = (int)(items.Length * 0.9);
if (this.size < length) if (size < length)
Array.Resize<HeapItem>(ref this.items, Math.Min(this.size, DEFAULT_CAPACITY)); Array.Resize<HeapItem>(ref items, Math.Min(size, capacity));
} }
private void RemoveAt(int index) private void RemoveAt(int index)
{ {
if (this.size == 0) if (size == 0)
throw new InvalidOperationException("Heap is empty"); throw new InvalidOperationException("Heap is empty");
if (index >= this.size) if (index >= size)
throw new ArgumentOutOfRangeException("index"); throw new ArgumentOutOfRangeException("index");
this.items[index].Clear(); items[index].Clear();
if (--this.size > 0 && index != this.size) if (--size > 0 && index != size)
{ {
Set(this.items[this.size], index); Set(items[size], index);
this.items[this.size].ClearRef(); items[size].ClearRef();
if (!BubbleUp(index)) if (!BubbleUp(index))
BubbleDown(index); BubbleDown(index);
} }
if(size == 0 && items.Length > 4 * capacity)
items = new HeapItem[capacity];
} }
public T RemoveMin() public T RemoveMin()
{ {
if (this.size == 0) if (size == 0)
throw new InvalidOperationException("Heap is empty"); throw new InvalidOperationException("Heap is empty");
HeapItem item = this.items[0]; HeapItem item = items[0];
RemoveAt(0); RemoveAt(0);
return item.value; return item.value;
} }
@ -307,7 +317,7 @@ namespace OpenSim.Framework
public T Remove(IHandle ihandle) public T Remove(IHandle ihandle)
{ {
Handle handle = ValidateThisHandle(ihandle); Handle handle = ValidateThisHandle(ihandle);
HeapItem item = this.items[handle.index]; HeapItem item = items[handle.index];
RemoveAt(handle.index); RemoveAt(handle.index);
return item.value; return item.value;
} }
@ -317,9 +327,9 @@ namespace OpenSim.Framework
EqualityComparer<T> comparer = EqualityComparer<T>.Default; EqualityComparer<T> comparer = EqualityComparer<T>.Default;
int index; int index;
for (index = 0; index < this.size; ++index) for (index = 0; index < size; ++index)
{ {
if (comparer.Equals(this.items[index].value, value)) if (comparer.Equals(items[index].value, value))
return index; return index;
} }
return -1; return -1;
@ -356,8 +366,8 @@ namespace OpenSim.Framework
if ((length - index) < this.size) if ((length - index) < this.size)
throw new ArgumentException("Not enough space available in array starting at index"); throw new ArgumentException("Not enough space available in array starting at index");
for (int i = 0; i < this.size; ++i) for (int i = 0; i < size; ++i)
array[index + i] = this.items[i].value; array[index + i] = items[i].value;
} }
public void CopyTo(Array array, int index) public void CopyTo(Array array, int index)
@ -372,13 +382,13 @@ namespace OpenSim.Framework
int length = array.Length; int length = array.Length;
if ((index < 0) || (index > length)) if ((index < 0) || (index > length))
throw new ArgumentOutOfRangeException("index"); throw new ArgumentOutOfRangeException("index");
if ((length - index) < this.size) if ((length - index) < size)
throw new ArgumentException("Not enough space available in array starting at index"); throw new ArgumentException("Not enough space available in array starting at index");
try try
{ {
for (int i = 0; i < this.size; ++i) for (int i = 0; i < size; ++i)
array.SetValue(this.items[i].value, index + i); array.SetValue(items[i].value, index + i);
} }
catch (ArrayTypeMismatchException) catch (ArrayTypeMismatchException)
{ {
@ -388,13 +398,13 @@ namespace OpenSim.Framework
public IEnumerator<T> GetEnumerator() public IEnumerator<T> GetEnumerator()
{ {
int version = this.version; int cversion = version;
for (int index = 0; index < this.size; ++index) for (int index = 0; index < size; ++index)
{ {
if (version != this.version) if (cversion != version)
throw new InvalidOperationException("Heap was modified while enumerating"); throw new InvalidOperationException("Heap was modified while enumerating");
yield return this.items[index].value; yield return items[index].value;
} }
} }