本文整理汇总了C#中Trie.GetByPrefix方法的典型用法代码示例。如果您正苦于以下问题:C# Trie.GetByPrefix方法的具体用法?C# Trie.GetByPrefix怎么用?C# Trie.GetByPrefix使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Trie
的用法示例。
在下文中一共展示了Trie.GetByPrefix方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BenchmarkTest
public void BenchmarkTest()
{
const int Count = 1;
var words = GetWords();
TestContext.WriteLine(
"Words count: {0}. Iterations count: {1}. Prefixes count: {2}.", words.Count(), Count, Prefixes.Length);
var stopWatch = Stopwatch.StartNew();
for (int i = 0; i < Count; i++)
{
foreach (var prefix in Prefixes)
{
var resultArray = words.Where(w => w.StartsWith(prefix)).ToArray();
}
}
stopWatch.Stop();
TestContext.WriteLine("ToArray method: {0}", stopWatch.ElapsedMilliseconds);
stopWatch.Restart();
var trie = new Trie<bool>();
trie.AddRange(words.Select(w => new TrieEntry<bool>(w, false)));
stopWatch.Stop();
TestContext.WriteLine("Build tree: {0}", stopWatch.ElapsedMilliseconds);
stopWatch.Restart();
for (int i = 0; i < Count; i++)
{
foreach (var prefix in Prefixes)
{
var resultTrie = trie.GetByPrefix(prefix).Select(w => w.Key).ToArray();
}
}
stopWatch.Stop();
TestContext.WriteLine("Trie find prefixes: {0}", stopWatch.ElapsedMilliseconds);
}
示例2: Main
public static void Main()
{
var words = LoadWords(VocabularyPath);
Console.WriteLine("Words count: {0}", words.Count());
const int PrefixLength = 2;
var prefixes = GetAllMatches(Enumerable.Range('A', 'Z' - 'A' + 1).
Select(i => ((char)i)).ToArray(), PrefixLength)
.ToArray();
Console.WriteLine("Search prefixes count: {0}", prefixes.Count());
Console.WriteLine();
var stopWatch = Stopwatch.StartNew();
int matchesCount = 0;
foreach (var prefix in prefixes)
{
var resultArray = words.Where(w => w.StartsWith(prefix)).ToArray();
matchesCount += resultArray.Count();
}
Console.WriteLine("Found {0} matches", matchesCount);
stopWatch.Stop();
Console.WriteLine("Regular string matching time: {0} ms",
stopWatch.ElapsedMilliseconds);
Console.WriteLine();
stopWatch.Restart();
var trie = new Trie<bool>();
trie.AddRange(words.Select(w => new TrieEntry<bool>(w, false)));
stopWatch.Stop();
Console.WriteLine("Build trie time: {0} ms", stopWatch.ElapsedMilliseconds);
Console.WriteLine();
stopWatch.Restart();
matchesCount = 0;
foreach (var prefix in prefixes)
{
var resultTrie = trie.GetByPrefix(prefix).Select(w => w.Key).ToArray();
matchesCount += resultTrie.Count();
}
Console.WriteLine("Found {0} matches", matchesCount);
stopWatch.Stop();
Console.WriteLine("Trie find prefixes time: {0} ms", stopWatch.ElapsedMilliseconds);
}
示例3: Main
public static void Main(string[] args)
{
const int PrefixLength = 1;
var prefixes = GetAllMatches(Enumerable.Range(65, 26).Select(i => ((char)i)).ToArray(), PrefixLength)
.ToArray();
var words = GetWords();
Console.WriteLine("Words count: {0}. Prefixes count: {1}.", words.Count(), prefixes.Length);
var stopWatch = Stopwatch.StartNew();
foreach (var prefix in prefixes)
{
var resultArray = words.Where(w => w.StartsWith(prefix)).ToArray();
}
stopWatch.Stop();
Console.WriteLine("ToArray method: {0}ms.", stopWatch.ElapsedMilliseconds);
stopWatch.Restart();
var trie = new Trie<bool>();
trie.AddRange(words.Select(w => new TrieEntry<bool>(w, false)));
stopWatch.Stop();
Console.WriteLine("Build tree: {0}ms.", stopWatch.ElapsedMilliseconds);
stopWatch.Restart();
foreach (var prefix in prefixes)
{
var resultTrie = trie.GetByPrefix(prefix).Select(w => w.Key).ToArray();
}
stopWatch.Stop();
Console.WriteLine("Trie find prefixes: {0}ms.", stopWatch.ElapsedMilliseconds);
}
示例4: GetByPrefix
public void GetByPrefix()
{
var trie = new Trie<bool>();
trie.Add("ABC", false);
trie.Add("AB", false);
(trie as ICollection<KeyValuePair<string, bool>>).Add(new KeyValuePair<string, bool>("ADE", false));
trie.Add(new TrieEntry<bool>("ABCDE", false));
var result = trie.GetByPrefix("ABC").Select(t => t.Key).OrderBy(t => t);
string[] expectedResult = { "ABC", "ABCDE" };
Assert.AreEqual(4, trie.Count);
Assert.IsTrue(expectedResult.SequenceEqual(result));
}
示例5: CheckTest
public void CheckTest()
{
var words = GetWords();
var trie = new Trie<bool>();
trie.AddRange(words.Select(w => new TrieEntry<bool>(w, false)));
foreach (var prefix in Prefixes)
{
var result1 = words.Where(w => w.StartsWith(prefix));
var result2 = trie.GetByPrefix(prefix).Select(t => t.Key).OrderBy(w => w);
Assert.IsTrue(result1.SequenceEqual(result2));
}
}