comparerstests.cs
来自「C#写的类似于STL的集合类,首先是C#编写,可以用于.net变程.」· CS 代码 · 共 396 行 · 第 1/2 页
CS
396 行
//******************************
// Written by Peter Golde
// Copyright (c) 2004-2005, Wintellect
//
// Use and restribution of this code is subject to the license agreement
// contained in the file "License.txt" accompanying this file.
//******************************
#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
#endregion
namespace Wintellect.PowerCollections.Tests
{
/// <summary>
/// Class that doesn't implement any IComparable.
/// </summary>
class Unorderable
{
}
/// <summary>
/// Comparable that compares ints, sorting odds before evens.
/// </summary>
class OddEvenComparable : System.IComparable
{
public int val;
public OddEvenComparable(int v)
{
val = v;
}
public int CompareTo(object other)
{
int e1 = val;
int e2 = ((OddEvenComparable)other).val;
if ((e1 & 1) == 1 && (e2 & 1) == 0)
return -1;
else if ((e1 & 1) == 0 && (e2 & 1) == 1)
return 1;
else if (e1 < e2)
return -1;
else if (e1 > e2)
return 1;
else
return 0;
}
public override bool Equals(object obj)
{
if (obj is OddEvenComparable)
return CompareTo((OddEvenComparable) obj) == 0;
else
return false;
}
public override int GetHashCode()
{
return val.GetHashCode();
}
}
/// <summary>
/// Comparable that compares ints, sorting odds before evens.
/// </summary>
class GOddEvenComparable : System.IComparable<GOddEvenComparable>
{
public int val;
public GOddEvenComparable(int v)
{
val = v;
}
public int CompareTo(GOddEvenComparable other)
{
int e1 = val;
int e2 = other.val;
if ((e1 & 1) == 1 && (e2 & 1) == 0)
return -1;
else if ((e1 & 1) == 0 && (e2 & 1) == 1)
return 1;
else if (e1 < e2)
return -1;
else if (e1 > e2)
return 1;
else
return 0;
}
}
/// <summary>
/// Comparable that compares ints, equating odds with odds and evens with evrents..
/// </summary>
class GOddEvenEquatable : System.IEquatable<GOddEvenEquatable>
{
public int val;
public GOddEvenEquatable(int v)
{
val = v;
}
public bool Equals(GOddEvenEquatable other)
{
return ((this.val & 1) == (other.val & 1));
}
public override int GetHashCode()
{
return val.GetHashCode();
}
}
/// <summary>
/// Comparer that compares ints, sorting odds before evens.
/// </summary>
class OddEvenComparer : System.Collections.IComparer
{
public int Compare(object x, object y)
{
int e1 = (int)x;
int e2 = (int)y;
if ((e1 & 1) == 1 && (e2 & 1) == 0)
return -1;
else if ((e1 & 1) == 0 && (e2 & 1) == 1)
return 1;
else if (e1 < e2)
return -1;
else if (e1 > e2)
return 1;
else
return 0;
}
}
/// <summary>
/// Comparer that compares ints, sorting odds before evens.
/// </summary>
class GOddEvenComparer : System.Collections.Generic.IComparer<int>
{
public int Compare(int e1, int e2)
{
if ((e1 & 1) == 1 && (e2 & 1) == 0)
return -1;
else if ((e1 & 1) == 0 && (e2 & 1) == 1)
return 1;
else if (e1 < e2)
return -1;
else if (e1 > e2)
return 1;
else
return 0;
}
public override bool Equals(object obj)
{
return (obj is GOddEvenComparer);
}
public override int GetHashCode()
{
return 123569;
}
}
/// <summary>
/// Comparer that compares ints, sorting odds before evens.
/// </summary>
class GOddEvenEqualityComparer : System.Collections.Generic.IEqualityComparer<int>
{
public bool Equals(int e1, int e2)
{
return ((e1 & 1) == (e2 & 1));
}
public int GetHashCode(int i)
{
return i;
}
public override bool Equals(object obj)
{
return (obj is GOddEvenComparer);
}
public override int GetHashCode()
{
return 123569;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?