diff --git a/plugins/word-completion/prefix-tree-node.vala b/plugins/word-completion/prefix-tree-node.vala index 89da8d0c6b..d64ba5c0de 100644 --- a/plugins/word-completion/prefix-tree-node.vala +++ b/plugins/word-completion/prefix-tree-node.vala @@ -20,16 +20,50 @@ */ public class Scratch.Plugins.PrefixNode : Object { - public GLib.List children; + public enum NodeType { + ROOT, + CHAR, + WORD_END + } + + private const unichar WORD_END_CHAR = '\0'; + private uint occurrences; // Only used for WORD_END nodes + + public unichar uc { get; construct; } + public NodeType node_type { get; construct; } + public PrefixNode? parent { get; construct; } public unichar value { get; construct; } - public PrefixNode (unichar c = '\0') { + public Gee.ArrayList children; + + public PrefixNode.from_unichar (unichar c, PrefixNode? _parent) requires (c != WORD_END_CHAR) { Object ( - value: c + value: c, + parent: _parent, + uc: c, + node_type: NodeType.CHAR ); } + public PrefixNode.root () { + Object ( + parent: null, + uc: WORD_END_CHAR, + node_type: NodeType.ROOT + ); + } + + public PrefixNode.word_end (PrefixNode _parent) { + Object ( + parent: _parent, + uc: WORD_END_CHAR, + node_type: NodeType.WORD_END + ); + + occurrences = 1; + } + construct { - children = new List (); + children = new Gee.ArrayList (); } } diff --git a/plugins/word-completion/prefix-tree.vala b/plugins/word-completion/prefix-tree.vala index 28681f6ea8..916ed9612d 100644 --- a/plugins/word-completion/prefix-tree.vala +++ b/plugins/word-completion/prefix-tree.vala @@ -36,8 +36,9 @@ namespace Scratch.Plugins { } } - var new_child = new PrefixNode (curr); - node.children.insert_sorted (new_child, (c1, c2) => { + var new_child = new PrefixNode.from_unichar (curr, null); + node.children.insert (0, new_child); + node.children.sort ((c1, c2) => { if (c1.value > c2.value) { return 1; } else if (c1.value == c2.value) {