PageRenderTime 17ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

/gecko_api/include/nsDeque.h

http://firefox-mac-pdf.googlecode.com/
C Header | 400 lines | 63 code | 47 blank | 290 comment | 0 complexity | a96d803dd50d1a91c5c31375835e9623 MD5 | raw file
  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
  4. *
  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/
  9. *
  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
  13. * License.
  14. *
  15. * The Original Code is mozilla.org code.
  16. *
  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.
  21. *
  22. * Contributor(s):
  23. *
  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.
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. /**
  38. * MODULE NOTES:
  39. *
  40. * The Deque is a very small, very efficient container object
  41. * than can hold elements of type void*, offering the following features:
  42. * Its interface supports pushing and popping of elements.
  43. * It can iterate (via an interator class) its elements.
  44. * When full, it can efficiently resize dynamically.
  45. *
  46. *
  47. * NOTE: The only bit of trickery here is that this deque is
  48. * built upon a ring-buffer. Like all ring buffers, the first
  49. * element may not be at index[0]. The mOrigin member determines
  50. * where the first child is. This point is quietly hidden from
  51. * customers of this class.
  52. *
  53. */
  54. #ifndef _NSDEQUE
  55. #define _NSDEQUE
  56. #include "nscore.h"
  57. /**
  58. * The nsDequeFunctor class is used when you want to create
  59. * callbacks between the deque and your generic code.
  60. * Use these objects in a call to ForEach();
  61. *
  62. */
  63. class nsDequeFunctor{
  64. public:
  65. virtual void* operator()(void* anObject)=0;
  66. };
  67. /******************************************************
  68. * Here comes the nsDeque class itself...
  69. ******************************************************/
  70. /**
  71. * The deque (double-ended queue) class is a common container type,
  72. * whose behavior mimics a line in your favorite checkout stand.
  73. * Classic CS describes the common behavior of a queue as FIFO.
  74. * A deque allows insertion and removal at both ends of
  75. * the container.
  76. *
  77. * The deque stores pointers to items.
  78. */
  79. class nsDequeIterator;
  80. class NS_COM_GLUE nsDeque {
  81. friend class nsDequeIterator;
  82. public:
  83. nsDeque(nsDequeFunctor* aDeallocator = nsnull);
  84. ~nsDeque();
  85. /**
  86. * Returns the number of elements currently stored in
  87. * this deque.
  88. *
  89. * @return number of elements currently in the deque
  90. */
  91. inline PRInt32 GetSize() const {return mSize;}
  92. /**
  93. * Appends new member at the end of the deque.
  94. *
  95. * @param item to store in deque
  96. * @return *this
  97. */
  98. nsDeque& Push(void* aItem);
  99. /**
  100. * Inserts new member at the front of the deque.
  101. *
  102. * @param item to store in deque
  103. * @return *this
  104. */
  105. nsDeque& PushFront(void* aItem);
  106. /**
  107. * Remove and return the last item in the container.
  108. *
  109. * @return the item that was the last item in container
  110. */
  111. void* Pop();
  112. /**
  113. * Remove and return the first item in the container.
  114. *
  115. * @return the item that was first item in container
  116. */
  117. void* PopFront();
  118. /**
  119. * Retrieve the bottom item without removing it.
  120. *
  121. * @return the first item in container
  122. */
  123. void* Peek();
  124. /**
  125. * Return topmost item without removing it.
  126. *
  127. * @return the first item in container
  128. */
  129. void* PeekFront();
  130. /**
  131. * Retrieve the i'th member from the deque without removing it.
  132. *
  133. * @param index of desired item
  134. * @return i'th element in list
  135. */
  136. void* ObjectAt(int aIndex) const;
  137. /**
  138. * Remove all items from container without destroying them.
  139. *
  140. * @return *this
  141. */
  142. nsDeque& Empty();
  143. /**
  144. * Remove and delete all items from container.
  145. * Deletes are handled by the deallocator nsDequeFunctor
  146. * which is specified at deque construction.
  147. *
  148. * @return *this
  149. */
  150. nsDeque& Erase();
  151. /**
  152. * Creates a new iterator, pointing to the first
  153. * item in the deque.
  154. *
  155. * @return new dequeIterator
  156. */
  157. nsDequeIterator Begin() const;
  158. /**
  159. * Creates a new iterator, pointing to the last
  160. * item in the deque.
  161. *
  162. * @return new dequeIterator
  163. */
  164. nsDequeIterator End() const;
  165. void* Last() const;
  166. /**
  167. * Call this method when you want to iterate all the
  168. * members of the container, passing a functor along
  169. * to call your code.
  170. *
  171. * @param aFunctor object to call for each member
  172. * @return *this
  173. */
  174. void ForEach(nsDequeFunctor& aFunctor) const;
  175. /**
  176. * Call this method when you want to iterate all the
  177. * members of the container, calling the functor you
  178. * passed with each member. This process will interrupt
  179. * if your function returns non 0 to this method.
  180. *
  181. * @param aFunctor object to call for each member
  182. * @return first nonzero result of aFunctor or 0.
  183. */
  184. const void* FirstThat(nsDequeFunctor& aFunctor) const;
  185. void SetDeallocator(nsDequeFunctor* aDeallocator);
  186. protected:
  187. PRInt32 mSize;
  188. PRInt32 mCapacity;
  189. PRInt32 mOrigin;
  190. nsDequeFunctor* mDeallocator;
  191. void* mBuffer[8];
  192. void** mData;
  193. private:
  194. /**
  195. * Copy constructor (PRIVATE)
  196. *
  197. * @param another deque
  198. */
  199. nsDeque(const nsDeque& other);
  200. /**
  201. * Deque assignment operator (PRIVATE)
  202. *
  203. * @param another deque
  204. * @return *this
  205. */
  206. nsDeque& operator=(const nsDeque& anOther);
  207. PRInt32 GrowCapacity();
  208. };
  209. /******************************************************
  210. * Here comes the nsDequeIterator class...
  211. ******************************************************/
  212. class NS_COM_GLUE nsDequeIterator {
  213. public:
  214. /**
  215. * DequeIterator is an object that knows how to iterate
  216. * (forward and backward) through a Deque. Normally,
  217. * you don't need to do this, but there are some special
  218. * cases where it is pretty handy.
  219. *
  220. * One warning: the iterator is not bound to an item,
  221. * it is bound to an index, so if you insert or remove
  222. * from the beginning while using an iterator
  223. * (which is not recommended) then the iterator will
  224. * point to a different item. @see GetCurrent()
  225. *
  226. * Here you go.
  227. *
  228. * @param aQueue is the deque object to be iterated
  229. * @param aIndex is the starting position for your iteration
  230. */
  231. nsDequeIterator(const nsDeque& aQueue, int aIndex=0);
  232. /**
  233. * Create a copy of a DequeIterator
  234. *
  235. * @param aCopy is another iterator to copy from
  236. */
  237. nsDequeIterator(const nsDequeIterator& aCopy);
  238. /**
  239. * Moves iterator to first element in the deque
  240. * @return *this
  241. */
  242. nsDequeIterator& First();
  243. /**
  244. * Standard assignment operator for dequeiterator
  245. * @param aCopy is another iterator to copy from
  246. * @return *this
  247. */
  248. nsDequeIterator& operator=(const nsDequeIterator& aCopy);
  249. /**
  250. * preform ! operation against two iterators to test for equivalence
  251. * (or lack thereof)!
  252. *
  253. * @param aIter is the object to be compared to
  254. * @return TRUE if NOT equal.
  255. */
  256. PRBool operator!=(nsDequeIterator& aIter);
  257. /**
  258. * Compare two iterators for increasing order.
  259. *
  260. * @param aIter is the other iterator to be compared to
  261. * @return TRUE if this object points to an element before
  262. * the element pointed to by aIter.
  263. * FALSE if this and aIter are not iterating over
  264. * the same deque.
  265. */
  266. PRBool operator<(nsDequeIterator& aIter);
  267. /**
  268. * Compare two iterators for equivalence.
  269. *
  270. * @param aIter is the other iterator to be compared to
  271. * @return TRUE if EQUAL
  272. */
  273. PRBool operator==(nsDequeIterator& aIter);
  274. /**
  275. * Compare two iterators for non strict decreasing order.
  276. *
  277. * @param aIter is the other iterator to be compared to
  278. * @return TRUE if this object points to the same element, or
  279. * an element after the element pointed to by aIter.
  280. * FALSE if this and aIter are not iterating over
  281. * the same deque.
  282. */
  283. PRBool operator>=(nsDequeIterator& aIter);
  284. /**
  285. * Pre-increment operator
  286. * Iterator will advance one index towards the end.
  287. *
  288. * @return object_at(++index)
  289. */
  290. void* operator++();
  291. /**
  292. * Post-increment operator
  293. * Iterator will advance one index towards the end.
  294. *
  295. * @param param is ignored
  296. * @return object_at(mIndex++)
  297. */
  298. void* operator++(int);
  299. /**
  300. * Pre-decrement operator
  301. * Iterator will advance one index towards the beginning.
  302. *
  303. * @return object_at(--index)
  304. */
  305. void* operator--();
  306. /**
  307. * Post-decrement operator
  308. * Iterator will advance one index towards the beginning.
  309. *
  310. * @param param is ignored
  311. * @return object_at(index--)
  312. */
  313. void* operator--(int);
  314. /**
  315. * Retrieve the the iterator's notion of current node.
  316. *
  317. * Note that the iterator floats, so you don't need to do:
  318. * <code>++iter; aDeque.PopFront();</code>
  319. * Unless you actually want your iterator to jump 2 positions
  320. * relative to its origin.
  321. *
  322. * Picture: [1 2i 3 4]
  323. * PopFront()
  324. * Picture: [2 3i 4]
  325. * Note that I still happily points to object at the second index.
  326. *
  327. * @return object at i'th index
  328. */
  329. void* GetCurrent();
  330. /**
  331. * Call this method when you want to iterate all the
  332. * members of the container, passing a functor along
  333. * to call your code.
  334. *
  335. * @param aFunctor object to call for each member
  336. * @return *this
  337. */
  338. void ForEach(nsDequeFunctor& aFunctor) const;
  339. /**
  340. * Call this method when you want to iterate all the
  341. * members of the container, calling the functor you
  342. * passed with each member. This process will interrupt
  343. * if your function returns non 0 to this method.
  344. *
  345. * @param aFunctor object to call for each member
  346. * @return first nonzero result of aFunctor or 0.
  347. */
  348. const void* FirstThat(nsDequeFunctor& aFunctor) const;
  349. protected:
  350. PRInt32 mIndex;
  351. const nsDeque& mDeque;
  352. };
  353. #endif