| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/gfx/range/range.h" | 5 #include "ui/gfx/range/range.h" |
| 6 | 6 |
| 7 #include <inttypes.h> | 7 #include <inttypes.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <limits> | |
| 11 | 10 |
| 12 #include "base/logging.h" | 11 #include "base/logging.h" |
| 13 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 14 | 13 |
| 15 namespace gfx { | 14 namespace gfx { |
| 16 | 15 |
| 17 Range::Range() | |
| 18 : start_(0), | |
| 19 end_(0) { | |
| 20 } | |
| 21 | |
| 22 Range::Range(uint32_t start, uint32_t end) | |
| 23 : start_(start), | |
| 24 end_(end) { | |
| 25 } | |
| 26 | |
| 27 Range::Range(uint32_t position) | |
| 28 : start_(position), | |
| 29 end_(position) { | |
| 30 } | |
| 31 | |
| 32 // static | |
| 33 const Range Range::InvalidRange() { | |
| 34 return Range(std::numeric_limits<uint32_t>::max()); | |
| 35 } | |
| 36 | |
| 37 bool Range::IsValid() const { | |
| 38 return *this != InvalidRange(); | |
| 39 } | |
| 40 | |
| 41 uint32_t Range::GetMin() const { | |
| 42 return std::min(start(), end()); | |
| 43 } | |
| 44 | |
| 45 uint32_t Range::GetMax() const { | |
| 46 return std::max(start(), end()); | |
| 47 } | |
| 48 | |
| 49 bool Range::operator==(const Range& other) const { | |
| 50 return start() == other.start() && end() == other.end(); | |
| 51 } | |
| 52 | |
| 53 bool Range::operator!=(const Range& other) const { | |
| 54 return !(*this == other); | |
| 55 } | |
| 56 | |
| 57 bool Range::EqualsIgnoringDirection(const Range& other) const { | |
| 58 return GetMin() == other.GetMin() && GetMax() == other.GetMax(); | |
| 59 } | |
| 60 | |
| 61 bool Range::Intersects(const Range& range) const { | |
| 62 return IsValid() && range.IsValid() && | |
| 63 !(range.GetMax() < GetMin() || range.GetMin() >= GetMax()); | |
| 64 } | |
| 65 | |
| 66 bool Range::Contains(const Range& range) const { | |
| 67 return IsValid() && range.IsValid() && | |
| 68 GetMin() <= range.GetMin() && range.GetMax() <= GetMax(); | |
| 69 } | |
| 70 | |
| 71 Range Range::Intersect(const Range& range) const { | 16 Range Range::Intersect(const Range& range) const { |
| 72 uint32_t min = std::max(GetMin(), range.GetMin()); | 17 uint32_t min = std::max(GetMin(), range.GetMin()); |
| 73 uint32_t max = std::min(GetMax(), range.GetMax()); | 18 uint32_t max = std::min(GetMax(), range.GetMax()); |
| 74 | 19 |
| 75 if (min >= max) // No intersection. | 20 if (min >= max) // No intersection. |
| 76 return InvalidRange(); | 21 return InvalidRange(); |
| 77 | 22 |
| 78 return Range(min, max); | 23 return Range(min, max); |
| 79 } | 24 } |
| 80 | 25 |
| 81 std::string Range::ToString() const { | 26 std::string Range::ToString() const { |
| 82 return base::StringPrintf("{%" PRIu32 ",%" PRIu32 "}", start(), end()); | 27 return base::StringPrintf("{%" PRIu32 ",%" PRIu32 "}", start(), end()); |
| 83 } | 28 } |
| 84 | 29 |
| 85 std::ostream& operator<<(std::ostream& os, const Range& range) { | 30 std::ostream& operator<<(std::ostream& os, const Range& range) { |
| 86 return os << range.ToString(); | 31 return os << range.ToString(); |
| 87 } | 32 } |
| 88 | 33 |
| 89 } // namespace gfx | 34 } // namespace gfx |
| OLD | NEW |