diff options
author | Benji Dial <benji6283@gmail.com> | 2021-06-21 17:47:13 -0400 |
---|---|---|
committer | Benji Dial <benji6283@gmail.com> | 2021-06-21 17:47:13 -0400 |
commit | f57e2eabe0a10c9732c83532e01654a499fb8dcf (patch) | |
tree | cbf91a23fcdd65e0ea7ed55b0940ca7042d59bef /src/user/include/cxx/structs | |
parent | 83835306d57461205a7bcfef9f4c3e06bc504006 (diff) | |
download | portland-os-f57e2eabe0a10c9732c83532e01654a499fb8dcf.tar.gz |
many, many changes; settings is broken
Diffstat (limited to 'src/user/include/cxx/structs')
-rw-r--r-- | src/user/include/cxx/structs/alist.h | 63 | ||||
-rw-r--r-- | src/user/include/cxx/structs/duple.h | 1 | ||||
-rw-r--r-- | src/user/include/cxx/structs/map.h | 7 |
3 files changed, 57 insertions, 14 deletions
diff --git a/src/user/include/cxx/structs/alist.h b/src/user/include/cxx/structs/alist.h index 050f775..13a24c4 100644 --- a/src/user/include/cxx/structs/alist.h +++ b/src/user/include/cxx/structs/alist.h @@ -15,20 +15,67 @@ public: uint32_t expand_by; data *buf; - alist(uint32_t default_size=10, uint32_t expand_by=10) + 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))) {} + buf(new data[default_size]) {} + + //from is copied + alist(const data *from, uint32_t count, uint32_t extra_size = 10, uint32_t expand_by = 10) + : n_entries(count), buf_size(count + extra_size), expand_by(expand_by), + buf(new data[count + extra_size]) { + for (uint32_t i = 0; i < count; ++i) + buf[i] = from[i]; + } + + ~alist() { + delete[] buf; + } + + void copy_from(const data *from, uint32_t count) { + expand_to(count); + n_entries = count; + for (uint32_t i = 0; i < count; ++i) + buf[i] = from[i]; + } + + void expand_to(uint32_t size) { + if (size <= buf_size) + return; + data *const new_buf = new data[size * sizeof(data)]; + for (uint32_t i = 0; i < buf_size; ++i) + new_buf[i] = buf[i]; + delete[] buf; + buf = new_buf; + buf_size = size; + } + + void copy_from(const alist<data> &other) { + expand_to(n_entries = other.n_entries); + for (uint32_t i = 0; i < n_entries; ++i) + buf[i] = other.buf[i]; + } 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; - } + if (n_entries == buf_size) + expand_to(buf_size + expand_by); buf[n_entries++] = d; } + void insert(uint32_t i, data d) { + if (n_entries == buf_size) + expand_to(buf_size + expand_by); + for (uint32_t n = n_entries; n > i; --n) + buf[n] = buf[n - 1]; + buf[i] = d; + ++n_entries; + } + + void remove(uint32_t i) { + --n_entries; + for (uint32_t n = i; n < n_entries; ++n) + buf[n] = buf[n + 1]; + } + //returns -1 if not found uint32_t index_of(data d) { for (uint32_t i = 0; i < n_entries; ++i) diff --git a/src/user/include/cxx/structs/duple.h b/src/user/include/cxx/structs/duple.h index 874e4f6..0b23c92 100644 --- a/src/user/include/cxx/structs/duple.h +++ b/src/user/include/cxx/structs/duple.h @@ -4,6 +4,7 @@ template<class at, class bt> class duple { public: + duple() : a(), b() {} duple(at a, bt b) : a(a), b(b) {} at a; diff --git a/src/user/include/cxx/structs/map.h b/src/user/include/cxx/structs/map.h index f9c477f..311bfa9 100644 --- a/src/user/include/cxx/structs/map.h +++ b/src/user/include/cxx/structs/map.h @@ -3,12 +3,7 @@ #include <structs/alist.h> -template<class type> -bool default_equals(type a, type b) { - return a == b; -} - -template<class key, class value, bool (*equals)(key, key) = &default_equals<key>> +template<class key, class value, bool (*equals)(key, key) = &key::operator==> class map { public: alist<key> keys; |