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.

btree_iterator.tpp 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* COPYRIGHT 2019 Huawei Technologies Co., Ltd.All Rights Reserved.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #ifndef DATASET_UTIL_BTREE_ITERATOR_H_
  16. #define DATASET_UTIL_BTREE_ITERATOR_H_
  17. #include "dataset/util/de_error.h"
  18. #include "utils/log_adapter.h"
  19. #include "btree.h"
  20. namespace mindspore {
  21. namespace dataset {
  22. template <typename K, typename V, typename C, typename T>
  23. typename BPlusTree<K, V, C, T>::Iterator &BPlusTree<K, V, C, T>::Iterator::operator++() {
  24. if (slot_ + 1u < cur_->slotuse_) {
  25. ++slot_;
  26. } else if (cur_->link_.next) {
  27. cur_ = cur_->link_.next;
  28. slot_ = 0;
  29. } else {
  30. slot_ = cur_->slotuse_;
  31. }
  32. return *this;
  33. }
  34. template <typename K, typename V, typename C, typename T>
  35. typename BPlusTree<K, V, C, T>::Iterator BPlusTree<K, V, C, T>::Iterator::operator++(int) {
  36. Iterator tmp = *this;
  37. if (slot_ + 1u < cur_->slotuse_) {
  38. ++slot_;
  39. } else if (cur_->link_.next) {
  40. cur_ = cur_->link_.next;
  41. slot_ = 0;
  42. } else {
  43. slot_ = cur_->slotuse_;
  44. }
  45. return tmp;
  46. }
  47. template <typename K, typename V, typename C, typename T>
  48. typename BPlusTree<K, V, C, T>::Iterator &BPlusTree<K, V, C, T>::Iterator::operator--() {
  49. if (slot_ > 0) {
  50. --slot_;
  51. } else if (cur_->link_.prev) {
  52. cur_ = cur_->link_.prev;
  53. slot_ = cur_->slotuse_ - 1;
  54. } else {
  55. slot_ = 0;
  56. }
  57. return *this;
  58. }
  59. template <typename K, typename V, typename C, typename T>
  60. typename BPlusTree<K, V, C, T>::Iterator BPlusTree<K, V, C, T>::Iterator::operator--(int) {
  61. Iterator tmp = *this;
  62. if (slot_ > 0) {
  63. --slot_;
  64. } else if (cur_->link_.prev) {
  65. cur_ = cur_->link_.prev;
  66. slot_ = cur_->slotuse_ - 1;
  67. } else {
  68. slot_ = 0;
  69. }
  70. return tmp;
  71. }
  72. template <typename K, typename V, typename C, typename T>
  73. typename BPlusTree<K, V, C, T>::ConstIterator &BPlusTree<K, V, C, T>::ConstIterator::operator++() {
  74. if (slot_ + 1u < cur_->slotuse_) {
  75. ++slot_;
  76. } else if (cur_->link_.next) {
  77. cur_ = cur_->link_.next;
  78. slot_ = 0;
  79. } else {
  80. slot_ = cur_->slotuse_;
  81. }
  82. return *this;
  83. }
  84. template <typename K, typename V, typename C, typename T>
  85. typename BPlusTree<K, V, C, T>::ConstIterator BPlusTree<K, V, C, T>::ConstIterator::operator++(int) {
  86. Iterator tmp = *this;
  87. if (slot_ + 1u < cur_->slotuse_) {
  88. ++slot_;
  89. } else if (cur_->link_.next) {
  90. cur_ = cur_->link_.next;
  91. slot_ = 0;
  92. } else {
  93. slot_ = cur_->slotuse_;
  94. }
  95. return tmp;
  96. }
  97. template <typename K, typename V, typename C, typename T>
  98. typename BPlusTree<K, V, C, T>::ConstIterator &BPlusTree<K, V, C, T>::ConstIterator::operator--() {
  99. if (slot_ > 0) {
  100. --slot_;
  101. } else if (cur_->link_.prev) {
  102. cur_ = cur_->link_.prev;
  103. slot_ = cur_->slotuse_ - 1;
  104. } else {
  105. slot_ = 0;
  106. }
  107. return *this;
  108. }
  109. template <typename K, typename V, typename C, typename T>
  110. typename BPlusTree<K, V, C, T>::ConstIterator BPlusTree<K, V, C, T>::ConstIterator::operator--(int) {
  111. Iterator tmp = *this;
  112. if (slot_ > 0) {
  113. --slot_;
  114. } else if (cur_->link_.prev) {
  115. cur_ = cur_->link_.prev;
  116. slot_ = cur_->slotuse_ - 1;
  117. } else {
  118. slot_ = 0;
  119. }
  120. return tmp;
  121. }
  122. template <typename K, typename V, typename C, typename T>
  123. typename BPlusTree<K, V, C, T>::ConstIterator BPlusTree<K, V, C, T>::Search(const key_type &key) const {
  124. if (root_ != nullptr) {
  125. LeafNode *leaf = nullptr;
  126. slot_type slot;
  127. IndexRc rc = Locate(root_, key, &leaf, &slot);
  128. if (rc == IndexRc::kOk) {
  129. return ConstIterator(leaf, slot);
  130. } else {
  131. MS_LOG(INFO) << "Key not found. rc = " << static_cast<int>(rc) << ".";
  132. return end();
  133. }
  134. } else {
  135. return end();
  136. }
  137. }
  138. template <typename K, typename V, typename C, typename T>
  139. typename BPlusTree<K, V, C, T>::value_type BPlusTree<K, V, C, T>::operator[](key_type key) {
  140. ConstIterator it = Search(key);
  141. return it.value();
  142. }
  143. template <typename K, typename V, typename C, typename T>
  144. typename BPlusTree<K, V, C, T>::Iterator BPlusTree<K, V, C, T>::begin() {
  145. return Iterator(this);
  146. }
  147. template <typename K, typename V, typename C, typename T>
  148. typename BPlusTree<K, V, C, T>::Iterator BPlusTree<K, V, C, T>::end() {
  149. return Iterator(this->leaf_nodes_.tail, this->leaf_nodes_.tail ? this->leaf_nodes_.tail->slotuse_ : 0);
  150. }
  151. template <typename K, typename V, typename C, typename T>
  152. typename BPlusTree<K, V, C, T>::ConstIterator BPlusTree<K, V, C, T>::begin() const {
  153. return ConstIterator(this);
  154. }
  155. template <typename K, typename V, typename C, typename T>
  156. typename BPlusTree<K, V, C, T>::ConstIterator BPlusTree<K, V, C, T>::end() const {
  157. return ConstIterator(this->leaf_nodes_.tail, this->leaf_nodes_.tail ? this->leaf_nodes_.tail->slotuse_ : 0);
  158. }
  159. template <typename K, typename V, typename C, typename T>
  160. typename BPlusTree<K, V, C, T>::ConstIterator BPlusTree<K, V, C, T>::cbegin() const {
  161. return ConstIterator(this);
  162. }
  163. template <typename K, typename V, typename C, typename T>
  164. typename BPlusTree<K, V, C, T>::ConstIterator BPlusTree<K, V, C, T>::cend() const {
  165. return ConstIterator(this->leaf_nodes_.tail, this->leaf_nodes_.tail ? this->leaf_nodes_.tail->slotuse_ : 0);
  166. }
  167. } // namespace dataset
  168. } // namespace mindspore
  169. #endif