1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
41 * The Deque is a very small, very efficient container object
42 * than can hold elements of type void*, offering the following features:
43 * Its interface supports pushing and popping of elements.
44 * It can iterate (via an interator class) its elements.
45 * When full, it can efficiently resize dynamically.
48 * NOTE: The only bit of trickery here is that this deque is
49 * built upon a ring-buffer. Like all ring buffers, the first
50 * element may not be at index[0]. The mOrigin member determines
51 * where the first child is. This point is quietly hidden from
52 * customers of this class.
62 * The nsDequeFunctor class is used when you want to create
63 * callbacks between the deque and your generic code.
64 * Use these objects in a call to ForEach();
70 virtual void* operator()(void* anObject
)=0;
73 /******************************************************
74 * Here comes the nsDeque class itself...
75 ******************************************************/
78 * The deque (double-ended queue) class is a common container type,
79 * whose behavior mimics a line in your favorite checkout stand.
80 * Classic CS describes the common behavior of a queue as FIFO.
81 * A deque allows insertion and removal at both ends of
84 * The deque stores pointers to items.
87 class nsDequeIterator
;
89 class NS_COM_GLUE nsDeque
{
90 friend class nsDequeIterator
;
92 nsDeque(nsDequeFunctor
* aDeallocator
= nsnull
);
96 * Returns the number of elements currently stored in
99 * @return number of elements currently in the deque
101 inline PRInt32
GetSize() const {return mSize
;}
104 * Appends new member at the end of the deque.
106 * @param item to store in deque
109 nsDeque
& Push(void* aItem
);
112 * Inserts new member at the front of the deque.
114 * @param item to store in deque
117 nsDeque
& PushFront(void* aItem
);
120 * Remove and return the last item in the container.
122 * @return the item that was the last item in container
127 * Remove and return the first item in the container.
129 * @return the item that was first item in container
134 * Retrieve the bottom item without removing it.
136 * @return the first item in container
141 * Return topmost item without removing it.
143 * @return the first item in container
148 * Retrieve the i'th member from the deque without removing it.
150 * @param index of desired item
151 * @return i'th element in list
153 void* ObjectAt(int aIndex
) const;
156 * Remove all items from container without destroying them.
163 * Remove and delete all items from container.
164 * Deletes are handled by the deallocator nsDequeFunctor
165 * which is specified at deque construction.
172 * Creates a new iterator, pointing to the first
175 * @return new dequeIterator
177 nsDequeIterator
Begin() const;
180 * Creates a new iterator, pointing to the last
183 * @return new dequeIterator
185 nsDequeIterator
End() const;
189 * Call this method when you want to iterate all the
190 * members of the container, passing a functor along
193 * @param aFunctor object to call for each member
196 void ForEach(nsDequeFunctor
& aFunctor
) const;
199 * Call this method when you want to iterate all the
200 * members of the container, calling the functor you
201 * passed with each member. This process will interrupt
202 * if your function returns non 0 to this method.
204 * @param aFunctor object to call for each member
205 * @return first nonzero result of aFunctor or 0.
207 const void* FirstThat(nsDequeFunctor
& aFunctor
) const;
209 void SetDeallocator(nsDequeFunctor
* aDeallocator
);
215 nsDequeFunctor
* mDeallocator
;
222 * Copy constructor (PRIVATE)
224 * @param another deque
226 nsDeque(const nsDeque
& other
);
229 * Deque assignment operator (PRIVATE)
231 * @param another deque
234 nsDeque
& operator=(const nsDeque
& anOther
);
236 PRInt32
GrowCapacity();
239 /******************************************************
240 * Here comes the nsDequeIterator class...
241 ******************************************************/
243 class NS_COM_GLUE nsDequeIterator
{
246 * DequeIterator is an object that knows how to iterate
247 * (forward and backward) through a Deque. Normally,
248 * you don't need to do this, but there are some special
249 * cases where it is pretty handy.
251 * One warning: the iterator is not bound to an item,
252 * it is bound to an index, so if you insert or remove
253 * from the beginning while using an iterator
254 * (which is not recommended) then the iterator will
255 * point to a different item. @see GetCurrent()
259 * @param aQueue is the deque object to be iterated
260 * @param aIndex is the starting position for your iteration
262 nsDequeIterator(const nsDeque
& aQueue
, int aIndex
=0);
265 * Create a copy of a DequeIterator
267 * @param aCopy is another iterator to copy from
269 nsDequeIterator(const nsDequeIterator
& aCopy
);
272 * Moves iterator to first element in the deque
275 nsDequeIterator
& First();
278 * Standard assignment operator for dequeiterator
279 * @param aCopy is another iterator to copy from
282 nsDequeIterator
& operator=(const nsDequeIterator
& aCopy
);
285 * preform ! operation against two iterators to test for equivalence
288 * @param aIter is the object to be compared to
289 * @return TRUE if NOT equal.
291 PRBool
operator!=(nsDequeIterator
& aIter
);
294 * Compare two iterators for increasing order.
296 * @param aIter is the other iterator to be compared to
297 * @return TRUE if this object points to an element before
298 * the element pointed to by aIter.
299 * FALSE if this and aIter are not iterating over
302 PRBool
operator<(nsDequeIterator
& aIter
);
305 * Compare two iterators for equivalence.
307 * @param aIter is the other iterator to be compared to
308 * @return TRUE if EQUAL
310 PRBool
operator==(nsDequeIterator
& aIter
);
313 * Compare two iterators for non strict decreasing order.
315 * @param aIter is the other iterator to be compared to
316 * @return TRUE if this object points to the same element, or
317 * an element after the element pointed to by aIter.
318 * FALSE if this and aIter are not iterating over
321 PRBool
operator>=(nsDequeIterator
& aIter
);
324 * Pre-increment operator
325 * Iterator will advance one index towards the end.
327 * @return object_at(++index)
332 * Post-increment operator
333 * Iterator will advance one index towards the end.
335 * @param param is ignored
336 * @return object_at(mIndex++)
338 void* operator++(int);
341 * Pre-decrement operator
342 * Iterator will advance one index towards the beginning.
344 * @return object_at(--index)
349 * Post-decrement operator
350 * Iterator will advance one index towards the beginning.
352 * @param param is ignored
353 * @return object_at(index--)
355 void* operator--(int);
358 * Retrieve the the iterator's notion of current node.
360 * Note that the iterator floats, so you don't need to do:
361 * <code>++iter; aDeque.PopFront();</code>
362 * Unless you actually want your iterator to jump 2 positions
363 * relative to its origin.
365 * Picture: [1 2i 3 4]
368 * Note that I still happily points to object at the second index.
370 * @return object at i'th index
375 * Call this method when you want to iterate all the
376 * members of the container, passing a functor along
379 * @param aFunctor object to call for each member
382 void ForEach(nsDequeFunctor
& aFunctor
) const;
385 * Call this method when you want to iterate all the
386 * members of the container, calling the functor you
387 * passed with each member. This process will interrupt
388 * if your function returns non 0 to this method.
390 * @param aFunctor object to call for each member
391 * @return first nonzero result of aFunctor or 0.
393 const void* FirstThat(nsDequeFunctor
& aFunctor
) const;
398 const nsDeque
& mDeque
;