41 DimValue(int64_t v) : value_(v), constant_(true) {}
43 if (constant_ && b.constant_)
51 return std::to_string(value_);
56 if (!constant_)
throw std::runtime_error(
"DimValue is not constant");
71 explicit Dim() : value_(0), constant_(false), origin_(this) {}
74 : value_(0), constant_(false), origin_(this), name_(name) {}
77 : value_(value), constant_(true), origin_(nullptr), name_(
name) {}
80 : value_(other.value_),
81 constant_(other.constant_),
82 origin_(other.origin_),
87 Dim& operator=(
const Dim&) =
delete;
91 return origin_->value_;
98 return origin_->constant_;
122 return std::to_string(
value());
140 static int64_t
apply(int64_t a, int64_t b) {
return a + b; }
142 template <
class T1,
class T2>
144 if (!a.constant() && a.constant() == b.constant()) {
147 throw std::runtime_error(
"Illegal dim expression: " + exstr);
149 }
else if (!a.constant()) {
150 return a.assign(ans - b.value());
152 return b.assign(ans - a.value());
161 static int64_t
apply(int64_t a, int64_t b) {
return a - b; }
163 template <
class T1,
class T2>
165 if (!a.constant() && a.constant() == b.constant()) {
168 throw std::runtime_error(
"Illegal dim expression: " + exstr);
170 }
else if (!a.constant()) {
171 return a.assign(ans + b.value());
173 return b.assign(a.value() - ans);
182 static int64_t
apply(int64_t a, int64_t b) {
return a * b; }
184 template <
class T1,
class T2>
188 throw std::runtime_error(
"Illegal dim expression: " + exstr);
197 static int64_t
apply(int64_t a, int64_t b) {
return a / b; }
199 template <
class T1,
class T2>
203 throw std::runtime_error(
"Illegal dim expression: " + exstr);
212 static int64_t
apply(int64_t a, int64_t b) {
213 throw std::runtime_error(
"Cannot evaluate OR expression");
216 template <
class T1,
class T2>
218 return a.assign(ans) || b.assign(ans);
225 template <
class TLeft,
class TRight,
class TOp>
229 return DimX(left, right);
234 return TOp::apply(left_.value(), right_.value());
246 return TOp::backprop(a, left_, right_);
251 return left_.ToString(show_value) + TOp::ToString() +
252 right_.ToString(show_value);
256 DimX(TLeft left, TRight right) : left_(left), right_(right) {
257 constant_ = left.constant() && right.constant() && TOp::constant();
268 #define DEFINE_DIMX_OPERATOR(opclass, symbol) \ 269 inline DimX<Dim, Dim, opclass> operator symbol(Dim a, Dim b) { \ 270 return DimX<Dim, Dim, opclass>::Create(a, b); \ 273 template <class TL, class TR, class TOp> \ 274 inline DimX<Dim, DimX<TL, TR, TOp>, opclass> operator symbol( \ 275 Dim a, DimX<TL, TR, TOp>&& b) { \ 276 return DimX<Dim, DimX<TL, TR, TOp>, opclass>::Create(a, b); \ 279 template <class TL, class TR, class TOp> \ 280 inline DimX<DimX<TL, TR, TOp>, Dim, opclass> operator symbol( \ 281 DimX<TL, TR, TOp>&& a, Dim b) { \ 282 return DimX<DimX<TL, TR, TOp>, Dim, opclass>::Create(a, b); \ 285 template <class TL1, class TR1, class TOp1, class TL2, class TR2, \ 287 inline DimX<DimX<TL1, TR1, TOp1>, DimX<TL2, TR2, TOp2>, opclass> \ 288 operator symbol(DimX<TL1, TR1, TOp1>&& a, DimX<TL2, TR2, TOp2>&& b) { \ 289 return DimX<DimX<TL1, TR1, TOp1>, DimX<TL2, TR2, TOp2>, \ 290 opclass>::Create(a, b); \ 298 #undef DEFINE_DIMX_OPERATOR 305 template <
class TLeft,
class TRight,
class TOp>
309 return b_copy.assign(a.
value());
325 template <
class... args>
327 static const size_t value =
sizeof...(args);
330 template <
class TLeft,
class TRight,
class TOp>
339 template <
class TLeft,
class TRight,
class TOp>
344 template <
class TLeft,
class TRight,
class TOp>
349 return unknown_dim_value;
360 return unknown_dim_value;
366 template <
class TDimX>
371 template <
class TDimX,
class... TArgs>
376 template <
class TDimX>
378 int64_t unknown_dim_value,
380 out.push_back(
GetValue(dimex, unknown_dim_value));
383 template <
class TDimX,
class... TArgs>
385 int64_t unknown_dim_value,
388 out.push_back(
GetValue(dimex, unknown_dim_value));
392 template <
class TDimX>
394 std::vector<int64_t> out;
399 template <
class TDimX,
class... TArgs>
403 std::vector<int64_t> out;
412 template <
class TLeft,
class TRight,
class TOp>
414 bool status = (lhs == std::forward<DimX<TLeft, TRight, TOp>>(rhs));
419 bool status = lhs == d;
432 template <CSOpt Opt = CSOpt::NONE,
class TDimX>
433 bool _CheckShape(
const std::vector<DimValue>& shape, TDimX&& dimex) {
435 const int rank_diff = shape.size() - 1;
441 if (rank_diff != 0) {
450 for (
int i = 0; i < rank_diff + 1; ++i) s *= shape[i];
451 status =
CheckDim(s, std::forward<TDimX>(dimex));
453 status =
CheckDim(shape[rank_diff], std::forward<TDimX>(dimex));
457 status =
CheckDim(s, std::forward<TDimX>(dimex));
459 status =
CheckDim(shape[0], std::forward<TDimX>(dimex));
475 if (rank_diff != 0) {
484 for (
int i = 0; i < rank_diff + 1; ++i) s *= shape[i];
485 status =
CheckDim(s, std::forward<TDimX>(dimex));
487 status =
CheckDim(shape[rank_diff], std::forward<TDimX>(dimex));
489 status =
CheckDim(shape[0], std::forward<TDimX>(dimex));
496 std::vector<DimValue> shape2(shape.begin() +
offset, shape.end());
497 bool status2 = _CheckShape<Opt>(shape2, std::forward<TArgs>(args)...);
499 return status && status2;
593 std::tuple<bool, std::string>
CheckShape(
const std::vector<DimValue>& shape,
596 const bool status = _CheckShape<Opt>(shape, std::forward<TDimX>(dimex),
597 std::forward<TArgs>(args)...);
599 return std::make_tuple(status, std::string());
605 std::string shape_str;
606 if (rank_diff <= 0) {
608 for (
int i = 0; i <
int(shape.size()); ++i) {
609 shape_str += shape[i].ToString();
610 if (i + 1 <
int(shape.size())) shape_str +=
", ";
616 for (
int i = 0; i < rank_diff; ++i) {
617 shape_str += shape[i].ToString();
618 if (i + 1 <
int(shape.size())) shape_str +=
"*";
622 for (
int i = 0; i < rank_diff; ++i) {
623 shape_str += shape[i].ToString();
624 if (i + 1 < rank_diff) shape_str +=
", ";
636 int end = shape.size();
638 end -= rank_diff + 1;
642 for (
int i = start; i < end; ++i) {
643 shape_str += shape[i].ToString();
644 if (i + 1 < end) shape_str +=
", ";
648 for (
int i = std::max<int>(0, shape.size() - rank_diff - 1);
649 i <
int(shape.size()); ++i) {
650 shape_str += shape[i].ToString();
651 if (i + 1 <
int(shape.size())) shape_str +=
"*";
656 for (
int i = std::max<int>(0, shape.size() - rank_diff);
657 i <
int(shape.size()); ++i) {
658 shape_str += shape[i].ToString();
659 if (i + 1 <
int(shape.size())) shape_str +=
", ";
668 std::string expected_shape;
670 expected_shape =
"[" +
GetString(dimex) +
"]";
673 expected_shape =
"[" +
GetString(dimex) +
", " +
681 errstr =
"got rank " + std::to_string(shape.size()) +
" " +
682 shape_str +
", expected rank " +
686 errstr =
"got " + shape_str +
", expected " + expected_shape;
688 return std::make_tuple(status, errstr);
std::string ToString(bool show_value=true)
Definition: ShapeChecking.h:250
static int64_t apply(int64_t a, int64_t b)
Definition: ShapeChecking.h:182
static bool constant()
Definition: ShapeChecking.h:181
static std::string ToString()
Definition: ShapeChecking.h:177
static DimX< TLeft, TRight, TOp > Create(TLeft left, TRight right)
Definition: ShapeChecking.h:228
Dim(const Dim &other)
Definition: ShapeChecking.h:79
static bool backprop(int64_t ans, T1 a, T2 b)
Definition: ShapeChecking.h:164
Class for representing a possibly unknown dimension value.
Definition: ShapeChecking.h:38
bool CheckDim(const DimValue &lhs, DimX< TLeft, TRight, TOp > &&rhs)
Definition: ShapeChecking.h:413
Dim(int64_t value, const std::string &name="")
Definition: ShapeChecking.h:76
int offset
Definition: FilePCD.cpp:64
bool _CheckShape(const std::vector< DimValue > &shape, TDimX &&dimex)
Definition: ShapeChecking.h:433
DimValue UnknownValue()
Definition: ShapeChecking.h:66
Definition: ShapeChecking.h:210
CSOpt
Check shape options.
Definition: ShapeChecking.h:424
Definition: ShapeChecking.h:180
Definition: ShapeChecking.h:159
Definition: ShapeChecking.h:138
std::string GetString(DimX< TLeft, TRight, TOp > a, bool show_value=true)
Definition: ShapeChecking.h:331
static bool constant()
Definition: ShapeChecking.h:196
static bool backprop(int64_t ans, T1 a, T2 b)
Definition: ShapeChecking.h:217
static std::string ToString()
Definition: ShapeChecking.h:221
Class for dimensions for which the value should be inferred.
Definition: ShapeChecking.h:69
std::string ToString(bool show_value=true)
Definition: ShapeChecking.h:113
static int64_t apply(int64_t a, int64_t b)
Definition: ShapeChecking.h:212
void CreateDimVector(std::vector< int64_t > &out, int64_t unknown_dim_value, TDimX dimex)
Definition: ShapeChecking.h:377
int64_t value()
Definition: ShapeChecking.h:232
static int64_t apply(int64_t a, int64_t b)
Definition: ShapeChecking.h:161
bool operator==(DimValue a, DimX< TLeft, TRight, TOp > &&b)
Definition: ShapeChecking.h:306
DimValue(int64_t v)
Definition: ShapeChecking.h:41
#define DEFINE_DIMX_OPERATOR(opclass, symbol)
Definition: ShapeChecking.h:268
static std::string ToString()
Definition: ShapeChecking.h:207
int64_t & value()
Definition: ShapeChecking.h:55
~Dim()
Definition: ShapeChecking.h:85
Definition: ShapeChecking.h:195
int64_t & value()
Definition: ShapeChecking.h:89
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample playback_handle k4a_logging_message_cb_t void min_level device_handle k4a_imu_sample_t timeout_in_ms capture_handle capture_handle capture_handle image_handle temperature_c int
Definition: K4aPlugin.cpp:479
DimValue & operator*=(const DimValue &b)
Definition: ShapeChecking.h:42
static bool constant()
Definition: ShapeChecking.h:160
static bool backprop(int64_t ans, T1 a, T2 b)
Definition: ShapeChecking.h:185
bool assign(int64_t a)
assigns a value to the expression
Definition: ShapeChecking.h:242
Dim(const std::string &name)
Definition: ShapeChecking.h:73
Definition: PinholeCameraIntrinsic.cpp:35
static int64_t apply(int64_t a, int64_t b)
Definition: ShapeChecking.h:197
bool & constant()
Definition: ShapeChecking.h:59
std::string name
Definition: FilePCD.cpp:58
bool & constant()
Definition: ShapeChecking.h:239
static bool backprop(int64_t ans, T1 a, T2 b)
Definition: ShapeChecking.h:200
static std::string ToString()
Definition: ShapeChecking.h:156
std::string CreateDimXString()
Definition: ShapeChecking.h:364
Dim expression class.
Definition: ShapeChecking.h:226
Definition: ShapeChecking.h:326
std::tuple< bool, std::string > CheckShape(const std::vector< DimValue > &shape, TDimX &&dimex, TArgs &&... args)
Definition: ShapeChecking.h:593
Dim()
Definition: ShapeChecking.h:71
std::string ToString() const
Definition: ShapeChecking.h:49
static bool backprop(int64_t ans, T1 a, T2 b)
Definition: ShapeChecking.h:143
int64_t GetValue(DimX< TLeft, TRight, TOp > a)
Definition: ShapeChecking.h:340
bool assign(int64_t a)
Definition: ShapeChecking.h:105
static bool constant()
Definition: ShapeChecking.h:139
DimValue()
Definition: ShapeChecking.h:40
bool & constant()
Definition: ShapeChecking.h:96
static bool constant()
Definition: ShapeChecking.h:211
static int64_t apply(int64_t a, int64_t b)
Definition: ShapeChecking.h:140
static std::string ToString()
Definition: ShapeChecking.h:192