You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

iterator.h 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_ITERATOR_H_
  17. #define MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_ITERATOR_H_
  18. #include <unordered_map>
  19. #include <memory>
  20. #include <vector>
  21. #include <string>
  22. #include "minddata/dataset/include/status.h"
  23. namespace mindspore {
  24. namespace dataset {
  25. // Forward declare
  26. class ExecutionTree;
  27. class DatasetIterator;
  28. class DatasetOp;
  29. class Tensor;
  30. namespace api {
  31. class Dataset;
  32. using TensorMap = std::unordered_map<std::string, std::shared_ptr<Tensor>>;
  33. using TensorVec = std::vector<std::shared_ptr<Tensor>>;
  34. // Abstract class for iterating over the dataset.
  35. class Iterator {
  36. public:
  37. /// \brief Constructor
  38. Iterator() = default;
  39. /// \brief Destructor
  40. ~Iterator() = default;
  41. /// \brief Method for building and launching the pipeline.
  42. /// \param[in] ops - a vector of DatasetOp in the data pipeline.
  43. /// \return - a Status error code, returns OK if no error encountered.
  44. Status BuildAndLaunchTree(std::shared_ptr<Dataset> ds);
  45. /// \brief Function to get the next row from the data pipeline.
  46. /// \note Type of return data is a map(with column name).
  47. /// \param[out] row - the output tensor row.
  48. /// \return Returns true if no error encountered else false.
  49. bool GetNextRow(TensorMap *row);
  50. /// \brief Function to get the next row from the data pipeline.
  51. /// \note Type of return data is a vector(without column name).
  52. /// \param[out] row - the output tensor row.
  53. /// \return Returns true if no error encountered else false.
  54. bool GetNextRow(TensorVec *row);
  55. /// \brief Function to shut down the data pipeline.
  56. void Stop();
  57. class _Iterator {
  58. public:
  59. explicit _Iterator(Iterator *lt) : lt_{lt}, cur_row_{nullptr} {
  60. if (lt_) {
  61. cur_row_ = new TensorMap();
  62. lt_->GetNextRow(cur_row_);
  63. }
  64. }
  65. // Destructor
  66. ~_Iterator() {
  67. if (cur_row_) {
  68. delete cur_row_;
  69. }
  70. }
  71. _Iterator &operator++() {
  72. if (lt_) {
  73. ++ind_;
  74. lt_->GetNextRow(cur_row_);
  75. }
  76. if (cur_row_ && cur_row_->size() == 0) {
  77. delete cur_row_;
  78. cur_row_ = nullptr;
  79. }
  80. return *this;
  81. } // prefix ++ overload
  82. TensorMap &operator*() { return *cur_row_; } // dereference operator
  83. TensorMap *operator->() { return cur_row_; }
  84. bool operator!=(const _Iterator &rhs) { return cur_row_ != rhs.cur_row_; }
  85. private:
  86. int ind_; // the cur node our Iterator points to
  87. Iterator *lt_;
  88. TensorMap *cur_row_;
  89. };
  90. _Iterator begin() { return _Iterator(this); }
  91. _Iterator end() { return _Iterator(nullptr); }
  92. private:
  93. // Runtime tree.
  94. // Use shared_ptr instead of unique_ptr because the DatasetIterator constructor takes in a shared_ptr type.
  95. std::shared_ptr<ExecutionTree> tree_;
  96. // Runtime iterator
  97. std::unique_ptr<DatasetIterator> iterator_;
  98. };
  99. } // namespace api
  100. } // namespace dataset
  101. } // namespace mindspore
  102. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_ITERATOR_H_