7 namespace tdc {
namespace esp {
8 template<
typename ipd_t>
10 template<
typename X,
size_t B>
15 struct SizeAdjust<size_t, B> {
18 inline static uint_t<B> cast_to(
size_t val) {
22 inline static size_t cast_from(
uint_t<B> val) {
27 template<
size_t N,
size_t B>
28 struct SizeAdjust<Array<N, size_t>, B> {
33 for(
size_t i = 0; i < ret.
m_data.size(); i++) {
42 for(
size_t i = 0; i < ret.
m_data.size(); i++) {
51 Meta m(
"ipd",
"dynamic_size");
58 template<
size_t N,
typename K,
typename V>
63 val = std::max(val, x);
69 using Updater = std::function<void(V&)>;
70 using ForEach = std::function<void(const Array<N, K>&,
const V&)>;
72 virtual ~DynamicMap() {}
74 virtual V access(
const Array<N, K>& key, Updater updater) = 0;
75 virtual size_t size()
const = 0;
76 virtual void for_all(ForEach f)
const = 0;
77 virtual bool can_fit_value(V new_val)
const = 0;
78 virtual bool can_fit_key(
const Array<N, K>& key)
const = 0;
80 template<
size_t BK,
size_t BV>
81 struct DynamicMapOf: DynamicMap {
82 using Updater =
typename DynamicMap::Updater;
83 using ForEach =
typename DynamicMap::ForEach;
84 using MappedK =
typename SizeAdjust<K, BK>::Type;
85 using MappedV =
typename SizeAdjust<V, BV>::Type;
90 inline DynamicMapOf(
size_t bucket_count,
const Array<N, K>& empty):
91 m_empty(SizeAdjust<
Array<N, K>, BK>::cast_to(empty)),
92 m_map(bucket_count, m_empty) {}
94 inline virtual size_t size()
const override final {
98 inline virtual void for_all(ForEach f)
const override final {
99 m_map.for_all([&](
const auto& key,
const auto& val) {
100 const auto& key2 = SizeAdjust<Array<N, K>, BK>::cast_from(key);
101 const auto& val2 = SizeAdjust<V, BK>::cast_from(val);
103 f(key2.as_view(), val2);
107 inline virtual bool can_fit_value(V val)
const override final {
111 inline virtual bool can_fit_key(
const Array<N, K>& key)
const override final {
112 if (SizeAdjust<
Array<N, K>, BK>::cast_to(key) == m_empty) {
116 size_t val = key_max(key);
120 inline virtual V access(
const Array<N, K>& key, Updater updater)
override final {
121 auto actual_key = SizeAdjust<Array<N, K>, BK>::cast_to(key);
122 return m_map.access(actual_key, [&](MappedV& val) {
123 V val2 = SizeAdjust<V, BV>::cast_from(val);
125 val = SizeAdjust<V, BV>::cast_to(val2);
131 size_t m_map_bits = 0;
132 std::unique_ptr<DynamicMap> m_map;
134 const static size_t INITIAL_BITS = 8;
137 std::unique_ptr<DynamicMap> new_map(
size_t bucket_count,
const Array<N, K>& empty) {
138 return std::make_unique<DynamicMapOf<B, B>>(bucket_count, empty);
141 inline void resize(
size_t bits) {
142 std::unique_ptr<DynamicMap> n_map;
146 size_t s = m_map->size();
149 else if (bits <= 8) { n_map = new_map<8> (s, m_empty); n_bits = 8; }
150 else if (bits <= 16) { n_map = new_map<16>(s, m_empty); n_bits = 16; }
151 else if (bits <= 24) { n_map = new_map<24>(s, m_empty); n_bits = 24; }
152 else if (bits <= 32) { n_map = new_map<32>(s, m_empty); n_bits = 32; }
153 else if (bits <= 40) { n_map = new_map<40>(s, m_empty); n_bits = 40; }
154 else if (bits <= 48) { n_map = new_map<48>(s, m_empty); n_bits = 48; }
155 else if (bits <= 56) { n_map = new_map<56>(s, m_empty); n_bits = 56; }
156 else if (bits <= 64) { n_map = new_map<64>(s, m_empty); n_bits = 64; }
158 m_map->for_all([&](
const auto& key,
const auto& val) {
159 n_map->access(key, [&](
auto& v) {
164 DCHECK_EQ(m_map->size(), n_map->size());
165 m_map = std::move(n_map);
169 inline void resize_key(
const Array<N, K>& max_key) {
170 auto max = key_max(max_key);
171 resize(std::max(
size_t(
bits_for(max)), m_map_bits + 1));
173 inline void resize_val(V max_val) {
175 resize(std::max(
size_t(
bits_for(max)), m_map_bits + 1));
180 m_map_bits = INITIAL_BITS;
181 m_map = new_map<INITIAL_BITS>(bucket_count, m_empty);
184 template<
typename Updater>
186 if (!m_map->can_fit_key(key)) {
191 bool value_resize =
false;
193 V ret = m_map->access(key, [&](V& val) {
196 if (m_map->can_fit_value(copy)) {
205 ret = m_map->access(key, [&](V& val) {
210 DCHECK_EQ(ret, copy);
216 return m_map->size();
Contains the text compression and encoding framework.
constexpr uint_fast8_t bits_for(size_t n)
Computes the number of bits required to store the given integer value.
V access(const Array< N, K > &key, Updater updater)
Algorithm(Algorithm const &)=default
typename uint_dispatch_t< N >::type uint_t
std::array< T, N > m_data
IPDMap(size_t bucket_count, const Array< N, K > &empty)
Interface for algorithms.