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] 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()