private static StringTrie <ushort> InitDictionary() { var dictionary = new StringTrie <ushort>(); dictionary.AddRange(Chars.Select((c, i) => new StringEntry <ushort>(c.ToString(), (ushort)i))); return(dictionary); }
static void TraverseTrie(StringTrie <string> trie, Action <ITrieNode <char, string> > nodeFunc, Action <ITrieNode <char, string> > leafFunc) { Enumerable.Range(Convert.ToInt32('A'), Convert.ToInt32('Z')).ToList().ForEach(e => { var c = Convert.ToChar(e); var node = trie.Find(c.ToString()); TraverseChildren(node, nodeFunc, leafFunc); }); }
static unsafe void Main(string[] args) { Console.WriteLine("Emulate opendir()"); //var buf = new byte[65536]; //var fd = LibC.open(toNullTerm(Encoding.UTF8.GetBytes("/tmp")), LibC.O_DIRECTORY|LibC.O_RDONLY); //Console.WriteLine($"Open fd={fd} errno={LibC.errno}"); //LibC.fcntl(fd, LibC.F_SETFD, LibC.FD_CLOEXEC); //Console.WriteLine($"Fcntl fd={fd} errno={LibC.errno}"); //var dh = Mono.Unix.Native.Syscall.opendir("/tmp"); //while (true) //{ // var rd = Mono.Unix.Native.Syscall.readdir(dh); // if (rd == null) break; // Console.WriteLine($"{rd.d_name}"); //} //fixed (byte* p = buf) //{ // var count = LibC.read(fd, p, 65536); // Console.WriteLine($"Read {(int) count}"); // dumpBytes(buf, (int) count); //} //var dh = RawDirs.opendir(toNullTerm(Encoding.UTF8.GetBytes("/tmp"))); //while (true) //{ // var dir = RawDirs.readdir_wrap(dh); // if (dir == null) break; // var d = dir.Value; // var name = Encoding.UTF8.GetString(d.d_name); // Console.WriteLine($"{name}"); //} //RawDirs.closedir(dh); var mt = new StringTrie <string>(); mt.Add("/a/b/", "ab#"); mt.Add("/a/c/", "ac#"); mt.Add("/b/b/", "bb#"); var a = mt.FindPredecessor("/a/b/c"); Console.WriteLine($"/a/b/c {a.Value}"); //a = mt.FindPredecessor("/a/b"); //Console.WriteLine($"/a/b {a.Value}"); }
public void AddRange() { const int count = 10; var trie = new StringTrie <bool>(); trie.AddRange(Enumerable.Range(0, count).Select(i => new StringEntry <bool>(i.ToString(), false))); Assert.Equal(count, trie.Count); }
public void Clear() { var trie = new StringTrie <bool> { { "ABC", false }, { "AB", false }, { "ADE", false }, { "ABCDE", false } }; trie.Clear(); Assert.Empty(trie); }
public SplitInfo(string fullName) { FullName = fullName; ExportsUsedFromLazyBundles = new Dictionary <AstNode, string>(); ImportsFromOtherBundles = new Dictionary <AstNode, ImportFromOtherBundle>(); ExportsAllUsedFromLazyBundles = new Dictionary <string, string>(); DirectSplitsForcedLazy = new HashSet <SplitInfo>(); PlainJsDependencies = new OrderedHashSet <string>(); ImportFromExternals = new StringTrie <AstSymbolRef>(); }
public void InsertGet_Wildcards_SecondAndThird() { var target = new StringTrie <int>(); target.GetOrInsert("a", new[] { "a", "c" }, () => 1); target.GetOrInsert("a", new[] { "a", "d" }, () => 2); target.GetOrInsert("a", new[] { "b", "c" }, () => 3); target.GetOrInsert("a", new[] { "b", "d" }, () => 4); target.Get("a", new[] { "*", "*" }).Should().OnlyHaveUniqueItems().And.BeEquivalentTo(1, 2, 3, 4); }
public void Given_a_new_Trie_When_one_item_is_added_Then_it_has_one_item() { //Given var trie = new StringTrie <string>(); trie.Add("Alpha", "first"); //Then var item = trie.Single(); AssertEqual(item, "Alpha", "first"); }
public TrieAtom(string name, string[] strings, bool case_sensitive = false) { Name = name; Score = -1.0 * strings.Length / strings.Sum(s => s.Length); Description = name; CaseSensitive = case_sensitive; trie = new StringTrie <string>(); foreach (string s in strings) { trie.Add(CaseSensitive ? s : s.ToUpper(), s); } }
public void ContainsKeyClear() { var trie = new StringTrie <bool> { { "ABC", false }, { "AB", false }, { "ADE", false }, { "ABCDE", false } }; trie.Clear(); Assert.False(trie.ContainsKey("ABC")); Assert.False(trie.ContainsKey("AB")); Assert.False(trie.ContainsKey("ADE")); Assert.False(trie.ContainsKey("ABCDE")); }
public void Given_a_new_Trie_When_two_items_are_added_in_reverse_order_Then_it_has_two_item() { //Given var trie = new StringTrie <string>(); trie.Add("Beta", "second"); trie.Add("Alpha", "first"); //Then var items = trie.ToArray(); AssertEqual(items[0], "Alpha", "first"); AssertEqual(items[1], "Beta", "second"); }
public void Given_a_Trie_with_one_item_When_a_subitem_is_added_Then_it_has_two_item() { //Given var trie = new StringTrie <string>(); trie.Add("Alpha", "first"); trie.Add("Aztec", "second"); //Then var items = trie.ToArray(); AssertEqual(items[0], "Alpha", "first"); AssertEqual(items[1], "Aztec", "second"); }
public void Given_a_new_Trie_When_two_items_are_added_with_indexer_Then_it_has_two_item() { //Given var trie = new StringTrie <string>(); trie["Alpha"] = "first"; trie["Beta"] = "second"; //Then var items = trie.ToArray(); AssertEqual(items[0], "Alpha", "first"); AssertEqual(items[1], "Beta", "second"); }
public void Given_a_new_Trie_with_two_items_are_added_When_toArray_Then_the_array_is_filled() { //Given var trie = new StringTrie <string>(); trie.Add("Beta", "second"); trie.Add("Alpha", "first"); //Then var items = trie.ToArray(); AssertEqual(items[0], "Alpha", "first"); AssertEqual(items[1], "Beta", "second"); }
public void InsertGet_Test() { var target = new StringTrie <int>(); target.GetOrInsert("a", new string[0], () => 1); target.GetOrInsert("a", new[] { "b", }, () => 2); target.GetOrInsert("a", new[] { "b", "c" }, () => 3); target.GetOrInsert("a", new[] { "b", "c" }, () => 4); target.GetOrInsert("a", new[] { "b", "d" }, () => 5); target.Get("a", new string[] { }).Should().OnlyHaveUniqueItems().And.BeEquivalentTo(1); target.Get("a", new[] { "b" }).Should().OnlyHaveUniqueItems().And.BeEquivalentTo(2); target.Get("a", new[] { "b", "c" }).Should().OnlyHaveUniqueItems().And.BeEquivalentTo(3); target.Get("a", new[] { "b", "d" }).Should().OnlyHaveUniqueItems().And.BeEquivalentTo(5); }
public void Contains() { var trie = new StringTrie <bool> { { "ABC", false }, { "AB", false }, { "ADE", true }, { "ABCDE", false } }; var t = (IDictionary <string, bool>)trie; Assert.True(t.Contains(new KeyValuePair <string, bool>("ABC", false))); Assert.True(t.Contains(new KeyValuePair <string, bool>("AB", false))); Assert.True(t.Contains(new KeyValuePair <string, bool>("ADE", true))); Assert.True(t.Contains(new KeyValuePair <string, bool>("ABCDE", false))); Assert.False(t.Contains(new KeyValuePair <string, bool>("X", false))); Assert.False(t.Contains(new KeyValuePair <string, bool>("ADE", false))); Assert.False(t.Contains(new KeyValuePair <string, bool>("ABCD", false))); }
public void Given_a_Trie_with_two_items_When_a_subitem_is_added_Then_it_has_three_items() { //Given var trie = new StringTrie <string>(); trie.Add("Alpha", "first"); trie.Add("B", "second"); //When trie.Add("Beta", "third"); Helper.WriteLine(trie.ToString()); //Then var items = trie.ToArray(); AssertEqual(items[0], "Alpha", "first"); AssertEqual(items[1], "B", "second"); AssertEqual(items[2], "Beta", "third"); }
public StringTrieRemoveTests(ITestOutputHelper helper) { Helper = helper; _underTest = new StringTrie <string> { { "Alpha", "Alpha value" }, { "Alphabet", "Alphabet value" }, { "Bat", "Bat value" }, { "Badminton", "Badminton value" }, { "Charlie", "Charlie value" }, { "Delta", "Delta value" }, { "Epsilon", "Epsilon value" }, { "Epsilon Delta Gamma", "Epsilon Delta Gamma value" }, { "Gamma", "Gamma value" }, { "Aarg", "Aarg value" }, { "Beast", "Beast value" }, }; }
public static SourceFile BuildSourceFile(string name, string content, SourceMap.SourceMap?sourceMap, Func <string, string, string> resolver) { AstToplevel toplevel; AstSymbol? symbol; if (PathUtils.GetExtension(name) == "json") { (toplevel, symbol) = Helpers.EmitVarDefineJson(content, name); toplevel.FigureOutScope(); var exports = new StringTrie <AstNode>(); exports.Add(new(), symbol); return(new(name, toplevel) { Exports = exports }); } var commentListener = new CommentListener(); toplevel = new Parser(new() { SourceFile = name, OnComment = commentListener.OnComment }, content).Parse(); commentListener.Walk(toplevel); sourceMap?.ResolveInAst(toplevel); UnwrapIIFE(toplevel); toplevel.FigureOutScope(); if (toplevel.Globals !.ContainsKey("module")) { (toplevel, symbol) = Helpers.IfPossibleEmitModuleExportsJsWrapper(toplevel); if (symbol == null) { (toplevel, symbol) = Helpers.EmitCommonJsWrapper(toplevel); } toplevel.FigureOutScope(); var exports = new StringTrie <AstNode>(); exports.Add(new(), symbol); var sourceFile = new SourceFile(name, toplevel) { Exports = exports, OnlyWholeExport = true }; sourceFile.Ast = (AstToplevel) new ImportExportTransformer(sourceFile, resolver).Transform(toplevel); return(sourceFile); }
public void CopyTo() { var trie = new StringTrie <bool> { { "ABC", true }, { "AB", false }, { "ADE", true }, { "ABCDE", false } }; var destinationArray = new KeyValuePair <string, bool> [6]; trie.CopyTo(destinationArray, 1); var result = destinationArray.Where(i => i.Key != null).OrderBy(i => i.Key).ToArray(); var expected = new[] { new KeyValuePair <string, bool>("AB", false), new KeyValuePair <string, bool>("ABC", true), new KeyValuePair <string, bool>("ABCDE", false), new KeyValuePair <string, bool>("ADE", true) }; Assert.Equal(new KeyValuePair <string, bool>(), destinationArray[0]); Assert.Equal(new KeyValuePair <string, bool>(), destinationArray[^ 1]);
public SuggestionSource() { Words = new StringTrie <string>(); }
public SuggestionSource(StringTrie <string> trie) { Words = trie; }
public TrieListenerStore() { _listeners = new StringTrie <object>(); }
public TrieSubscriptionStore() { _topicChannels = new StringTrie <object>(); _topiclessChannels = new ConcurrentDictionary <string, object>(); _topicMap = new ConcurrentDictionary <Guid, string[]>(); }
public void TrieTest1() { var t = new StringTrie<string>(); t.Add("arm", "armum"); t.Add("armat", "armatus"); t.Add("arment", "armenta"); var three = t.GetValues("armenta").ToArray(); }
static void Main(string[] args) { GetAppSettings settings = new GetAppSettings(); Console.WriteLine(settings.GetSetting("Test")); Spelling spelling = new Spelling(); string word = ""; word = "PORK"; Console.WriteLine("{0} => {1}", word, spelling.Correct(word)); word = "FUU"; // 'correcter' is not in the dictionary file so this doesn't work Console.WriteLine("{0} => {1}", word, spelling.Correct(word)); word = "LAMM"; Console.WriteLine("{0} => {1}", word, spelling.Correct(word)); word = "PAINT"; Console.WriteLine("{0} => {1}", word, spelling.Correct(word)); word = "ETAE"; Console.WriteLine("{0} => {1}", word, spelling.Correct(word)); // A sentence string sentence = "I havve speled thes woord wwrong"; // sees speed instead of spelled (see notes on norvig.com) string correction = ""; foreach (string item in sentence.Split(' ')) { correction += " " + spelling.Correct(item); } Console.WriteLine("Did you mean:" + correction); Enumerable.Range(Convert.ToInt32('A'), 26).Union(Enumerable.Range(Convert.ToInt32('0'), 10)).ToList(). ForEach(e => Console.WriteLine(Convert.ToChar(e))); Console.WriteLine($"{DuoVia.FuzzyStrings.StringExtensions.FuzzyMatch("HONG", "HONG")}"); Console.WriteLine($"{DuoVia.FuzzyStrings.StringExtensions.FuzzyMatch("HOONG", "HONG")}"); Console.WriteLine($"{DuoVia.FuzzyStrings.StringExtensions.FuzzyMatch("HOONG", "HSOUNG")}"); Console.WriteLine($"{DuoVia.FuzzyStrings.StringExtensions.FuzzyMatch("HOONG", "HSIN")}"); Console.WriteLine($"{DuoVia.FuzzyStrings.StringExtensions.FuzzyMatch("Hello Wor1ld3", "Hello Warl12d3")}"); Console.WriteLine($"{FuzzyString.ComparisonMetrics.JaccardDistance("Hello World", "Hello World")}"); Console.WriteLine($"{FuzzyString.ComparisonMetrics.JaccardDistance("Hello World", "Hello Warld")}"); Console.WriteLine($"{FuzzyString.ComparisonMetrics.JaccardDistance("Hello World", "Hello Warl12d3")}"); Console.WriteLine($"{FuzzyString.ComparisonMetrics.JaccardDistance("Hello Wor1ld3", "Hello Warl12d3")}"); Console.WriteLine($"{FuzzyString.ComparisonMetrics.HammingDistance("Hello World", "Hello World")}"); Console.WriteLine($"{FuzzyString.ComparisonMetrics.HammingDistance("Hello World", "Hello Warld")}"); Console.WriteLine($"{FuzzyString.ComparisonMetrics.HammingDistance("Hello World", "Hello Warl12d3")}"); Console.WriteLine($"{FuzzyString.ComparisonMetrics.HammingDistance("Hello Wor1ld3", "Hello Warl12d3")}"); Console.WriteLine($"{FuzzyString.ComparisonMetrics.SorensenDiceDistance("Hello World", "Hello World")}"); Console.WriteLine($"{FuzzyString.ComparisonMetrics.SorensenDiceDistance("Hello World", "Hello Warld")}"); Console.WriteLine($"{FuzzyString.ComparisonMetrics.SorensenDiceDistance("Hello World", "Hello Warl12d3")}"); Console.WriteLine($"{FuzzyString.ComparisonMetrics.SorensenDiceDistance("Hello Wor1ld3", "Hello Warl12d3")}"); Console.WriteLine("Hello World".DiceCoefficient("Hello World")); Console.WriteLine("Hello World".DiceCoefficient("Hello Warld")); Console.WriteLine("Hello World".DiceCoefficient("Heo Warl12d3")); Console.WriteLine("Hello World".DiceCoefficient("Hello Warl12d3")); StringTrie <string> Trie = new StringTrie <string>(); Trie.Add("ABC", "ABC"); Trie.Add("ABCDEF", "ABCDEF"); Trie.Add("FGHIJK", "FGHIJK"); Trie.Add("KKK", "KKK"); Trie.Add("LLLLL", "LLLLL"); Trie.Add("LLLLLT", "LLLLLT"); Trie.Add("Tai Po", "Tai Po"); Trie.Add("Tai Po ABC", "Tai Po ABC"); Trie.Add("Tai Po ABC DEF", "Tai Po ABC DEF"); Trie.Add("Tai Po XXX", "Tai Po XXX"); Trie.Add("Tai Po YYY", "Tai Po YYY"); TraverseTrie(Trie, n => { Console.WriteLine($"Node: {n.Value}"); }, lf => { Console.WriteLine($"Leaf: *****"); }); var node = Trie.Find("Tai Po A"); Console.WriteLine(node.IsLeaf + " " + node.HasValue); Console.WriteLine("*************************"); node.Descendants.ToList().ForEach(e => { if (e.HasValue) { Console.WriteLine(e.Value); } }); Console.WriteLine("*************************"); node = Trie.Find("Tai Po AB"); Console.WriteLine(node.IsLeaf + " " + node.HasValue); node = Trie.Find("Tai Po ABC"); Console.WriteLine(node.IsLeaf + " " + node.HasValue); List <string> l = new List <string>(); l.Add(""); l.Add(null); l.Add("Hello World"); foreach (var ll in l) { if (string.IsNullOrEmpty(ll)) { continue; } Console.WriteLine(ll); } var s = "([([}) [] A [] [] Test ]]]]] "; Console.WriteLine(s.Replace('(', Convert.ToChar(32)).Replace(')', Convert.ToChar(32)).Replace('[', Convert.ToChar(32))); /* * FileStream fs = new FileStream(@"C:\Temp\TestAddr.csv", FileMode.Open, FileAccess.Read); * var reader = new CsvReader(new StreamReader(fs)); * reader.ReadHeader(); * int i = 0; * while (reader.Read()) * { * var addr = reader.GetRecord<Addr>(); * Console.WriteLine(addr); * if (i++ >= 10) * break; * } * var p = System.IO.Path.GetTempPath(); */ var r = "A".CompareTo("B"); var cm = new CodeMappings(); HashSet <int> hs = new HashSet <int>(); hs.Add(1); hs.Add(2); hs.Add(1); hs.Add(2); hs.Add(3); foreach (var x in hs) { Console.WriteLine(x); } Console.WriteLine("First Result Set ******************************************************************************"); var res1 = LCSFinder.GetLCS("A B C D E F G H", "A B C D U V Y K"); var res2 = LCSFinder.GetLCS("A B C D E F G H", "A I B T C O D L"); Console.WriteLine(res1); Console.WriteLine(res2); Console.WriteLine("Second Result Set ******************************************************************************"); var res3 = LCSFinder.GetLCS("AA BB CC DDD EE FFFF GG H", "AA XXXX BDB YYYYYY CC DD123DD U EE V Y K"); var res4 = LCSFinder.GetLCS("AA BB CC DDD EE FFFF GG H", "AA I BB T CC OXXXXX DDD L EE"); Console.WriteLine(res3); Console.WriteLine(res4); Console.WriteLine("Third Result Set ******************************************************************************"); var res5 = LCSFinder.GetLCS("AA BB CC DD E F G", "AA BB CC BB II CC KK DD H I K"); var res6 = LCSFinder.GetLCS("AA BB CC DD E F G", "AA BB CCCC DD II E A F B G"); Console.WriteLine(res5); Console.WriteLine(res6); Console.WriteLine("******************************************************************************"); Console.WriteLine("First Result Set ******************************************************************************"); var resX = "A B C D E F G H".LongestCommonSubsequence("A B C D U V Y K"); var resY = "A B C D E F G H".LongestCommonSubsequence("A I B T C O D L"); Console.WriteLine(resX); Console.WriteLine(resY); Console.WriteLine("Second Result Set ******************************************************************************"); var resA = "AA BB CC DDD EE FFFF GG H".LongestCommonSubsequence("AA XXXX BDB YYYYYY CC DD123DD U EE V Y K"); var resB = "AA BB CC DDD EE FFFF GG H".LongestCommonSubsequence("AA I BB T CC OXXXXX DDD L EE"); Console.WriteLine(resA); Console.WriteLine(resB); Console.WriteLine("Third Result Set ******************************************************************************"); var resC = "AA BB CC DD E F G".LongestCommonSubsequence("AA BB CC BB II CC KK DD H I K"); var resD = "AA BB CC DD E F G".LongestCommonSubsequence("AA BB CCCC DD II E A F B G"); Console.WriteLine(resC); Console.WriteLine(resD); Console.WriteLine("******************************************************************************"); Console.WriteLine("1 Result Set ******************************************************************************"); var r1 = "ABCDEFGH".LongestCommonSubsequence("ABCDUVYK"); var r2 = "ABCDEFGH".LongestCommonSubsequence("AIBTCODL"); Console.WriteLine(r1); Console.WriteLine(r2); Console.WriteLine("2 Result Set ******************************************************************************"); var r3 = "AABBCCDDDEEFFFFGGH".LongestCommonSubsequence("AAXXXXBDBYYYYYYCCDD123DDUEEVYK"); var r4 = "AABBCCDDDEEFFFFGGH".LongestCommonSubsequence("AAIBBTCCOXXXXXDDDLEE"); Console.WriteLine(r3); Console.WriteLine(r4); Console.WriteLine("3 Result Set ******************************************************************************"); var r5 = "AABBCCDDEFG".LongestCommonSubsequence("AABBCCBBIICCKKDDHIK"); var r6 = "AABBCCDDEFG".LongestCommonSubsequence("AABBCCCCDDIIEAFBG"); Console.WriteLine(r5); Console.WriteLine(r6); Console.WriteLine("******************************************************************************"); Console.WriteLine("4 Result Set ******************************************************************************"); var r7 = "SUN FUNG WAI".LongestCommonSubsequence("SAN FUNG WAI"); var r8 = "SUN FUNG WAI".LongestCommonSubsequence("SAN WAI COURT"); Console.WriteLine(r7); Console.WriteLine(r8); Console.WriteLine("******************************************************************************"); Enumerable.Range(1, 20).ToList().ForEach(e => { var rA = ToRoman(e); var sA = ConvertRomanToNumber(rA); Console.WriteLine($"{rA}->{sA}"); }); MainStore ms = new MainStore(); ms.Serialize(@"C:\Temp\tt.json"); List <StreetNoExpansion> stl = new List <StreetNoExpansion>() { new StreetNoExpansion(13, "A", 13, "A"), //0 new StreetNoExpansion(13, "L", 14, "L"), //1 new StreetNoExpansion(13, "L", 17, "L"), //2 new StreetNoExpansion(13, "L", 20, "L"), //3 new StreetNoExpansion(13, "L", 14, "M"), //4 new StreetNoExpansion(12, "", 12, ""), //5 new StreetNoExpansion(12, "A", 12, "A"), //6 new StreetNoExpansion(12, "", 19, ""), //7 new StreetNoExpansion(12, "", 20, ""), //8 new StreetNoExpansion(12, "", 12, ""), //9 new StreetNoExpansion(12, "", 13, ""), //10 new StreetNoExpansion(12, "A", 12, "B"), //11 new StreetNoExpansion(14, "D", 21, "S"), //12 new StreetNoExpansion(15, "A", 17, ""), //13 new StreetNoExpansion(15, "A", 29, ""), //14 new StreetNoExpansion(15, "B", 16, ""), //15 new StreetNoExpansion(15, "C", 15, ""), //16 new StreetNoExpansion(16, "", 17, "B"), //17 new StreetNoExpansion(16, "", 18, "B"), //18 new StreetNoExpansion(16, "", 16, "B"), //19 new StreetNoExpansion(16, "", 20, "D"), //20 new StreetNoExpansion(16, "A", 17, ""), //21 new StreetNoExpansion(16, "B", 18, ""), //22 new StreetNoExpansion(16, "C", 16, ""), //23 new StreetNoExpansion(16, "D", 20, ""), //24 new StreetNoExpansion(3, "EA3", 3, "EA3"), //25 new StreetNoExpansion(3, "A", 0, ""), //26 new StreetNoExpansion(3, "", 0, ""), //27 }; stl.ForEach(e => Console.WriteLine(e)); var line = ""; while (!string.IsNullOrEmpty(line = Console.ReadLine())) { Console.Write(">"); if (!ConvertStreetNoFromStringToComponents(line, out int from, out string fromA, out int to, out string toA)) { Console.WriteLine("Conversion Failed"); continue; } int idx = 0; stl.ForEach(e => { Console.WriteLine($"{idx++}->{e.MatchStreetNo(from, fromA, to, toA)}"); }); } }
public void Setup() { Trie = new StringTrie(); Trie.Add("INSERT", "DELETE", "UPDATE", "CREATE", "ALTER", "INTO", "DROP") .Build(); }
public TrieParticipantStore() { _channels = new StringTrie <object>(); }