Chromium Code Reviews| 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 <class T> | |
|
Alexei Svitkine (slow)
2013/01/28 20:52:30
Nit: change "class" to "typename", since its used
msw
2013/01/29 17:17:25
Done.
| |
| 27 class BreakList { | |
| 28 public: | |
| 29 // A BreakList const iterator, typedef'ed for convenience. | |
| 30 typedef typename std::vector<std::pair<size_t, T> >::const_iterator | |
|
Alexei Svitkine (slow)
2013/01/28 20:52:30
Nit: How about typedefing std::pair<size_t, T> to
msw
2013/01/29 17:17:25
Done.
| |
| 31 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<std::pair<size_t, T> >& list() const { return list_; } | |
|
Alexei Svitkine (slow)
2013/01/28 20:52:30
Nit: Instead of naming it |list|, how about |break
msw
2013/01/29 17:17:25
Done.
| |
| 38 | |
| 39 // Clear the list and set a break at position 0 with the supplied |value|. | |
| 40 void SetValue(T value); | |
| 41 | |
| 42 // Adjust the list 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<std::pair<size_t, T> >::iterator GetBreak( | |
| 50 size_t position); | |
| 51 | |
| 52 // Get the last position to which the supplied break applies; returns the | |
| 53 // start position of the next break, or INT_MAX for the terminal break. | |
| 54 size_t GetBreakEnd(const typename BreakList<T>::const_iterator& i) const; | |
| 55 | |
| 56 // Save and restore the list to apply temporary values between calls. | |
| 57 void SaveList(); | |
|
Alexei Svitkine (slow)
2013/01/28 20:52:30
I'd prefer if this functionality was outside of th
msw
2013/01/29 17:17:25
Done.
| |
| 58 void RestoreList(); | |
| 59 | |
| 60 // Comparison functions for testing purposes. | |
| 61 bool EqualsValueForTest(T value) const; | |
| 62 bool EqualsListForTest(const std::vector<std::pair<size_t, T> >& list) const; | |
|
Alexei Svitkine (slow)
2013/01/28 20:52:30
I think the convention is to suffix it with "ForTe
msw
2013/01/29 17:17:25
Done.
| |
| 63 | |
| 64 private: | |
| 65 #ifndef NDEBUG | |
| 66 // Check for ordered breaks [0->length) with no adjacent equivalent values. | |
| 67 void CheckList(size_t length); | |
| 68 #endif | |
| 69 | |
| 70 std::vector<std::pair<size_t, T> > list_; | |
| 71 std::vector<std::pair<size_t, T> > saved_list_; | |
| 72 | |
| 73 DISALLOW_COPY_AND_ASSIGN(BreakList); | |
| 74 }; | |
| 75 | |
| 76 template<class T> | |
| 77 BreakList<T>::BreakList() : list_(1, std::pair<size_t, T>(0, T())) { | |
| 78 } | |
| 79 | |
| 80 template<class T> | |
| 81 BreakList<T>::BreakList(T value) : list_(1, std::pair<size_t, T>(0, value)) { | |
| 82 } | |
| 83 | |
| 84 template<class T> | |
| 85 void BreakList<T>::SetValue(T value) { | |
| 86 list_.clear(); | |
| 87 list_.push_back(std::pair<size_t, T>(0, value)); | |
| 88 } | |
| 89 | |
| 90 template<class T> | |
| 91 void BreakList<T>::ApplyValue(T value, const ui::Range& range) { | |
| 92 if (!range.IsValid() || range.is_empty()) | |
| 93 return; | |
| 94 DCHECK(!list_.empty()); | |
| 95 DCHECK(!range.is_reversed()); | |
| 96 DCHECK(ui::Range(0, INT_MAX).Contains(range)); | |
| 97 | |
| 98 // Erase any breaks in |range|, then add start and end breaks as needed. | |
| 99 typename std::vector<std::pair<size_t, T> >::iterator start = | |
| 100 GetBreak(range.start()); | |
| 101 start += start->first < range.start() ? 1 : 0; | |
| 102 typename std::vector<std::pair<size_t, T> >::iterator end = | |
| 103 GetBreak(range.end()); | |
| 104 T trailing_value = end->second; | |
| 105 typename std::vector<std::pair<size_t, T> >::iterator i = | |
| 106 start == list_.end() ? start : list_.erase(start, end + 1); | |
| 107 if (range.start() == 0 || (i - 1)->second != value) | |
| 108 i = list_.insert(i, std::pair<size_t, T>(range.start(), value)) + 1; | |
| 109 if (trailing_value != value && range.end() != INT_MAX) | |
| 110 list_.insert(i, std::pair<size_t, T>(range.end(), trailing_value)); | |
| 111 | |
| 112 #ifndef NDEBUG | |
| 113 CheckList(INT_MAX); | |
| 114 #endif | |
| 115 } | |
| 116 | |
| 117 template<class T> | |
| 118 void BreakList<T>::TrimBreaks(size_t length) { | |
| 119 for (size_t i = 1; i < list_.size(); ++i) | |
| 120 if (list_[i].first >= length) | |
| 121 list_.resize(i); | |
| 122 | |
| 123 #ifndef NDEBUG | |
| 124 CheckList(length); | |
| 125 #endif | |
| 126 } | |
| 127 | |
| 128 template<class T> | |
| 129 typename std::vector<std::pair<size_t, T> >::iterator BreakList<T>::GetBreak( | |
| 130 size_t position) { | |
| 131 typename std::vector<std::pair<size_t, T> >::iterator i = list_.end() - 1; | |
| 132 for (; i != list_.begin() && i->first > position; --i); | |
| 133 return i; | |
| 134 } | |
| 135 | |
| 136 template<class T> | |
| 137 size_t BreakList<T>::GetBreakEnd( | |
| 138 const typename BreakList<T>::const_iterator& i) const { | |
| 139 return (i + 1) == list_.end() ? INT_MAX : (i + 1)->first; | |
| 140 } | |
| 141 | |
| 142 template<class T> | |
| 143 void BreakList<T>::SaveList() { | |
| 144 DCHECK(saved_list_.empty()); | |
| 145 saved_list_ = list_; | |
| 146 } | |
| 147 | |
| 148 template<class T> | |
| 149 void BreakList<T>::RestoreList() { | |
| 150 DCHECK(!saved_list_.empty()); | |
| 151 list_ = saved_list_; | |
| 152 saved_list_.clear(); | |
| 153 } | |
| 154 | |
| 155 template<class T> | |
| 156 bool BreakList<T>::EqualsValueForTest(T value) const { | |
| 157 return list_.size() == 1 && list_[0] == std::pair<size_t, T>(0, value); | |
| 158 } | |
| 159 | |
| 160 template<class T> | |
| 161 bool BreakList<T>::EqualsListForTest( | |
| 162 const std::vector<std::pair<size_t, T> >& list) const { | |
| 163 if (list_.size() != list.size()) | |
| 164 return false; | |
| 165 for (size_t i = 0; i < list.size(); ++i) | |
| 166 if (list_[i] != list[i]) | |
| 167 return false; | |
| 168 return true; | |
| 169 } | |
| 170 | |
| 171 #ifndef NDEBUG | |
| 172 template <class T> | |
| 173 void BreakList<T>::CheckList(size_t length) { | |
| 174 DCHECK_EQ(list_[0].first, 0U) << "The first break must be at position 0."; | |
| 175 for (size_t i = 0; i < list_.size() - 1; ++i) { | |
| 176 DCHECK_LT(list_[i].first, list_[i + 1].first) << "Break out of order."; | |
| 177 DCHECK_NE(list_[i].second, list_[i + 1].second) << "Redundant break."; | |
| 178 } | |
| 179 if (length > 0) | |
| 180 DCHECK_LT(list_.back().first, length) << "Break beyond max length."; | |
| 181 } | |
| 182 #endif | |
| 183 | |
| 184 } // namespace gfx | |
| 185 | |
| 186 #endif // UI_GFX_BREAK_LIST_H_ | |
| OLD | NEW |