mirror of https://github.com/deavmi/niknaks
parent
41ebad0768
commit
11fd2f856e
|
@ -596,7 +596,7 @@ unittest
|
||||||
// then 0 moves into 1's place
|
// then 0 moves into 1's place
|
||||||
// 0's position is then filled with T.init
|
// 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
|
// Out of range
|
||||||
if(position >= array.length)
|
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..$];
|
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;
|
return array;
|
||||||
}
|
}
|
||||||
|
@ -637,57 +663,83 @@ public T[] shift(T)(T[] array, size_t position, bool rightwards = false, bool sh
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
int[] numbas = [1, 5, 2];
|
int[] numbas = [1, 5, 2];
|
||||||
numbas = numbas.shift(1, true);
|
numbas = numbas.shiftInto(1, true);
|
||||||
|
|
||||||
// should now be [0, 1, 2]
|
// should now be [0, 1, 2]
|
||||||
writeln(numbas);
|
writeln(numbas);
|
||||||
assert(numbas == [0, 1, 2]);
|
assert(numbas == [0, 1, 2]);
|
||||||
|
|
||||||
numbas = [1, 5, 2];
|
numbas = [1, 5, 2];
|
||||||
numbas = numbas.shift(0, true);
|
numbas = numbas.shiftInto(0, true);
|
||||||
|
|
||||||
// should now be [1, 5, 2]
|
// should now be [1, 5, 2]
|
||||||
writeln(numbas);
|
writeln(numbas);
|
||||||
assert(numbas == [1, 5, 2]);
|
assert(numbas == [1, 5, 2]);
|
||||||
|
|
||||||
numbas = [1, 5, 2];
|
numbas = [1, 5, 2];
|
||||||
numbas = numbas.shift(2, true);
|
numbas = numbas.shiftInto(2, true);
|
||||||
|
|
||||||
// should now be [0, 1, 5]
|
// should now be [0, 1, 5]
|
||||||
writeln(numbas);
|
writeln(numbas);
|
||||||
assert(numbas == [0, 1, 5]);
|
assert(numbas == [0, 1, 5]);
|
||||||
|
|
||||||
numbas = [1, 2];
|
numbas = [1, 2];
|
||||||
numbas = numbas.shift(1, true);
|
numbas = numbas.shiftInto(1, true);
|
||||||
|
|
||||||
// should now be [0, 1]
|
// should now be [0, 1]
|
||||||
writeln(numbas);
|
writeln(numbas);
|
||||||
assert(numbas == [0, 1]);
|
assert(numbas == [0, 1]);
|
||||||
|
|
||||||
numbas = [1, 2];
|
numbas = [1, 2];
|
||||||
numbas = numbas.shift(0, true);
|
numbas = numbas.shiftInto(0, true);
|
||||||
|
|
||||||
// should now be [1, 2]
|
// should now be [1, 2]
|
||||||
writeln(numbas);
|
writeln(numbas);
|
||||||
assert(numbas == [1, 2]);
|
assert(numbas == [1, 2]);
|
||||||
|
|
||||||
numbas = [];
|
numbas = [];
|
||||||
numbas = numbas.shift(0, true);
|
numbas = numbas.shiftInto(0, false);
|
||||||
|
|
||||||
// should now be []
|
// should now be []
|
||||||
writeln(numbas);
|
writeln(numbas);
|
||||||
assert(numbas == []);
|
assert(numbas == []);
|
||||||
}
|
}
|
||||||
|
|
||||||
public T[] remove(T)(T[] array, size_t idx)
|
// leftwards testung (no shrink)
|
||||||
|
unittest
|
||||||
{
|
{
|
||||||
// Return your array on this
|
int[] numbas = [1, 5, 2];
|
||||||
if(!(idx < array.length))
|
numbas = numbas.shiftInto(1, false);
|
||||||
{
|
|
||||||
return array;
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
// TODO: make delegate kak
|
||||||
|
@ -767,16 +819,24 @@ public class Tree(T)
|
||||||
public bool removeNode(Tree!(T) node)
|
public bool removeNode(Tree!(T) node)
|
||||||
{
|
{
|
||||||
bool found = false;
|
bool found = false;
|
||||||
|
size_t idx;
|
||||||
for(size_t i = 0; i < this.children.length; i++)
|
for(size_t i = 0; i < this.children.length; i++)
|
||||||
{
|
{
|
||||||
found = this.children[i] == node;
|
found = this.children[i] == node;
|
||||||
if(found)
|
if(found)
|
||||||
{
|
{
|
||||||
|
idx = i;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
// if(found)
|
||||||
|
// {
|
||||||
|
// this.children = this.children.removeResize(idx, true, true);
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public T[] dfs
|
public T[] dfs
|
||||||
|
|
Loading…
Reference in New Issue