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