XSTL
Loading...
Searching...
No Matches
association_map.hpp
Go to the documentation of this file.
1
4
5#pragma once
6
7#include "xstl/internal/map_interface.hpp"
8#include <iostream>
9#include <memory>
10#include <vector>
11#include <utility>
12
13namespace xstd {
14
19 template <typename T>
20 class association_map : public internal::map_interface<association_map<T>> {
21 public:
22 using key_type = int32_t;
23 using mapped_type = T;
24 using value_type = T;
25 using size_type = std::size_t;
26 using iterator = value_type*;
27 using const_iterator = const value_type*;
28 using key_container_type = std::vector<key_type>;
29 using mapped_container_type = std::vector<mapped_type>;
30
31 struct containers {
32 mapped_container_type values;
33 key_container_type keys;
34 key_container_type& keys_host;
35
36 explicit containers(size_type values_size, size_type keys_size)
37 : values(values_size), keys(keys_size + 1), keys_host{keys} {}
38 };
39
40 struct Extents {
41 size_type values;
42 size_type keys;
43 };
44
45 struct View {
46 value_type* values;
47 key_type* keys;
48
49 constexpr std::span<value_type> operator[](key_type key) const {
50 const auto offset = keys[key];
51 const auto size = keys[key + 1] - offset;
52 return std::span<const value_type>(values + offset, size);
53 }
54
55 constexpr std::span<value_type> operator[](key_type key) {
56 const auto offset = keys[key];
57 const auto size = keys[key + 1] - offset;
58 return std::span<value_type>(values + offset, size);
59 }
60 };
61
66 explicit association_map(size_type values, size_type keys)
67 : m_data(values, keys),
68 m_view{m_data.values.data(), m_data.keys.data()},
69 m_values{values},
70 m_keys{keys} {}
71
72#ifdef XSTL_BUILD_DOXYGEN
76 auto empty() const;
80 auto size() const;
84 auto extents() const;
85
89 iterator begin();
93 const_iterator begin() const;
97 const_iterator cbegin() const;
98
102 iterator end();
103 const_iterator end() const;
107 const_iterator cend() const;
108
113 iterator find(key_type key);
118 const_iterator find(key_type key) const;
119
124 size_type count(key_type key) const;
125
130 bool contains(key_type key) const;
131
136 iterator lower_bound(key_type key);
141 const_iterator lower_bound(key_type key) const;
142
147 iterator upper_bound(key_type key);
152 const_iterator upper_bound(key_type key) const;
153
158 std::pair<iterator, iterator> equal_range(key_type key);
163 std::pair<const_iterator, const_iterator> equal_range(key_type key) const;
164
169 void fill(std::span<key_type> keys, std::span<mapped_type> values);
170
175#endif
176
177 private:
178 containers m_data;
179 View m_view;
180 size_type m_values;
181 size_type m_keys;
182
183 private:
184 Extents extents_impl() const;
185
186 void fill_impl(std::span<key_type> keys, std::span<mapped_type> values);
187
188 friend struct internal::map_interface<association_map<T>>;
189 };
190
191} // namespace xstd
192
193#include "xstl/cpu/detail/association_map.hpp"
bool contains(key_type key) const
Checks if the association map contains values associated to a specific key.
const_iterator lower_bound(key_type key) const
Returns a const iterator to the first element with a key not less than the specified key.
const_iterator begin() const
Returns a const iterator to the beginning of the values array.
const_iterator upper_bound(key_type key) const
Returns a const iterator to the first element with a key greater than the specified key.
View view()
Returns a view of the association map.
auto extents() const
Returns the extents of the containers in the association map.
const_iterator find(key_type key) const
Returns a const iterator to the first element with a specific key.
void fill(std::span< key_type > keys, std::span< mapped_type > values)
Fills the association map with keys and values from the provided spans.
iterator lower_bound(key_type key)
Returns an iterator to the first element with a key not less than the specified key.
auto empty() const
Checks if the association map is empty.
auto size() const
Returns the number of values in the association map.
const_iterator cbegin() const
Returns a const iterator to the beginning of the values array.
iterator end()
Returns an iterator to the end of the values array.
size_type count(key_type key) const
Returns the number of values with a specific key.
association_map(size_type values, size_type keys)
Constructs an association map with a specified number of values and keys.
Definition association_map.hpp:66
std::pair< iterator, iterator > equal_range(key_type key)
Returns a pair of iterators representing the range of values with a specific key.
const_iterator cend() const
Returns a const iterator to the end of the values array.
iterator upper_bound(key_type key)
Returns an iterator to the first element with a key greater than the specified key.
std::pair< const_iterator, const_iterator > equal_range(key_type key) const
Returns a pair of const iterators representing the range of values with a specific key.
iterator begin()
Returns an iterator to the beginning of the values array.
iterator find(key_type key)
Returns an iterator to the first element with a specific key.
Definition association_map.hpp:40
Definition association_map.hpp:45
Definition association_map.hpp:31