-
Notifications
You must be signed in to change notification settings - Fork 0
/
slice.h
48 lines (35 loc) · 1.47 KB
/
slice.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#ifndef SLICE_H
#define SLICE_H
#include <stddef.h>
#include <stdint.h>
struct Slice {
// A non-null reference to the underlying buffer. Note that
// this may point to dangling memory when `len` is zero.
uint8_t * buffer;
size_t length;
};
// Creates a valid slice into `buf`.
struct Slice slice_create_valid(uint8_t * buf, size_t len);
// Creates an empty slice with `buf` set to `NULL`.
struct Slice slice_create_empty(void);
// Skips the first `n` elements in the slice.
struct Slice slice_skip_n(struct Slice self, size_t n);
// Takes up to the first `n` elements in the slice.
struct Slice slice_take_n(struct Slice self, size_t n);
// Checked version of the subscript operator. Returns `NULL` if not found.
uint8_t * slice_try_get(struct Slice self, size_t n);
uint8_t slice_get(struct Slice self, size_t n);
uint8_t slice_get_first(struct Slice self);
uint8_t slice_get_last(struct Slice self);
// Pops the last element off of this view in-place.
uint8_t slice_pop(struct Slice * self);
void slice_reverse(struct Slice self);
void slice_rotate_left(struct Slice self, size_t n);
void slice_rotate_right(struct Slice self, size_t n);
// Maintain the min-heap invariant for an almost-heap.
void slice_heapify(struct Slice self);
// Removes the element at index `n` in-place and returns it.
// The removed element is replaced by the last element in O(1)
// time. Note that order is not preserved.
uint8_t slice_swap_remove(struct Slice * self, size_t n);
#endif // SLICE_H