UniqueVector.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/ADT/UniqueVector.h ----------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_ADT_UNIQUEVECTOR_H
  14. #define LLVM_ADT_UNIQUEVECTOR_H
  15. #include <cassert>
  16. #include <cstddef>
  17. #include <map>
  18. #include <vector>
  19. namespace llvm {
  20. //===----------------------------------------------------------------------===//
  21. /// UniqueVector - This class produces a sequential ID number (base 1) for each
  22. /// unique entry that is added. T is the type of entries in the vector. This
  23. /// class should have an implementation of operator== and of operator<.
  24. /// Entries can be fetched using operator[] with the entry ID.
  25. template<class T> class UniqueVector {
  26. public:
  27. using VectorType = typename std::vector<T>;
  28. using iterator = typename VectorType::iterator;
  29. using const_iterator = typename VectorType::const_iterator;
  30. private:
  31. // Map - Used to handle the correspondence of entry to ID.
  32. std::map<T, unsigned> Map;
  33. // Vector - ID ordered vector of entries. Entries can be indexed by ID - 1.
  34. VectorType Vector;
  35. public:
  36. /// insert - Append entry to the vector if it doesn't already exist. Returns
  37. /// the entry's index + 1 to be used as a unique ID.
  38. unsigned insert(const T &Entry) {
  39. // Check if the entry is already in the map.
  40. unsigned &Val = Map[Entry];
  41. // See if entry exists, if so return prior ID.
  42. if (Val) return Val;
  43. // Compute ID for entry.
  44. Val = static_cast<unsigned>(Vector.size()) + 1;
  45. // Insert in vector.
  46. Vector.push_back(Entry);
  47. return Val;
  48. }
  49. /// idFor - return the ID for an existing entry. Returns 0 if the entry is
  50. /// not found.
  51. unsigned idFor(const T &Entry) const {
  52. // Search for entry in the map.
  53. typename std::map<T, unsigned>::const_iterator MI = Map.find(Entry);
  54. // See if entry exists, if so return ID.
  55. if (MI != Map.end()) return MI->second;
  56. // No luck.
  57. return 0;
  58. }
  59. /// operator[] - Returns a reference to the entry with the specified ID.
  60. const T &operator[](unsigned ID) const {
  61. assert(ID-1 < size() && "ID is 0 or out of range!");
  62. return Vector[ID - 1];
  63. }
  64. /// Return an iterator to the start of the vector.
  65. iterator begin() { return Vector.begin(); }
  66. /// Return an iterator to the start of the vector.
  67. const_iterator begin() const { return Vector.begin(); }
  68. /// Return an iterator to the end of the vector.
  69. iterator end() { return Vector.end(); }
  70. /// Return an iterator to the end of the vector.
  71. const_iterator end() const { return Vector.end(); }
  72. /// size - Returns the number of entries in the vector.
  73. size_t size() const { return Vector.size(); }
  74. /// empty - Returns true if the vector is empty.
  75. bool empty() const { return Vector.empty(); }
  76. /// reset - Clears all the entries.
  77. void reset() {
  78. Map.clear();
  79. Vector.resize(0, 0);
  80. }
  81. };
  82. } // end namespace llvm
  83. #endif // LLVM_ADT_UNIQUEVECTOR_H
  84. #ifdef __GNUC__
  85. #pragma GCC diagnostic pop
  86. #endif