fix comments
[oota-llvm.git] / include / llvm / ADT / ArrayRef.h
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
16 namespace llvm {
17   class APInt;
18   
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.
22   ///
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.
27   ///
28   /// This is intended to be trivially copyable, so it should be passed by
29   /// value.
30   template<typename T>
31   class ArrayRef {
32   public:
33     typedef const T *iterator;
34     typedef const T *const_iterator;
35     typedef size_t size_type;
36     
37   private:
38     /// The start of the array, in an external buffer.
39     const T *Data;
40     
41     /// The number of elements.
42     size_t Length;
43     
44   public:
45     /// @name Constructors
46     /// @{
47     
48     /// Construct an empty ArrayRef.
49     /*implicit*/ ArrayRef() : Data(0), Length(0) {}
50     
51     /// Construct an ArrayRef from a single element.
52     /*implicit*/ ArrayRef(const T &OneElt)
53       : Data(&OneElt), Length(1) {}
54     
55     /// Construct an ArrayRef from a pointer and length.
56     /*implicit*/ ArrayRef(const T *data, size_t length)
57       : Data(data), Length(length) {}
58     
59     /// Construct an ArrayRef from a SmallVector.
60     /*implicit*/ ArrayRef(const SmallVectorImpl<T> &Vec)
61       : Data(Vec.data()), Length(Vec.size()) {}
62
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()) {}
66     
67     // TODO: C arrays.
68     
69     /// @}
70     /// @name Simple Operations
71     /// @{
72
73     iterator begin() const { return Data; }
74     iterator end() const { return Data + Length; }
75     
76     /// empty - Check if the array is empty.
77     bool empty() const { return Length == 0; }
78     
79     /// size - Get the array size.
80     size_t size() const { return Length; }
81     
82     /// front - Get the first element.
83     const T &front() const {
84       assert(!empty());
85       return Data[0];
86     }
87     
88     /// back - Get the last element.
89     const T &back() const {
90       assert(!empty());
91       return Data[Length-1];
92     }
93     
94     /// @}
95     /// @name Operator Overloads
96     /// @{
97     
98     const T &operator[](size_t Index) const {
99       assert(Index < Length && "Invalid index!");
100       return Data[Index];
101     }
102     
103     /// @}
104     /// @name Expensive Operations
105     /// @{
106     
107     std::vector<T> vec() const {
108       return std::vector<T>(Data, Data+Length);
109     }
110     
111     /// @}
112   };
113   
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;
118   };
119 }
120
121 #endif