File: MapList.h

package info (click to toggle)
intel-graphics-compiler 1.0.12504.6-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 83,912 kB
  • sloc: cpp: 910,147; lisp: 202,655; ansic: 15,197; python: 4,025; yacc: 2,241; lex: 1,570; pascal: 244; sh: 104; makefile: 25
file content (126 lines) | stat: -rw-r--r-- 4,115 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
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2021 Intel Corporation

SPDX-License-Identifier: MIT

============================= end_copyright_notice ===========================*/

/*========================== begin_copyright_notice ============================

This file is distributed under the University of Illinois Open Source License.
See LICENSE.TXT for details.

============================= end_copyright_notice ===========================*/

#pragma once

#include <map>
#include <list>

namespace IGC {
    /// This class implements a map that also provides access to all stored values
    /// in a deterministic order. The values are kept in a std::list and the
    /// mapping is done with DenseMap from Keys to indexes in that vector.
    /// Inspired by LLVM's stable data-structures.
    template <typename KeyT, typename ValueT> class MapList {
        typedef std::list<std::pair<KeyT, ValueT> > ListType;
        typedef typename ListType::iterator iterator_type;
        typedef std::map<KeyT, iterator_type> MapType;
        typedef typename ListType::size_type SizeType;

        MapType Map;
        ListType List;

    public:
        typedef iterator_type iterator;
        typedef typename ListType::const_iterator const_iterator;

        SizeType size() const {
            return List.size();
        }

        iterator begin() {
            return List.begin();
        }

        const_iterator begin() const {
            return List.begin();
        }

        iterator end() {
            return List.end();
        }

        const_iterator end() const {
            return List.end();
        }

        bool empty() const {
            return List.empty();
        }

        std::pair<KeyT, ValueT>& front() { return List.front(); }
        const std::pair<KeyT, ValueT>& front() const { return List.front(); }
        std::pair<KeyT, ValueT>& back() { return List.back(); }
        const std::pair<KeyT, ValueT>& back()  const { return List.back(); }

        void clear() {
            Map.clear();
            List.clear();
        }

        ValueT& operator[](const KeyT& Key) {
            std::pair<KeyT, iterator> Pair = std::make_pair(Key, end());
            std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair);
            iterator& I = Result.first->second;
            if (Result.second)
                I = List.insert(end(), std::make_pair(Key, ValueT()));
            return (*I).second;
        }

        ValueT lookup(const KeyT& Key) const {
            typename MapType::const_iterator Pos = Map.find(Key);
            return Pos == Map.end() ? ValueT() : *(Pos->second).second;
        }

        std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT>& KV) {
            std::pair<KeyT, iterator> Pair = std::make_pair(KV.first, end());
            std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair);
            iterator& I = Result.first->second;
            if (Result.second) {
                I = List.insert(end(), std::make_pair(KV.first, KV.second));
                return std::make_pair(I, true);
            }
            return std::make_pair(I, false);
        }

        unsigned count(const KeyT& Key) const {
            typename MapType::const_iterator Pos = Map.find(Key);
            return Pos == Map.end() ? 0 : 1;
        }

        iterator find(const KeyT& Key) {
            typename MapType::const_iterator Pos = Map.find(Key);
            return Pos == Map.end() ? end() : Pos->second;
        }

        const_iterator find(const KeyT& Key) const {
            typename MapType::const_iterator Pos = Map.find(Key);
            return Pos == Map.end() ? end() : Pos->second;
        }

        void pop_back() {
            typename MapType::iterator Pos = Map.find(back().first);
            Map.erase(Pos);
            List.pop_back();
        }

        void erase(iterator I) {
            typename MapType::iterator Pos = Map.find((*I).first);
            Map.erase(Pos);
            List.erase(I);
        }
    };

}