Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
range.h
Go to the documentation of this file.
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#ifndef UI_GFX_RANGE_RANGE_H_
6#define UI_GFX_RANGE_RANGE_H_
7
8#include <algorithm>
9#include <cstddef>
10#include <cstdint>
11#include <limits>
12#include <ostream>
13#include <string>
14
16#include "gfx_range_export.h"
17
18#if defined(OS_APPLE)
19#if __OBJC__
20#import <Foundation/Foundation.h>
21#else
22typedef struct _NSRange NSRange;
23#endif
24#endif // defined(OS_APPLE)
25
26namespace gfx {
27
28// This class represents either a forward range [min, max) or a reverse range
29// (max, min]. |start_| is always the first of these and |end_| the second; as a
30// result, the range is forward if (start_ <= end_). The zero-width range
31// [val, val) is legal, contains and intersects itself, and is contained by and
32// intersects any nonempty range [min, max) where min <= val < max.
34 public:
35 // Creates an empty range {0,0}.
36 constexpr Range() : Range(0) {}
37
38 // Initializes the range with a start and end.
39 constexpr Range(uint32_t start, uint32_t end) : start_(start), end_(end) {}
40
41 // Initializes the range with the same start and end positions.
42 constexpr explicit Range(uint32_t position) : Range(position, position) {}
43
44 // Platform constructors.
45#if defined(OS_APPLE)
46 explicit Range(const NSRange& range);
47#endif
48
49 // Returns a range that is invalid, which is {UINT32_MAX,UINT32_MAX}.
50 static constexpr Range InvalidRange() { return Range(std::numeric_limits<uint32_t>::max()); }
51
52 // Checks if the range is valid through comparison to InvalidRange().
53 constexpr bool IsValid() const { return *this != InvalidRange(); }
54
55 // Getters and setters.
56 constexpr uint32_t start() const { return start_; }
57 void set_start(uint32_t start) { start_ = start; }
58
59 constexpr uint32_t end() const { return end_; }
60 void set_end(uint32_t end) { end_ = end; }
61
62 // Returns the absolute value of the length.
63 constexpr uint32_t length() const { return GetMax() - GetMin(); }
64
65 constexpr bool is_reversed() const { return start() > end(); }
66 constexpr bool is_empty() const { return start() == end(); }
67
68 // Returns the minimum and maximum values.
69 constexpr uint32_t GetMin() const { return start() < end() ? start() : end(); }
70 constexpr uint32_t GetMax() const { return start() > end() ? start() : end(); }
71
72 constexpr bool operator==(const Range& other) const {
73 return start() == other.start() && end() == other.end();
74 }
75 constexpr bool operator!=(const Range& other) const { return !(*this == other); }
76 constexpr bool EqualsIgnoringDirection(const Range& other) const {
77 return GetMin() == other.GetMin() && GetMax() == other.GetMax();
78 }
79
80 // Returns true if this range intersects the specified |range|.
81 constexpr bool Intersects(const Range& range) const { return Intersect(range).IsValid(); }
82
83 // Returns true if this range contains the specified |range|.
84 constexpr bool Contains(const Range& range) const {
85 return range.IsBoundedBy(*this) &&
86 // A non-empty range doesn't contain the range [max, max).
87 (range.GetMax() != GetMax() || range.is_empty() == is_empty());
88 }
89
90 // Returns true if this range is contained by the specified |range| or it is
91 // an empty range and ending the range |range|.
92 constexpr bool IsBoundedBy(const Range& range) const {
93 return IsValid() && range.IsValid() && GetMin() >= range.GetMin() && GetMax() <= range.GetMax();
94 }
95
96 // Computes the intersection of this range with the given |range|.
97 // If they don't intersect, it returns an InvalidRange().
98 // The returned range is always empty or forward (never reversed).
99 constexpr Range Intersect(const Range& range) const {
100 const uint32_t min = std::max(GetMin(), range.GetMin());
101 const uint32_t max = std::min(GetMax(), range.GetMax());
102 return (min < max || Contains(range) || range.Contains(*this)) ? Range(min, max)
103 : InvalidRange();
104 }
105
106#if defined(OS_APPLE)
107 Range& operator=(const NSRange& range);
108
109 // NSRange does not store the directionality of a range, so if this
110 // is_reversed(), the range will get flipped when converted to an NSRange.
111 NSRange ToNSRange() const;
112#endif
113 // GTK+ has no concept of a range.
114
115 std::string ToString() const;
116
117 private:
118 // Note: we use uint32_t instead of size_t because this struct is sent over
119 // IPC which could span 32 & 64 bit processes. This is fine since text spans
120 // shouldn't exceed UINT32_MAX even on 64 bit builds.
121 uint32_t start_;
122 uint32_t end_;
123};
124
125GFX_RANGE_EXPORT std::ostream& operator<<(std::ostream& os, const Range& range);
126
127} // namespace gfx
128
129#endif // UI_GFX_RANGE_RANGE_H_
static void is_empty(skiatest::Reporter *reporter, const SkPath &p)
constexpr bool Intersects(const Range &range) const
Definition range.h:81
constexpr Range Intersect(const Range &range) const
Definition range.h:99
constexpr uint32_t GetMax() const
Definition range.h:70
constexpr bool is_empty() const
Definition range.h:66
constexpr bool IsBoundedBy(const Range &range) const
Definition range.h:92
constexpr uint32_t GetMin() const
Definition range.h:69
constexpr bool EqualsIgnoringDirection(const Range &other) const
Definition range.h:76
constexpr bool operator!=(const Range &other) const
Definition range.h:75
void set_start(uint32_t start)
Definition range.h:57
constexpr bool operator==(const Range &other) const
Definition range.h:72
constexpr uint32_t end() const
Definition range.h:59
constexpr bool IsValid() const
Definition range.h:53
constexpr uint32_t length() const
Definition range.h:63
constexpr uint32_t start() const
Definition range.h:56
constexpr Range()
Definition range.h:36
constexpr Range(uint32_t position)
Definition range.h:42
constexpr Range(uint32_t start, uint32_t end)
Definition range.h:39
constexpr bool is_reversed() const
Definition range.h:65
void set_end(uint32_t end)
Definition range.h:60
constexpr bool Contains(const Range &range) const
Definition range.h:84
static constexpr Range InvalidRange()
Definition range.h:50
glong glong end
#define GFX_RANGE_EXPORT
static float max(float r, float g, float b)
Definition hsl.cpp:49
static float min(float r, float g, float b)
Definition hsl.cpp:48
Definition insets.cc:10
std::ostream & operator<<(std::ostream &os, const Range &range)
Definition range.cc:18