Skip to content

datatypes #

datatypes

This module provides implementations of less frequently used, but still common data types.

V's builtin module is imported implicitly, and has implementations for arrays, maps and strings. These are good for many applications, but there are a plethora of other useful data structures/containers, like linked lists, priority queues, tries, etc, that allow for algorithms with different time complexities, which may be more suitable for your specific application.

It is implemented using generics, that you have to specialise for the type of your actual elements. For example:

import datatypes

mut stack := datatypes.Stack<int>{}
stack.push(1)
println(stack)

Currently Implemented Datatypes:

  • Linked list
  • Doubly linked list
  • Stack (LIFO)
  • Queue (FIFO)
  • Min heap (priority queue)
  • ...

fn (DoublyLinkedList) is_empty #

fn (list DoublyLinkedList<T>) is_empty() bool

is_empty checks if the linked list is empty

fn (DoublyLinkedList) len #

fn (list DoublyLinkedList<T>) len() int

len returns the length of the linked list

fn (DoublyLinkedList) first #

fn (list DoublyLinkedList<T>) first() ?T

first returns the first element of the linked list

fn (DoublyLinkedList) last #

fn (list DoublyLinkedList<T>) last() ?T

last returns the last element of the linked list

fn (DoublyLinkedList) push_back #

fn (mut list DoublyLinkedList<T>) push_back(item T)

push_back adds an element to the end of the linked list

fn (DoublyLinkedList) push_front #

fn (mut list DoublyLinkedList<T>) push_front(item T)

push_front adds an element to the beginning of the linked list

fn (DoublyLinkedList) pop_back #

fn (mut list DoublyLinkedList<T>) pop_back() ?T

pop_back removes the last element of the linked list

fn (DoublyLinkedList) pop_front #

fn (mut list DoublyLinkedList<T>) pop_front() ?T

pop_front removes the last element of the linked list

fn (DoublyLinkedList) insert #

fn (mut list DoublyLinkedList<T>) insert(idx int, item T) ?

insert adds an element to the linked list at the given index

fn (DoublyLinkedList) index #

fn (list &DoublyLinkedList<T>) index(item T) ?int

index searches the linked list for item and returns the forward index or none if not found.

fn (DoublyLinkedList) delete #

fn (mut list DoublyLinkedList<T>) delete(idx int)

delete removes index idx from the linked list and is safe to call for any idx.

fn (DoublyLinkedList) str #

fn (list DoublyLinkedList<T>) str() string

str returns a string representation of the linked list

fn (DoublyLinkedList) next #

fn (mut list DoublyLinkedList<T>) next() ?T

next implements the iter interface to use DoublyLinkedList with V's for loop syntax.

fn (LinkedList) is_empty #

fn (list LinkedList<T>) is_empty() bool

is_empty checks if the linked list is empty

fn (LinkedList) len #

fn (list LinkedList<T>) len() int

len returns the length of the linked list

fn (LinkedList) first #

fn (list LinkedList<T>) first() ?T

first returns the first element of the linked list

fn (LinkedList) last #

fn (list LinkedList<T>) last() ?T

last returns the last element of the linked list

fn (LinkedList) index #

fn (list LinkedList<T>) index(idx int) ?T

index returns the element at the given index of the linked list

fn (LinkedList) push #

fn (mut list LinkedList<T>) push(item T)

push adds an element to the end of the linked list

fn (LinkedList) pop #

fn (mut list LinkedList<T>) pop() ?T

pop removes the last element of the linked list

fn (LinkedList) shift #

fn (mut list LinkedList<T>) shift() ?T

shift removes the first element of the linked list

fn (LinkedList) insert #

fn (mut list LinkedList<T>) insert(idx int, item T) ?

insert adds an element to the linked list at the given index

fn (LinkedList) prepend #

fn (mut list LinkedList<T>) prepend(item T)

prepend adds an element to the beginning of the linked list (equivalent to insert(0, item))

fn (LinkedList) str #

fn (list LinkedList<T>) str() string

str returns a string representation of the linked list

fn (MinHeap) insert #

fn (mut heap MinHeap<T>) insert(item T)

insert adds an element to the heap.

fn (MinHeap) pop #

fn (mut heap MinHeap<T>) pop() ?T

pop removes the top-most element from the heap.

fn (MinHeap) peek #

fn (heap MinHeap<T>) peek() ?T

peek gets the top-most element from the heap without removing it.

fn (MinHeap) len #

fn (heap MinHeap<T>) len() int

len returns the number of elements in the heap.

fn (Queue) is_empty #

fn (queue Queue<T>) is_empty() bool

is_empty checks if the queue is empty

fn (Queue) len #

fn (queue Queue<T>) len() int

len returns the length of the queue

fn (Queue) peek #

fn (queue Queue<T>) peek() ?T

peek returns the head of the queue (first element added)

fn (Queue) last #

fn (queue Queue<T>) last() ?T

last returns the tail of the queue (last element added)

fn (Queue) index #

fn (queue Queue<T>) index(idx int) ?T

index returns the element at the given index of the queue

fn (Queue) push #

fn (mut queue Queue<T>) push(item T)

push adds an element to the tail of the queue

fn (Queue) pop #

fn (mut queue Queue<T>) pop() ?T

pop removes the element at the head of the queue and returns it

fn (Queue) str #

fn (queue Queue<T>) str() string

str returns a string representation of the queue

fn (Stack) is_empty #

fn (stack Stack<T>) is_empty() bool

is_empty checks if the stack is empty

fn (Stack) len #

fn (stack Stack<T>) len() int

len returns the length of the stack

fn (Stack) peek #

fn (stack Stack<T>) peek() ?T

peek returns the top of the stack

fn (Stack) push #

fn (mut stack Stack<T>) push(item T)

push adds an element to the top of the stack

fn (Stack) pop #

fn (mut stack Stack<T>) pop() ?T

pop removes the element at the top of the stack and returns it

fn (Stack) str #

fn (stack Stack<T>) str() string

str returns a string representation of the stack

struct DoublyLinkedList #

struct DoublyLinkedList<T> {
mut:
	head &DoublyListNode<T> = 0
	tail &DoublyListNode<T> = 0

	iter &DoublyListIter<T> = 0
	len  int
}

struct LinkedList #

struct LinkedList<T> {
mut:
	head &ListNode<T> = 0
	len  int
}

struct ListNode #

struct ListNode<T> {
mut:
	data T
	next &ListNode<T> = 0
}

struct MinHeap #

struct MinHeap<T> {
mut:
	data []T
}

MinHeap is a binary minimum heap data structure.

struct Queue #

struct Queue<T> {
mut:
	elements LinkedList<T>
}

struct Stack #

struct Stack<T> {
mut:
	elements []T
}