1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/layout/generic/nsIntervalSet.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,88 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +// vim:cindent:ts=8:et:sw=4: 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +/* a set of ranges on a number-line */ 1.11 + 1.12 +#include "nsIntervalSet.h" 1.13 +#include <new> 1.14 +#include <algorithm> 1.15 + 1.16 +nsIntervalSet::nsIntervalSet(IntervalSetAlloc aAlloc, IntervalSetFree aFree, 1.17 + void* aAllocatorClosure) 1.18 + : mList(nullptr), 1.19 + mAlloc(aAlloc), 1.20 + mFree(aFree), 1.21 + mAllocatorClosure(aAllocatorClosure) 1.22 +{ 1.23 + NS_ASSERTION(mAlloc && mFree, "null callback params"); 1.24 +} 1.25 + 1.26 +nsIntervalSet::~nsIntervalSet() 1.27 +{ 1.28 + Interval *current = mList; 1.29 + while (current) { 1.30 + Interval *trash = current; 1.31 + current = current->mNext; 1.32 + FreeInterval(trash); 1.33 + } 1.34 +} 1.35 + 1.36 +void nsIntervalSet::FreeInterval(nsIntervalSet::Interval *aInterval) 1.37 +{ 1.38 + NS_ASSERTION(aInterval, "null interval"); 1.39 + 1.40 + aInterval->Interval::~Interval(); 1.41 + (*mFree)(sizeof(Interval), aInterval, mAllocatorClosure); 1.42 +} 1.43 + 1.44 +void nsIntervalSet::IncludeInterval(coord_type aBegin, coord_type aEnd) 1.45 +{ 1.46 + Interval *newInterval = static_cast<Interval*> 1.47 + ((*mAlloc)(sizeof(Interval), mAllocatorClosure)); 1.48 + if (!newInterval) { 1.49 + NS_NOTREACHED("allocation failure"); 1.50 + return; 1.51 + } 1.52 + new(newInterval) Interval(aBegin, aEnd); 1.53 + 1.54 + Interval **current = &mList; 1.55 + while (*current && (*current)->mEnd < aBegin) 1.56 + current = &(*current)->mNext; 1.57 + 1.58 + newInterval->mNext = *current; 1.59 + *current = newInterval; 1.60 + 1.61 + Interval *subsumed = newInterval->mNext; 1.62 + while (subsumed && subsumed->mBegin <= aEnd) { 1.63 + newInterval->mBegin = std::min(newInterval->mBegin, subsumed->mBegin); 1.64 + newInterval->mEnd = std::max(newInterval->mEnd, subsumed->mEnd); 1.65 + newInterval->mNext = subsumed->mNext; 1.66 + FreeInterval(subsumed); 1.67 + subsumed = newInterval->mNext; 1.68 + } 1.69 +} 1.70 + 1.71 +bool nsIntervalSet::Intersects(coord_type aBegin, coord_type aEnd) const 1.72 +{ 1.73 + Interval *current = mList; 1.74 + while (current && current->mBegin <= aEnd) { 1.75 + if (current->mEnd >= aBegin) 1.76 + return true; 1.77 + current = current->mNext; 1.78 + } 1.79 + return false; 1.80 +} 1.81 + 1.82 +bool nsIntervalSet::Contains(coord_type aBegin, coord_type aEnd) const 1.83 +{ 1.84 + Interval *current = mList; 1.85 + while (current && current->mBegin <= aBegin) { 1.86 + if (current->mEnd >= aEnd) 1.87 + return true; 1.88 + current = current->mNext; 1.89 + } 1.90 + return false; 1.91 +}