blob: 19c947f5afae545604a5ae84977824f330ed825d [file] [log] [blame]
[email protected]ccfa43f02013-02-01 04:42:171// 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
avic89eb8d42015-12-23 08:08:188#include <stddef.h>
9
[email protected]ccfa43f02013-02-01 04:42:1710#include <utility>
11#include <vector>
12
Hans Wennborg4b46b3b2020-06-23 08:29:0813#include "base/check_op.h"
[email protected]db4fc1e22013-09-06 20:01:5114#include "ui/gfx/range/range.h"
[email protected]ccfa43f02013-02-01 04:42:1715
16namespace 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).
27template <typename T>
28class 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]3dfe5c52013-09-18 22:01:2244 void ApplyValue(T value, const Range& range);
[email protected]ccfa43f02013-02-01 04:42:1745
46 // Set the max position and trim any breaks at or beyond that position.
47 void SetMax(size_t max);
[email protected]eced6cb2013-09-16 20:16:2048 size_t max() const { return max_; }
[email protected]ccfa43f02013-02-01 04:42:1749
50 // Get the break applicable to |position| (at or preceeding |position|).
51 typename std::vector<Break>::iterator GetBreak(size_t position);
[email protected]1b8f8cd42013-07-29 01:19:1952 typename std::vector<Break>::const_iterator GetBreak(size_t position) const;
[email protected]ccfa43f02013-02-01 04:42:1753
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]3dfe5c52013-09-18 22:01:2256 Range GetRange(const typename BreakList<T>::const_iterator& i) const;
[email protected]ccfa43f02013-02-01 04:42:1757
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
72template<class T>
73BreakList<T>::BreakList() : breaks_(1, Break(0, T())), max_(0) {
74}
75
76template<class T>
77BreakList<T>::BreakList(T value) : breaks_(1, Break(0, value)), max_(0) {
78}
79
80template<class T>
81void BreakList<T>::SetValue(T value) {
82 breaks_.clear();
83 breaks_.push_back(Break(0, value));
84}
85
86template<class T>
[email protected]3dfe5c52013-09-18 22:01:2287void BreakList<T>::ApplyValue(T value, const Range& range) {
[email protected]ccfa43f02013-02-01 04:42:1788 if (!range.IsValid() || range.is_empty())
89 return;
90 DCHECK(!breaks_.empty());
91 DCHECK(!range.is_reversed());
Bruce Dawson7567ac72018-03-27 06:53:3592 DCHECK(Range(0, static_cast<uint32_t>(max_)).Contains(range));
[email protected]ccfa43f02013-02-01 04:42:1793
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
111template<class T>
112void 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
123template<class T>
124typename 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
131template<class T>
[email protected]1b8f8cd42013-07-29 01:19:19132typename 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
139template<class T>
[email protected]3dfe5c52013-09-18 22:01:22140Range BreakList<T>::GetRange(
[email protected]ccfa43f02013-02-01 04:42:17141 const typename BreakList<T>::const_iterator& i) const {
142 const typename BreakList<T>::const_iterator next = i + 1;
[email protected]3dfe5c52013-09-18 22:01:22143 return Range(i->first, next == breaks_.end() ? max_ : next->first);
[email protected]ccfa43f02013-02-01 04:42:17144}
145
146template<class T>
147bool BreakList<T>::EqualsValueForTesting(T value) const {
148 return breaks_.size() == 1 && breaks_[0] == Break(0, value);
149}
150
151template<class T>
152bool 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
162template <class T>
163void 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_