2014-06-30 19:39:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1998-04-22 22:28:48 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* MODULE NOTES:
|
2002-02-20 06:08:32 +03:00
|
|
|
*
|
1998-04-22 22:28:48 +04:00
|
|
|
* The Deque is a very small, very efficient container object
|
2017-02-03 04:43:13 +03:00
|
|
|
* than can hold items of type void*, offering the following features:
|
|
|
|
* - Its interface supports pushing, popping, and peeking of items at the back
|
|
|
|
* or front, and retrieval from any position.
|
|
|
|
* - It can iterate over items via a ForEach method, range-for, or an iterator
|
|
|
|
* class.
|
|
|
|
* - When full, it can efficiently resize dynamically.
|
1998-04-22 22:28:48 +04:00
|
|
|
*
|
|
|
|
* NOTE: The only bit of trickery here is that this deque is
|
2002-02-20 06:08:32 +03:00
|
|
|
* built upon a ring-buffer. Like all ring buffers, the first
|
2017-02-03 04:43:13 +03:00
|
|
|
* item may not be at index[0]. The mOrigin member determines
|
2002-02-20 06:08:32 +03:00
|
|
|
* where the first child is. This point is quietly hidden from
|
1998-04-22 22:28:48 +04:00
|
|
|
* customers of this class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _NSDEQUE
|
|
|
|
#define _NSDEQUE
|
|
|
|
|
|
|
|
#include "nscore.h"
|
2013-05-29 03:57:54 +04:00
|
|
|
#include "nsDebug.h"
|
2012-09-21 22:27:27 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-05-29 03:57:54 +04:00
|
|
|
#include "mozilla/fallible.h"
|
2014-03-06 01:31:04 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
1998-04-22 22:28:48 +04:00
|
|
|
|
|
|
|
/**
|
2002-02-20 06:08:32 +03:00
|
|
|
* The nsDequeFunctor class is used when you want to create
|
1998-04-22 22:28:48 +04:00
|
|
|
* callbacks between the deque and your generic code.
|
2017-02-03 04:43:13 +03:00
|
|
|
* Use these objects in a call to ForEach(), and as custom deallocators.
|
1998-04-22 22:28:48 +04:00
|
|
|
*/
|
2014-06-27 05:35:39 +04:00
|
|
|
class nsDequeFunctor
|
|
|
|
{
|
1998-04-22 22:28:48 +04:00
|
|
|
public:
|
2017-02-09 06:06:36 +03:00
|
|
|
virtual void operator()(void* aObject) = 0;
|
2012-09-21 22:27:27 +04:00
|
|
|
virtual ~nsDequeFunctor() {}
|
1998-04-22 22:28:48 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* Here comes the nsDeque class itself...
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
/**
|
2002-02-20 06:08:32 +03:00
|
|
|
* The deque (double-ended queue) class is a common container type,
|
1998-04-22 22:28:48 +04:00
|
|
|
* whose behavior mimics a line in your favorite checkout stand.
|
2002-02-20 06:08:32 +03:00
|
|
|
* Classic CS describes the common behavior of a queue as FIFO.
|
|
|
|
* A deque allows insertion and removal at both ends of
|
|
|
|
* the container.
|
1998-04-22 22:28:48 +04:00
|
|
|
*
|
2002-02-20 06:08:32 +03:00
|
|
|
* The deque stores pointers to items.
|
1998-04-22 22:28:48 +04:00
|
|
|
*/
|
2014-08-28 02:47:27 +04:00
|
|
|
class nsDeque
|
2014-06-27 05:35:39 +04:00
|
|
|
{
|
2013-05-29 03:57:54 +04:00
|
|
|
typedef mozilla::fallible_t fallible_t;
|
2014-06-27 05:35:39 +04:00
|
|
|
public:
|
2017-02-03 04:43:13 +03:00
|
|
|
/**
|
|
|
|
* Constructs an empty deque.
|
|
|
|
*
|
|
|
|
* @param aDeallocator Optional deallocator functor that will be called from
|
|
|
|
* Erase() and the destructor on any remaining item.
|
|
|
|
* The deallocator is owned by the deque and will be
|
|
|
|
* deleted at destruction time.
|
|
|
|
*/
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsDeque(nsDequeFunctor* aDeallocator = nullptr);
|
2017-02-03 04:43:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deque destructor. Erases all items, deletes the deallocator.
|
|
|
|
*/
|
2002-02-20 06:08:32 +03:00
|
|
|
~nsDeque();
|
|
|
|
|
1998-04-22 22:28:48 +04:00
|
|
|
/**
|
2017-02-03 04:43:13 +03:00
|
|
|
* Returns the number of items currently stored in
|
1998-04-22 22:28:48 +04:00
|
|
|
* this deque.
|
|
|
|
*
|
2017-02-03 04:43:13 +03:00
|
|
|
* @return number of items currently in the deque
|
1998-04-22 22:28:48 +04:00
|
|
|
*/
|
2015-11-15 16:48:08 +03:00
|
|
|
inline size_t GetSize() const { return mSize; }
|
1998-04-22 22:28:48 +04:00
|
|
|
|
|
|
|
/**
|
2002-02-20 06:08:32 +03:00
|
|
|
* Appends new member at the end of the deque.
|
1998-04-22 22:28:48 +04:00
|
|
|
*
|
2017-02-03 04:43:13 +03:00
|
|
|
* @param aItem item to store in deque
|
1998-04-22 22:28:48 +04:00
|
|
|
*/
|
2014-06-27 05:35:39 +04:00
|
|
|
void Push(void* aItem)
|
|
|
|
{
|
2015-01-28 12:00:40 +03:00
|
|
|
if (!Push(aItem, mozilla::fallible)) {
|
2014-11-12 11:13:44 +03:00
|
|
|
NS_ABORT_OOM(mSize * sizeof(void*));
|
2013-05-29 03:57:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-03 04:43:13 +03:00
|
|
|
/**
|
|
|
|
* Appends new member at the end of the deque.
|
|
|
|
*
|
|
|
|
* @param aItem item to store in deque
|
|
|
|
* @return true if succeeded, false if failed to resize deque as needed
|
|
|
|
*/
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE bool Push(void* aItem, const fallible_t&);
|
1999-01-09 04:09:39 +03:00
|
|
|
|
|
|
|
/**
|
2002-02-20 06:08:32 +03:00
|
|
|
* Inserts new member at the front of the deque.
|
1999-01-09 04:09:39 +03:00
|
|
|
*
|
2017-02-03 04:43:13 +03:00
|
|
|
* @param aItem item to store in deque
|
1999-01-09 04:09:39 +03:00
|
|
|
*/
|
2014-06-27 05:35:39 +04:00
|
|
|
void PushFront(void* aItem)
|
|
|
|
{
|
2015-01-28 12:00:40 +03:00
|
|
|
if (!PushFront(aItem, mozilla::fallible)) {
|
2014-11-12 11:13:44 +03:00
|
|
|
NS_ABORT_OOM(mSize * sizeof(void*));
|
2013-05-29 03:57:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-03 04:43:13 +03:00
|
|
|
/**
|
|
|
|
* Inserts new member at the front of the deque.
|
|
|
|
*
|
|
|
|
* @param aItem item to store in deque
|
|
|
|
* @return true if succeeded, false if failed to resize deque as needed
|
|
|
|
*/
|
2016-04-27 07:16:50 +03:00
|
|
|
MOZ_MUST_USE bool PushFront(void* aItem, const fallible_t&);
|
2002-02-20 06:08:32 +03:00
|
|
|
|
1998-04-22 22:28:48 +04:00
|
|
|
/**
|
2002-02-20 06:08:32 +03:00
|
|
|
* Remove and return the last item in the container.
|
|
|
|
*
|
|
|
|
* @return the item that was the last item in container
|
1998-04-22 22:28:48 +04:00
|
|
|
*/
|
2002-02-20 06:08:32 +03:00
|
|
|
void* Pop();
|
1998-08-05 05:59:34 +04:00
|
|
|
|
2002-02-20 06:08:32 +03:00
|
|
|
/**
|
1999-01-09 04:09:39 +03:00
|
|
|
* Remove and return the first item in the container.
|
2002-02-20 06:08:32 +03:00
|
|
|
*
|
|
|
|
* @return the item that was first item in container
|
1998-08-05 05:59:34 +04:00
|
|
|
*/
|
2002-02-20 06:08:32 +03:00
|
|
|
void* PopFront();
|
1999-01-09 04:09:39 +03:00
|
|
|
|
1999-11-27 20:47:05 +03:00
|
|
|
/**
|
2017-02-03 04:43:13 +03:00
|
|
|
* Retrieve the last item without removing it.
|
2002-02-20 06:08:32 +03:00
|
|
|
*
|
2017-02-03 04:43:13 +03:00
|
|
|
* @return the last item in container
|
2002-02-20 06:08:32 +03:00
|
|
|
*/
|
2016-04-25 05:13:55 +03:00
|
|
|
void* Peek() const;
|
2017-02-03 04:43:13 +03:00
|
|
|
|
1998-04-22 22:28:48 +04:00
|
|
|
/**
|
2017-02-03 04:43:13 +03:00
|
|
|
* Retrieve the first item without removing it.
|
2002-02-20 06:08:32 +03:00
|
|
|
*
|
|
|
|
* @return the first item in container
|
1998-04-22 22:28:48 +04:00
|
|
|
*/
|
2016-04-25 05:13:55 +03:00
|
|
|
void* PeekFront() const;
|
2002-02-20 06:08:32 +03:00
|
|
|
|
1999-01-15 22:20:51 +03:00
|
|
|
/**
|
2014-06-27 05:35:39 +04:00
|
|
|
* Retrieve a member from the deque without removing it.
|
1999-01-15 22:20:51 +03:00
|
|
|
*
|
|
|
|
* @param index of desired item
|
2017-02-03 04:43:13 +03:00
|
|
|
* @return item in list, or nullptr if index is outside the deque
|
1999-01-15 22:20:51 +03:00
|
|
|
*/
|
2015-11-15 16:48:08 +03:00
|
|
|
void* ObjectAt(size_t aIndex) const;
|
2002-02-20 06:08:32 +03:00
|
|
|
|
1998-04-22 22:28:48 +04:00
|
|
|
/**
|
2002-02-20 06:08:32 +03:00
|
|
|
* Remove and delete all items from container.
|
|
|
|
* Deletes are handled by the deallocator nsDequeFunctor
|
|
|
|
* which is specified at deque construction.
|
1998-04-22 22:28:48 +04:00
|
|
|
*/
|
2013-05-29 03:57:54 +04:00
|
|
|
void Erase();
|
1998-04-22 22:28:48 +04:00
|
|
|
|
|
|
|
/**
|
2017-02-03 04:43:13 +03:00
|
|
|
* Call this method when you want to iterate through all
|
|
|
|
* items in the container, passing a functor along
|
1998-04-22 22:28:48 +04:00
|
|
|
* to call your code.
|
2017-02-03 01:19:47 +03:00
|
|
|
* If the deque is modified during ForEach, iteration will continue based on
|
|
|
|
* item indices; meaning that front operations may effectively skip over
|
|
|
|
* items or visit some items multiple times.
|
1998-04-22 22:28:48 +04:00
|
|
|
*
|
|
|
|
* @param aFunctor object to call for each member
|
|
|
|
*/
|
1998-08-27 06:39:36 +04:00
|
|
|
void ForEach(nsDequeFunctor& aFunctor) const;
|
1998-08-05 05:59:34 +04:00
|
|
|
|
2017-02-03 05:08:10 +03:00
|
|
|
// This iterator assumes that the deque itself is const, i.e., it cannot be
|
|
|
|
// modified while the iterator is used.
|
|
|
|
// Also it is a 'const' iterator in that it provides copies of the deque's
|
|
|
|
// elements, and therefore it is not possible to modify the deque's contents
|
|
|
|
// by assigning to a dereference of this iterator.
|
|
|
|
class ConstDequeIterator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ConstDequeIterator(const nsDeque& aDeque, size_t aIndex)
|
|
|
|
: mDeque(aDeque)
|
|
|
|
, mIndex(aIndex)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
ConstDequeIterator& operator++()
|
|
|
|
{
|
|
|
|
++mIndex;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
bool operator==(const ConstDequeIterator& aOther) const
|
|
|
|
{
|
|
|
|
return mIndex == aOther.mIndex;
|
|
|
|
}
|
|
|
|
bool operator!=(const ConstDequeIterator& aOther) const
|
|
|
|
{
|
|
|
|
return mIndex != aOther.mIndex;
|
|
|
|
}
|
|
|
|
void* operator*() const
|
|
|
|
{
|
|
|
|
// Don't allow out-of-deque dereferences.
|
|
|
|
MOZ_RELEASE_ASSERT(mIndex < mDeque.GetSize());
|
|
|
|
return mDeque.ObjectAt(mIndex);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
const nsDeque& mDeque;
|
|
|
|
size_t mIndex;
|
|
|
|
};
|
|
|
|
// If this deque is const, we can provide ConstDequeIterator's.
|
|
|
|
ConstDequeIterator begin() const
|
|
|
|
{
|
|
|
|
return ConstDequeIterator(*this, 0);
|
|
|
|
}
|
|
|
|
ConstDequeIterator end() const
|
|
|
|
{
|
|
|
|
return ConstDequeIterator(*this, mSize);
|
|
|
|
}
|
|
|
|
|
2017-02-03 01:19:47 +03:00
|
|
|
// It is a 'const' iterator in that it provides copies of the deque's
|
|
|
|
// elements, and therefore it is not possible to modify the deque's contents
|
|
|
|
// by assigning to a dereference of this iterator.
|
|
|
|
// If the deque is modified in other ways, this iterator will stay at the same
|
|
|
|
// index, and will handle past-the-end comparisons, but not dereferencing.
|
2017-02-02 04:28:35 +03:00
|
|
|
class ConstIterator
|
|
|
|
{
|
|
|
|
public:
|
2017-02-03 01:19:47 +03:00
|
|
|
// Special index for the end iterator, to track the possibly-shifting
|
|
|
|
// deque size.
|
|
|
|
static const size_t EndIteratorIndex = size_t(-1);
|
|
|
|
|
|
|
|
ConstIterator(const nsDeque& aDeque, size_t aIndex)
|
|
|
|
: mDeque(aDeque)
|
|
|
|
, mIndex(aIndex)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
ConstIterator& operator++()
|
|
|
|
{
|
|
|
|
// End-iterator shouldn't be modified.
|
|
|
|
MOZ_ASSERT(mIndex != EndIteratorIndex);
|
|
|
|
++mIndex;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
bool operator==(const ConstIterator& aOther) const
|
|
|
|
{
|
|
|
|
return EffectiveIndex() == aOther.EffectiveIndex();
|
|
|
|
}
|
|
|
|
bool operator!=(const ConstIterator& aOther) const
|
|
|
|
{
|
|
|
|
return EffectiveIndex() != aOther.EffectiveIndex();
|
|
|
|
}
|
|
|
|
void* operator*() const
|
|
|
|
{
|
|
|
|
// Don't allow out-of-deque dereferences.
|
|
|
|
MOZ_RELEASE_ASSERT(mIndex < mDeque.GetSize());
|
|
|
|
return mDeque.ObjectAt(mIndex);
|
|
|
|
}
|
2017-02-02 04:28:35 +03:00
|
|
|
private:
|
2017-02-03 01:19:47 +03:00
|
|
|
// 0 <= index < deque.GetSize() inside the deque, deque.GetSize() otherwise.
|
|
|
|
// Only used when comparing indices, not to actually access items.
|
|
|
|
size_t EffectiveIndex() const
|
|
|
|
{
|
|
|
|
return (mIndex < mDeque.GetSize()) ? mIndex : mDeque.GetSize();
|
|
|
|
}
|
|
|
|
|
2017-02-02 04:28:35 +03:00
|
|
|
const nsDeque& mDeque;
|
2017-02-03 01:19:47 +03:00
|
|
|
size_t mIndex; // May point outside the deque!
|
2017-02-02 04:28:35 +03:00
|
|
|
};
|
2017-02-03 05:08:10 +03:00
|
|
|
// If this deque is *not* const, we provide ConstIterator's that can handle
|
|
|
|
// deque size changes.
|
|
|
|
ConstIterator begin()
|
2017-02-03 01:19:47 +03:00
|
|
|
{
|
|
|
|
return ConstIterator(*this, 0);
|
|
|
|
}
|
2017-02-03 05:08:10 +03:00
|
|
|
ConstIterator end()
|
2017-02-03 01:19:47 +03:00
|
|
|
{
|
|
|
|
return ConstIterator(*this, ConstIterator::EndIteratorIndex);
|
|
|
|
}
|
2017-02-02 04:28:35 +03:00
|
|
|
|
2014-03-06 01:31:04 +04:00
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
|
|
|
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
|
|
|
|
1998-04-22 22:28:48 +04:00
|
|
|
protected:
|
2015-11-15 16:48:08 +03:00
|
|
|
size_t mSize;
|
|
|
|
size_t mCapacity;
|
|
|
|
size_t mOrigin;
|
1999-02-26 09:33:54 +03:00
|
|
|
nsDequeFunctor* mDeallocator;
|
1999-07-16 21:31:00 +04:00
|
|
|
void* mBuffer[8];
|
1998-06-18 03:13:28 +04:00
|
|
|
void** mData;
|
|
|
|
|
2002-02-20 06:08:32 +03:00
|
|
|
private:
|
1998-04-22 22:28:48 +04:00
|
|
|
|
|
|
|
/**
|
2017-02-03 04:43:13 +03:00
|
|
|
* Copy constructor (deleted)
|
2002-02-20 06:08:32 +03:00
|
|
|
*
|
2014-06-27 05:35:39 +04:00
|
|
|
* @param aOther another deque
|
1998-04-22 22:28:48 +04:00
|
|
|
*/
|
2017-02-03 04:43:13 +03:00
|
|
|
nsDeque(const nsDeque& aOther) = delete;
|
1998-04-22 22:28:48 +04:00
|
|
|
|
|
|
|
/**
|
2017-02-03 04:43:13 +03:00
|
|
|
* Deque assignment operator (deleted)
|
1998-04-22 22:28:48 +04:00
|
|
|
*
|
2014-06-27 05:35:39 +04:00
|
|
|
* @param aOther another deque
|
|
|
|
* @return *this
|
1998-04-22 22:28:48 +04:00
|
|
|
*/
|
2017-02-03 04:43:13 +03:00
|
|
|
nsDeque& operator=(const nsDeque& aOther) = delete;
|
1998-04-22 22:28:48 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool GrowCapacity();
|
2015-11-15 16:48:02 +03:00
|
|
|
void SetDeallocator(nsDequeFunctor* aDeallocator);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all items from container without destroying them.
|
|
|
|
*/
|
|
|
|
void Empty();
|
1998-04-22 22:28:48 +04:00
|
|
|
};
|
|
|
|
#endif
|