Chromium Code Reviews| Index: cc/base/pyramid_sequence.cc |
| diff --git a/cc/base/pyramid_sequence.cc b/cc/base/pyramid_sequence.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..28a0ab6cee6755f17b3faf678bda85f94a438041 |
| --- /dev/null |
| +++ b/cc/base/pyramid_sequence.cc |
| @@ -0,0 +1,141 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "cc/base/pyramid_sequence.h" |
| + |
| +namespace cc { |
| + |
| +// PyramidSequence |
| +PyramidSequence::PyramidSequence(Type type, int start, int end, int levels) |
| + : start_(start), |
| + end_(end), |
| + levels_(levels), |
| + levels_traversed_(0), |
| + within_bounds_(true) { |
| + DCHECK_GE(levels, 0); |
| + Init(type); |
| + step_ = start <= end ? 1 : -1; |
| + Reset(); |
| +} |
| + |
| +PyramidSequence::~PyramidSequence() {} |
| + |
| +int PyramidSequence::GetCurrent() { |
| + if (levels_traversed_ == levels_) |
| + return -1; |
| + |
| + if (!within_bounds_) |
| + (this->*inflater_)(); |
| + |
| + return current_; |
| +} |
| + |
| +int PyramidSequence::GetCoverage() { |
| + return step_ == 1 ? end_ - start_ + 1 : start_ - end_ + 1; |
| +} |
| + |
| +void PyramidSequence::Advance() { |
| + current_ += step_; |
| + within_bounds_ = (this->*within_bounds_checker_)(); |
| +} |
| + |
| +void PyramidSequence::Init(PyramidSequence::Type type) { |
| + switch (type) { |
| + case PyramidSequence::Type::FORWARD: |
| + inflater_ = &PyramidSequence::InflateForward; |
| + within_bounds_checker_ = &PyramidSequence::IsWithinBoundsForward; |
| + break; |
| + case PyramidSequence::Type::BACKWARD: |
| + inflater_ = &PyramidSequence::InflateBackward; |
| + within_bounds_checker_ = &PyramidSequence::IsWithinBoundsBackward; |
| + break; |
| + case PyramidSequence::Type::DIAGONAL_FORWARD: |
| + inflater_ = &PyramidSequence::InflateDiagonalForward; |
| + within_bounds_checker_ = &PyramidSequence::IsWithinBoundsDiagonalForward; |
| + break; |
| + case PyramidSequence::Type::DIAGONAL_BACKWARD: |
| + inflater_ = &PyramidSequence::InflateDiagonalBackward; |
| + within_bounds_checker_ = &PyramidSequence::IsWithinBoundsDiagonalBackward; |
| + break; |
| + default: |
|
vmpstr
2016/06/16 18:28:57
Don't put the default, so that the compiler compla
|
| + NOTREACHED(); |
| + } |
| +} |
| + |
| +void PyramidSequence::Reset() { |
| + current_ = start_; |
| + within_bounds_ = true; |
| +} |
| + |
| +void PyramidSequence::InflateForward() { |
| + DCHECK_EQ(step_, 1); |
| + ++levels_traversed_; |
| + --start_; |
| + ++end_; |
| + |
| + Reset(); |
| +} |
| + |
| +bool PyramidSequence::IsWithinBoundsForward() { |
| + DCHECK_EQ(step_, 1); |
| + DCHECK_GE(current_, start_); |
| + |
| + if (current_ > end_) |
|
vmpstr
2016/06/16 18:28:58
return current_ <= end_;
|
| + return false; |
| + |
| + return true; |
| +} |
| + |
| +void PyramidSequence::InflateBackward() { |
| + DCHECK_EQ(step_, -1); |
| + ++levels_traversed_; |
| + ++start_; |
| + --end_; |
| + |
| + Reset(); |
| +} |
| + |
| +bool PyramidSequence::IsWithinBoundsBackward() { |
| + DCHECK_EQ(step_, -1); |
| + DCHECK_LE(current_, start_); |
| + |
| + if (current_ < end_) |
|
vmpstr
2016/06/16 18:28:57
return current_ >= end_
|
| + return false; |
| + |
| + return true; |
| +} |
| + |
| +void PyramidSequence::InflateDiagonalForward() { |
| + ++levels_traversed_; |
| + ++start_; |
| + ++end_; |
| + |
| + Reset(); |
| +} |
| + |
| +bool PyramidSequence::IsWithinBoundsDiagonalForward() { |
| + DCHECK_EQ(start_, end_); |
| + if (current_ != start_) |
|
vmpstr
2016/06/16 18:28:57
return current_ == start_;
|
| + return false; |
| + |
| + return true; |
| +} |
| + |
| +void PyramidSequence::InflateDiagonalBackward() { |
| + ++levels_traversed_; |
| + --start_; |
| + --end_; |
| + |
| + Reset(); |
| +} |
| + |
| +bool PyramidSequence::IsWithinBoundsDiagonalBackward() { |
| + DCHECK_EQ(start_, end_); |
| + if (current_ != start_) |
|
vmpstr
2016/06/16 18:28:58
return current_ == start_;
Also, diagonal forward
|
| + return false; |
| + |
| + return true; |
| +} |
| + |
| +} // namespace cc |