diff options
author | Benji Dial <benji6283@gmail.com> | 2021-03-11 22:00:22 -0500 |
---|---|---|
committer | Benji Dial <benji6283@gmail.com> | 2021-03-11 22:00:22 -0500 |
commit | 5fcf57739e68a8b5053e03778aaee0eed445babd (patch) | |
tree | e7a8bab18668d112e58b1b48190195035c71fa8a /src/user/include/cxx/structs/alist.h | |
parent | 0f2398d1f622cce37925f52d978d92e6cce1c7a9 (diff) | |
download | portland-os-5fcf57739e68a8b5053e03778aaee0eed445babd.tar.gz |
settings editor, and lots of changes in service of that
Diffstat (limited to 'src/user/include/cxx/structs/alist.h')
-rw-r--r-- | src/user/include/cxx/structs/alist.h | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/user/include/cxx/structs/alist.h b/src/user/include/cxx/structs/alist.h new file mode 100644 index 0000000..050f775 --- /dev/null +++ b/src/user/include/cxx/structs/alist.h @@ -0,0 +1,41 @@ +#ifndef STRUCTS_ALIST_H +#define STRUCTS_ALIST_H + +#include <knob/format.h> +#include <knob/block.h> +#include <knob/panic.h> +#include <knob/heap.h> +#include <stdint.h> + +template<class data> +class alist { +public: + uint32_t n_entries; + uint32_t buf_size; + uint32_t expand_by; + data *buf; + + alist(uint32_t default_size=10, uint32_t expand_by=10) + : n_entries(0), buf_size(default_size), expand_by(expand_by), + buf((data *)get_block(default_size * sizeof(data))) {} + + void add_back(data d) { + if (n_entries == buf_size) { + data *const new_buf = (data *)get_block((buf_size += expand_by) * sizeof(data)); + blockcpy(new_buf, buf, n_entries * sizeof(data)); + free_block(buf); + buf = new_buf; + } + buf[n_entries++] = d; + } + + //returns -1 if not found + uint32_t index_of(data d) { + for (uint32_t i = 0; i < n_entries; ++i) + if (buf[i] == d) + return i; + return -1; + } +}; + +#endif
\ No newline at end of file |