From 34d2b3031f56dc683dffb137efe5e585abb0c595 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Sat, 13 Apr 2024 14:56:54 +0200 Subject: [PATCH 01/31] Tree - WIP --- source/niknaks/containers.d | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/source/niknaks/containers.d b/source/niknaks/containers.d index dd255bc..14d55d2 100644 --- a/source/niknaks/containers.d +++ b/source/niknaks/containers.d @@ -589,4 +589,18 @@ unittest // Destroy the map (such that it ends the sweeper destroy(map); +} + + +public class Tree(T) +{ + private T value; + private Tree!(T)[] children; + + this(T value) + { + this.value = value; + } + + public T[] dfs() } \ No newline at end of file From 60028a34be90a65099cd2fffa5baeb848c5d130a Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Mon, 22 Apr 2024 17:02:24 +0200 Subject: [PATCH 02/31] Tree - Added initial dfs --- source/niknaks/containers.d | 61 ++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/source/niknaks/containers.d b/source/niknaks/containers.d index 14d55d2..c168494 100644 --- a/source/niknaks/containers.d +++ b/source/niknaks/containers.d @@ -591,6 +591,32 @@ unittest destroy(map); } +// TODO: make delegate kak +// public interface InclusionStratergy(T) +// { +// public bool include(T item); +// } + +// private class AlwaysStrat(T) : InclusionStratergy +// { +// public override bool include(T item) +// { +// return true; +// } +// } + +public template Always(T) +{ + public bool always(T) + { + return true; + } +} + +public template InclusionStratergy(T) +{ + public alias InclusionStratergy = bool delegate(T item); +} public class Tree(T) { @@ -602,5 +628,38 @@ public class Tree(T) this.value = value; } - public T[] dfs() + public T[] dfs(InclusionStratergy!(T) strat) + { + T[] collected; + foreach(Tree!(T) child; this.children) + { + if(strat(child.value)) + { + collected ~= child.dfs(strat); + + } + } + + if(strat(this.value)) + { + collected ~= this.value; + } + + return collected; + } +} + + +version(unittest) +{ + import std.functional : toDelegate; + import std.stdio : writeln; +} + +unittest +{ + Tree!(string) treeOfStrings = new Tree!(string)("Top"); + + string[] result = treeOfStrings.dfs(toDelegate(&Always!(string).always)); + writeln("dfs: ", result); } \ No newline at end of file From 84f18407e866bdbdbb16eff996a5011b1c8c7bb7 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Mon, 22 Apr 2024 17:03:30 +0200 Subject: [PATCH 03/31] Tree - By default use the `always` strat --- source/niknaks/containers.d | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/niknaks/containers.d b/source/niknaks/containers.d index c168494..53f2afe 100644 --- a/source/niknaks/containers.d +++ b/source/niknaks/containers.d @@ -628,7 +628,7 @@ public class Tree(T) this.value = value; } - public T[] dfs(InclusionStratergy!(T) strat) + public T[] dfs(InclusionStratergy!(T) strat = toDelegate(&Always!(T).always)) { T[] collected; foreach(Tree!(T) child; this.children) @@ -660,6 +660,6 @@ unittest { Tree!(string) treeOfStrings = new Tree!(string)("Top"); - string[] result = treeOfStrings.dfs(toDelegate(&Always!(string).always)); + string[] result = treeOfStrings.dfs(); writeln("dfs: ", result); } \ No newline at end of file From 4ecb57d4fa3576a9538282bca1fe9ebefc0ad0f1 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Mon, 22 Apr 2024 20:14:26 +0200 Subject: [PATCH 04/31] Tree - Added a TODO --- source/niknaks/containers.d | 1 + 1 file changed, 1 insertion(+) diff --git a/source/niknaks/containers.d b/source/niknaks/containers.d index 53f2afe..39ad66c 100644 --- a/source/niknaks/containers.d +++ b/source/niknaks/containers.d @@ -618,6 +618,7 @@ public template InclusionStratergy(T) public alias InclusionStratergy = bool delegate(T item); } +// TODO: Technically this is a graph public class Tree(T) { private T value; From d5b00c548e885ad0b4d0f78d61f5cc51a622b995 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Mon, 22 Apr 2024 20:18:50 +0200 Subject: [PATCH 05/31] Tree - Added ability to append Tree (unittests) - Updated unittests to test appending --- source/niknaks/containers.d | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/source/niknaks/containers.d b/source/niknaks/containers.d index 39ad66c..22cec7c 100644 --- a/source/niknaks/containers.d +++ b/source/niknaks/containers.d @@ -629,6 +629,16 @@ public class Tree(T) this.value = value; } + public void appendValue(T value) + { + + } + + public void appendNode(Tree!(T) node) + { + this.children ~= node; + } + public T[] dfs(InclusionStratergy!(T) strat = toDelegate(&Always!(T).always)) { T[] collected; @@ -661,6 +671,15 @@ unittest { Tree!(string) treeOfStrings = new Tree!(string)("Top"); + Tree!(string) subtree_1 = new Tree!(string)("1"); + Tree!(string) subtree_2 = new Tree!(string)("2"); + Tree!(string) subtree_3 = new Tree!(string)("3"); + + treeOfStrings.appendNode(subtree_1); + treeOfStrings.appendNode(subtree_2); + treeOfStrings.appendNode(subtree_3); + + string[] result = treeOfStrings.dfs(); writeln("dfs: ", result); } \ No newline at end of file From f49ecf190812e56134388e5cface8110dfcc15ce Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Mon, 22 Apr 2024 22:14:28 +0200 Subject: [PATCH 06/31] InclusionStratergy - Now uses the `TreeNode!(T)` instead of the `T` itself --- source/niknaks/containers.d | 59 ++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 7 deletions(-) diff --git a/source/niknaks/containers.d b/source/niknaks/containers.d index 22cec7c..5c13fdc 100644 --- a/source/niknaks/containers.d +++ b/source/niknaks/containers.d @@ -607,17 +607,38 @@ unittest public template Always(T) { - public bool always(T) + public bool Always(Tree!(T) treeNode) { + version(unittest) + { + import std.stdio : writeln; + writeln("Strat for: ", treeNode); + } return true; } } +public template Nothing(T) +{ + public void Nothing(Tree!(T) treeNode) + { + + } +} + public template InclusionStratergy(T) { - public alias InclusionStratergy = bool delegate(T item); + public alias InclusionStratergy = bool delegate(Tree!(T) item); } +// Called prior to visitation? +public template TouchStratergy(T) +{ + public alias TouchStratergy = void delegate(Tree!(T) item); +} + +import std.string : format; + // TODO: Technically this is a graph public class Tree(T) { @@ -639,25 +660,41 @@ public class Tree(T) this.children ~= node; } - public T[] dfs(InclusionStratergy!(T) strat = toDelegate(&Always!(T).always)) + public T[] dfs + ( + InclusionStratergy!(T) strat = toDelegate(&Always!(T)), + TouchStratergy!(T) touch = toDelegate(&Nothing!(T)) + ) { T[] collected; foreach(Tree!(T) child; this.children) { - if(strat(child.value)) + if(strat(child)) { + // Touch + touch(child); + + // Visit collected ~= child.dfs(strat); - } } - if(strat(this.value)) + if(strat(this)) { + // Touch + touch(this); + + // "Visit" collected ~= this.value; } return collected; } + + public override string toString() + { + return format("TreeNode [val: %s]", this.value); + } } @@ -665,6 +702,11 @@ version(unittest) { import std.functional : toDelegate; import std.stdio : writeln; + + private void DebugTouch(T)(Tree!(T) node) + { + writeln("Touching tree node ", node); + } } unittest @@ -680,6 +722,9 @@ unittest treeOfStrings.appendNode(subtree_3); - string[] result = treeOfStrings.dfs(); + InclusionStratergy!(string) strat = toDelegate(&Always!(string)); + TouchStratergy!(string) touch = toDelegate(&DebugTouch!(string)); + + string[] result = treeOfStrings.dfs(strat, touch); writeln("dfs: ", result); } \ No newline at end of file From 167c2b7c0de277dffdb7b4900a7c1403b95eb9bb Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Tue, 23 Apr 2024 15:49:49 +0200 Subject: [PATCH 07/31] VisitationTree - Working on a visitation tree implementation --- source/niknaks/containers.d | 52 +++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/source/niknaks/containers.d b/source/niknaks/containers.d index 5c13fdc..518c943 100644 --- a/source/niknaks/containers.d +++ b/source/niknaks/containers.d @@ -727,4 +727,56 @@ unittest string[] result = treeOfStrings.dfs(strat, touch); writeln("dfs: ", result); +} + +public class VisitationTree(T) : Tree!(T) +{ + private bool visisted; + + this(T value) + { + super(value); + } + + public T[] linearize() + { + return dfs(toDelegate(&_shouldVisit), toDelegate(&_touch)); + } + + private static bool _shouldVisit(Tree!(T) tnode) + { + VisitationTree!(T) vnode = cast(VisitationTree!(T))tnode; + return vnode && !vnode.isVisited(); + } + + private static void _touch(Tree!(T) tnode) + { + VisitationTree!(T) vnode = cast(VisitationTree!(T))tnode; + if(vnode) + { + vnode.mark(); + } + } + + private void mark() + { + this.visisted = true; + } + + private bool isVisited() + { + return this.visisted; + } +} + +unittest +{ + VisitationTree!(string) root = new VisitationTree!(string)("root"); + + VisitationTree!(string) thing = new VisitationTree!(string)("subtree"); + root.appendNode(thing); + thing.appendNode(root); + + writeln(root.linearize()); + } \ No newline at end of file From 04edc241adb1635bf6de60b64d80c67e101a46c5 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Tue, 23 Apr 2024 15:52:02 +0200 Subject: [PATCH 08/31] Tree - Pass in, explcitly, the touch startergy --- source/niknaks/containers.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/niknaks/containers.d b/source/niknaks/containers.d index 518c943..7d7eee8 100644 --- a/source/niknaks/containers.d +++ b/source/niknaks/containers.d @@ -675,7 +675,7 @@ public class Tree(T) touch(child); // Visit - collected ~= child.dfs(strat); + collected ~= child.dfs(strat, touch); } } From ce78102af9943e823095f89fb717149eb1c6fea7 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Tue, 23 Apr 2024 16:02:40 +0200 Subject: [PATCH 09/31] Tree - Correct visitation stratergy --- source/niknaks/containers.d | 42 +++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/source/niknaks/containers.d b/source/niknaks/containers.d index 7d7eee8..20b128f 100644 --- a/source/niknaks/containers.d +++ b/source/niknaks/containers.d @@ -666,27 +666,47 @@ public class Tree(T) TouchStratergy!(T) touch = toDelegate(&Nothing!(T)) ) { + version(unittest) + { + writeln("dfs entry: ", this); + } + T[] collected; - foreach(Tree!(T) child; this.children) + scope(exit) + { + version(unittest) + { + writeln("leaving node ", this, " with collected ", collected); + } + } + + // Touch + touch(this); // root[x] + + foreach(Tree!(T) child; this.children) // subtree[x], { if(strat(child)) { - // Touch - touch(child); + version(unittest) + { + writeln("dfs, strat good for child: ", child); + } // Visit collected ~= child.dfs(strat, touch); } + else + { + version(unittest) + { + writeln("dfs, strat ignored for child: ", child); + } + } } - if(strat(this)) - { - // Touch - touch(this); - - // "Visit" - collected ~= this.value; - } + // "Visit" + collected ~= this.value; + return collected; } From e44caf3643a355d07dc1a3903c16c428fa85e89b Mon Sep 17 00:00:00 2001 From: Tristan Brice Velloza Kildaire Date: Fri, 26 Apr 2024 15:35:29 +0200 Subject: [PATCH 10/31] VisitationTree (unittests) - Added missing assertions --- source/niknaks/containers.d | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/source/niknaks/containers.d b/source/niknaks/containers.d index 20b128f..05b6cee 100644 --- a/source/niknaks/containers.d +++ b/source/niknaks/containers.d @@ -797,6 +797,10 @@ unittest root.appendNode(thing); thing.appendNode(root); - writeln(root.linearize()); + string[] linearized = root.linearize(); + writeln(linearized); + + assert(linearized[0] == "subtree"); + assert(linearized[1] == "root"); } \ No newline at end of file From 838ddbb9db75e07f21201c5db9f6c603aadf7008 Mon Sep 17 00:00:00 2001 From: Tristan Brice Velloza Kildaire Date: Fri, 26 Apr 2024 15:36:54 +0200 Subject: [PATCH 11/31] Tree (unittests) - Added missing assertions --- source/niknaks/containers.d | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/source/niknaks/containers.d b/source/niknaks/containers.d index 05b6cee..e0dbd26 100644 --- a/source/niknaks/containers.d +++ b/source/niknaks/containers.d @@ -747,6 +747,11 @@ unittest string[] result = treeOfStrings.dfs(strat, touch); writeln("dfs: ", result); + + assert(result[0] == "1"); + assert(result[1] == "2"); + assert(result[2] == "3"); + assert(result[3] == "Top"); } public class VisitationTree(T) : Tree!(T) From 41ebad076802aca2d1a8ae83b26530c06913b428 Mon Sep 17 00:00:00 2001 From: Tristan Brice Velloza Kildaire Date: Fri, 26 Apr 2024 16:05:10 +0200 Subject: [PATCH 12/31] Methods - Added rightward shifting mechanism --- source/niknaks/containers.d | 121 +++++++++++++++++++++++++++++++++++- 1 file changed, 120 insertions(+), 1 deletion(-) diff --git a/source/niknaks/containers.d b/source/niknaks/containers.d index e0dbd26..6f36008 100644 --- a/source/niknaks/containers.d +++ b/source/niknaks/containers.d @@ -591,6 +591,105 @@ unittest destroy(map); } +// Given [0, 1, 5] +// and shift right at index 1 +// then 0 moves into 1's place +// 0's position is then filled with T.init + +public T[] shift(T)(T[] array, size_t position, bool rightwards = false, bool shrink = false) +{ + // Out of range + if(position >= array.length) + { + return array; + } + + // if rightwards + if(rightwards) + { + // nothing further left than index 0 + if(!position) + { + return array; + } + + for(size_t i = position; i > 0; i--) + { + array[i] = array[i-1]; + } + + // no shrink, then fill with T.init + if(!shrink) + { + array[0] = T.init; + } + // chomp left-hand side + else + { + array = array[1..$]; + } + } + + return array; +} + +// rightwards testung (no shrink) +unittest +{ + int[] numbas = [1, 5, 2]; + numbas = numbas.shift(1, true); + + // should now be [0, 1, 2] + writeln(numbas); + assert(numbas == [0, 1, 2]); + + numbas = [1, 5, 2]; + numbas = numbas.shift(0, true); + + // should now be [1, 5, 2] + writeln(numbas); + assert(numbas == [1, 5, 2]); + + numbas = [1, 5, 2]; + numbas = numbas.shift(2, true); + + // should now be [0, 1, 5] + writeln(numbas); + assert(numbas == [0, 1, 5]); + + numbas = [1, 2]; + numbas = numbas.shift(1, true); + + // should now be [0, 1] + writeln(numbas); + assert(numbas == [0, 1]); + + numbas = [1, 2]; + numbas = numbas.shift(0, true); + + // should now be [1, 2] + writeln(numbas); + assert(numbas == [1, 2]); + + numbas = []; + numbas = numbas.shift(0, true); + + // should now be [] + writeln(numbas); + assert(numbas == []); +} + +public T[] remove(T)(T[] array, size_t idx) +{ + // Return your array on this + if(!(idx < array.length)) + { + return array; + } + + return null; +} + // TODO: make delegate kak // public interface InclusionStratergy(T) // { @@ -650,16 +749,36 @@ public class Tree(T) this.value = value; } - public void appendValue(T value) + this() { } + public void setValue(T value) + { + this.value = value; + } + public void appendNode(Tree!(T) node) { this.children ~= node; } + public bool removeNode(Tree!(T) node) + { + bool found = false; + for(size_t i = 0; i < this.children.length; i++) + { + found = this.children[i] == node; + if(found) + { + + } + } + + return true; + } + public T[] dfs ( InclusionStratergy!(T) strat = toDelegate(&Always!(T)), From 11fd2f856e3b51d5423d6205afa94c1b88bdb314 Mon Sep 17 00:00:00 2001 From: Tristan Brice Velloza Kildaire Date: Fri, 26 Apr 2024 16:28:19 +0200 Subject: [PATCH 13/31] Methods - Added leftwards shifting mechanism --- source/niknaks/containers.d | 92 ++++++++++++++++++++++++++++++------- 1 file changed, 76 insertions(+), 16 deletions(-) diff --git a/source/niknaks/containers.d b/source/niknaks/containers.d index 6f36008..0fc8883 100644 --- a/source/niknaks/containers.d +++ b/source/niknaks/containers.d @@ -596,7 +596,7 @@ unittest // then 0 moves into 1's place // 0's position is then filled with T.init -public T[] shift(T)(T[] array, size_t position, bool rightwards = false, bool shrink = false) +public T[] shiftInto(T)(T[] array, size_t position, bool rightwards = false, bool shrink = false) { // Out of range if(position >= array.length) @@ -629,6 +629,32 @@ public T[] shift(T)(T[] array, size_t position, bool rightwards = false, bool sh array = array[1..$]; } } + // if leftwards + else + { + // nothing furtherright + if(position == array.length-1) + { + return array; + } + + for(size_t i = position; i < array.length-1; i++) + { + array[i] = array[i+1]; + } + + // no shrink, then fill with T.init + if(!shrink) + { + array[$-1] = T.init; + } + // chomp right-hand side + else + { + array = array[0..$-1]; + } + } + return array; } @@ -637,57 +663,83 @@ public T[] shift(T)(T[] array, size_t position, bool rightwards = false, bool sh unittest { int[] numbas = [1, 5, 2]; - numbas = numbas.shift(1, true); + numbas = numbas.shiftInto(1, true); // should now be [0, 1, 2] writeln(numbas); assert(numbas == [0, 1, 2]); numbas = [1, 5, 2]; - numbas = numbas.shift(0, true); + numbas = numbas.shiftInto(0, true); // should now be [1, 5, 2] writeln(numbas); assert(numbas == [1, 5, 2]); numbas = [1, 5, 2]; - numbas = numbas.shift(2, true); + numbas = numbas.shiftInto(2, true); // should now be [0, 1, 5] writeln(numbas); assert(numbas == [0, 1, 5]); numbas = [1, 2]; - numbas = numbas.shift(1, true); + numbas = numbas.shiftInto(1, true); // should now be [0, 1] writeln(numbas); assert(numbas == [0, 1]); numbas = [1, 2]; - numbas = numbas.shift(0, true); + numbas = numbas.shiftInto(0, true); // should now be [1, 2] writeln(numbas); assert(numbas == [1, 2]); numbas = []; - numbas = numbas.shift(0, true); + numbas = numbas.shiftInto(0, false); // should now be [] writeln(numbas); assert(numbas == []); } -public T[] remove(T)(T[] array, size_t idx) +// leftwards testung (no shrink) +unittest { - // Return your array on this - if(!(idx < array.length)) - { - return array; - } + int[] numbas = [1, 5, 2]; + numbas = numbas.shiftInto(1, false); - return null; + // should now be [1, 2, 0] + writeln(numbas); + assert(numbas == [1, 2, 0]); + + numbas = [1, 5, 2]; + numbas = numbas.shiftInto(0, false); + + // should now be [5, 2, 0] + writeln(numbas); + assert(numbas == [5, 2, 0]); + + numbas = [1, 5, 2]; + numbas = numbas.shiftInto(2, false); + + // should now be [1, 5, 2] + writeln(numbas); + assert(numbas == [1, 5, 2]); + + numbas = []; + numbas = numbas.shiftInto(0, true); + + // should now be [] + writeln(numbas); + assert(numbas == []); +} + +public T[] removeResize(T)(T[] array, size_t position) +{ + return array.shiftInto(position, false, true); } // TODO: make delegate kak @@ -767,16 +819,24 @@ public class Tree(T) public bool removeNode(Tree!(T) node) { bool found = false; + size_t idx; for(size_t i = 0; i < this.children.length; i++) { found = this.children[i] == node; if(found) { - + idx = i; + break; } } - return true; + // if(found) + // { + // this.children = this.children.removeResize(idx, true, true); + // return true; + // } + + return false; } public T[] dfs From 4a431c43d104189a083b42b3e6312252893d5c33 Mon Sep 17 00:00:00 2001 From: Tristan Brice Velloza Kildaire Date: Fri, 26 Apr 2024 16:35:21 +0200 Subject: [PATCH 14/31] TreeNode - Added removal - Added indexing support --- source/niknaks/containers.d | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/source/niknaks/containers.d b/source/niknaks/containers.d index 0fc8883..1eaff9f 100644 --- a/source/niknaks/containers.d +++ b/source/niknaks/containers.d @@ -830,15 +830,25 @@ public class Tree(T) } } - // if(found) - // { - // this.children = this.children.removeResize(idx, true, true); - // return true; - // } + if(found) + { + this.children = this.children.removeResize(idx); + return true; + } return false; } + public T opIndex(size_t idx) + { + return idx < this.children.length ? this.children[idx].getValue() : T.init; + } + + public T getValue() + { + return this.value; + } + public T[] dfs ( InclusionStratergy!(T) strat = toDelegate(&Always!(T)), From 8dca2ebf732559961d9b9fbf3a2c9b8b498c10a4 Mon Sep 17 00:00:00 2001 From: Tristan Brice Velloza Kildaire Date: Fri, 26 Apr 2024 16:50:01 +0200 Subject: [PATCH 15/31] Tree - Reworking opSlicwe --- source/niknaks/containers.d | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/source/niknaks/containers.d b/source/niknaks/containers.d index 1eaff9f..29feea7 100644 --- a/source/niknaks/containers.d +++ b/source/niknaks/containers.d @@ -839,9 +839,41 @@ public class Tree(T) return false; } - public T opIndex(size_t idx) + // public T opIndex(size_t idx) + // { + // return idx < this.children.length ? this.children[idx].getValue() : T.init; + // } + + private bool isTreeNodeType(E)() { - return idx < this.children.length ? this.children[idx].getValue() : T.init; + return __traits(isSame, E, Tree!(T)[]); + } + + private bool isTreeValueType(E)() + { + return __traits(isSame, E, T[]); + } + + public E opSlice(E)() + if(isTreeNodeType!(E) || isTreeValueType!(E)) + { + // If the children as tree nodes is requested + static if(isTreeNodeType!(E)) + { + return this.children; + } + else static if(isTreeValueType!(E)) + { + T[] slice; + foreach(Tree!(Tree) tnode; this.children) + { + slice ~= tnode.getValue(); + } + return slice; + import std.algorithm.iteration : map; + + return map!(&getValue, this.children); + } } public T getValue() From d7a830ffa96fed0372d51e23e60f3fa8030f7009 Mon Sep 17 00:00:00 2001 From: Tristan Brice Velloza Kildaire Date: Fri, 26 Apr 2024 18:22:31 +0200 Subject: [PATCH 16/31] Tree - opSlice done --- source/niknaks/containers.d | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/source/niknaks/containers.d b/source/niknaks/containers.d index 29feea7..ccbe5c4 100644 --- a/source/niknaks/containers.d +++ b/source/niknaks/containers.d @@ -844,17 +844,17 @@ public class Tree(T) // return idx < this.children.length ? this.children[idx].getValue() : T.init; // } - private bool isTreeNodeType(E)() + private static bool isTreeNodeType(E)() { - return __traits(isSame, E, Tree!(T)[]); + return __traits(isSame, E, Tree!(T)); } - private bool isTreeValueType(E)() + private static bool isTreeValueType(E)() { - return __traits(isSame, E, T[]); + return __traits(isSame, E, T); } - public E opSlice(E)() + public E[] opSlice(E)() if(isTreeNodeType!(E) || isTreeValueType!(E)) { // If the children as tree nodes is requested @@ -865,17 +865,21 @@ public class Tree(T) else static if(isTreeValueType!(E)) { T[] slice; - foreach(Tree!(Tree) tnode; this.children) + foreach(Tree!(T) tnode; this.children) { slice ~= tnode.getValue(); } return slice; - import std.algorithm.iteration : map; - - return map!(&getValue, this.children); + // import std.algorithm.iteration : map; + // return map!(getValue)(this.children)[]; } } + // public E opIndex(E)() + // { + + // } + public T getValue() { return this.value; @@ -973,6 +977,13 @@ unittest assert(result[1] == "2"); assert(result[2] == "3"); assert(result[3] == "Top"); + + + auto i = treeOfStrings.opSlice!(Tree!(string))(); + writeln("Siblings: ", i); + + auto p = treeOfStrings.opSlice!(string)(); + writeln("Siblings (vals): ", p); } public class VisitationTree(T) : Tree!(T) From 54b66ec3f114f5efb4f1706ad38a0799cc9334c7 Mon Sep 17 00:00:00 2001 From: Tristan Brice Velloza Kildaire Date: Fri, 26 Apr 2024 18:24:31 +0200 Subject: [PATCH 17/31] Tree - Added normal opSlice as well --- source/niknaks/containers.d | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/source/niknaks/containers.d b/source/niknaks/containers.d index ccbe5c4..4dd2780 100644 --- a/source/niknaks/containers.d +++ b/source/niknaks/containers.d @@ -875,6 +875,11 @@ public class Tree(T) } } + public T[] opSlice() + { + return opSlice!(T)(); + } + // public E opIndex(E)() // { @@ -984,6 +989,7 @@ unittest auto p = treeOfStrings.opSlice!(string)(); writeln("Siblings (vals): ", p); + assert(p == treeOfStrings[]); } public class VisitationTree(T) : Tree!(T) From 450d811f524fdab3ec5892cc66461cb8014a2bf6 Mon Sep 17 00:00:00 2001 From: Tristan Brice Velloza Kildaire Date: Fri, 26 Apr 2024 18:25:17 +0200 Subject: [PATCH 18/31] Tree (unittests) - Updated test for parametwerized opSlice --- source/niknaks/containers.d | 3 +++ 1 file changed, 3 insertions(+) diff --git a/source/niknaks/containers.d b/source/niknaks/containers.d index 4dd2780..29ecbc5 100644 --- a/source/niknaks/containers.d +++ b/source/niknaks/containers.d @@ -986,6 +986,9 @@ unittest auto i = treeOfStrings.opSlice!(Tree!(string))(); writeln("Siblings: ", i); + assert(i[0] == subtree_1); + assert(i[1] == subtree_2); + assert(i[2] == subtree_3); auto p = treeOfStrings.opSlice!(string)(); writeln("Siblings (vals): ", p); From 04c5373a694fe62cca5e2fbeeea90a70f7c68c67 Mon Sep 17 00:00:00 2001 From: Tristan Brice Velloza Kildaire Date: Fri, 26 Apr 2024 18:28:06 +0200 Subject: [PATCH 19/31] Tree - Added opIndex --- source/niknaks/containers.d | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/source/niknaks/containers.d b/source/niknaks/containers.d index 29ecbc5..9fe5199 100644 --- a/source/niknaks/containers.d +++ b/source/niknaks/containers.d @@ -862,6 +862,7 @@ public class Tree(T) { return this.children; } + // If the children as values themselves is requested else static if(isTreeValueType!(E)) { T[] slice; @@ -880,10 +881,25 @@ public class Tree(T) return opSlice!(T)(); } - // public E opIndex(E)() - // { + public E opIndex(E)(size_t idx) + if(isTreeNodeType!(E) || isTreeValueType!(E)) + { + // If the cjild as a tree node is requested + static if(isTreeNodeType!(E)) + { + return this.children[idx]; + } + // If the child as a value itself is requested + else static if(isTreeValueType!(E)) + { + return this.children[idx].getValue(); + } + } - // } + public T opIndex(size_t idx) + { + return opIndex!(T)(idx); + } public T getValue() { From d3eec6eba6419f95e3ab8fd972ae6c0121627987 Mon Sep 17 00:00:00 2001 From: Tristan Brice Velloza Kildaire Date: Fri, 26 Apr 2024 18:30:28 +0200 Subject: [PATCH 20/31] Tree - Cleaned up - Removed `getValue()` Tree (unittests) - Added test for `removeNode(Tree!(T))` --- source/niknaks/containers.d | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/source/niknaks/containers.d b/source/niknaks/containers.d index 9fe5199..eb4547a 100644 --- a/source/niknaks/containers.d +++ b/source/niknaks/containers.d @@ -868,7 +868,7 @@ public class Tree(T) T[] slice; foreach(Tree!(T) tnode; this.children) { - slice ~= tnode.getValue(); + slice ~= tnode.value; } return slice; // import std.algorithm.iteration : map; @@ -892,7 +892,7 @@ public class Tree(T) // If the child as a value itself is requested else static if(isTreeValueType!(E)) { - return this.children[idx].getValue(); + return this.children[idx].value; } } @@ -901,11 +901,6 @@ public class Tree(T) return opIndex!(T)(idx); } - public T getValue() - { - return this.value; - } - public T[] dfs ( InclusionStratergy!(T) strat = toDelegate(&Always!(T)), @@ -1009,6 +1004,10 @@ unittest auto p = treeOfStrings.opSlice!(string)(); writeln("Siblings (vals): ", p); assert(p == treeOfStrings[]); + + + assert(treeOfStrings.removeNode(subtree_1)); + assert(!treeOfStrings.removeNode(subtree_1)); } public class VisitationTree(T) : Tree!(T) From 38169bbebd53127c4542b15ede7f676225d8502b Mon Sep 17 00:00:00 2001 From: Tristan Brice Velloza Kildaire Date: Sat, 27 Apr 2024 15:19:25 +0200 Subject: [PATCH 21/31] Containers - Added `shiftIntoLeftwards` and `shiftIntoRightwards` --- source/niknaks/containers.d | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/source/niknaks/containers.d b/source/niknaks/containers.d index a87bd25..afa51a6 100644 --- a/source/niknaks/containers.d +++ b/source/niknaks/containers.d @@ -667,6 +667,16 @@ public T[] shiftInto(T)(T[] array, size_t position, bool rightwards = false, boo return array; } +public T[] shiftIntoRightwards(T)(T[] array, size_t position, bool shrink = false) +{ + return shiftInto(array, position, true, shrink); +} + +public T[] shiftIntoLeftwards(T)(T[] array, size_t position, bool shrink = false) +{ + return shiftInto(array, position, false, shrink); +} + // rightwards testung (no shrink) unittest { From 5b205019f6be9b20ea3095b3cb0f27cce44b1de1 Mon Sep 17 00:00:00 2001 From: Tristan Brice Velloza Kildaire Date: Sat, 27 Apr 2024 15:20:59 +0200 Subject: [PATCH 22/31] Containers (unittests) - Use new methods --- source/niknaks/containers.d | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/source/niknaks/containers.d b/source/niknaks/containers.d index afa51a6..614b00f 100644 --- a/source/niknaks/containers.d +++ b/source/niknaks/containers.d @@ -681,42 +681,42 @@ public T[] shiftIntoLeftwards(T)(T[] array, size_t position, bool shrink = false unittest { int[] numbas = [1, 5, 2]; - numbas = numbas.shiftInto(1, true); + numbas = numbas.shiftIntoRightwards(1); // should now be [0, 1, 2] writeln(numbas); assert(numbas == [0, 1, 2]); numbas = [1, 5, 2]; - numbas = numbas.shiftInto(0, true); + numbas = numbas.shiftIntoRightwards(0); // should now be [1, 5, 2] writeln(numbas); assert(numbas == [1, 5, 2]); numbas = [1, 5, 2]; - numbas = numbas.shiftInto(2, true); + numbas = numbas.shiftIntoRightwards(2); // should now be [0, 1, 5] writeln(numbas); assert(numbas == [0, 1, 5]); numbas = [1, 2]; - numbas = numbas.shiftInto(1, true); + numbas = numbas.shiftIntoRightwards(1); // should now be [0, 1] writeln(numbas); assert(numbas == [0, 1]); numbas = [1, 2]; - numbas = numbas.shiftInto(0, true); + numbas = numbas.shiftIntoRightwards(0); // should now be [1, 2] writeln(numbas); assert(numbas == [1, 2]); numbas = []; - numbas = numbas.shiftInto(0, false); + numbas = numbas.shiftIntoRightwards(0); // should now be [] writeln(numbas); @@ -727,28 +727,28 @@ unittest unittest { int[] numbas = [1, 5, 2]; - numbas = numbas.shiftInto(1, false); + numbas = numbas.shiftIntoLeftwards(1); // should now be [1, 2, 0] writeln(numbas); assert(numbas == [1, 2, 0]); numbas = [1, 5, 2]; - numbas = numbas.shiftInto(0, false); + numbas = numbas.shiftIntoLeftwards(0); // should now be [5, 2, 0] writeln(numbas); assert(numbas == [5, 2, 0]); numbas = [1, 5, 2]; - numbas = numbas.shiftInto(2, false); + numbas = numbas.shiftIntoLeftwards(2); // should now be [1, 5, 2] writeln(numbas); assert(numbas == [1, 5, 2]); numbas = []; - numbas = numbas.shiftInto(0, true); + numbas = numbas.shiftIntoLeftwards(0); // should now be [] writeln(numbas); From 9d39cc3710b51803c69a8243f419f9e3dd1523f5 Mon Sep 17 00:00:00 2001 From: Tristan Brice Velloza Kildaire Date: Sat, 27 Apr 2024 15:22:57 +0200 Subject: [PATCH 23/31] Containers - Cleaned up --- source/niknaks/containers.d | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/source/niknaks/containers.d b/source/niknaks/containers.d index 614b00f..dc0547d 100644 --- a/source/niknaks/containers.d +++ b/source/niknaks/containers.d @@ -760,20 +760,6 @@ public T[] removeResize(T)(T[] array, size_t position) return array.shiftInto(position, false, true); } -// TODO: make delegate kak -// public interface InclusionStratergy(T) -// { -// public bool include(T item); -// } - -// private class AlwaysStrat(T) : InclusionStratergy -// { -// public override bool include(T item) -// { -// return true; -// } -// } - public template Always(T) { public bool Always(Tree!(T) treeNode) From b1f2cb594bd57b9ab62edc7d39d5f6240ee5d4ff Mon Sep 17 00:00:00 2001 From: Tristan Brice Velloza Kildaire Date: Sat, 27 Apr 2024 15:27:31 +0200 Subject: [PATCH 24/31] InclusionStratergy - Documented TouchStratergy - Documented --- source/niknaks/containers.d | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/source/niknaks/containers.d b/source/niknaks/containers.d index dc0547d..7b7b59d 100644 --- a/source/niknaks/containers.d +++ b/source/niknaks/containers.d @@ -781,12 +781,29 @@ public template Nothing(T) } } +/** + * The inclusion stratergy which + * will be called upon the tree + * node prior to it being visited + * during a dfs operation. + * + * It is a predicate to determine + * whether or not the tree node + * in concern should be recursed + * upon. + */ public template InclusionStratergy(T) { public alias InclusionStratergy = bool delegate(Tree!(T) item); } -// Called prior to visitation? +/** + * This is called on a tree node + * as part of the first action + * that takes place during the + * visitation of said node during + * a dfs operation. + */ public template TouchStratergy(T) { public alias TouchStratergy = void delegate(Tree!(T) item); From 457f876a95e50adae0f24a58fa8ff8696c01ca08 Mon Sep 17 00:00:00 2001 From: Tristan Brice Velloza Kildaire Date: Sat, 27 Apr 2024 15:38:10 +0200 Subject: [PATCH 25/31] Containers - Documented method --- source/niknaks/containers.d | 40 +++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/source/niknaks/containers.d b/source/niknaks/containers.d index 7b7b59d..7d70d6e 100644 --- a/source/niknaks/containers.d +++ b/source/niknaks/containers.d @@ -11,6 +11,7 @@ import std.datetime.stopwatch : StopWatch, AutoStart; import core.thread : Thread; import core.sync.condition : Condition; import std.functional : toDelegate; +import std.string : format; version(unittest) { @@ -604,7 +605,34 @@ unittest // then 0 moves into 1's place // 0's position is then filled with T.init -public T[] shiftInto(T)(T[] array, size_t position, bool rightwards = false, bool shrink = false) +/** + * Shifts a subset of the elements of + * the given array to a given position + * either from the left or right. + * + * Optionally allowing the shrinking + * of the array after the process, + * otherwise the last element shifted's + * previous value will be set to the + * value specified. + * + * Params: + * array = the input array + * position = the position to shift + * onto + * rightwards = if `true` then shift + * elements into the position rightwards, + * else leftwards (which is also the default) + * shrink = if set to `true` then + * the array will be resized to exclude + * the now "empty" element + * filler = the value to place in + * the space where the last element + * shifted no longer occupies, by default + * this is `T.init` + * Returns: the shifted array + */ +public T[] shiftInto(T)(T[] array, size_t position, bool rightwards = false, bool shrink = false, T filler = T.init) { // Out of range if(position >= array.length) @@ -626,10 +654,10 @@ public T[] shiftInto(T)(T[] array, size_t position, bool rightwards = false, boo array[i] = array[i-1]; } - // no shrink, then fill with T.init + // no shrink, then fill with filler if(!shrink) { - array[0] = T.init; + array[0] = filler; } // chomp left-hand side else @@ -651,10 +679,10 @@ public T[] shiftInto(T)(T[] array, size_t position, bool rightwards = false, boo array[i] = array[i+1]; } - // no shrink, then fill with T.init + // no shrink, then fill with filler if(!shrink) { - array[$-1] = T.init; + array[$-1] = filler; } // chomp right-hand side else @@ -809,7 +837,7 @@ public template TouchStratergy(T) public alias TouchStratergy = void delegate(Tree!(T) item); } -import std.string : format; + // TODO: Technically this is a graph public class Tree(T) From 4dceaad585ddb3f64c9538089f6fc32722a273c4 Mon Sep 17 00:00:00 2001 From: Tristan Brice Velloza Kildaire Date: Sat, 27 Apr 2024 15:39:36 +0200 Subject: [PATCH 26/31] niknaks.arrays - Moved here niknaks.containers - Moved here --- source/niknaks/arrays.d | 188 +++++++++++++++++++++++++++++++++++ source/niknaks/containers.d | 189 +----------------------------------- 2 files changed, 189 insertions(+), 188 deletions(-) diff --git a/source/niknaks/arrays.d b/source/niknaks/arrays.d index 3def1d6..395409e 100644 --- a/source/niknaks/arrays.d +++ b/source/niknaks/arrays.d @@ -247,4 +247,192 @@ unittest // TODO: See why not auto detecting the array type filter!(int)(vals, predicateOf!(onlyEven), vals_got); assert(vals_got == vals_expected); +} + +// Given [0, 1, 5] +// and shift right at index 1 +// then 0 moves into 1's place +// 0's position is then filled with T.init + +/** + * Shifts a subset of the elements of + * the given array to a given position + * either from the left or right. + * + * Optionally allowing the shrinking + * of the array after the process, + * otherwise the last element shifted's + * previous value will be set to the + * value specified. + * + * Params: + * array = the input array + * position = the position to shift + * onto + * rightwards = if `true` then shift + * elements into the position rightwards, + * else leftwards (which is also the default) + * shrink = if set to `true` then + * the array will be resized to exclude + * the now "empty" element + * filler = the value to place in + * the space where the last element + * shifted no longer occupies, by default + * this is `T.init` + * Returns: the shifted array + */ +public T[] shiftInto(T)(T[] array, size_t position, bool rightwards = false, bool shrink = false, T filler = T.init) +{ + // Out of range + if(position >= array.length) + { + return array; + } + + // if rightwards + if(rightwards) + { + // nothing further left than index 0 + if(!position) + { + return array; + } + + for(size_t i = position; i > 0; i--) + { + array[i] = array[i-1]; + } + + // no shrink, then fill with filler + if(!shrink) + { + array[0] = filler; + } + // chomp left-hand side + else + { + array = array[1..$]; + } + } + // if leftwards + else + { + // nothing furtherright + if(position == array.length-1) + { + return array; + } + + for(size_t i = position; i < array.length-1; i++) + { + array[i] = array[i+1]; + } + + // no shrink, then fill with filler + if(!shrink) + { + array[$-1] = filler; + } + // chomp right-hand side + else + { + array = array[0..$-1]; + } + } + + + return array; +} + +public T[] shiftIntoRightwards(T)(T[] array, size_t position, bool shrink = false) +{ + return shiftInto(array, position, true, shrink); +} + +public T[] shiftIntoLeftwards(T)(T[] array, size_t position, bool shrink = false) +{ + return shiftInto(array, position, false, shrink); +} + +// rightwards testung (no shrink) +unittest +{ + int[] numbas = [1, 5, 2]; + numbas = numbas.shiftIntoRightwards(1); + + // should now be [0, 1, 2] + writeln(numbas); + assert(numbas == [0, 1, 2]); + + numbas = [1, 5, 2]; + numbas = numbas.shiftIntoRightwards(0); + + // should now be [1, 5, 2] + writeln(numbas); + assert(numbas == [1, 5, 2]); + + numbas = [1, 5, 2]; + numbas = numbas.shiftIntoRightwards(2); + + // should now be [0, 1, 5] + writeln(numbas); + assert(numbas == [0, 1, 5]); + + numbas = [1, 2]; + numbas = numbas.shiftIntoRightwards(1); + + // should now be [0, 1] + writeln(numbas); + assert(numbas == [0, 1]); + + numbas = [1, 2]; + numbas = numbas.shiftIntoRightwards(0); + + // should now be [1, 2] + writeln(numbas); + assert(numbas == [1, 2]); + + numbas = []; + numbas = numbas.shiftIntoRightwards(0); + + // should now be [] + writeln(numbas); + assert(numbas == []); +} + +// leftwards testung (no shrink) +unittest +{ + int[] numbas = [1, 5, 2]; + numbas = numbas.shiftIntoLeftwards(1); + + // should now be [1, 2, 0] + writeln(numbas); + assert(numbas == [1, 2, 0]); + + numbas = [1, 5, 2]; + numbas = numbas.shiftIntoLeftwards(0); + + // should now be [5, 2, 0] + writeln(numbas); + assert(numbas == [5, 2, 0]); + + numbas = [1, 5, 2]; + numbas = numbas.shiftIntoLeftwards(2); + + // should now be [1, 5, 2] + writeln(numbas); + assert(numbas == [1, 5, 2]); + + numbas = []; + numbas = numbas.shiftIntoLeftwards(0); + + // should now be [] + writeln(numbas); + assert(numbas == []); +} + +public T[] removeResize(T)(T[] array, size_t position) +{ + return array.shiftInto(position, false, true); } \ No newline at end of file diff --git a/source/niknaks/containers.d b/source/niknaks/containers.d index 7d70d6e..020d505 100644 --- a/source/niknaks/containers.d +++ b/source/niknaks/containers.d @@ -12,6 +12,7 @@ import core.thread : Thread; import core.sync.condition : Condition; import std.functional : toDelegate; import std.string : format; +import niknaks.arrays : removeResize; version(unittest) { @@ -600,194 +601,6 @@ unittest destroy(map); } -// Given [0, 1, 5] -// and shift right at index 1 -// then 0 moves into 1's place -// 0's position is then filled with T.init - -/** - * Shifts a subset of the elements of - * the given array to a given position - * either from the left or right. - * - * Optionally allowing the shrinking - * of the array after the process, - * otherwise the last element shifted's - * previous value will be set to the - * value specified. - * - * Params: - * array = the input array - * position = the position to shift - * onto - * rightwards = if `true` then shift - * elements into the position rightwards, - * else leftwards (which is also the default) - * shrink = if set to `true` then - * the array will be resized to exclude - * the now "empty" element - * filler = the value to place in - * the space where the last element - * shifted no longer occupies, by default - * this is `T.init` - * Returns: the shifted array - */ -public T[] shiftInto(T)(T[] array, size_t position, bool rightwards = false, bool shrink = false, T filler = T.init) -{ - // Out of range - if(position >= array.length) - { - return array; - } - - // if rightwards - if(rightwards) - { - // nothing further left than index 0 - if(!position) - { - return array; - } - - for(size_t i = position; i > 0; i--) - { - array[i] = array[i-1]; - } - - // no shrink, then fill with filler - if(!shrink) - { - array[0] = filler; - } - // chomp left-hand side - else - { - array = array[1..$]; - } - } - // if leftwards - else - { - // nothing furtherright - if(position == array.length-1) - { - return array; - } - - for(size_t i = position; i < array.length-1; i++) - { - array[i] = array[i+1]; - } - - // no shrink, then fill with filler - if(!shrink) - { - array[$-1] = filler; - } - // chomp right-hand side - else - { - array = array[0..$-1]; - } - } - - - return array; -} - -public T[] shiftIntoRightwards(T)(T[] array, size_t position, bool shrink = false) -{ - return shiftInto(array, position, true, shrink); -} - -public T[] shiftIntoLeftwards(T)(T[] array, size_t position, bool shrink = false) -{ - return shiftInto(array, position, false, shrink); -} - -// rightwards testung (no shrink) -unittest -{ - int[] numbas = [1, 5, 2]; - numbas = numbas.shiftIntoRightwards(1); - - // should now be [0, 1, 2] - writeln(numbas); - assert(numbas == [0, 1, 2]); - - numbas = [1, 5, 2]; - numbas = numbas.shiftIntoRightwards(0); - - // should now be [1, 5, 2] - writeln(numbas); - assert(numbas == [1, 5, 2]); - - numbas = [1, 5, 2]; - numbas = numbas.shiftIntoRightwards(2); - - // should now be [0, 1, 5] - writeln(numbas); - assert(numbas == [0, 1, 5]); - - numbas = [1, 2]; - numbas = numbas.shiftIntoRightwards(1); - - // should now be [0, 1] - writeln(numbas); - assert(numbas == [0, 1]); - - numbas = [1, 2]; - numbas = numbas.shiftIntoRightwards(0); - - // should now be [1, 2] - writeln(numbas); - assert(numbas == [1, 2]); - - numbas = []; - numbas = numbas.shiftIntoRightwards(0); - - // should now be [] - writeln(numbas); - assert(numbas == []); -} - -// leftwards testung (no shrink) -unittest -{ - int[] numbas = [1, 5, 2]; - numbas = numbas.shiftIntoLeftwards(1); - - // should now be [1, 2, 0] - writeln(numbas); - assert(numbas == [1, 2, 0]); - - numbas = [1, 5, 2]; - numbas = numbas.shiftIntoLeftwards(0); - - // should now be [5, 2, 0] - writeln(numbas); - assert(numbas == [5, 2, 0]); - - numbas = [1, 5, 2]; - numbas = numbas.shiftIntoLeftwards(2); - - // should now be [1, 5, 2] - writeln(numbas); - assert(numbas == [1, 5, 2]); - - numbas = []; - numbas = numbas.shiftIntoLeftwards(0); - - // should now be [] - writeln(numbas); - assert(numbas == []); -} - -public T[] removeResize(T)(T[] array, size_t position) -{ - return array.shiftInto(position, false, true); -} - public template Always(T) { public bool Always(Tree!(T) treeNode) From ee9d0cee7ef97fea8311160241798ac33782dec9 Mon Sep 17 00:00:00 2001 From: Tristan Brice Velloza Kildaire Date: Sat, 27 Apr 2024 15:43:20 +0200 Subject: [PATCH 27/31] niknaks.arrays - Updated unittestesd (test shrinking) - Added docs --- source/niknaks/arrays.d | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/source/niknaks/arrays.d b/source/niknaks/arrays.d index 395409e..ba7e80f 100644 --- a/source/niknaks/arrays.d +++ b/source/niknaks/arrays.d @@ -249,11 +249,6 @@ unittest assert(vals_got == vals_expected); } -// Given [0, 1, 5] -// and shift right at index 1 -// then 0 moves into 1's place -// 0's position is then filled with T.init - /** * Shifts a subset of the elements of * the given array to a given position @@ -344,17 +339,29 @@ public T[] shiftInto(T)(T[] array, size_t position, bool rightwards = false, boo return array; } +/** + * Rightwards shifting into + * + * See_Also: `shiftInto` + */ public T[] shiftIntoRightwards(T)(T[] array, size_t position, bool shrink = false) { return shiftInto(array, position, true, shrink); } +/** + * Leftwards shifting into + * + * See_Also: `shiftInto` + */ public T[] shiftIntoLeftwards(T)(T[] array, size_t position, bool shrink = false) { return shiftInto(array, position, false, shrink); } -// rightwards testung (no shrink) +/** + * Tests the rightwards shifting + */ unittest { int[] numbas = [1, 5, 2]; @@ -398,9 +405,18 @@ unittest // should now be [] writeln(numbas); assert(numbas == []); + + numbas = [1, 5, 2]; + numbas = numbas.shiftIntoRightwards(1, true); + + // should now be [1, 2] + writeln(numbas); + assert(numbas == [1, 2]); } -// leftwards testung (no shrink) +/** + * Tests the leftwards shifting + */ unittest { int[] numbas = [1, 5, 2]; @@ -430,6 +446,13 @@ unittest // should now be [] writeln(numbas); assert(numbas == []); + + numbas = [1, 5, 2]; + numbas = numbas.shiftIntoLeftwards(1, true); + + // should now be [1, 2] + writeln(numbas); + assert(numbas == [1, 2]); } public T[] removeResize(T)(T[] array, size_t position) From 45a6ca0f6717b6f5a669a5cc6123e263f12dbae0 Mon Sep 17 00:00:00 2001 From: Tristan Brice Velloza Kildaire Date: Sat, 27 Apr 2024 15:43:58 +0200 Subject: [PATCH 28/31] niknaks.arrays - Refactored --- source/niknaks/arrays.d | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/source/niknaks/arrays.d b/source/niknaks/arrays.d index ba7e80f..23e15d7 100644 --- a/source/niknaks/arrays.d +++ b/source/niknaks/arrays.d @@ -349,16 +349,6 @@ public T[] shiftIntoRightwards(T)(T[] array, size_t position, bool shrink = fals return shiftInto(array, position, true, shrink); } -/** - * Leftwards shifting into - * - * See_Also: `shiftInto` - */ -public T[] shiftIntoLeftwards(T)(T[] array, size_t position, bool shrink = false) -{ - return shiftInto(array, position, false, shrink); -} - /** * Tests the rightwards shifting */ @@ -414,6 +404,16 @@ unittest assert(numbas == [1, 2]); } +/** + * Leftwards shifting into + * + * See_Also: `shiftInto` + */ +public T[] shiftIntoLeftwards(T)(T[] array, size_t position, bool shrink = false) +{ + return shiftInto(array, position, false, shrink); +} + /** * Tests the leftwards shifting */ From 930fa1ace524cd3a723e4be506d3d796194d3e0b Mon Sep 17 00:00:00 2001 From: Tristan Brice Velloza Kildaire Date: Sat, 27 Apr 2024 15:46:28 +0200 Subject: [PATCH 29/31] niknaks.arrays - Added this --- source/niknaks/arrays.d | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/source/niknaks/arrays.d b/source/niknaks/arrays.d index 23e15d7..039b3d4 100644 --- a/source/niknaks/arrays.d +++ b/source/niknaks/arrays.d @@ -455,6 +455,17 @@ unittest assert(numbas == [1, 2]); } +/** + * Removes the element at the + * provided position in the + * given array + * + * Params: + * array = the array + * position = position of + * element to remove + * Returns: the array + */ public T[] removeResize(T)(T[] array, size_t position) { return array.shiftInto(position, false, true); From 926598086dcfb41c8054c511fc8d51e055fc1e7c Mon Sep 17 00:00:00 2001 From: Tristan Brice Velloza Kildaire Date: Sat, 27 Apr 2024 16:25:47 +0200 Subject: [PATCH 30/31] VisitationTree - Documented --- source/niknaks/containers.d | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/source/niknaks/containers.d b/source/niknaks/containers.d index 020d505..5f11708 100644 --- a/source/niknaks/containers.d +++ b/source/niknaks/containers.d @@ -872,6 +872,27 @@ unittest assert(!treeOfStrings.removeNode(subtree_1)); } +/** + * A kind-of a tree which has the ability + * to linearize all of its nodes which + * results in performing a depth first + * search resulting in the collection of + * all nodes into a single array with + * elements on the left hand side being + * the most leafiest (and left-to-right + * on the same depth are in said order). + * + * It also marks a node as visited on + * entry to it via the dfs call to it. + * + * When dfs is performed, a child node + * is only recursed upon if it has not + * yet been visited. + * + * With all this, it means a graph of + * relations can be flattened into an + * array. + */ public class VisitationTree(T) : Tree!(T) { private bool visisted; From a5f7e9fc952612850ed7e2edbc9c262f465022d8 Mon Sep 17 00:00:00 2001 From: Tristan Brice Velloza Kildaire Date: Sat, 27 Apr 2024 16:42:07 +0200 Subject: [PATCH 31/31] VisitationTree - Documented --- source/niknaks/containers.d | 44 ++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/source/niknaks/containers.d b/source/niknaks/containers.d index 5f11708..3519fc0 100644 --- a/source/niknaks/containers.d +++ b/source/niknaks/containers.d @@ -897,22 +897,51 @@ public class VisitationTree(T) : Tree!(T) { private bool visisted; + /** + * Constructs a new node + * + * Params: + * value = the value + */ this(T value) { super(value); } + /** + * Performs the linearization + * + * Returns: the linearized list + */ public T[] linearize() { return dfs(toDelegate(&_shouldVisit), toDelegate(&_touch)); } + /** + * The inclusion startergy + * + * Params: + * tnode = the tree node + * Returns: `true` if not + * yet visited or incompatible + * node type + */ private static bool _shouldVisit(Tree!(T) tnode) { VisitationTree!(T) vnode = cast(VisitationTree!(T))tnode; return vnode && !vnode.isVisited(); } + /** + * The touching stratergy + * + * Only works on compatible + * tree nodes + * + * Params: + * tnode = the tree node + */ private static void _touch(Tree!(T) tnode) { VisitationTree!(T) vnode = cast(VisitationTree!(T))tnode; @@ -922,17 +951,31 @@ public class VisitationTree(T) : Tree!(T) } } + /** + * Marks this node as + * visited + */ private void mark() { this.visisted = true; } + /** + * Checks this node has been + * visited + * + * Returns: `true` if visited, + * otherwise `false` + */ private bool isVisited() { return this.visisted; } } +/** + * Tests out using the visitation tree + */ unittest { VisitationTree!(string) root = new VisitationTree!(string)("root"); @@ -946,5 +989,4 @@ unittest assert(linearized[0] == "subtree"); assert(linearized[1] == "root"); - } \ No newline at end of file