From 46573eb538c13a214a24be70307c191f1974c1da Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 22 Aug 2016 22:52:06 -0400 Subject: [PATCH] intrusive_list: Add insert_before() and insert_after() helper functions Small helpers for inserting nodes before and after an existing one. insert() is the same as insert_before(), so insert() is just made to be an alias of this. --- src/common/intrusive_list.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/common/intrusive_list.h b/src/common/intrusive_list.h index 0b9424fb..021eea05 100644 --- a/src/common/intrusive_list.h +++ b/src/common/intrusive_list.h @@ -132,6 +132,17 @@ public: * @param new_node The node to add. */ iterator insert(iterator location, pointer new_node) { + return insert_before(location, new_node); + } + + /** + * Inserts a node at the given location, moving the previous + * node occupant ahead of the one inserted. + * + * @param location The location to insert the new node. + * @param new_node The node to insert into the list. + */ + iterator insert_before(iterator location, pointer new_node) { auto existing_node = location.AsNodePointer(); new_node->next = existing_node; @@ -142,6 +153,19 @@ public: return iterator(root.get(), new_node); } + /** + * Inserts a new node into the list ahead of the position indicated. + * + * @param position Location to insert the node in front of. + * @param new_node The node to be inserted into the list. + */ + iterator insert_after(iterator position, pointer new_node) { + if (empty()) + return insert(begin(), new_node); + + return insert(++position, new_node); + } + /** * Add an entry to the start of the list. * @param node Node to add to the list.