xref: /llvm-project/llvm/include/llvm/DWARFLinker/IndexedValuesMap.h (revision ec60030639000daa6d64a92e088d74e65fcfc8a1)
1 //===- IndexedValuesMap.h ---------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_DWARFLINKER_INDEXEDVALUESMAP_H
10 #define LLVM_DWARFLINKER_INDEXEDVALUESMAP_H
11 
12 #include "llvm/ADT/DenseMap.h"
13 #include "llvm/ADT/SmallVector.h"
14 #include <cstdint>
15 #include <utility>
16 
17 namespace llvm {
18 namespace dwarf_linker {
19 
20 /// This class stores values sequentually and assigns index to the each value.
21 template <typename T> class IndexedValuesMap {
22 public:
23   uint64_t getValueIndex(T Value) {
24     auto [It, Inserted] = ValueToIndexMap.try_emplace(Value, Values.size());
25     if (Inserted)
26       Values.push_back(Value);
27     return It->second;
28   }
29 
30   const SmallVector<T> &getValues() const { return Values; }
31 
32   void clear() {
33     ValueToIndexMap.clear();
34     Values.clear();
35   }
36 
37   bool empty() { return Values.empty(); }
38 
39 protected:
40   using ValueToIndexMapTy = DenseMap<T, uint64_t>;
41   ValueToIndexMapTy ValueToIndexMap;
42   SmallVector<T> Values;
43 };
44 
45 } // end of namespace dwarf_linker
46 } // end of namespace llvm
47 
48 #endif // LLVM_DWARFLINKER_INDEXEDVALUESMAP_H
49