[util] Use chunk_size alias instead of N in deque

Using an alias to increase readability. No functional changes.
This commit is contained in:
Justin C. Miller
2023-07-10 01:44:19 -07:00
parent 3ba0600694
commit 1ec46ee641

View File

@@ -13,7 +13,9 @@ template <typename T, unsigned N = 16>
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<node>;
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}; }