Back to index

lightning-sunbird  0.9+nobinonly
nsIntervalSet.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 // vim:cindent:ts=8:et:sw=4:
00003 /* ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is nsIntervalSet. 
00017  *
00018  * The Initial Developer of the Original Code is Netscape Communications
00019  * Corporation.  Portions created by the Initial Developer are Copyright
00020  * (C) 2001 the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   L. David Baron <dbaron@dbaron.org> (original author)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsIntervalSet.h"
00040 #include NEW_H
00041 
00042 nsIntervalSet::nsIntervalSet(IntervalSetAlloc aAlloc, IntervalSetFree aFree,
00043                              void* aAllocatorClosure)
00044     : mList(nsnull),
00045       mAlloc(aAlloc),
00046       mFree(aFree),
00047       mAllocatorClosure(aAllocatorClosure)
00048 {
00049     NS_ASSERTION(mAlloc && mFree, "null callback params");
00050 }
00051 
00052 nsIntervalSet::~nsIntervalSet()
00053 {
00054     Interval *current = mList;
00055     while (current) {
00056         Interval *trash = current;
00057         current = current->mNext;
00058         FreeInterval(trash);
00059     }
00060 }
00061 
00062 void nsIntervalSet::FreeInterval(nsIntervalSet::Interval *aInterval)
00063 {
00064     NS_ASSERTION(aInterval, "null interval");
00065 
00066     aInterval->Interval::~Interval();
00067     (*mFree)(sizeof(Interval), aInterval, mAllocatorClosure);
00068 }
00069 
00070 void nsIntervalSet::IncludeInterval(coord_type aBegin, coord_type aEnd)
00071 {
00072     Interval *newInterval = NS_STATIC_CAST(Interval*,
00073                                (*mAlloc)(sizeof(Interval), mAllocatorClosure));
00074     if (!newInterval) {
00075         NS_NOTREACHED("allocation failure");
00076         return;
00077     }
00078     new(newInterval) Interval(aBegin, aEnd);
00079 
00080     Interval **current = &mList;
00081     while (*current && (*current)->mEnd < aBegin)
00082         current = &(*current)->mNext;
00083 
00084     newInterval->mNext = *current;
00085     *current = newInterval;
00086 
00087     Interval *subsumed = newInterval->mNext;
00088     while (subsumed && subsumed->mBegin <= aEnd) {
00089         newInterval->mEnd = PR_MAX(newInterval->mEnd, subsumed->mEnd);
00090         newInterval->mNext = subsumed->mNext;
00091         FreeInterval(subsumed);
00092         subsumed = newInterval->mNext;
00093     }
00094 }
00095 
00096 PRBool nsIntervalSet::HasPoint(coord_type aPoint) const
00097 {
00098     Interval *current = mList;
00099     while (current && current->mBegin <= aPoint) {
00100         if (current->mEnd >= aPoint)
00101             return PR_TRUE;
00102         current = current->mNext;
00103     }
00104     return PR_FALSE;
00105 }
00106 
00107 PRBool nsIntervalSet::Intersects(coord_type aBegin, coord_type aEnd) const
00108 {
00109     Interval *current = mList;
00110     while (current && current->mBegin <= aEnd) {
00111         if (current->mEnd >= aBegin)
00112             return PR_TRUE;
00113         current = current->mNext;
00114     }
00115     return PR_FALSE;
00116 }
00117 
00118 PRBool nsIntervalSet::Contains(coord_type aBegin, coord_type aEnd) const
00119 {
00120     Interval *current = mList;
00121     while (current && current->mBegin <= aBegin) {
00122         if (current->mEnd >= aEnd)
00123             return PR_TRUE;
00124         current = current->mNext;
00125     }
00126     return PR_FALSE;
00127 }