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