summaryrefslogtreecommitdiff
path: root/src/user/include/cxx/structs/map.h
diff options
context:
space:
mode:
authorBenji Dial <benji6283@gmail.com>2021-03-11 22:00:22 -0500
committerBenji Dial <benji6283@gmail.com>2021-03-11 22:00:22 -0500
commit5fcf57739e68a8b5053e03778aaee0eed445babd (patch)
treee7a8bab18668d112e58b1b48190195035c71fa8a /src/user/include/cxx/structs/map.h
parent0f2398d1f622cce37925f52d978d92e6cce1c7a9 (diff)
downloadportland-os-5fcf57739e68a8b5053e03778aaee0eed445babd.tar.gz
settings editor, and lots of changes in service of that
Diffstat (limited to 'src/user/include/cxx/structs/map.h')
-rw-r--r--src/user/include/cxx/structs/map.h39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/user/include/cxx/structs/map.h b/src/user/include/cxx/structs/map.h
new file mode 100644
index 0000000..f9c477f
--- /dev/null
+++ b/src/user/include/cxx/structs/map.h
@@ -0,0 +1,39 @@
+#ifndef STRUCTS_MAP_H
+#define STRUCTS_MAP_H
+
+#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>>
+class map {
+public:
+ alist<key> keys;
+ alist<value> values;
+
+ map(uint32_t default_size=10, uint32_t expand_by=10)
+ : keys(default_size, expand_by), values(default_size, expand_by) {}
+
+ void add_pair(key k, value v) {
+ for (key *i = keys.buf; i < keys.buf + keys.n_entries; ++i)
+ if (equals(k, *i)) {
+ values.buf[i - keys.buf] = v;
+ return;
+ }
+ keys.add_back(k);
+ values.add_back(v);
+ }
+
+ __attribute__ ((pure))
+ value transform(key k, value fallback=value()) {
+ for (key *i = keys.buf; i < keys.buf + keys.n_entries; ++i)
+ if (equals(k, *i))
+ return values.buf[i - keys.buf];
+ return fallback;
+ }
+};
+
+#endif \ No newline at end of file