/// <summary> /// Finds the appropriate insertion point for the specified item, given that the collection is sorted according /// to the specified comparison. /// </summary> /// <param name="item">The item for which to determine the insertion point.</param> /// <param name="comparison">The comparison by which the collection is sorted.</param> /// <returns>A positive integer indicating the appropriate insertion point.</returns> /// <remarks> /// Assuming the item collection is already sorted by the specified comparison, this method /// will locate the correct insertion point for the specified item using a binary search. /// This index value can then be passed to the <see cref="Insert(int, TItem)"/> method. /// </remarks> public int FindInsertionPoint(TItem item, Comparison <TItem> comparison) { var comparer = new ComparisonComparer <TItem>(comparison); var i = _list.BinarySearch(item, comparer); return(i >= 0 ? i : ~i); }
public static void Sort(List <LP_Category> list) { var logicalComparer = new StringLogicalComparer(); var comparisonComparer = new ComparisonComparer <LP_Category>((x, y) => CategoryComparer(logicalComparer, x, y)); list.Sort(comparisonComparer); }
public void CreateAndCallComparsison() { var mocks = new Mock <IComparer <string> >(); mocks.Setup(x => x.Compare("hello", "there")).Returns(5).Verifiable(); mocks.Setup(x => x.Compare("x", "y")).Returns(-3).Verifiable(); Exception exception = new Exception(); mocks.Setup(x => x.Compare("throw", "exception")).Throws(exception).Verifiable(); Comparison <string> comparison = ComparisonComparer <string> .CreateComparison(mocks.Object); Assert.AreEqual(5, comparison("hello", "there")); Assert.AreEqual(-3, comparison("x", "y")); try { comparison("throw", "exception"); Assert.Fail("Expected exception"); } catch (Exception e) { Assert.AreSame(exception, e); } mocks.VerifyAll(); }
public void Ctor_DefaultsToAscending() { var subject = new ComparisonComparer <ComparisonSubject>( (x, y) => x.Property2.CompareTo(y.Property2)); Assert.That(subject.SortDirection, Is.EqualTo(Direction.Ascending)); }
public void CreateComparisonWithNull() { Assert.Throws <ArgumentNullException>(() => { ComparisonComparer <string> .CreateComparison(null); }); }
public void Ctor_SetsDirection() { var subject = new ComparisonComparer <ComparisonSubject>( (x, y) => x.Property2.CompareTo(y.Property2), Direction.Descending); Assert.That(subject.SortDirection, Is.EqualTo(Direction.Descending)); }
public void TestCollectionConstructorUsesCorrectComparer() { var key1 = new StrongBox <int>(1); var key2 = new StrongBox <int>(2); KeyValuePair <StrongBox <int>, int>[] pairs = { new KeyValuePair <StrongBox <int>, int>(key1, 1), new KeyValuePair <StrongBox <int>, int>(key2, 2), }; var comparer = new ComparisonComparer <StrongBox <int> >((x, y) => Comparer <int> .Default.Compare(x.Value, y.Value)); var objectDictionary = ImmutableSortedTreeDictionary.CreateRange(comparer, pairs); Assert.Same(comparer, objectDictionary.KeyComparer); Assert.Equal(2, objectDictionary.Count); Assert.Equal(new[] { new KeyValuePair <StrongBox <int>, int>(key1, 1), new KeyValuePair <StrongBox <int>, int>(key2, 2) }, objectDictionary); var stringDictionary = ImmutableSortedTreeDictionary.Create <string, int>(); Assert.Same(Comparer <string> .Default, stringDictionary.KeyComparer); stringDictionary = ImmutableSortedTreeDictionary.Create <string, int>(StringComparer.OrdinalIgnoreCase); Assert.Same(StringComparer.OrdinalIgnoreCase, stringDictionary.KeyComparer); KeyValuePair <StrongBox <int>, int>[] pairsWithDuplicateKey = { new KeyValuePair <StrongBox <int>, int>(key1, 1), new KeyValuePair <StrongBox <int>, int>(key2, 2), new KeyValuePair <StrongBox <int>, int>(key1, 3), }; Assert.Throws <ArgumentException>(() => ImmutableSortedTreeDictionary.CreateRange(comparer, pairsWithDuplicateKey)); }
public void CreateAndCallComparsison() { MockRepository mocks = new MockRepository(); IComparer <string> comparer = mocks.CreateMock <IComparer <string> >(); Expect.Call(comparer.Compare("hello", "there")).Return(5); Expect.Call(comparer.Compare("x", "y")).Return(-3); Exception exception = new Exception(); Expect.Call(comparer.Compare("throw", "exception")).Throw(exception); mocks.ReplayAll(); Comparison <string> comparison = ComparisonComparer <string> .CreateComparison(comparer); Assert.AreEqual(5, comparison("hello", "there")); Assert.AreEqual(-3, comparison("x", "y")); try { comparison("throw", "exception"); Assert.Fail("Expected exception"); } catch (Exception e) { Assert.AreSame(exception, e); } mocks.VerifyAll(); }
public void Adapts_ComparisonDelegates_ToIComparable() { Comparison <ComparisonSubject> comparison = (x, y) => x.Property2.CompareTo(y.Property2); IComparer <ComparisonSubject> comparer = new ComparisonComparer <ComparisonSubject>(comparison); Assert.That(comparer.Compare(ComparisonSubject.One, ComparisonSubject.Two), Is.LessThan(0)); Assert.That(comparison(ComparisonSubject.One, ComparisonSubject.Two), Is.LessThan(0)); }
public void Clients_DoNotHaveToCareAboutNulls() { var notNull = new ComparisonSubject("a", 1, 1m); var chainable = new ComparisonComparer <ComparisonSubject>((x, y) => x.Property2.CompareTo(y.Property2)); Assert.That(chainable.Compare(notNull, null), Is.GreaterThan(0)); Assert.That(chainable.Compare(null, notNull), Is.LessThan(0)); Assert.That(chainable.Compare(null, null), Is.EqualTo(0)); }
public void Compare_ComparedTheSelectedProperty_HonoringDirection() { Comparison <ComparisonSubject> compare2 = (x, y) => x.Property2.CompareTo(y.Property2); var subject = new ComparisonComparer <ComparisonSubject>(compare2, Direction.Ascending); Assert.That(subject.Compare(ComparisonSubject.One, ComparisonSubject.Two), Is.LessThan(0)); subject = new ComparisonComparer <ComparisonSubject>(compare2, Direction.Descending); Assert.That(subject.Compare(ComparisonSubject.One, ComparisonSubject.Two), Is.GreaterThan(0)); }
public void Compare() { var comparer = ComparisonComparer <DateTime> .Create((a, b) => a.Date.Month.CompareTo(b.Date.Month)); var x = new DateTime(2006, 10, 31); var y = new DateTime(2006, 11, 1); Assert.IsTrue(comparer.Compare(x, y) < 0); Assert.IsTrue(comparer.Compare(y, x) > 0); Assert.IsTrue(comparer.Compare(x, x) == 0); Assert.ThrowsException <ArgumentNullException>(() => ComparisonComparer <string> .Create(null)); }
public void CanBeChained() { IComparer <ComparisonSubject> by3Then2Desc = new ComparisonComparer <ComparisonSubject>((x, y) => x.Property3.CompareTo(y.Property3)) .Then(new ComparisonComparer <ComparisonSubject>((x, y) => x.Property2.CompareTo(y.Property2), Direction.Descending)); var list = new List <ComparisonSubject> { _b, _c, _a }; list.Sort(by3Then2Desc); Assert.That(list, Must.Be.RepresentableAs("B, A, C")); }
public void Comparison_HonorsDirection() { Comparison <ComparisonSubject> subject = new ComparisonComparer <ComparisonSubject>( (x, y) => x.Property2.CompareTo(y.Property2), Direction.Ascending) .Comparison; Assert.That(subject(ComparisonSubject.One, ComparisonSubject.Two), Is.EqualTo(-1)); subject = new ComparisonComparer <ComparisonSubject>( (x, y) => x.Property2.CompareTo(y.Property2), Direction.Descending) .Comparison; Assert.That(subject(ComparisonSubject.One, ComparisonSubject.Two), Is.EqualTo(1)); }
public void Composes_ComparisonDelegates() { var comparer = new ComparisonComparer <ComparisonSubject>((x, y) => x.Property3.CompareTo(y.Property3)); comparer.Then(new ComparisonComparer <ComparisonSubject>((x, y) => x.Property2.CompareTo(y.Property2), Direction.Descending)); var list = new List <ComparisonSubject> { _b, _c, _a }; list.Sort(comparer.Comparison); Assert.That(list, Must.Be.RepresentableAs("B, A, C")); }
public void TestExplicitComparer() { var objComparer = new ComparisonComparer <object>((x, y) => 0); var intComparer = new ComparisonComparer <int>((x, y) => 0); var comparableComparer = new ComparisonComparer <IComparable>((x, y) => 0); Assert.Same(objComparer, ImmutableSortedTreeList.CreateBuilder <object>(comparer: objComparer).Comparer); Assert.Same(intComparer, ImmutableSortedTreeList.CreateBuilder <int>(comparer: intComparer).Comparer); Assert.Same(comparableComparer, ImmutableSortedTreeList.CreateBuilder <IComparable>(comparer: comparableComparer).Comparer); Assert.Same(objComparer, ImmutableSortedTreeList.CreateRange <object>(comparer: objComparer, Enumerable.Empty <object>()).ToBuilder().Comparer); Assert.Same(intComparer, ImmutableSortedTreeList.CreateRange <int>(comparer: intComparer, Enumerable.Empty <int>()).ToBuilder().Comparer); Assert.Same(comparableComparer, ImmutableSortedTreeList.CreateRange <IComparable>(comparer: comparableComparer, Enumerable.Empty <IComparable>()).ToBuilder().Comparer); }
public void Explore() { Comparison <ComparisonSubject> compare2 = (x, y) => x.Property2.CompareTo(y.Property2); var subject = new ComparisonComparer <ComparisonSubject>(compare2, Direction.Ascending); Assert.That(subject.Compare(ComparisonSubject.One, ComparisonSubject.Two), Is.LessThan(0)); Assert.That(subject, Is.InstanceOf <IComparer <ComparisonSubject> >()); Assert.That(subject.Comparison, Is.InstanceOf <Comparison <ComparisonSubject> >()); IComparer <ComparisonSubject> by3Then2Desc = new ComparisonComparer <ComparisonSubject>((x, y) => x.Property3.CompareTo(y.Property3)) .Then(new ComparisonComparer <ComparisonSubject>((x, y) => x.Property2.CompareTo(y.Property2), Direction.Descending)); by3Then2Desc = new ComparisonComparer <ComparisonSubject>((x, y) => x.Property3.CompareTo(y.Property3)) .Then((x, y) => x.Property2.CompareTo(y.Property2), Direction.Descending); by3Then2Desc = Cmp <ComparisonSubject> .By((x, y) => x.Property3.CompareTo(y.Property3)) .Then((x, y) => x.Property2.CompareTo(y.Property2), Direction.Descending); }
public void TestCollectionConstructorUsesCorrectComparer() { var instance1 = new StrongBox <int>(1); var instance2 = new StrongBox <int>(2); var comparer = new ComparisonComparer <StrongBox <int> >((x, y) => Comparer <int> .Default.Compare(x.Value, y.Value)); var objectSet = ImmutableSortedTreeSet.Create(comparer, new[] { instance1, instance2, instance1 }).ToBuilder(); Assert.Same(comparer, objectSet.KeyComparer); Assert.Equal(2, objectSet.Count); Assert.Equal(new[] { instance1, instance2 }, objectSet); ImmutableSortedTreeSet <string?> .Builder stringSet = ImmutableSortedTreeSet.CreateBuilder <string?>(); Assert.Same(Comparer <string> .Default, stringSet.KeyComparer); stringSet = ImmutableSortedTreeSet.CreateBuilder(StringComparer.OrdinalIgnoreCase); Assert.Same(StringComparer.OrdinalIgnoreCase, stringSet.KeyComparer); }
public void TestInsertionLocation() { var comparer = new ComparisonComparer <StrongBox <int> >((x, y) => x.Value - y.Value); var list = new SortedTreeList <StrongBox <int> >(branchingFactor: 4, comparer: comparer); for (int i = 0; i < 1000; i++) { var value = new StrongBox <int>(Generator.GetInt32(0, 200)); int index = list.LastIndexOf(value); if (index < 0) { // No item with this value already exists index = list.FindLastIndex(box => box.Value < value.Value); } else { Assert.Equal(list[index].Value, value.Value); } if (index < list.Count - 1) { Assert.True(list[index + 1].Value > value.Value); } // The item is inserted after the previous last item with this value (or the last item less than it) list.Add(value); Assert.Same(list[index + 1], value); Assert.Equal(index + 1, list.LastIndexOf(new StrongBox <int>(value.Value))); // Check IndexOf as well int firstInstance = list.IndexOf(new StrongBox <int>(value.Value)); Assert.True(firstInstance >= 0 && firstInstance < list.Count); Assert.Equal(value.Value, list[firstInstance].Value); Assert.True(firstInstance == 0 || list[firstInstance - 1].Value < value.Value); // Check BinarySearch consistency int binarySearch = list.BinarySearch(new StrongBox <int>(value.Value)); Assert.True(binarySearch >= firstInstance && binarySearch <= index + 1); Assert.Equal(firstInstance, list.BinarySearch(0, firstInstance + 1, new StrongBox <int>(value.Value))); Assert.Equal(~firstInstance, list.BinarySearch(0, firstInstance, new StrongBox <int>(value.Value))); Assert.Equal(index + 1, list.BinarySearch(index + 1, list.Count - index - 1, new StrongBox <int>(value.Value))); Assert.Equal(~(index + 2), list.BinarySearch(index + 2, list.Count - index - 2, new StrongBox <int>(value.Value))); } }
public void TestExplicitComparer() { var objComparer = new ComparisonComparer <object>((x, y) => 0); var intComparer = new ComparisonComparer <int>((x, y) => 0); var comparableComparer = new ComparisonComparer <IComparable>((x, y) => 0); ZeroHashCodeEqualityComparer <object?> objValueComparer = ZeroHashCodeEqualityComparer <object?> .Default; Assert.Same(objComparer, ImmutableSortedTreeDictionary.Create <object, int>(keyComparer: objComparer).KeyComparer); Assert.Same(intComparer, ImmutableSortedTreeDictionary.Create <int, int>(keyComparer: intComparer).KeyComparer); Assert.Same(comparableComparer, ImmutableSortedTreeDictionary.Create <IComparable, int>(keyComparer: comparableComparer).KeyComparer); Assert.Same(objComparer, ImmutableSortedTreeDictionary.CreateRange <object, int>(keyComparer: objComparer, Enumerable.Empty <KeyValuePair <object, int> >()).KeyComparer); Assert.Same(intComparer, ImmutableSortedTreeDictionary.CreateRange <int, int>(keyComparer: intComparer, Enumerable.Empty <KeyValuePair <int, int> >()).KeyComparer); Assert.Same(comparableComparer, ImmutableSortedTreeDictionary.CreateRange <IComparable, int>(keyComparer: comparableComparer, Enumerable.Empty <KeyValuePair <IComparable, int> >()).KeyComparer); Assert.Same(Comparer <object> .Default, ImmutableSortedTreeDictionary.Create <object, object>(keyComparer: null, valueComparer: objValueComparer).KeyComparer); Assert.Same(objValueComparer, ImmutableSortedTreeDictionary.Create <object, object>(keyComparer: null, valueComparer: objValueComparer).ValueComparer); Assert.Same(Comparer <object> .Default, ImmutableSortedTreeDictionary.Create <object, object?>().Add(new object(), null).WithComparers(keyComparer: null, valueComparer: objValueComparer).KeyComparer); Assert.Same(objValueComparer, ImmutableSortedTreeDictionary.Create <object, object?>().Add(new object(), null).WithComparers(keyComparer: null, valueComparer: objValueComparer).ValueComparer); }
public void TestExplicitComparer() { var objComparer = new ComparisonComparer <object>((x, y) => 0); var intComparer = new ComparisonComparer <int>((x, y) => 0); var comparableComparer = new ComparisonComparer <IComparable>((x, y) => 0); Assert.Same(objComparer, new SortedTreeList <object>(comparer: objComparer).Comparer); Assert.Same(intComparer, new SortedTreeList <int>(comparer: intComparer).Comparer); Assert.Same(comparableComparer, new SortedTreeList <IComparable>(comparer: comparableComparer).Comparer); Assert.Same(objComparer, new SortedTreeList <object>(Enumerable.Empty <object>(), comparer: objComparer).Comparer); Assert.Same(intComparer, new SortedTreeList <int>(Enumerable.Empty <int>(), comparer: intComparer).Comparer); Assert.Same(comparableComparer, new SortedTreeList <IComparable>(Enumerable.Empty <IComparable>(), comparer: comparableComparer).Comparer); Assert.Same(objComparer, new SortedTreeList <object>(4, comparer: objComparer).Comparer); Assert.Same(intComparer, new SortedTreeList <int>(4, comparer: intComparer).Comparer); Assert.Same(comparableComparer, new SortedTreeList <IComparable>(4, comparer: comparableComparer).Comparer); Assert.Same(objComparer, new SortedTreeList <object>(4, Enumerable.Empty <object>(), comparer: objComparer).Comparer); Assert.Same(intComparer, new SortedTreeList <int>(4, Enumerable.Empty <int>(), comparer: intComparer).Comparer); Assert.Same(comparableComparer, new SortedTreeList <IComparable>(4, Enumerable.Empty <IComparable>(), comparer: comparableComparer).Comparer); }
public void TestExplicitComparer() { var objComparer = new ComparisonComparer <object>((x, y) => 0); var intComparer = new ComparisonComparer <int>((x, y) => 0); var comparableComparer = new ComparisonComparer <IComparable>((x, y) => 0); Assert.Same(objComparer, new SortedTreeDictionary <object, object>(comparer: objComparer).Comparer); Assert.Same(intComparer, new SortedTreeDictionary <int, int>(comparer: intComparer).Comparer); Assert.Same(comparableComparer, new SortedTreeDictionary <IComparable, IComparable>(comparer: comparableComparer).Comparer); Assert.Same(objComparer, new SortedTreeDictionary <object, object>(Enumerable.Empty <KeyValuePair <object, object> >(), comparer: objComparer).Comparer); Assert.Same(intComparer, new SortedTreeDictionary <int, int>(Enumerable.Empty <KeyValuePair <int, int> >(), comparer: intComparer).Comparer); Assert.Same(comparableComparer, new SortedTreeDictionary <IComparable, IComparable>(Enumerable.Empty <KeyValuePair <IComparable, IComparable> >(), comparer: comparableComparer).Comparer); Assert.Same(objComparer, new SortedTreeDictionary <object, object>(4, comparer: objComparer).Comparer); Assert.Same(intComparer, new SortedTreeDictionary <int, int>(4, comparer: intComparer).Comparer); Assert.Same(comparableComparer, new SortedTreeDictionary <IComparable, IComparable>(4, comparer: comparableComparer).Comparer); Assert.Same(objComparer, new SortedTreeDictionary <object, object>(4, Enumerable.Empty <KeyValuePair <object, object> >(), comparer: objComparer).Comparer); Assert.Same(intComparer, new SortedTreeDictionary <int, int>(4, Enumerable.Empty <KeyValuePair <int, int> >(), comparer: intComparer).Comparer); Assert.Same(comparableComparer, new SortedTreeDictionary <IComparable, IComparable>(4, Enumerable.Empty <KeyValuePair <IComparable, IComparable> >(), comparer: comparableComparer).Comparer); }
public void GetDirectories2() { string path = Path.GetTempPath(); DirectoryInfo di = new DirectoryInfo(path); ExtendedDirectoryInfo edi = new ExtendedDirectoryInfo(path); Assert.IsNotNull(edi); DirectoryInfo[] actual = edi.GetDirectories("*", SearchOption.TopDirectoryOnly); DirectoryInfo[] expected = di.GetDirectories("*", SearchOption.TopDirectoryOnly); CollectionAssert.AreEqual(expected, actual, ComparisonComparer <DirectoryInfo> .Create((a, b) => a.FullName.CompareTo(b.FullName))); foreach (DirectoryInfo d in actual) { Assert.IsTrue(Array.Exists(expected, delegate(DirectoryInfo o) { return(d.FullName == o.FullName); })); } }
public void CreateAndCall() { MockRepository mocks = new MockRepository(); Comparison<string> comparison = mocks.CreateMock<Comparison<string>>(); Expect.Call(comparison("hello", "there")).Return(5); Expect.Call(comparison("x", "y")).Return(-3); Exception exception = new Exception(); Expect.Call(comparison("throw", "exception")).Throw(exception); mocks.ReplayAll(); IComparer<string> comparer = new ComparisonComparer<string>(comparison); Assert.AreEqual(5, comparer.Compare("hello", "there")); Assert.AreEqual(-3, comparer.Compare("x", "y")); try { comparer.Compare("throw", "exception"); Assert.Fail("Expected exception"); } catch (Exception e) { Assert.AreSame(exception, e); } mocks.VerifyAll(); }
public static DataTable GetChartDataTable(String groupMember, String yMember, String xMember, IEnumerable <DataRowView> collection, bool sortData) { var logicalComparer = new StringLogicalComparer(); var comparisonComparer = new ComparisonComparer <Object>((x, y) => Compare(logicalComparer, x, y)); var dataRowViewsXQuery = (from DataRowView n in collection let v = n[xMember] select new { Grouper = v, DataRow = n }); if (sortData) { dataRowViewsXQuery = dataRowViewsXQuery.OrderBy(n => n.Grouper, comparisonComparer); } var dataRowViewsXLp = dataRowViewsXQuery.ToLookup(n => n.Grouper); var verticalColumns = dataRowViewsXLp.Select(n => n.Key).ToList(); var horizontalColumnsQuery = (from DataRowView n in collection let v = GetGrouperValue(n, groupMember, yMember) select v); if (sortData) { horizontalColumnsQuery = horizontalColumnsQuery.OrderBy(n => n, comparisonComparer); } var horizontalColumns = horizontalColumnsQuery.Distinct().ToList(); horizontalColumns.Insert(0, xMember); var dataTable = new DataTable(); foreach (var horizontalColumn in horizontalColumns) { dataTable.Columns.Add(horizontalColumn).AllowDBNull = true; } foreach (var verticalColumn in verticalColumns) { var dataRow = dataTable.NewRow(); dataRow[xMember] = Convert.ToString(verticalColumn); var dataRowViewsGroupQuery = from n in dataRowViewsXLp[verticalColumn] let v = GetGrouperValue(n.DataRow, groupMember, yMember) select new { Grouper = v, DataRow = n.DataRow }; if (sortData) { dataRowViewsGroupQuery = dataRowViewsGroupQuery.OrderBy(n => n.Grouper, comparisonComparer); } var dataRowViewsGroupLp = dataRowViewsGroupQuery.ToLookup(n => n.Grouper); foreach (var horizontalColumn in horizontalColumns) { if (horizontalColumn == xMember) { continue; } var dataRowViewsGroupGrp = dataRowViewsGroupLp[horizontalColumn]; var values = (from n in dataRowViewsGroupGrp let v = n.DataRow[yMember] let m = DataConverter.ToNullableDouble(v) where m != null select m); var dbl = values.Sum(); if (dbl != null) { dbl = Math.Round(dbl.Value, 2); } var value = String.Format("{0:0.00}", dbl); dataRow[horizontalColumn] = value; } dataTable.Rows.Add(dataRow); } return(dataTable); }
/// <summary> /// Creates a <see cref="PriorityQueue{T}"/> with the specified initial /// capacity that orders its elements according to the specified /// <paramref name="comparison"/>. /// </summary> /// <param name="initialCapacity"> /// The initial capacity for this priority queue. /// </param> /// <param name="comparison"> /// The comparison delegate used to order this priority queue. If /// <c>null</c> then the order depends on the elements' natural /// ordering. /// </param> /// <exception cref="ArgumentException"> /// If <paramref name="initialCapacity"/> is less than 1. /// </exception> public PriorityQueue(int initialCapacity, Comparison <T> comparison) : this(initialCapacity, ComparisonComparer <T> .From(comparison)) { }
public FunctorComparer([NotNull] Comparison <T> comparison) : this(ComparisonComparer.FromComparison(comparison)) { }
public void CreateComparisonWithNull() { ComparisonComparer <string> .CreateComparison(null); }