ArrayRef.h revision fced2945995b4fd8f28f7dec9fcb5a6ab2e2798d
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 10#ifndef LLVM_ADT_ARRAYREF_H 11#define LLVM_ADT_ARRAYREF_H 12 13#include "llvm/ADT/SmallVector.h" 14#include <vector> 15 16namespace llvm { 17 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. 21 /// 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. 26 /// 27 /// This is intended to be trivially copyable, so it should be passed by 28 /// value. 29 template<typename T> 30 class ArrayRef { 31 public: 32 typedef const T *iterator; 33 typedef const T *const_iterator; 34 typedef size_t size_type; 35 36 private: 37 /// The start of the array, in an external buffer. 38 const T *Data; 39 40 /// The number of elements. 41 size_type Length; 42 43 public: 44 /// @name Constructors 45 /// @{ 46 47 /// Construct an empty ArrayRef. 48 /*implicit*/ ArrayRef() : Data(0), Length(0) {} 49 50 /// Construct an ArrayRef from a single element. 51 /*implicit*/ ArrayRef(const T &OneElt) 52 : Data(&OneElt), Length(1) {} 53 54 /// Construct an ArrayRef from a pointer and length. 55 /*implicit*/ ArrayRef(const T *data, size_t length) 56 : Data(data), Length(length) {} 57 58 /// Construct an ArrayRef from a range. 59 ArrayRef(const T *begin, const T *end) 60 : Data(begin), Length(end - begin) {} 61 62 /// Construct an ArrayRef from a SmallVector. 63 /*implicit*/ ArrayRef(const SmallVectorImpl<T> &Vec) 64 : Data(Vec.data()), Length(Vec.size()) {} 65 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()) {} 69 70 /// Construct an ArrayRef from a C array. 71 template <size_t N> 72 /*implicit*/ ArrayRef(const T (&Arr)[N]) 73 : Data(Arr), Length(N) {} 74 75 /// @} 76 /// @name Simple Operations 77 /// @{ 78 79 iterator begin() const { return Data; } 80 iterator end() const { return Data + Length; } 81 82 /// empty - Check if the array is empty. 83 bool empty() const { return Length == 0; } 84 85 const T *data() const { return Data; } 86 87 /// size - Get the array size. 88 size_t size() const { return Length; } 89 90 /// front - Get the first element. 91 const T &front() const { 92 assert(!empty()); 93 return Data[0]; 94 } 95 96 /// back - Get the last element. 97 const T &back() const { 98 assert(!empty()); 99 return Data[Length-1]; 100 } 101 102 /// equals - Check for element-wise equality. 103 bool equals(ArrayRef RHS) const { 104 if (Length != RHS.Length) 105 return false; 106 for (size_type i = 0; i != Length; i++) 107 if (Data[i] != RHS.Data[i]) 108 return false; 109 return true; 110 } 111 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); 116 } 117 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); 123 } 124 125 /// @} 126 /// @name Operator Overloads 127 /// @{ 128 const T &operator[](size_t Index) const { 129 assert(Index < Length && "Invalid index!"); 130 return Data[Index]; 131 } 132 133 /// @} 134 /// @name Expensive Operations 135 /// @{ 136 std::vector<T> vec() const { 137 return std::vector<T>(Data, Data+Length); 138 } 139 140 /// @} 141 /// @name Conversion operators 142 /// @{ 143 operator std::vector<T>() const { 144 return std::vector<T>(Data, Data+Length); 145 } 146 147 /// @} 148 }; 149 150 /// @name ArrayRef Convenience constructors 151 /// @{ 152 153 /// Construct an ArrayRef from a single element. 154 template<typename T> 155 ArrayRef<T> makeArrayRef(const T &OneElt) { 156 return OneElt; 157 } 158 159 /// Construct an ArrayRef from a pointer and length. 160 template<typename T> 161 ArrayRef<T> makeArrayRef(const T *data, size_t length) { 162 return ArrayRef<T>(data, length); 163 } 164 165 /// Construct an ArrayRef from a range. 166 template<typename T> 167 ArrayRef<T> makeArrayRef(const T *begin, const T *end) { 168 return ArrayRef<T>(begin, end); 169 } 170 171 /// Construct an ArrayRef from a SmallVector. 172 template <typename T> 173 ArrayRef<T> makeArrayRef(const SmallVectorImpl<T> &Vec) { 174 return Vec; 175 } 176 177 /// Construct an ArrayRef from a SmallVector. 178 template <typename T, unsigned N> 179 ArrayRef<T> makeArrayRef(const SmallVector<T, N> &Vec) { 180 return Vec; 181 } 182 183 /// Construct an ArrayRef from a std::vector. 184 template<typename T> 185 ArrayRef<T> makeArrayRef(const std::vector<T> &Vec) { 186 return Vec; 187 } 188 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); 193 } 194 195 /// @} 196 /// @name ArrayRef Comparison Operators 197 /// @{ 198 199 template<typename T> 200 inline bool operator==(ArrayRef<T> LHS, ArrayRef<T> RHS) { 201 return LHS.equals(RHS); 202 } 203 204 template<typename T> 205 inline bool operator!=(ArrayRef<T> LHS, ArrayRef<T> RHS) { 206 return !(LHS == RHS); 207 } 208 209 /// @} 210 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; 215 }; 216} 217 218#endif 219