From 79225e0596533916415bd3d9c5a9c8f013a8dfd3 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Sat, 1 Feb 2025 19:26:56 +0000 Subject: [PATCH 1/4] Start to prepare PrefixNode for dynamic updating --- plugins/word-completion/prefix-tree-node.vala | 43 +++++++++++++++++-- plugins/word-completion/prefix-tree.vala | 5 ++- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/plugins/word-completion/prefix-tree-node.vala b/plugins/word-completion/prefix-tree-node.vala index 89da8d0c6b..751b4bf9ca 100644 --- a/plugins/word-completion/prefix-tree-node.vala +++ b/plugins/word-completion/prefix-tree-node.vala @@ -20,16 +20,51 @@ */ public class Scratch.Plugins.PrefixNode : Object { - public GLib.List children; + private enum NodeType { + ROOT, + CHAR, + WORD_END + } + + private const unichar WORD_END_CHAR = '\0'; + private unichar? uc = null; + private NodeType type = ROOT; + public Gee.ArrayList children; + public PrefixNode? parent { get; construct; default = null; } public unichar value { get; construct; } + public uint occurrences { get; set construct; default = 0; } + + public PrefixNode.from_unichar (unichar c, PrefixNode? _parent) requires (c != WORD_END_CHAR) { + Object ( + value: c, + parent: _parent, + occurrences: 1 + ); + + uc = c; + type = CHAR; + } - public PrefixNode (unichar c = '\0') { + public PrefixNode.root () { Object ( - value: c + parent: null, + occurrences: 0 ); + + type = ROOT; + } + + public PrefixNode.word_end (PrefixNode _parent) { + Object ( + parent: _parent, + occurrences: 1 + ); + + uc = WORD_END_CHAR; + type = WORD_END; } 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) { From dc4372c0a3d0746fd42bbf18dbb1fdd51984cbd0 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Sun, 2 Feb 2025 12:00:32 +0000 Subject: [PATCH 2/4] Make members properties --- plugins/word-completion/prefix-tree-node.vala | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/plugins/word-completion/prefix-tree-node.vala b/plugins/word-completion/prefix-tree-node.vala index 751b4bf9ca..bdbe6d2249 100644 --- a/plugins/word-completion/prefix-tree-node.vala +++ b/plugins/word-completion/prefix-tree-node.vala @@ -20,48 +20,47 @@ */ public class Scratch.Plugins.PrefixNode : Object { - private enum NodeType { + public enum NodeType { ROOT, CHAR, WORD_END } private const unichar WORD_END_CHAR = '\0'; - private unichar? uc = null; - private NodeType type = ROOT; - public Gee.ArrayList children; - public PrefixNode? parent { get; construct; default = null; } + public unichar uc { get; construct; } + public NodeType node_type { get; construct; } + public PrefixNode? parent { get; construct; } public unichar value { get; construct; } public uint occurrences { get; set construct; default = 0; } + public Gee.ArrayList children; + public PrefixNode.from_unichar (unichar c, PrefixNode? _parent) requires (c != WORD_END_CHAR) { Object ( value: c, parent: _parent, + uc: c, + node_type: NodeType.CHAR, occurrences: 1 ); - - uc = c; - type = CHAR; } public PrefixNode.root () { Object ( parent: null, + uc: WORD_END_CHAR, + node_type: NodeType.ROOT, occurrences: 0 ); - - type = ROOT; } public PrefixNode.word_end (PrefixNode _parent) { Object ( parent: _parent, + uc: WORD_END_CHAR, + node_type: NodeType.WORD_END, occurrences: 1 ); - - uc = WORD_END_CHAR; - type = WORD_END; } construct { From 04d08cdc835cef6bab3706f3fd163587a8c7ec3f Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Sun, 2 Feb 2025 12:09:50 +0000 Subject: [PATCH 3/4] Make `occurrences` private --- plugins/word-completion/prefix-tree-node.vala | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/plugins/word-completion/prefix-tree-node.vala b/plugins/word-completion/prefix-tree-node.vala index bdbe6d2249..85503b8fb4 100644 --- a/plugins/word-completion/prefix-tree-node.vala +++ b/plugins/word-completion/prefix-tree-node.vala @@ -27,11 +27,12 @@ public class Scratch.Plugins.PrefixNode : Object { } 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 uint occurrences { get; set construct; default = 0; } public Gee.ArrayList children; @@ -40,8 +41,7 @@ public class Scratch.Plugins.PrefixNode : Object { value: c, parent: _parent, uc: c, - node_type: NodeType.CHAR, - occurrences: 1 + node_type: NodeType.CHAR ); } @@ -49,8 +49,7 @@ public class Scratch.Plugins.PrefixNode : Object { Object ( parent: null, uc: WORD_END_CHAR, - node_type: NodeType.ROOT, - occurrences: 0 + node_type: NodeType.ROOT ); } @@ -58,9 +57,10 @@ public class Scratch.Plugins.PrefixNode : Object { Object ( parent: _parent, uc: WORD_END_CHAR, - node_type: NodeType.WORD_END, - occurrences: 1 + node_type: NodeType.WORD_END ); + + occurrences = 1; } construct { From 5c414a2d12f4a5fd2d02597b7cd8403ba9d71e58 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Sun, 2 Feb 2025 19:32:28 +0000 Subject: [PATCH 4/4] Fix lint --- plugins/word-completion/prefix-tree-node.vala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/word-completion/prefix-tree-node.vala b/plugins/word-completion/prefix-tree-node.vala index 85503b8fb4..d64ba5c0de 100644 --- a/plugins/word-completion/prefix-tree-node.vala +++ b/plugins/word-completion/prefix-tree-node.vala @@ -27,7 +27,7 @@ public class Scratch.Plugins.PrefixNode : Object { } private const unichar WORD_END_CHAR = '\0'; - private uint occurrences; // Only used for WORD_END nodes + private uint occurrences; // Only used for WORD_END nodes public unichar uc { get; construct; } public NodeType node_type { get; construct; }