// From an anagram point of view, a word is just a bag of // characters. So an anagram class is represented as HashBag<char> // which permits fast equality comparison -- we shall use them as // elements of hash sets or keys in hash maps. public static HashBag<char> AnagramClass(String s) { HashBag<char> anagram = new HashBag<char>(); foreach (char c in s) anagram.Add(c); return anagram; }
public HashPrimes(int endOfRange) { EndOfRange = endOfRange; Primes = new HashBag<int>(); Primes.AddAll(Enumerable.Range(2, endOfRange - 1).ToList()); //SieveLoop(); }
// From an anagram point of view, a word is just a bag of // characters. So an anagram class is represented as HashBag<char> // which permits fast equality comparison -- we shall use them as // elements of hash sets or keys in hash maps. static HashBag <char> AnagramClass(string s) { var anagram = new HashBag <char>(); foreach (char c in s) { anagram.Add(c); } return(anagram); }
public static void FindCollisions(SCG.IEnumerable<String> ss) { HashBag<int> occurrences = new HashBag<int>(); foreach (String s in ss) { TreeBag<char> tb = TreeBag(s); // HashBag<char> hb = HashBag(s); occurrences.Add(sequencedTreeBagHasher.GetHashCode(tb)); // unsequencedTreeBagHasher.GetHashCode(tb); // unsequencedHashBagHasher.GetHashCode(hb); } }
public void BagEqualsTest2() { var bag = new HashBag <int> { 3, 5, 1, 4, 6, 2 }; var l = new List <int> { 2, 5, 3, 1, 6, 4, 6, 4, 2 }; Assert.False(bag.BagEquals(l)); }
public void CtorTest5() { var l = new List <int> { 1, 2, 3, 4, 5, 6 }; var bag = new HashBag <int>(l, (x, y) => y.CompareTo(x) == 0); var expected = new[] { 6, 5, 4, 3, 2, 1 }; Assert.Equal(expected.Length, bag.Count); Assert.Equal(expected.Length, bag.Count); Assert.True(expected.All(bag.Contains)); }
public void RemoveTest1() { var bag = new HashBag <int> { 3, 5, 1, 4, 6, 2 }; Assert.Equal(6, bag.Count); Assert.True(bag.Remove(1)); Assert.Equal(5, bag.Count); Assert.False(bag.Remove(10)); Assert.Equal(5, bag.Count); }
public void UpdateOrAdd2() { ICollection <String> coll = new HashBag <String>(); // s1 and s2 are distinct objects but contain the same text: String s1 = "abc", s2 = ("def" + s1).Substring(3); Assert.IsFalse(coll.UpdateOrAdd(s1, out string old)); Assert.AreEqual(null, old); Assert.IsTrue(coll.UpdateOrAdd(s2, out old)); Assert.IsTrue(Object.ReferenceEquals(s1, old)); Assert.IsFalse(Object.ReferenceEquals(s2, old)); }
public void AddRangeTest2() { var bag = new HashBag <int>(); var l = new System.Collections.Generic.List <int> { 1, 2, 1 }; bag.AddRange(l); Assert.Equal(3, bag.Count); Assert.Equal(2, bag.GetCount(1)); Assert.Equal(1, bag.GetCount(2)); }
public void ExceptWithTest2() { var bag = new HashBag <int> { 3, 5, 1, 4, 6, 2, 3, 5, 1, 4, 6, 2 }; var l = new List <int> { 1, 3, 5 }; bag.ExceptWith(l); Assert.Equal(new[] { 3, 5, 1, 4, 4, 6, 6, 2, 2 }, bag); }
public void IntersectWithTest2() { var bag = new HashBag <int> { 3, 5, 1, 4, 6, 2, 3, 5, 1, 4, 6, 2 }; var l = new List <int> { 1, 1, 2, 3, 3, 4, 5, 7, 8, 9 }; bag.IntersectWith(l); Assert.Equal(new[] { 3, 3, 5, 1, 1, 4, 2 }, bag); }
public static void FindCollisions(SCG.IEnumerable <String> ss) { HashBag <int> occurrences = new HashBag <int>(); foreach (String s in ss) { TreeBag <char> tb = TreeBag(s); // HashBag<char> hb = HashBag(s); occurrences.Add(sequencedTreeBagHasher.GetHashCode(tb)); // unsequencedTreeBagHasher.GetHashCode(tb); // unsequencedHashBagHasher.GetHashCode(hb); } }
public void SymmetricExceptWithTest1() { var bag = new HashBag <int> { 1, 2, 3, 4 }; var l = new List <int> { 1, 2, 3, 4 }; bag.SymmetricExceptWith(l); Assert.Equal(0, bag.Count); }
public void AddAll() { hashbag.Add(3); hashbag.Add(4); hashbag.Add(4); hashbag.Add(5); hashbag.Add(4); HashBag <int> hashbag2 = new HashBag <int>(); hashbag2.AddAll(hashbag); Assert.IsTrue(IC.seteq(hashbag2, 3, 4, 4, 4, 5)); hashbag.Add(9); hashbag.AddAll(hashbag2); Assert.IsTrue(IC.seteq(hashbag2, 3, 4, 4, 4, 5)); Assert.IsTrue(IC.seteq(hashbag, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 9)); }
public void RetainAll() { HashBag <int> list2 = new HashBag <int>(); hashbag.Add(4); hashbag.Add(5); hashbag.Add(4); hashbag.Add(6); hashbag.Add(4); list2.Add(5); list2.Add(4); list2.Add(7); list2.Add(4); hashbag.RetainAll(list2); Assert.IsTrue(IC.seteq(hashbag, 4, 4, 5)); hashbag.Add(6); list2.Clear(); list2.Add(7); list2.Add(8); list2.Add(9); hashbag.RetainAll(list2); Assert.IsTrue(IC.eq(hashbag)); }
// Given a sequence of strings, return only the first member of each // anagram class. public static SCG.IEnumerable <String> FirstAnagramOnly(SCG.IEnumerable <String> ss) { HashSet <HashBag <char> > anagrams = new HashSet <HashBag <char> >(); foreach (String s in ss) { HashBag <char> anagram = AnagramClass(s); if (!anagrams.Contains(anagram)) { anagrams.Add(anagram); yield return(s); } } }
public void SymmetricExceptWithTest3() { var bag = new HashBag <int> { 1, 2, 3, 4 }; var l = new List <int> { 3, 4, 5, 6 }; bag.SymmetricExceptWith(l); Assert.Equal(4, bag.Count); Assert.Equal(new[] { 1, 2, 6, 5 }, bag); }
public void UnionWithTest3() { var bag = new HashBag <int> { 1, 2, 3, 4 }; var l = new List <int> { 5, 6 }; bag.UnionWith(l); Assert.Equal(6, bag.Count); Assert.Equal(new[] { 1, 2, 3, 4, 5, 6 }, bag); }
public static void UnindexedCollectionEvents() { ICollection <int> coll = new ArrayList <int>(); ICollection <int> bag1 = new HashBag <int>(); bag1.AddAll(new[] { 3, 2, 5, 5, 7, 7, 5, 3, 7, 7 }); // Add change handler coll.CollectionChanged += delegate(Object c) { Console.WriteLine("Collection changed"); }; // Add cleared handler coll.CollectionCleared += delegate(Object c, ClearedEventArgs args) { Console.WriteLine("Collection cleared"); }; // Add added handler coll.ItemsAdded += delegate(Object c, ItemCountEventArgs <int> args) { Console.WriteLine("Item {0} added", args.Item); }; // Add item count handler AddItemsAddedCounter(coll); AddItemsRemovedCounter(coll); coll.AddAll(bag1); coll.RemoveAll(new[] { 2, 5, 6, 3, 7, 2 }); coll.Clear(); ICollection <int> bag2 = new HashBag <int>(); // Add added handler with multiplicity bag2.ItemsAdded += delegate(Object c, ItemCountEventArgs <int> args) { Console.WriteLine("{0} copies of {1} added", args.Count, args.Item); }; bag2.AddAll(bag1); // Add removed handler with multiplicity bag2.ItemsRemoved += delegate(Object c, ItemCountEventArgs <int> args) { Console.WriteLine("{0} copies of {1} removed", args.Count, args.Item); }; bag2.RemoveAllCopies(7); }
// Event patterns on indexed collections public static void IndexedCollectionEvents() { var coll = new ArrayList <int>(); var bag = new HashBag <int>(); bag.AddAll(new[] { 3, 2, 5, 5, 7, 7, 5, 3, 7, 7 }); // Add item inserted handler coll.ItemInserted += (o, args) => Console.WriteLine($"Item {args.Item} inserted at {args.Index}"); coll.InsertAll(0, bag); // Add item removed-at handler coll.ItemRemovedAt += (o, args) => Console.WriteLine($"Item {args.Item} removed at {args.Index}"); coll.RemoveLast(); coll.RemoveFirst(); coll.RemoveAt(1); }
public static int getCategoryWithMostVotes(HashBag <int> votes) { int max = int.MinValue; int catWithMostVotes = 0; foreach (int category in votes.Keys) { int?i = votes.get(category); int ii = i ?? default(int); if (i > max) { max = ii; catWithMostVotes = category; } } return(catWithMostVotes); }
public void RemoveAll() { HashBag <int> list2 = new HashBag <int>(); hashbag.Add(4); hashbag.Add(5); hashbag.Add(6); hashbag.Add(4); hashbag.Add(5); list2.Add(5); list2.Add(4); list2.Add(7); list2.Add(4); hashbag.RemoveAll(list2); Assert.IsTrue(IC.SetEq(hashbag, 5, 6)); hashbag.Add(5); hashbag.Add(4); list2.Clear(); list2.Add(6); list2.Add(5); hashbag.RemoveAll(list2); Assert.IsTrue(IC.SetEq(hashbag, 4, 5)); list2.Clear(); list2.Add(7); list2.Add(8); list2.Add(9); hashbag.RemoveAll(list2); Assert.IsTrue(IC.SetEq(hashbag, 4, 5)); }
public static void runCaptureForOneLanguage(LangDescriptor language) { IList <string> filenames = Tool.getFilenames(language.corpusDir, language.fileRegex); IList <InputDocument> documents = Tool.load(filenames, language); foreach (string fileName in filenames) { // Examine info for this file in isolation Corpus fileCorpus = new Corpus(fileName, language); fileCorpus.train(); Console.WriteLine(fileName); // examineCorpus(corpus); ArrayListMultiMap <FeatureVectorAsObject, int> ws = getWSContextCategoryMap(fileCorpus); ArrayListMultiMap <FeatureVectorAsObject, int> hpos = getHPosContextCategoryMap(fileCorpus); // Compare with corpus minus this file string path = fileName; IList <InputDocument> others = BuffUtils.filter(documents, d => !d.fileName.Equals(path)); Corpus corpus = new Corpus(others, language); corpus.train(); // examineCorpus(corpus); ArrayListMultiMap <FeatureVectorAsObject, int> corpus_ws = getWSContextCategoryMap(corpus); ArrayListMultiMap <FeatureVectorAsObject, int> corpus_hpos = getHPosContextCategoryMap(corpus); foreach (FeatureVectorAsObject x in ws.Keys) { HashBag <int> fwsCats = getCategoriesBag(ws[x]); IList <float> fwsRatios = getCategoryRatios(fwsCats.Values); HashBag <int> wsCats = getCategoriesBag(corpus_ws[x]); IList <float> wsRatios = getCategoryRatios(wsCats.Values); // compare file predictions with corpus predictions if (!fwsRatios.SequenceEqual(wsRatios)) { Console.WriteLine(fwsRatios + " vs " + wsRatios); } HashBag <int> fhposCats = getCategoriesBag(hpos[x]); HashBag <int> hposCats = getCategoriesBag(corpus_hpos[x]); } break; } }
public override void CreateNewInstanceOfInternalC5DataStructure(C5DataStructure dataStructure) { switch (dataStructure) { case C5DataStructure.ArrayList: InternalC5DataStructure = new ArrayList <T>(); break; case C5DataStructure.LinkedList: InternalC5DataStructure = new LinkedList <T>(); break; case C5DataStructure.HashBag: InternalC5DataStructure = new HashBag <T>(); break; case C5DataStructure.TreeBag: InternalC5DataStructure = new TreeBag <T>(); break; case C5DataStructure.HashedArrayList: InternalC5DataStructure = new HashedArrayList <T>(); break; case C5DataStructure.HashedLinkedList: InternalC5DataStructure = new HashedLinkedList <T>(); break; case C5DataStructure.SortedArray: InternalC5DataStructure = new SortedArray <T>(); break; case C5DataStructure.HashSet: InternalC5DataStructure = new HashSet <T>(); break; case C5DataStructure.TreeSet: InternalC5DataStructure = new TreeSet <T>(); break; default: throw new ArgumentException("Unknown C5 Collection name"); } }
public void ContainsAll() { HashBag <int> list2 = new HashBag <int>(); Assert.IsTrue(hashbag.ContainsAll(list2)); list2.Add(4); Assert.IsFalse(hashbag.ContainsAll(list2)); hashbag.Add(4); Assert.IsTrue(hashbag.ContainsAll(list2)); hashbag.Add(5); Assert.IsTrue(hashbag.ContainsAll(list2)); list2.Add(20); Assert.IsFalse(hashbag.ContainsAll(list2)); hashbag.Add(20); Assert.IsTrue(hashbag.ContainsAll(list2)); list2.Add(4); Assert.IsFalse(hashbag.ContainsAll(list2)); hashbag.Add(4); Assert.IsTrue(hashbag.ContainsAll(list2)); }
public void RemoveAllTest1() { var bag = new HashBag <int> { 3, 5, 1, 1, 4, 6, 2, 2, 2 }; Assert.Equal(9, bag.Count); Assert.Equal(2, bag.RemoveAll(1)); Assert.Equal(7, bag.Count); Assert.Equal(0, bag.RemoveAll(10)); Assert.Equal(7, bag.Count); Assert.Equal(3, bag.RemoveAll(2)); Assert.Equal(4, bag.Count); Assert.Equal(1, bag.RemoveAll(5)); Assert.Equal(3, bag.Count); }
AnagramClasses(SCG.IEnumerable <String> ss) { IDictionary <HashBag <char>, TreeSet <String> > classes = new HashDictionary <HashBag <char>, TreeSet <String> >(); foreach (String s in ss) { HashBag <char> anagram = AnagramClass(s); if (!classes.Find(ref anagram, out TreeSet <string> anagramClass)) { classes[anagram] = anagramClass = new TreeSet <String>(); } anagramClass.Add(s); } foreach (TreeSet <String> anagramClass in classes.Values) { if (anagramClass.Count > 1) { yield return(anagramClass); } } }
public static void CharBagSet() { var text = @"three sorted streams aligned by leading masters are stored there; an integral triangle ends, and stable tables keep; being alert, they later reread the logarithm, peek at the recent center, then begin to send their reader algorithm."; var words = text.Split(' ', '\n', '\r', ';', ',', '.'); var anagrams = new HashSet <ICollection <char> >(); int count = 0; foreach (var word in words) { if (word != "") { count++; var anagram = new HashBag <char>(); anagram.AddAll(word.ToCharArray()); anagrams.Add(anagram); } } Console.WriteLine("Found {0} anagrams", count - anagrams.Count); }
// Event patterns on indexed collections public static void IndexedCollectionEvents() { IList <int> coll = new ArrayList <int>(); ICollection <int> bag = new HashBag <int>(); bag.AddAll(new int[] { 3, 2, 5, 5, 7, 7, 5, 3, 7, 7 }); // Add item inserted handler coll.ItemInserted += delegate(Object c, ItemAtEventArgs <int> args) { Console.WriteLine("Item {0} inserted at {1}", args.Item, args.Index); }; coll.InsertAll(0, bag); // Add item removed-at handler coll.ItemRemovedAt += delegate(Object c, ItemAtEventArgs <int> args) { Console.WriteLine("Item {0} removed at {1}", args.Item, args.Index); }; coll.RemoveLast(); coll.RemoveFirst(); coll.RemoveAt(1); }
/// <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); } } }
public void RemoveTest2() { var bag = new HashBag <int> { 3, 5, 1, 1, 4, 6, 2, 2, 2 }; Assert.Equal(9, bag.Count); Assert.Equal(2, bag.Remove(1, 2)); Assert.Equal(7, bag.Count); Assert.Equal(0, bag.Remove(10, 3)); Assert.Equal(7, bag.Count); Assert.Equal(2, bag.Remove(2, 2)); Assert.Equal(5, bag.Count); Assert.Equal(1, bag.Remove(5, 1)); Assert.Equal(4, bag.Count); Assert.Equal(1, bag.Remove(2, 5)); Assert.Equal(3, bag.Count); }
public static void CharBagSet() { String text = @"three sorted streams aligned by leading masters are stored there; an integral triangle ends, and stable tables keep; being alert, they later reread the logarithm, peek at the recent center, then begin to send their reader algorithm."; String[] words = text.Split(' ', '\n', '\r', ';', ',', '.'); ICollection<ICollection<char>> anagrams = new HashSet<ICollection<char>>(); int count = 0; foreach (String word in words) { if (word != "") { count++; HashBag<char> anagram = new HashBag<char>(); anagram.AddAll(word.ToCharArray()); anagrams.Add(anagram); } } Console.WriteLine("Found {0} anagrams", count - anagrams.Count); }
public static void examineCorpus(Corpus corpus) { ArrayListMultiMap <FeatureVectorAsObject, int> wsByFeatureVectorGroup = ArrayListMultiMap <FeatureVectorAsObject, int> .create(); ArrayListMultiMap <FeatureVectorAsObject, int> hposByFeatureVectorGroup = ArrayListMultiMap <FeatureVectorAsObject, int> .create(); int numContexts = corpus.featureVectors.Count; for (int i = 0; i < numContexts; i++) { int[] X = corpus.featureVectors[i]; int y1 = corpus.injectWhitespace[i]; int y2 = corpus.hpos[i]; wsByFeatureVectorGroup.Add(new FeatureVectorAsObject(X, Trainer.FEATURES_INJECT_WS), y1); hposByFeatureVectorGroup.Add(new FeatureVectorAsObject(X, Trainer.FEATURES_HPOS), y2); } IList <double> wsEntropies = new List <double>(); IList <double> hposEntropies = new List <double>(); foreach (FeatureVectorAsObject x in wsByFeatureVectorGroup.Keys) { var cats = wsByFeatureVectorGroup[x]; var cats2 = hposByFeatureVectorGroup[x]; HashBag <int> wsCats = getCategoriesBag(cats); HashBag <int> hposCats = getCategoriesBag(cats2); double wsEntropy = getNormalizedCategoryEntropy(getCategoryRatios(wsCats.Values)); double hposEntropy = getNormalizedCategoryEntropy(getCategoryRatios(hposCats.Values)); wsEntropies.Add(wsEntropy); hposEntropies.Add(hposEntropy); Console.Write("{0,130} : {1},{2} {3},{4}\n", x, wsCats, wsEntropy, hposCats, hposEntropy); } Console.WriteLine("MEAN " + BuffUtils.mean(wsEntropies)); Console.WriteLine("MEAN " + BuffUtils.mean(hposEntropies)); float contextRichness = wsEntropies.Count / (float)numContexts; // 0..1 where 1 means every token had different context Console.WriteLine("Context richness = " + contextRichness + " uniq ctxs=" + wsEntropies.Count + ", nctxs=" + numContexts); }
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> > (delegate(KeyValuePair <String, int> p1, KeyValuePair <String, int> 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); } }
public override void CountUses(Typ typ, HashBag<FullCellAddr> numberUses) { es[0].CountUses(Typ.Number, numberUses); for (int i = 1; i < es.Length; i++) { es[i].CountUses(typ, numberUses); } }
public override void CountUses(Typ typ, HashBag<FullCellAddr> numberUses) { if (typ == Typ.Number) { numberUses.Add(this.cellAddr); } }
public HashBag<FullCellAddr> CountNumberUses() { var numberUses = new HashBag<FullCellAddr>(); foreach (ComputeCell ccell in programList) { ccell.CountUses(ccell.Type, numberUses); } return numberUses; }
public override void CountUses(Typ typ, HashBag<FullCellAddr> numberUses) { if (es.Length == 3) { es[0].CountUses(Typ.Number, numberUses); es[1].CountUses(typ, numberUses); es[2].CountUses(typ, numberUses); } }
public void CountUses(Typ typ, HashBag<FullCellAddr> numberUses) { expr.CountUses(typ, numberUses); if (evalCond != null) { evalCond.CountUses(Typ.Number, numberUses); } }
public SiftedPrimes(int endOfRange) { Primes = new HashBag<int>(); _endOfRange = endOfRange; SieveLoop(); }
public override void CountUses(Typ typ, HashBag<FullCellAddr> numberUses) { }
public override void CountUses(Typ typ, HashBag<FullCellAddr> numberUses) { for (int i = 0; i < es.Length; i++) { es[i].CountUses(GetInputTyp(i), numberUses); } }
public override void CountUses(Typ typ, HashBag<FullCellAddr> numberUses) { foreach (CGExpr e in es) { e.CountUses(Typ.Number, numberUses); } }
/// <summary> /// Update the numberUses bag with the number of number-uses of each full cell /// address in this expression. /// </summary> /// <param name="typ">The expected type of this expression</param> /// <param name="numberUses">A hashbag noting for each full cell address /// the number of times it is used as a NumberValue.</param> public abstract void CountUses(Typ typ, HashBag<FullCellAddr> numberUses);