Caffe2 - C++ API
A deep learning, cross platform ML framework
expanding_array.h
1 #pragma once
2 
3 #include <c10/util/ArrayRef.h>
4 #include <c10/util/Exception.h>
5 
6 #include <algorithm>
7 #include <array>
8 #include <cstdint>
9 #include <initializer_list>
10 #include <vector>
11 
12 namespace torch {
13 
20 template <size_t D, typename T = int64_t>
22  public:
26  /*implicit*/ ExpandingArray(std::initializer_list<T> list)
27  : ExpandingArray(at::ArrayRef<T>(list)) {}
28 
32  /*implicit*/ ExpandingArray(at::ArrayRef<T> values) {
33  // clang-format off
34  AT_CHECK(
35  values.size() == D,
36  "Expected ", D, " values, but instead got ", values.size());
37  // clang-format on
38  std::copy(values.begin(), values.end(), values_.begin());
39  }
40 
43  /*implicit*/ ExpandingArray(T single_size) {
44  values_.fill(single_size);
45  }
46 
48  /*implicit*/ ExpandingArray(const std::array<T, D>& values)
49  : values_(values) {}
50 
52  std::array<T, D>& operator*() {
53  return values_;
54  }
55 
57  const std::array<T, D>& operator*() const {
58  return values_;
59  }
60 
62  std::array<T, D>* operator->() {
63  return &values_;
64  }
65 
67  const std::array<T, D>* operator->() const {
68  return &values_;
69  }
70 
72  operator at::ArrayRef<T>() const {
73  return values_;
74  }
75 
77  size_t size() const noexcept {
78  return D;
79  }
80 
81  private:
83  std::array<T, D> values_;
84 };
85 
86 template <size_t D, typename T>
87 std::ostream& operator<<(
88  std::ostream& stream,
89  const ExpandingArray<D, T>& expanding_array) {
90  if (expanding_array.size() == 1) {
91  return stream << expanding_array->at(0);
92  }
93  return stream << static_cast<at::ArrayRef<T>>(expanding_array);
94 }
95 } // namespace torch
const std::array< T, D > * operator->() const
Accesses the underlying std::array.
size_t size() const noexcept
Returns the extent of the ExpandingArray.
const std::array< T, D > & operator*() const
Accesses the underlying std::array.
ExpandingArray(T single_size)
Constructs an ExpandingArray from a single value, which is repeated D times (where D is the extent pa...
std::array< T, D > & operator*()
Accesses the underlying std::array.
ExpandingArray(const std::array< T, D > &values)
Constructs an ExpandingArray from a correctly sized std::array.
ExpandingArray(std::initializer_list< T > list)
Constructs an ExpandingArray from an initializer_list.
constexpr size_t size() const
size - Get the array size.
Definition: ArrayRef.h:138
ExpandingArray(at::ArrayRef< T > values)
Constructs an ExpandingArray from an initializer_list.
A utility class that accepts either a container of D-many values, or a single value, which is internally repeated D times.
Definition: jit_type.h:17
std::array< T, D > * operator->()
Accesses the underlying std::array.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:41
Flush-To-Zero and Denormals-Are-Zero mode.
Definition: static.cpp:70