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"
19 /// ArrayRef - Represent a constant reference to an array (0 or more elements
20 /// consecutively in memory), i.e. a start pointer and a length. It allows
21 /// various APIs to take consecutive elements easily and conveniently.
23 /// This class does not own the underlying data, it is expected to be used in
24 /// situations where the data resides in some other buffer, whose lifetime
25 /// extends past that of the StringRef. For this reason, it is not in general
26 /// safe to store a ArrayRef.
28 /// This is intended to be trivially copyable, so it should be passed by
33 typedef const T *iterator;
34 typedef const T *const_iterator;
35 typedef size_t size_type;
38 /// The start of the array, in an external buffer.
41 /// The number of elements.
45 /// @name Constructors
48 /// Construct an empty ArrayRef.
49 /*implicit*/ ArrayRef() : Data(0), Length(0) {}
51 /// Construct an ArrayRef from a single element.
52 /*implicit*/ ArrayRef(const T &OneElt)
53 : Data(&OneElt), Length(1) {}
55 /// Construct an ArrayRef from a pointer and length.
56 /*implicit*/ ArrayRef(const T *data, size_t length)
57 : Data(data), Length(length) {}
59 /// Construct an ArrayRef from a SmallVector.
60 /*implicit*/ ArrayRef(const SmallVectorImpl<T> &Vec)
61 : Data(Vec.data()), Length(Vec.size()) {}
63 /// Construct an ArrayRef from a std::vector.
64 /*implicit*/ ArrayRef(const std::vector<T> &Vec)
65 : Data(Vec.empty() ? (T*)0 : &Vec[0]), Length(Vec.size()) {}
70 /// @name Simple Operations
73 iterator begin() const { return Data; }
74 iterator end() const { return Data + Length; }
76 /// empty - Check if the array is empty.
77 bool empty() const { return Length == 0; }
79 /// size - Get the array size.
80 size_t size() const { return Length; }
82 /// front - Get the first element.
83 const T &front() const {
88 /// back - Get the last element.
89 const T &back() const {
91 return Data[Length-1];
95 /// @name Operator Overloads
98 const T &operator[](size_t Index) const {
99 assert(Index < Length && "Invalid index!");
104 /// @name Expensive Operations
107 std::vector<T> vec() const {
108 return std::vector<T>(Data, Data+Length);
114 // ArrayRefs can be treated like a POD type.
115 template <typename T> struct isPodLike;
116 template <typename T> struct isPodLike<ArrayRef<T> > {
117 static const bool value = true;