1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : // vim:cindent:ts=8:et:sw=4:
3 : /* ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is nsIntervalSet.
17 : *
18 : * The Initial Developer of the Original Code is Netscape Communications
19 : * Corporation. Portions created by the Initial Developer are Copyright
20 : * (C) 2001 the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * L. David Baron <dbaron@dbaron.org> (original author)
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : /* a set of ranges on a number-line */
40 :
41 : #include "nsIntervalSet.h"
42 : #include "nsAlgorithm.h"
43 : #include NEW_H
44 :
45 0 : nsIntervalSet::nsIntervalSet(IntervalSetAlloc aAlloc, IntervalSetFree aFree,
46 : void* aAllocatorClosure)
47 : : mList(nsnull),
48 : mAlloc(aAlloc),
49 : mFree(aFree),
50 0 : mAllocatorClosure(aAllocatorClosure)
51 : {
52 0 : NS_ASSERTION(mAlloc && mFree, "null callback params");
53 0 : }
54 :
55 0 : nsIntervalSet::~nsIntervalSet()
56 : {
57 0 : Interval *current = mList;
58 0 : while (current) {
59 0 : Interval *trash = current;
60 0 : current = current->mNext;
61 0 : FreeInterval(trash);
62 : }
63 0 : }
64 :
65 0 : void nsIntervalSet::FreeInterval(nsIntervalSet::Interval *aInterval)
66 : {
67 0 : NS_ASSERTION(aInterval, "null interval");
68 :
69 0 : aInterval->Interval::~Interval();
70 0 : (*mFree)(sizeof(Interval), aInterval, mAllocatorClosure);
71 0 : }
72 :
73 0 : void nsIntervalSet::IncludeInterval(coord_type aBegin, coord_type aEnd)
74 : {
75 : Interval *newInterval = static_cast<Interval*>
76 0 : ((*mAlloc)(sizeof(Interval), mAllocatorClosure));
77 0 : if (!newInterval) {
78 0 : NS_NOTREACHED("allocation failure");
79 0 : return;
80 : }
81 0 : new(newInterval) Interval(aBegin, aEnd);
82 :
83 0 : Interval **current = &mList;
84 0 : while (*current && (*current)->mEnd < aBegin)
85 0 : current = &(*current)->mNext;
86 :
87 0 : newInterval->mNext = *current;
88 0 : *current = newInterval;
89 :
90 0 : Interval *subsumed = newInterval->mNext;
91 0 : while (subsumed && subsumed->mBegin <= aEnd) {
92 0 : newInterval->mBegin = NS_MIN(newInterval->mBegin, subsumed->mBegin);
93 0 : newInterval->mEnd = NS_MAX(newInterval->mEnd, subsumed->mEnd);
94 0 : newInterval->mNext = subsumed->mNext;
95 0 : FreeInterval(subsumed);
96 0 : subsumed = newInterval->mNext;
97 : }
98 : }
99 :
100 0 : bool nsIntervalSet::Intersects(coord_type aBegin, coord_type aEnd) const
101 : {
102 0 : Interval *current = mList;
103 0 : while (current && current->mBegin <= aEnd) {
104 0 : if (current->mEnd >= aBegin)
105 0 : return true;
106 0 : current = current->mNext;
107 : }
108 0 : return false;
109 : }
110 :
111 0 : bool nsIntervalSet::Contains(coord_type aBegin, coord_type aEnd) const
112 : {
113 0 : Interval *current = mList;
114 0 : while (current && current->mBegin <= aBegin) {
115 0 : if (current->mEnd >= aEnd)
116 0 : return true;
117 0 : current = current->mNext;
118 : }
119 0 : return false;
120 : }
|