static void Main() { var musicians = new RankedSet <string> (StringComparer.InvariantCultureIgnoreCase); foreach (var m1 in new string[] { "Falco", "k.d. lang", "Madonna", "Tom Petty", "Joni Mitchell", "Grimes", "Warren Zevon" }) { musicians.Add(m1); } Console.WriteLine("Candidates:"); foreach (var item in musicians) { Console.WriteLine($" {item}"); } musicians.Remove("Falco"); musicians.RemoveWhere(IsPolynymous); musicians.RemoveRange(1, musicians.Count - 1); Console.WriteLine("\nFavorite:"); foreach (var item in musicians) { Console.WriteLine($" {item}"); } }
static void Main() { set = new RankedSet <int>() { Capacity = 4 }; Console.WriteLine("Empty tree is a single leaf:"); WriteInfo(true); Console.WriteLine("Create sequentially loaded tree of order 4:"); for (int i = 2; i <= 24; i += 2) { set.Add(i); } WriteInfo(true); Console.WriteLine("Cascade split by adding 17:"); set.Add(17); WriteInfo(); Console.WriteLine("Split a leaf by adding 3:"); set.Add(3); WriteInfo(); Console.WriteLine("Create non-rightmost branch with 2 children by adding 9:"); set.Add(9); WriteInfo(); Console.WriteLine("Cascade coalesce by removing 2:"); set.Remove(2); WriteInfo(); }
static void Main() { // Here is an example of why 'private protected' is better than 'internal'. // Kaos.Collections v4.1 and earlier it was internal and would have compiled for RxBench02. // After v4.1, as private protected it is not accessible. // Commented out assignment no longer compiles for single-assembly projects: var rs = new RankedSet <int>(); // var kc = rs.maxKeyCount; // Here is a curiosity where the BCL allows creation of an enumerator without a target class. // This default constructor is not emulated by Kaos.Collections. var strangeEnumerator = new SortedSet <int> .Enumerator(); // Another curiosity: BCL enumerators remain active after disposing. // The KaosCollections library emulates this seemingly wrong behavior. var ss = new SortedSet <int> { 3, 4, 5 }; SortedSet <int> .Enumerator ssEtor = ss.GetEnumerator(); bool ssNext1 = ssEtor.MoveNext(); ssEtor.Dispose(); bool ssNext2 = ssEtor.MoveNext(); Console.WriteLine("Dispose() then MoveNext() = " + ssNext2); Console.WriteLine("Dispose() then Current = " + ssEtor.Current); }
static void Main() { set = new RankedSet <int>() { Capacity = 5 }; Console.WriteLine("Create sequentially loaded tree of order 5:"); for (int i = 2; i <= 10; i += 2) { set.Add(i); } WriteInfo(true); Console.WriteLine("Add 12-40:"); for (int i = 12; i <= 40; i += 2) { set.Add(i); } WriteInfo(true); Console.WriteLine("Add 42:"); set.Add(42); WriteInfo(true); }
public void UnitRsc_SetEquals2() { #if TEST_BCL var cp = SortedSet <User> .CreateSetComparer(); var user1 = new SortedSet <User>(new UserComparer()); var user2 = new SortedSet <User>(new UserComparer()); #else var cp = RankedSet <User> .CreateSetComparer(); var user1 = new RankedSet <User>(new UserComparer()); var user2 = new RankedSet <User>(new UserComparer()); #endif bool eq0 = cp.Equals(user1, user2); Assert.IsTrue(eq0); user1.Add(new User("admin")); user2.Add(new User("tester")); bool eq1 = cp.Equals(user1, user2); Assert.IsFalse(eq1); user1.Add(new User("tester")); user2.Add(new User("admin")); bool eq2 = cp.Equals(user1, user2); Assert.IsTrue(eq2); }
static void Main() { var set1 = new RankedSet <int> { 3, 5, 7 }; var set2 = new RankedSet <int> { 5, 7 }; var set3 = new RankedSet <int> { 1, 9 }; var set4 = new RankedSet <int> { 5, 9 }; var arg5 = new int[] { 5, 9, 9 }; bool isSub1 = set2.IsSubsetOf(set1); bool isSub2 = set2.IsSubsetOf(set2); bool isSub3 = set4.IsSubsetOf(set2); bool isSup1 = set1.IsSupersetOf(set2); bool isSup2 = set2.IsSupersetOf(set2); bool isSup3 = set2.IsSupersetOf(set4); bool isPSub1 = set2.IsProperSubsetOf(set1); bool isPSub2 = set2.IsProperSubsetOf(set2); bool isPSup1 = set1.IsProperSupersetOf(set2); bool isPSup2 = set2.IsProperSupersetOf(set2); bool isOlap1 = set1.Overlaps(set4); bool isOlap2 = set1.Overlaps(set3); bool isEq1 = set4.SetEquals(set4); bool isEq2 = set4.SetEquals(set3); bool isEq3 = set4.SetEquals(arg5); Console.WriteLine($"{Text(set2)} IsSubsetOf {Text(set1)} = {isSub1}"); Console.WriteLine($"{Text(set2)} IsSubsetOf {Text(set2)} = {isSub2}"); Console.WriteLine($"{Text(set4)} IsSubsetOf {Text(set2)} = {isSub3}"); Console.WriteLine(); Console.WriteLine($"{Text(set1)} IsSupersetOf {Text(set2)} = {isSup1}"); Console.WriteLine($"{Text(set2)} IsSupersetOf {Text(set2)} = {isSup2}"); Console.WriteLine($"{Text(set2)} IsSupersetOf {Text(set4)} = {isSup3}"); Console.WriteLine(); Console.WriteLine($"{Text(set2)} IsProperSubsetOf {Text(set1)} = {isPSub1}"); Console.WriteLine($"{Text(set2)} IsProperSubsetOf {Text(set2)} = {isPSub2}"); Console.WriteLine(); Console.WriteLine($"{Text(set1)} IsProperSupersetOf {Text(set2)} = {isPSup1}"); Console.WriteLine($"{Text(set2)} IsProperSupersetOf {Text(set2)} = {isPSup2}"); Console.WriteLine(); Console.WriteLine($"{Text(set1)} Overlaps {Text(set4)} = {isOlap1}"); Console.WriteLine($"{Text(set1)} Overlaps {Text(set3)} = {isOlap2}"); Console.WriteLine(); Console.WriteLine($"{Text(set4)} SetEquals {Text(set4)} = {isEq1}"); Console.WriteLine($"{Text(set4)} SetEquals {Text(set3)} = {isEq2}"); Console.WriteLine($"{Text(set4)} SetEquals {Text(arg5)} = {isEq3}"); Console.WriteLine(); }
public OrderBook() { // Always want a single limit so we can have less checks in the Ask and Bid methods. Limits = new RankedSet <Limit>(new LimitPriceComparer()) { new Limit { Price = 1 * ScaleFactor } }; Subscriptions = new Dictionary <long, Subscription>(); Orders = new Dictionary <long, Order>(); }
static void Main() { int maxN = 500000; var watch = new Stopwatch(); bool isPass1 = true; Console.WriteLine("Removals in Thousands,Set Size in Thousands,RankedSet,SortedSet"); for (int n = maxN / 10; n <= maxN;) { var c1 = new RankedSet <int>(); for (int i = 0; i < n; ++i) { c1.Add(i); } watch.Reset(); watch.Start(); c1.RemoveWhere(IsCull); var c1Time = watch.ElapsedMilliseconds; int c1Count = c1.Count; c1.Clear(); var c2 = new SortedSet <int>(); for (int i = 0; i < n; ++i) { c2.Add(i); } watch.Reset(); watch.Start(); c2.RemoveWhere(IsCull); var c2Time = watch.ElapsedMilliseconds; int c2Count = c2.Count; c2.Clear(); int removes = n * division / divisionCount; if (c1Count != n - removes || c2Count != n - removes) { Console.WriteLine($"*** ERROR *** {c1Count}, {c2Count}"); } if (isPass1) { isPass1 = false; } else { Console.WriteLine($"{removes/1000},{n/1000},{c1Time},{c2Time}"); n += maxN / 10; } } }
static void Main() { var rs = new RankedSet <int> { 3, 5, 7 }; Console.WriteLine("Set items must be distinct:"); foreach (var item in rs) { Console.WriteLine(item); } var rd = new RankedDictionary <int, int> { { 3, 0 }, { 1, 11 }, { 5, 0 }, { 9, 99 } }; Console.WriteLine("\nDictionary keys must be distinct:"); foreach (var kv in rd) { Console.WriteLine(kv); } Console.WriteLine("Just the keys: { " + String.Join(",", rd.Keys) + " }"); Console.WriteLine("Just the values: { " + String.Join(",", rd.Values) + " }"); var rb = new RankedBag <int> { 1, 2, 2, 3 }; Console.WriteLine("\nBag items may repeat:"); foreach (var bx in rb) { Console.WriteLine(bx); } var rm = new RankedMap <int, int> { { 2, 0 }, { 4, 0 }, { 4, 44 }, { 6, 66 } }; Console.WriteLine("\nMap keys may repeat:"); foreach (var kv in rm) { Console.WriteLine(kv); } Console.WriteLine("Just the keys: { " + String.Join(",", rm.Keys) + " }"); Console.WriteLine("Just the values: { " + String.Join(",", rm.Values) + " }"); }
static void Main() { set = new RankedSet <int>() { Capacity = 6 }; Console.WriteLine("Create sequentially loaded tree of order 6:"); for (int i = 2; i <= 60; i += 2) { set.Add(i); } WriteInfo(true); Console.WriteLine("Create rightmost nodes with 1 key by appending 62:"); set.Add(62); WriteInfo(); Console.WriteLine("Split leaf by adding 5:"); set.Add(5); WriteInfo(); Console.WriteLine("Split leaf, cascade split branches by adding 25:"); set.Add(25); WriteInfo(); Console.WriteLine("Balance leaves, replace 2 pivots by removing 6:"); set.Remove(6); WriteInfo(); Console.WriteLine("Prune leaf by removing 62:"); set.Remove(62); WriteInfo(); Console.WriteLine("Remove 54-60:"); set.Remove(54); set.Remove(56); set.Remove(58); set.Remove(60); WriteInfo(); Console.WriteLine("Prune leaf and branch by removing 52:"); set.Remove(52); WriteInfo(); }
static void Main() { set = new RankedSet <int>() { Capacity = 5 }; Console.WriteLine("Create sequentially loaded tree of order 5:"); for (int i = 2; i <= 66; i += 2) { set.Add(i); } WriteInfo(); Console.WriteLine("Thin the tree by removing several keys:"); foreach (int i in new int[] { 4, 6, 14, 16, 18, 20, 22, 24, 26, 30, 36, 38, 46 }) { set.Remove(i); } WriteInfo(); Console.WriteLine("Coalesce leaves, balance branches by removing 12:"); set.Remove(12); WriteInfo(); Console.WriteLine("Change a branch by removing 10:"); set.Remove(10); WriteInfo(); Console.WriteLine("Change the root by removing 50:"); set.Remove(50); WriteInfo(); Console.WriteLine("Coalesce leaves by removing 40:"); set.Remove(40); WriteInfo(); Console.WriteLine("Prune rightmost leaf by removing 66:"); set.Remove(66); WriteInfo(); Console.WriteLine("Coalesce leaves and branches, prune root by removing 8:"); set.Remove(8); WriteInfo(); }
static void Main() { set = new RankedSet <int>() { Capacity = 4 }; Console.WriteLine("Create tree of order 4:"); for (int i = 2; i <= 50; i += 2) { set.Add(i); } for (int i = 3; i <= 50; i += 6) { set.Add(i); } WriteInfo(); Console.WriteLine("Add 29,35:"); set.Add(29); set.Add(35); WriteInfo(); Console.WriteLine("Remove 27:"); set.Remove(27); WriteInfo(); Console.WriteLine("Remove 21:"); set.Remove(21); WriteInfo(); Console.WriteLine("Remove 22:"); set.Remove(22); WriteInfo(); Console.WriteLine("Remove 24:"); set.Remove(24); WriteInfo(); }
static void Main() { for (int width = 1; ; ++width) { Console.Write(width + " "); for (int count = 0; count <= width; ++count) { for (int index = 0; index <= width - count; ++index) { for (int order = 4; order <= 7; ++order) { set = new RankedSet <int> { Capacity = order }; for (int ii = 0; ii < width; ++ii) { set.Add(ii); } try { set.RemoveRange(index, count); Debug.Assert(set.Count == width - count); Debug.Assert(set.Count == set.Count()); Debug.Assert(set.Count == set.Reverse().Count()); #if DEBUG set.SanityCheck(); #endif } catch (Exception) { Console.WriteLine($"\nwidth={width}, index={index}, count={count}, order={order}"); throw; } } } } } }
static void Main() { var set = new RankedSet <int> { 3, 5, 7 }; var arg = new int[] { 5, 7, 9 }; var ew = new RankedSet <int> (set); var iw = new RankedSet <int> (set); var se = new RankedSet <int> (set); var uw = new RankedSet <int> (set); ew.ExceptWith(arg); iw.IntersectWith(arg); se.SymmetricExceptWith(arg); uw.UnionWith(arg); Console.WriteLine($"{Text(set)} ExceptWith {Text(arg)} = {Text(ew)}"); Console.WriteLine($"{Text(set)} IntersectWith {Text(arg)} = {Text(iw)}"); Console.WriteLine($"{Text(set)} SymmetricExceptWith {Text(arg)} = {Text(se)}"); Console.WriteLine($"{Text(set)} UnionWith {Text(arg)} = {Text(uw)}"); }
static void Main() { for (int i = 1; i <= 12; i += 1) { var set = new RankedSet <int>() { Capacity = 4 }; foreach (int kk in new int[] { 3, 5, 6, 7, 9, 10, 11 }) { set.Add(kk); } set.Remove(6); set.Remove(10); WriteInfo(set); set.SymmetricExceptWith(new int[] { i }); Console.WriteLine("SymmetricExceptWith " + i + ":"); WriteInfo(set); Console.WriteLine("----"); } }
public void UnitRsc_Equals() { #if TEST_BCL var setComparer2 = SortedSet <string> .CreateSetComparer(); var setComparer3 = SortedSet <int> .CreateSetComparer(); #else var setComparer2 = RankedSet <string> .CreateSetComparer(); var setComparer3 = RankedSet <int> .CreateSetComparer(); #endif bool eq0 = setComparer.Equals(null); Assert.IsFalse(eq0); bool eq1 = setComparer.Equals(setComparer); Assert.IsTrue(eq1); bool eq2 = setComparer.Equals(setComparer2); Assert.IsTrue(eq2); bool eq3 = setComparer.Equals(setComparer3); Assert.IsFalse(eq3); }
static void Main() { var set1 = new RankedSet <Person> (new PersonComparer()); set1.Add(new Person("Hugh", "Mann")); set1.Add(new Person("Hammond", "Egger")); string fileName = "Persons.bin"; IFormatter formatter = new BinaryFormatter(); SerializePersons(fileName, set1, formatter); Console.WriteLine($"Wrote {set1.Count} items to file '{fileName}'."); Console.WriteLine(); RankedSet <Person> set2 = DeserializePersons(fileName, formatter); Console.WriteLine($"Read back {set2.Count} items:"); foreach (var p2 in set2) { Console.WriteLine(p2); } }
public static void SerializePersons(string fn, RankedSet <Person> set, IFormatter formatter) { using (var fs = new FileStream(fn, FileMode.Create)) { formatter.Serialize(fs, set); } }
static void Main() { var names1 = new string[] { "Falco", "Nico", "David Bowie", "Tom Petty", "Joni Mitchell", "Warren Zevon" }; var names2 = new string[] { "Michelangelo", "Rembrandt", "Joni Mitchell", "David Bowie" }; var musicians = new RankedSet <string> (names1); // Remove mononymous items. Console.WriteLine("Remove single names from the set..."); Console.WriteLine($" Count before: {musicians.Count}"); musicians.RemoveWhere(IsMononymous); Console.WriteLine($" Count after: {musicians.Count}\n"); // List names starting with 'J'. Console.WriteLine("Musicians J-T"); foreach (var name in musicians.ElementsBetween("J", "U")) { Console.WriteLine($" {name}"); } // Create another RankedSet. var painters = new RankedSet <string> (names2); // Remove elements in musicians that are also in painters. Console.WriteLine("\nRemove duplicates (of painters) from the musicians..."); Console.WriteLine($" Count before: {musicians.Count}"); musicians.ExceptWith(painters); Console.WriteLine($" Count after: {musicians.Count}\n"); Console.WriteLine("List of musicians that are not painters:"); foreach (string name in musicians) { Console.WriteLine($" {name}"); } var comp = RankedSet <string> .CreateSetComparer(); HashSet <RankedSet <string> > setOfSets = new HashSet <RankedSet <string> > (comp); setOfSets.Add(musicians); setOfSets.Add(painters); Console.WriteLine("\nAll sets in hash set:"); foreach (var set in setOfSets) { Console.WriteLine($" {set.Count} items:"); foreach (var item in set) { Console.WriteLine($" {item}"); } } // Create a 3rd RankedSet. var people = new RankedSet <string> { "Tom Petty", "Warren Zevon" }; // Create a set equality comparer. var comparer = RankedSet <string> .CreateSetComparer(); Console.WriteLine($"\nSet comparison 1: {comparer.Equals (musicians, people)}"); Console.WriteLine($"Set comparison 2: {comparer.Equals (painters, people)}"); }
static void Main() { set = new RankedSet <int>() { Capacity = 4 }; Console.WriteLine("Create sequentially loaded tree of order 4:"); for (int i = 2; i <= 44; i += 2) { set.Add(i); } WriteInfo(); Console.WriteLine("Add 1,21:"); set.Add(1); set.Add(21); WriteInfo(); Console.WriteLine("Add 9:"); set.Add(9); WriteInfo(); Console.WriteLine("Add 27:"); set.Add(27); WriteInfo(); Console.WriteLine("Remove 44:"); set.Remove(44); WriteInfo(); Console.WriteLine("Remove 40,42:"); set.Remove(40); set.Remove(42); WriteInfo(); Console.WriteLine("Remove 38:"); set.Remove(38); WriteInfo(); Console.WriteLine("Remove 34,36:"); set.Remove(34); set.Remove(36); WriteInfo(); Console.WriteLine("Remove 32:"); set.Remove(32); WriteInfo(); Console.WriteLine("Remove 28:"); set.Remove(28); WriteInfo(); Console.WriteLine("Seek 30:"); var isOk = set.Contains(30); Console.WriteLine(); Console.WriteLine("Result = " + isOk); Console.WriteLine(); Console.WriteLine("Remove 30:"); set.Remove(30); WriteInfo(); }
static void Main() { Console.WriteLine("RankedBag: "); #region RbSkip var bag = new RankedSet <int>() { 295 }; for (var i = 2; i < 500; i += 2) { bag.Add(i); } foreach (int x in bag.Skip(100).SkipWhile(i => i % 2 == 0).Skip(100)) { Console.Write($"{x} "); } // Output: 494 496 498 #endregion Console.WriteLine("\nRankedSet: "); #region RsSkip var set = new RankedSet <int>() { 295 }; for (int i = 2; i < 500; i += 2) { set.Add(i); } foreach (var x in set.Skip(100).SkipWhile(i => i % 2 == 0).Skip(100)) { Console.Write($"{x} "); } // Output: 494 496 498 #endregion Console.WriteLine("\nRankedDictionary: "); #region RdSkip var dary = new RankedDictionary <int, int>() { { 295, -295 } }; for (int i = 2; i < 500; i += 2) { dary.Add(i, -i); } foreach (var x in dary.Skip(100).SkipWhile(kv => kv.Key % 2 == 0).Skip(100)) { Console.Write($"{x} "); } // Output: [494, -494] [496, -496] [498, -498] #endregion Console.WriteLine("\nRankedDictionary.Keys: "); #region RdkSkip var daryk = new RankedMap <int, int>() { { 295, -295 } }; for (int i = 2; i < 500; i += 2) { daryk.Add(i, -i); } foreach (var x in daryk.Keys.Skip(100).SkipWhile(k => k % 2 == 0).Skip(100)) { Console.Write($"{x} "); } // Output: 494 496 498 #endregion Console.WriteLine("\nRankedDictionary.Values: "); #region RdvSkip var daryv = new RankedMap <int, int>() { { 295, -295 } }; for (int i = 2; i < 500; i += 2) { daryv.Add(i, -i); } foreach (var x in daryv.Values.Skip(100).SkipWhile(k => k % 2 == 0).Skip(100)) { Console.Write($"{x} "); } // Output: -494 -496 -498 #endregion Console.WriteLine("\nRankedMap: "); #region RmSkip var map = new RankedMap <int, int>() { { 295, -295 } }; for (int i = 2; i < 500; i += 2) { map.Add(i, -i); } foreach (var x in map.Skip(100).SkipWhile(kv => kv.Key % 2 == 0).Skip(100)) { Console.Write($"{x} "); } // Output: [494, -494] [496, -496] [498, -498] #endregion Console.WriteLine("\nRankedMap.Keys: "); #region RmkSkip var mapk = new RankedMap <int, int>() { { 295, -295 } }; for (int i = 2; i < 500; i += 2) { mapk.Add(i, -i); } foreach (var x in mapk.Keys.Skip(100).SkipWhile(k => k % 2 == 0).Skip(100)) { Console.Write($"{x} "); } // Output: 494 496 498 #endregion Console.WriteLine("\nRankedMap.Values: "); #region RmvSkip var mapv = new RankedMap <int, int>() { { 295, -295 } }; for (int i = 2; i < 500; i += 2) { mapv.Add(i, -i); } foreach (var x in mapv.Values.Skip(100).SkipWhile(k => k % 2 == 0).Skip(100)) { Console.Write($"{x} "); } // Output: -494 -496 -498 #endregion Console.WriteLine(); }