compiler/
lib/
examples/
std/
arch/
char/
collections/
lang/
sys/
arch.rad
68 B
char.rad
855 B
collections.rad
39 B
fmt.rad
8.1 KiB
intrinsics.rad
467 B
io.rad
1.3 KiB
lang.rad
276 B
mem.rad
2.2 KiB
sys.rad
173 B
testing.rad
2.4 KiB
tests.rad
12.9 KiB
vec.rad
1.7 KiB
std.rad
281 B
scripts/
seed/
sublime/
test/
vim/
.gitignore
336 B
.gitsigners
112 B
LICENSE
1.1 KiB
Makefile
3.7 KiB
README
4.8 KiB
STYLE
2.5 KiB
std.lib
1.2 KiB
std.lib.test
347 B
lib/std/vec.rad
raw
| 1 | //! Typed vector backed by caller-owned static storage. |
| 2 | |
| 3 | /// Typed vector metadata backed by caller-owned storage. |
| 4 | export record Vec⟨T⟩ { |
| 5 | /// Typed slice containing the vector's storage. |
| 6 | data: *mut [T], |
| 7 | /// Number of initialized elements. |
| 8 | len: u32, |
| 9 | } |
| 10 | |
| 11 | /// Initialize a vector over caller-owned storage. |
| 12 | export fn init⟨T⟩(vec: *mut Vec⟨T⟩, arena: *mut [T]) { |
| 13 | set vec.data = arena; |
| 14 | set vec.len = 0; |
| 15 | } |
| 16 | |
| 17 | /// Return the number of initialized elements. |
| 18 | export fn len⟨T⟩(vec: *Vec⟨T⟩) -> u32 { |
| 19 | return vec.len; |
| 20 | } |
| 21 | |
| 22 | /// Return the storage capacity in elements. |
| 23 | export fn capacity⟨T⟩(vec: *Vec⟨T⟩) -> u32 { |
| 24 | return vec.data.len; |
| 25 | } |
| 26 | |
| 27 | /// Reset a vector without clearing its storage. |
| 28 | export fn reset⟨T⟩(vec: *mut Vec⟨T⟩) { |
| 29 | set vec.len = 0; |
| 30 | } |
| 31 | |
| 32 | /// Return a typed pointer to an element, or `nil` when out of bounds. |
| 33 | export fn get⟨T⟩(vec: *Vec⟨T⟩, index: u32) -> ?*T { |
| 34 | if index >= vec.len { |
| 35 | return nil; |
| 36 | } |
| 37 | return &vec.data[index]; |
| 38 | } |
| 39 | |
| 40 | /// Append an element, returning `false` when the vector is full. |
| 41 | export fn push⟨T⟩(vec: *mut Vec⟨T⟩, value: T) -> bool { |
| 42 | if vec.len >= vec.data.len { |
| 43 | return false; |
| 44 | } |
| 45 | set vec.data[vec.len] = value; |
| 46 | set vec.len += 1; |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | /// Replace an initialized element, returning `false` when out of bounds. |
| 51 | export fn put⟨T⟩(vec: *mut Vec⟨T⟩, index: u32, value: T) -> bool { |
| 52 | if index >= vec.len { |
| 53 | return false; |
| 54 | } |
| 55 | set vec.data[index] = value; |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | /// Remove and return the last element, or `nil` when empty. |
| 60 | export fn pop⟨T⟩(vec: *mut Vec⟨T⟩) -> ?T { |
| 61 | if vec.len == 0 { |
| 62 | return nil; |
| 63 | } |
| 64 | set vec.len -= 1; |
| 65 | return vec.data[vec.len]; |
| 66 | } |