void _AddToTree(string key, Termin t) { if (key == null) { return; } CharNode nod = this._getRoot(t.Lang, t.Lang.IsUndefined && Pullenti.Morph.LanguageHelper.IsLatin(key)); for (int i = 0; i < key.Length; i++) { short ch = (short)key[i]; if (nod.Children == null) { nod.Children = new Dictionary <short, CharNode>(); } CharNode nn; if (!nod.Children.TryGetValue(ch, out nn)) { nod.Children.Add(ch, (nn = new CharNode())); } nod = nn; } if (nod.Termins == null) { nod.Termins = new List <Termin>(); } if (!nod.Termins.Contains(t)) { nod.Termins.Add(t); } }
void _RemoveFromTree(string key, Termin t) { if (key == null) { return; } CharNode nod = this._getRoot(t.Lang, t.Lang.IsUndefined && Pullenti.Morph.LanguageHelper.IsLatin(key)); for (int i = 0; i < key.Length; i++) { short ch = (short)key[i]; if (nod.Children == null) { return; } CharNode nn; if (!nod.Children.TryGetValue(ch, out nn)) { return; } nod = nn; } if (nod.Termins == null) { return; } if (nod.Termins.Contains(t)) { nod.Termins.Remove(t); } }
public CharNode AddChild(char c) { CharNode child = new CharNode(c); children.Add(child); return(child); }
public void TestContainsInLeftRightSubTree() { CharNode root = ExampleTree; List <CharNode> leaves = new List <CharNode>(root.Leaves); List <CharNode> leaf_parents = new List <CharNode>(); foreach (CharNode n in leaves) { leaf_parents.Add(n.Parent); } Assert.IsTrue(root.ContainsInLeftSubTree(leaves[0])); // 'd' Assert.IsFalse(root.ContainsInLeftSubTree(leaves[1])); // 'f' Assert.IsFalse(root.ContainsInLeftSubTree(leaves[2])); // 'h' Assert.IsTrue(leaf_parents[0].ContainsInLeftSubTree(leaves[0])); Assert.IsTrue(leaf_parents[1].ContainsInLeftSubTree(leaves[1])); Assert.IsFalse(leaf_parents[1].ContainsInRightSubTree(leaves[1])); Assert.IsTrue(leaf_parents[1].ContainsInRightSubTree(leaves[2])); // When query node is not part of subtree the method throws an exception NUnitExtensions.Assert.Throws(typeof(InvalidOperationException), delegate { leaf_parents[1].ContainsInRightSubTree(leaves[0]); }); // When the query node is the same node as the node the query is executed on // an exception is thrown NUnitExtensions.Assert.Throws(typeof(InvalidOperationException), delegate { leaf_parents[1].ContainsInRightSubTree(leaf_parents[1]); }); NUnitExtensions.Assert.Throws(typeof(InvalidOperationException), delegate { leaf_parents[1].ContainsInLeftSubTree(leaf_parents[1]); }); }
private void GetWordOccurence(string word, int index, CharNode currentNode, ref int count) { char currentChar = word[index]; bool lastChar = false; if (index == word.Length - 1) { lastChar = true; } foreach (var node in currentNode.Children) { if (node.Letter == currentChar) { if (lastChar) { // If this is the last char of the string, set the count count = node.Count; return; } else { // Go to the next level if there already exists a node with this char at the current one this.GetWordOccurence(word, index + 1, node, ref count); return; } } } // If no node with the current char exists at this level in the trie, add it: return; }
public void Add(string oldStr) { char _char = oldStr [0]; if (oldStr.Length == 1) { if (Dict.ContainsKey(_char)) { return; } else { Dict.Add(_char, new CharNode(true)); } } else { if (Dict.ContainsKey(_char)) { oldStr = oldStr.Substring(1, oldStr.Length - 1); Dict [_char].Add(oldStr); } else { CharNode node = new CharNode(false); Dict.Add(_char, node); oldStr = oldStr.Substring(1, oldStr.Length - 1); node.Add(oldStr); } } }
private Graph <Char> MockGraph() { var graph = new Graph <Char>(); var a = new CharNode('A'); var b = new CharNode('B'); var c = new CharNode('C'); var d = new CharNode('D'); var e = new CharNode('E'); var g = new CharNode('G'); graph.AddNode(a); graph.AddNode(b); graph.AddNode(c); graph.AddNode(d); graph.AddNode(e); graph.AddNode(g); graph.AddUndirectedEdge(a, b); graph.AddUndirectedEdge(a, e); graph.AddUndirectedEdge(b, c); graph.AddUndirectedEdge(b, d); graph.AddUndirectedEdge(c, d); graph.AddUndirectedEdge(c, e); graph.AddUndirectedEdge(d, e); return(graph); }
/// <inheritdoc/> public override (Node Head, Node Tail) Insert(Int32 index, Char element) { Node head; Node tail; if (index == 0) { tail = this; head = new CharNode(element, previous: null, next: tail); tail.Previous = head; } else if (index == Count) { head = this; tail = new CharNode(element, previous: head, next: null); head.Next = tail; } else { head = Slice(0, index); tail = Slice(index, Count - index); Node mid = new CharNode(element, previous: head, next: tail); head.Next = mid; tail.Previous = mid; } return(head, tail); }
public void TestLeafVsIntermediateState() { CharNode root = ExampleTree; Assert.IsTrue(root.Intermediate); Assert.IsFalse(root.Leaf); Assert.AreEqual("dfh", MakeStringFromIteration(root.Leaves)); }
//potrzebne do niszczenia za sobą ścieżek przy przechodzeniu przez graf public void buildVirtualEdges() { virtualEdgesList = new List <CharNode>(); foreach (var p in edgesList) { var tmp = new CharNode(p.key, p.value); virtualEdgesList.Add(tmp); } }
List <Termin> _FindInTree(string key, Pullenti.Morph.MorphLang lang) { if (key == null) { return(null); } CharNode nod = this._getRoot(lang, ((lang == null || lang.IsUndefined)) && Pullenti.Morph.LanguageHelper.IsLatin(key)); for (int i = 0; i < key.Length; i++) { short ch = (short)key[i]; CharNode nn = null; if (nod.Children != null) { nod.Children.TryGetValue(ch, out nn); } if (nn == null) { if (ch == 32) { if (nod.Termins != null) { string[] pp = key.Split(' '); List <Termin> res = null; foreach (Termin t in nod.Termins) { if (t.Terms.Count == pp.Length) { int k; for (k = 1; k < pp.Length; k++) { if (!t.Terms[k].Variants.Contains(pp[k])) { break; } } if (k >= pp.Length) { if (res == null) { res = new List <Termin>(); } res.Add(t); } } } return(res); } } return(null); } nod = nn; } return(nod.Termins); }
private CharNode AddNodeChild(CharNode target, char ch) { if (target.Children == null) { target.Children = new Dictionary <char, CharNode>(); } CharNode child = new CharNode(ch); target.Children.Add(ch, child); NodeCount++; return(child); }
public void TestAncestorTraversal() { CharNode root = ExampleTree; Assert.AreEqual("a", MakeStringFromIteration(root.Ancestors)); List <CharNode> leaves = new List <CharNode>(root.Leaves); Assert.AreEqual("dcba", MakeStringFromIteration(leaves[0].Ancestors)); Assert.AreEqual("fea", MakeStringFromIteration(leaves[1].Ancestors)); Assert.AreEqual("hgea", MakeStringFromIteration(leaves[2].Ancestors)); }
/// <inheritdoc/> public override (Node Head, Node Tail) Replace(Char search, Char replace) { if (Equals(Char, search)) { Node rep = new CharNode(replace, previous: null, next: null); return(rep, rep); } else { return(this, this); } }
public void BuildTree(string compressMe) { foreach (char c in compressMe) { bool incremented = false; foreach (Node c_Nodes in nodes) { if (c_Nodes.CompareTo(c) == 1) { incremented = true; c_Nodes.incFreq(); } } if (!incremented) { nodes.Add(new Node(c)); } } List <Node> charNodes = nodes.OrderBy(node => node.m_Freq).ToList <Node>(); treeInfos = ""; while (nodes.Count > 1) { List <Node> orgNodes = nodes.OrderBy(node => node.m_Freq).ToList <Node>(); if (orgNodes.Count >= 2) { List <Node> twoChildNodes = orgNodes.Take(2).ToList <Node>(); Node newDaddyNode = new Node((twoChildNodes[0].getFreq() + twoChildNodes[1].getFreq())); newDaddyNode.setLeftNode(twoChildNodes[0]); newDaddyNode.setRightNode(twoChildNodes[1]); nodes.Remove(twoChildNodes[0]); nodes.Remove(twoChildNodes[1]); nodes.Add(newDaddyNode); } this.m_Root = nodes.FirstOrDefault(); } foreach (Node CharNode in charNodes) { List <bool> codedChar = this.m_Root.Traverse(CharNode.getChar(), new List <bool>()); foreach (bool b in codedChar) { treeInfos += Convert.ToInt16(b); } treeInfos += CharNode.getChar(); } }
public CharNode(int w, string z = "", CharNode left = null, CharNode right = null, string code = "") { character = z; value = w; if (right != null && left != null) { rightSide = right; leftSide = left; } if (code != String.Empty) { codeValue = code; } }
private static bool TryGetReplacement(CharNode tildaNode, out char newChar) { int x, y; if (!IsHex(tildaNode.Next?.Value, out x) || !IsHex(tildaNode.Next.Next?.Value, out y)) { newChar = default(char); return(false); } newChar = char.ToUpper((char)(16 * x + y)); return(true); }
/// <summary> /// Initializes a new instance of the <see cref="TextMatchHelper"/> class. /// </summary> /// <param name="matches">The matches to match against.</param> /// <exception cref="System.ArgumentNullException"></exception> public TextMatchHelper(HashSet <string> matches) { if (matches == null) { throw new ArgumentNullException(nameof(matches)); } var list = new List <string>(matches); root = new CharNode(); listCache = new ListCache(); BuildMap(root, 0, list); listCache.Clear(); }
//[Benchmark] public void CharNode() { foreach (var(t, p) in TestData.TypeNames.Take(1)) { // 追加は1回限り var items = p.Select(x => new KeyValuePair <string, string>(x, x)); var d = new CharNode <string>(); foreach (var(key, value) in items) { d.Add(key, value); } Bench(items, allKeys.Except(p), d); } }
private WeightedGraph <Char> TestCharGraph() { var graph = new WeightedGraph <Char>(); var a = new CharNode('A'); var b = new CharNode('B'); graph.AddNode(a); graph.AddNode(b); graph.AddUndirectedEdge(a, b, 10); return(graph); }
public void AddNode(CharNode node) { // 是也可以不管順序直接 add 到尾端就好,反正 rol col 的索引都已經有了 // 假如要轉成二維陣列存的話,也可以找出需要的rold col 數, // 填入陣列後,再掃一次陣列,把字元挑出來就好 // 只是目前是希望可以省空間複雜度,從 n^2 變成 n //Console.WriteLine("Add [ {0}, {1} ] = {2}", node.RowIndex, node.ColIndex, node.Character); CharNode current = this; // 假如是 head,先 next(一開始 head 是空的情況,第一次 add 跑 while 迴圈就會失敗) // if (current.Character == '\0') // { // current = current.Next; // } while (current.Next != null) { // 遇到同 row if (node.RowIndex == current.RowIndex) { // 找出同 row 中,正確的 col 位置 while (node.ColIndex > current.ColIndex && node.RowIndex == current.RowIndex && current.Next != null) { current = current.Next; } if (node.RowIndex != current.RowIndex) { current.Previous.Next = node; node.Previous = current.Previous; node.Next = current; current.Previous = node; return; } } else { current = current.Next; } } // 執行到這邊,表示 此 node 就是要接在最尾端 current.Next = node; node.Previous = current; }
private void TraverseNodes <TSelect>(string word, int maxCount, Func <string, string, TSelect, int> evalQuality) where TSelect : T { while (stack.Count > 0) { CharNode current = stack.Pop(); if (current.Items != null) { foreach (NodeItem item in current.Items) { if (item.Item is TSelect) { int quality = evalQuality(item.Key, word, (TSelect)item.Item); if (quality > 0) { Matched match; bool exists = matchLookup.TryGetValue(item, out match); if (unique.Add(item)) { if (!exists) { match.Value = item; maxCount--; } match.Rank += quality; matchLookup[item] = match; } else if (exists && match.Rank < quality) { match.Rank = quality; matchLookup[item] = match; } } } if (maxCount == 0) { return; } } } if (current.Children != null) { foreach (var child in current.Children) { stack.Push(child.Value); } } } }
private static CharNode GetBestMatchChildNode(CharNode current, string word) { for (int idx = 0; idx < word.Length; idx++) { if (current.Children != null && current.Children.TryGetValue(word[idx], out CharNode next)) { current = next; } else { break; } } return(current); }
private void AddLetters(CharNode currentNode, char letterToAdd, string word, int position) { currentNode.Add(letterToAdd); char letter = letterToAdd; position++; if (position == word.Length) { currentNode.Add('|'); return; } letterToAdd = word[position]; AddLetters(currentNode[letter], letterToAdd, word, position); }
private void AddChars(string word, int index, CharNode currentNode) { if (word == string.Empty) { return; } char currentChar = word[index]; bool lastChar = false; if (index == word.Length - 1) { lastChar = true; } foreach (var node in currentNode.Children) { if (node.Letter == currentChar) { if (lastChar) { // If this is the last char of the string, increase the count of this string occurences node.Count++; return; } else { // Go to the next level if there already exists a node with this char at the current one this.AddChars(word, index + 1, node); return; } } } // If no node with the current char exists at this level in the trie, add it: CharNode newNode = new CharNode(currentChar); currentNode.Children.Add(newNode); if (!lastChar) { // If the word is not finished, continue this.AddChars(word, index + 1, newNode); } else { newNode.Count++; } }
public CharNode(char c, List<char> chars) { //Console.WriteLine("In constructor, array length is: {0}", chars.Count); data = c; chars.Remove(c); if (chars.Count > 1) { children = new CharNode[chars.Count]; for (int i = 0; i < chars.Count; i++) { char nextchar = chars[i]; children[i] = new CharNode(nextchar, chars.ToList<char>()); } } else { children = new CharNode[1]; children[0] = new CharNode(chars[0]); } }
static List<long> buildPermutations() { char[] chars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; // List<long> perms = new List<long>(); List<CharNode> tree = new List<CharNode>(); foreach (char c in chars) { CharNode cn = new CharNode(c, chars.ToList<char>()); tree.Add(cn); } foreach (CharNode cn in tree) { //Console.WriteLine(cn.getTree().Count); foreach (string s in cn.getTree()) { //Console.WriteLine(s); perms.Add(long.Parse(s)); } } return perms; }
private void FindOccurence(CharNode currentNode, string word, int position, ref int occurences) { position++; if (position == word.Length - 1) { occurences = currentNode.Occurences; return; } else if (!currentNode.ContainsLetter(word[position])) { occurences = 0; return; } else { FindOccurence(currentNode[word[position]], word, position, ref occurences); } }
private void BuildMap(CharNode node, int index, List <string> list) { // TODO(lazy): This code for building the nodes is not very efficient in terms of memory usage and could be optimized (using structs and indices) // At least, we are using a cache for the temporary objects build (List<string>) for (var i = 0; i < list.Count; i++) { var str = list[i]; var c = str[index]; CharNode nextNode; if (!node.TryGetValue(c, out nextNode)) { nextNode = new CharNode(); node.Add(c, nextNode); } // We have found a string for this node if (index + 1 == str.Length) { nextNode.Content = str; } else { if (nextNode.NextList == null) { nextNode.NextList = listCache.Get(); } nextNode.NextList.Add(str); } } foreach (var charList in node) { if (charList.Value.NextList != null) { BuildMap(charList.Value, index + 1, charList.Value.NextList); listCache.Release(charList.Value.NextList); charList.Value.NextList = null; } } }
public CharNode(char c, List <char> chars) { //Console.WriteLine("In constructor, array length is: {0}", chars.Count); data = c; chars.Remove(c); if (chars.Count > 1) { children = new CharNode[chars.Count]; for (int i = 0; i < chars.Count; i++) { char nextchar = chars[i]; children[i] = new CharNode(nextchar, chars.ToList <char>()); } } else { children = new CharNode[1]; children[0] = new CharNode(chars[0]); } }
static bool IsPalindrome(CharLinkedList linkedList) { StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); CharStack stack = new CharStack(new CharLinkedList()); CharNode current = linkedList.Head; while (current != null) { char value = current.Value; sb1.Append(value); stack.Push(value); current = current.Next; } while (!stack.IsEmpty()) { sb2.Append(stack.Pop()); } return(sb1.ToString() == sb2.ToString()); }
static public void createCode(ref List <CharNode> nodes, string input) { foreach (char letter in input) { charsToNodes(letter.ToString(), ref nodes); } IEnumerable <CharNode> nodeIE = nodes.OrderBy(node => node.value).Reverse(); while (nodeIE.Count() > 1) { CharNode left = nodeIE.Last(); nodeIE = nodeIE.Where(node => node != left).Select(node => node); CharNode right = nodeIE.Last(); nodeIE = nodeIE.Where(node => node != right).Select(node => node); nodeIE = nodeIE.Append(new CharNode(left.value + right.value, left: left, right: right)); nodeIE = nodeIE.OrderBy(node => node.value).Reverse(); } nodeIE.Last().Code(); nodes.Clear(); nodeIE.Last().Transfer(ref nodes); }
public SuffixTrie() { this.root = new CharNode('$'); }
public CharNode(CharNode rhs) { Value = rhs.Value; }
public CharNode(CharNode rhs) { value = rhs.value; }