OLD | NEW |
---|---|
(Empty) | |
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 | |
8 #include <limits.h> | |
9 | |
10 #include <vector> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/logging.h" | |
14 #include "ui/base/range/range.h" | |
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, INT_MAX). | |
23 // The first break always has position 0, to ensure all positions have a value. | |
24 // The value of the terminal break applies to the range [break.first, INT_MAX). | |
25 // The value of other breaks apply to the range [break.first, (break+1).first). | |
26 template <typename T> | |
27 class BreakList { | |
28 public: | |
29 // The break type and const iterator, typedef'ed for convenience. | |
30 typedef std::pair<size_t, T> Break; | |
31 typedef typename std::vector<Break>::const_iterator const_iterator; | |
32 | |
33 // Initialize a break at position 0 with the default or supplied |value|. | |
34 BreakList(); | |
35 explicit BreakList(T value); | |
36 | |
37 const std::vector<Break>& breaks() const { return breaks_; } | |
38 | |
39 // Clear the breaks and set a break at position 0 with the supplied |value|. | |
40 void SetValue(T value); | |
41 | |
42 // Adjust the breaks to apply |value| over the supplied |range|. | |
43 void ApplyValue(T value, const ui::Range& range); | |
44 | |
45 // Trim any breaks in the range [|position|, INT_MAX). | |
46 void TrimBreaks(size_t position); | |
47 | |
48 // Get the break applicable to |position| (at or preceeding |position|). | |
49 typename std::vector<Break>::iterator GetBreak(size_t position); | |
50 | |
51 // Get the last position to which the supplied break applies; returns the | |
52 // start position of the next break, or INT_MAX for the terminal break. | |
53 size_t GetBreakEnd(const typename BreakList<T>::const_iterator& i) const; | |
54 | |
55 // Comparison functions for testing purposes. | |
56 bool EqualsValueForTesting(T value) const; | |
57 bool EqualsForTesting(const std::vector<Break>& breaks) const; | |
58 | |
59 private: | |
60 #ifndef NDEBUG | |
61 // Check for ordered breaks [0->length) with no adjacent equivalent values. | |
62 void CheckBreaks(size_t length); | |
63 #endif | |
64 | |
65 std::vector<Break> breaks_; | |
66 }; | |
67 | |
68 template<class T> | |
69 BreakList<T>::BreakList() : breaks_(1, Break(0, T())) { | |
70 } | |
71 | |
72 template<class T> | |
73 BreakList<T>::BreakList(T value) : breaks_(1, Break(0, value)) { | |
74 } | |
75 | |
76 template<class T> | |
77 void BreakList<T>::SetValue(T value) { | |
78 breaks_.clear(); | |
79 breaks_.push_back(Break(0, value)); | |
80 } | |
81 | |
82 template<class T> | |
83 void BreakList<T>::ApplyValue(T value, const ui::Range& range) { | |
84 if (!range.IsValid() || range.is_empty()) | |
85 return; | |
86 DCHECK(!breaks_.empty()); | |
87 DCHECK(!range.is_reversed()); | |
88 DCHECK(ui::Range(0, INT_MAX).Contains(range)); | |
89 | |
90 // Erase any breaks in |range|, then add start and end breaks as needed. | |
91 typename std::vector<Break>::iterator start = GetBreak(range.start()); | |
92 start += start->first < range.start() ? 1 : 0; | |
93 typename std::vector<Break>::iterator end = GetBreak(range.end()); | |
94 T trailing_value = end->second; | |
95 typename std::vector<Break>::iterator i = | |
96 start == breaks_.end() ? start : breaks_.erase(start, end + 1); | |
97 if (range.start() == 0 || (i - 1)->second != value) | |
98 i = breaks_.insert(i, Break(range.start(), value)) + 1; | |
99 if (trailing_value != value && range.end() != INT_MAX) | |
100 breaks_.insert(i, Break(range.end(), trailing_value)); | |
101 | |
102 #ifndef NDEBUG | |
103 CheckBreaks(INT_MAX); | |
104 #endif | |
105 } | |
106 | |
107 template<class T> | |
108 void BreakList<T>::TrimBreaks(size_t length) { | |
109 for (size_t i = 1; i < breaks_.size(); ++i) | |
110 if (breaks_[i].first >= length) | |
111 breaks_.resize(i); | |
112 | |
113 #ifndef NDEBUG | |
114 CheckBreaks(length); | |
115 #endif | |
116 } | |
117 | |
118 template<class T> | |
119 typename std::vector<std::pair<size_t, T> >::iterator BreakList<T>::GetBreak( | |
Alexei Svitkine (slow)
2013/01/29 22:07:25
Nit: std::vector<Break>?
msw
2013/01/31 01:48:54
Actually, it seems like the compiler requires:
"ty
| |
120 size_t position) { | |
121 typename std::vector<Break>::iterator i = breaks_.end() - 1; | |
122 for (; i != breaks_.begin() && i->first > position; --i); | |
123 return i; | |
124 } | |
125 | |
126 template<class T> | |
127 size_t BreakList<T>::GetBreakEnd( | |
128 const typename BreakList<T>::const_iterator& i) const { | |
129 return (i + 1) == breaks_.end() ? INT_MAX : (i + 1)->first; | |
130 } | |
131 | |
132 template<class T> | |
133 bool BreakList<T>::EqualsValueForTesting(T value) const { | |
134 return breaks_.size() == 1 && breaks_[0] == Break(0, value); | |
135 } | |
136 | |
137 template<class T> | |
138 bool BreakList<T>::EqualsForTesting( | |
139 const std::vector<std::pair<size_t, T> >& breaks) const { | |
Alexei Svitkine (slow)
2013/01/29 22:07:25
Nit: std::vector<Break>?
msw
2013/01/31 01:48:54
Done.
| |
140 if (breaks_.size() != breaks.size()) | |
141 return false; | |
142 for (size_t i = 0; i < breaks.size(); ++i) | |
143 if (breaks_[i] != breaks[i]) | |
144 return false; | |
145 return true; | |
146 } | |
147 | |
148 #ifndef NDEBUG | |
149 template <class T> | |
150 void BreakList<T>::CheckBreaks(size_t length) { | |
151 DCHECK_EQ(breaks_[0].first, 0U) << "The first break must be at position 0."; | |
152 for (size_t i = 0; i < breaks_.size() - 1; ++i) { | |
153 DCHECK_LT(breaks_[i].first, breaks_[i + 1].first) << "Break out of order."; | |
154 DCHECK_NE(breaks_[i].second, breaks_[i + 1].second) << "Redundant break."; | |
155 } | |
156 if (length > 0) | |
157 DCHECK_LT(breaks_.back().first, length) << "Break beyond max length."; | |
158 } | |
159 #endif | |
160 | |
161 } // namespace gfx | |
162 | |
163 #endif // UI_GFX_BREAK_LIST_H_ | |
OLD | NEW |