1 #ifndef __HASHTABLE_DEQUE_HPP 2 #define __HASHTABLE_DEQUE_HPP 5 #include <unordered_map> 35 template<
class key_type, key_type inval
id_key>
50 :
std::make_unique<
std::unordered_map<key_type, KeyPair>>()),
69 bool contains(
const key_type& k)
const noexcept {
90 (*_directAccessDeque)[k] = KeyPair(
_back, invalid_key);
91 (*_directAccessDeque)[
_back]._next = k;
93 w_assert1(_directAccessDeque->size() == oldSize + 1);
98 (*_directAccessDeque)[k] = KeyPair(invalid_key, invalid_key);
122 (*_directAccessDeque)[k] = KeyPair(
_back, invalid_key);
123 (*_directAccessDeque)[
_back]._next = k;
125 w_assert1(_directAccessDeque->size() == oldSize + 1);
130 (*_directAccessDeque)[k] = KeyPair(invalid_key, invalid_key);
157 key_type oldFront =
_front;
158 KeyPair oldFrontEntry = (*_directAccessDeque)[
_front];
162 _front = oldFrontEntry._next;
163 (*_directAccessDeque)[oldFrontEntry._next]._previous = invalid_key;
191 key_type oldFront =
_front;
192 KeyPair oldFrontEntry = (*_directAccessDeque)[
_front];
196 _front = oldFrontEntry._next;
197 (*_directAccessDeque)[oldFrontEntry._next]._previous = invalid_key;
221 (*_directAccessDeque)[k] = KeyPair(invalid_key,
_front);
222 (*_directAccessDeque)[
_front]._previous = k;
224 w_assert1(_directAccessDeque->size() == oldSize + 1);
229 (*_directAccessDeque)[k] = KeyPair(invalid_key, invalid_key);
253 (*_directAccessDeque)[k] = KeyPair(invalid_key,
_front);
254 (*_directAccessDeque)[
_front]._previous = k;
256 w_assert1(_directAccessDeque->size() == oldSize + 1);
261 (*_directAccessDeque)[k] = KeyPair(invalid_key, invalid_key);
288 key_type oldBack =
_back;
289 KeyPair oldBackEntry = (*_directAccessDeque)[
_back];
293 _back = oldBackEntry._previous;
294 (*_directAccessDeque)[oldBackEntry._previous]._next = invalid_key;
322 key_type oldBack =
_back;
323 KeyPair oldBackEntry = (*_directAccessDeque)[
_back];
327 _back = oldBackEntry._previous;
328 (*_directAccessDeque)[oldBackEntry._previous]._next = invalid_key;
343 void remove(
const key_type& k) {
349 KeyPair old_key = (*_directAccessDeque)[k];
350 if (old_key._next != invalid_key) {
351 (*_directAccessDeque)[old_key._next]._previous = old_key._previous;
353 _back = old_key._previous;
355 if (old_key._previous != invalid_key) {
356 (*_directAccessDeque)[old_key._previous]._next = old_key._next;
373 void remove(key_type&& k) {
379 KeyPair old_key = (*_directAccessDeque)[k];
380 if (old_key._next != invalid_key) {
381 (*_directAccessDeque)[old_key._next]._previous = old_key._previous;
383 _back = old_key._previous;
385 if (old_key._previous != invalid_key) {
386 (*_directAccessDeque)[old_key._previous]._next = old_key._next;
413 }
else if (
_front == ref) {
417 (*_directAccessDeque)[k] = KeyPair(invalid_key, ref);
419 (*_directAccessDeque)[ref]._previous = k;
425 (*_directAccessDeque)[(*_directAccessDeque)[ref]._previous]._next = k;
426 (*_directAccessDeque)[ref]._previous = k;
449 }
else if (
_front == ref) {
453 (*_directAccessDeque)[k] = KeyPair(invalid_key, ref);
455 (*_directAccessDeque)[ref]._previous = k;
461 (*_directAccessDeque)[(*_directAccessDeque)[ref]._previous]._next = k;
462 (*_directAccessDeque)[ref]._previous = k;
485 }
else if (
_back == ref) {
489 (*_directAccessDeque)[k] = KeyPair(ref, invalid_key);
491 (*_directAccessDeque)[ref]._next = k;
497 (*_directAccessDeque)[(*_directAccessDeque)[ref]._next]._previous = k;
498 (*_directAccessDeque)[ref]._next = k;
521 }
else if (
_back == ref) {
525 (*_directAccessDeque)[k] = KeyPair(ref, invalid_key);
527 (*_directAccessDeque)[ref]._next = k;
533 (*_directAccessDeque)[(*_directAccessDeque)[ref]._next]._previous = k;
534 (*_directAccessDeque)[ref]._next = k;
615 }
else if (
_front == ref) {
620 k = (*_directAccessDeque)[ref]._previous;
640 }
else if (
_front == ref) {
645 k = (*_directAccessDeque)[ref]._previous;
665 }
else if (
_front == ref) {
690 }
else if (
_front == ref) {
715 }
else if (
_back == ref) {
720 k = (*_directAccessDeque)[ref]._next;
740 }
else if (
_back == ref) {
745 k = (*_directAccessDeque)[ref]._next;
765 }
else if (
_back == ref) {
790 }
else if (
_back == ref) {
805 inline uint64_t
length() const noexcept {
834 KeyPair(
const key_type& previous,
const key_type& next) {
890 #endif // __HASHTABLE_DEQUE_HPP key_type getBack()
Get the key at the back.
Definition: hashtable_deque.hpp:591
void getBack(key_type &k)
Get the key at the back.
Definition: hashtable_deque.hpp:576
void insertAfter(const key_type &k, const key_type &ref)
Add the key to after a reference key.
Definition: hashtable_deque.hpp:478
#define w_assert1(x)
Level 1 should not add significant extra time.
Definition: w_base.h:198
std::unique_ptr< std::unordered_map< key_type, KeyPair > > _directAccessDeque
Maps from keys to their deque entry.
Definition: hashtable_deque.hpp:872
key_type _previous
The previous element of this element.
Definition: hashtable_deque.hpp:851
key_type getAfter(const key_type &ref)
Definition: hashtable_deque.hpp:761
KeyPair(const key_type &previous, const key_type &next)
Constructor for a pair of keys with initial values.
Definition: hashtable_deque.hpp:834
Exception thrown when an entry was not already contained in an HashtableDeque.
Definition: hashtable_deque_exceptions.hpp:161
Definition: hashtable_deque.hpp:10
Exception thrown when an entry was already at the front of an HashtableDeque.
Definition: hashtable_deque_exceptions.hpp:204
key_type getFront()
Get the key at the front.
Definition: hashtable_deque.hpp:561
Exception thrown when an entry was already at the back of an HashtableDeque.
Definition: hashtable_deque_exceptions.hpp:247
void popFromBack(key_type &k)
Removes the next key from this deque.
Definition: hashtable_deque.hpp:307
bool contains(const key_type &k) const noexcept
Entry with given key contained.
Definition: hashtable_deque.hpp:69
void getBefore(key_type &k, const key_type &ref)
Get the key to before a reference key.
Definition: hashtable_deque.hpp:611
key_type _front
Element at the front.
Definition: hashtable_deque.hpp:886
void popFromBack()
Removes the next key from this deque.
Definition: hashtable_deque.hpp:274
key_type getAfter(key_type &&ref)
Definition: hashtable_deque.hpp:786
void insertBefore(key_type &&k, key_type &&ref)
Add the key to before a reference key.
Definition: hashtable_deque.hpp:442
void popFromFront()
Removes the next key from this deque.
Definition: hashtable_deque.hpp:143
uint64_t length() const noexcept
Number of entries in this deque.
Definition: hashtable_deque.hpp:805
~HashtableDeque()
Destructor of a Deque with Direct Access.
Definition: hashtable_deque.hpp:59
void pushToFront(const key_type &k)
Add the key to the front of this deque.
Definition: hashtable_deque.hpp:211
key_type _back
Element at the back.
Definition: hashtable_deque.hpp:879
void popFromFront(key_type &k)
Removes the next key from this deque.
Definition: hashtable_deque.hpp:176
KeyPair()
Constructor for an empty pair of keys.
Definition: hashtable_deque.hpp:824
void insertAfter(key_type &&k, key_type &&ref)
Add the key to after a reference key.
Definition: hashtable_deque.hpp:514
Exception thrown when an entry was already contained in an HashtableDeque.
Definition: hashtable_deque_exceptions.hpp:93
key_type getBefore(key_type &&ref)
Get the key to before a reference key.
Definition: hashtable_deque.hpp:686
A pair of keys for the implementation of a deque as a doubly-linked list.
Definition: hashtable_deque.hpp:817
Exception thrown when an HashtableDeque is empty.
Definition: hashtable_deque_exceptions.hpp:141
void getAfter(key_type &k, key_type &&ref)
Get the key to after a reference key.
Definition: hashtable_deque.hpp:736
HashtableDeque(key_type initialSize=0)
Constructor of a Deque with Direct Access.
Definition: hashtable_deque.hpp:48
void pushToFront(key_type &&k)
Add the key to the front of this deque (with move semantics)
Definition: hashtable_deque.hpp:243
void pushToBack(const key_type &k)
Add the key to the back of this deque.
Definition: hashtable_deque.hpp:80
void getAfter(key_type &k, const key_type &ref)
Get the key to after a reference key.
Definition: hashtable_deque.hpp:711
void pushToBack(key_type &&k)
Add the key to the back of this deque (with move semantics)
Definition: hashtable_deque.hpp:112
void insertBefore(const key_type &k, const key_type &ref)
Add the key to before a reference key.
Definition: hashtable_deque.hpp:406
key_type _next
The next element of this element.
Definition: hashtable_deque.hpp:859
virtual ~KeyPair()
Destructor for a pair of keys.
Definition: hashtable_deque.hpp:843
Deque with Direct Access.
Definition: hashtable_deque.hpp:36
void getBefore(key_type &k, key_type &&ref)
Get the key to before a reference key.
Definition: hashtable_deque.hpp:636
key_type getBefore(const key_type &ref)
Get the key to before a reference key.
Definition: hashtable_deque.hpp:661
void getFront(key_type &k)
Get the key at the front.
Definition: hashtable_deque.hpp:546