#ifndef UI_GFX_BREAK_LIST_H_
#define UI_GFX_BREAK_LIST_H_
#include <utility>
#include <vector>
#include "base/basictypes.h"
#include "base/logging.h"
#include "ui/gfx/range/range.h"
namespace gfx {
template <typename T>
class BreakList {
public:
typedef std::pair<size_t, T> Break;
typedef typename std::vector<Break>::const_iterator const_iterator;
BreakList();
explicit BreakList(T value);
const std::vector<Break>& breaks() const { return breaks_; }
void SetValue(T value);
void ApplyValue(T value, const Range& range);
void SetMax(size_t max);
size_t max() const { return max_; }
typename std::vector<Break>::iterator GetBreak(size_t position);
typename std::vector<Break>::const_iterator GetBreak(size_t position) const;
Range GetRange(const typename BreakList<T>::const_iterator& i) const;
bool EqualsValueForTesting(T value) const;
bool EqualsForTesting(const std::vector<Break>& breaks) const;
private:
#ifndef NDEBUG
void CheckBreaks();
#endif
std::vector<Break> breaks_;
size_t max_;
};
template<class T>
BreakList<T>::BreakList() : breaks_(1, Break(0, T())), max_(0) {
}
template<class T>
BreakList<T>::BreakList(T value) : breaks_(1, Break(0, value)), max_(0) {
}
template<class T>
void BreakList<T>::SetValue(T value) {
breaks_.clear();
breaks_.push_back(Break(0, value));
}
template<class T>
void BreakList<T>::ApplyValue(T value, const Range& range) {
if (!range.IsValid() || range.is_empty())
return;
DCHECK(!breaks_.empty());
DCHECK(!range.is_reversed());
DCHECK(Range(0, max_).Contains(range));
typename std::vector<Break>::iterator start = GetBreak(range.start());
start += start->first < range.start() ? 1 : 0;
typename std::vector<Break>::iterator end = GetBreak(range.end());
T trailing_value = end->second;
typename std::vector<Break>::iterator i =
start == breaks_.end() ? start : breaks_.erase(start, end + 1);
if (range.start() == 0 || (i - 1)->second != value)
i = breaks_.insert(i, Break(range.start(), value)) + 1;
if (trailing_value != value && range.end() != max_)
breaks_.insert(i, Break(range.end(), trailing_value));
#ifndef NDEBUG
CheckBreaks();
#endif
}
template<class T>
void BreakList<T>::SetMax(size_t max) {
typename std::vector<Break>::iterator i = GetBreak(max);
i += (i == breaks_.begin() || i->first < max) ? 1 : 0;
breaks_.erase(i, breaks_.end());
max_ = max;
#ifndef NDEBUG
CheckBreaks();
#endif
}
template<class T>
typename std::vector<std::pair<size_t, T> >::iterator BreakList<T>::GetBreak(
size_t position) {
typename std::vector<Break>::iterator i = breaks_.end() - 1;
for (; i != breaks_.begin() && i->first > position; --i);
return i;
}
template<class T>
typename std::vector<std::pair<size_t, T> >::const_iterator
BreakList<T>::GetBreak(size_t position) const {
typename std::vector<Break>::const_iterator i = breaks_.end() - 1;
for (; i != breaks_.begin() && i->first > position; --i);
return i;
}
template<class T>
Range BreakList<T>::GetRange(
const typename BreakList<T>::const_iterator& i) const {
const typename BreakList<T>::const_iterator next = i + 1;
return Range(i->first, next == breaks_.end() ? max_ : next->first);
}
template<class T>
bool BreakList<T>::EqualsValueForTesting(T value) const {
return breaks_.size() == 1 && breaks_[0] == Break(0, value);
}
template<class T>
bool BreakList<T>::EqualsForTesting(const std::vector<Break>& breaks) const {
if (breaks_.size() != breaks.size())
return false;
for (size_t i = 0; i < breaks.size(); ++i)
if (breaks_[i] != breaks[i])
return false;
return true;
}
#ifndef NDEBUG
template <class T>
void BreakList<T>::CheckBreaks() {
DCHECK_EQ(breaks_[0].first, 0U) << "The first break must be at position 0.";
for (size_t i = 0; i < breaks_.size() - 1; ++i) {
DCHECK_LT(breaks_[i].first, breaks_[i + 1].first) << "Break out of order.";
DCHECK_NE(breaks_[i].second, breaks_[i + 1].second) << "Redundant break.";
}
if (max_ > 0)
DCHECK_LT(breaks_.back().first, max_) << "Break beyond max position.";
}
#endif
}
#endif