lin
2025-07-30 fcd736bf35fd93b563e9bbf594f2aa7b62028cc9
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
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
 
#ifndef ANDROID_VINTF_MAP_VALUE_ITERATOR_H
#define ANDROID_VINTF_MAP_VALUE_ITERATOR_H
 
#include <iterator>
#include <map>
 
namespace android {
namespace vintf {
 
template<typename Map>
struct MapIterTypes {
    using K = typename Map::key_type;
    using V = typename Map::mapped_type;
 
    // Iterator over all values of a Map
    template<bool is_const>
    struct IteratorImpl : public std::iterator <
            std::bidirectional_iterator_tag, /* Category */
            V,
            ptrdiff_t, /* Distance */
            typename std::conditional<is_const, const V *, V *>::type /* Pointer */,
            typename std::conditional<is_const, const V &, V &>::type /* Reference */
        >
    {
        using traits = std::iterator_traits<IteratorImpl>;
        using ptr_type = typename traits::pointer;
        using ref_type = typename traits::reference;
        using diff_type = typename traits::difference_type;
 
        using map_iter = typename std::conditional<is_const,
                typename Map::const_iterator, typename Map::iterator>::type;
 
        IteratorImpl(map_iter i) : mIter(i) {}
 
        inline IteratorImpl &operator++()    {
            mIter++;
            return *this;
        }
        inline IteratorImpl  operator++(int) {
            IteratorImpl i = *this;
            mIter++;
            return i;
        }
        inline IteratorImpl &operator--()    {
            mIter--;
            return *this;
        }
        inline IteratorImpl  operator--(int) {
            IteratorImpl i = *this;
            mIter--;
            return i;
        }
        inline ref_type operator*() const  { return mIter->second; }
        inline ptr_type operator->() const { return &(mIter->second); }
        inline bool operator==(const IteratorImpl &rhs) const { return mIter == rhs.mIter; }
        inline bool operator!=(const IteratorImpl &rhs) const { return mIter != rhs.mIter; }
 
    private:
        map_iter mIter;
    };
 
    using ValueIterator = IteratorImpl<false>;
    using ConstValueIterator = IteratorImpl<true>;
 
    template<bool is_const>
    struct IterableImpl {
        using map_ref = typename std::conditional<is_const, const Map &, Map &>::type;
        IterableImpl(map_ref map) : mMap(map) {}
 
        IteratorImpl<is_const> begin() const {
            return IteratorImpl<is_const>(mMap.begin());
        }
 
        IteratorImpl<is_const> end() const {
            return IteratorImpl<is_const>(mMap.end());
        }
 
        bool empty() const { return begin() == end(); }
 
       private:
        map_ref mMap;
    };
 
    template <bool is_const>
    struct RangeImpl {
        using iter_type = typename std::conditional<is_const, typename Map::const_iterator,
                                                    typename Map::iterator>::type;
        using range_type = std::pair<iter_type, iter_type>;
        RangeImpl(range_type r) : mRange(r) {}
        IteratorImpl<is_const> begin() const { return mRange.first; }
        IteratorImpl<is_const> end() const { return mRange.second; }
        bool empty() const { return begin() == end(); }
 
       private:
        range_type mRange;
    };
 
    using ValueIterable = IterableImpl<false>;
    using ConstValueIterable = IterableImpl<true>;
};
 
template<typename K, typename V>
using ConstMapValueIterable = typename MapIterTypes<std::map<K, V>>::ConstValueIterable;
template<typename K, typename V>
using ConstMultiMapValueIterable = typename MapIterTypes<std::multimap<K, V>>::ConstValueIterable;
template <typename K, typename V>
using MapValueIterable = typename MapIterTypes<std::map<K, V>>::ValueIterable;
template <typename K, typename V>
using MultiMapValueIterable = typename MapIterTypes<std::multimap<K, V>>::ValueIterable;
 
template<typename K, typename V>
ConstMapValueIterable<K, V> iterateValues(const std::map<K, V> &map) {
    return map;
}
template<typename K, typename V>
ConstMultiMapValueIterable<K, V> iterateValues(const std::multimap<K, V> &map) {
    return map;
}
template <typename K, typename V>
MapValueIterable<K, V> iterateValues(std::map<K, V>& map) {
    return map;
}
template <typename K, typename V>
MultiMapValueIterable<K, V> iterateValues(std::multimap<K, V>& map) {
    return map;
}
 
template <typename K, typename V>
typename MapIterTypes<std::multimap<K, V>>::template RangeImpl<true> iterateValues(
    const std::multimap<K, V>& map, const K& key) {
    return map.equal_range(key);
}
 
} // namespace vintf
} // namespace android
 
#endif // ANDROID_VINTF_MAP_VALUE_ITERATOR_H