本文整理汇总了C#中IComparable.CompareTo方法的典型用法代码示例。如果您正苦于以下问题:C# IComparable.CompareTo方法的具体用法?C# IComparable.CompareTo怎么用?C# IComparable.CompareTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IComparable
的用法示例。
在下文中一共展示了IComparable.CompareTo方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ArgumentInRange
public static void ArgumentInRange(IComparable argument, IComparable minimum, IComparable maximum, string paramName = DefaultParamName)
{
if (argument.CompareTo(minimum) < 0 || argument.CompareTo(maximum) > 0)
{
throw new ArgumentException(string.Format("{0} must be between {1} and {2}.", paramName, minimum, maximum), paramName);
}
}
示例2: CompareToRange
public bool CompareToRange(IComparable target, IComparable start, IComparable end)
{
if (target == null || start == null || end == null) return false;
if (start.CompareTo(end) == 0 ) return target.CompareTo(start) == 0;
return target.CompareTo(start) >= 0 && target.CompareTo(end) <= 0;
}
示例3: ValidateMinMaxValue
internal static void ValidateMinMaxValue(this OpenType openType, IComparable defaultValue, IComparable minValue, IComparable maxValue)
{
if (minValue != null)
{
if (!openType.IsValue(minValue))
{
throw new OpenDataException("Minimum value must be valid for supplied open type.");
}
if (defaultValue != null && minValue.CompareTo(defaultValue) > 0)
{
throw new OpenDataException("Minimum value must be less or equal to default value.");
}
}
if (maxValue != null)
{
if (!openType.IsValue(maxValue))
{
throw new OpenDataException("Maximum value must be valid for supplied open type.");
}
if (defaultValue != null && defaultValue.CompareTo(maxValue) > 0)
{
throw new OpenDataException("Maximum value must be greater than or equal to default value.");
}
}
if (maxValue != null && minValue != null && minValue.CompareTo(maxValue) > 0)
{
throw new OpenDataException("Maximum value must be greater than or equal to minimum value.");
}
}
示例4: Insert
/// <summary> Insert into the tree. Does nothing if item already present.
/// </summary>
/// <param name="item">the item to insert.
/// </param>
public virtual void Insert(IComparable item)
{
current = parent = grand = header;
nullNode.element = item;
while (current.element.CompareTo(item) != 0)
{
great = grand; grand = parent; parent = current;
current = item.CompareTo(current.element) < 0?current.left:current.right;
// Check if two red children; fix if so
if (current.left.color == RED && current.right.color == RED)
handleReorient(item);
}
// Insertion fails if already present
if (current != nullNode)
return ;
current = new RedBlackNode(item, nullNode, nullNode);
// Attach to parent
if (item.CompareTo(parent.element) < 0)
parent.left = current;
else
parent.right = current;
handleReorient(item);
}
示例5: vad
public static IComparable vad(IComparable val1, IComparable val2)
{
if (val1.CompareTo(val2) < 0) return val1;
if (val1.CompareTo(val2) > 0) return val2;
if (val1.CompareTo(val2) == 0) return val1;
return null;
}
示例6: Put
/// <summary>
/// adds a new item to the tree
/// </summary>
/// <param name="newKey">the key for the new entry</param>
/// <param name="o">the new item<</param>
/// <returns>true id added successfully</returns>
public bool Put(IComparable newKey, Object o)
{
if (key == null)
{
key = newKey;
dataItem = o;
return true;
}
else if (newKey.CompareTo(key) > 0)
{
if (rightSubTree == null)
rightSubTree = new SimpleBST();
bool result = rightSubTree.Put(newKey, o);
return result;
}
else if (newKey.CompareTo(key) < 0)
{
if (leftSubTree == null)
leftSubTree = new SimpleBST();
bool result = leftSubTree.Put(newKey, o);
return result;
}
else
return false;
}
示例7: Verify
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public override bool Verify(IComparable value)
{
int fr =value.CompareTo(this.From) ;
int tr = value.CompareTo(this.To );
if (fr >= 0 && tr <= 0)
{
return true;
}
return false;
}
示例8: delete
public void delete(IComparable item)
{
ListNode currNode = list;
if (item.CompareTo(currNode.info) == 0)
list = list.next; // delete first node
else
{
while (item.CompareTo(currNode.next.info) != 0)
currNode = currNode.next; //move ahead
currNode.next = currNode.next.next; //delete node at a location ie delete current.next node
numItems--;
}
}
示例9: compare
static bool compare(IComparable a, IComparable b)
{
if (a.CompareTo(b) > 0)
{ return true; }
else
{ return false; }
}
示例10: IsAfter
/// <summary>
/// Checks that a comparable checked value is after another given value.
/// </summary>
/// <param name="checkedValue">The checked value.</param>
/// <param name="givenValue">The other given value.</param>
/// <exception cref="NFluent.FluentCheckException">The checked value is not after the given value.</exception>
public static void IsAfter(IComparable checkedValue, IComparable givenValue)
{
if (checkedValue == null || checkedValue.CompareTo(givenValue) <= 0)
{
throw new FluentCheckException(FluentMessage.BuildMessage("The {0} is not after the reference value.").On(checkedValue).And.Expected(givenValue).Comparison("after").ToString());
}
}
示例11: BetweenValidator
/// <summary>
/// Initializes a new instance of the <see cref="BetweenValidator"/> class.
/// </summary>
/// <param name="from">From.</param>
/// <param name="to">The automatic.</param>
public BetweenValidator(IComparable @from, IComparable to)
{
if (@from == null)
{
throw new ArgumentNullException("from");
}
if (to == null)
{
throw new ArgumentNullException("to");
}
if (!to.GetType().IsInstanceOfType(@from))
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "'To' value should be type of '{0}'", @from.GetType().FullName));
}
if (to.CompareTo(@from) == -1)
{
throw new ArgumentOutOfRangeException("to", "'To' should be larger than 'from'.");
}
m_From = @from;
m_To = to;
m_ValidatorProperties = new ValidatorProperties
{
{ Constants.ValidationMessageParameterNames.FROM_VALUE, FromValue },
{ Constants.ValidationMessageParameterNames.TO_VALUE, ToValue }
};
}
示例12: Compare
/// <summary>
/// The following method compares any 2 objects and test if they are equal.
/// It will also compare equality of numbers in a very special way.
/// Examples:
/// IsEqual(Int64.MaxValue, Int64.MaxValue) //is true
/// IsEqual(Int64.MaxValue, Int64.MaxValue-1) //is false
/// IsEqual(123, 123.0) //is true
/// IsEqual(654f, 654d) //is true
/// </summary>
internal static int Compare(IComparable a, IComparable b)
{
if (IsNumber(a) && IsNumber(b))
{
if (IsFloatingPoint(a) || IsFloatingPoint(b))
{
double da, db;
if (Double.TryParse(a.ToString(), out da) && Double.TryParse(b.ToString(), out db))
return da.CompareTo(db);
}
else
{
if (a.ToString().StartsWith("-") || b.ToString().StartsWith("-"))
{
var a1 = Convert.ToInt64(a);
var b1 = Convert.ToInt64(b);
return a1.CompareTo(b1);
}
else
{
var a1 = Convert.ToUInt64(a);
var b1 = Convert.ToUInt64(b);
return a1.CompareTo(b1);
}
}
}
return a.CompareTo(b);
}
示例13: newHole
private static int newHole(int hole, IComparable item, int lastIndex)
{
int left = (hole*2) + 1; //left child
int right = (hole*2) + 2; //right child
if (left > lastIndex)
// hole has no childerns
return hole;
if (left == lastIndex) // hole has left child only
if (item.CompareTo(elements[left]) < 0)
// item < left child
return left;
else
// item >= right child
return hole;
// left child < right child
if (elements[left].CompareTo(elements[right]) < 0)
if (elements[right].CompareTo(item) < 0)
// right child <= item
return hole;
else
// item < right child
return right;
else
// left child >=right child
if (elements[left].CompareTo(item) < 0)
// left child <= item
return hole;
else
// item < left child
return left;
}
示例14: InsertKeyAndValue
public override void InsertKeyAndValue(IComparable key, object value)
{
var position = GetPositionOfKey(key);
var addToExistingCollection = false;
int realPosition;
if (position >= 0)
{
addToExistingCollection = true;
realPosition = position - 1;
}
else
{
realPosition = -position - 1;
}
// If there is an element at this position and the key is different,
// then right shift, size
// safety is guaranteed by the rightShiftFrom method
if (realPosition < NbKeys && key.CompareTo(Keys[realPosition]) != 0)
RightShiftFrom(realPosition, true);
Keys[realPosition] = key;
// This is a non unique btree node, manage collection
ManageCollectionValue(realPosition, value);
if (!addToExistingCollection)
NbKeys++;
}
示例15: ArgumentAtMost
public static void ArgumentAtMost(IComparable argument, IComparable maximum, string paramName = DefaultParamName)
{
if (argument.CompareTo(maximum) > 0)
{
throw new ArgumentException(string.Format("{0} must be at most {1}.", paramName, maximum), paramName);
}
}