namespace Holo.Virtual.Rooms.Pathfinding { using System; using System.Collections; using System.Reflection; using System.Runtime.CompilerServices; public class Heap : IList, ICollection, IEnumerable, ICloneable { private bool FAddDuplicates; private IComparer FComparer; private ArrayList FList; private bool FUseObjectsComparison; public Heap() { this.InitProperties(null, 0); } public Heap(IComparer Comparer) { this.InitProperties(Comparer, 0); } public Heap(int Capacity) { this.InitProperties(null, Capacity); } public Heap(IComparer Comparer, int Capacity) { this.InitProperties(Comparer, Capacity); } public int Add(object O) { int num = -1; if (!this.ObjectIsCompliant(O)) { return num; } int index = this.IndexOf(O); int num3 = (index >= 0) ? index : (-index - 1); if (num3 >= this.Count) { this.FList.Add(O); } else { this.FList.Insert(num3, O); } return num3; } public void AddRange(ICollection C) { foreach (object obj2 in C) { this.Add(obj2); } } public void Clear() { this.FList.Clear(); } public object Clone() { return new Heap(this.FComparer, this.FList.Capacity) { FList = (ArrayList) this.FList.Clone(), FAddDuplicates = this.FAddDuplicates }; } public bool Contains(object O) { return (this.FList.BinarySearch(O, this.FComparer) >= 0); } public void CopyTo(Array array, int arrayIndex) { this.FList.CopyTo(array, arrayIndex); } public override bool Equals(object Object) { Heap heap = (Heap) Object; if (heap.Count != this.Count) { return false; } for (int i = 0; i < this.Count; i++) { if (!heap[i].Equals(this[i])) { return false; } } return true; } public IEnumerator GetEnumerator() { return this.FList.GetEnumerator(); } public override int GetHashCode() { return this.FList.GetHashCode(); } public int IndexOf(object O) { int num = -1; num = this.FList.BinarySearch(O, this.FComparer); while ((num > 0) && this.FList[num - 1].Equals(O)) { num--; } return num; } public int IndexOf(object Object, Equality AreEqual) { for (int i = 0; i < this.FList.Count; i++) { if (AreEqual(this.FList[i], Object)) { return i; } } return -1; } public int IndexOf(object Object, int Start) { int num = -1; num = this.FList.BinarySearch(Start, this.FList.Count - Start, Object, this.FComparer); while ((num > Start) && this.FList[num - 1].Equals(Object)) { num--; } return num; } public int IndexOf(object Object, int Start, Equality AreEqual) { if ((Start < 0) || (Start >= this.FList.Count)) { throw new ArgumentException("Start index must belong to [0; Count-1]."); } for (int i = Start; i < this.FList.Count; i++) { if (AreEqual(this.FList[i], Object)) { return i; } } return -1; } public int IndexOfMax() { int num = -1; if (this.FList.Count > 0) { num = this.FList.Count - 1; object obj1 = this.FList[this.FList.Count - 1]; } return num; } public int IndexOfMin() { int num = -1; if (this.FList.Count > 0) { num = 0; object obj1 = this.FList[0]; } return num; } private void InitProperties(IComparer Comparer, int Capacity) { if (Comparer != null) { this.FComparer = Comparer; this.FUseObjectsComparison = false; } else { this.FComparer = new Comparison(); this.FUseObjectsComparison = true; } this.FList = (Capacity > 0) ? new ArrayList(Capacity) : new ArrayList(); this.FAddDuplicates = true; } public void Insert(int Index, object O) { throw new InvalidOperationException("Insert method cannot be called on a Heap."); } public void InsertRange(int Index, ICollection C) { throw new InvalidOperationException("Insert cannot be called on a Heap."); } public void LimitOccurrences(object Value, int NumberToKeep) { if (Value == null) { throw new ArgumentNullException("Value"); } int start = 0; while ((start = this.IndexOf(Value, start)) >= 0) { if (NumberToKeep <= 0) { this.FList.RemoveAt(start); } else { start++; NumberToKeep--; } if (this.FComparer.Compare(this.FList[start], Value) > 0) { return; } } } private bool ObjectIsCompliant(object Object) { if (this.FUseObjectsComparison && !(Object is IComparable)) { throw new ArgumentException("The Heap is set to use the IComparable interface of objects, and the object to add does not implement the IComparable interface."); } if (!this.FAddDuplicates && this.Contains(Object)) { return false; } return true; } public object Pop() { if (this.FList.Count == 0) { throw new InvalidOperationException("The heap is empty."); } object obj2 = this.FList[this.Count - 1]; this.FList.RemoveAt(this.Count - 1); return obj2; } public int Push(object Object) { return this.Add(Object); } public void Remove(object Value) { this.FList.Remove(Value); } public void RemoveAt(int Index) { this.FList.RemoveAt(Index); } public void RemoveDuplicates() { int index = 0; while (index < (this.Count - 1)) { if (this.FComparer.Compare(this[index], this[index + 1]) == 0) { this.RemoveAt(index); } else { index++; } } } public override string ToString() { string str = "{"; for (int i = 0; i < this.FList.Count; i++) { str = str + this.FList[i].ToString() + ((i != (this.FList.Count - 1)) ? "; " : "}"); } return str; } public bool AddDuplicates { get { return this.FAddDuplicates; } set { this.FAddDuplicates = value; } } public int Capacity { get { return this.FList.Capacity; } set { this.FList.Capacity = value; } } public int Count { get { return this.FList.Count; } } public bool IsFixedSize { get { return this.FList.IsFixedSize; } } public bool IsReadOnly { get { return this.FList.IsReadOnly; } } public bool IsSynchronized { get { return this.FList.IsSynchronized; } } public object this[int Index] { get { if ((Index >= this.FList.Count) || (Index < 0)) { throw new ArgumentOutOfRangeException("Index is less than zero or Index is greater than Count."); } return this.FList[Index]; } set { throw new InvalidOperationException("[] operator cannot be used to set a value in a Heap."); } } public object SyncRoot { get { return this.FList.SyncRoot; } } private class Comparison : IComparer { public int Compare(object Object1, object Object2) { IComparable comparable = Object1 as IComparable; return comparable.CompareTo(Object2); } } public delegate bool Equality(object Object1, object Object2); } }