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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. void GetNextRow(TensorMap *row);
  49. /// \brief Function to get the next row from the data pipeline.
  50. /// \note Type of return data is a vector(without column name).
  51. /// \param[out] row - the output tensor row.
  52. void GetNextRow(TensorVec *row);
  53. /// \brief Function to shut down the data pipeline.
  54. void Stop();
  55. class _Iterator {
  56. public:
  57. explicit _Iterator(Iterator *lt) : lt_{lt}, cur_row_{nullptr} {
  58. if (lt_) {
  59. cur_row_ = new TensorMap();
  60. lt_->GetNextRow(cur_row_);
  61. }
  62. }
  63. // Destructor
  64. ~_Iterator() {
  65. if (cur_row_) {
  66. delete cur_row_;
  67. }
  68. }
  69. _Iterator &operator++() {
  70. if (lt_) {
  71. ++ind_;
  72. lt_->GetNextRow(cur_row_);
  73. }
  74. if (cur_row_ && cur_row_->size() == 0) {
  75. delete cur_row_;
  76. cur_row_ = nullptr;
  77. }
  78. return *this;
  79. } // prefix ++ overload
  80. TensorMap &operator*() { return *cur_row_; } // dereference operator
  81. TensorMap *operator->() { return cur_row_; }
  82. bool operator!=(const _Iterator &rhs) { return cur_row_ != rhs.cur_row_; }
  83. private:
  84. int ind_; // the cur node our Iterator points to
  85. Iterator *lt_;
  86. TensorMap *cur_row_;
  87. };
  88. _Iterator begin() { return _Iterator(this); }
  89. _Iterator end() { return _Iterator(nullptr); }
  90. private:
  91. // Runtime tree.
  92. // Use shared_ptr instead of unique_ptr because the DatasetIterator constructor takes in a shared_ptr type.
  93. std::shared_ptr<ExecutionTree> tree_;
  94. // Runtime iterator
  95. std::unique_ptr<DatasetIterator> iterator_;
  96. };
  97. } // namespace api
  98. } // namespace dataset
  99. } // namespace mindspore
  100. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_ITERATOR_H_