summaryrefslogtreecommitdiff
path: root/src/mbgl/style/group_by_layout.cpp
blob: 52d33827ef91ce0391d9f76cabddffe41a034b88 (plain)
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
#include <mbgl/style/group_by_layout.hpp>
#include <mbgl/style/layer.hpp>
#include <mbgl/style/layer_impl.hpp>
#include <mbgl/style/conversion/stringify.hpp>
#include <mbgl/util/rapidjson.hpp>

#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>

#include <unordered_map>

namespace mbgl {
namespace style {

std::string layoutKey(const Layer& layer) {
    using namespace conversion;

    rapidjson::StringBuffer s;
    rapidjson::Writer<rapidjson::StringBuffer> writer(s);

    writer.StartArray();
    writer.Uint(static_cast<uint32_t>(layer.type));
    writer.String(layer.baseImpl->source);
    writer.String(layer.baseImpl->sourceLayer);
    writer.Double(layer.baseImpl->minZoom);
    writer.Double(layer.baseImpl->maxZoom);
    writer.Uint(static_cast<uint32_t>(layer.baseImpl->visibility));
    stringify(writer, layer.baseImpl->filter);
    layer.baseImpl->stringifyLayout(writer);
    writer.EndArray();

    return s.GetString();
}

std::vector<std::vector<std::unique_ptr<Layer>>> groupByLayout(std::vector<std::unique_ptr<Layer>> layers) {
    std::unordered_map<std::string, std::vector<std::unique_ptr<Layer>>> map;
    for (auto& layer : layers) {
        auto& vector = map[layoutKey(*layer)];
        vector.push_back(std::move(layer));
    }

    std::vector<std::vector<std::unique_ptr<Layer>>> result;
    for (auto& pair : map) {
        result.push_back(std::move(pair.second));
    }

    return result;
}

} // namespace style
} // namespace mbgl