[email protected] | ccfa43f0 | 2013-02-01 04:42:17 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | #ifndef UI_GFX_BREAK_LIST_H_ |
| 6 | #define UI_GFX_BREAK_LIST_H_ |
| 7 | |
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
[email protected] | ccfa43f0 | 2013-02-01 04:42:17 | [diff] [blame] | 10 | #include <utility> |
| 11 | #include <vector> |
| 12 | |
Hans Wennborg | 4b46b3b | 2020-06-23 08:29:08 | [diff] [blame] | 13 | #include "base/check_op.h" |
[email protected] | db4fc1e2 | 2013-09-06 20:01:51 | [diff] [blame] | 14 | #include "ui/gfx/range/range.h" |
[email protected] | ccfa43f0 | 2013-02-01 04:42:17 | [diff] [blame] | 15 | |
| 16 | namespace gfx { |
| 17 | |
| 18 | // BreakLists manage ordered, non-overlapping, and non-repeating ranged values. |
| 19 | // These may be used to apply ranged colors and styles to text, for an example. |
| 20 | // |
| 21 | // Each break stores the start position and value of its associated range. |
| 22 | // A solitary break at position 0 applies to the entire space [0, max_). |
| 23 | // |max_| is initially 0 and should be set to match the available ranged space. |
| 24 | // The first break always has position 0, to ensure all positions have a value. |
| 25 | // The value of the terminal break applies to the range [break.first, max_). |
| 26 | // The value of other breaks apply to the range [break.first, (break+1).first). |
| 27 | template <typename T> |
| 28 | class BreakList { |
| 29 | public: |
| 30 | // The break type and const iterator, typedef'ed for convenience. |
| 31 | typedef std::pair<size_t, T> Break; |
| 32 | typedef typename std::vector<Break>::const_iterator const_iterator; |
| 33 | |
| 34 | // Initialize a break at position 0 with the default or supplied |value|. |
| 35 | BreakList(); |
| 36 | explicit BreakList(T value); |
| 37 | |
| 38 | const std::vector<Break>& breaks() const { return breaks_; } |
| 39 | |
| 40 | // Clear the breaks and set a break at position 0 with the supplied |value|. |
| 41 | void SetValue(T value); |
| 42 | |
| 43 | // Adjust the breaks to apply |value| over the supplied |range|. |
[email protected] | 3dfe5c5 | 2013-09-18 22:01:22 | [diff] [blame] | 44 | void ApplyValue(T value, const Range& range); |
[email protected] | ccfa43f0 | 2013-02-01 04:42:17 | [diff] [blame] | 45 | |
| 46 | // Set the max position and trim any breaks at or beyond that position. |
| 47 | void SetMax(size_t max); |
[email protected] | eced6cb | 2013-09-16 20:16:20 | [diff] [blame] | 48 | size_t max() const { return max_; } |
[email protected] | ccfa43f0 | 2013-02-01 04:42:17 | [diff] [blame] | 49 | |
| 50 | // Get the break applicable to |position| (at or preceeding |position|). |
| 51 | typename std::vector<Break>::iterator GetBreak(size_t position); |
[email protected] | 1b8f8cd4 | 2013-07-29 01:19:19 | [diff] [blame] | 52 | typename std::vector<Break>::const_iterator GetBreak(size_t position) const; |
[email protected] | ccfa43f0 | 2013-02-01 04:42:17 | [diff] [blame] | 53 | |
| 54 | // Get the range of the supplied break; returns the break's start position and |
| 55 | // the next break's start position (or |max_| for the terminal break). |
[email protected] | 3dfe5c5 | 2013-09-18 22:01:22 | [diff] [blame] | 56 | Range GetRange(const typename BreakList<T>::const_iterator& i) const; |
[email protected] | ccfa43f0 | 2013-02-01 04:42:17 | [diff] [blame] | 57 | |
| 58 | // Comparison functions for testing purposes. |
| 59 | bool EqualsValueForTesting(T value) const; |
| 60 | bool EqualsForTesting(const std::vector<Break>& breaks) const; |
| 61 | |
| 62 | private: |
| 63 | #ifndef NDEBUG |
| 64 | // Check for ordered breaks [0, |max_|) with no adjacent equivalent values. |
| 65 | void CheckBreaks(); |
| 66 | #endif |
| 67 | |
| 68 | std::vector<Break> breaks_; |
| 69 | size_t max_; |
| 70 | }; |
| 71 | |
| 72 | template<class T> |
| 73 | BreakList<T>::BreakList() : breaks_(1, Break(0, T())), max_(0) { |
| 74 | } |
| 75 | |
| 76 | template<class T> |
| 77 | BreakList<T>::BreakList(T value) : breaks_(1, Break(0, value)), max_(0) { |
| 78 | } |
| 79 | |
| 80 | template<class T> |
| 81 | void BreakList<T>::SetValue(T value) { |
| 82 | breaks_.clear(); |
| 83 | breaks_.push_back(Break(0, value)); |
| 84 | } |
| 85 | |
| 86 | template<class T> |
[email protected] | 3dfe5c5 | 2013-09-18 22:01:22 | [diff] [blame] | 87 | void BreakList<T>::ApplyValue(T value, const Range& range) { |
[email protected] | ccfa43f0 | 2013-02-01 04:42:17 | [diff] [blame] | 88 | if (!range.IsValid() || range.is_empty()) |
| 89 | return; |
| 90 | DCHECK(!breaks_.empty()); |
| 91 | DCHECK(!range.is_reversed()); |
Bruce Dawson | 7567ac7 | 2018-03-27 06:53:35 | [diff] [blame] | 92 | DCHECK(Range(0, static_cast<uint32_t>(max_)).Contains(range)); |
[email protected] | ccfa43f0 | 2013-02-01 04:42:17 | [diff] [blame] | 93 | |
| 94 | // Erase any breaks in |range|, then add start and end breaks as needed. |
| 95 | typename std::vector<Break>::iterator start = GetBreak(range.start()); |
| 96 | start += start->first < range.start() ? 1 : 0; |
| 97 | typename std::vector<Break>::iterator end = GetBreak(range.end()); |
| 98 | T trailing_value = end->second; |
| 99 | typename std::vector<Break>::iterator i = |
| 100 | start == breaks_.end() ? start : breaks_.erase(start, end + 1); |
| 101 | if (range.start() == 0 || (i - 1)->second != value) |
| 102 | i = breaks_.insert(i, Break(range.start(), value)) + 1; |
| 103 | if (trailing_value != value && range.end() != max_) |
| 104 | breaks_.insert(i, Break(range.end(), trailing_value)); |
| 105 | |
| 106 | #ifndef NDEBUG |
| 107 | CheckBreaks(); |
| 108 | #endif |
| 109 | } |
| 110 | |
| 111 | template<class T> |
| 112 | void BreakList<T>::SetMax(size_t max) { |
| 113 | typename std::vector<Break>::iterator i = GetBreak(max); |
| 114 | i += (i == breaks_.begin() || i->first < max) ? 1 : 0; |
| 115 | breaks_.erase(i, breaks_.end()); |
| 116 | max_ = max; |
| 117 | |
| 118 | #ifndef NDEBUG |
| 119 | CheckBreaks(); |
| 120 | #endif |
| 121 | } |
| 122 | |
| 123 | template<class T> |
| 124 | typename std::vector<std::pair<size_t, T> >::iterator BreakList<T>::GetBreak( |
| 125 | size_t position) { |
| 126 | typename std::vector<Break>::iterator i = breaks_.end() - 1; |
| 127 | for (; i != breaks_.begin() && i->first > position; --i); |
| 128 | return i; |
| 129 | } |
| 130 | |
| 131 | template<class T> |
[email protected] | 1b8f8cd4 | 2013-07-29 01:19:19 | [diff] [blame] | 132 | typename std::vector<std::pair<size_t, T> >::const_iterator |
| 133 | BreakList<T>::GetBreak(size_t position) const { |
| 134 | typename std::vector<Break>::const_iterator i = breaks_.end() - 1; |
| 135 | for (; i != breaks_.begin() && i->first > position; --i); |
| 136 | return i; |
| 137 | } |
| 138 | |
| 139 | template<class T> |
[email protected] | 3dfe5c5 | 2013-09-18 22:01:22 | [diff] [blame] | 140 | Range BreakList<T>::GetRange( |
[email protected] | ccfa43f0 | 2013-02-01 04:42:17 | [diff] [blame] | 141 | const typename BreakList<T>::const_iterator& i) const { |
| 142 | const typename BreakList<T>::const_iterator next = i + 1; |
[email protected] | 3dfe5c5 | 2013-09-18 22:01:22 | [diff] [blame] | 143 | return Range(i->first, next == breaks_.end() ? max_ : next->first); |
[email protected] | ccfa43f0 | 2013-02-01 04:42:17 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | template<class T> |
| 147 | bool BreakList<T>::EqualsValueForTesting(T value) const { |
| 148 | return breaks_.size() == 1 && breaks_[0] == Break(0, value); |
| 149 | } |
| 150 | |
| 151 | template<class T> |
| 152 | bool BreakList<T>::EqualsForTesting(const std::vector<Break>& breaks) const { |
| 153 | if (breaks_.size() != breaks.size()) |
| 154 | return false; |
| 155 | for (size_t i = 0; i < breaks.size(); ++i) |
| 156 | if (breaks_[i] != breaks[i]) |
| 157 | return false; |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | #ifndef NDEBUG |
| 162 | template <class T> |
| 163 | void BreakList<T>::CheckBreaks() { |
| 164 | DCHECK_EQ(breaks_[0].first, 0U) << "The first break must be at position 0."; |
| 165 | for (size_t i = 0; i < breaks_.size() - 1; ++i) { |
| 166 | DCHECK_LT(breaks_[i].first, breaks_[i + 1].first) << "Break out of order."; |
| 167 | DCHECK_NE(breaks_[i].second, breaks_[i + 1].second) << "Redundant break."; |
| 168 | } |
| 169 | if (max_ > 0) |
| 170 | DCHECK_LT(breaks_.back().first, max_) << "Break beyond max position."; |
| 171 | } |
| 172 | #endif |
| 173 | |
| 174 | } // namespace gfx |
| 175 | |
| 176 | #endif // UI_GFX_BREAK_LIST_H_ |