|
|
|||
File indexing completed on 2026-05-10 08:43:10
0001 //===- llvm/ADT/UniqueVector.h ----------------------------------*- C++ -*-===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 0009 #ifndef LLVM_ADT_UNIQUEVECTOR_H 0010 #define LLVM_ADT_UNIQUEVECTOR_H 0011 0012 #include <cassert> 0013 #include <cstddef> 0014 #include <map> 0015 #include <vector> 0016 0017 namespace llvm { 0018 0019 //===----------------------------------------------------------------------===// 0020 /// UniqueVector - This class produces a sequential ID number (base 1) for each 0021 /// unique entry that is added. T is the type of entries in the vector. This 0022 /// class should have an implementation of operator== and of operator<. 0023 /// Entries can be fetched using operator[] with the entry ID. 0024 template<class T> class UniqueVector { 0025 public: 0026 using VectorType = typename std::vector<T>; 0027 using iterator = typename VectorType::iterator; 0028 using const_iterator = typename VectorType::const_iterator; 0029 0030 private: 0031 // Map - Used to handle the correspondence of entry to ID. 0032 std::map<T, unsigned> Map; 0033 0034 // Vector - ID ordered vector of entries. Entries can be indexed by ID - 1. 0035 VectorType Vector; 0036 0037 public: 0038 /// insert - Append entry to the vector if it doesn't already exist. Returns 0039 /// the entry's index + 1 to be used as a unique ID. 0040 unsigned insert(const T &Entry) { 0041 // Check if the entry is already in the map. 0042 unsigned &Val = Map[Entry]; 0043 0044 // See if entry exists, if so return prior ID. 0045 if (Val) return Val; 0046 0047 // Compute ID for entry. 0048 Val = static_cast<unsigned>(Vector.size()) + 1; 0049 0050 // Insert in vector. 0051 Vector.push_back(Entry); 0052 return Val; 0053 } 0054 0055 /// idFor - return the ID for an existing entry. Returns 0 if the entry is 0056 /// not found. 0057 unsigned idFor(const T &Entry) const { 0058 // Search for entry in the map. 0059 typename std::map<T, unsigned>::const_iterator MI = Map.find(Entry); 0060 0061 // See if entry exists, if so return ID. 0062 if (MI != Map.end()) return MI->second; 0063 0064 // No luck. 0065 return 0; 0066 } 0067 0068 /// operator[] - Returns a reference to the entry with the specified ID. 0069 const T &operator[](unsigned ID) const { 0070 assert(ID-1 < size() && "ID is 0 or out of range!"); 0071 return Vector[ID - 1]; 0072 } 0073 0074 /// Return an iterator to the start of the vector. 0075 iterator begin() { return Vector.begin(); } 0076 0077 /// Return an iterator to the start of the vector. 0078 const_iterator begin() const { return Vector.begin(); } 0079 0080 /// Return an iterator to the end of the vector. 0081 iterator end() { return Vector.end(); } 0082 0083 /// Return an iterator to the end of the vector. 0084 const_iterator end() const { return Vector.end(); } 0085 0086 /// size - Returns the number of entries in the vector. 0087 size_t size() const { return Vector.size(); } 0088 0089 /// empty - Returns true if the vector is empty. 0090 bool empty() const { return Vector.empty(); } 0091 0092 /// reset - Clears all the entries. 0093 void reset() { 0094 Map.clear(); 0095 Vector.resize(0, 0); 0096 } 0097 }; 0098 0099 } // end namespace llvm 0100 0101 #endif // LLVM_ADT_UNIQUEVECTOR_H
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|