| 12 | namespace nd::impl { |
| 13 | |
| 14 | class dict_array |
| 15 | { |
| 16 | public: |
| 17 | explicit dict_array(icm::string_map<nd::array> dict, uint64_t num_rows = 1) |
| 18 | : dict_(std::move(dict)) |
| 19 | , num_rows_(num_rows) |
| 20 | { |
| 21 | ASSERT(!dict_.empty()); |
| 22 | } |
| 23 | |
| 24 | enum dtype dtype() const noexcept |
| 25 | { |
| 26 | return nd::dtype::object; |
| 27 | } |
| 28 | |
| 29 | nd::array get(int64_t index) const |
| 30 | { |
| 31 | if (num_rows_ == 1) { |
| 32 | icm::string_map<nd::array> key2arr; |
| 33 | for (const auto& [key, arr] : dict_) { |
| 34 | key2arr[key] = arr[index]; |
| 35 | } |
| 36 | return adapt(nd::dict(std::move(key2arr))); |
| 37 | } |
| 38 | std::vector<nd::array> results; |
| 39 | for (auto idx = 0; idx < num_rows_; ++idx) { |
| 40 | icm::string_map<nd::array> key2arr; |
| 41 | for (const auto& [key, arr] : dict_) { |
| 42 | key2arr[key] = arr[index][idx]; |
| 43 | } |
| 44 | results.emplace_back(adapt(nd::dict(std::move(key2arr)))); |
| 45 | } |
| 46 | return nd::dynamic(std::move(results)); |
| 47 | } |
| 48 | |
| 49 | nd::dict value(int64_t index) const; |
| 50 | |
| 51 | icm::shape shape() const noexcept |
| 52 | { |
| 53 | return dict_.begin()->second.shape(); |
| 54 | } |
| 55 | |
| 56 | uint8_t dimensions() const |
| 57 | { |
| 58 | return static_cast<uint8_t>(dict_.begin()->second.dimensions()); |
| 59 | } |
| 60 | |
| 61 | constexpr bool is_dynamic() const noexcept |
| 62 | { |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | uint64_t num_rows_ = 1; |
| 68 | icm::string_map<nd::array> dict_; |
| 69 | }; |
| 70 | |
| 71 | class dict_array_entry |