/src/mozilla-central/layout/generic/nsIntervalSet.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | // vim:cindent:ts=8:et:sw=4: |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | /* a set of ranges on a number-line */ |
8 | | |
9 | | #include "nsIntervalSet.h" |
10 | | #include <new> |
11 | | #include <algorithm> |
12 | | #include "nsIPresShell.h" // for allocation |
13 | | |
14 | | using namespace mozilla; |
15 | | |
16 | | nsIntervalSet::nsIntervalSet(nsIPresShell* aPresShell) |
17 | | : mList(nullptr), |
18 | | mPresShell(aPresShell) |
19 | 0 | { |
20 | 0 | } |
21 | | |
22 | | nsIntervalSet::~nsIntervalSet() |
23 | 0 | { |
24 | 0 | Interval *current = mList; |
25 | 0 | while (current) { |
26 | 0 | Interval *trash = current; |
27 | 0 | current = current->mNext; |
28 | 0 | FreeInterval(trash); |
29 | 0 | } |
30 | 0 | } |
31 | | |
32 | | void* |
33 | | nsIntervalSet::AllocateInterval() |
34 | 0 | { |
35 | 0 | return mPresShell->AllocateByObjectID( |
36 | 0 | eArenaObjectID_nsIntervalSet_Interval, sizeof(Interval)); |
37 | 0 | } |
38 | | |
39 | | void nsIntervalSet::FreeInterval(nsIntervalSet::Interval *aInterval) |
40 | 0 | { |
41 | 0 | NS_ASSERTION(aInterval, "null interval"); |
42 | 0 |
|
43 | 0 | aInterval->Interval::~Interval(); |
44 | 0 | mPresShell->FreeByObjectID(eArenaObjectID_nsIntervalSet_Interval, aInterval); |
45 | 0 | } |
46 | | |
47 | | void nsIntervalSet::IncludeInterval(coord_type aBegin, coord_type aEnd) |
48 | 0 | { |
49 | 0 | auto newInterval = static_cast<Interval*>(AllocateInterval()); |
50 | 0 | new(newInterval) Interval(aBegin, aEnd); |
51 | 0 |
|
52 | 0 | Interval **current = &mList; |
53 | 0 | while (*current && (*current)->mEnd < aBegin) |
54 | 0 | current = &(*current)->mNext; |
55 | 0 |
|
56 | 0 | newInterval->mNext = *current; |
57 | 0 | *current = newInterval; |
58 | 0 |
|
59 | 0 | Interval *subsumed = newInterval->mNext; |
60 | 0 | while (subsumed && subsumed->mBegin <= aEnd) { |
61 | 0 | newInterval->mBegin = std::min(newInterval->mBegin, subsumed->mBegin); |
62 | 0 | newInterval->mEnd = std::max(newInterval->mEnd, subsumed->mEnd); |
63 | 0 | newInterval->mNext = subsumed->mNext; |
64 | 0 | FreeInterval(subsumed); |
65 | 0 | subsumed = newInterval->mNext; |
66 | 0 | } |
67 | 0 | } |
68 | | |
69 | | bool nsIntervalSet::Intersects(coord_type aBegin, coord_type aEnd) const |
70 | 0 | { |
71 | 0 | Interval *current = mList; |
72 | 0 | while (current && current->mBegin <= aEnd) { |
73 | 0 | if (current->mEnd >= aBegin) |
74 | 0 | return true; |
75 | 0 | current = current->mNext; |
76 | 0 | } |
77 | 0 | return false; |
78 | 0 | } |
79 | | |
80 | | bool nsIntervalSet::Contains(coord_type aBegin, coord_type aEnd) const |
81 | 0 | { |
82 | 0 | Interval *current = mList; |
83 | 0 | while (current && current->mBegin <= aBegin) { |
84 | 0 | if (current->mEnd >= aEnd) |
85 | 0 | return true; |
86 | 0 | current = current->mNext; |
87 | 0 | } |
88 | 0 | return false; |
89 | 0 | } |