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