From 1ec46ee6417e3fcbe84fa01ea8406f729a17f985 Mon Sep 17 00:00:00 2001 From: "Justin C. Miller" Date: Mon, 10 Jul 2023 01:44:19 -0700 Subject: [PATCH] [util] Use chunk_size alias instead of `N` in deque Using an alias to increase readability. No functional changes. --- src/libraries/util/util/deque.h | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/libraries/util/util/deque.h b/src/libraries/util/util/deque.h index 4e40d9c..ed947ea 100644 --- a/src/libraries/util/util/deque.h +++ b/src/libraries/util/util/deque.h @@ -13,7 +13,9 @@ template class deque { public: - struct node { T items[N]; }; + static constexpr unsigned chunk_size = N; + + struct node { T items[chunk_size]; }; using list_type = linked_list; using node_type = typename list_type::item_type; @@ -21,7 +23,7 @@ public: { public: iterator(node_type *n, size_t i) : node(n), index(i) { - if (node && index >= N) { node = node->next(); index = 0; } + if (node && index >= chunk_size) { node = node->next(); index = 0; } } iterator(const iterator &o) : node(o.node), index(o.index) {} inline T& operator*() { assert(node); return node->items[index]; } @@ -33,12 +35,12 @@ public: return index == o.index && node == o.node; } private: - void incr() { if (++index >= N) { index = 0; node = node->next(); }} + void incr() { if (++index >= chunk_size) { index = 0; node = node->next(); }} node_type *node; size_t index; }; - deque() : m_first {0}, m_next {N} {} + deque() : m_first {0}, m_next {chunk_size} {} deque(deque &&other) : m_first {other.m_first}, @@ -60,13 +62,13 @@ public: node_type *n = new node_type; memset(n, 0, sizeof(node_type)); m_list.push_front(n); - m_first = N; + m_first = chunk_size; } m_list.front()->items[--m_first] = item; } inline void push_back(const T& item) { - if (m_next == N) { // need a new block at the end + if (m_next == chunk_size) { // need a new block at the end node_type *n = new node_type; memset(n, 0, sizeof(node_type)); m_list.push_back(n); @@ -78,11 +80,11 @@ public: inline T pop_front() { assert(!empty() && "Calling pop_front() on an empty deque"); T value = m_list.front()->items[m_first++]; - if (m_first == N) { + if (m_first == chunk_size) { delete m_list.pop_front(); m_first = 0; if (m_list.empty()) - m_next = N; + m_next = chunk_size; } return value; } @@ -92,7 +94,7 @@ public: T value = m_list.back()->items[--m_next]; if (m_next == 0) { delete m_list.pop_back(); - m_next = N; + m_next = chunk_size; if (m_list.empty()) m_first = 0; } @@ -128,7 +130,7 @@ public: while (!m_list.empty()) delete m_list.pop_front(); m_first = 0; - m_next = N; + m_next = chunk_size; } iterator begin() { return iterator {m_list.front(), m_first}; }