当前位置: 首页>>代码示例>>C#>>正文


C# Trie类代码示例

本文整理汇总了C#中Trie的典型用法代码示例。如果您正苦于以下问题:C# Trie类的具体用法?C# Trie怎么用?C# Trie使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


Trie类属于命名空间,在下文中一共展示了Trie类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: EmptyTrie

        public void EmptyTrie()
        {
            var trie = new Trie<string, string>();

            var get = trie.Values("43").SingleOrDefault();
            Assert.Null(get);
        }
开发者ID:8thbit,项目名称:Trie,代码行数:7,代码来源:GeneralTrieTests.cs

示例2: AddWithSameKey

        public void AddWithSameKey()
        {
            var trie = new Trie<bool>();

            trie.Add("a", false);
            trie.Add("a", true);
        }
开发者ID:kpol,项目名称:trie,代码行数:7,代码来源:TrieTests.cs

示例3: AddWords

 public void AddWords()
 {
     words = new Trie();
     CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
     CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
     CloudBlobContainer container = blobClient.GetContainerReference("http://hudpoi459storage.blob.core.windows.net/hudson/");
     CloudBlockBlob blockBlob = container.GetBlockBlobReference("wikipedia.txt");
     try
     {
         using (var stream = blockBlob.OpenRead())
         {
             using (StreamReader sr = new StreamReader(stream))
             {
                 string line;
                 while ((line = sr.ReadLine()) != null)
                 {
                     words.Insert(line);
                 }
             }
         }
     }
     catch (OutOfMemoryException)
     {
         GC.Collect();
     }
 }
开发者ID:hudpoi459,项目名称:Info344,代码行数:26,代码来源:WebService.cs

示例4: PrefixMatchTest

        public void PrefixMatchTest()
        {
            string s = @"
                In computer science, a trie, or prefix tree,
                is an ordered tree data structure that is used to
                store an associative array where the keys are strings.
                Unlike a binary search tree, no node in the tree
                stores the key associated with that node;
                instead, its position in the tree shows what key
                it is associated with. All the descendants
                of any one node have a common prefix of the string
                associated with that node, and the root is associated
                with the empty string. Values are normally not associated
                with every node, only with leaves and some inner nodes
                that happen to correspond to keys of interest.
            ";

            Trie<char, string> wordIndex = new Trie<char, string>(new LettersDomain());
            foreach (string word in s.Replace("\r", "").Replace("\n", " ").Replace("\t", "").Split(' ', ',', ';', '.'))
            {
                if (word.Length > 0)
                {
                    wordIndex.Add(word, word);
                }
            }

            ITrieEnumerator<char, string> matches = wordIndex.PrefixMatch("p");
            while (matches.MoveNext())
            {
                Console.WriteLine(matches.Current.Value);
            }
        }
开发者ID:jeremysimmons,项目名称:sixpack-library,代码行数:32,代码来源:TrieTests.cs

示例5: EnemyInputManager

 public EnemyInputManager(List<String> texts, Game game)
 {
     trie = new Trie<Enemy>();
     this.texts = texts;
     this.game = game;
     currentlyMatched = new List<Enemy>();
 }
开发者ID:himasharox,项目名称:typocalypse,代码行数:7,代码来源:EnemyInputManager.cs

示例6: Main

        public static void Main()
        {
            Trie<int> trie = new Trie<int>();

            using (StreamReader reader = new StreamReader("../../test.txt"))
            {
                while (!reader.EndOfStream)
                {
                    string[] line = reader.ReadLine().Split(new char[] { ' ', '.', ',', '!', '?', ':', ';', '-' }, StringSplitOptions.RemoveEmptyEntries).ToArray();

                    foreach (var word in line)
                    {
                        if (!trie.ContainsKey(word))
                        {
                            trie.Add(word, 1);
                        }
                        else
                        {
                            trie[word]++;
                        }
                    }
                }
            }

            trie.Matcher.Next("eros");
            Console.WriteLine(trie.Matcher.GetExactMatch());
        }
开发者ID:MarinaGeorgieva,项目名称:TelerikAcademy,代码行数:27,代码来源:Startup.cs

示例7: Add_ItemExists_Throws

        public void Add_ItemExists_Throws()
        {
            Trie<string, char, int> trie = new Trie<string, char, int>();

            trie.Add("key", 5);
            trie.Add("key", 6);
        }
开发者ID:flashcurd,项目名称:Shared.Utilities,代码行数:7,代码来源:TrieTests.cs

示例8: Main

        static void Main(string[] args)
        {
            Stopwatch _s = new Stopwatch();

            _s.Start();

            /* */
            //Trie<byte[]> _trie = new Trie<byte[]>();
            Trie<string> _trie = new Trie<string>();
            for (int i = 0; i < 1000000; i++)
            {
                //byte[] bkey = Encoding.ASCII.GetBytes("root/sub/key" + i);
                //byte[] bvalue = Encoding.ASCII.GetBytes("test_" + i);
                _trie.Add("root/sub/key" + i, "test_" + i);
            }

            //var f=_trie.Find("root/sub/key999090");
            var g = _trie.FindAll("root/sub/key0");
            // SPEED ~ 1700 msec
            //*/

            /* /
            Trie2 _trie2 = new Trie2();
            for (int i = 0; i < 1000000; i++)
            {
                _trie2.AddNodeForWord("root/sub/key" + i);
            }
            //*/

            _s.Stop();

            Console.WriteLine("hj");
            Console.ReadKey();
        }
开发者ID:stas-github-projects,项目名称:test_git_vs,代码行数:34,代码来源:Program.cs

示例9: TrieTests_EnumerateInOrder2

        public void TrieTests_EnumerateInOrder2()
        {
            Trie<char> trie = new Trie<char>();

            List<string> items = new List<string>();

            Random r = new Random();
            for (int i = 0; i < 100000; i++)
            {
                char[] word = new char[r.Next(10) + 1];

                for (int j = 0; j < word.Length; j++)
                {
                    word[j] = (char)(97 + r.Next(26));
                }

                string sword = new string(word);

                items.Add(sword);

                trie.Insert(sword);
            }

            items.Sort();

            var actualOrder = trie.EnumerateInOrder().Select(sequence => new string(sequence.ToArray())).ToList();

            Assert.IsTrue(items.Except(actualOrder).Count() == 0);
            Assert.IsTrue(actualOrder.Except(items).Count() == 0);
        }
开发者ID:ZuTa,项目名称:Algorithms,代码行数:30,代码来源:TrieTests.cs

示例10: Trie_Clone_should_work

        public void Trie_Clone_should_work()
        {
            var trie = new Trie<char, int>();
              trie.SetValue("foo", 42);
              trie.SetValue("foobar", 13);
              trie.SetValue("fizban", 53);
              trie.SetValue("fizbang", 12);

              var cloneTrie = trie.Clone();
              cloneTrie.SetValue("fip", 17);

              int value;

              trie.TryGetValue("foo", out value).Should().BeTrue();
              value.Should().Be(42);
              trie.TryGetValue("foobar", out value).Should().BeTrue();
              value.Should().Be(13);
              trie.TryGetValue("fizban", out value).Should().BeTrue();
              value.Should().Be(53);
              trie.TryGetValue("fizbang", out value).Should().BeTrue();
              value.Should().Be(12);
              trie.TryGetValue("fip", out value).Should().BeFalse();

              cloneTrie.TryGetValue("foo", out value).Should().BeTrue();
              value.Should().Be(42);
              cloneTrie.TryGetValue("foobar", out value).Should().BeTrue();
              value.Should().Be(13);
              cloneTrie.TryGetValue("fizban", out value).Should().BeTrue();
              value.Should().Be(53);
              cloneTrie.TryGetValue("fizbang", out value).Should().BeTrue();
              value.Should().Be(12);
              cloneTrie.TryGetValue("fip", out value).Should().BeTrue();
              value.Should().Be(17);
        }
开发者ID:jcracknell,项目名称:emd,代码行数:34,代码来源:TrieTests.cs

示例11: Main

    public static void Main()
    {
        string inputText;
        StreamReader reader = new StreamReader("test.txt");

        using (reader)
        {
            inputText = reader.ReadToEnd().ToLower();
        }

        var matches = Regex.Matches(inputText, @"[a-zA-Z]+");
        HashSet<string> uniqueWords = new HashSet<string>();
        var trie = new Trie();

        for (int i = 0; i < matches.Count; i++)
        {
            uniqueWords.Add(matches[i].ToString());
            trie.Add(matches[i].ToString());
        }

        foreach (var word in uniqueWords)
        {
            Console.WriteLine("{0} -> {1} times", word, trie.GetWordOccurance(word));
        }
    }
开发者ID:VDGone,项目名称:TelerikAcademy-2,代码行数:25,代码来源:Testing.cs

示例12: PatternMatcher

 //    Start a pattern match at the beginning with one object.
 internal PatternMatcher(ProseRuntime runtime)
 {
     this.runtime = runtime;
     patternTrie = runtime.GlobalScope.PatternTree;
     state = MatcherState.MATCHING_OBJECT;
     currNode = patternTrie.Root;
 }
开发者ID:FizzyP,项目名称:Prose,代码行数:8,代码来源:PatternMatcher.cs

示例13: Main

        static void Main(string[] args)
        {
            Trie trie = new Trie();
            Dictionary<string, int> dict = new Dictionary<string, int>();
            Stopwatch sw = new Stopwatch();

            sw.Start();
            List<string> wordsFromText = GetWordsFromText();
            sw.Stop();
            Console.WriteLine("Word extraction finished in {0}", sw.Elapsed);

            List<string> wordsToSearch = new List<string>();
            int numberOfWords = 100;

            int len = numberOfWords < wordsFromText.Count ? numberOfWords : wordsFromText.Count;

            for (int i = 0; i < len; i++)
			{
                wordsToSearch.Add(wordsFromText[i]);			 
			}

            AddWordsUsingTrie(trie, wordsFromText, sw);
            AddWordsUsingDict(dict, wordsFromText, sw);

            FindWordsUsingTrie(trie, wordsToSearch, sw);
            FindWordsUsingDict(dict, wordsToSearch, sw);
        }
开发者ID:Cecosam,项目名称:Csharp-Projects,代码行数:27,代码来源:TrieDemo.cs

示例14: Main

    static void Main()
    {
        Trie<string> trie = new Trie<string>();
        StreamReader reader = new StreamReader(@"../../../text.txt");
        using (reader)
        {
            string text = reader.ReadToEnd();

            char[] separators = { ' ', '.', '!', '-', '?', '_' };
            string[] array = text.Split(separators, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 0; i < array.Length; i++)
            {
                trie.Put(array[i], i.ToString());
            }

            string word = "Conan";
            for (int i = 0; i < word.Length; i++)
            {

            }
            foreach (var item in trie.Matcher.GetPrefixMatches())
            {
                Console.WriteLine(item);
            }
            Console.WriteLine(trie.Matcher.GetExactMatch());
        }
    }
开发者ID:Jarolim,项目名称:TelerikAcademy-1,代码行数:28,代码来源:Program.cs

示例15: ParseFile

        private static Trie<int> ParseFile(string filename)
        {
            Trie<int> trie = new Trie<int>();

            Console.WriteLine("Reading file and loading content in trie:");

            using (StreamReader reader = new StreamReader(filename))
            {
                while (!reader.EndOfStream)
                {
                    reader
                        .ReadLine()
                        .Split(' ', '.', ',', '?', '!', ':')
                        .ToList()
                        .ForEach(word =>
                        {
                            if (!trie.ContainsKey(word))
                            {
                                trie.Add(word, 1);
                            }
                            else
                            {
                                trie[word] += 1;
                            }
                        });
                }
            }

            Console.WriteLine("File read and loaded into trie");

            return trie;
        }
开发者ID:Novkirishki,项目名称:Data-Structures-and-Algorithms,代码行数:32,代码来源:Startup.cs


注:本文中的Trie类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。