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.

291 lines
7.9 KiB

  1. //===- llvm/ADT/TinyPtrVector.h - 'Normally tiny' vectors -------*- 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_TINYPTRVECTOR_H
  10. #define LLVM_ADT_TINYPTRVECTOR_H
  11. #include "llvm/ADT/ArrayRef.h"
  12. #include "llvm/ADT/PointerUnion.h"
  13. #include "llvm/ADT/STLExtras.h"
  14. #include "llvm/ADT/SmallVector.h"
  15. #include "llvm/Support/Compiler.h"
  16. namespace llvm {
  17. /// TinyPtrVector - This class is specialized for cases where there are
  18. /// normally 0 or 1 element in a vector, but is general enough to go beyond that
  19. /// when required.
  20. ///
  21. /// NOTE: This container doesn't allow you to store a null pointer into it.
  22. ///
  23. template <typename EltTy>
  24. class TinyPtrVector {
  25. public:
  26. typedef llvm::SmallVector<EltTy, 4> VecTy;
  27. typedef typename VecTy::value_type value_type;
  28. llvm::PointerUnion<EltTy, VecTy*> Val;
  29. TinyPtrVector() {}
  30. ~TinyPtrVector() {
  31. if (VecTy *V = Val.template dyn_cast<VecTy*>())
  32. delete V;
  33. }
  34. TinyPtrVector(const TinyPtrVector &RHS) : Val(RHS.Val) {
  35. if (VecTy *V = Val.template dyn_cast<VecTy*>())
  36. Val = new VecTy(*V);
  37. }
  38. TinyPtrVector &operator=(const TinyPtrVector &RHS) {
  39. if (this == &RHS)
  40. return *this;
  41. if (RHS.empty()) {
  42. this->clear();
  43. return *this;
  44. }
  45. // Try to squeeze into the single slot. If it won't fit, allocate a copied
  46. // vector.
  47. if (Val.template is<EltTy>()) {
  48. if (RHS.size() == 1)
  49. Val = RHS.front();
  50. else
  51. Val = new VecTy(*RHS.Val.template get<VecTy*>());
  52. return *this;
  53. }
  54. // If we have a full vector allocated, try to re-use it.
  55. if (RHS.Val.template is<EltTy>()) {
  56. Val.template get<VecTy*>()->clear();
  57. Val.template get<VecTy*>()->push_back(RHS.front());
  58. } else {
  59. *Val.template get<VecTy*>() = *RHS.Val.template get<VecTy*>();
  60. }
  61. return *this;
  62. }
  63. #if LLVM_HAS_RVALUE_REFERENCES
  64. TinyPtrVector(TinyPtrVector &&RHS) : Val(RHS.Val) {
  65. RHS.Val = (EltTy)0;
  66. }
  67. TinyPtrVector &operator=(TinyPtrVector &&RHS) {
  68. if (this == &RHS)
  69. return *this;
  70. if (RHS.empty()) {
  71. this->clear();
  72. return *this;
  73. }
  74. // If this vector has been allocated on the heap, re-use it if cheap. If it
  75. // would require more copying, just delete it and we'll steal the other
  76. // side.
  77. if (VecTy *V = Val.template dyn_cast<VecTy*>()) {
  78. if (RHS.Val.template is<EltTy>()) {
  79. V->clear();
  80. V->push_back(RHS.front());
  81. return *this;
  82. }
  83. delete V;
  84. }
  85. Val = RHS.Val;
  86. RHS.Val = (EltTy)0;
  87. return *this;
  88. }
  89. #endif
  90. // implicit conversion operator to ArrayRef.
  91. operator ArrayRef<EltTy>() const {
  92. if (Val.isNull())
  93. return ArrayRef<EltTy>();
  94. if (Val.template is<EltTy>())
  95. return *Val.getAddrOfPtr1();
  96. return *Val.template get<VecTy*>();
  97. }
  98. bool empty() const {
  99. // This vector can be empty if it contains no element, or if it
  100. // contains a pointer to an empty vector.
  101. if (Val.isNull()) return true;
  102. if (VecTy *Vec = Val.template dyn_cast<VecTy*>())
  103. return Vec->empty();
  104. return false;
  105. }
  106. unsigned size() const {
  107. if (empty())
  108. return 0;
  109. if (Val.template is<EltTy>())
  110. return 1;
  111. return Val.template get<VecTy*>()->size();
  112. }
  113. typedef const EltTy *const_iterator;
  114. typedef EltTy *iterator;
  115. iterator begin() {
  116. if (Val.template is<EltTy>())
  117. return Val.getAddrOfPtr1();
  118. return Val.template get<VecTy *>()->begin();
  119. }
  120. iterator end() {
  121. if (Val.template is<EltTy>())
  122. return begin() + (Val.isNull() ? 0 : 1);
  123. return Val.template get<VecTy *>()->end();
  124. }
  125. const_iterator begin() const {
  126. return (const_iterator)const_cast<TinyPtrVector*>(this)->begin();
  127. }
  128. const_iterator end() const {
  129. return (const_iterator)const_cast<TinyPtrVector*>(this)->end();
  130. }
  131. EltTy operator[](unsigned i) const {
  132. assert(!Val.isNull() && "can't index into an empty vector");
  133. if (EltTy V = Val.template dyn_cast<EltTy>()) {
  134. assert(i == 0 && "tinyvector index out of range");
  135. return V;
  136. }
  137. assert(i < Val.template get<VecTy*>()->size() &&
  138. "tinyvector index out of range");
  139. return (*Val.template get<VecTy*>())[i];
  140. }
  141. EltTy front() const {
  142. assert(!empty() && "vector empty");
  143. if (EltTy V = Val.template dyn_cast<EltTy>())
  144. return V;
  145. return Val.template get<VecTy*>()->front();
  146. }
  147. EltTy back() const {
  148. assert(!empty() && "vector empty");
  149. if (EltTy V = Val.template dyn_cast<EltTy>())
  150. return V;
  151. return Val.template get<VecTy*>()->back();
  152. }
  153. void push_back(EltTy NewVal) {
  154. assert(NewVal != 0 && "Can't add a null value");
  155. // If we have nothing, add something.
  156. if (Val.isNull()) {
  157. Val = NewVal;
  158. return;
  159. }
  160. // If we have a single value, convert to a vector.
  161. if (EltTy V = Val.template dyn_cast<EltTy>()) {
  162. Val = new VecTy();
  163. Val.template get<VecTy*>()->push_back(V);
  164. }
  165. // Add the new value, we know we have a vector.
  166. Val.template get<VecTy*>()->push_back(NewVal);
  167. }
  168. void pop_back() {
  169. // If we have a single value, convert to empty.
  170. if (Val.template is<EltTy>())
  171. Val = (EltTy)0;
  172. else if (VecTy *Vec = Val.template get<VecTy*>())
  173. Vec->pop_back();
  174. }
  175. void clear() {
  176. // If we have a single value, convert to empty.
  177. if (Val.template is<EltTy>()) {
  178. Val = (EltTy)0;
  179. } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
  180. // If we have a vector form, just clear it.
  181. Vec->clear();
  182. }
  183. // Otherwise, we're already empty.
  184. }
  185. iterator erase(iterator I) {
  186. assert(I >= begin() && "Iterator to erase is out of bounds.");
  187. assert(I < end() && "Erasing at past-the-end iterator.");
  188. // If we have a single value, convert to empty.
  189. if (Val.template is<EltTy>()) {
  190. if (I == begin())
  191. Val = (EltTy)0;
  192. } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
  193. // multiple items in a vector; just do the erase, there is no
  194. // benefit to collapsing back to a pointer
  195. return Vec->erase(I);
  196. }
  197. return end();
  198. }
  199. iterator erase(iterator S, iterator E) {
  200. assert(S >= begin() && "Range to erase is out of bounds.");
  201. assert(S <= E && "Trying to erase invalid range.");
  202. assert(E <= end() && "Trying to erase past the end.");
  203. if (Val.template is<EltTy>()) {
  204. if (S == begin() && S != E)
  205. Val = (EltTy)0;
  206. } else if (VecTy *Vec = Val.template dyn_cast<VecTy*>()) {
  207. return Vec->erase(S, E);
  208. }
  209. return end();
  210. }
  211. iterator insert(iterator I, const EltTy &Elt) {
  212. assert(I >= this->begin() && "Insertion iterator is out of bounds.");
  213. assert(I <= this->end() && "Inserting past the end of the vector.");
  214. if (I == end()) {
  215. push_back(Elt);
  216. return llvm::prior(end());
  217. }
  218. assert(!Val.isNull() && "Null value with non-end insert iterator.");
  219. if (EltTy V = Val.template dyn_cast<EltTy>()) {
  220. assert(I == begin());
  221. Val = Elt;
  222. push_back(V);
  223. return begin();
  224. }
  225. return Val.template get<VecTy*>()->insert(I, Elt);
  226. }
  227. template<typename ItTy>
  228. iterator insert(iterator I, ItTy From, ItTy To) {
  229. assert(I >= this->begin() && "Insertion iterator is out of bounds.");
  230. assert(I <= this->end() && "Inserting past the end of the vector.");
  231. if (From == To)
  232. return I;
  233. // If we have a single value, convert to a vector.
  234. ptrdiff_t Offset = I - begin();
  235. if (Val.isNull()) {
  236. if (llvm::next(From) == To) {
  237. Val = *From;
  238. return begin();
  239. }
  240. Val = new VecTy();
  241. } else if (EltTy V = Val.template dyn_cast<EltTy>()) {
  242. Val = new VecTy();
  243. Val.template get<VecTy*>()->push_back(V);
  244. }
  245. return Val.template get<VecTy*>()->insert(begin() + Offset, From, To);
  246. }
  247. };
  248. } // end namespace llvm
  249. #endif