本文整理汇总了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);
}
示例2: AddWithSameKey
public void AddWithSameKey()
{
var trie = new Trie<bool>();
trie.Add("a", false);
trie.Add("a", true);
}
示例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();
}
}
示例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);
}
}
示例5: EnemyInputManager
public EnemyInputManager(List<String> texts, Game game)
{
trie = new Trie<Enemy>();
this.texts = texts;
this.game = game;
currentlyMatched = new List<Enemy>();
}
示例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());
}
示例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);
}
示例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();
}
示例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);
}
示例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);
}
示例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));
}
}
示例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;
}
示例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);
}
示例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());
}
}
示例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;
}