Stack (array based)

API

#include <ds/arrstack.h>

Array based stacks are backed by a slice. Data is stored in a contiguous array for better cache locality. Some of the functions for manipulating the underlying memory are aliases to slice functions. To keep the code clearer you should use the aliases when working with array based data structures, and you should not interact with the slice directly.

See also

slice

Functions

struct slice *arrstack_make(size_t el_size, size_t el_no) __attribute__((alias("slice_make")))

Initializes a stack and allocates its memory.

Every call to slice_make must have a matching call to slice_del to release the managed memory.

See also

slice_make

Parameters:
  • p – Handle to the slice.

  • el_size – Size of each element.

  • el_no – Number of elements for the initial allocation.

bool arrstack_empty(struct slice *p) __attribute__((alias("slice_empty")))

Returns whether the given stack is empty.

See also

slice_empty

Parameters:
  • p – Handle to the slice.

Returns:

Non-zero if the stack is empty.

void arrstack_push(struct slice *p, void *el) __attribute__((alias("slice_append")))

Pushes a value to the top of the stack without checking its capacity.

Copies the element stored at el to the top of the stack. Does not check if there is enough space on the slice. For a safe version that grows the slice when it’s out of space, use arrstack_spush.

See also

slice_append

Parameters:
  • p – Handle to the slice.

  • el – Pointer to the element to push.

void arrstack_spush(struct slice *p, void *el) __attribute__((alias("slice_sappend")))

Pushes a value to the top of the stack checking its capacity.

Copies the element stored at el to the top of the stack. If the slice is out of space, grows the allocated memory before pushing. When certain that the slice has enough space, use arrstack_push instead.

See also

slice_sappend

Parameters:
  • p – Handle to the slice.

  • el – Pointer to the element to push.

data *arrstack_peek(struct slice *p) __attribute__((alias("slice_last")))

Returns a pointer to the element at the top of the stack.

Returns a view into the stack without copying the data.

Parameters:
  • p – Handle to the slice.

void arrstack_pop(struct slice *p, data *dst)

Pops and returns the value at top of the stack.

Copies the value at the top of the stack to dst, then pops it. Should be used when you need to keep the value outside the stack after popping it. If you only need to read the value without copying it out of the stack, use arrstack_peek followed by arrstack_rwd to avoid a copy.

Parameters:
  • p – Handle to the slice.

  • dst – Handle to allocated memory to hold the popped element.

void arrstack_rwd(struct slice *p)

Pops and discards the value at top of the stack.

Meant to be used if you only need to read the value at the top of the stack *(without copying it out)*. Rewinds the stack to pop the value at the top while avoiding a copy. Should probably be used after a arrstack_peek. For copying the popped value out of the stack use arrstack_pop instead.

Parameters:
  • p – Handle to the slice.

void arrstack_clear(struct slice *p) __attribute__((alias("slice_clear")))

Pops and discards all values on the stack.

See also

slice_clear

Parameters:
  • p – Handle to the slice.