1 | /* |
2 | * Copyright (C) 2012 Adobe Systems Incorporated. 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 | * |
8 | * 1. Redistributions of source code must retain the above |
9 | * copyright notice, this list of conditions and the following |
10 | * disclaimer. |
11 | * 2. Redistributions in binary form must reproduce the above |
12 | * copyright notice, this list of conditions and the following |
13 | * disclaimer in the documentation and/or other materials |
14 | * provided with the distribution. |
15 | * |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
19 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
20 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
25 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
27 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
29 | |
30 | #pragma once |
31 | |
32 | #include <wtf/Vector.h> |
33 | |
34 | namespace WebCore { |
35 | |
36 | template <typename T> |
37 | class ShapeInterval { |
38 | WTF_MAKE_FAST_ALLOCATED; |
39 | public: |
40 | ShapeInterval() |
41 | : m_x1(-1) |
42 | , m_x2(-2) |
43 | { |
44 | // The initial values of m_x1,x2 don't matter (unless you're looking |
45 | // at them in the debugger) so long as isUndefined() is true. |
46 | ASSERT(isUndefined()); |
47 | } |
48 | |
49 | ShapeInterval(T x1, T x2) |
50 | : m_x1(x1) |
51 | , m_x2(x2) |
52 | { |
53 | ASSERT(x2 >= x1); |
54 | } |
55 | |
56 | bool isUndefined() const { return m_x2 < m_x1; } |
57 | T x1() const { return isUndefined() ? 0 : m_x1; } |
58 | T x2() const { return isUndefined() ? 0 : m_x2; } |
59 | T width() const { return isUndefined() ? 0 : m_x2 - m_x1; } |
60 | bool isEmpty() const { return isUndefined() ? true : m_x1 == m_x2; } |
61 | |
62 | void set(T x1, T x2) |
63 | { |
64 | ASSERT(x2 >= x1); |
65 | m_x1 = x1; |
66 | m_x2 = x2; |
67 | } |
68 | |
69 | bool overlaps(const ShapeInterval<T>& interval) const |
70 | { |
71 | if (isUndefined() || interval.isUndefined()) |
72 | return false; |
73 | return x2() >= interval.x1() && x1() <= interval.x2(); |
74 | } |
75 | |
76 | bool contains(const ShapeInterval<T>& interval) const |
77 | { |
78 | if (isUndefined() || interval.isUndefined()) |
79 | return false; |
80 | return x1() <= interval.x1() && x2() >= interval.x2(); |
81 | } |
82 | |
83 | void unite(const ShapeInterval<T>& interval) |
84 | { |
85 | if (interval.isUndefined()) |
86 | return; |
87 | if (isUndefined()) |
88 | set(interval.x1(), interval.x2()); |
89 | else |
90 | set(std::min<T>(x1(), interval.x1()), std::max<T>(x2(), interval.x2())); |
91 | } |
92 | |
93 | bool operator==(const ShapeInterval<T>& other) const { return x1() == other.x1() && x2() == other.x2(); } |
94 | bool operator!=(const ShapeInterval<T>& other) const { return !operator==(other); } |
95 | |
96 | private: |
97 | T m_x1; |
98 | T m_x2; |
99 | }; |
100 | |
101 | typedef ShapeInterval<int> IntShapeInterval; |
102 | typedef ShapeInterval<float> FloatShapeInterval; |
103 | |
104 | typedef Vector<IntShapeInterval> IntShapeIntervals; |
105 | typedef Vector<FloatShapeInterval> FloatShapeIntervals; |
106 | |
107 | } // namespace WebCore |
108 | |