Exemple #1
0
        public void TestIterativeDFS()
        {
            var result = new List<string>();
            var dfs = new IterativeDFS<Node>();
            dfs.Init(null, Node.GetChild, (context, branch) => result.Add(Node.BranchToString(branch)));
            while (dfs.Next());
            Assert.AreEqual(0, result.Count);

            dfs.Init(Root, Node.GetChild, (context, branch) => result.Add(Node.BranchToString(branch)));
            while (dfs.Next());
            Assert.AreEqual(Paths.Count, result.Count);
            for (int i = 0; i < result.Count; i++) {
                Assert.AreEqual(Paths[i], result[i]);
            }
        }
 static IEnumerable TestIterativeDFS()
 {
     var result = new List<string>();
     var dfs = new IterativeDFS<Node>();
     dfs.Init(Root, Node.GetChild, (context, branch) => result.Add(Node.BranchToString(branch)));
     while (dfs.Next());
     return result;
 }
        public Task StartGettingWords(List<T9Key> pressed, Action<string> processWord, CancellationToken ct)
        {
            var dfs = new IterativeDFS<T9Letter>();
            dfs.Init(this, (context, branch, index) => {
                if (branch.Count >= pressed.Count)
                    return null;
                if (index >= pressed[branch.Count].Letters.Length)
                    return null;
                return new T9Letter() {
                    Key = pressed[branch.Count],
                    LetterIndex = index
                };
            }, (context, branch) => {
                var word = branch.Aggregate("", (w, v) => w + v.Letter);
                var matches = FindWordsWithPrefix(word);
                if (matches != null) {
                    foreach (var match in matches) {
                        processWord(match);
                    }
                }
            });

            var task = Task.Factory.StartNew(() => {
                ct.ThrowIfCancellationRequested();

                while (dfs.Next()) {
                    if (ct.IsCancellationRequested) {
                        ct.ThrowIfCancellationRequested();
                    }
                }
            });
            return task;
        }