/** * This method adds an item to the front of the deque. * This operation has the potential to cause the * underlying buffer to resize. * * --Commments for GrowCapacity() case * 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 */ bool nsDeque::PushFront(void* aItem, const fallible_t&) { mOrigin--; modasgn(mOrigin,mCapacity); if (mSize==mCapacity) { if (!GrowCapacity()) { return false; } /* Comments explaining this are above*/ mData[mSize]=mData[mOrigin]; } mData[mOrigin]=aItem; mSize++; return true; }
/** * This method adds an item to the front of the deque. * This operation has the potential to cause the * underlying buffer to resize. * * --Commments for GrowCapacity() case * 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 */ nsDeque& nsDeque::PushFront(void* aItem) { mOrigin--; modasgn(mOrigin,mCapacity); if (mSize==mCapacity) { if (!GrowCapacity()) { NS_WARNING("out of memory"); return *this; } /* Comments explaining this are above*/ mData[mSize]=mData[mOrigin]; } mData[mOrigin]=aItem; mSize++; return *this; }