123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- #ifndef INFER_SERVER_SHAPE_H_
- #define INFER_SERVER_SHAPE_H_
- #include <iostream>
- #include <vector>
- namespace infer_server {
- class Shape {
- public:
-
- using value_type = int64_t;
-
- Shape() = default;
-
- explicit Shape(const std::vector<value_type>& v) noexcept { data_ = v; }
-
- Shape& operator=(const std::vector<value_type>& v) noexcept {
- data_ = v;
- return *this;
- }
- Shape(const Shape&) = default;
- Shape& operator=(const Shape&) = default;
- Shape(Shape&&) = default;
- Shape& operator=(Shape&&) = default;
-
- value_type operator[](int offset) const noexcept { return data_[offset]; }
-
- value_type& operator[](int offset) noexcept { return data_[offset]; }
-
- size_t Size() const noexcept { return data_.size(); };
-
- bool Empty() const noexcept { return data_.empty(); };
-
- std::vector<value_type> Vectorize() const noexcept { return data_; }
-
- value_type BatchSize() const noexcept { return data_[0]; }
-
- int64_t DataCount() const noexcept {
- int64_t cnt = 1;
- for (size_t i = 1; i < data_.size(); ++i) {
- cnt *= data_[i];
- }
- return cnt;
- }
-
- int64_t BatchDataCount() const noexcept {
- int64_t cnt = 1;
- for (size_t i = 0; i < data_.size(); ++i) {
- cnt *= data_[i];
- }
- return cnt;
- }
-
- friend std::ostream& operator<<(std::ostream& os, const Shape& shape) {
- os << "Shape (";
- for (size_t i = 0; i < shape.Size() - 1; ++i) {
- os << shape[i] << ", ";
- }
- if (shape.Size() > 0) os << shape[shape.Size() - 1];
- os << ")";
- return os;
- }
-
- bool operator==(const Shape& other) const noexcept {
- if (Size() != other.Size()) return false;
- for (size_t i = 0; i < Size(); ++i) {
- if (data_[i] != other[i]) return false;
- }
- return true;
- }
-
- bool operator!=(const Shape& other) const noexcept { return !(*this == other); }
- private:
- std::vector<value_type> data_;
- };
- }
- #endif
|