| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <algorithm> | 7 #include "base/float_util.h" |
| 8 #include <limits> | |
| 9 | |
| 10 #include "base/format_macros.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/strings/stringprintf.h" | |
| 13 | 8 |
| 14 namespace gfx { | 9 namespace gfx { |
| 15 | 10 |
| 16 Range::Range() | 11 namespace internal { |
| 17 : start_(0), | 12 |
| 18 end_(0) { | 13 template <> |
| 14 TRangeBase<size_t> InvalidRange<size_t>() { |
| 15 return TRangeBase<size_t>(std::numeric_limits<size_t>::max(), |
| 16 std::numeric_limits<size_t>::max()); |
| 19 } | 17 } |
| 20 | 18 |
| 21 Range::Range(size_t start, size_t end) | 19 template <> |
| 22 : start_(start), | 20 TRangeBase<float> InvalidRange<float>() { |
| 23 end_(end) { | 21 return TRangeBase<float>(std::numeric_limits<double>::quiet_NaN(), |
| 22 std::numeric_limits<double>::quiet_NaN()); |
| 24 } | 23 } |
| 25 | 24 |
| 26 Range::Range(size_t position) | 25 bool IsValidRange(const TRangeBase<size_t>& range) { |
| 27 : start_(position), | 26 return range != InvalidRange<size_t>(); |
| 28 end_(position) { | |
| 29 } | 27 } |
| 30 | 28 |
| 31 // static | 29 bool IsValidRange(const TRangeBase<float>& range) { |
| 32 const Range Range::InvalidRange() { | 30 return !base::IsNaN(range.start()) && !base::IsNaN(range.end()); |
| 33 return Range(std::numeric_limits<size_t>::max()); | |
| 34 } | 31 } |
| 35 | 32 |
| 36 bool Range::IsValid() const { | 33 size_t RangeLength(const TRangeBase<size_t>& range) { |
| 37 return *this != InvalidRange(); | 34 ptrdiff_t length = range.end() - range.start(); |
| 35 return length >= 0 ? length : -length; |
| 38 } | 36 } |
| 39 | 37 |
| 40 size_t Range::GetMin() const { | 38 float RangeLength(const TRangeBase<float>& range) { |
| 41 return std::min(start(), end()); | 39 float length = range.end() - range.start(); |
| 40 return length >= 0 ? length : -length; |
| 42 } | 41 } |
| 43 | 42 |
| 44 size_t Range::GetMax() const { | 43 bool RangeIsEmpty(const TRangeBase<size_t>& range) { |
| 45 return std::max(start(), end()); | 44 return range.start() == range.end(); |
| 46 } | 45 } |
| 47 | 46 |
| 48 bool Range::operator==(const Range& other) const { | 47 bool RangeIsEmpty(const TRangeBase<float>& range) { |
| 49 return start() == other.start() && end() == other.end(); | 48 return !range.IsValid() || range.start() == range.end(); |
| 50 } | 49 } |
| 51 | 50 |
| 52 bool Range::operator!=(const Range& other) const { | 51 } // namespace internal |
| 53 return !(*this == other); | |
| 54 } | |
| 55 | |
| 56 bool Range::EqualsIgnoringDirection(const Range& other) const { | |
| 57 return GetMin() == other.GetMin() && GetMax() == other.GetMax(); | |
| 58 } | |
| 59 | |
| 60 bool Range::Intersects(const Range& range) const { | |
| 61 return IsValid() && range.IsValid() && | |
| 62 !(range.GetMax() < GetMin() || range.GetMin() >= GetMax()); | |
| 63 } | |
| 64 | |
| 65 bool Range::Contains(const Range& range) const { | |
| 66 return IsValid() && range.IsValid() && | |
| 67 GetMin() <= range.GetMin() && range.GetMax() <= GetMax(); | |
| 68 } | |
| 69 | |
| 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()); | |
| 73 | |
| 74 if (min >= max) // No intersection. | |
| 75 return InvalidRange(); | |
| 76 | |
| 77 return Range(min, max); | |
| 78 } | |
| 79 | |
| 80 std::string Range::ToString() const { | |
| 81 return base::StringPrintf("{%" PRIuS ",%" PRIuS "}", start(), end()); | |
| 82 } | |
| 83 | 52 |
| 84 std::ostream& operator<<(std::ostream& os, const Range& range) { | 53 std::ostream& operator<<(std::ostream& os, const Range& range) { |
| 85 return os << range.ToString(); | 54 return os << range.ToString(); |
| 86 } | 55 } |
| 87 | 56 |
| 57 std::ostream& operator<<(std::ostream& os, const RangeF& range) { |
| 58 return os << range.ToString(); |
| 59 } |
| 60 |
| 88 } // namespace gfx | 61 } // namespace gfx |
| OLD | NEW |