diff --git a/queues.scad b/queues.scad deleted file mode 100644 index 0a49614..0000000 --- a/queues.scad +++ /dev/null @@ -1,198 +0,0 @@ -////////////////////////////////////////////////////////////////////// -// LibFile: queues.scad -// Queue data structure implementation. -// Includes: -// include -// include -////////////////////////////////////////////////////////////////////// - - -// Section: Queue Data Structure -// A queue is a first-in-first-out collection of items. You can add items onto the tail of the -// queue, or pop items off the head. While you can treat a queue as an opaque data type, using the -// functions below, it's simply implemented as a list. This means that you can use any list -// function to manipulate the queue. The first item in the list is the head queue item. - - -// Function: queue_init() -// Usage: -// queue = queue_init(); -// Description: -// Creates an empty queue/list. -// Example: -// queue = queue_init(); // Return: [] -function queue_init() = []; - - -// Function: queue_empty() -// Usage: -// if (queue_empty(queue)) ... -// Description: -// Returns true if the given queue is empty. -// Arguments: -// queue = The queue to test if empty. -// Example: -// queue = queue_init(); -// is_empty = queue_empty(queue); // Returns: true -// queue2 = queue_add(queue, "foo"); -// is_empty2 = queue_empty(queue2); // Returns: false -function queue_empty(queue) = - assert(is_list(queue)) - len(queue)==0; - - -// Function: queue_size() -// Usage: -// depth = queue_size(queue); -// Description: -// Returns the number of items in the given queue. -// Arguments: -// queue = The queue to get the size of. -// Example: -// queue = queue_init(); -// depth = queue_size(queue); // Returns: 0 -// queue2 = queue_add(queue, "foo"); -// depth2 = queue_size(queue2); // Returns: 1 -// queue3 = queue_add(queue2, ["bar","baz","qux"]); -// depth3 = queue_size(queue3); // Returns: 4 -function queue_size(queue) = - assert(is_list(queue)) - len(queue); - - -// Function: queue_head() -// Usage: -// item = queue_head(queue); -// list = queue_head(queue,n); -// Description: -// If `n` is not given, returns the first item from the head of the queue. -// If `n` is given, returns a list of the first `n` items from the head of the queue. -// Arguments: -// queue = The queue/list to get item(s) from the head of. -// Example: -// queue = [4,5,6,7,8,9]; -// item = queue_head(queue); // Returns: 4 -// list = queue_head(queue,n=3); // Returns: [4,5,6] -function queue_head(queue,n=undef) = - assert(is_list(queue)) - is_undef(n)? ( - queue[0] - ) : ( - let(queuesize = len(queue)) - assert(is_num(n)) - assert(n>=0) - assert(queuesize>=n, "queue underflow") - [for (i=[0:1:n-1]) queue[i]] - ); - - -// Function: queue_tail() -// Usage: -// item = queue_tail(queue); -// list = queue_tail(queue,n); -// Description: -// If `n` is not given, returns the last item from the tail of the queue. -// If `n` is given, returns a list of the last `n` items from the tail of the queue. -// Arguments: -// queue = The queue/list to get item(s) from the tail of. -// Example: -// queue = [4,5,6,7,8,9]; -// item = queue_tail(queue); // Returns: 9 -// list = queue_tail(queue,n=3); // Returns: [7,8,9] -function queue_tail(queue,n=undef) = - assert(is_list(queue)) - let(queuesize = len(queue)) - is_undef(n)? ( - queue[queuesize-1] - ) : ( - assert(is_num(n)) - assert(n>=0) - assert(queuesize>=n, "queue underflow") - [for (i=[0:1:n-1]) queue[queuesize-n+i]] - ); - - -// Function: queue_peek() -// Usage: -// item = queue_peek(queue,[pos]); -// list = queue_peek(queue,pos,n); -// Description: -// If `n` is not given, returns the queue item at position `pos`. -// If `n` is given, returns a list of the `n` queue items at and after position `pos`. -// Arguments: -// queue = The queue to read from. -// pos = The position of the queue item to read. Default: 0 -// n = The number of queue items to return. Default: undef (Return only the queue item at `pos`) -// Example: -// queue = [2,3,4,5,6,7,8,9]; -// item = queue_peek(queue); // Returns: 2 -// item2 = queue_peek(queue, 3); // Returns: 5 -// list = queue_peek(queue, 4, 3); // Returns: [6,7,8] -function queue_peek(queue,pos=0,n=undef) = - assert(is_list(queue)) - assert(is_num(pos)) - assert(pos>=0) - let(queuesize = len(queue)) - assert(queuesize>=pos, "queue underflow") - is_undef(n)? ( - queue[pos] - ) : ( - assert(is_num(n)) - assert(n>=0) - assert(n=0) - let(queuesize = len(queue)) - assert(queuesize>=n, "queue underflow") - [for (i = [n:1:queuesize-1]) queue[i]]; - - - -// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap diff --git a/stacks.scad b/stacks.scad deleted file mode 100644 index 9e53df8..0000000 --- a/stacks.scad +++ /dev/null @@ -1,191 +0,0 @@ -////////////////////////////////////////////////////////////////////// -// LibFile: stacks.scad -// Stack data structure implementation. -// Includes: -// include -// include -////////////////////////////////////////////////////////////////////// - - -// Section: Stack Data Structure -// A stack is a last-in-first-out collection of items. You can push items onto the top of the -// stack, or pop the top item off. While you can treat a stack as an opaque data type, using the -// functions below, it's simply implemented as a list. This means that you can use any list -// function to manipulate the stack. The last item in the list is the topmost stack item. -// The depth of an item is how far buried in the stack that item is. An item at depth 1 is the -// top-most stack item. An item at depth 3 is two items below the top-most stack item. - - -// Function: stack_init() -// Usage: -// stack = stack_init(); -// Description: -// Creates an empty stack/list. -// Example: -// stack = stack_init(); // Return: [] -function stack_init() = []; - - -// Function: stack_empty() -// Usage: -// if (stack_empty(stack)) ... -// Description: -// Returns true if the given stack is empty. -// Arguments: -// stack = The stack to test if empty. -// Example: -// stack = stack_init(); -// is_empty = stack_empty(stack); // Returns: true -// stack2 = stack_push(stack, "foo"); -// is_empty2 = stack_empty(stack2); // Returns: false -function stack_empty(stack) = - assert(is_list(stack)) - len(stack)==0; - - -// Function: stack_depth() -// Usage: -// depth = stack_depth(stack); -// Description: -// Returns the depth of the given stack. -// Arguments: -// stack = The stack to get the depth of. -// Example: -// stack = stack_init(); -// depth = stack_depth(stack); // Returns: 0 -// stack2 = stack_push(stack, "foo"); -// depth2 = stack_depth(stack2); // Returns: 1 -// stack3 = stack_push(stack2, ["bar","baz","qux"]); -// depth3 = stack_depth(stack3); // Returns: 4 -function stack_depth(stack) = - assert(is_list(stack)) - len(stack); - - -// Function: stack_top() -// Usage: -// item = stack_top(stack); -// list = stack_top(stack,n); -// Description: -// If n is not given, returns the topmost item of the given stack. -// If n is given, returns a list of the `n` topmost items. -// Arguments: -// stack = The stack/list to get the top item(s) of. -// Example: -// stack = [4,5,6,7]; -// item = stack_top(stack); // Returns: 7 -// list = stack_top(stack,n=3); // Returns: [5,6,7] -function stack_top(stack,n=undef) = - assert(is_list(stack)) - is_undef(n)? ( - stack[len(stack)-1] - ) : ( - let(stacksize = len(stack)) - assert(is_num(n)) - assert(n>=0) - assert(stacksize>=n, "stack underflow") - [for (i=[0:1:n-1]) stack[stacksize-n+i]] - ); - - -// Function: stack_peek() -// Usage: -// item = stack_peek(stack,[depth]); -// list = stack_peek(stack,depth,n); -// Description: -// If `n` is not given, returns the stack item at depth `depth`. -// If `n` is given, returns a list of the `n` stack items at and above depth `depth`. -// Arguments: -// stack = The stack to read from. -// depth = The depth of the stack item to read. Default: 0 -// n = The number of stack items to return. Default: undef (Return only the stack item at `depth`) -// Example: -// stack = [2,3,4,5,6,7,8,9]; -// item = stack_peek(stack); // Returns: 9 -// item2 = stack_peek(stack, 3); // Returns: 7 -// list = stack_peek(stack, 6, 4); // Returns: [4,5,6,7] -function stack_peek(stack,depth=0,n=undef) = - assert(is_list(stack)) - assert(is_num(depth)) - assert(depth>=0) - let(stacksize = len(stack)) - assert(stacksize>=depth, "stack underflow") - is_undef(n)? ( - stack[stacksize-depth-1] - ) : ( - assert(is_num(n)) - assert(n>=0) - assert(n<=depth+1) - [for (i=[0:1:n-1]) stack[stacksize-1-depth+i]] - ); - - -// Function: stack_push() -// Usage: -// modified_stack = stack_push(stack,items); -// Description: -// Pushes the given `items` onto the stack `stack`. Returns the modified stack. -// Arguments: -// stack = The stack to modify. -// items = A value or list of values to push onto the stack. -// Example: -// stack = [4,9,2,3]; -// stack2 = stack_push(stack,7); // Returns: [4,9,2,3,7] -// stack3 = stack_push(stack2,[6,1]); // Returns: [4,9,2,3,7,6,1] -// stack4 = stack_push(stack,[[5,8]]); // Returns: [4,9,2,3,[5,8]] -// stack5 = stack_push(stack,[[5,8],6,7]); // Returns: [4,9,2,3,[5,8],6,7] -function stack_push(stack,items) = - assert(is_list(stack)) - is_list(items)? concat(stack, items) : concat(stack, [items]); - - -// Function: stack_pop() -// Usage: -// modified_stack = stack_pop(stack, [n]); -// Description: -// Removes the `n` topmost items from the stack. Returns the modified stack. -// Arguments: -// stack = The stack to modify. -// n = The number of items to remove off the top of the stack. Default: 1 -// Example: -// stack = [4,5,6,7,8,9]; -// stack2 = stack_pop(stack); // Returns: [4,5,6,7,8] -// stack3 = stack_pop(stack2,n=3); // Returns: [4,5] -function stack_pop(stack,n=1) = - assert(is_list(stack)) - assert(is_num(n)) - assert(n>=0) - assert(len(stack)>=n, "stack underflow") - [for (i = [0:1:len(stack)-1-n]) stack[i]]; - - -// Function: stack_rotate() -// Usage: -// modified_stack = stack_rotate(stack, [n]); -// Description: -// Rotates the top `abs(n)` stack items, and returns the modified stack. -// If `n` is positive, then the depth `n` stack item is rotated (left) to the top. -// If `n` is negative, then the top stack item is rotated (right) to depth `abs(n)`. -// Arguments: -// stack = The stack to modify. -// n = The number of stack items to rotate. If negative, reverse rotation direction. Default: 3 -// Example: -// stack = [4,5,6,7,8]; -// stack2 = stack_rotate(stack,3); // Returns: [4,5,7,8,6] -// stack3 = stack_rotate(stack2,-4); // Returns: [4,6,5,7,8] -function stack_rotate(stack,n=3) = - assert(is_list(stack)) - let(stacksize = len(stack)) - assert(stacksize>=n, "stack underflow") - n>=0? concat( - [for (i=[0:1:stacksize-1-n]) stack[i]], - [for (i=[0:1:n-2]) stack[stacksize-n+i+1]], - [stack[stacksize-n]] - ) : concat( - [for (i=[0:1:stacksize-1+n]) stack[i]], - [stack[stacksize-1]], - [for (i=[0:1:-n-2]) stack[stacksize+n+i]] - ); - - -// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap diff --git a/tests/test_queues.scad b/tests/test_queues.scad deleted file mode 100644 index 4ce3c8c..0000000 --- a/tests/test_queues.scad +++ /dev/null @@ -1,77 +0,0 @@ -include <../std.scad> -include <../queues.scad> - - -module test_queue_init() { - assert(queue_init()==[]); -} -test_queue_init(); - - -module test_queue_empty() { - assert(queue_empty([])); - assert(!queue_empty([3])); - assert(!queue_empty([2,4,8])); -} -test_queue_empty(); - - -module test_queue_size() { - assert(queue_size([]) == 0); - assert(queue_size([3]) == 1); - assert(queue_size([2,4,8]) == 3); -} -test_queue_size(); - - -module test_queue_head() { - assert(queue_head([]) == undef); - assert(queue_head([3,5,7,9]) == 3); - assert(queue_head([3,5,7,9], 3) == [3,5,7]); -} -test_queue_head(); - - -module test_queue_tail() { - assert(queue_tail([]) == undef); - assert(queue_tail([3,5,7,9]) == 9); - assert(queue_tail([3,5,7,9], 3) == [5,7,9]); -} -test_queue_tail(); - - -module test_queue_peek() { - q = [8,5,4,3,2,3,7]; - assert(queue_peek(q,0) == 8); - assert(queue_peek(q,2) == 4); - assert(queue_peek(q,2,1) == [4]); - assert(queue_peek(q,2,3) == [4,3,2]); -} -test_queue_peek(); - - -module test_queue_add() { - q1 = queue_init(); - q2 = queue_add(q1, "Foo"); - assert(q2==["Foo"]); - q3 = queue_add(q2, "Bar"); - assert(q3==["Foo","Bar"]); - q4 = queue_add(q3, "Baz"); - assert(q4==["Foo","Bar","Baz"]); -} -test_queue_add(); - - -module test_queue_pop() { - q = ["Foo", "Bar", "Baz", "Qux"]; - q1 = queue_pop(q); - assert(q1 == ["Bar", "Baz", "Qux"]); - q2 = queue_pop(q,2); - assert(q2 == ["Baz", "Qux"]); - q3 = queue_pop(q,3); - assert(q3 == ["Qux"]); -} -test_queue_pop(); - - -// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap diff --git a/tests/test_stacks.scad b/tests/test_stacks.scad deleted file mode 100644 index 7241203..0000000 --- a/tests/test_stacks.scad +++ /dev/null @@ -1,79 +0,0 @@ -include <../std.scad> -include <../stacks.scad> - - -module test_stack_init() { - assert(stack_init()==[]); -} -test_stack_init(); - - -module test_stack_empty() { - assert(stack_empty([])); - assert(!stack_empty([3])); - assert(!stack_empty([2,4,8])); -} -test_stack_empty(); - - -module test_stack_depth() { - assert(stack_depth([]) == 0); - assert(stack_depth([3]) == 1); - assert(stack_depth([2,4,8]) == 3); -} -test_stack_depth(); - - -module test_stack_top() { - assert(stack_top([]) == undef); - assert(stack_top([3,5,7,9]) == 9); - assert(stack_top([3,5,7,9], 3) == [5,7,9]); -} -test_stack_top(); - - -module test_stack_peek() { - s = [8,5,4,3,2,3,7]; - assert(stack_peek(s,0) == 7); - assert(stack_peek(s,2) == 2); - assert(stack_peek(s,2,1) == [2]); - assert(stack_peek(s,2,3) == [2,3,7]); -} -test_stack_peek(); - - -module test_stack_push() { - s1 = stack_init(); - s2 = stack_push(s1, "Foo"); - assert(s2==["Foo"]); - s3 = stack_push(s2, "Bar"); - assert(s3==["Foo","Bar"]); - s4 = stack_push(s3, "Baz"); - assert(s4==["Foo","Bar","Baz"]); -} -test_stack_push(); - - -module test_stack_pop() { - s = ["Foo", "Bar", "Baz", "Qux"]; - s1 = stack_pop(s); - assert(s1 == ["Foo", "Bar", "Baz"]); - s2 = stack_pop(s,2); - assert(s2 == ["Foo", "Bar"]); - s3 = stack_pop(s,3); - assert(s3 == ["Foo"]); -} -test_stack_pop(); - - -module test_stack_rotate() { - s = ["Foo", "Bar", "Baz", "Qux", "Quux"]; - s1 = stack_rotate(s,4); - assert(s1 == ["Foo", "Baz", "Qux", "Quux", "Bar"]); - s2 = stack_rotate(s,-4); - assert(s2 == ["Foo", "Quux", "Bar", "Baz", "Qux"]); -} -test_stack_rotate(); - - -// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap