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