Compare commits

...

24 Commits

Author SHA1 Message Date
Tristan B. Velloza Kildaire 474d43c271
Merge 5b205019f6 into 6e11752590 2024-04-27 13:21:12 +00:00
Tristan B. Velloza Kildaire 5b205019f6 Containers (unittests)
- Use new methods
2024-04-27 15:20:59 +02:00
Tristan B. Velloza Kildaire 38169bbebd Containers
- Added `shiftIntoLeftwards` and `shiftIntoRightwards`
2024-04-27 15:19:25 +02:00
Tristan B. Velloza Kildaire eeb1fd4a46 Merge branch 'master' into feature/tree 2024-04-27 15:17:13 +02:00
Tristan B. Velloza Kildaire d3eec6eba6 Tree
- Cleaned up
- Removed `getValue()`

Tree (unittests)

- Added test for `removeNode(Tree!(T))`
2024-04-26 18:30:28 +02:00
Tristan B. Velloza Kildaire 04c5373a69 Tree
- Added opIndex
2024-04-26 18:28:06 +02:00
Tristan B. Velloza Kildaire 450d811f52 Tree (unittests)
- Updated test for parametwerized opSlice
2024-04-26 18:25:17 +02:00
Tristan B. Velloza Kildaire 54b66ec3f1 Tree
- Added normal opSlice as well
2024-04-26 18:24:31 +02:00
Tristan B. Velloza Kildaire d7a830ffa9 Tree
- opSlice done
2024-04-26 18:22:31 +02:00
Tristan B. Velloza Kildaire 8dca2ebf73 Tree
- Reworking opSlicwe
2024-04-26 16:50:01 +02:00
Tristan B. Velloza Kildaire 4a431c43d1 TreeNode
- Added removal
- Added indexing support
2024-04-26 16:35:21 +02:00
Tristan B. Velloza Kildaire 11fd2f856e Methods
- Added leftwards shifting mechanism
2024-04-26 16:28:19 +02:00
Tristan B. Velloza Kildaire 41ebad0768 Methods
- Added rightward shifting mechanism
2024-04-26 16:05:10 +02:00
Tristan B. Velloza Kildaire 838ddbb9db Tree (unittests)
- Added missing assertions
2024-04-26 15:36:54 +02:00
Tristan B. Velloza Kildaire e44caf3643 VisitationTree (unittests)
- Added missing assertions
2024-04-26 15:35:29 +02:00
Tristan B. Velloza Kildaire ce78102af9 Tree
- Correct visitation stratergy
2024-04-23 16:02:40 +02:00
Tristan B. Velloza Kildaire 04edc241ad Tree
- Pass in, explcitly, the touch startergy
2024-04-23 15:52:02 +02:00
Tristan B. Velloza Kildaire 167c2b7c0d VisitationTree
- Working on a visitation tree implementation
2024-04-23 15:49:49 +02:00
Tristan B. Velloza Kildaire f49ecf1908 InclusionStratergy
- Now uses the `TreeNode!(T)` instead of the `T` itself
2024-04-22 22:14:28 +02:00
Tristan B. Velloza Kildaire d5b00c548e Tree
- Added ability to append

Tree (unittests)

- Updated unittests to test appending
2024-04-22 20:18:50 +02:00
Tristan B. Velloza Kildaire 4ecb57d4fa Tree
- Added a TODO
2024-04-22 20:14:26 +02:00
Tristan B. Velloza Kildaire 84f18407e8 Tree
- By default use the `always` strat
2024-04-22 17:03:30 +02:00
Tristan B. Velloza Kildaire 60028a34be Tree
- Added initial dfs
2024-04-22 17:02:24 +02:00
Tristan B. Velloza Kildaire 34d2b3031f Tree
- WIP
2024-04-13 14:56:54 +02:00
1 changed files with 485 additions and 0 deletions

View File

@ -597,4 +597,489 @@ unittest
// Destroy the map (such that it ends the sweeper
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[] shiftInto(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..$];
}
}
// 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;
}
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);
}
// 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)
{
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(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)
{
private T value;
private Tree!(T)[] children;
this(T value)
{
this.value = 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;
size_t idx;
for(size_t i = 0; i < this.children.length; i++)
{
found = this.children[i] == node;
if(found)
{
idx = i;
break;
}
}
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;
// }
private static bool isTreeNodeType(E)()
{
return __traits(isSame, E, Tree!(T));
}
private static 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;
}
// If the children as values themselves is requested
else static if(isTreeValueType!(E))
{
T[] slice;
foreach(Tree!(T) tnode; this.children)
{
slice ~= tnode.value;
}
return slice;
// import std.algorithm.iteration : map;
// return map!(getValue)(this.children)[];
}
}
public T[] opSlice()
{
return opSlice!(T)();
}
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].value;
}
}
public T opIndex(size_t idx)
{
return opIndex!(T)(idx);
}
public T[] dfs
(
InclusionStratergy!(T) strat = toDelegate(&Always!(T)),
TouchStratergy!(T) touch = toDelegate(&Nothing!(T))
)
{
version(unittest)
{
writeln("dfs entry: ", this);
}
T[] collected;
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))
{
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);
}
}
}
// "Visit"
collected ~= this.value;
return collected;
}
public override string toString()
{
return format("TreeNode [val: %s]", this.value);
}
}
version(unittest)
{
import std.functional : toDelegate;
import std.stdio : writeln;
private void DebugTouch(T)(Tree!(T) node)
{
writeln("Touching tree node ", node);
}
}
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);
InclusionStratergy!(string) strat = toDelegate(&Always!(string));
TouchStratergy!(string) touch = toDelegate(&DebugTouch!(string));
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");
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);
assert(p == treeOfStrings[]);
assert(treeOfStrings.removeNode(subtree_1));
assert(!treeOfStrings.removeNode(subtree_1));
}
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);
string[] linearized = root.linearize();
writeln(linearized);
assert(linearized[0] == "subtree");
assert(linearized[1] == "root");
}