1/*
2 * Copyright (C) 2015-2017 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include <limits.h>
29#include <wtf/MathExtras.h>
30#include <wtf/PrintStream.h>
31
32namespace WTF {
33
34// Note that the 'begin' is inclusive, while the 'end' is exclusive. These two ranges are non-
35// overlapping:
36//
37// rangeA = 0...8
38// rangeB = 8...16
39
40template<typename PassedType>
41class Range {
42public:
43 typedef PassedType Type;
44
45 Range()
46 : m_begin(0)
47 , m_end(0)
48 {
49 }
50
51 explicit Range(Type value)
52 : m_begin(value)
53 , m_end(value + 1)
54 {
55 ASSERT(m_end >= m_begin);
56 }
57
58 Range(Type begin, Type end)
59 : m_begin(begin)
60 , m_end(end)
61 {
62 ASSERT(m_end >= m_begin);
63 if (m_begin == m_end) {
64 // Canonicalize empty ranges.
65 m_begin = 0;
66 m_end = 0;
67 }
68 }
69
70 static Range top()
71 {
72 return Range(std::numeric_limits<Type>::min(), std::numeric_limits<Type>::max());
73 }
74
75 bool operator==(const Range& other) const
76 {
77 return m_begin == other.m_begin
78 && m_end == other.m_end;
79 }
80
81 bool operator!=(const Range& other) const
82 {
83 return !(*this == other);
84 }
85
86 explicit operator bool() const { return m_begin != m_end; }
87
88 Range operator|(const Range& other) const
89 {
90 if (!*this)
91 return other;
92 if (!other)
93 return *this;
94 return Range(
95 std::min(m_begin, other.m_begin),
96 std::max(m_end, other.m_end));
97 }
98
99 Range& operator|=(const Range& other)
100 {
101 return *this = *this | other;
102 }
103
104 Type begin() const { return m_begin; }
105 Type end() const { return m_end; }
106
107 bool overlaps(const Range& other) const
108 {
109 return WTF::rangesOverlap(m_begin, m_end, other.m_begin, other.m_end);
110 }
111
112 bool contains(Type point) const
113 {
114 return m_begin <= point && point < m_end;
115 }
116
117 void dump(PrintStream& out) const
118 {
119 if (*this == Range()) {
120 out.print("Bottom");
121 return;
122 }
123 if (*this == top()) {
124 out.print("Top");
125 return;
126 }
127 if (m_begin + 1 == m_end) {
128 out.print(m_begin);
129 return;
130 }
131 out.print(m_begin, "...", m_end);
132 }
133
134private:
135 Type m_begin;
136 Type m_end;
137};
138
139} // namespace WTF
140
141using WTF::Range;
142
143