public OrderChildrenByTreeWalker( ITreeWalker <T> walker, CompoundComparer <T> comparers) { this._Walker = walker; this._Comparers = comparers; }
public void Serializable() { var compoundComparer = new CompoundComparer <int> (Comparer <int> .Default); var result = Serializer.SerializeAndDeserialize(compoundComparer); Assert.That(result.Compare(7, 4), Is.EqualTo(1)); }
public void NoComparer_ReturnsZero() { var compoundComparer = new CompoundComparer <int> (); Assert.That(compoundComparer.Compare(1, 2), Is.EqualTo(0)); Assert.That(compoundComparer.Compare(2, 1), Is.EqualTo(0)); Assert.That(compoundComparer.Compare(2, 2), Is.EqualTo(0)); }
public void OneComparer_Zero() { var stubComparer = MockRepository.GenerateStub <IComparer <int> > (); var compoundComparer = new CompoundComparer <int> (stubComparer); stubComparer.Stub(stub => stub.Compare(1, 2)).Return(0); Assert.That(compoundComparer.Compare(1, 2), Is.EqualTo(0)); }
public void CompoundComparerTest() { var comparer = new CompoundComparer<string>(firstLetter, length); LessThan(comparer,"appletree", "banana"); GreaterThan(comparer,"banana", "appletree"); LessThan(comparer,"pear", "pineapple"); GreaterThan(comparer,"pineapple", "pear"); EqualTo(comparer,"pear", "plum"); }
public static void Main(string[] args) { List <City> cities = CityDataImporter.LoadData(); // TODO Swap out comparers as desired // 1.To sort the list by CITY Console.WriteLine(); Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); Console.WriteLine("** THE LIST ALPHABETICALLY SORTED BY CITY **"); IComparer <City> comparer = new NameComparer(); cities.Sort(comparer); PrintCities(cities); // 2.To sort the list by STATE Console.WriteLine(); Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); Console.WriteLine("** THE LIST ALPHABETICALLY SORTED BY STATE **"); IComparer <City> scomparer = new StateComparer(); cities.Sort(scomparer); PrintCities(cities); // 3.To sort the list by POPULATION largest to smallest Console.WriteLine(); Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); Console.WriteLine("** THE LIST SORTED BY POPULATION-LARGEST TO SMALLEST **"); IComparer <City> pcomparer = new PopulationComparer(); cities.Sort(pcomparer); cities.Reverse(); PrintCities(cities); //BONUS MISSION- TO SORT FIRST BY STATE AND THEN BY POPULATION Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); Console.WriteLine("** THE LIST SORTED BY STATE AND THEN BY POPULATION **"); CompoundComparer ccomparer = new CompoundComparer(); ccomparer.Comparers.Add(new StateComparer()); ccomparer.Comparers.Add(new PopulationComparer()); cities.Sort(ccomparer); PrintCities(cities); Console.ReadLine(); }
public static void Main(string[] args) { List <City> cities = CityDataImporter.LoadData(); // TODO Swap out comparers as desired IComparer <City> comparer = new NameComparer(); cities.Sort(comparer); PrintCities(cities); Console.ReadLine(); IComparer <City> comparerState = new StateComparer(); cities.Sort(comparerState); PrintCities(cities); Console.ReadLine(); IComparer <City> comparerArea = new AreaComparer(); cities.Sort(comparerArea); PrintCities(cities); Console.ReadLine(); IComparer <City> comparerPopulation = new PopulationComparer(); cities.Sort(comparerPopulation); PrintCities(cities); Console.ReadLine(); CompoundComparer comparerCompound = new CompoundComparer(); comparerCompound.Comparers.Add(new StateComparer()); //cities.Sort(comparerCompound); comparerCompound.Comparers.Add(new PopulationComparer()); //cities.Sort(comparerCompound); cities.Sort(comparerCompound); PrintCities(cities); Console.ReadLine(); }
public static void Main(string[] args) { List <City> cities = CityDataImporter.LoadData(); // TODO Swap out comparers as desired CompoundComparer comparer = new CompoundComparer(); comparer.Comparers.Add(new StateComparer()); comparer.Comparers.Add(new PopulationComparer()); cities.Sort(comparer); PrintCities(cities); Console.ReadLine(); }
public void ManyComparers_UsesFirstNonZero() { var stubComparer1 = MockRepository.GenerateStub <IComparer <int> > (); var stubComparer2 = MockRepository.GenerateStub <IComparer <int> > (); var stubComparer3 = MockRepository.GenerateStub <IComparer <int> > (); var stubComparer4 = MockRepository.GenerateStub <IComparer <int> > (); var compoundComparer = new CompoundComparer <int> (stubComparer1, stubComparer2, stubComparer3, stubComparer4); stubComparer1.Stub(stub => stub.Compare(1, 2)).Return(0); stubComparer2.Stub(stub => stub.Compare(1, 2)).Return(0); stubComparer3.Stub(stub => stub.Compare(1, 2)).Return(1); stubComparer4.Stub(stub => stub.Compare(1, 2)).Return(-1); Assert.That(compoundComparer.Compare(1, 2), Is.EqualTo(1)); }
> ThenByImpl <TItem, TFirstKey, TComparer, TOuterEnumerable, TOuterEnumerator, TInnerEnumerable, TInnerEnumerator, TSecondKey>( ref TOuterEnumerable source, Func <TItem, TSecondKey> keySelector ) where TOuterEnumerable : struct, IStructEnumerable <TItem, TOuterEnumerator>, IHasComparer <TItem, TFirstKey, TComparer, TInnerEnumerable, TInnerEnumerator> where TOuterEnumerator : struct, IStructEnumerator <TItem> where TInnerEnumerable : struct, IStructEnumerable <TItem, TInnerEnumerator> where TInnerEnumerator : struct, IStructEnumerator <TItem> where TComparer : struct, IStructComparer <TItem, TFirstKey> { var leftComparer = source.GetComparer(); var rightComparer = new DefaultAscending <TItem, TSecondKey>(keySelector); var newComparer = new CompoundComparer <TItem, TFirstKey, TComparer, TSecondKey, DefaultAscending <TItem, TSecondKey> >(ref leftComparer, ref rightComparer); var inner = source.GetInnerEnumerable(); return(new OrderByEnumerable <TItem, CompoundKey <TFirstKey, TSecondKey>, TInnerEnumerable, TInnerEnumerator, CompoundComparer <TItem, TFirstKey, TComparer, TSecondKey, DefaultAscending <TItem, TSecondKey> > >(ref inner, ref newComparer)); }
public static void Main(string[] args) { List <City> cities = CityDataImporter.LoadData(); // TODO COMPLETE Swap out comparers as desired //IComparer<City> comparer = new NameComparer(); //IComparer<City> comparer = new AreaComparer(); //IComparer<City> comparer = new PopulationComparer(); //IComparer<City> comparer = new StateComparer(); CompoundComparer compoundComparer = new CompoundComparer(); // by decending population - second sort key // within ascending state name - first sort key compoundComparer.Comparers.Add(new StateComparer()); compoundComparer.Comparers.Add(new PopulationComparer()); cities.Sort(compoundComparer); PrintCities(cities); Console.ReadLine(); }
> CreateOrderedEnumerable <TItem, TFirstKey, TComparer, TOuterEnumerable, TOuterEnumerator, TInnerEnumerable, TInnerEnumerator, TSecondKey>( ref TOuterEnumerable source, Func <TItem, TSecondKey> keySelector, IComparer <TSecondKey> comparer, bool descending ) where TOuterEnumerable : struct, IStructEnumerable <TItem, TOuterEnumerator>, IHasComparer <TItem, TFirstKey, TComparer, TInnerEnumerable, TInnerEnumerator> where TOuterEnumerator : struct, IStructEnumerator <TItem> where TInnerEnumerable : struct, IStructEnumerable <TItem, TInnerEnumerator> where TInnerEnumerator : struct, IStructEnumerator <TItem> where TComparer : struct, IStructComparer <TItem, TFirstKey> { if (source.IsDefaultValue()) { throw CommonImplementation.Uninitialized(nameof(source)); } if (keySelector == null) { throw CommonImplementation.ArgumentNull(nameof(keySelector)); } comparer = comparer ?? Comparer <TSecondKey> .Default; var configured = new ConfiguredComparer <TItem, TSecondKey>(keySelector, comparer, descending); var first = source.GetComparer(); var compound = new CompoundComparer <TItem, TFirstKey, TComparer, TSecondKey, ConfiguredComparer <TItem, TSecondKey> >(ref first, ref configured); var inner = source.GetInnerEnumerable(); return (new OrderByEnumerable <TItem, CompoundKey <TFirstKey, TSecondKey>, TInnerEnumerable, TInnerEnumerator, CompoundComparer <TItem, TFirstKey, TComparer, TSecondKey, ConfiguredComparer <TItem, TSecondKey> > >( ref inner, ref compound )); }
/// <summary> /// Erstellt einen neuen Vergleichsalgorithmus auf Basis einer Dateibeschreibung. /// </summary> /// <param name="fileContents">Der tatsächliche Inhalt der Datei.</param> /// <param name="nameComparer">Der Algorithmus zum Vegleich von Gerätenamen.</param> /// <returns>Die gewünschte Beschreibung.</returns> internal static IComparer <SchedulePlan> Create(byte[] fileContents, IEqualityComparer <string> nameComparer) { // Validate if (fileContents == null) { throw new ArgumentNullException("fileContents"); } // Result var resourceComparer = default(CompoundComparer <ResourcePlan>); var planComparer = new CompoundComparer <SchedulePlan>(); // Create reader using (var stream = new MemoryStream(fileContents, false)) using (var reader = new StreamReader(stream, true)) for (string line; (line = reader.ReadLine()) != null;) { // Check for comment and empty line var dataLength = line.IndexOf('#'); var data = line.Substring(0, (dataLength < 0) ? line.Length : dataLength).Trim(); if (string.IsNullOrEmpty(data)) { continue; } // Check operation var parts = data.Split(':'); if (parts.Length != 2) { throw new InvalidDataException(string.Format(Properties.SchedulerResources.Exception_BadScheduleFile, data)); } // Check mode if (resourceComparer == null) { // New rule to use var rule = default(IComparer <SchedulePlan>); // Check for supported operations switch (parts[0]) { case "StartTime": planComparer.Comparers.Add(SchedulePlan.CompareByOverlappingStart(parts[1], nameComparer)); continue; case "ParallelSource": rule = SchedulePlan.CompareByParallelSourceTime; break; case "ResourceCount": rule = SchedulePlan.CompareByResourceCount; break; case "TotalCut": rule = SchedulePlan.CompareByTotalCut; break; case "ByPriority": { // Create inner resourceComparer = new CompoundComparer <ResourcePlan>(); // Check mode switch (parts[1]) { case "Ascending": rule = resourceComparer.ByPriority(false); break; case "Descending": rule = resourceComparer.ByPriority(true); break; default: throw new InvalidDataException(string.Format(Properties.SchedulerResources.Exception_UnknownOrder, parts[0], parts[1])); } // Done break; } } // Validate if (rule == null) { throw new InvalidDataException(string.Format(Properties.SchedulerResources.Exception_UnknownProperty, parts[0])); } // Process adaption if (resourceComparer == null) { switch (parts[1]) { case "Min": rule = Invert(rule); break; case "Max": break; default: throw new InvalidDataException(string.Format(Properties.SchedulerResources.Exception_UnknownOrder, parts[0], parts[1])); } } // Remember planComparer.Comparers.Add(rule); } else { // New rule to use var rule = default(IComparer <ResourcePlan>); // Check for supported operations switch (parts[0]) { case "RecordingCount": rule = ResourcePlan.CompareByRecordingCount; break; case "SourceCount": rule = ResourcePlan.CompareBySourceCount; break; case "ByPriority": { // Check mode switch (parts[1]) { case "End": resourceComparer = null; break; default: throw new InvalidDataException(Properties.SchedulerResources.Exception_ResourcesNotTerminated); } // Done break; } } // Validate if (rule != null) { // May invert switch (parts[1]) { case "Min": rule = Invert(rule); break; case "Max": break; default: throw new InvalidDataException(string.Format(Properties.SchedulerResources.Exception_UnknownOrder, parts[0], parts[1])); } // Remember resourceComparer.Comparers.Add(rule); } else if (resourceComparer != null) { throw new InvalidDataException(string.Format(Properties.SchedulerResources.Exception_UnknownProperty, parts[0])); } } } // Validate if (resourceComparer != null) { throw new InvalidDataException(Properties.SchedulerResources.Exception_ResourcesNotTerminated); } // Report return(planComparer); }