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

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