michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: // vim:cindent:ts=8:et:sw=4: michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* a set of ranges on a number-line */ michael@0: michael@0: #include "nsIntervalSet.h" michael@0: #include michael@0: #include michael@0: michael@0: nsIntervalSet::nsIntervalSet(IntervalSetAlloc aAlloc, IntervalSetFree aFree, michael@0: void* aAllocatorClosure) michael@0: : mList(nullptr), michael@0: mAlloc(aAlloc), michael@0: mFree(aFree), michael@0: mAllocatorClosure(aAllocatorClosure) michael@0: { michael@0: NS_ASSERTION(mAlloc && mFree, "null callback params"); michael@0: } michael@0: michael@0: nsIntervalSet::~nsIntervalSet() michael@0: { michael@0: Interval *current = mList; michael@0: while (current) { michael@0: Interval *trash = current; michael@0: current = current->mNext; michael@0: FreeInterval(trash); michael@0: } michael@0: } michael@0: michael@0: void nsIntervalSet::FreeInterval(nsIntervalSet::Interval *aInterval) michael@0: { michael@0: NS_ASSERTION(aInterval, "null interval"); michael@0: michael@0: aInterval->Interval::~Interval(); michael@0: (*mFree)(sizeof(Interval), aInterval, mAllocatorClosure); michael@0: } michael@0: michael@0: void nsIntervalSet::IncludeInterval(coord_type aBegin, coord_type aEnd) michael@0: { michael@0: Interval *newInterval = static_cast michael@0: ((*mAlloc)(sizeof(Interval), mAllocatorClosure)); michael@0: if (!newInterval) { michael@0: NS_NOTREACHED("allocation failure"); michael@0: return; michael@0: } michael@0: new(newInterval) Interval(aBegin, aEnd); michael@0: michael@0: Interval **current = &mList; michael@0: while (*current && (*current)->mEnd < aBegin) michael@0: current = &(*current)->mNext; michael@0: michael@0: newInterval->mNext = *current; michael@0: *current = newInterval; michael@0: michael@0: Interval *subsumed = newInterval->mNext; michael@0: while (subsumed && subsumed->mBegin <= aEnd) { michael@0: newInterval->mBegin = std::min(newInterval->mBegin, subsumed->mBegin); michael@0: newInterval->mEnd = std::max(newInterval->mEnd, subsumed->mEnd); michael@0: newInterval->mNext = subsumed->mNext; michael@0: FreeInterval(subsumed); michael@0: subsumed = newInterval->mNext; michael@0: } michael@0: } michael@0: michael@0: bool nsIntervalSet::Intersects(coord_type aBegin, coord_type aEnd) const michael@0: { michael@0: Interval *current = mList; michael@0: while (current && current->mBegin <= aEnd) { michael@0: if (current->mEnd >= aBegin) michael@0: return true; michael@0: current = current->mNext; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: bool nsIntervalSet::Contains(coord_type aBegin, coord_type aEnd) const michael@0: { michael@0: Interval *current = mList; michael@0: while (current && current->mBegin <= aBegin) { michael@0: if (current->mEnd >= aEnd) michael@0: return true; michael@0: current = current->mNext; michael@0: } michael@0: return false; michael@0: }