summaryrefslogtreecommitdiff
path: root/src/mbgl/renderer/style_diff.cpp
blob: 0017280310221595d7cb2c9bc5f47d9c2f043972 (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
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
#include <mbgl/renderer/style_diff.hpp>
#include <mbgl/style/layer_impl.hpp>
#include <mbgl/util/immutable.hpp>
#include <mbgl/util/variant.hpp>
#include <mbgl/util/longest_common_subsequence.hpp>

namespace mbgl {

template <class T, class Eq>
StyleDifference<T> diff(const Immutable<std::vector<T>>& a,
                        const Immutable<std::vector<T>>& b,
                        const Eq& eq) {
    StyleDifference<T> result;

    if (a == b) {
        return result;
    }

    std::vector<T> lcs;

    longest_common_subsequence(a->begin(), a->end(), b->begin(), b->end(), std::back_inserter(lcs), eq);

    auto aIt = a->begin();
    auto bIt = b->begin();
    auto lIt = lcs.begin();

    while (aIt != a->end() || bIt != b->end()) {
        if (aIt != a->end() && (lIt == lcs.end() || !eq(*lIt, *aIt))) {
            result.removed.emplace((*aIt)->id, *aIt);
            aIt++;
        } else if (bIt != b->end() && (lIt == lcs.end() || !eq(*lIt, *bIt))) {
            result.added.emplace((*bIt)->id, *bIt);
            bIt++;
        } else {
            if (aIt->get() != bIt->get()) {
                result.changed.emplace((*bIt)->id, StyleChange<T> { *aIt, *bIt });
            }
            aIt++;
            bIt++;
            lIt++;
        }
    }

    return result;
}

ImageDifference diffImages(const Immutable<std::vector<ImmutableImage>>& a,
                           const Immutable<std::vector<ImmutableImage>>& b) {
    return diff(a, b, [] (const ImmutableImage& lhs, const ImmutableImage& rhs) {
        return lhs->id == rhs->id;
    });
}

SourceDifference diffSources(const Immutable<std::vector<ImmutableSource>>& a,
                             const Immutable<std::vector<ImmutableSource>>& b) {
    return diff(a, b, [] (const ImmutableSource& lhs, const ImmutableSource& rhs) {
        return std::tie(lhs->id, lhs->type)
            == std::tie(rhs->id, rhs->type);
    });
}

LayerDifference diffLayers(const Immutable<std::vector<ImmutableLayer>>& a,
                           const Immutable<std::vector<ImmutableLayer>>& b) {
    return diff(a, b, [] (const ImmutableLayer& lhs, const ImmutableLayer& rhs) {
        return std::tie(lhs->id, lhs->type)
            == std::tie(rhs->id, rhs->type);
    });
}

bool hasLayoutDifference(const LayerDifference& layerDiff, const std::string& layerID) {
    if (layerDiff.added.count(layerID))
        return true;
    const auto it = layerDiff.changed.find(layerID);
    if (it == layerDiff.changed.end())
        return false;
    return it->second.before->hasLayoutDifference(*it->second.after);
}

} // namespace mbgl