Chromium Code Reviews| Index: ui/gfx/break_list.h |
| diff --git a/ui/gfx/break_list.h b/ui/gfx/break_list.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..90d849d03aabd8937fb0f70bc621d19c702861f1 |
| --- /dev/null |
| +++ b/ui/gfx/break_list.h |
| @@ -0,0 +1,186 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef UI_GFX_BREAK_LIST_H_ |
| +#define UI_GFX_BREAK_LIST_H_ |
| + |
| +#include <limits.h> |
| + |
| +#include <vector> |
| + |
| +#include "base/basictypes.h" |
| +#include "base/logging.h" |
| +#include "ui/base/range/range.h" |
| + |
| +namespace gfx { |
| + |
| +// BreakLists manage ordered, non-overlapping, and non-repeating ranged values. |
| +// These may be used to apply ranged colors and styles to text, for an example. |
| +// |
| +// Each break stores the start position and value of its associated range. |
| +// A solitary break at position 0 applies to the entire space [0, INT_MAX). |
| +// The first break always has position 0, to ensure all positions have a value. |
| +// The value of the terminal break applies to the range [break.first, INT_MAX). |
| +// The value of other breaks apply to the range [break.first, (break+1).first). |
| +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.
|
| +class BreakList { |
| + public: |
| + // A BreakList const iterator, typedef'ed for convenience. |
| + 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.
|
| + const_iterator; |
| + |
| + // Initialize a break at position 0 with the default or supplied |value|. |
| + BreakList(); |
| + explicit BreakList(T value); |
| + |
| + 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.
|
| + |
| + // Clear the list and set a break at position 0 with the supplied |value|. |
| + void SetValue(T value); |
| + |
| + // Adjust the list to apply |value| over the supplied |range|. |
| + void ApplyValue(T value, const ui::Range& range); |
| + |
| + // Trim any breaks in the range [|position|, INT_MAX). |
| + void TrimBreaks(size_t position); |
| + |
| + // Get the break applicable to |position| (at or preceeding |position|). |
| + typename std::vector<std::pair<size_t, T> >::iterator GetBreak( |
| + size_t position); |
| + |
| + // Get the last position to which the supplied break applies; returns the |
| + // start position of the next break, or INT_MAX for the terminal break. |
| + size_t GetBreakEnd(const typename BreakList<T>::const_iterator& i) const; |
| + |
| + // Save and restore the list to apply temporary values between calls. |
| + 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.
|
| + void RestoreList(); |
| + |
| + // Comparison functions for testing purposes. |
| + bool EqualsValueForTest(T value) const; |
| + 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.
|
| + |
| + private: |
| +#ifndef NDEBUG |
| + // Check for ordered breaks [0->length) with no adjacent equivalent values. |
| + void CheckList(size_t length); |
| +#endif |
| + |
| + std::vector<std::pair<size_t, T> > list_; |
| + std::vector<std::pair<size_t, T> > saved_list_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(BreakList); |
| +}; |
| + |
| +template<class T> |
| +BreakList<T>::BreakList() : list_(1, std::pair<size_t, T>(0, T())) { |
| +} |
| + |
| +template<class T> |
| +BreakList<T>::BreakList(T value) : list_(1, std::pair<size_t, T>(0, value)) { |
| +} |
| + |
| +template<class T> |
| +void BreakList<T>::SetValue(T value) { |
| + list_.clear(); |
| + list_.push_back(std::pair<size_t, T>(0, value)); |
| +} |
| + |
| +template<class T> |
| +void BreakList<T>::ApplyValue(T value, const ui::Range& range) { |
| + if (!range.IsValid() || range.is_empty()) |
| + return; |
| + DCHECK(!list_.empty()); |
| + DCHECK(!range.is_reversed()); |
| + DCHECK(ui::Range(0, INT_MAX).Contains(range)); |
| + |
| + // Erase any breaks in |range|, then add start and end breaks as needed. |
| + typename std::vector<std::pair<size_t, T> >::iterator start = |
| + GetBreak(range.start()); |
| + start += start->first < range.start() ? 1 : 0; |
| + typename std::vector<std::pair<size_t, T> >::iterator end = |
| + GetBreak(range.end()); |
| + T trailing_value = end->second; |
| + typename std::vector<std::pair<size_t, T> >::iterator i = |
| + start == list_.end() ? start : list_.erase(start, end + 1); |
| + if (range.start() == 0 || (i - 1)->second != value) |
| + i = list_.insert(i, std::pair<size_t, T>(range.start(), value)) + 1; |
| + if (trailing_value != value && range.end() != INT_MAX) |
| + list_.insert(i, std::pair<size_t, T>(range.end(), trailing_value)); |
| + |
| +#ifndef NDEBUG |
| + CheckList(INT_MAX); |
| +#endif |
| +} |
| + |
| +template<class T> |
| +void BreakList<T>::TrimBreaks(size_t length) { |
| + for (size_t i = 1; i < list_.size(); ++i) |
| + if (list_[i].first >= length) |
| + list_.resize(i); |
| + |
| +#ifndef NDEBUG |
| + CheckList(length); |
| +#endif |
| +} |
| + |
| +template<class T> |
| +typename std::vector<std::pair<size_t, T> >::iterator BreakList<T>::GetBreak( |
| + size_t position) { |
| + typename std::vector<std::pair<size_t, T> >::iterator i = list_.end() - 1; |
| + for (; i != list_.begin() && i->first > position; --i); |
| + return i; |
| +} |
| + |
| +template<class T> |
| +size_t BreakList<T>::GetBreakEnd( |
| + const typename BreakList<T>::const_iterator& i) const { |
| + return (i + 1) == list_.end() ? INT_MAX : (i + 1)->first; |
| +} |
| + |
| +template<class T> |
| +void BreakList<T>::SaveList() { |
| + DCHECK(saved_list_.empty()); |
| + saved_list_ = list_; |
| +} |
| + |
| +template<class T> |
| +void BreakList<T>::RestoreList() { |
| + DCHECK(!saved_list_.empty()); |
| + list_ = saved_list_; |
| + saved_list_.clear(); |
| +} |
| + |
| +template<class T> |
| +bool BreakList<T>::EqualsValueForTest(T value) const { |
| + return list_.size() == 1 && list_[0] == std::pair<size_t, T>(0, value); |
| +} |
| + |
| +template<class T> |
| +bool BreakList<T>::EqualsListForTest( |
| + const std::vector<std::pair<size_t, T> >& list) const { |
| + if (list_.size() != list.size()) |
| + return false; |
| + for (size_t i = 0; i < list.size(); ++i) |
| + if (list_[i] != list[i]) |
| + return false; |
| + return true; |
| +} |
| + |
| +#ifndef NDEBUG |
| +template <class T> |
| +void BreakList<T>::CheckList(size_t length) { |
| + DCHECK_EQ(list_[0].first, 0U) << "The first break must be at position 0."; |
| + for (size_t i = 0; i < list_.size() - 1; ++i) { |
| + DCHECK_LT(list_[i].first, list_[i + 1].first) << "Break out of order."; |
| + DCHECK_NE(list_[i].second, list_[i + 1].second) << "Redundant break."; |
| + } |
| + if (length > 0) |
| + DCHECK_LT(list_.back().first, length) << "Break beyond max length."; |
| +} |
| +#endif |
| + |
| +} // namespace gfx |
| + |
| +#endif // UI_GFX_BREAK_LIST_H_ |