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

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