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.

slice_op.h 2.7 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 DATASET_KERNELS_DATA_SLICE_OP_H_
  17. #define DATASET_KERNELS_DATA_SLICE_OP_H_
  18. #include <algorithm>
  19. #include <memory>
  20. #include <string>
  21. #include <utility>
  22. #include <vector>
  23. #include "dataset/core/tensor.h"
  24. #include "dataset/kernels/tensor_op.h"
  25. namespace mindspore {
  26. namespace dataset {
  27. class Slice {
  28. public:
  29. Slice() : start_(0), stop_(0), step_(0) {}
  30. Slice(dsize_t start, dsize_t stop, dsize_t step) : start_(start), stop_(stop), step_(step) {}
  31. Slice(dsize_t start, dsize_t stop) : start_(start), stop_(stop), step_(1) {}
  32. explicit Slice(dsize_t stop) : start_(0), stop_(stop), step_(1) {}
  33. ~Slice() = default;
  34. std::vector<dsize_t> Indices(dsize_t length) {
  35. std::vector<dsize_t> indices;
  36. dsize_t index = std::min(Tensor::HandleNeg(start_, length), length);
  37. dsize_t end_index = std::min(Tensor::HandleNeg(stop_, length), length);
  38. if (step_ > 0) {
  39. for (; index < end_index; index += step_) {
  40. indices.push_back(index);
  41. }
  42. } else {
  43. for (; index > end_index; index += step_) {
  44. indices.push_back(index);
  45. }
  46. }
  47. return indices;
  48. }
  49. bool valid() { return !(start_ == 0 && stop_ == 0 && step_ == 0); }
  50. dsize_t start_;
  51. dsize_t stop_;
  52. dsize_t step_;
  53. };
  54. class SliceOp : public TensorOp {
  55. public:
  56. explicit SliceOp(std::vector<dsize_t> indices) : indices_(std::move(indices)) {}
  57. explicit SliceOp(Slice slice) : slice_(slice) {}
  58. explicit SliceOp(bool all) : all_(all) {}
  59. ~SliceOp() override = default;
  60. void Print(std::ostream &out) const override { out << "SliceOp"; }
  61. Status Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) override;
  62. private:
  63. // only on of the following will be valid
  64. // given indices to slice the Tensor. Empty vector if invalid.
  65. std::vector<dsize_t> indices_;
  66. // Slice object. All start, stop and step are 0 if invalid.
  67. Slice slice_;
  68. // Flag to read all indcies in the dim.
  69. bool all_ = false;
  70. };
  71. } // namespace dataset
  72. } // namespace mindspore
  73. #endif // DATASET_KERNELS_DATA_SLICE_OP_H_