Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.5 KiB

  1. //===-- llvm/ADT/UniqueVector.h ---------------------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef LLVM_ADT_UNIQUEVECTOR_H
  10. #define LLVM_ADT_UNIQUEVECTOR_H
  11. #include <cassert>
  12. #include <map>
  13. #include <vector>
  14. namespace llvm {
  15. //===----------------------------------------------------------------------===//
  16. /// UniqueVector - This class produces a sequential ID number (base 1) for each
  17. /// unique entry that is added. T is the type of entries in the vector. This
  18. /// class should have an implementation of operator== and of operator<.
  19. /// Entries can be fetched using operator[] with the entry ID.
  20. template<class T> class UniqueVector {
  21. private:
  22. // Map - Used to handle the correspondence of entry to ID.
  23. std::map<T, unsigned> Map;
  24. // Vector - ID ordered vector of entries. Entries can be indexed by ID - 1.
  25. //
  26. std::vector<T> Vector;
  27. public:
  28. /// insert - Append entry to the vector if it doesn't already exist. Returns
  29. /// the entry's index + 1 to be used as a unique ID.
  30. unsigned insert(const T &Entry) {
  31. // Check if the entry is already in the map.
  32. unsigned &Val = Map[Entry];
  33. // See if entry exists, if so return prior ID.
  34. if (Val) return Val;
  35. // Compute ID for entry.
  36. Val = static_cast<unsigned>(Vector.size()) + 1;
  37. // Insert in vector.
  38. Vector.push_back(Entry);
  39. return Val;
  40. }
  41. /// idFor - return the ID for an existing entry. Returns 0 if the entry is
  42. /// not found.
  43. unsigned idFor(const T &Entry) const {
  44. // Search for entry in the map.
  45. typename std::map<T, unsigned>::const_iterator MI = Map.find(Entry);
  46. // See if entry exists, if so return ID.
  47. if (MI != Map.end()) return MI->second;
  48. // No luck.
  49. return 0;
  50. }
  51. /// operator[] - Returns a reference to the entry with the specified ID.
  52. ///
  53. const T &operator[](unsigned ID) const {
  54. assert(ID-1 < size() && "ID is 0 or out of range!");
  55. return Vector[ID - 1];
  56. }
  57. /// size - Returns the number of entries in the vector.
  58. ///
  59. size_t size() const { return Vector.size(); }
  60. /// empty - Returns true if the vector is empty.
  61. ///
  62. bool empty() const { return Vector.empty(); }
  63. /// reset - Clears all the entries.
  64. ///
  65. void reset() {
  66. Map.clear();
  67. Vector.resize(0, 0);
  68. }
  69. };
  70. } // End of namespace llvm
  71. #endif // LLVM_ADT_UNIQUEVECTOR_H