Iros
 
Loading...
Searching...
No Matches
hash_node_iterator.h
Go to the documentation of this file.
1#pragma once
2
10#include "di/types/prelude.h"
12
13namespace di::container {
14template<typename Value, typename Tag>
15class HashNodeIterator : public IteratorBase<HashNodeIterator<Value, Tag>, ForwardIteratorTag, Value, isize> {
16private:
17 using Node = HashNode<Tag>;
18 using ConcreteNode = decltype(Tag::node_type(in_place_type<Value>));
20 using BucketIter = meta::ContainerIterator<Bucket>;
21
22public:
23 HashNodeIterator() = default;
24
26 : m_buckets({ const_cast<Bucket*>(buckets.data()), buckets.size() }), m_bucket_index(bucket_index) {
27 if (m_bucket_index < m_buckets.size()) {
28 m_before_current = m_buckets[m_bucket_index].before_begin();
29 }
30 }
31
33 BucketIter current_current)
34 : m_buckets({ const_cast<Bucket*>(buckets.data()), buckets.size() })
35 , m_bucket_index(bucket_index)
36 , m_before_current(current_current) {}
37
38 constexpr auto operator*() const -> Value& {
39 DI_ASSERT(m_before_current != BucketIter {});
40 return Tag::down_cast(in_place_type<Value>,
41 static_cast<ConcreteNode&>(*container::next(m_before_current).node()));
42 }
43 constexpr auto operator->() const -> Value* { return util::addressof(**this); }
44
45 constexpr void advance_one() {
46 DI_ASSERT(m_before_current != BucketIter {});
47 ++m_before_current;
48 if (container::next(m_before_current) != m_buckets[m_bucket_index].end()) {
49 return;
50 }
51
52 ++m_bucket_index;
53 while (m_bucket_index < m_buckets.size()) {
54 m_before_current = m_buckets[m_bucket_index].before_begin();
55 if (container::next(m_before_current) != m_buckets[m_bucket_index].end()) {
56 return;
57 }
58
59 ++m_bucket_index;
60 }
61
62 m_before_current = {};
63 }
64
65 constexpr auto before_current() const -> BucketIter { return m_before_current; }
66 constexpr auto bucket_index() const -> usize { return m_bucket_index; }
67 constexpr auto node() const -> Node& { return static_cast<Node&>(*container::next(m_before_current).node()); }
68
69private:
70 constexpr friend auto operator==(HashNodeIterator const& a, HashNodeIterator const& b) -> bool {
71 return a.m_before_current == b.m_before_current;
72 }
73
74 vocab::Span<Bucket> m_buckets {};
75 usize m_bucket_index { 0 };
76 BucketIter m_before_current {};
77};
78}
#define DI_ASSERT(...)
Definition assert_bool.h:7
constexpr auto bucket_index() const -> usize
Definition hash_node_iterator.h:66
constexpr void advance_one()
Definition hash_node_iterator.h:45
constexpr auto operator->() const -> Value *
Definition hash_node_iterator.h:43
constexpr auto before_current() const -> BucketIter
Definition hash_node_iterator.h:65
constexpr HashNodeIterator(vocab::Span< Bucket const > buckets, usize bucket_index)
Definition hash_node_iterator.h:25
constexpr auto operator*() const -> Value &
Definition hash_node_iterator.h:38
constexpr HashNodeIterator(vocab::Span< Bucket const > buckets, usize bucket_index, BucketIter current_current)
Definition hash_node_iterator.h:32
constexpr friend auto operator==(HashNodeIterator const &a, HashNodeIterator const &b) -> bool
Definition hash_node_iterator.h:70
constexpr auto node() const -> Node &
Definition hash_node_iterator.h:67
Definition forward_list_forward_declaration.h:12
Definition span_forward_declaration.h:10
Definition sequence.h:12
constexpr auto next
Definition next.h:35
constexpr auto end
Definition end.h:47
decltype(container::begin(util::declval< T & >())) ContainerIterator
Definition container_iterator.h:8
size_t usize
Definition integers.h:33
constexpr auto in_place_type
Definition in_place_type.h:12
Definition hash_node.h:7