intrusive_iterator.tpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // intrusive_iterator.hpp
  3. // utilities
  4. //
  5. // Created by Sam Jaffe on 5/10/13.
  6. // Copyright (c) 2013 Sam Jaffe. All rights reserved.
  7. //
  8. #pragma once
  9. template <typename N>
  10. intrusive_iterator<N>::intrusive_iterator(N* node) :
  11. node_(node) {
  12. }
  13. template <typename N>
  14. auto intrusive_iterator<N>::operator*() -> reference {
  15. return *(node_->ptr_);
  16. }
  17. template <typename N>
  18. auto intrusive_iterator<N>::operator*() const -> const_reference {
  19. return *(node_->ptr_);
  20. }
  21. template <typename N>
  22. auto intrusive_iterator<N>::operator->() -> pointer {
  23. return node_->ptr_;
  24. }
  25. template <typename N>
  26. auto intrusive_iterator<N>::operator->() const -> const_pointer {
  27. return node_->ptr_;
  28. }
  29. template <typename N>
  30. intrusive_iterator<N> intrusive_iterator<N>::operator++(int) {
  31. intrusive_iterator<N> it{*this};
  32. node_ = node_->next_;
  33. return it;
  34. }
  35. template <typename N>
  36. intrusive_iterator<N>& intrusive_iterator<N>::operator++() {
  37. node_ = node_->next_;
  38. return *this;
  39. }
  40. template <typename N>
  41. intrusive_iterator<N> intrusive_iterator<N>::operator--(int) {
  42. intrusive_iterator<N> it{*this};
  43. node_ = node_->prev_;
  44. return it;
  45. }
  46. template <typename N>
  47. intrusive_iterator<N>& intrusive_iterator<N>::operator--() {
  48. node_ = node_->prev_;
  49. return *this;
  50. }
  51. template <typename N>
  52. N* intrusive_iterator<N>::get() {
  53. return node_;
  54. }
  55. template <typename N>
  56. const N* intrusive_iterator<N>::get() const {
  57. return node_;
  58. }
  59. template <typename N>
  60. bool intrusive_iterator<N>::operator==(const intrusive_iterator<N>& other) const {
  61. return node_ == other.node_;
  62. }
  63. template <typename N>
  64. bool intrusive_iterator<N>::operator!=(const intrusive_iterator<N>& other) const {
  65. return node_ != other.node_;
  66. }
  67. #pragma mark const_intrusive_iterator
  68. template <typename N>
  69. const_intrusive_iterator<N>::const_intrusive_iterator(const N* node) :
  70. node_(node) {
  71. }
  72. template <typename N>
  73. auto const_intrusive_iterator<N>::operator*() const -> const_reference {
  74. return *(node_->ptr_);
  75. }
  76. template <typename N>
  77. auto const_intrusive_iterator<N>::operator->() const -> const_pointer {
  78. return node_->ptr_;
  79. }
  80. template <typename N>
  81. const_intrusive_iterator<N> const_intrusive_iterator<N>::operator++(int) {
  82. const_intrusive_iterator<N> it{*this};
  83. node_ = node_->next_;
  84. return it;
  85. }
  86. template <typename N>
  87. const_intrusive_iterator<N>& const_intrusive_iterator<N>::operator++() {
  88. node_ = node_->next_;
  89. return *this;
  90. }
  91. template <typename N>
  92. const_intrusive_iterator<N> const_intrusive_iterator<N>::operator--(int) {
  93. const_intrusive_iterator<N> it{*this};
  94. node_ = node_->prev_;
  95. return it;
  96. }
  97. template <typename N>
  98. const_intrusive_iterator<N>& const_intrusive_iterator<N>::operator--() {
  99. node_ = node_->prev_;
  100. return *this;
  101. }
  102. template <typename N>
  103. const N* const_intrusive_iterator<N>::get() const {
  104. return node_;
  105. }
  106. template <typename N>
  107. bool const_intrusive_iterator<N>::operator==(const const_intrusive_iterator<N>& other) const {
  108. return node_ == other.node_;
  109. }
  110. template <typename N>
  111. bool const_intrusive_iterator<N>::operator!=(const const_intrusive_iterator<N>& other) const {
  112. return node_ != other.node_;
  113. }