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.

132 lines
3.9 KiB

  1. //===- llvm/ADT/MapVector.h - Map with deterministic value order *- 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. //
  10. // This file implements a map that provides insertion order iteration. The
  11. // interface is purposefully minimal. The key is assumed to be cheap to copy
  12. // and 2 copies are kept, one for indexing in a DenseMap, one for iteration in
  13. // a std::vector.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #ifndef LLVM_ADT_MAPVECTOR_H
  17. #define LLVM_ADT_MAPVECTOR_H
  18. #include "llvm/ADT/ArrayRef.h"
  19. #include "llvm/ADT/DenseMap.h"
  20. #include "llvm/ADT/STLExtras.h"
  21. #include <vector>
  22. namespace llvm {
  23. /// This class implements a map that also provides access to all stored values
  24. /// in a deterministic order. The values are kept in a std::vector and the
  25. /// mapping is done with DenseMap from Keys to indexes in that vector.
  26. template<typename KeyT, typename ValueT,
  27. typename MapType = llvm::DenseMap<KeyT, unsigned>,
  28. typename VectorType = std::vector<std::pair<KeyT, ValueT> > >
  29. class MapVector {
  30. typedef typename VectorType::size_type SizeType;
  31. MapType Map;
  32. VectorType Vector;
  33. public:
  34. typedef typename VectorType::iterator iterator;
  35. typedef typename VectorType::const_iterator const_iterator;
  36. SizeType size() const {
  37. return Vector.size();
  38. }
  39. iterator begin() {
  40. return Vector.begin();
  41. }
  42. const_iterator begin() const {
  43. return Vector.begin();
  44. }
  45. iterator end() {
  46. return Vector.end();
  47. }
  48. const_iterator end() const {
  49. return Vector.end();
  50. }
  51. bool empty() const {
  52. return Vector.empty();
  53. }
  54. std::pair<KeyT, ValueT> &front() { return Vector.front(); }
  55. const std::pair<KeyT, ValueT> &front() const { return Vector.front(); }
  56. std::pair<KeyT, ValueT> &back() { return Vector.back(); }
  57. const std::pair<KeyT, ValueT> &back() const { return Vector.back(); }
  58. void clear() {
  59. Map.clear();
  60. Vector.clear();
  61. }
  62. ValueT &operator[](const KeyT &Key) {
  63. std::pair<KeyT, unsigned> Pair = std::make_pair(Key, 0);
  64. std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair);
  65. unsigned &I = Result.first->second;
  66. if (Result.second) {
  67. Vector.push_back(std::make_pair(Key, ValueT()));
  68. I = Vector.size() - 1;
  69. }
  70. return Vector[I].second;
  71. }
  72. ValueT lookup(const KeyT &Key) const {
  73. typename MapType::const_iterator Pos = Map.find(Key);
  74. return Pos == Map.end()? ValueT() : Vector[Pos->second].second;
  75. }
  76. std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
  77. std::pair<KeyT, unsigned> Pair = std::make_pair(KV.first, 0);
  78. std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair);
  79. unsigned &I = Result.first->second;
  80. if (Result.second) {
  81. Vector.push_back(std::make_pair(KV.first, KV.second));
  82. I = Vector.size() - 1;
  83. return std::make_pair(llvm::prior(end()), true);
  84. }
  85. return std::make_pair(begin() + I, false);
  86. }
  87. unsigned count(const KeyT &Key) const {
  88. typename MapType::const_iterator Pos = Map.find(Key);
  89. return Pos == Map.end()? 0 : 1;
  90. }
  91. iterator find(const KeyT &Key) {
  92. typename MapType::const_iterator Pos = Map.find(Key);
  93. return Pos == Map.end()? Vector.end() :
  94. (Vector.begin() + Pos->second);
  95. }
  96. const_iterator find(const KeyT &Key) const {
  97. typename MapType::const_iterator Pos = Map.find(Key);
  98. return Pos == Map.end()? Vector.end() :
  99. (Vector.begin() + Pos->second);
  100. }
  101. /// \brief Remove the last element from the vector.
  102. void pop_back() {
  103. typename MapType::iterator Pos = Map.find(Vector.back().first);
  104. Map.erase(Pos);
  105. Vector.pop_back();
  106. }
  107. };
  108. }
  109. #endif