ArrayRef.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. //===--- ArrayRef.h - Array Reference Wrapper -------------------*- 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_ARRAYREF_H
  10. #define LLVM_ADT_ARRAYREF_H
  11. #include "llvm/ADT/None.h"
  12. #include "llvm/ADT/SmallVector.h"
  13. #include <vector>
  14. namespace llvm {
  15. /// ArrayRef - Represent a constant reference to an array (0 or more elements
  16. /// consecutively in memory), i.e. a start pointer and a length. It allows
  17. /// various APIs to take consecutive elements easily and conveniently.
  18. ///
  19. /// This class does not own the underlying data, it is expected to be used in
  20. /// situations where the data resides in some other buffer, whose lifetime
  21. /// extends past that of the ArrayRef. For this reason, it is not in general
  22. /// safe to store an ArrayRef.
  23. ///
  24. /// This is intended to be trivially copyable, so it should be passed by
  25. /// value.
  26. template<typename T>
  27. class ArrayRef {
  28. public:
  29. typedef const T *iterator;
  30. typedef const T *const_iterator;
  31. typedef size_t size_type;
  32. typedef std::reverse_iterator<iterator> reverse_iterator;
  33. private:
  34. /// The start of the array, in an external buffer.
  35. const T *Data;
  36. /// The number of elements.
  37. size_type Length;
  38. public:
  39. /// @name Constructors
  40. /// @{
  41. /// Construct an empty ArrayRef.
  42. /*implicit*/ ArrayRef() : Data(nullptr), Length(0) {}
  43. /// Construct an empty ArrayRef from None.
  44. /*implicit*/ ArrayRef(NoneType) : Data(nullptr), Length(0) {}
  45. /// Construct an ArrayRef from a single element.
  46. /*implicit*/ ArrayRef(const T &OneElt)
  47. : Data(&OneElt), Length(1) {}
  48. /// Construct an ArrayRef from a pointer and length.
  49. /*implicit*/ ArrayRef(const T *data, size_t length)
  50. : Data(data), Length(length) {}
  51. /// Construct an ArrayRef from a range.
  52. ArrayRef(const T *begin, const T *end)
  53. : Data(begin), Length(end - begin) {}
  54. /// Construct an ArrayRef from a SmallVector. This is templated in order to
  55. /// avoid instantiating SmallVectorTemplateCommon<T> whenever we
  56. /// copy-construct an ArrayRef.
  57. template<typename U>
  58. /*implicit*/ ArrayRef(const SmallVectorTemplateCommon<T, U> &Vec)
  59. : Data(Vec.data()), Length(Vec.size()) {
  60. }
  61. /// Construct an ArrayRef from a std::vector.
  62. template<typename A>
  63. /*implicit*/ ArrayRef(const std::vector<T, A> &Vec)
  64. : Data(Vec.data()), Length(Vec.size()) {}
  65. /// Construct an ArrayRef from a C array.
  66. template <size_t N>
  67. /*implicit*/ LLVM_CONSTEXPR ArrayRef(const T (&Arr)[N])
  68. : Data(Arr), Length(N) {}
  69. /// Construct an ArrayRef from a std::initializer_list.
  70. /*implicit*/ ArrayRef(const std::initializer_list<T> &Vec)
  71. : Data(Vec.begin() == Vec.end() ? (T*)0 : Vec.begin()),
  72. Length(Vec.size()) {}
  73. /// Construct an ArrayRef<const T*> from ArrayRef<T*>. This uses SFINAE to
  74. /// ensure that only ArrayRefs of pointers can be converted.
  75. template <typename U>
  76. ArrayRef(const ArrayRef<U *> &A,
  77. typename std::enable_if<
  78. std::is_convertible<U *const *, T const *>::value>::type* = 0)
  79. : Data(A.data()), Length(A.size()) {}
  80. /// Construct an ArrayRef<const T*> from a SmallVector<T*>. This is
  81. /// templated in order to avoid instantiating SmallVectorTemplateCommon<T>
  82. /// whenever we copy-construct an ArrayRef.
  83. template<typename U, typename DummyT>
  84. /*implicit*/ ArrayRef(const SmallVectorTemplateCommon<U*, DummyT> &Vec,
  85. typename std::enable_if<
  86. std::is_convertible<U *const *,
  87. T const *>::value>::type* = 0)
  88. : Data(Vec.data()), Length(Vec.size()) {
  89. }
  90. /// Construct an ArrayRef<const T*> from std::vector<T*>. This uses SFINAE
  91. /// to ensure that only vectors of pointers can be converted.
  92. template<typename U, typename A>
  93. ArrayRef(const std::vector<U *, A> &Vec,
  94. typename std::enable_if<
  95. std::is_convertible<U *const *, T const *>::value>::type* = 0)
  96. : Data(Vec.data()), Length(Vec.size()) {}
  97. /// @}
  98. /// @name Simple Operations
  99. /// @{
  100. iterator begin() const { return Data; }
  101. iterator end() const { return Data + Length; }
  102. reverse_iterator rbegin() const { return reverse_iterator(end()); }
  103. reverse_iterator rend() const { return reverse_iterator(begin()); }
  104. /// empty - Check if the array is empty.
  105. bool empty() const { return Length == 0; }
  106. const T *data() const { return Data; }
  107. /// size - Get the array size.
  108. size_t size() const { return Length; }
  109. /// front - Get the first element.
  110. const T &front() const {
  111. assert(!empty());
  112. return Data[0];
  113. }
  114. /// back - Get the last element.
  115. const T &back() const {
  116. assert(!empty());
  117. return Data[Length-1];
  118. }
  119. // copy - Allocate copy in Allocator and return ArrayRef<T> to it.
  120. template <typename Allocator> ArrayRef<T> copy(Allocator &A) {
  121. T *Buff = A.template Allocate<T>(Length);
  122. std::copy(begin(), end(), Buff);
  123. return ArrayRef<T>(Buff, Length);
  124. }
  125. /// equals - Check for element-wise equality.
  126. bool equals(ArrayRef RHS) const {
  127. if (Length != RHS.Length)
  128. return false;
  129. if (Length == 0)
  130. return true;
  131. return std::equal(begin(), end(), RHS.begin());
  132. }
  133. /// slice(n) - Chop off the first N elements of the array.
  134. ArrayRef<T> slice(unsigned N) const {
  135. assert(N <= size() && "Invalid specifier");
  136. return ArrayRef<T>(data()+N, size()-N);
  137. }
  138. /// slice(n, m) - Chop off the first N elements of the array, and keep M
  139. /// elements in the array.
  140. ArrayRef<T> slice(unsigned N, unsigned M) const {
  141. assert(N+M <= size() && "Invalid specifier");
  142. return ArrayRef<T>(data()+N, M);
  143. }
  144. // \brief Drop the last \p N elements of the array.
  145. ArrayRef<T> drop_back(unsigned N = 1) const {
  146. assert(size() >= N && "Dropping more elements than exist");
  147. return slice(0, size() - N);
  148. }
  149. /// @}
  150. /// @name Operator Overloads
  151. /// @{
  152. const T &operator[](size_t Index) const {
  153. assert(Index < Length && "Invalid index!");
  154. return Data[Index];
  155. }
  156. /// @}
  157. /// @name Expensive Operations
  158. /// @{
  159. std::vector<T> vec() const {
  160. return std::vector<T>(Data, Data+Length);
  161. }
  162. /// @}
  163. /// @name Conversion operators
  164. /// @{
  165. operator std::vector<T>() const {
  166. return std::vector<T>(Data, Data+Length);
  167. }
  168. /// @}
  169. };
  170. /// MutableArrayRef - Represent a mutable reference to an array (0 or more
  171. /// elements consecutively in memory), i.e. a start pointer and a length. It
  172. /// allows various APIs to take and modify consecutive elements easily and
  173. /// conveniently.
  174. ///
  175. /// This class does not own the underlying data, it is expected to be used in
  176. /// situations where the data resides in some other buffer, whose lifetime
  177. /// extends past that of the MutableArrayRef. For this reason, it is not in
  178. /// general safe to store a MutableArrayRef.
  179. ///
  180. /// This is intended to be trivially copyable, so it should be passed by
  181. /// value.
  182. template<typename T>
  183. class MutableArrayRef : public ArrayRef<T> {
  184. public:
  185. typedef T *iterator;
  186. typedef std::reverse_iterator<iterator> reverse_iterator;
  187. /// Construct an empty MutableArrayRef.
  188. /*implicit*/ MutableArrayRef() : ArrayRef<T>() {}
  189. /// Construct an empty MutableArrayRef from None.
  190. /*implicit*/ MutableArrayRef(NoneType) : ArrayRef<T>() {}
  191. /// Construct an MutableArrayRef from a single element.
  192. /*implicit*/ MutableArrayRef(T &OneElt) : ArrayRef<T>(OneElt) {}
  193. /// Construct an MutableArrayRef from a pointer and length.
  194. /*implicit*/ MutableArrayRef(T *data, size_t length)
  195. : ArrayRef<T>(data, length) {}
  196. /// Construct an MutableArrayRef from a range.
  197. MutableArrayRef(T *begin, T *end) : ArrayRef<T>(begin, end) {}
  198. /// Construct an MutableArrayRef from a SmallVector.
  199. /*implicit*/ MutableArrayRef(SmallVectorImpl<T> &Vec)
  200. : ArrayRef<T>(Vec) {}
  201. /// Construct a MutableArrayRef from a std::vector.
  202. /*implicit*/ MutableArrayRef(std::vector<T> &Vec)
  203. : ArrayRef<T>(Vec) {}
  204. /// Construct an MutableArrayRef from a C array.
  205. template <size_t N>
  206. /*implicit*/ LLVM_CONSTEXPR MutableArrayRef(T (&Arr)[N])
  207. : ArrayRef<T>(Arr) {}
  208. T *data() const { return const_cast<T*>(ArrayRef<T>::data()); }
  209. iterator begin() const { return data(); }
  210. iterator end() const { return data() + this->size(); }
  211. reverse_iterator rbegin() const { return reverse_iterator(end()); }
  212. reverse_iterator rend() const { return reverse_iterator(begin()); }
  213. /// front - Get the first element.
  214. T &front() const {
  215. assert(!this->empty());
  216. return data()[0];
  217. }
  218. /// back - Get the last element.
  219. T &back() const {
  220. assert(!this->empty());
  221. return data()[this->size()-1];
  222. }
  223. /// slice(n) - Chop off the first N elements of the array.
  224. MutableArrayRef<T> slice(unsigned N) const {
  225. assert(N <= this->size() && "Invalid specifier");
  226. return MutableArrayRef<T>(data()+N, this->size()-N);
  227. }
  228. /// slice(n, m) - Chop off the first N elements of the array, and keep M
  229. /// elements in the array.
  230. MutableArrayRef<T> slice(unsigned N, unsigned M) const {
  231. assert(N+M <= this->size() && "Invalid specifier");
  232. return MutableArrayRef<T>(data()+N, M);
  233. }
  234. MutableArrayRef<T> drop_back(unsigned N) const {
  235. assert(this->size() >= N && "Dropping more elements than exist");
  236. return slice(0, this->size() - N);
  237. }
  238. /// @}
  239. /// @name Operator Overloads
  240. /// @{
  241. T &operator[](size_t Index) const {
  242. assert(Index < this->size() && "Invalid index!");
  243. return data()[Index];
  244. }
  245. };
  246. /// @name ArrayRef Convenience constructors
  247. /// @{
  248. /// Construct an ArrayRef from a single element.
  249. template<typename T>
  250. ArrayRef<T> makeArrayRef(const T &OneElt) {
  251. return OneElt;
  252. }
  253. /// Construct an ArrayRef from a pointer and length.
  254. template<typename T>
  255. ArrayRef<T> makeArrayRef(const T *data, size_t length) {
  256. return ArrayRef<T>(data, length);
  257. }
  258. /// Construct an ArrayRef from a range.
  259. template<typename T>
  260. ArrayRef<T> makeArrayRef(const T *begin, const T *end) {
  261. return ArrayRef<T>(begin, end);
  262. }
  263. /// Construct an ArrayRef from a SmallVector.
  264. template <typename T>
  265. ArrayRef<T> makeArrayRef(const SmallVectorImpl<T> &Vec) {
  266. return Vec;
  267. }
  268. /// Construct an ArrayRef from a SmallVector.
  269. template <typename T, unsigned N>
  270. ArrayRef<T> makeArrayRef(const SmallVector<T, N> &Vec) {
  271. return Vec;
  272. }
  273. /// Construct an ArrayRef from a std::vector.
  274. template<typename T>
  275. ArrayRef<T> makeArrayRef(const std::vector<T> &Vec) {
  276. return Vec;
  277. }
  278. /// Construct an ArrayRef from a C array.
  279. template<typename T, size_t N>
  280. ArrayRef<T> makeArrayRef(const T (&Arr)[N]) {
  281. return ArrayRef<T>(Arr);
  282. }
  283. /// @}
  284. /// @name ArrayRef Comparison Operators
  285. /// @{
  286. template<typename T>
  287. inline bool operator==(ArrayRef<T> LHS, ArrayRef<T> RHS) {
  288. return LHS.equals(RHS);
  289. }
  290. template<typename T>
  291. inline bool operator!=(ArrayRef<T> LHS, ArrayRef<T> RHS) {
  292. return !(LHS == RHS);
  293. }
  294. /// @}
  295. // ArrayRefs can be treated like a POD type.
  296. template <typename T> struct isPodLike;
  297. template <typename T> struct isPodLike<ArrayRef<T> > {
  298. static const bool value = true;
  299. };
  300. }
  301. #endif