Back to index

lightning-sunbird  0.9+nobinonly
nsIntervalSet.h
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 #ifndef nsIntervalSet_h___
00040 #define nsIntervalSet_h___
00041 
00042 #include "prtypes.h"
00043 #include "nsCoord.h"
00044 #include "nsDebug.h"
00045 
00046 typedef void *
00047 (* PR_CALLBACK IntervalSetAlloc)(size_t aBytes, void *aClosure);
00048 
00049 typedef void
00050 (* PR_CALLBACK IntervalSetFree) (size_t aBytes, void *aPtr, void *aClosure);
00051 
00052 /*
00053  * A list-based class (hopefully tree-based when I get around to it)
00054  * for representing a set of ranges on a number-line.
00055  */
00056 class nsIntervalSet {
00057 
00058 public:
00059 
00060     typedef nscoord coord_type;
00061 
00062     nsIntervalSet(IntervalSetAlloc aAlloc, IntervalSetFree aFree,
00063                   void* aAllocatorClosure);
00064     ~nsIntervalSet();
00065 
00066     /*
00067      * Include the interval [aBegin, aEnd] in the set.
00068      *
00069      * Removal of intervals added is not supported because that would
00070      * require keeping track of the individual intervals that were
00071      * added (nsIntervalMap should do that).  It would be simple to
00072      * implement ExcludeInterval if anyone wants it, though.
00073      */
00074     void IncludeInterval(coord_type aBegin, coord_type aEnd);
00075 
00076     /*
00077      * Is the point aPoint contained within the set of intervals?
00078      */
00079     PRBool HasPoint(coord_type aPoint) const;
00080 
00081     /*
00082      * Are _some_ points in [aBegin, aEnd] contained within the set
00083      * of intervals?
00084      */
00085     PRBool Intersects(coord_type aBegin, coord_type aEnd) const;
00086 
00087     /*
00088      * Are _all_ points in [aBegin, aEnd] contained within the set
00089      * of intervals?
00090      */
00091     PRBool Contains(coord_type aBegin, coord_type aEnd) const;
00092 
00093     PRBool IsEmpty() const
00094     {
00095         return !mList;
00096     }
00097 
00098 private:
00099 
00100     class Interval {
00101 
00102     public:
00103         Interval(coord_type aBegin, coord_type aEnd)
00104             : mBegin(aBegin),
00105               mEnd(aEnd),
00106               mPrev(nsnull),
00107               mNext(nsnull)
00108         {
00109         }
00110 
00111         coord_type mBegin;
00112         coord_type mEnd;
00113         Interval *mPrev;
00114         Interval *mNext;
00115     };
00116 
00117     void FreeInterval(Interval *aInterval);
00118 
00119     Interval           *mList;
00120     IntervalSetAlloc    mAlloc;
00121     IntervalSetFree     mFree;
00122     void               *mAllocatorClosure;
00123         
00124 };
00125 
00126 #endif // !defined(nsIntervalSet_h___)