Here are the examples of the csharp api System.IComparable.CompareTo(T) taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
587 Examples
19
View Source File : MinHeap.cs
License : MIT License
Project Creator : 39M
License : MIT License
Project Creator : 39M
private T removeAt(int index) {
#if VALIDATE
validateHeapInternal("Remove At");
#endif
T ret = _array[index];
_count--;
if (_count == 0) {
return ret;
}
var bottom = _array[_count];
bottom.heapIndex = index;
int parentIndex = getParentIndex(index);
if (isValidIndex(parentIndex) && _array[parentIndex].CompareTo(bottom) > 0) {
bubbleUp(bottom);
} else {
bubbleDown(bottom);
}
return ret;
}
19
View Source File : MinHeap.cs
License : MIT License
Project Creator : 39M
License : MIT License
Project Creator : 39M
private void bubbleUp(T element) {
while (true) {
if (element.heapIndex == 0) {
break;
}
int parentIndex = getParentIndex(element.heapIndex);
var parent = _array[parentIndex];
if (parent.CompareTo(element) <= 0) {
break;
}
parent.heapIndex = element.heapIndex;
_array[element.heapIndex] = parent;
element.heapIndex = parentIndex;
}
_array[element.heapIndex] = element;
#if VALIDATE
validateHeapInternal("Bubble Up");
#endif
}
19
View Source File : MinHeap.cs
License : MIT License
Project Creator : 39M
License : MIT License
Project Creator : 39M
private void bubbleDown(T element) {
int elementIndex = element.heapIndex;
while (true) {
int leftIndex = getChildLeftIndex(elementIndex);
int rightIndex = getChildRightIndex(elementIndex);
T smallest = element;
int smallestIndex = elementIndex;
if (isValidIndex(leftIndex)) {
var leftChild = _array[leftIndex];
if (leftChild.CompareTo(smallest) < 0) {
smallest = leftChild;
smallestIndex = leftIndex;
}
} else {
break;
}
if (isValidIndex(rightIndex)) {
var rightChild = _array[rightIndex];
if (rightChild.CompareTo(smallest) < 0) {
smallest = rightChild;
smallestIndex = rightIndex;
}
}
if (smallestIndex == elementIndex) {
break;
}
smallest.heapIndex = elementIndex;
_array[elementIndex] = smallest;
elementIndex = smallestIndex;
}
element.heapIndex = elementIndex;
_array[elementIndex] = element;
#if VALIDATE
validateHeapInternal("Bubble Down");
#endif
}
19
View Source File : MinHeap.cs
License : MIT License
Project Creator : 39M
License : MIT License
Project Creator : 39M
private bool validateHeapInternal(string operation) {
for (int i = 0; i < _count; i++) {
if (_array[i].heapIndex != i) {
Debug.LogError("Element " + i + " had an index of " + _array[i].heapIndex + " instead, after " + operation);
return false;
}
if (i != 0) {
var parent = _array[getParentIndex(i)];
if (parent.CompareTo(_array[i]) > 0) {
Debug.LogError("Element " + i + " had an incorrect order after " + operation);
return false;
}
}
}
return true;
}
19
View Source File : PropertyValidation.cs
License : MIT License
Project Creator : actions
License : MIT License
Project Creator : actions
public static void CheckRange<T>(T propertyValue, T minValue, T maxValue, String propertyName, Type containerType, String topLevelParamName)
where T : IComparable<T>
{
if (propertyValue.CompareTo(minValue) < 0 || propertyValue.CompareTo(maxValue) > 0)
{
// paramName comes first for ArgumentOutOfRangeException.
throw new ArgumentOutOfRangeException(topLevelParamName, CommonResources.ValueTypeOutOfRange(propertyValue, propertyName, containerType.Name, minValue, maxValue));
}
}
19
View Source File : PropertyValidation.cs
License : MIT License
Project Creator : actions
License : MIT License
Project Creator : actions
private static void CheckRange<T>(T propertyValue, T minValue, T maxValue, String propertyName, String topLevelParamName)
where T : IComparable<T>
{
if (propertyValue.CompareTo(minValue) < 0 || propertyValue.CompareTo(maxValue) > 0)
{
// paramName comes first for ArgumentOutOfRangeException.
throw new ArgumentOutOfRangeException(topLevelParamName, CommonResources.VssPropertyValueOutOfRange(propertyName, propertyValue, minValue, maxValue));
}
}
19
View Source File : Numbers.cs
License : MIT License
Project Creator : ADeltaX
License : MIT License
Project Creator : ADeltaX
public static bool GreaterThan(T a, T b)
{
return a.CompareTo(b) > 0;
}
19
View Source File : Numbers.cs
License : MIT License
Project Creator : ADeltaX
License : MIT License
Project Creator : ADeltaX
public static bool GreaterThanOrEqual(T a, T b)
{
return a.CompareTo(b) >= 0;
}
19
View Source File : Numbers.cs
License : MIT License
Project Creator : ADeltaX
License : MIT License
Project Creator : ADeltaX
public static bool LessThan(T a, T b)
{
return a.CompareTo(b) < 0;
}
19
View Source File : Numbers.cs
License : MIT License
Project Creator : ADeltaX
License : MIT License
Project Creator : ADeltaX
public static bool LessThanOrEqual(T a, T b)
{
return a.CompareTo(b) <= 0;
}
19
View Source File : Numbers.cs
License : MIT License
Project Creator : ADeltaX
License : MIT License
Project Creator : ADeltaX
public static bool Equal(T a, T b)
{
return a.CompareTo(b) == 0;
}
19
View Source File : Numbers.cs
License : MIT License
Project Creator : ADeltaX
License : MIT License
Project Creator : ADeltaX
public static bool NotEqual(T a, T b)
{
return a.CompareTo(b) != 0;
}
19
View Source File : Utilities.cs
License : MIT License
Project Creator : ADeltaX
License : MIT License
Project Creator : ADeltaX
public static bool RangesOverlap<T>(T xFirst, T xLast, T yFirst, T yLast) where T : IComparable<T>
{
return !((xLast.CompareTo(yFirst) <= 0) || (xFirst.CompareTo(yLast) >= 0));
}
19
View Source File : Throw.cs
License : MIT License
Project Creator : akamsteeg
License : MIT License
Project Creator : akamsteeg
public static void WhenLessThan<T>(T value, T mustBeMoreThan, string argumentName)
where T : IComparable<T>
=> When(value.CompareTo(mustBeMoreThan) < 0, argumentName);
19
View Source File : Throw.cs
License : MIT License
Project Creator : akamsteeg
License : MIT License
Project Creator : akamsteeg
public static void WhenLessThan<T>(T value, T mustBeMoreThan, string argumentName, string message)
where T : IComparable<T>
=> When(value.CompareTo(mustBeMoreThan) < 0, argumentName, message);
19
View Source File : Throw.cs
License : MIT License
Project Creator : akamsteeg
License : MIT License
Project Creator : akamsteeg
public static void WhenMoreThan<T>(T value, T mustBeMoreThan, string argumentName)
where T : IComparable<T>
=> When(value.CompareTo(mustBeMoreThan) > 0, argumentName);
19
View Source File : Throw.cs
License : MIT License
Project Creator : akamsteeg
License : MIT License
Project Creator : akamsteeg
public static void WhenMoreThan<T>(T value, T mustBeMoreThan, string argumentName, string message)
where T : IComparable<T>
=> When(value.CompareTo(mustBeMoreThan) > 0, argumentName, message);
19
View Source File : RangeExtensions.cs
License : MIT License
Project Creator : AlFasGD
License : MIT License
Project Creator : AlFasGD
public static bool AfterInclusiveStart<T>(this Range<T> range, T value)
where T : IComparable<T>, IEquatable<T>
{
return value.CompareTo(range.Begin) >= 0;
}
19
View Source File : RangeExtensions.cs
License : MIT License
Project Creator : AlFasGD
License : MIT License
Project Creator : AlFasGD
public static bool AfterExclusiveStart<T>(this Range<T> range, T value)
where T : IComparable<T>, IEquatable<T>
{
return value.CompareTo(range.Begin) > 0;
}
19
View Source File : RangeExtensions.cs
License : MIT License
Project Creator : AlFasGD
License : MIT License
Project Creator : AlFasGD
public static bool BeforeInclusiveEnd<T>(this Range<T> range, T value)
where T : IComparable<T>, IEquatable<T>
{
return value.CompareTo(range.End) <= 0;
}
19
View Source File : SortedList.cs
License : MIT License
Project Creator : AlFasGD
License : MIT License
Project Creator : AlFasGD
public SortedList<T> RemoveDuplicates()
{
var result = new SortedList<T>(Count, comparison);
result.list.Add(list[0]);
for (int i = 1; i < Count; i++)
if (result.Maximum.CompareTo(list[i]) < 0)
result.list.Add(list[i]);
return result;
}
19
View Source File : TimingPoint.cs
License : MIT License
Project Creator : AlFasGD
License : MIT License
Project Creator : AlFasGD
public int CompareTo(TimingPoint<T> other) => TimePosition.CompareTo(other.TimePosition);
19
View Source File : RangeExtensions.cs
License : MIT License
Project Creator : AlFasGD
License : MIT License
Project Creator : AlFasGD
public static bool BeforeExclusiveEnd<T>(this Range<T> range, T value)
where T : IComparable<T>, IEquatable<T>
{
return value.CompareTo(range.End) < 0;
}
19
View Source File : SortedList.cs
License : MIT License
Project Creator : AlFasGD
License : MIT License
Project Creator : AlFasGD
private static int DefaultComparison(T left, T right) => left.CompareTo(right);
19
View Source File : Math.cs
License : Apache License 2.0
Project Creator : Algoryx
License : Apache License 2.0
Project Creator : Algoryx
public static T Clamp<T>( T value, T min, T max ) where T : IComparable<T>
{
return value.CompareTo( min ) < 0 ? min : value.CompareTo( max ) > 0 ? max : value;
}
19
View Source File : GeneratorWorker.cs
License : MIT License
Project Creator : AliTsuki
License : MIT License
Project Creator : AliTsuki
public static T Clamp<T>(this T val, T min, T max) where T : IComparable<T>
{
if(val.CompareTo(min) < 0)
{
return min;
}
else if(val.CompareTo(max) > 0)
{
return max;
}
else
{
return val;
}
}
19
View Source File : FixedBuffer.cs
License : MIT License
Project Creator : allisterb
License : MIT License
Project Creator : allisterb
public unsafe bool VectorLessThreplacedl(T value, out int index)
{
ThrowIfInvalid();
index = -1;
bool r = true;
int c = Vector<T>.Count;
int i;
Vector<T> v = new Vector<T>(value);
Vector<T> O = Vector<T>.One;
for (i = 0; i <= _Length - c; i+= c)
{
Vector<T> s = Unsafe.Read<Vector<T>>(BufferHelpers.Add<T>(_Ptr, i).ToPointer());
Vector<T> vcmp = Vector.LessThan(s, v);
if (vcmp == O)
{
continue;
}
else
{
r = false;
for (int j = 0; j < c; j++)
{
if (vcmp[j].Equals(default))
{
index = i + j;
return r;
}
}
return r;
}
}
for (; i < _Length; ++i)
{
if (Read(i).CompareTo(value) >= 0)
{
r = false;
index = i;
return r;
}
}
return r;
}
19
View Source File : ObjectExtension.cs
License : MIT License
Project Creator : AlphaYu
License : MIT License
Project Creator : AlphaYu
public static bool InRange<T>([NotNull] this T @this, T minValue, T maxValue) where T : IComparable<T>
{
return @this.CompareTo(minValue) >= 0 && @this.CompareTo(maxValue) <= 0;
}
19
View Source File : Pair.cs
License : GNU General Public License v3.0
Project Creator : andy-wood
License : GNU General Public License v3.0
Project Creator : andy-wood
public static Pair<T> newSorted(T p0, T p1)
{
bool isSorted = p0.CompareTo (p1) <= 0;
return new Pair<T> (isSorted ? p0 : p1, isSorted ? p1 : p0);
}
19
View Source File : Pair.cs
License : GNU General Public License v3.0
Project Creator : andy-wood
License : GNU General Public License v3.0
Project Creator : andy-wood
public int CompareTo(IPair<T> b)
{
return b.IsNull () ?
1 : this.Equals (b) ?
0 : _p0.CompareTo (b.p0);
}
19
View Source File : PriorityQueue.cs
License : Apache License 2.0
Project Creator : AnkiUniversal
License : Apache License 2.0
Project Creator : AnkiUniversal
public void Add(T item)
{
data.Add(item);
int childIndex = data.Count - 1;
while (childIndex > 0)
{
int parentIndex = (childIndex - 1) / 2;
if (data[childIndex].CompareTo(data[parentIndex]) >= 0)
break;
SwapParentAndChild(parentIndex, childIndex);
childIndex = parentIndex;
}
}
19
View Source File : PriorityQueue.cs
License : Apache License 2.0
Project Creator : AnkiUniversal
License : Apache License 2.0
Project Creator : AnkiUniversal
public T Poll()
{
if (IsEmpty())
return default(T);
// replacedumes pq is not empty; up to calling code
int lastIndex = data.Count - 1;
T fronreplacedem = data[0]; // fetch the front
data[0] = data[lastIndex];
data.RemoveAt(lastIndex);
--lastIndex;
int parentIndex = 0;
while (true)
{
int leftChildIndex = parentIndex * 2 + 1; // left child index of parent
if (leftChildIndex > lastIndex)
break; // no children so done
int rightChild = leftChildIndex + 1;
// if there is a rc (ci + 1), and it is smaller than left child, use the rc instead
if (rightChild <= lastIndex && data[rightChild].CompareTo(data[leftChildIndex]) < 0)
leftChildIndex = rightChild;
if (data[parentIndex].CompareTo(data[leftChildIndex]) <= 0)
break; // parent is smaller than (or equal to) smallest child so done
SwapParentAndChild(parentIndex, leftChildIndex);
parentIndex = leftChildIndex;
}
return fronreplacedem;
}
19
View Source File : PriorityQueue.cs
License : Apache License 2.0
Project Creator : AnkiUniversal
License : Apache License 2.0
Project Creator : AnkiUniversal
public bool IsConsistent()
{
// is the heap property true for all data?
if (data.Count == 0)
return true;
int li = data.Count - 1; // last index
for (int pi = 0; pi < data.Count; ++pi) // each parent index
{
int lci = 2 * pi + 1; // left child index
int rci = 2 * pi + 2; // right child index
if (lci <= li && data[pi].CompareTo(data[lci]) > 0)
return false; // if lc exists and it's greater than parent then bad.
if (rci <= li && data[pi].CompareTo(data[rci]) > 0)
return false; // check the right child too.
}
return true;
}
19
View Source File : BinaryHeap.cs
License : GNU General Public License v3.0
Project Creator : anotak
License : GNU General Public License v3.0
Project Creator : anotak
public virtual void Add(T item)
{
int index = heap.Count;
// Add to the end of the heap
heap.Add(item);
// Continue until the item is at the top
// or compares higher to the parent item
while((index > 0) && (heap[index].CompareTo(heap[ParentOf(index)]) > 0))
{
// Swap with parent item
SwapItems(index, ParentOf(index));
index = ParentOf(index);
}
}
19
View Source File : BinaryHeap.cs
License : GNU General Public License v3.0
Project Creator : anotak
License : GNU General Public License v3.0
Project Creator : anotak
public virtual void RemoveAt(int index)
{
int newindex = index;
// Replace with last item
heap[index] = heap[heap.Count - 1];
heap.RemoveAt(heap.Count - 1);
// Continue while item has at least a left child
while(LeftOf(index) < heap.Count)
{
// Right childs also available?
if(RightOf(index) < heap.Count)
{
// Compare with both childs
// NOTE: Using newindex as indexer in the second line to ensure the lowest of both is chosen
if(heap[index].CompareTo(heap[LeftOf(index)]) < 0) newindex = LeftOf(index);
if(heap[newindex].CompareTo(heap[RightOf(index)]) < 0) newindex = RightOf(index);
}
// Only left child available
else
{
// Compare with left child
if(heap[index].CompareTo(heap[LeftOf(index)]) < 0) newindex = LeftOf(index);
}
// Item should move down?
if(newindex != index)
{
// Swap the items
SwapItems(index, newindex);
index = newindex;
}
else
{
// Item is fine where it is, we're done
break;
}
}
}
19
View Source File : BaseMinMaxFiVm.cs
License : Apache License 2.0
Project Creator : AppRopio
License : Apache License 2.0
Project Creator : AppRopio
protected virtual void OnMinValueChanged()
{
Task.Run(() =>
{
var minValue = FromString(MinValueRaw);
var maxValue = FromString(MaxValueRaw);
var minCompareValue = Min.CompareTo(Max) < 0 ? Min : Max;
minCompareValue = minCompareValue.CompareTo(minValue) < 0 ? minValue : minCompareValue;
minCompareValue = minCompareValue.CompareTo(maxValue) > 0 ? maxValue : minCompareValue;
InvokeOnMainThread(() => Min = minCompareValue);
SelectedValue.MinValue = ToRawString(Min);
});
}
19
View Source File : BaseMinMaxFiVm.cs
License : Apache License 2.0
Project Creator : AppRopio
License : Apache License 2.0
Project Creator : AppRopio
protected virtual void OnMaxValueChanged()
{
Task.Run(() =>
{
var minValue = FromString(MinValueRaw);
var maxValue = FromString(MaxValueRaw);
var maxCompareValue = Max.CompareTo(Min) > 0 ? Max : Min;
maxCompareValue = maxCompareValue.CompareTo(minValue) < 0 ? minValue : maxCompareValue;
maxCompareValue = maxCompareValue.CompareTo(maxValue) > 0 ? maxValue : maxCompareValue;
InvokeOnMainThread(() => Max = maxCompareValue);
SelectedValue.MaxValue = ToRawString(Max);
});
}
19
View Source File : BaseMinMaxPciVm.cs
License : Apache License 2.0
Project Creator : AppRopio
License : Apache License 2.0
Project Creator : AppRopio
protected virtual void OnMinValueChanged()
{
Task.Run(() =>
{
var minValue = FromString(MinValueRaw);
var maxValue = FromString(MaxValueRaw);
var minCompareValue = Min.CompareTo(Max) < 0 ? Min : Max;
minCompareValue = minCompareValue.CompareTo(minValue) < 0 ? minValue : minCompareValue;
minCompareValue = minCompareValue.CompareTo(maxValue) > 0 ? maxValue : minCompareValue;
InvokeOnMainThread(() => Min = minCompareValue);
SelectedValue.MinValue = ToRawString(Min);
RaisePropertyChanged(() => SelectedValue);
});
}
19
View Source File : BaseMinMaxPciVm.cs
License : Apache License 2.0
Project Creator : AppRopio
License : Apache License 2.0
Project Creator : AppRopio
protected virtual void OnMaxValueChanged()
{
Task.Run(() =>
{
var minValue = FromString(MinValueRaw);
var maxValue = FromString(MaxValueRaw);
var maxCompareValue = Max.CompareTo(Min) > 0 ? Max : Min;
maxCompareValue = maxCompareValue.CompareTo(minValue) < 0 ? minValue : maxCompareValue;
maxCompareValue = maxCompareValue.CompareTo(maxValue) > 0 ? maxValue : maxCompareValue;
InvokeOnMainThread(() => Max = maxCompareValue);
SelectedValue.MaxValue = ToRawString(Max);
RaisePropertyChanged(() => SelectedValue);
});
}
19
View Source File : MathExtensions.cs
License : MIT License
Project Creator : Archomeda
License : MIT License
Project Creator : Archomeda
public static T Clamp<T>(this T value, T min, T max) where T : IComparable<T>
{
if (value.CompareTo(min) < 0)
return min;
else if (value.CompareTo(max) > 0)
return max;
return value;
}
19
View Source File : MathExtensions.cs
License : MIT License
Project Creator : Archomeda
License : MIT License
Project Creator : Archomeda
public static bool IsClampedIn<T>(this T value, T min, T max) where T : IComparable<T> =>
value.CompareTo(min) >= 0 && value.CompareTo(max) <= 0;
19
View Source File : Range.cs
License : MIT License
Project Creator : arcusmaximus
License : MIT License
Project Creator : arcusmaximus
public bool Contains(T point)
{
return point.CompareTo(Start) >= 0 && point.CompareTo(End) < 0;
}
19
View Source File : Range.cs
License : MIT License
Project Creator : arcusmaximus
License : MIT License
Project Creator : arcusmaximus
public bool Overlaps(Range<T> other)
{
return Start.CompareTo(other.End) < 0 && End.CompareTo(other.Start) > 0;
}
19
View Source File : Range.cs
License : MIT License
Project Creator : arcusmaximus
License : MIT License
Project Creator : arcusmaximus
public int CompareTo(Range<T> other)
{
return Start.CompareTo(other.Start);
}
19
View Source File : Range.cs
License : MIT License
Project Creator : arcusmaximus
License : MIT License
Project Creator : arcusmaximus
private static T Min(T x, T y)
{
return x.CompareTo(y) < 0 ? x : y;
}
19
View Source File : Range.cs
License : MIT License
Project Creator : arcusmaximus
License : MIT License
Project Creator : arcusmaximus
private static T Max(T x, T y)
{
return x.CompareTo(y) > 0 ? x : y;
}
19
View Source File : GuardClauseExtensions.cs
License : MIT License
Project Creator : ardalis
License : MIT License
Project Creator : ardalis
public static IEnumerable<T> OutOfRange<T>(this IGuardClause guardClause, IEnumerable<T> input, string parameterName, T rangeFrom, T rangeTo, string? message = null) where T : IComparable, IComparable<T>
{
if (rangeFrom.CompareTo(rangeTo) > 0)
{
throw new ArgumentException(message ?? $"{nameof(rangeFrom)} should be less or equal than {nameof(rangeTo)}");
}
if (input.Any(x => x.CompareTo(rangeFrom) < 0 || x.CompareTo(rangeTo) > 0))
{
if (string.IsNullOrEmpty(message))
{
throw new ArgumentOutOfRangeException(parameterName, message ?? $"Input {parameterName} had out of range item(s)");
}
throw new ArgumentOutOfRangeException(message, (Exception?)null);
}
return input;
}
19
View Source File : GuardClauseExtensions.cs
License : MIT License
Project Creator : ardalis
License : MIT License
Project Creator : ardalis
public static T OutOfRange<T>(this IGuardClause guardClause, T input, [JetBrainsInvokerParameterName] string parameterName, T rangeFrom, T rangeTo, string? message = null) where T : IComparable, IComparable<T>
{
if (rangeFrom.CompareTo(rangeTo) > 0)
{
throw new ArgumentException(message ?? $"{nameof(rangeFrom)} should be less or equal than {nameof(rangeTo)}");
}
if (input.CompareTo(rangeFrom) < 0 || input.CompareTo(rangeTo) > 0)
{
if (string.IsNullOrEmpty(message))
{
throw new ArgumentOutOfRangeException(parameterName, $"Input {parameterName} was out of range");
}
throw new ArgumentOutOfRangeException(message, (Exception?)null);
}
return input;
}
19
View Source File : MinMaxExtensions.cs
License : MIT License
Project Creator : ARKlab
License : MIT License
Project Creator : ARKlab
public static T MinWith<T>(this T first, params T[] args) where T : IComparable<T>
{
if (args?.Length > 0)
{
var argmin = args.Min();
return argmin.CompareTo(first) < 0 ? argmin : first;
}
return first;
}
19
View Source File : MinMaxExtensions.cs
License : MIT License
Project Creator : ARKlab
License : MIT License
Project Creator : ARKlab
public static T MaxWith<T>(this T first, params T[] args) where T : IComparable<T>
{
if (args?.Length > 0)
{
var argmax = args.Max();
return argmax.CompareTo(first) > 0 ? argmax : first;
}
return first;
}
See More Examples