1998-04-22 18:28:48 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2001-09-28 20:14:13 +00:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 14:21:17 +00:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1998-04-22 18:28:48 +00:00
|
|
|
*
|
2004-04-18 14:21:17 +00:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
2001-09-28 20:14:13 +00:00
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1998-04-22 18:28:48 +00:00
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2002-02-20 03:08:32 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-28 20:14:13 +00:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 03:43:54 +00:00
|
|
|
*
|
2001-09-28 20:14:13 +00:00
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-18 14:21:17 +00:00
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
2001-09-28 20:14:13 +00:00
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-18 14:21:17 +00:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-28 20:14:13 +00:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-18 14:21:17 +00:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-28 20:14:13 +00:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1998-04-22 18:28:48 +00:00
|
|
|
|
|
|
|
#include "nsDeque.h"
|
1998-05-21 02:27:34 +00:00
|
|
|
#include "nsCRT.h"
|
2002-02-20 03:08:32 +00:00
|
|
|
#ifdef DEBUG_rickg
|
1999-07-16 17:31:00 +00:00
|
|
|
#include <stdio.h>
|
2002-02-20 03:08:32 +00:00
|
|
|
#endif
|
1998-04-22 18:28:48 +00:00
|
|
|
|
2002-02-20 03:08:32 +00:00
|
|
|
/**
|
|
|
|
* 07/02/2001 09:17p 509,104 clangref.pdf from openwatcom's site
|
|
|
|
* Watcom C Language Reference Edition 11.0c
|
|
|
|
* page 118 of 297
|
|
|
|
*
|
|
|
|
* The % symbol yields the remainder from the division of the first operand
|
|
|
|
* by the second operand. The operands of % must have integral type.
|
|
|
|
*
|
|
|
|
* When both operands of % are positive, the result is a positive value
|
|
|
|
* smaller than the second operand. When one or both operands is negative,
|
|
|
|
* whether the result is positive or negative is implementation-defined.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/* Ok, so first of all, C is underspecified. joy.
|
|
|
|
* The following functions do not provide a correct implementation of modulus
|
|
|
|
* They provide functionality for x>-y.
|
|
|
|
* There are risks of 2*y being greater than max int, which is part of the
|
|
|
|
* reason no multiplication is used and other operations are avoided.
|
|
|
|
*
|
|
|
|
* modasgn
|
|
|
|
* @param x variable
|
|
|
|
* @param y expression
|
|
|
|
* approximately equivalent to x %= y
|
|
|
|
*
|
|
|
|
* modulus
|
|
|
|
* @param x expression
|
|
|
|
* @param y expression
|
|
|
|
* approximately equivalent to x % y
|
|
|
|
*/
|
|
|
|
#define modasgn(x,y) if (x<0) x+=y; x%=y
|
|
|
|
#define modulus(x,y) ((x<0)?(x+y)%(y):(x)%(y))
|
1998-08-21 02:03:56 +00:00
|
|
|
|
1998-04-22 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* Standard constructor
|
2002-02-20 03:08:32 +00:00
|
|
|
* @param deallocator, called by Erase and ~nsDeque
|
1998-04-22 18:28:48 +00:00
|
|
|
*/
|
1999-02-26 06:33:54 +00:00
|
|
|
nsDeque::nsDeque(nsDequeFunctor* aDeallocator) {
|
2001-01-10 04:04:57 +00:00
|
|
|
MOZ_COUNT_CTOR(nsDeque);
|
1999-02-26 06:33:54 +00:00
|
|
|
mDeallocator=aDeallocator;
|
1998-04-22 18:28:48 +00:00
|
|
|
mOrigin=mSize=0;
|
1999-07-16 17:31:00 +00:00
|
|
|
mData=mBuffer; // don't allocate space until you must
|
|
|
|
mCapacity=sizeof(mBuffer)/sizeof(mBuffer[0]);
|
2002-01-30 02:55:57 +00:00
|
|
|
memset(mData, 0, mCapacity*sizeof(mBuffer[0]));
|
1998-04-22 18:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor
|
|
|
|
*/
|
|
|
|
nsDeque::~nsDeque() {
|
2001-01-10 04:04:57 +00:00
|
|
|
MOZ_COUNT_DTOR(nsDeque);
|
1999-07-25 17:18:41 +00:00
|
|
|
|
2002-02-20 03:08:32 +00:00
|
|
|
#ifdef DEBUG_rickg
|
1999-07-25 17:18:41 +00:00
|
|
|
char buffer[30];
|
2002-02-20 03:08:32 +00:00
|
|
|
printf("Capacity: %i\n", mCapacity);
|
1999-07-16 17:31:00 +00:00
|
|
|
|
|
|
|
static int mCaps[15] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
|
|
|
|
switch(mCapacity) {
|
|
|
|
case 4: mCaps[0]++; break;
|
|
|
|
case 8: mCaps[1]++; break;
|
|
|
|
case 16: mCaps[2]++; break;
|
|
|
|
case 32: mCaps[3]++; break;
|
|
|
|
case 64: mCaps[4]++; break;
|
|
|
|
case 128: mCaps[5]++; break;
|
|
|
|
case 256: mCaps[6]++; break;
|
|
|
|
case 512: mCaps[7]++; break;
|
|
|
|
case 1024: mCaps[8]++; break;
|
|
|
|
case 2048: mCaps[9]++; break;
|
|
|
|
case 4096: mCaps[10]++; break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
1999-07-25 17:18:41 +00:00
|
|
|
#endif
|
1999-07-16 17:31:00 +00:00
|
|
|
|
1998-06-17 23:13:28 +00:00
|
|
|
Erase();
|
2002-02-20 03:08:32 +00:00
|
|
|
if (mData && (mData!=mBuffer)) {
|
2009-11-15 00:45:42 +00:00
|
|
|
free(mData);
|
1999-02-26 06:33:54 +00:00
|
|
|
}
|
2002-02-20 03:08:32 +00:00
|
|
|
mData=0;
|
|
|
|
SetDeallocator(0);
|
1998-04-22 18:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2002-02-20 03:08:32 +00:00
|
|
|
* Set the functor to be called by Erase()
|
|
|
|
* The deque owns the functor.
|
1998-04-22 18:28:48 +00:00
|
|
|
*
|
2002-02-20 03:08:32 +00:00
|
|
|
* @param aDeallocator functor object for use by Erase()
|
|
|
|
*/
|
1999-02-26 07:23:56 +00:00
|
|
|
void nsDeque::SetDeallocator(nsDequeFunctor* aDeallocator){
|
2002-02-20 03:08:32 +00:00
|
|
|
if (mDeallocator) {
|
1999-02-26 07:23:56 +00:00
|
|
|
delete mDeallocator;
|
|
|
|
}
|
|
|
|
mDeallocator=aDeallocator;
|
|
|
|
}
|
|
|
|
|
1998-04-22 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* Remove all items from container without destroying them.
|
|
|
|
*
|
2002-02-20 03:08:32 +00:00
|
|
|
* @return *this
|
1998-04-22 18:28:48 +00:00
|
|
|
*/
|
|
|
|
nsDeque& nsDeque::Empty() {
|
2002-02-20 03:08:32 +00:00
|
|
|
if (mSize && mData) {
|
2002-01-30 02:55:57 +00:00
|
|
|
memset(mData, 0, mCapacity*sizeof(mData));
|
1999-02-16 06:47:00 +00:00
|
|
|
}
|
1998-04-22 18:28:48 +00:00
|
|
|
mSize=0;
|
|
|
|
mOrigin=0;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove and delete all items from container
|
2002-02-20 03:08:32 +00:00
|
|
|
*
|
|
|
|
* @return *this
|
1998-04-22 18:28:48 +00:00
|
|
|
*/
|
2002-02-20 03:08:32 +00:00
|
|
|
nsDeque& nsDeque::Erase() {
|
|
|
|
if (mDeallocator && mSize) {
|
1999-02-26 06:33:54 +00:00
|
|
|
ForEach(*mDeallocator);
|
|
|
|
}
|
1998-04-22 18:28:48 +00:00
|
|
|
return Empty();
|
|
|
|
}
|
|
|
|
|
1999-03-31 08:42:06 +00:00
|
|
|
/**
|
2002-02-20 03:08:32 +00:00
|
|
|
* This method quadruples the size of the deque
|
|
|
|
* Elements in the deque are resequenced so that elements
|
|
|
|
* in the deque are stored sequentially
|
|
|
|
*
|
|
|
|
* If the deque actually overflows, there's very little we can do.
|
2002-03-06 03:21:17 +00:00
|
|
|
* Perhaps this function should return PRBool/nsresult indicating success/failure.
|
1999-03-31 08:42:06 +00:00
|
|
|
*
|
2009-10-30 21:31:41 +00:00
|
|
|
* @return whether growing succeeded
|
1999-03-31 08:42:06 +00:00
|
|
|
*/
|
2009-10-08 03:22:42 +00:00
|
|
|
PRBool nsDeque::GrowCapacity() {
|
2002-02-20 03:08:32 +00:00
|
|
|
PRInt32 theNewSize=mCapacity<<2;
|
|
|
|
NS_ASSERTION(theNewSize>mCapacity, "Overflow");
|
|
|
|
if (theNewSize<=mCapacity)
|
2009-10-08 03:22:42 +00:00
|
|
|
return PR_FALSE;
|
2009-11-15 00:45:42 +00:00
|
|
|
void** temp=(void**)malloc(theNewSize * sizeof(void*));
|
2009-10-08 03:22:42 +00:00
|
|
|
if (!temp)
|
|
|
|
return PR_FALSE;
|
1999-03-31 08:42:06 +00:00
|
|
|
|
|
|
|
//Here's the interesting part: You can't just move the elements
|
2002-02-20 03:08:32 +00:00
|
|
|
//directly (in situ) from the old buffer to the new one.
|
1999-03-31 08:42:06 +00:00
|
|
|
//Since capacity has changed, the old origin doesn't make
|
|
|
|
//sense anymore. It's better to resequence the elements now.
|
1999-07-16 17:31:00 +00:00
|
|
|
|
2009-10-08 03:22:42 +00:00
|
|
|
memcpy(temp, mData + mOrigin, sizeof(void*) * (mCapacity - mOrigin));
|
|
|
|
memcpy(temp + (mCapacity - mOrigin), mData, sizeof(void*) * mOrigin);
|
|
|
|
|
|
|
|
if (mData != mBuffer) {
|
2009-11-15 00:45:42 +00:00
|
|
|
free(mData);
|
1999-07-16 17:31:00 +00:00
|
|
|
}
|
2009-10-08 03:22:42 +00:00
|
|
|
|
|
|
|
mCapacity=theNewSize;
|
|
|
|
mOrigin=0; //now realign the origin...
|
|
|
|
mData=temp;
|
|
|
|
|
|
|
|
return PR_TRUE;
|
1999-03-31 08:42:06 +00:00
|
|
|
}
|
|
|
|
|
1998-04-22 18:28:48 +00:00
|
|
|
/**
|
2002-02-20 03:08:32 +00:00
|
|
|
* This method adds an item to the end of the deque.
|
|
|
|
* This operation has the potential to cause the
|
1998-04-22 18:28:48 +00:00
|
|
|
* underlying buffer to resize.
|
|
|
|
*
|
2002-02-20 03:08:32 +00:00
|
|
|
* @param aItem: new item to be added to deque
|
|
|
|
* @return *this
|
1998-04-22 18:28:48 +00:00
|
|
|
*/
|
2002-02-20 03:08:32 +00:00
|
|
|
nsDeque& nsDeque::Push(void* aItem) {
|
2009-10-08 03:22:42 +00:00
|
|
|
if (mSize==mCapacity && !GrowCapacity()) {
|
|
|
|
NS_WARNING("out of memory");
|
|
|
|
return *this;
|
1998-04-22 18:28:48 +00:00
|
|
|
}
|
2002-02-20 03:08:32 +00:00
|
|
|
mData[modulus(mOrigin + mSize, mCapacity)]=aItem;
|
1998-04-22 18:28:48 +00:00
|
|
|
mSize++;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
1999-01-09 01:09:39 +00:00
|
|
|
/**
|
2002-02-20 03:08:32 +00:00
|
|
|
* This method adds an item to the front of the deque.
|
|
|
|
* This operation has the potential to cause the
|
1999-01-09 01:09:39 +00:00
|
|
|
* underlying buffer to resize.
|
|
|
|
*
|
2002-03-06 03:21:17 +00:00
|
|
|
* --Commments for GrowCapacity() case
|
2002-02-20 03:08:32 +00:00
|
|
|
* We've grown and shifted which means that the old
|
|
|
|
* final element in the deque is now the first element
|
|
|
|
* in the deque. This is temporary.
|
|
|
|
* We haven't inserted the new element at the front.
|
|
|
|
*
|
|
|
|
* To continue with the idea of having the front at zero
|
|
|
|
* after a grow, we move the old final item (which through
|
|
|
|
* the voodoo of mOrigin-- is now the first) to its final
|
|
|
|
* position which is conveniently the old length.
|
|
|
|
*
|
|
|
|
* Note that this case only happens when the deque is full.
|
|
|
|
* [And that pieces of this magic only work if the deque is full.]
|
|
|
|
* picture:
|
|
|
|
* [ABCDEFGH] @[mOrigin:3]:D.
|
|
|
|
* Task: PushFront("Z")
|
|
|
|
* shift mOrigin so, @[mOrigin:2]:C
|
|
|
|
* stretch and rearrange: (mOrigin:0)
|
|
|
|
* [CDEFGHAB ________ ________ ________]
|
|
|
|
* copy: (The second C is currently out of bounds)
|
|
|
|
* [CDEFGHAB C_______ ________ ________]
|
|
|
|
* later we will insert Z:
|
|
|
|
* [ZDEFGHAB C_______ ________ ________]
|
|
|
|
* and increment size: 9. (C is no longer out of bounds)
|
|
|
|
* --
|
|
|
|
* @param aItem: new item to be added to deque
|
|
|
|
* @return *this
|
1999-01-09 01:09:39 +00:00
|
|
|
*/
|
2002-02-20 03:08:32 +00:00
|
|
|
nsDeque& nsDeque::PushFront(void* aItem) {
|
|
|
|
mOrigin--;
|
|
|
|
modasgn(mOrigin,mCapacity);
|
|
|
|
if (mSize==mCapacity) {
|
2009-10-08 03:22:42 +00:00
|
|
|
if (!GrowCapacity()) {
|
|
|
|
NS_WARNING("out of memory");
|
|
|
|
return *this;
|
|
|
|
}
|
2002-02-20 03:08:32 +00:00
|
|
|
/* Comments explaining this are above*/
|
|
|
|
mData[mSize]=mData[mOrigin];
|
1999-03-31 08:42:06 +00:00
|
|
|
}
|
2002-02-20 03:08:32 +00:00
|
|
|
mData[mOrigin]=aItem;
|
1999-04-07 21:05:19 +00:00
|
|
|
mSize++;
|
1999-01-09 01:09:39 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove and return the last item in the container.
|
2002-02-20 03:08:32 +00:00
|
|
|
*
|
1999-01-09 01:09:39 +00:00
|
|
|
* @return ptr to last item in container
|
|
|
|
*/
|
2002-02-20 03:08:32 +00:00
|
|
|
void* nsDeque::Pop() {
|
1999-01-09 01:09:39 +00:00
|
|
|
void* result=0;
|
2002-02-20 03:08:32 +00:00
|
|
|
if (mSize>0) {
|
|
|
|
--mSize;
|
|
|
|
PRInt32 offset=modulus(mSize + mOrigin, mCapacity);
|
1999-01-09 01:09:39 +00:00
|
|
|
result=mData[offset];
|
|
|
|
mData[offset]=0;
|
2002-02-20 03:08:32 +00:00
|
|
|
if (!mSize) {
|
1999-01-09 01:09:39 +00:00
|
|
|
mOrigin=0;
|
2002-02-20 03:08:32 +00:00
|
|
|
}
|
1999-01-09 01:09:39 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1998-04-22 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* This method gets called you want to remove and return
|
|
|
|
* the first member in the container.
|
|
|
|
*
|
|
|
|
* @return last item in container
|
|
|
|
*/
|
1999-01-09 01:09:39 +00:00
|
|
|
void* nsDeque::PopFront() {
|
1998-04-22 18:28:48 +00:00
|
|
|
void* result=0;
|
2002-02-20 03:08:32 +00:00
|
|
|
if (mSize>0) {
|
|
|
|
NS_ASSERTION(mOrigin < mCapacity, "Error: Bad origin");
|
1998-04-22 18:28:48 +00:00
|
|
|
result=mData[mOrigin];
|
|
|
|
mData[mOrigin++]=0; //zero it out for debugging purposes.
|
|
|
|
mSize--;
|
2002-02-20 03:08:32 +00:00
|
|
|
// Cycle around if we pop off the end
|
|
|
|
// and reset origin if when we pop the last element
|
|
|
|
if (mCapacity==mOrigin || !mSize) {
|
1998-04-22 18:28:48 +00:00
|
|
|
mOrigin=0;
|
2002-02-20 03:08:32 +00:00
|
|
|
}
|
1998-04-22 18:28:48 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2002-02-20 03:08:32 +00:00
|
|
|
|
1999-11-27 17:47:05 +00:00
|
|
|
/**
|
|
|
|
* This method gets called you want to peek at the bottom
|
|
|
|
* member without removing it.
|
|
|
|
*
|
|
|
|
* @return last item in container
|
|
|
|
*/
|
|
|
|
void* nsDeque::Peek() {
|
2002-02-20 03:08:32 +00:00
|
|
|
void* result=0;
|
|
|
|
if (mSize>0) {
|
|
|
|
result = mData[modulus(mSize - 1 + mOrigin, mCapacity)];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
1998-04-22 18:28:48 +00:00
|
|
|
|
1998-08-05 01:59:34 +00:00
|
|
|
/**
|
|
|
|
* This method gets called you want to peek at the topmost
|
|
|
|
* member without removing it.
|
|
|
|
*
|
|
|
|
* @return last item in container
|
|
|
|
*/
|
1999-11-27 17:47:05 +00:00
|
|
|
void* nsDeque::PeekFront() {
|
1998-08-05 01:59:34 +00:00
|
|
|
void* result=0;
|
2002-02-20 03:08:32 +00:00
|
|
|
if (mSize>0) {
|
1998-08-05 01:59:34 +00:00
|
|
|
result=mData[mOrigin];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1998-04-22 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* Call this to retrieve the ith element from this container.
|
2002-02-20 03:08:32 +00:00
|
|
|
* Keep in mind that accessing the underlying elements is
|
1998-04-22 18:28:48 +00:00
|
|
|
* done in a relative fashion. Object 0 is not necessarily
|
|
|
|
* the first element (the first element is at mOrigin).
|
|
|
|
*
|
2002-02-20 03:08:32 +00:00
|
|
|
* @param aIndex : 0 relative offset of item you want
|
1998-04-22 18:28:48 +00:00
|
|
|
* @return void* or null
|
|
|
|
*/
|
2002-02-20 03:08:32 +00:00
|
|
|
void* nsDeque::ObjectAt(PRInt32 aIndex) const {
|
1998-04-22 18:28:48 +00:00
|
|
|
void* result=0;
|
2002-02-20 03:08:32 +00:00
|
|
|
if ((aIndex>=0) && (aIndex<mSize)) {
|
|
|
|
result=mData[modulus(mOrigin + aIndex, mCapacity)];
|
1998-04-22 18:28:48 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create and return an iterator pointing to
|
|
|
|
* the beginning of the queue. Note that this
|
|
|
|
* takes the circular buffer semantics into account.
|
2002-02-20 03:08:32 +00:00
|
|
|
*
|
1998-04-22 18:28:48 +00:00
|
|
|
* @return new deque iterator, init'ed to 1st item
|
|
|
|
*/
|
2002-02-20 03:08:32 +00:00
|
|
|
nsDequeIterator nsDeque::Begin() const{
|
|
|
|
return nsDequeIterator(*this, 0);
|
1998-04-22 18:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create and return an iterator pointing to
|
2002-03-06 03:21:17 +00:00
|
|
|
* the last item in the deque.
|
|
|
|
* Note that this takes the circular buffer semantics
|
|
|
|
* into account.
|
2002-02-20 03:08:32 +00:00
|
|
|
*
|
2002-03-06 03:21:17 +00:00
|
|
|
* @return new deque iterator, init'ed to the last item
|
1998-04-22 18:28:48 +00:00
|
|
|
*/
|
2002-02-20 03:08:32 +00:00
|
|
|
nsDequeIterator nsDeque::End() const{
|
2002-03-06 03:21:17 +00:00
|
|
|
return nsDequeIterator(*this, mSize - 1);
|
1998-04-22 18:28:48 +00:00
|
|
|
}
|
|
|
|
|
2003-03-15 01:04:32 +00:00
|
|
|
void* nsDeque::Last() const {
|
|
|
|
return End().GetCurrent();
|
|
|
|
}
|
|
|
|
|
1998-04-22 18:28:48 +00:00
|
|
|
/**
|
2001-12-08 06:59:49 +00:00
|
|
|
* Call this method when you want to iterate all the
|
1998-04-22 18:28:48 +00:00
|
|
|
* members of the container, passing a functor along
|
|
|
|
* to call your code.
|
|
|
|
*
|
|
|
|
* @param aFunctor object to call for each member
|
|
|
|
* @return *this
|
|
|
|
*/
|
1998-08-27 02:39:36 +00:00
|
|
|
void nsDeque::ForEach(nsDequeFunctor& aFunctor) const{
|
2002-02-20 03:08:32 +00:00
|
|
|
for (PRInt32 i=0; i<mSize; i++) {
|
|
|
|
aFunctor(ObjectAt(i));
|
1998-08-05 01:59:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2001-12-08 06:59:49 +00:00
|
|
|
* Call this method when you want to iterate all the
|
2002-02-20 03:08:32 +00:00
|
|
|
* members of the container, calling the functor you
|
|
|
|
* passed with each member. This process will interrupt
|
|
|
|
* if your function returns non 0 to this method.
|
1998-08-05 01:59:34 +00:00
|
|
|
*
|
|
|
|
* @param aFunctor object to call for each member
|
2002-02-20 03:08:32 +00:00
|
|
|
* @return first nonzero result of aFunctor or 0.
|
1998-08-05 01:59:34 +00:00
|
|
|
*/
|
|
|
|
const void* nsDeque::FirstThat(nsDequeFunctor& aFunctor) const{
|
2002-02-20 03:08:32 +00:00
|
|
|
for (PRInt32 i=0; i<mSize; i++) {
|
|
|
|
void* obj=aFunctor(ObjectAt(i));
|
|
|
|
if (obj) {
|
1998-07-02 08:14:22 +00:00
|
|
|
return obj;
|
2002-02-20 03:08:32 +00:00
|
|
|
}
|
1998-04-22 18:28:48 +00:00
|
|
|
}
|
1998-07-02 08:14:22 +00:00
|
|
|
return 0;
|
1998-04-22 18:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* Here comes the nsDequeIterator class...
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DequeIterator is an object that knows how to iterate (forward and backward)
|
2002-02-20 03:08:32 +00:00
|
|
|
* through a Deque. Normally, you don't need to do this, but there are some special
|
1998-04-22 18:28:48 +00:00
|
|
|
* cases where it is pretty handy, so here you go.
|
|
|
|
*
|
|
|
|
* This is a standard dequeiterator constructor
|
|
|
|
*
|
|
|
|
* @param aQueue is the deque object to be iterated
|
2002-02-20 03:08:32 +00:00
|
|
|
* @param aIndex is the starting position for your iteration
|
1998-04-22 18:28:48 +00:00
|
|
|
*/
|
2002-02-20 03:08:32 +00:00
|
|
|
nsDequeIterator::nsDequeIterator(const nsDeque& aQueue, int aIndex)
|
|
|
|
: mIndex(aIndex),
|
|
|
|
mDeque(aQueue)
|
|
|
|
{
|
1998-04-22 18:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2002-02-20 03:08:32 +00:00
|
|
|
* Create a copy of a DequeIterator
|
1998-04-22 18:28:48 +00:00
|
|
|
*
|
|
|
|
* @param aCopy is another iterator to copy from
|
|
|
|
*/
|
2002-02-20 03:08:32 +00:00
|
|
|
nsDequeIterator::nsDequeIterator(const nsDequeIterator& aCopy)
|
|
|
|
: mIndex(aCopy.mIndex),
|
|
|
|
mDeque(aCopy.mDeque)
|
|
|
|
{
|
1998-04-22 18:28:48 +00:00
|
|
|
}
|
|
|
|
|
1998-08-21 02:03:56 +00:00
|
|
|
/**
|
|
|
|
* Moves iterator to first element in deque
|
2002-02-20 03:08:32 +00:00
|
|
|
* @return *this
|
1998-08-21 02:03:56 +00:00
|
|
|
*/
|
2002-02-20 03:08:32 +00:00
|
|
|
nsDequeIterator& nsDequeIterator::First(){
|
1998-08-21 02:03:56 +00:00
|
|
|
mIndex=0;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
1998-04-22 18:28:48 +00:00
|
|
|
/**
|
|
|
|
* Standard assignment operator for dequeiterator
|
|
|
|
*
|
|
|
|
* @param aCopy is an iterator to be copied from
|
|
|
|
* @return *this
|
|
|
|
*/
|
1998-05-19 01:51:24 +00:00
|
|
|
nsDequeIterator& nsDequeIterator::operator=(const nsDequeIterator& aCopy) {
|
2002-02-20 03:08:32 +00:00
|
|
|
NS_ASSERTION(&mDeque==&aCopy.mDeque,"you can't change the deque that an interator is iterating over, sorry.");
|
1998-04-22 18:28:48 +00:00
|
|
|
mIndex=aCopy.mIndex;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* preform ! operation against to iterators to test for equivalence
|
|
|
|
* (or lack thereof)!
|
|
|
|
*
|
2002-02-20 03:08:32 +00:00
|
|
|
* @param aIter is the object to be compared to
|
1998-04-22 18:28:48 +00:00
|
|
|
* @return TRUE if NOT equal.
|
|
|
|
*/
|
2002-02-20 03:08:32 +00:00
|
|
|
PRBool nsDequeIterator::operator!=(nsDequeIterator& aIter) {
|
|
|
|
return PRBool(!this->operator==(aIter));
|
1998-04-22 18:28:48 +00:00
|
|
|
}
|
|
|
|
|
1998-04-30 05:55:51 +00:00
|
|
|
/**
|
2002-02-20 03:08:32 +00:00
|
|
|
* Compare two iterators for increasing order.
|
1998-04-30 05:55:51 +00:00
|
|
|
*
|
2002-02-20 03:08:32 +00:00
|
|
|
* @param aIter is the other iterator to be compared to
|
|
|
|
* @return TRUE if this object points to an element before
|
|
|
|
* the element pointed to by aIter.
|
|
|
|
* FALSE if this and aIter are not iterating over the same deque.
|
1998-04-30 05:55:51 +00:00
|
|
|
*/
|
2002-02-20 03:08:32 +00:00
|
|
|
PRBool nsDequeIterator::operator<(nsDequeIterator& aIter) {
|
|
|
|
return PRBool(((mIndex<aIter.mIndex) && (&mDeque==&aIter.mDeque)));
|
1998-04-30 05:55:51 +00:00
|
|
|
}
|
|
|
|
|
1998-04-22 18:28:48 +00:00
|
|
|
/**
|
2002-02-20 03:08:32 +00:00
|
|
|
* Compare two iterators for equivalence.
|
1998-04-22 18:28:48 +00:00
|
|
|
*
|
2002-02-20 03:08:32 +00:00
|
|
|
* @param aIter is the other iterator to be compared to
|
1998-04-22 18:28:48 +00:00
|
|
|
* @return TRUE if EQUAL
|
|
|
|
*/
|
2002-02-20 03:08:32 +00:00
|
|
|
PRBool nsDequeIterator::operator==(nsDequeIterator& aIter) {
|
|
|
|
return PRBool(((mIndex==aIter.mIndex) && (&mDeque==&aIter.mDeque)));
|
1998-04-22 18:28:48 +00:00
|
|
|
}
|
|
|
|
|
1998-04-30 05:55:51 +00:00
|
|
|
/**
|
2002-02-20 03:08:32 +00:00
|
|
|
* Compare two iterators for non strict decreasing order.
|
1998-04-30 05:55:51 +00:00
|
|
|
*
|
2002-02-20 03:08:32 +00:00
|
|
|
* @param aIter is the other iterator to be compared to
|
|
|
|
* @return TRUE if this object points to the same element, or
|
|
|
|
* an element after the element pointed to by aIter.
|
|
|
|
* FALSE if this and aIter are not iterating over the same deque.
|
1998-04-30 05:55:51 +00:00
|
|
|
*/
|
2002-02-20 03:08:32 +00:00
|
|
|
PRBool nsDequeIterator::operator>=(nsDequeIterator& aIter) {
|
|
|
|
return PRBool(((mIndex>=aIter.mIndex) && (&mDeque==&aIter.mDeque)));
|
1998-04-30 05:55:51 +00:00
|
|
|
}
|
|
|
|
|
1998-04-22 18:28:48 +00:00
|
|
|
/**
|
2002-02-20 03:08:32 +00:00
|
|
|
* Pre-increment operator
|
1998-04-22 18:28:48 +00:00
|
|
|
*
|
2002-02-20 03:08:32 +00:00
|
|
|
* @return object at post-incremented index
|
1998-04-22 18:28:48 +00:00
|
|
|
*/
|
|
|
|
void* nsDequeIterator::operator++() {
|
2002-02-20 03:08:32 +00:00
|
|
|
NS_ASSERTION(mIndex<mDeque.mSize,
|
|
|
|
"You have reached the end of the Internet."\
|
|
|
|
"You have seen everything there is to see. Please go back. Now."
|
|
|
|
);
|
|
|
|
#ifndef TIMELESS_LIGHTWEIGHT
|
|
|
|
if (mIndex>=mDeque.mSize) return 0;
|
|
|
|
#endif
|
1998-04-22 18:28:48 +00:00
|
|
|
return mDeque.ObjectAt(++mIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2002-02-20 03:08:32 +00:00
|
|
|
* Post-increment operator
|
1998-04-22 18:28:48 +00:00
|
|
|
*
|
|
|
|
* @param param is ignored
|
2002-02-20 03:08:32 +00:00
|
|
|
* @return object at pre-incremented index
|
1998-04-22 18:28:48 +00:00
|
|
|
*/
|
|
|
|
void* nsDequeIterator::operator++(int) {
|
2002-02-20 03:08:32 +00:00
|
|
|
NS_ASSERTION(mIndex<=mDeque.mSize,
|
|
|
|
"You have already reached the end of the Internet."\
|
|
|
|
"You have seen everything there is to see. Please go back. Now."
|
|
|
|
);
|
|
|
|
#ifndef TIMELESS_LIGHTWEIGHT
|
|
|
|
if (mIndex>mDeque.mSize) return 0;
|
|
|
|
#endif
|
1998-04-22 18:28:48 +00:00
|
|
|
return mDeque.ObjectAt(mIndex++);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pre-decrement operator
|
|
|
|
*
|
|
|
|
* @return object at pre-decremented index
|
|
|
|
*/
|
|
|
|
void* nsDequeIterator::operator--() {
|
2002-02-20 03:08:32 +00:00
|
|
|
NS_ASSERTION(mIndex>=0,
|
|
|
|
"You have reached the beginning of the Internet."\
|
|
|
|
"You have seen everything there is to see. Please go forward. Now."
|
|
|
|
);
|
|
|
|
#ifndef TIMELESS_LIGHTWEIGHT
|
|
|
|
if (mIndex<0) return 0;
|
|
|
|
#endif
|
1998-04-22 18:28:48 +00:00
|
|
|
return mDeque.ObjectAt(--mIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Post-decrement operator
|
|
|
|
*
|
|
|
|
* @param param is ignored
|
|
|
|
* @return object at post-decremented index
|
|
|
|
*/
|
|
|
|
void* nsDequeIterator::operator--(int) {
|
2002-02-20 03:08:32 +00:00
|
|
|
NS_ASSERTION(mIndex>=0,
|
|
|
|
"You have already reached the beginning of the Internet."\
|
|
|
|
"You have seen everything there is to see. Please go forward. Now."
|
|
|
|
);
|
|
|
|
#ifndef TIMELESS_LIGHTWEIGHT
|
|
|
|
if (mIndex<0) return 0;
|
|
|
|
#endif
|
1998-04-22 18:28:48 +00:00
|
|
|
return mDeque.ObjectAt(mIndex--);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dereference operator
|
2002-02-20 03:08:32 +00:00
|
|
|
* Note that the iterator floats, so you don't need to do:
|
|
|
|
* <code>++iter; aDeque.PopFront();</code>
|
|
|
|
* Unless you actually want your iterator to jump 2 spaces.
|
|
|
|
*
|
|
|
|
* Picture: [1 2I 3 4]
|
|
|
|
* PopFront()
|
|
|
|
* Picture: [2 3I 4]
|
|
|
|
* Note that I still happily points to object at the second index
|
1998-04-22 18:28:48 +00:00
|
|
|
*
|
|
|
|
* @return object at ith index
|
|
|
|
*/
|
2002-02-20 03:08:32 +00:00
|
|
|
void* nsDequeIterator::GetCurrent() {
|
|
|
|
NS_ASSERTION(mIndex<mDeque.mSize&&mIndex>=0,"Current is out of bounds");
|
|
|
|
#ifndef TIMELESS_LIGHTWEIGHT
|
|
|
|
if (mIndex>=mDeque.mSize||mIndex<0) return 0;
|
|
|
|
#endif
|
1998-04-22 18:28:48 +00:00
|
|
|
return mDeque.ObjectAt(mIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2001-12-08 06:59:49 +00:00
|
|
|
* Call this method when you want to iterate all the
|
1998-04-22 18:28:48 +00:00
|
|
|
* members of the container, passing a functor along
|
|
|
|
* to call your code.
|
|
|
|
*
|
|
|
|
* @param aFunctor object to call for each member
|
|
|
|
* @return *this
|
|
|
|
*/
|
1998-08-27 02:39:36 +00:00
|
|
|
void nsDequeIterator::ForEach(nsDequeFunctor& aFunctor) const{
|
1998-08-05 01:59:34 +00:00
|
|
|
mDeque.ForEach(aFunctor);
|
1998-04-22 18:28:48 +00:00
|
|
|
}
|
|
|
|
|
1998-08-05 01:59:34 +00:00
|
|
|
/**
|
2001-12-08 06:59:49 +00:00
|
|
|
* Call this method when you want to iterate all the
|
2002-02-20 03:08:32 +00:00
|
|
|
* members of the container, calling the functor you
|
|
|
|
* passed with each member. This process will interrupt
|
|
|
|
* if your function returns non 0 to this method.
|
1998-08-05 01:59:34 +00:00
|
|
|
*
|
|
|
|
* @param aFunctor object to call for each member
|
2002-02-20 03:08:32 +00:00
|
|
|
* @return first nonzero result of aFunctor or 0.
|
1998-08-05 01:59:34 +00:00
|
|
|
*/
|
|
|
|
const void* nsDequeIterator::FirstThat(nsDequeFunctor& aFunctor) const{
|
|
|
|
return mDeque.FirstThat(aFunctor);
|
|
|
|
}
|