File: property_mapper.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 (103 lines) | stat: -rw-r--r-- 3,001 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
#ifndef VTZERO_PROPERTY_MAPPER_HPP
#define VTZERO_PROPERTY_MAPPER_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 property_mapper.hpp
 *
 * @brief Contains the property_mapper class.
 */

#include "builder.hpp"
#include "layer.hpp"

#include <vector>

namespace vtzero {

    /**
     * Establishes a mapping between index values of properties of an existing
     * layer and a new layer. Can be used when copying some features from an
     * existing layer to a new layer.
     */
    class property_mapper {

        const layer& m_layer;
        layer_builder& m_layer_builder;

        std::vector<index_value> m_keys;
        std::vector<index_value> m_values;

    public:

        /**
         * Construct the mapping between the specified layers
         *
         * @param layer The existing layer from which (some) properties will
         *        be copied.
         * @param layer_builder The new layer that is being created.
         */
        property_mapper(const layer& layer, layer_builder& layer_builder) :
            m_layer(layer),
            m_layer_builder(layer_builder) {
            m_keys.resize(layer.key_table().size());
            m_values.resize(layer.value_table().size());
        }

        /**
         * Map the value index of a key.
         *
         * @param index The value index of the key in the existing table.
         * @returns The value index of the same key in the new table.
         */
        index_value map_key(index_value index) {
            auto& k = m_keys[index.value()];

            if (!k.valid()) {
                k = m_layer_builder.add_key_without_dup_check(m_layer.key(index));
            }

            return k;
        }

        /**
         * Map the value index of a value.
         *
         * @param index The value index of the value in the existing table.
         * @returns The value index of the same value in the new table.
         */
        index_value map_value(index_value index) {
            auto& v = m_values[index.value()];

            if (!v.valid()) {
                v = m_layer_builder.add_value_without_dup_check(m_layer.value(index));
            }

            return v;
        }

        /**
         * Map the value indexes of a key/value pair.
         *
         * @param idxs The value indexes of the key/value pair in the existing
         *        table.
         * @returns The value indexes of the same key/value pair in the new
         *          table.
         */
        index_value_pair operator()(index_value_pair idxs) {
            return {map_key(idxs.key()), map_value(idxs.value())};
        }

    }; // class property_mapper

} // namespace vtzero

#endif // VTZERO_PROPERTY_MAPPER_HPP