public void Test() { Assert.IsTrue(IC.seteq(list.UniqueItems())); Assert.IsTrue(IC.seteq(list.ItemMultiplicities())); list.AddAll(new int[] { 7, 9, 7 }); Assert.IsTrue(IC.seteq(list.UniqueItems(), 7, 9)); Assert.IsTrue(IC.seteq(list.ItemMultiplicities(), 7, 2, 9, 1)); }
static void PrintMostCommon(int maxWords, String filename) { ICollection <String> wordbag = new HashBag <String>(); Regex delim = new Regex("[^a-zA-Z0-9]+"); using (TextReader rd = new StreamReader(filename)) { for (String line = rd.ReadLine(); line != null; line = rd.ReadLine()) { foreach (String s in delim.Split(line)) { if (s != "") { wordbag.Add(s); } } } } KeyValuePair <String, int>[] frequency = wordbag.ItemMultiplicities().ToArray(); // Sorting.IntroSort(frequency, 0, frequency.Length, new FreqOrder()); Sorting.IntroSort(frequency, 0, frequency.Length, new DelegateComparer <KeyValuePair <String, int> > ((p1, p2) => { int major = p2.Value.CompareTo(p1.Value); return(major != 0 ? major : p1.Key.CompareTo(p2.Key)); })); int stop = Math.Min(frequency.Length, maxWords); for (int i = 0; i < stop; i++) { KeyValuePair <String, int> p = frequency[i]; Console.WriteLine("{0,4} occurrences of {1}", p.Value, p.Key); } }
/// <summary> /// Insert code to unwrap the computed value of a cell, if the cell /// has type Value but is referred to as a Number more than once. /// Also register the unwrapped version of the variable /// in the NumberVariables dictionary. /// CodeGenerate.Initialize(ilg) must be called first. /// </summary> public void CreateUnwrappedNumberCells() { HashBag <FullCellAddr> numberUses = CountNumberUses(); foreach (KeyValuePair <FullCellAddr, int> numberUseCount in numberUses.ItemMultiplicities()) { FullCellAddr fca = numberUseCount.Key; if (numberUseCount.Value >= 2 && addressToVariable[fca].Type == Typ.Value) { Variable numberVar = new LocalVariable(fca + "_number", Typ.Number); ComputeCell ccell; if (fcaToComputeCell.TryGetValue(fca, out ccell)) // fca is ordinary computed cell { ccell.NumberVar = numberVar; } else // fca is an input cell { unwrapInputCells.Add(new UnwrapInputCell(addressToVariable[fca], numberVar)); } NumberVariables.Add(fca, numberVar); } } }
static void PrintMostCommon(int maxWords, string filename) { var wordbag = new HashBag <string>(); var delimiter = new Regex("[^a-zA-Z0-9]+"); using (var reader = File.OpenText(filename)) { for (var line = reader.ReadLine(); line != null; line = reader.ReadLine()) { foreach (var s in delimiter.Split(line)) { if (s != "") { wordbag.Add(s); } } } } var frequency = wordbag.ItemMultiplicities().ToArray(); Sorting.IntroSort(frequency, 0, frequency.Length, // Lexicographic ordering: decreasing frequency, then increasing string ComparerFactory <KeyValuePair <string, int> > .CreateComparer( (p1, p2) => { int major = p2.Value.CompareTo(p1.Value); return(major != 0 ? major : p1.Key.CompareTo(p2.Key)); })); var stop = Math.Min(frequency.Length, maxWords); for (var i = 0; i < stop; i++) { var p = frequency[i]; Console.WriteLine("{0,4} occurrences of {1}", p.Value, p.Key); } }