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