blob: 1a7f2e8c02e9ae5e10677fdbf88b74a97024d9f9 [file] [log] [blame]
Vladimir Marko089142c2014-06-05 10:57:05 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_UTILS_ARRAY_REF_H_
18#define ART_COMPILER_UTILS_ARRAY_REF_H_
19
20#include <type_traits>
21#include <vector>
22
23#include "base/logging.h"
24
25namespace art {
26
Vladimir Marko089142c2014-06-05 10:57:05 +010027/**
28 * @brief A container that references an array.
29 *
30 * @details The template class ArrayRef provides a container that references
31 * an external array. This external array must remain alive while the ArrayRef
32 * object is in use. The external array may be a std::vector<>-backed storage
33 * or any other contiguous chunk of memory but that memory must remain valid,
34 * i.e. the std::vector<> must not be resized for example.
35 *
36 * Except for copy/assign and insert/erase/capacity functions, the interface
37 * is essentially the same as std::vector<>. Since we don't want to throw
38 * exceptions, at() is also excluded.
39 */
40template <typename T>
41class ArrayRef {
Vladimir Marko01106192014-06-05 16:35:31 +010042 private:
43 struct tag { };
44
Vladimir Marko089142c2014-06-05 10:57:05 +010045 public:
46 typedef T value_type;
47 typedef T& reference;
48 typedef const T& const_reference;
49 typedef T* pointer;
50 typedef const T* const_pointer;
51 typedef T* iterator;
52 typedef const T* const_iterator;
53 typedef std::reverse_iterator<iterator> reverse_iterator;
54 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
55 typedef ptrdiff_t difference_type;
56 typedef size_t size_type;
57
58 // Constructors.
59
60 constexpr ArrayRef()
61 : array_(nullptr), size_(0u) {
62 }
63
64 template <size_t size>
65 constexpr ArrayRef(T (&array)[size])
66 : array_(array), size_(size) {
67 }
68
69 template <typename U, size_t size>
70 constexpr ArrayRef(U (&array)[size],
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070071 typename std::enable_if<std::is_same<T, const U>::value, tag>::type
72 t ATTRIBUTE_UNUSED = tag())
Vladimir Marko089142c2014-06-05 10:57:05 +010073 : array_(array), size_(size) {
74 }
75
Andreas Gampe277ccbd2014-11-03 21:36:10 -080076 constexpr ArrayRef(T* array_in, size_t size_in)
77 : array_(array_in), size_(size_in) {
Vladimir Marko089142c2014-06-05 10:57:05 +010078 }
79
Vladimir Markof4da6752014-08-01 19:04:18 +010080 template <typename Alloc>
81 explicit ArrayRef(std::vector<T, Alloc>& v)
Vladimir Marko089142c2014-06-05 10:57:05 +010082 : array_(v.data()), size_(v.size()) {
83 }
84
Vladimir Markof4da6752014-08-01 19:04:18 +010085 template <typename U, typename Alloc>
86 ArrayRef(const std::vector<U, Alloc>& v,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070087 typename std::enable_if<std::is_same<T, const U>::value, tag>::tag
88 t ATTRIBUTE_UNUSED = tag())
Vladimir Marko089142c2014-06-05 10:57:05 +010089 : array_(v.data()), size_(v.size()) {
90 }
91
92 // Assignment operators.
93
94 ArrayRef& operator=(const ArrayRef& other) {
95 array_ = other.array_;
96 size_ = other.size_;
97 return *this;
98 }
99
100 template <typename U>
Vladimir Marko01106192014-06-05 16:35:31 +0100101 typename std::enable_if<std::is_same<T, const U>::value, ArrayRef>::type&
Vladimir Marko089142c2014-06-05 10:57:05 +0100102 operator=(const ArrayRef<U>& other) {
103 return *this = ArrayRef(other);
104 }
105
106 // Destructor.
107 ~ArrayRef() = default;
108
109 // Iterators.
110 iterator begin() { return array_; }
111 const_iterator begin() const { return array_; }
112 const_iterator cbegin() const { return array_; }
113 iterator end() { return array_ + size_; }
114 const_iterator end() const { return array_ + size_; }
115 const_iterator cend() const { return array_ + size_; }
116 reverse_iterator rbegin() { return reverse_iterator(end()); }
117 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
118 const_reverse_iterator crbegin() const { return const_reverse_iterator(cend()); }
119 reverse_iterator rend() { return reverse_iterator(begin()); }
120 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
121 const_reverse_iterator crend() const { return const_reverse_iterator(cbegin()); }
122
123 // Size.
124 size_type size() const { return size_; }
125 bool empty() const { return size() == 0u; }
126
127 // Element access. NOTE: Not providing at().
128
129 reference operator[](size_type n) {
130 DCHECK_LT(n, size_);
131 return array_[n];
132 }
133
134 const_reference operator[](size_type n) const {
135 DCHECK_LT(n, size_);
136 return array_[n];
137 }
138
139 reference front() {
140 DCHECK_NE(size_, 0u);
141 return array_[0];
142 }
143
144 const_reference front() const {
145 DCHECK_NE(size_, 0u);
146 return array_[0];
147 }
148
149 reference back() {
150 DCHECK_NE(size_, 0u);
151 return array_[size_ - 1u];
152 }
153
154 const_reference back() const {
155 DCHECK_NE(size_, 0u);
156 return array_[size_ - 1u];
157 }
158
159 value_type* data() { return array_; }
160 const value_type* data() const { return array_; }
161
162 private:
Vladimir Marko089142c2014-06-05 10:57:05 +0100163 T* array_;
164 size_t size_;
165};
166
Vladimir Markof4da6752014-08-01 19:04:18 +0100167template <typename T>
168bool operator==(const ArrayRef<T>& lhs, const ArrayRef<T>& rhs) {
169 return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());
170}
171
172template <typename T>
173bool operator!=(const ArrayRef<T>& lhs, const ArrayRef<T>& rhs) {
174 return !(lhs == rhs);
175}
176
Vladimir Marko089142c2014-06-05 10:57:05 +0100177} // namespace art
178
179
180#endif // ART_COMPILER_UTILS_ARRAY_REF_H_