Hermes
size.h
Go to the documentation of this file.
1 
32 #ifndef HERMES_COMMON_SIZE_H
33 #define HERMES_COMMON_SIZE_H
34 
35 #include <hermes/common/defs.h>
36 #include <type_traits>
37 #include <iostream>
38 
39 namespace hermes {
40 
41 // *********************************************************************************************************************
42 // Size2
43 // *********************************************************************************************************************
47 template<typename T> class Size2 {
48  static_assert(std::is_same<T, u8>::value
49  || std::is_same<T, u16>::value ||
50  std::is_same<T, u32>::value || std::is_same<T, u64>::value,
51  "Size2 must hold an unsigned integer type!");
52 
53 public:
54  // *******************************************************************************************************************
55  // CONSTRUCTORS
56  // *******************************************************************************************************************
61  HERMES_DEVICE_CALLABLE explicit Size2(T size) : width(size), height(size) {}
66  : width(width), height(height) {}
67  // *******************************************************************************************************************
68  // OPERATORS
69  // *******************************************************************************************************************
70  // access
76  HERMES_DEVICE_CALLABLE T operator[](int i) const { return (&width)[i]; }
82  HERMES_DEVICE_CALLABLE T &operator[](int i) { return (&width)[i]; }
83  // arithmetic
88  return Size2<T>(width + b.width, height + b.height);
89  }
94  return Size2<T>(width / n, height / n);
95  }
100  return Size2<T>(width * s, height * s);
101  }
102  // boolean
107  return width == b.width && height == b.height;
108  }
113  return width != b.width || height != b.height;
114  }
115  // *******************************************************************************************************************
116  // METHODS
117  // *******************************************************************************************************************
120  HERMES_DEVICE_CALLABLE T total() const { return width * height; }
125  [[nodiscard]] HERMES_DEVICE_CALLABLE bool contains(int i, int j) const {
126  return i >= 0 && j >= 0 && i < static_cast<i64>(width) &&
127  j < static_cast<i64>(height);
128  }
129  // *******************************************************************************************************************
130  // PUBLIC FIELDS
131  // *******************************************************************************************************************
132  T width{0};
133  T height{0};
134 };
135 
136 // *********************************************************************************************************************
137 // Size3
138 // *********************************************************************************************************************
142 template<typename T> class Size3 {
143  static_assert(std::is_same<T, u8>::value
144  || std::is_same<T, u16>::value ||
145  std::is_same<T, u32>::value || std::is_same<T, u64>::value,
146  "Size3 must hold an unsigned integer type!");
147 
148 public:
149  // *******************************************************************************************************************
150  // CONSTRUCTORS
151  // *******************************************************************************************************************
156  HERMES_DEVICE_CALLABLE explicit Size3(T size) : width(size), height(size), depth(size) {}
161  HERMES_DEVICE_CALLABLE Size3(T _width, T _height, T _depth)
162  : width(_width), height(_height), depth(_depth) {}
163  // *******************************************************************************************************************
164  // OPERATORS
165  // *******************************************************************************************************************
166  // access
172  HERMES_DEVICE_CALLABLE T operator[](int i) const { return (&width)[i]; }
178  HERMES_DEVICE_CALLABLE T &operator[](int i) { return (&width)[i]; }
179  // arithmetic
184  return {width + b.width, height + b.height, depth + b.depth};
185  }
191  return {width - b.width, height - b.height, depth - b.depth};
192  }
193  // boolean
198  return width == b.width && height == b.height && depth == b.depth;
199  }
204  return width != b.width || height != b.height || depth != b.depth;
205  }
206  // *******************************************************************************************************************
207  // METHODS
208  // *******************************************************************************************************************
211  HERMES_DEVICE_CALLABLE T total() const { return width * height * depth; }
218  HERMES_DEVICE_CALLABLE Size2<T> slice(int d1 = 0, int d2 = 1) const {
219  return Size2<T>((&width)[d1], (&width)[d2]);
220  }
221  // *******************************************************************************************************************
222  // PUBLIC FIELDS
223  // *******************************************************************************************************************
224  T width{0};
225  T height{0};
226  T depth{0};
227 };
228 
229 // *********************************************************************************************************************
230 // IO
231 // *********************************************************************************************************************
237 template<typename T>
238 std::ostream &operator<<(std::ostream &o, const Size2<T> &s) {
239  o << "Size[" << s.width << ", " << s.height << "]";
240  return o;
241 }
247 template<typename T>
248 std::ostream &operator<<(std::ostream &o, const Size3<T> &s) {
249  o << "Size[" << s.width << ", " << s.height << ", " << s.depth << "]";
250  return o;
251 }
252 
253 // *********************************************************************************************************************
254 // TYPEDEFS
255 // *********************************************************************************************************************
256 using size2 = Size2<u32>;
261 using size3 = Size3<u32>;
266 
267 } // namespace hermes
268 
269 #endif
270 
Holds 2-dimensional size.
Definition: size.h:47
HERMES_DEVICE_CALLABLE T operator[](int i) const
Dimension size const access.
Definition: size.h:76
HERMES_DEVICE_CALLABLE Size2< T > operator+(const Size2< T > &b) const
Addition.
Definition: size.h:87
HERMES_DEVICE_CALLABLE bool operator==(const Size2< T > &b) const
Comparison.
Definition: size.h:106
HERMES_DEVICE_CALLABLE bool operator!=(const Size2< T > &b) const
Comparison.
Definition: size.h:112
HERMES_DEVICE_CALLABLE T & operator[](int i)
Dimension size access.
Definition: size.h:82
HERMES_DEVICE_CALLABLE Size2(T size)
Single value constructor.
Definition: size.h:61
HERMES_DEVICE_CALLABLE Size2()
Default constructor.
Definition: size.h:58
T height
1-th dimension size
Definition: size.h:133
HERMES_DEVICE_CALLABLE bool contains(int i, int j) const
Checks if coordinate is inside half-open range [0, size)
Definition: size.h:125
T width
0-th dimension size
Definition: size.h:132
HERMES_DEVICE_CALLABLE Size2< T > operator/(T n) const
Definition: size.h:93
HERMES_DEVICE_CALLABLE T total() const
Computes total size area.
Definition: size.h:120
HERMES_DEVICE_CALLABLE Size2< T > operator*(T s) const
Scalar multiplication.
Definition: size.h:99
HERMES_DEVICE_CALLABLE Size2(T width, T height)
Constructor.
Definition: size.h:65
Holds 2-dimensional size.
Definition: size.h:142
HERMES_DEVICE_CALLABLE bool operator==(const Size3< T > &b) const
Dimension-wise comparison.
Definition: size.h:197
T width
0-th dimension size
Definition: size.h:224
HERMES_DEVICE_CALLABLE Size3< T > operator-(const Size3< T > &b) const
Subtraction.
Definition: size.h:190
HERMES_DEVICE_CALLABLE Size3(T _width, T _height, T _depth)
Constructor.
Definition: size.h:161
HERMES_DEVICE_CALLABLE T total() const
Computes total size area.
Definition: size.h:211
HERMES_DEVICE_CALLABLE T operator[](int i) const
Dimension size const access.
Definition: size.h:172
HERMES_DEVICE_CALLABLE Size3< T > operator+(const Size3< T > &b) const
Addition.
Definition: size.h:183
T height
1-th dimension size
Definition: size.h:225
HERMES_DEVICE_CALLABLE Size3(T size)
Single value constructor.
Definition: size.h:156
HERMES_DEVICE_CALLABLE T & operator[](int i)
Dimension size access.
Definition: size.h:178
T depth
2-th dimension size
Definition: size.h:226
HERMES_DEVICE_CALLABLE Size3()
Default constructor.
Definition: size.h:153
HERMES_DEVICE_CALLABLE Size2< T > slice(int d1=0, int d2=1) const
Gets 2-dimensional slice.
Definition: size.h:218
HERMES_DEVICE_CALLABLE bool operator!=(const Size3< T > &b) const
Dimension-wise comparison.
Definition: size.h:203
Data type definitions.
#define HERMES_DEVICE_CALLABLE
Specifies that the function can be called from both host and device sides.
Definition: defs.h:45