From d7deda50789947b17b315b54bb15167f8694bca6 Mon Sep 17 00:00:00 2001 From: Hunam Date: Thu, 23 Dec 2021 14:16:29 +0100 Subject: [PATCH] vlib: add an `adt` module (Abstract Data Types) (#12901) --- vlib/adt/README.md | 9 ++++++++ vlib/adt/stack.v | 36 ++++++++++++++++++++++++++++++ vlib/adt/stack_test.v | 52 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 vlib/adt/README.md create mode 100644 vlib/adt/stack.v create mode 100644 vlib/adt/stack_test.v diff --git a/vlib/adt/README.md b/vlib/adt/README.md new file mode 100644 index 0000000000..da6acd151d --- /dev/null +++ b/vlib/adt/README.md @@ -0,0 +1,9 @@ +# adt + +A library providing common ADTs (Abstract Data Type) implementations which have proved useful in a +great variety of applications. + +## implementations + +- [x] Stack (LIFO) +- [ ] ... diff --git a/vlib/adt/stack.v b/vlib/adt/stack.v new file mode 100644 index 0000000000..7e42842eff --- /dev/null +++ b/vlib/adt/stack.v @@ -0,0 +1,36 @@ +module adt + +pub struct Stack { +mut: + elements []T +} + +// is_empty checks if the stack is empty +pub fn (stack Stack) is_empty() bool { + return stack.elements.len == 0 +} + +// len returns the length of the stack +pub fn (stack Stack) len() int { + return stack.elements.len +} + +// peek returns the top of the stack +pub fn (stack Stack) peek() ?T { + return if !stack.is_empty() { stack.elements.last() } else { error('Stack is empty') } +} + +// push adds an element to the top of the stack +pub fn (mut stack Stack) push(item T) { + stack.elements << item +} + +// pop removes the element at the top of the stack and returns it +pub fn (mut stack Stack) pop() ?T { + return if !stack.is_empty() { stack.elements.pop() } else { error('Stack is empty') } +} + +// str returns a string representation of the stack +pub fn (stack Stack) str() string { + return stack.elements.str() +} diff --git a/vlib/adt/stack_test.v b/vlib/adt/stack_test.v new file mode 100644 index 0000000000..84c1a2a761 --- /dev/null +++ b/vlib/adt/stack_test.v @@ -0,0 +1,52 @@ +import adt + +fn test_is_empty() { + mut stack := adt.Stack{} + assert stack.is_empty() == true + stack.push(1) + assert stack.is_empty() == false +} + +fn test_len() ? { + mut stack := adt.Stack{} + assert stack.len() == 0 + stack.push(1) + assert stack.len() == 1 + stack.pop() ? + assert stack.len() == 0 +} + +fn test_peek() ? { + mut stack := adt.Stack{} + stack.push(1) + assert stack.peek() ? == 1 + stack.push(2) + assert stack.peek() ? == 2 + stack = adt.Stack{} + stack.peek() or { return } + assert false +} + +fn test_push() ? { + mut stack := adt.Stack{} + stack.push(1) + assert stack.peek() ? == 1 + stack.push(2) + assert stack.peek() ? == 2 + stack.push(3) + assert stack.peek() ? == 3 +} + +fn test_pop() ? { + mut stack := adt.Stack{} + stack.push(1) + stack.push(2) + stack.push(3) + assert stack.pop() ? == 3 + stack.push(4) + assert stack.pop() ? == 4 + assert stack.pop() ? == 2 + stack = adt.Stack{} + stack.pop() or { return } + assert false +}