based

Opinionated utility library
git clone git://git.dimitrijedobrota.com/based.git
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING

commit 11bb8457b521c7a424c157031193d7ad539fb75b
parent a13de84d744796b68b61650b2a84d647993fc2dc
author Dimitrije Dobrota < mail@dimitrijedobrota.com >
date Sun, 23 Mar 2025 15:41:31 +0100

List iterator

Diffstat:
M example/list.cpp | +++++++++++++ -
M include/based/list.hpp | +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -

2 files changed, 68 insertions(+), 2 deletions(-)


diff --git a/ example/list.cpp b/ example/list.cpp

@@ -7,14 +7,21 @@

int main()
{
using instrumented = based::instrumented<double>;
using list_pool = based::list_pool<instrumented, std::uint8_t>;
using iter = list_pool::iterator;

based::list_pool<instrumented, std::uint8_t> pool;
auto pool = list_pool();
auto head = pool.node_empty();

for (std::size_t i = 0; i < 0xFF; i++) {
head = pool.allocate(static_cast<double>(i), head);
}

for (auto it = iter(pool, head); it != iter(pool); ++it) {
std::cout << *it << " ";
}
std::cout << '\n';

based::free_list(pool, head);

auto queue = pool.queue_empty();

@@ -30,6 +37,11 @@ int main()

}
}

for (auto it = iter(pool, queue.first); it != iter(pool); ++it) {
std::cout << *it << " ";
}
std::cout << '\n';

pool.free(queue);

return 0;

diff --git a/ include/based/list.hpp b/ include/based/list.hpp

@@ -1,5 +1,6 @@

#pragma once

#include <functional>
#include <utility>
#include <vector>

@@ -40,6 +41,59 @@ public:

{
}

struct iterator
{
using iterator_category = std::forward_iterator_tag;
using difference_type = list_pool::list_type;
using value_type = list_pool::value_type;
using reference = value_type&;
using pointer = value_type*;

iterator() = default;

explicit iterator(list_pool& pool)
: iterator(pool, pool.node_empty())
{
}

iterator(list_pool& pool, list_pool::list_type node)
: m_pool(pool)
, m_node(node)
{
}

reference operator*() const { return m_pool.get().value(m_node); }
pointer operator->() const { return &**this; }

iterator& operator++()
{
m_node = m_pool.get().next(m_node);
return *this;
}

iterator operator++(int)
{
iterator tmp(*this);
++*this;
return tmp;
}

friend bool operator==(const iterator& x, const iterator& y)
{
// assert(x.m_pool == y.m_pool);
return x.m_node == y.m_node;
}

friend bool operator!=(const iterator& x, const iterator& y)
{
return !(x == y);
}

private:
std::reference_wrapper<list_pool> m_pool;
list_pool::list_type m_node;
};

bool is_empty(list_type x) const { return x == node_empty(); }
list_type node_empty() const { return list_type(0); }

@@ -107,7 +161,7 @@ public:

return {new_node, new_node};
}
next(que.second) = new_node;
return {new_node, new_node};
return {que.first, new_node};
}

queue_t pop_front(const queue_t& que)