Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / gfx / range / range.cc
blob1c3968aa6e2f82d561f068434abba3d52869a999
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.
5 #include "ui/gfx/range/range.h"
7 #include <algorithm>
8 #include <limits>
10 #include "base/format_macros.h"
11 #include "base/logging.h"
12 #include "base/strings/stringprintf.h"
14 namespace gfx {
16 Range::Range()
17 : start_(0),
18 end_(0) {
21 Range::Range(size_t start, size_t end)
22 : start_(start),
23 end_(end) {
26 Range::Range(size_t position)
27 : start_(position),
28 end_(position) {
31 // static
32 const Range Range::InvalidRange() {
33 return Range(std::numeric_limits<size_t>::max());
36 bool Range::IsValid() const {
37 return *this != InvalidRange();
40 size_t Range::GetMin() const {
41 return std::min(start(), end());
44 size_t Range::GetMax() const {
45 return std::max(start(), end());
48 bool Range::operator==(const Range& other) const {
49 return start() == other.start() && end() == other.end();
52 bool Range::operator!=(const Range& other) const {
53 return !(*this == other);
56 bool Range::EqualsIgnoringDirection(const Range& other) const {
57 return GetMin() == other.GetMin() && GetMax() == other.GetMax();
60 bool Range::Intersects(const Range& range) const {
61 return IsValid() && range.IsValid() &&
62 !(range.GetMax() < GetMin() || range.GetMin() >= GetMax());
65 bool Range::Contains(const Range& range) const {
66 return IsValid() && range.IsValid() &&
67 GetMin() <= range.GetMin() && range.GetMax() <= GetMax();
70 Range Range::Intersect(const Range& range) const {
71 size_t min = std::max(GetMin(), range.GetMin());
72 size_t max = std::min(GetMax(), range.GetMax());
74 if (min >= max) // No intersection.
75 return InvalidRange();
77 return Range(min, max);
80 std::string Range::ToString() const {
81 return base::StringPrintf("{%" PRIuS ",%" PRIuS "}", start(), end());
84 std::ostream& operator<<(std::ostream& os, const Range& range) {
85 return os << range.ToString();
88 } // namespace gfx