summaryrefslogtreecommitdiff
path: root/src/user/include/cxx/structs/dllist.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/dllist.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/dllist.h')
-rw-r--r--src/user/include/cxx/structs/dllist.h38
1 files changed, 37 insertions, 1 deletions
diff --git a/src/user/include/cxx/structs/dllist.h b/src/user/include/cxx/structs/dllist.h
index 5783364..91b0369 100644
--- a/src/user/include/cxx/structs/dllist.h
+++ b/src/user/include/cxx/structs/dllist.h
@@ -1,6 +1,8 @@
#ifndef STRUCTS_DLLIST_H
#define STRUCTS_DLLIST_H
+#include <stdint.h>
+
template<class data>
class dllist {
public:
@@ -13,20 +15,34 @@ public:
: next(next), prev(prev), d(d) {}
};
node *first;
+ node *last;
- dllist() : first(0) {}
+ dllist() : first(0), last(0) {}
void add_front(data d) {
node *const n = new node(first, 0, d);
if (first)
first->prev = n;
first = n;
+ if (!last)
+ last = n;
+ }
+
+ void add_back(data d) {
+ node *const n = new node(0, last, d);
+ if (last)
+ last->next = n;
+ last = n;
+ if (!first)
+ first = n;
}
//return previous, or zero if this is the first
node *remove_in_place(node *n) {
if (n == first)
first = n->next;
+ if (n == last)
+ last = n->prev;
if (n->next)
n->next->prev = n->prev;
if (n->prev)
@@ -34,6 +50,26 @@ public:
delete n;
return n->prev;
}
+
+ bool try_remove_by_ref(data d) {
+ for (node *n = first; n; n = n->next)
+ if (&n->d == &d) {
+ remove_in_place(n);
+ return true;
+ }
+ return false;
+ }
+
+ //returns -1 if it isn't found
+ uint32_t index_of(data d) {
+ uint32_t i = 0;
+ for (const node *n = first; n; n = n->next)
+ if (d == n->d)
+ return i;
+ else
+ ++i;
+ return -1;
+ }
};
#endif \ No newline at end of file