File: index.hpp

package info (click to toggle)
tilemaker 3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 78,284 kB
  • sloc: cpp: 28,715; ansic: 4,052; makefile: 180; ruby: 77; sh: 6
file content (264 lines) | stat: -rw-r--r-- 8,249 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#ifndef VTZERO_INDEX_HPP
#define VTZERO_INDEX_HPP

/*****************************************************************************

vtzero - Tiny and fast vector tile decoder and encoder in C++.

This file is from https://github.com/mapbox/vtzero where you can find more
documentation.

*****************************************************************************/

/**
 * @file index.hpp
 *
 * @brief Contains classes for indexing the key/value tables inside layers.
 */

#include "builder.hpp"
#include "types.hpp"

#include <array>
#include <cstdint>
#include <vector>

namespace vtzero {

    /**
     * Used to store the mapping between property keys and the index value
     * in the table stored in a layer.
     *
     * @tparam TMap The map class to use (std::map, std::unordered_map or
     *         something compatible).
     */
    template <template <typename...> class TMap>
    class key_index {

        layer_builder& m_builder;

        TMap<std::string, index_value> m_index;

    public:

        /**
         * Construct index.
         *
         * @param builder The layer we are building containing the key table
         *        we are creating the index for.
         */
        explicit key_index(layer_builder& builder) :
            m_builder(builder) {
        }

        /**
         * Get the index value for the specified key. If the key was not in
         * the table, it will be added.
         *
         * @param key The key to store.
         * @returns The index value of they key.
         */
        index_value operator()(const data_view key) {
            std::string text(key);
            const auto it = m_index.find(text);
            if (it == m_index.end()) {
                const auto idx = m_builder.add_key_without_dup_check(key);
                m_index.emplace(std::move(text), idx);
                return idx;
            }
            return it->second;
        }

    }; // class key_index

    /**
     * Used to store the mapping between property values and the index value
     * in the table stored in a layer. Stores the values in their original
     * form (as type TExternal) which is more efficient than the way
     * value_index_internal does it, but you need an instance of this class
     * for each value type you use.
     *
     * @tparam TInternal The type used in the vector tile to encode the value.
     *         Must be one of string/float/double/int/uint/sint/bool_value_type.
     * @tparam TExternal The type for the value used by the user of this class.
     * @tparam TMap The map class to use (std::map, std::unordered_map or
     *         something compatible).
     */
    template <typename TInternal, typename TExternal, template <typename...> class TMap>
    class value_index {

        layer_builder& m_builder;

        TMap<TExternal, index_value> m_index;

    public:

        /**
         * Construct index.
         *
         * @param builder The layer we are building containing the value
         *        table we are creating the index for.
         */
        explicit value_index(layer_builder& builder) :
            m_builder(builder) {
        }

        /**
         * Get the index value for the specified value. If the value was not in
         * the table, it will be added.
         *
         * @param value The value to store.
         * @returns The index value of they value.
         */
        index_value operator()(const TExternal& value) {
            const auto it = m_index.find(value);
            if (it == m_index.end()) {
                const auto idx = m_builder.add_value_without_dup_check(encoded_property_value{TInternal{value}});
                m_index.emplace(value, idx);
                return idx;
            }
            return it->second;
        }

    }; // class value_index

    /**
     * Used to store the mapping between bool property values and the index
     * value in the table stored in a layer.
     *
     * This is the most efficient index if you know that your property values
     * are bools.
     */
    class value_index_bool {

        layer_builder& m_builder;

        std::array<index_value, 2> m_index;

    public:

        /**
         * Construct index.
         *
         * @param builder The layer we are building containing the value
         *        table we are creating the index for.
         */
        explicit value_index_bool(layer_builder& builder) :
            m_builder(builder) {
        }

        /**
         * Get the index value for the specified value. If the value was not in
         * the table, it will be added.
         *
         * @param value The value to store.
         * @returns The index value of they value.
         */
        index_value operator()(const bool value) {
            auto& idx = m_index[static_cast<std::size_t>(value)]; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index)
            if (!idx.valid()) {
                idx = m_builder.add_value_without_dup_check(encoded_property_value{value});
            }
            return idx;
        }

    }; // class value_index_bool

    /**
     * Used to store the mapping between small unsigned int property values and
     * the index value in the table stored in a layer.
     *
     * This is the most efficient index if you know that your property values
     * are densly used small unsigned integers. This is usually the case for
     * enums.
     *
     * Interally a simple vector<index_value> is used and the value is used
     * as is to look up the index value in the vector.
     */
    class value_index_small_uint {

        layer_builder& m_builder;

        std::vector<index_value> m_index;

    public:

        /**
         * Construct index.
         *
         * @param builder The layer we are building containing the value
         *        table we are creating the index for.
         */
        explicit value_index_small_uint(layer_builder& builder) :
            m_builder(builder) {
        }

        /**
         * Get the index value for the specified value. If the value was not in
         * the table, it will be added.
         *
         * @param value The value to store.
         * @returns The index value of they value.
         */
        index_value operator()(const uint16_t value) {
            if (value >= m_index.size()) {
                m_index.resize(value + 1);
            }
            if (!m_index[value].valid()) {
                m_index[value] = m_builder.add_value_without_dup_check(encoded_property_value{value});
            }
            return m_index[value];
        }

    }; // class value_index_small_uint

    /**
     * Used to store the mapping between property values and the index value
     * in the table stored in a layer. Stores the values in the already
     * encoded form. This is simpler to use than the value_index class, but
     * has a higher overhead.
     *
     * @tparam TMap The map class to use (std::map, std::unordered_map or
     *         something compatible).
     */
    template <template <typename...> class TMap>
    class value_index_internal {

        layer_builder& m_builder;

        TMap<encoded_property_value, index_value> m_index;

    public:

        /**
         * Construct index.
         *
         * @param builder The layer we are building containing the value
         *        table we are creating the index for.
         */
        explicit value_index_internal(layer_builder& builder) :
            m_builder(builder) {
        }

        /**
         * Get the index value for the specified value. If the value was not in
         * the table, it will be added.
         *
         * @param value The value to store.
         * @returns The index value of they value.
         */
        index_value operator()(const encoded_property_value& value) {
            const auto it = m_index.find(value);
            if (it == m_index.end()) {
                const auto idx = m_builder.add_value_without_dup_check(value);
                m_index.emplace(value, idx);
                return idx;
            }
            return it->second;
        }

    }; // class value_index_internal

} // namespace vtzero

#endif // VTZERO_INDEX_HPP