• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "ui/gfx/range/range.h"
6 
7 #include <algorithm>
8 #include <limits>
9 
10 #include "base/format_macros.h"
11 #include "base/logging.h"
12 #include "base/strings/stringprintf.h"
13 
14 namespace gfx {
15 
Range()16 Range::Range()
17     : start_(0),
18       end_(0) {
19 }
20 
Range(size_t start,size_t end)21 Range::Range(size_t start, size_t end)
22     : start_(start),
23       end_(end) {
24 }
25 
Range(size_t position)26 Range::Range(size_t position)
27     : start_(position),
28       end_(position) {
29 }
30 
31 // static
InvalidRange()32 const Range Range::InvalidRange() {
33   return Range(std::numeric_limits<size_t>::max());
34 }
35 
IsValid() const36 bool Range::IsValid() const {
37   return *this != InvalidRange();
38 }
39 
GetMin() const40 size_t Range::GetMin() const {
41   return std::min(start(), end());
42 }
43 
GetMax() const44 size_t Range::GetMax() const {
45   return std::max(start(), end());
46 }
47 
operator ==(const Range & other) const48 bool Range::operator==(const Range& other) const {
49   return start() == other.start() && end() == other.end();
50 }
51 
operator !=(const Range & other) const52 bool Range::operator!=(const Range& other) const {
53   return !(*this == other);
54 }
55 
EqualsIgnoringDirection(const Range & other) const56 bool Range::EqualsIgnoringDirection(const Range& other) const {
57   return GetMin() == other.GetMin() && GetMax() == other.GetMax();
58 }
59 
Intersects(const Range & range) const60 bool Range::Intersects(const Range& range) const {
61   return IsValid() && range.IsValid() &&
62       !(range.GetMax() < GetMin() || range.GetMin() >= GetMax());
63 }
64 
Contains(const Range & range) const65 bool Range::Contains(const Range& range) const {
66   return IsValid() && range.IsValid() &&
67       GetMin() <= range.GetMin() && range.GetMax() <= GetMax();
68 }
69 
Intersect(const Range & range) const70 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 
ToString() const80 std::string Range::ToString() const {
81   return base::StringPrintf("{%" PRIuS ",%" PRIuS "}", start(), end());
82 }
83 
operator <<(std::ostream & os,const Range & range)84 std::ostream& operator<<(std::ostream& os, const Range& range) {
85   return os << range.ToString();
86 }
87 
88 }  // namespace gfx
89