PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/tensorflow/core/lib/gtl/array_slice.h

https://gitlab.com/hrishikeshvganu/tensorflow
C Header | 314 lines | 134 code | 42 blank | 138 comment | 6 complexity | 9462c7b3b68b2241e5bf11c70b2de5e8 MD5 | raw file
  1. /* Copyright 2015 Google Inc. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. // An ArraySlice<T> represents an immutable array of elements of type
  13. // T. It has a length "length", and a base pointer "ptr", and the
  14. // array it represents contains the elements "ptr[0] .. ptr[len-1]".
  15. // The backing store for the array is *not* owned by the ArraySlice
  16. // object, and clients must arrange for the backing store to remain
  17. // live while the ArraySlice object is in use.
  18. //
  19. // An ArraySlice<T> is somewhat analogous to a StringPiece, but for
  20. // array elements of type T.
  21. //
  22. // Implicit conversion operations are provided from types such as
  23. // std::vector<T> and util::gtl::InlinedVector<T, N>. Note that ArraySlice
  24. // objects constructed from types in this way may be invalidated by
  25. // any operations that mutate the underlying vector.
  26. //
  27. // One common use for ArraySlice is when passing arguments to a
  28. // routine where you want to be able to accept a variety of array
  29. // types (e.g. a vector, a util::gtl::InlinedVector, a C-style array,
  30. // etc.). The usual approach here is to have the client explicitly
  31. // pass in a pointer and a length, as in:
  32. //
  33. // void MyRoutine(const int* elems, int N) {
  34. // for (int i = 0; i < N; i++) { .. do something with elems[i] .. }
  35. // }
  36. //
  37. // Unfortunately, this leads to ugly and error-prone code at the call site:
  38. //
  39. // std::vector<int> my_vector;
  40. // MyRoutine(vector_as_array(&my_vector), my_vector.size());
  41. //
  42. // util::gtl::InlinedVector<int, 4> my_inline_vector;
  43. // MyRoutine(my_inline_vector.array(), my_inline_vector.size());
  44. //
  45. // int my_array[10];
  46. // MyRoutine(my_array, 10);
  47. //
  48. // Instead, you can use an ArraySlice as the argument to the routine:
  49. //
  50. // void MyRoutine(ArraySlice<int> a) {
  51. // for (int i = 0; i < a.size(); i++) { .. do something with a[i] .. }
  52. // }
  53. //
  54. // This makes the call sites cleaner, for the most part:
  55. //
  56. // std::vector<int> my_vector;
  57. // MyRoutine(my_vector);
  58. //
  59. // util::gtl::InlinedVector<int, 4> my_inline_vector;
  60. // MyRoutine(my_inline_vector);
  61. //
  62. // int my_array[10];
  63. // MyRoutine(my_array);
  64. //
  65. // int* my_array = new int[10];
  66. // MyRoutine(gtl::ArraySlice<int>(my_array, 10));
  67. //
  68. // MutableArraySlice<T> represents a mutable array of elements, and, like
  69. // ArraySlice, does not own the backing store. The implicit constructors it
  70. // provides allow functions not to worry about whether their mutable arguments
  71. // refer to vectors, arrays, proto2::RepeatedFields, etc.:
  72. //
  73. // void MyMutatingRoutine(MutableArraySlice<int> a) {
  74. // for (int i = 0; i < a.size(); i++) { .. mutate a[i] .. }
  75. // }
  76. //
  77. // std::vector<int> my_vector;
  78. // MyMutatingRoutine(&my_vector);
  79. //
  80. // int my_array[10];
  81. // MyMutatingRoutine(my_array);
  82. //
  83. // int* my_array = new int[10];
  84. // MyMutatingRoutine(gtl::MutableArraySlice<int>(my_array, 10));
  85. //
  86. // MyProto my_proto;
  87. // for (int i = 0; i < 10; ++i) { my_proto.add_value(i); }
  88. // MyMutatingRoutine(my_proto.mutable_value());
  89. #ifndef TENSORFLOW_LIB_GTL_ARRAY_SLICE_H_
  90. #define TENSORFLOW_LIB_GTL_ARRAY_SLICE_H_
  91. #include <initializer_list>
  92. #include <type_traits>
  93. #include <vector>
  94. #include "tensorflow/core/lib/gtl/array_slice_internal.h"
  95. #include "tensorflow/core/lib/gtl/inlined_vector.h"
  96. namespace tensorflow {
  97. namespace gtl {
  98. template <typename T>
  99. class ArraySlice {
  100. private:
  101. typedef array_slice_internal::ArraySliceImpl<T> Impl;
  102. public:
  103. typedef T value_type;
  104. typedef typename Impl::pointer pointer;
  105. typedef typename Impl::const_pointer const_pointer;
  106. typedef typename Impl::reference reference;
  107. typedef typename Impl::const_reference const_reference;
  108. typedef typename Impl::iterator iterator;
  109. typedef typename Impl::const_iterator const_iterator;
  110. typedef typename Impl::reverse_iterator reverse_iterator;
  111. typedef typename Impl::const_reverse_iterator const_reverse_iterator;
  112. typedef typename Impl::size_type size_type;
  113. typedef typename Impl::difference_type difference_type;
  114. static const size_type npos = Impl::npos;
  115. ArraySlice() : impl_(nullptr, 0) {}
  116. ArraySlice(const_pointer array, size_type length) : impl_(array, length) {}
  117. // Implicit conversion constructors
  118. ArraySlice(const std::vector<value_type>& v) // NOLINT(runtime/explicit)
  119. : impl_(v.data(), v.size()) {}
  120. template <size_t N>
  121. ArraySlice(const value_type (&a)[N]) // NOLINT(runtime/explicit)
  122. : impl_(a, N) {}
  123. template <int N>
  124. ArraySlice(const InlinedVector<value_type, N>& v) // NOLINT(runtime/explicit)
  125. : impl_(v.data(), v.size()) {}
  126. // The constructor for any class supplying 'data() const' that returns either
  127. // const T* or a less const-qualified version of it, and 'some_integral_type
  128. // size() const'. proto2::RepeatedField<T>, string and (since C++11)
  129. // std::vector<T,A> and std::array<T, N> are examples of this. See
  130. // array_slice_internal.h for details.
  131. template <typename V,
  132. typename = typename Impl::template EnableIfConvertibleFrom<V>>
  133. ArraySlice(const V& v) // NOLINT(runtime/explicit)
  134. : impl_(v) {}
  135. // Implicitly constructs an ArraySlice from an initializer list. This makes it
  136. // possible to pass a brace-enclosed initializer list to a function expecting
  137. // an ArraySlice:
  138. // void Process(ArraySlice<int> x);
  139. // Process({1, 2, 3});
  140. // The data referenced by the initializer_list must outlive this
  141. // ArraySlice. For example, "ArraySlice<int> s={1,2};" and "return
  142. // ArraySlice<int>({3,4});" are errors, as the resulting ArraySlice may
  143. // reference data that is no longer valid.
  144. ArraySlice(std::initializer_list<value_type> v) // NOLINT(runtime/explicit)
  145. : impl_(v.begin(), v.size()) {}
  146. // Substring of another ArraySlice.
  147. // pos must be non-negative and <= x.length().
  148. // len must be non-negative and will be pinned to at most x.length() - pos.
  149. // If len==npos, the substring continues till the end of x.
  150. ArraySlice(const ArraySlice& x, size_type pos, size_type len)
  151. : impl_(x.impl_, pos, len) {}
  152. const_pointer data() const { return impl_.data(); }
  153. size_type size() const { return impl_.size(); }
  154. size_type length() const { return size(); }
  155. bool empty() const { return size() == 0; }
  156. void clear() { impl_.clear(); }
  157. const_reference operator[](size_type i) const { return impl_[i]; }
  158. const_reference at(size_type i) const { return impl_.at(i); }
  159. const_reference front() const { return impl_.front(); }
  160. const_reference back() const { return impl_.back(); }
  161. const_iterator begin() const { return impl_.begin(); }
  162. const_iterator end() const { return impl_.end(); }
  163. const_reverse_iterator rbegin() const { return impl_.rbegin(); }
  164. const_reverse_iterator rend() const { return impl_.rend(); }
  165. void remove_prefix(size_type n) { impl_.remove_prefix(n); }
  166. void remove_suffix(size_type n) { impl_.remove_suffix(n); }
  167. void pop_back() { remove_suffix(1); }
  168. void pop_front() { remove_prefix(1); }
  169. // These relational operators have the same semantics as the
  170. // std::vector<T> relational operators: they do deep (elementwise)
  171. // comparisons. Array slices are equal iff their size is the same
  172. // and all their elements are equal.
  173. bool operator==(ArraySlice<T> other) const { return impl_ == other.impl_; }
  174. bool operator!=(ArraySlice<T> other) const { return impl_ != other.impl_; }
  175. private:
  176. Impl impl_;
  177. };
  178. // Mutable version of ArraySlice, which allows the clients to mutate the
  179. // underlying data. It is implicitly convertible to ArraySlice since it provides
  180. // the data() and size() methods with correct signatures. When a
  181. // MutableArraySlice is created from a pointer to a container (as opposed to raw
  182. // memory pointer), the pointer must not be null.
  183. //
  184. // A note on const-ness: "mutable" here refers to the mutability of the
  185. // underlying data, not of the slice itself. It is perfectly reasonable to have
  186. // a variable of type "const MutableArraySlice<T>"; this means that the bounds
  187. // of the view on the array cannot be changed, but the underlying data in the
  188. // array still may be modified. This is akin to a "T* const" pointer, as opposed
  189. // to a "const T*" pointer (corresponding to a non-const ArraySlice<T>).
  190. template <typename T>
  191. class MutableArraySlice {
  192. private:
  193. typedef array_slice_internal::MutableArraySliceImpl<T> Impl;
  194. public:
  195. typedef T value_type;
  196. typedef typename Impl::pointer pointer;
  197. typedef typename Impl::const_pointer const_pointer;
  198. typedef typename Impl::reference reference;
  199. typedef typename Impl::const_reference const_reference;
  200. typedef typename Impl::iterator iterator;
  201. typedef typename Impl::const_iterator const_iterator;
  202. typedef typename Impl::reverse_iterator reverse_iterator;
  203. typedef typename Impl::const_reverse_iterator const_reverse_iterator;
  204. typedef typename Impl::size_type size_type;
  205. typedef typename Impl::difference_type difference_type;
  206. static const size_type npos = Impl::npos;
  207. MutableArraySlice() : impl_(nullptr, 0) {}
  208. MutableArraySlice(pointer array, size_type length) : impl_(array, length) {}
  209. // Implicit conversion constructors
  210. MutableArraySlice(std::vector<value_type>* v) // NOLINT(runtime/explicit)
  211. : impl_(v->data(), v->size()) {}
  212. template <size_t N>
  213. MutableArraySlice(value_type (&a)[N]) // NOLINT(runtime/explicit)
  214. : impl_(a, N) {}
  215. template <int N>
  216. MutableArraySlice(
  217. InlinedVector<value_type, N>* v) // NOLINT(runtime/explicit)
  218. : impl_(v->data(), v->size()) {}
  219. // The constructor for any class supplying 'T* data()' or 'T* mutable_data()'
  220. // (the former is called if both exist), and 'some_integral_type size()
  221. // const'. proto2::RepeatedField is an example of this. Also supports string
  222. // arguments, when T==char. The appropriate ctor is selected using SFINAE. See
  223. // array_slice_internal.h for details.
  224. template <typename V,
  225. typename = typename Impl::template EnableIfConvertibleFrom<V>>
  226. MutableArraySlice(V* v) // NOLINT(runtime/explicit)
  227. : impl_(v) {}
  228. // Substring of another MutableArraySlice.
  229. // pos must be non-negative and <= x.length().
  230. // len must be non-negative and will be pinned to at most x.length() - pos.
  231. // If len==npos, the substring continues till the end of x.
  232. MutableArraySlice(const MutableArraySlice& x, size_type pos, size_type len)
  233. : impl_(x.impl_, pos, len) {}
  234. // Accessors.
  235. pointer data() const { return impl_.data(); }
  236. size_type size() const { return impl_.size(); }
  237. size_type length() const { return size(); }
  238. bool empty() const { return size() == 0; }
  239. void clear() { impl_.clear(); }
  240. reference operator[](size_type i) const { return impl_[i]; }
  241. reference at(size_type i) const { return impl_.at(i); }
  242. reference front() const { return impl_.front(); }
  243. reference back() const { return impl_.back(); }
  244. iterator begin() const { return impl_.begin(); }
  245. iterator end() const { return impl_.end(); }
  246. reverse_iterator rbegin() const { return impl_.rbegin(); }
  247. reverse_iterator rend() const { return impl_.rend(); }
  248. void remove_prefix(size_type n) { impl_.remove_prefix(n); }
  249. void remove_suffix(size_type n) { impl_.remove_suffix(n); }
  250. void pop_back() { remove_suffix(1); }
  251. void pop_front() { remove_prefix(1); }
  252. bool operator==(ArraySlice<T> other) const {
  253. return ArraySlice<T>(*this) == other;
  254. }
  255. bool operator!=(ArraySlice<T> other) const {
  256. return ArraySlice<T>(*this) != other;
  257. }
  258. // DEPRECATED(jacobsa): Please use data() instead.
  259. pointer mutable_data() const { return impl_.data(); }
  260. private:
  261. Impl impl_;
  262. };
  263. template <typename T>
  264. const typename ArraySlice<T>::size_type ArraySlice<T>::npos;
  265. template <typename T>
  266. const typename MutableArraySlice<T>::size_type MutableArraySlice<T>::npos;
  267. } // namespace gtl
  268. } // namespace tensorflow
  269. #endif // TENSORFLOW_LIB_GTL_ARRAY_SLICE_H_