Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 5 #include "cc/base/pyramid_sequence.h" | |
| 6 | |
| 7 namespace cc { | |
| 8 | |
| 9 // PyramidSequence | |
| 10 PyramidSequence::PyramidSequence(Type type, int start, int end, int levels) | |
| 11 : start_(start), | |
| 12 end_(end), | |
| 13 levels_(levels), | |
| 14 levels_traversed_(0), | |
| 15 within_bounds_(true) { | |
| 16 DCHECK_GE(levels, 0); | |
| 17 Init(type); | |
| 18 step_ = start <= end ? 1 : -1; | |
| 19 Reset(); | |
| 20 } | |
| 21 | |
| 22 PyramidSequence::~PyramidSequence() {} | |
| 23 | |
| 24 int PyramidSequence::GetCurrent() { | |
| 25 if (levels_traversed_ == levels_) | |
| 26 return -1; | |
| 27 | |
| 28 if (!within_bounds_) | |
| 29 (this->*inflater_)(); | |
| 30 | |
| 31 return current_; | |
| 32 } | |
| 33 | |
| 34 int PyramidSequence::GetCoverage() { | |
| 35 return step_ == 1 ? end_ - start_ + 1 : start_ - end_ + 1; | |
| 36 } | |
| 37 | |
| 38 void PyramidSequence::Advance() { | |
| 39 current_ += step_; | |
| 40 within_bounds_ = (this->*within_bounds_checker_)(); | |
| 41 } | |
| 42 | |
| 43 void PyramidSequence::Init(PyramidSequence::Type type) { | |
| 44 switch (type) { | |
| 45 case PyramidSequence::Type::FORWARD: | |
| 46 inflater_ = &PyramidSequence::InflateForward; | |
| 47 within_bounds_checker_ = &PyramidSequence::IsWithinBoundsForward; | |
| 48 break; | |
| 49 case PyramidSequence::Type::BACKWARD: | |
| 50 inflater_ = &PyramidSequence::InflateBackward; | |
| 51 within_bounds_checker_ = &PyramidSequence::IsWithinBoundsBackward; | |
| 52 break; | |
| 53 case PyramidSequence::Type::DIAGONAL_FORWARD: | |
| 54 inflater_ = &PyramidSequence::InflateDiagonalForward; | |
| 55 within_bounds_checker_ = &PyramidSequence::IsWithinBoundsDiagonalForward; | |
| 56 break; | |
| 57 case PyramidSequence::Type::DIAGONAL_BACKWARD: | |
| 58 inflater_ = &PyramidSequence::InflateDiagonalBackward; | |
| 59 within_bounds_checker_ = &PyramidSequence::IsWithinBoundsDiagonalBackward; | |
| 60 break; | |
| 61 default: | |
|
vmpstr
2016/06/16 18:28:57
Don't put the default, so that the compiler compla
| |
| 62 NOTREACHED(); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 void PyramidSequence::Reset() { | |
| 67 current_ = start_; | |
| 68 within_bounds_ = true; | |
| 69 } | |
| 70 | |
| 71 void PyramidSequence::InflateForward() { | |
| 72 DCHECK_EQ(step_, 1); | |
| 73 ++levels_traversed_; | |
| 74 --start_; | |
| 75 ++end_; | |
| 76 | |
| 77 Reset(); | |
| 78 } | |
| 79 | |
| 80 bool PyramidSequence::IsWithinBoundsForward() { | |
| 81 DCHECK_EQ(step_, 1); | |
| 82 DCHECK_GE(current_, start_); | |
| 83 | |
| 84 if (current_ > end_) | |
|
vmpstr
2016/06/16 18:28:58
return current_ <= end_;
| |
| 85 return false; | |
| 86 | |
| 87 return true; | |
| 88 } | |
| 89 | |
| 90 void PyramidSequence::InflateBackward() { | |
| 91 DCHECK_EQ(step_, -1); | |
| 92 ++levels_traversed_; | |
| 93 ++start_; | |
| 94 --end_; | |
| 95 | |
| 96 Reset(); | |
| 97 } | |
| 98 | |
| 99 bool PyramidSequence::IsWithinBoundsBackward() { | |
| 100 DCHECK_EQ(step_, -1); | |
| 101 DCHECK_LE(current_, start_); | |
| 102 | |
| 103 if (current_ < end_) | |
|
vmpstr
2016/06/16 18:28:57
return current_ >= end_
| |
| 104 return false; | |
| 105 | |
| 106 return true; | |
| 107 } | |
| 108 | |
| 109 void PyramidSequence::InflateDiagonalForward() { | |
| 110 ++levels_traversed_; | |
| 111 ++start_; | |
| 112 ++end_; | |
| 113 | |
| 114 Reset(); | |
| 115 } | |
| 116 | |
| 117 bool PyramidSequence::IsWithinBoundsDiagonalForward() { | |
| 118 DCHECK_EQ(start_, end_); | |
| 119 if (current_ != start_) | |
|
vmpstr
2016/06/16 18:28:57
return current_ == start_;
| |
| 120 return false; | |
| 121 | |
| 122 return true; | |
| 123 } | |
| 124 | |
| 125 void PyramidSequence::InflateDiagonalBackward() { | |
| 126 ++levels_traversed_; | |
| 127 --start_; | |
| 128 --end_; | |
| 129 | |
| 130 Reset(); | |
| 131 } | |
| 132 | |
| 133 bool PyramidSequence::IsWithinBoundsDiagonalBackward() { | |
| 134 DCHECK_EQ(start_, end_); | |
| 135 if (current_ != start_) | |
|
vmpstr
2016/06/16 18:28:58
return current_ == start_;
Also, diagonal forward
| |
| 136 return false; | |
| 137 | |
| 138 return true; | |
| 139 } | |
| 140 | |
| 141 } // namespace cc | |
| OLD | NEW |