layer_list_iterator.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "cc/layers/layer_list_iterator.h"
  5. #include "cc/layers/layer.h"
  6. namespace cc {
  7. LayerListIterator::LayerListIterator(Layer* root_layer)
  8. : current_layer_(root_layer) {
  9. DCHECK(!root_layer || !root_layer->parent());
  10. list_indices_.push_back(0);
  11. }
  12. LayerListIterator::LayerListIterator(const LayerListIterator& other) = default;
  13. LayerListIterator& LayerListIterator::operator++() {
  14. // case 0: done
  15. if (!current_layer_)
  16. return *this;
  17. // case 1: descend.
  18. if (!current_layer_->children().empty()) {
  19. current_layer_ = current_layer_->children()[0].get();
  20. list_indices_.push_back(0);
  21. return *this;
  22. }
  23. for (Layer* parent = current_layer_->mutable_parent(); parent;
  24. parent = parent->mutable_parent()) {
  25. // We now try and advance in some list of siblings.
  26. // case 2: Advance to a sibling.
  27. if (list_indices_.back() + 1 < parent->children().size()) {
  28. ++list_indices_.back();
  29. current_layer_ = parent->children()[list_indices_.back()].get();
  30. return *this;
  31. }
  32. // We need to ascend. We will pop an index off the stack.
  33. list_indices_.pop_back();
  34. }
  35. current_layer_ = nullptr;
  36. return *this;
  37. }
  38. LayerListIterator::~LayerListIterator() = default;
  39. LayerListConstIterator::LayerListConstIterator(const Layer* root_layer)
  40. : current_layer_(root_layer) {
  41. DCHECK(!root_layer || !root_layer->parent());
  42. list_indices_.push_back(0);
  43. }
  44. LayerListConstIterator::LayerListConstIterator(
  45. const LayerListConstIterator& other) = default;
  46. LayerListConstIterator& LayerListConstIterator::operator++() {
  47. // case 0: done
  48. if (!current_layer_)
  49. return *this;
  50. // case 1: descend.
  51. if (!current_layer_->children().empty()) {
  52. current_layer_ = current_layer_->children()[0].get();
  53. list_indices_.push_back(0);
  54. return *this;
  55. }
  56. for (const Layer* parent = current_layer_->parent(); parent;
  57. parent = parent->parent()) {
  58. // We now try and advance in some list of siblings.
  59. // case 2: Advance to a sibling.
  60. if (list_indices_.back() + 1 < parent->children().size()) {
  61. ++list_indices_.back();
  62. current_layer_ = parent->children()[list_indices_.back()].get();
  63. return *this;
  64. }
  65. // We need to ascend. We will pop an index off the stack.
  66. list_indices_.pop_back();
  67. }
  68. current_layer_ = nullptr;
  69. return *this;
  70. }
  71. LayerListConstIterator::~LayerListConstIterator() = default;
  72. LayerListReverseIterator::LayerListReverseIterator(Layer* root_layer)
  73. : current_layer_(root_layer) {
  74. DCHECK(!root_layer || !root_layer->parent());
  75. list_indices_.push_back(0);
  76. DescendToRightmostInSubtree();
  77. }
  78. LayerListReverseIterator::LayerListReverseIterator(
  79. const LayerListReverseIterator& other) = default;
  80. // We will only support prefix increment.
  81. LayerListReverseIterator& LayerListReverseIterator::operator++() {
  82. // case 0: done
  83. if (!current_layer_)
  84. return *this;
  85. // case 1: we're the leftmost sibling.
  86. if (!list_indices_.back()) {
  87. list_indices_.pop_back();
  88. current_layer_ = current_layer_->mutable_parent();
  89. return *this;
  90. }
  91. // case 2: we're not the leftmost sibling. In this case, we want to move one
  92. // sibling over, and then descend to the rightmost descendant in that subtree.
  93. CHECK(current_layer_->parent());
  94. --list_indices_.back();
  95. this->current_layer_ =
  96. current_layer_->mutable_parent()->children()[list_indices_.back()].get();
  97. DescendToRightmostInSubtree();
  98. return *this;
  99. }
  100. void LayerListReverseIterator::DescendToRightmostInSubtree() {
  101. if (!current_layer_)
  102. return;
  103. if (current_layer_->children().empty())
  104. return;
  105. size_t last_index = current_layer_->children().size() - 1;
  106. this->current_layer_ = current_layer_->children()[last_index].get();
  107. list_indices_.push_back(last_index);
  108. DescendToRightmostInSubtree();
  109. }
  110. LayerListReverseIterator::~LayerListReverseIterator() = default;
  111. LayerListReverseConstIterator::LayerListReverseConstIterator(
  112. const LayerListReverseConstIterator& other) = default;
  113. LayerListReverseConstIterator::LayerListReverseConstIterator(
  114. const Layer* root_layer)
  115. : current_layer_(root_layer) {
  116. DCHECK(!root_layer || !root_layer->parent());
  117. list_indices_.push_back(0);
  118. DescendToRightmostInSubtree();
  119. }
  120. LayerListReverseConstIterator& LayerListReverseConstIterator::operator++() {
  121. // case 0: done
  122. if (!current_layer_)
  123. return *this;
  124. // case 1: we're the leftmost sibling.
  125. if (!list_indices_.back()) {
  126. list_indices_.pop_back();
  127. current_layer_ = current_layer_->parent();
  128. return *this;
  129. }
  130. // case 2: we're not the leftmost sibling. In this case, we want to move one
  131. // sibling over, and then descend to the rightmost descendant in that subtree.
  132. CHECK(current_layer_->parent());
  133. --list_indices_.back();
  134. this->current_layer_ =
  135. current_layer_->parent()->children()[list_indices_.back()].get();
  136. DescendToRightmostInSubtree();
  137. return *this;
  138. }
  139. void LayerListReverseConstIterator::DescendToRightmostInSubtree() {
  140. if (!current_layer_)
  141. return;
  142. if (current_layer_->children().empty())
  143. return;
  144. size_t last_index = current_layer_->children().size() - 1;
  145. this->current_layer_ = current_layer_->children()[last_index].get();
  146. list_indices_.push_back(last_index);
  147. DescendToRightmostInSubtree();
  148. }
  149. LayerListReverseConstIterator::~LayerListReverseConstIterator() = default;
  150. } // namespace cc