1 //===--- ArrayRef.h - Array Reference Wrapper -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_ADT_ARRAYREF_H
11 #define LLVM_ADT_ARRAYREF_H
13 #include "llvm/ADT/SmallVector.h"
18 /// ArrayRef - Represent a constant reference to an array (0 or more elements
19 /// consecutively in memory), i.e. a start pointer and a length. It allows
20 /// various APIs to take consecutive elements easily and conveniently.
22 /// This class does not own the underlying data, it is expected to be used in
23 /// situations where the data resides in some other buffer, whose lifetime
24 /// extends past that of the ArrayRef. For this reason, it is not in general
25 /// safe to store an ArrayRef.
27 /// This is intended to be trivially copyable, so it should be passed by
32 typedef const T *iterator;
33 typedef const T *const_iterator;
34 typedef size_t size_type;
37 /// The start of the array, in an external buffer.
40 /// The number of elements.
44 /// @name Constructors
47 /// Construct an empty ArrayRef.
48 /*implicit*/ ArrayRef() : Data(0), Length(0) {}
50 /// Construct an ArrayRef from a single element.
51 /*implicit*/ ArrayRef(const T &OneElt)
52 : Data(&OneElt), Length(1) {}
54 /// Construct an ArrayRef from a pointer and length.
55 /*implicit*/ ArrayRef(const T *data, size_t length)
56 : Data(data), Length(length) {}
58 /// Construct an ArrayRef from a range.
59 ArrayRef(const T *begin, const T *end)
60 : Data(begin), Length(end - begin) {}
62 /// Construct an ArrayRef from a SmallVector.
63 /*implicit*/ ArrayRef(const SmallVectorImpl<T> &Vec)
64 : Data(Vec.data()), Length(Vec.size()) {}
66 /// Construct an ArrayRef from a std::vector.
67 /*implicit*/ ArrayRef(const std::vector<T> &Vec)
68 : Data(Vec.empty() ? (T*)0 : &Vec[0]), Length(Vec.size()) {}
70 /// Construct an ArrayRef from a C array.
72 /*implicit*/ ArrayRef(const T (&Arr)[N])
73 : Data(Arr), Length(N) {}
76 /// @name Simple Operations
79 iterator begin() const { return Data; }
80 iterator end() const { return Data + Length; }
82 /// empty - Check if the array is empty.
83 bool empty() const { return Length == 0; }
85 const T *data() const { return Data; }
87 /// size - Get the array size.
88 size_t size() const { return Length; }
90 /// front - Get the first element.
91 const T &front() const {
96 /// back - Get the last element.
97 const T &back() const {
99 return Data[Length-1];
102 /// equals - Check for element-wise equality.
103 bool equals(ArrayRef RHS) const {
104 if (Length != RHS.Length)
106 for (size_type i = 0; i != Length; i++)
107 if (Data[i] != RHS.Data[i])
112 /// slice(n) - Chop off the first N elements of the array.
113 ArrayRef<T> slice(unsigned N) const {
114 assert(N <= size() && "Invalid specifier");
115 return ArrayRef<T>(data()+N, size()-N);
118 /// slice(n, m) - Chop off the first N elements of the array, and keep M
119 /// elements in the array.
120 ArrayRef<T> slice(unsigned N, unsigned M) const {
121 assert(N+M <= size() && "Invalid specifier");
122 return ArrayRef<T>(data()+N, M);
126 /// @name Operator Overloads
128 const T &operator[](size_t Index) const {
129 assert(Index < Length && "Invalid index!");
134 /// @name Expensive Operations
136 std::vector<T> vec() const {
137 return std::vector<T>(Data, Data+Length);
141 /// @name Conversion operators
143 operator std::vector<T>() const {
144 return std::vector<T>(Data, Data+Length);
150 /// @name ArrayRef Convenience constructors
153 /// Construct an ArrayRef from a single element.
155 ArrayRef<T> makeArrayRef(const T &OneElt) {
159 /// Construct an ArrayRef from a pointer and length.
161 ArrayRef<T> makeArrayRef(const T *data, size_t length) {
162 return ArrayRef<T>(data, length);
165 /// Construct an ArrayRef from a range.
167 ArrayRef<T> makeArrayRef(const T *begin, const T *end) {
168 return ArrayRef<T>(begin, end);
171 /// Construct an ArrayRef from a SmallVector.
172 template <typename T>
173 ArrayRef<T> makeArrayRef(const SmallVectorImpl<T> &Vec) {
177 /// Construct an ArrayRef from a SmallVector.
178 template <typename T, unsigned N>
179 ArrayRef<T> makeArrayRef(const SmallVector<T, N> &Vec) {
183 /// Construct an ArrayRef from a std::vector.
185 ArrayRef<T> makeArrayRef(const std::vector<T> &Vec) {
189 /// Construct an ArrayRef from a C array.
190 template<typename T, size_t N>
191 ArrayRef<T> makeArrayRef(const T (&Arr)[N]) {
192 return ArrayRef<T>(Arr);
196 /// @name ArrayRef Comparison Operators
200 inline bool operator==(ArrayRef<T> LHS, ArrayRef<T> RHS) {
201 return LHS.equals(RHS);
205 inline bool operator!=(ArrayRef<T> LHS, ArrayRef<T> RHS) {
206 return !(LHS == RHS);
211 // ArrayRefs can be treated like a POD type.
212 template <typename T> struct isPodLike;
213 template <typename T> struct isPodLike<ArrayRef<T> > {
214 static const bool value = true;