System.IComparable.CompareTo(TPriority)

Here are the examples of the csharp api System.IComparable.CompareTo(TPriority) taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

6 Examples 7

19 Source : StablePriorityQueue.cs
with MIT License
from audinowho

private bool IsLarger(int index1, int index2)
        {
            var item1 = this.data[index1];
            var item2 = this.data[index2];
            return item1.Priority.CompareTo(item2.Priority) > 0 || (item1.Priority.CompareTo(item2.Priority) == 0 && item1.InsertOrder > item2.InsertOrder);
        }

19 Source : BoundedPriorityList.cs
with GNU Lesser General Public License v3.0
from Edgar077

[MethodImpl(MethodImplOptions.AggressiveInlining)]
        public void Add(TElement item, TPriority priority)
        {
            if (this.Count >= this.Capacity)
            {
                if (this.priorityList[this.priorityList.Count - 1].CompareTo(priority) < 0)
                {
                    return;
                }

                var index = this.priorityList.BinarySearch(priority);
                index = index >= 0 ? index : ~index;

                this.priorityList.Insert(index, priority);
                this.elementList.Insert(index, item);

                this.priorityList.RemoveAt(this.priorityList.Count - 1);
                this.elementList.RemoveAt(this.elementList.Count - 1);
            }
            else
            {
                var index = this.priorityList.BinarySearch(priority);
                index = index >= 0 ? index : ~index;

                this.priorityList.Insert(index, priority);
                this.elementList.Insert(index, item);
            }
        }

19 Source : PriorityQueue.cs
with MIT License
from lidgren

public void Enqueue(TPriority prio, replacedem item)
		{
			var entries = m_entries;

			var cnt = m_count;
			if (cnt >= entries.Length)
			{
				Array.Resize(ref m_entries, cnt * 2);
				entries = m_entries;
			}

			Entry entry;
			entry.Prio = prio;
			entry.Item = item;

			entries[cnt] = entry;
			int idx = cnt;
			cnt++;
			m_count = cnt;

			// bubble up
			while (idx > 0)
			{
				int parentIndex = (idx - 1) >> 1;
				ref readonly var parent = ref entries[parentIndex];
				if (parent.Prio.CompareTo(prio) <= 0)
					break;
				entries[idx] = parent;
				idx = parentIndex;
			}

			if (cnt > 0)
				entries[idx] = entry;
		}

19 Source : PriorityQueue.cs
with MIT License
from lidgren

public bool TryDequeue(out replacedem retval, TPriority threshold)
		{
			if (m_count == 0)
			{
				retval = default;
				return false;
			}

			ref readonly var peek = ref m_entries[0];
			if (peek.Prio.CompareTo(threshold) > 0)
			{
				retval = default;
				return false;
			}

			return TryDequeue(out retval);
		}

19 Source : PriorityQueue.cs
with MIT License
from lidgren

public bool TryDequeue(out replacedem retval)
		{
			if (m_count == 0)
			{
				retval = default;
				return false;
			}

			retval = m_entries[0].Item;

			int cnt = m_count - 1;
			var replacement = m_entries[cnt];
			m_count = cnt;
			if (cnt == 0)
				return true;

			// pull up
			int index = 0;
			for (; ; )
			{
				var leftChildIndex = index * 2 + 1;
				if (leftChildIndex >= cnt)
					break;
				int rightChildIndex = index * 2 + 2;
				int swapIndex = rightChildIndex < cnt && m_entries[rightChildIndex].Prio.CompareTo(m_entries[leftChildIndex].Prio) < 0 ? rightChildIndex : leftChildIndex;

				ref readonly var cmp = ref m_entries[swapIndex];
				if (cmp.Prio.CompareTo(replacement.Prio) >= 0)
					break;

				m_entries[index] = cmp;
				index = swapIndex;
			}

			m_entries[index] = replacement;
			return true;
		}

19 Source : PriorityQueue.cs
with MIT License
from lidgren

public bool Remove(in replacedem item)
		{
			var index = GereplacedemIndex(item);
			if (index == -1)
				return false;

			int cnt = m_count - 1;
			m_count = cnt;
			if (index == cnt)
				return true; // just drop it off the end

			if (cnt == 1)
			{
				// we removed top item
				CoreException.replacedert(index == 0);
				m_entries[0] = m_entries[1];
				return true;
			}

			var replacement = m_entries[cnt];

			while (index > 0)
			{
				// check parent for upwards travelling
				var parentIndex = (index - 1) / 2;
				ref readonly var parentEntry = ref m_entries[parentIndex];
				if (replacement.Prio.CompareTo(parentEntry.Prio) < 0)
				{
					// need to swap up
					m_entries[index] = parentEntry;
					index = parentIndex;
					continue; // loop back; may need to travel higher yet
				}
				break;
			}

			// ok, check children
			for (; ; )
			{
				var leftChildIndex = index * 2 + 1;
				if (leftChildIndex >= cnt)
					break;
				int rightChildIndex = index * 2 + 2;

				ref readonly var leftChildEntry = ref m_entries[leftChildIndex];
				if (rightChildIndex < cnt)
				{
					ref readonly var rightChildEntry = ref m_entries[rightChildIndex];
					if (leftChildEntry.Prio.CompareTo(rightChildEntry.Prio) >= 0)
					{
						// right child is smaller
						if (rightChildEntry.Prio.CompareTo(replacement.Prio) < 0)
						{
							m_entries[index] = rightChildEntry;
							index = rightChildIndex;
							continue;
						}
						break;
					}
				}

				// left child is smaller
				if (leftChildEntry.Prio.CompareTo(replacement.Prio) < 0)
				{
					m_entries[index] = leftChildEntry;
					index = leftChildIndex;
					continue;
				}
				break;
			}

			m_entries[index] = replacement;
			return true;
		}