1 //===- llvm/ADT/UniqueVector.h ----------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_ADT_UNIQUEVECTOR_H
10 #define LLVM_ADT_UNIQUEVECTOR_H
19 //===----------------------------------------------------------------------===//
20 /// UniqueVector - This class produces a sequential ID number (base 1) for each
21 /// unique entry that is added. T is the type of entries in the vector. This
22 /// class should have an implementation of operator== and of operator<.
23 /// Entries can be fetched using operator[] with the entry ID.
24 template<class T
> class UniqueVector
{
26 using VectorType
= typename
std::vector
<T
>;
27 using iterator
= typename
VectorType::iterator
;
28 using const_iterator
= typename
VectorType::const_iterator
;
31 // Map - Used to handle the correspondence of entry to ID.
32 std::map
<T
, unsigned> Map
;
34 // Vector - ID ordered vector of entries. Entries can be indexed by ID - 1.
38 /// insert - Append entry to the vector if it doesn't already exist. Returns
39 /// the entry's index + 1 to be used as a unique ID.
40 unsigned insert(const T
&Entry
) {
41 // Check if the entry is already in the map.
42 unsigned &Val
= Map
[Entry
];
44 // See if entry exists, if so return prior ID.
47 // Compute ID for entry.
48 Val
= static_cast<unsigned>(Vector
.size()) + 1;
51 Vector
.push_back(Entry
);
55 /// idFor - return the ID for an existing entry. Returns 0 if the entry is
57 unsigned idFor(const T
&Entry
) const {
58 // Search for entry in the map.
59 typename
std::map
<T
, unsigned>::const_iterator MI
= Map
.find(Entry
);
61 // See if entry exists, if so return ID.
62 if (MI
!= Map
.end()) return MI
->second
;
68 /// operator[] - Returns a reference to the entry with the specified ID.
69 const T
&operator[](unsigned ID
) const {
70 assert(ID
-1 < size() && "ID is 0 or out of range!");
71 return Vector
[ID
- 1];
74 /// Return an iterator to the start of the vector.
75 iterator
begin() { return Vector
.begin(); }
77 /// Return an iterator to the start of the vector.
78 const_iterator
begin() const { return Vector
.begin(); }
80 /// Return an iterator to the end of the vector.
81 iterator
end() { return Vector
.end(); }
83 /// Return an iterator to the end of the vector.
84 const_iterator
end() const { return Vector
.end(); }
86 /// size - Returns the number of entries in the vector.
87 size_t size() const { return Vector
.size(); }
89 /// empty - Returns true if the vector is empty.
90 bool empty() const { return Vector
.empty(); }
92 /// reset - Clears all the entries.
99 } // end namespace llvm
101 #endif // LLVM_ADT_UNIQUEVECTOR_H