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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * Copyright 2020-2021 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 <memory>
  19. #include <string>
  20. #include <unordered_map>
  21. #include <vector>
  22. #include "include/api/status.h"
  23. #include "include/api/types.h"
  24. namespace mindspore {
  25. namespace dataset {
  26. // Forward declare
  27. class ExecutionTree;
  28. class DatasetIterator;
  29. class DatasetOp;
  30. class Tensor;
  31. class NativeRuntimeContext;
  32. class IteratorConsumer;
  33. class Dataset;
  34. using MSTensorMap = std::unordered_map<std::string, mindspore::MSTensor>;
  35. using MSTensorVec = std::vector<mindspore::MSTensor>;
  36. // Abstract class for iterating over the dataset.
  37. class Iterator {
  38. public:
  39. /// \brief Constructor
  40. Iterator();
  41. /// \brief Destructor
  42. ~Iterator();
  43. /// \brief Method for building and launching the pipeline.
  44. /// \param[in] ops - a vector of DatasetOp in the data pipeline.
  45. /// \param[in] num_epochs Number of epochs passed down to EpochCtrlNode, default -1, infinite epochs
  46. /// \return - a Status error code, returns OK if no error encountered.
  47. Status BuildAndLaunchTree(std::shared_ptr<Dataset> ds, int32_t num_epochs);
  48. /// \brief Function to get the next row from the data pipeline.
  49. /// \note Type of return data is a map(with column name).
  50. /// \param[out] row - the output tensor row.
  51. /// \return - a Status error code, returns OK if no error encountered.
  52. Status GetNextRow(MSTensorMap *row);
  53. /// \brief Function to get the next row from the data pipeline.
  54. /// \note Type of return data is a vector(without column name).
  55. /// \param[out] row - the output tensor row.
  56. /// \return - a Status error code, returns OK if no error encountered.
  57. Status GetNextRow(MSTensorVec *row);
  58. /// \brief Function to shut down the data pipeline.
  59. void Stop();
  60. class _Iterator {
  61. public:
  62. explicit _Iterator(Iterator *lt) : lt_{lt}, cur_row_{nullptr} {
  63. if (lt_) {
  64. cur_row_ = new MSTensorMap();
  65. lt_->GetNextRow(cur_row_);
  66. }
  67. }
  68. // Destructor
  69. ~_Iterator() {
  70. if (cur_row_) {
  71. delete cur_row_;
  72. }
  73. }
  74. _Iterator &operator++() {
  75. if (lt_) {
  76. ++ind_;
  77. lt_->GetNextRow(cur_row_);
  78. }
  79. if (cur_row_ && cur_row_->size() == 0) {
  80. delete cur_row_;
  81. cur_row_ = nullptr;
  82. }
  83. return *this;
  84. } // prefix ++ overload
  85. MSTensorMap &operator*() { return *cur_row_; } // dereference operator
  86. MSTensorMap *operator->() { return cur_row_; }
  87. bool operator!=(const _Iterator &rhs) { return cur_row_ != rhs.cur_row_; }
  88. private:
  89. int ind_; // the cur node our Iterator points to
  90. Iterator *lt_;
  91. MSTensorMap *cur_row_;
  92. };
  93. _Iterator begin() { return _Iterator(this); }
  94. _Iterator end() { return _Iterator(nullptr); }
  95. private:
  96. std::unique_ptr<NativeRuntimeContext> runtime_context_;
  97. IteratorConsumer *consumer_;
  98. };
  99. } // namespace dataset
  100. } // namespace mindspore
  101. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_ITERATOR_H_