2 * Copyright 2014 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * John Scipione, jscipione@gmail.com
9 * headers/os/support/ObjectList.h hrev40252
17 \brief Defines the BObjectList class.
25 \brief BObjectList is a wrapper around BList that adds type safety,
26 optional object ownership, search, and insert operations.
33 \fn template<class T> BObjectList<T>::BObjectList(int32 itemsPerBlock,
35 \brief Creates a new BObjectList.
37 \param itemsPerBlock The number of items per block to allocate.
38 \param owning Whether or not the BObjectList deletes its items when
39 it is emptied or deleted.
46 \fn template<class T> BObjectList<T>::BObjectList(
47 const BObjectList<T>& list)
48 \brief Creates a new BObjectList as a copy of another \a list.
50 \param list The \a list to copy from.
57 \fn template<class T> BObjectList<T>::~BObjectList()
58 \brief Deletes the list.
60 If the list owns its items they are deleted too.
75 \fn template<class T> BObjectList<T>& BObjectList<T>::operator=(
76 const BObjectList<T>& list)
77 \brief Creates a new BObjectList as a copy of another \a list by
78 overloading the = operator.
80 \param list The \a list to copy from.
90 \name Adding and Removing Items
98 \fn template<class T> bool BObjectList<T>::AddItem(T* item)
99 \brief Append the \a item to the end of the list.
101 \param item The \a item to append.
103 \return Whether or not the \a item was appended.
104 \retval true The \a item was appended.
105 \retval false \a item was not appended, since resizing the BObjectList
113 \fn template<class T> bool BObjectList<T>::AddItem(T* item, int32 index)
114 \brief Add \a item at the specified \a index.
116 \param item The \a item to add.
117 \param index The place in the list to add the \a item.
119 \return Whether or not the item was added.
120 \retval true The item was added.
121 \retval false Item was not added. Either the index is negative or invalid,
122 or resizing the BObjectList failed.
129 \fn template<class T> bool BObjectList<T>::AddList(BObjectList<T>* list)
130 \brief Append a \a list of items to this list.
132 The original list is not altered.
134 \param list The \a list to be added.
136 \return Whether or not the \a list was added.
137 \retval true The \a list was added.
138 \retval false Failed to append the list, resizing failed.
145 \fn template<class T> bool BObjectList<T>::AddList(BObjectList<T>* list,
147 \brief Add a \a list of items to this list at the specified \a index.
149 The original list is not altered.
151 \param list The \a list to be added.
152 \param index The position in the current \a list where the new item(s) are
155 \return Whether or not the \a list was added.
156 \retval true The \a list was added.
157 \retval false Failed to insert the \a list resizing failed.
164 \fn template<class T> bool BObjectList<T>::RemoveItem(T* item,
166 \brief Remove \a item from the list.
168 \param item The \a item to be removed.
169 \param deleteIfOwning Also delete the \a item if owned.
171 \return Whether or not the \a item was removed.
172 \retval true The \a item was found and removed.
173 \retval false The \a item was not in this list and thus not removed.
180 \fn template<class T> T* BObjectList<T>::RemoveItemAt(int32 index)
181 \brief Remove the item at \a index from the list.
183 \param index The \a index of the item to be removed.
185 \return The pointer to the item that was removed, or \c NULL if the
186 \a index was invalid.
193 \fn template<class T> inline T* BObjectList<T>::ItemAt(int32 index) const
194 \brief Return a pointer to the item at the given \a index.
196 \param index The item to retrieve.
198 \return A pointer to the item in that position, or \c NULL if the
199 \a index is out of bounds.
206 \fn template<class T> bool BObjectList<T>::ReplaceItem(int32 index, T* item)
207 \brief Replace an item with another one.
209 \param index The offset in the list where to put the \a item.
210 \param item The new \a item to put in the list.
212 \return Whether or not the item was replaced.
213 \retval true The item was replaced.
214 \retval false The \a index was invalid.
221 \fn template<class T> void BObjectList<T>::MakeEmpty(bool deleteIfOwning)
222 \brief Clear all the items from the list.
224 \param deleteIfOwning Also deletes items if owned.
234 \name Reordering Items
242 \fn template<class T> T* BObjectList<T>::SwapWithItem(int32 index, T* item)
243 \brief Swap the \a item with the item at \a index.
245 \param index The offset in the list where to put the \a item.
246 \param item The new \a item to put in the list.
248 \return A pointer to the item in that position, or \c NULL if the
249 \a index is out of bounds.
256 \fn template<class T> bool BObjectList<T>::MoveItem(int32 from, int32 to)
257 \brief Move the item at \a from to the position of \a to.
259 \param from The index to move the item \a from.
260 \param to The index to move the item \a to.
262 \return Whether or not the items were moved.
263 \retval true The items were moved.
264 \retval false The \a from or \a to index was invalid.
266 \see BList::MoveItem()
284 \fn template<class T> int32 BObjectList<T>::IndexOf(const T* item) const
285 \brief Return the index of \a item.
287 \return The index of the item, or -1 when the item is not in the list.
294 \fn template<class T> T* BObjectList<T>::FirstItem() const
295 \brief Return a pointer to the first item in the list.
297 \return A pointer to the first item or \c NULL if the list is empty.
299 \see BObjectList<T>::LastItem() const
306 \fn template<class T> T* BObjectList<T>::LastItem() const
307 \brief Return a pointer to the last item in the list.
309 \return A pointer to the last item or \c NULL if the list is empty.
311 \see BObjectList<T>::FirstItem() const
318 \fn template<class T> bool BObjectList<T>::HasItem(const T* item) const
319 \brief Return whether or not \a item is in the list.
321 \return \c true if the \a item was in the list, \c false otherwise.
328 \fn template<class T> bool BObjectList<T>::IsEmpty() const
329 \brief Return whether or not there are items in the list.
331 \return \c true if the list was empty, \c false otherwise.
338 \fn template<class T> int32 BObjectList<T>::CountItems() const
339 \brief Returns the number of items in the list.
341 \return The number of items in the list as an int32.
351 \name Iterating Over Items
359 \fn template<class T> T* BObjectList<T>::EachElement(EachFunction func,
361 \brief Perform an action on each item in the list.
363 \param func A function that takes a \c void* argument and returns a
365 \param params parameters for \a func.
367 \return A pointer to the item.
374 \fn template<class T> const T* BObjectList<T>::EachElement(
375 ConstEachFunction func, void* params) const
376 \brief Perform an action on each item in the list.
378 \param func A function that takes a \c void* argument and returns a
380 \param params parameters for \a func.
382 \return A pointer to the item.
400 \fn template<class T> const T* BObjectList<T>::FindIf(
401 const UnaryPredicate<T>& predicate) const
402 \brief Find items that match \a predicate.
404 \return A const pointer to T.
411 \fn template<class T> T* BObjectList<T>::FindIf(
412 const UnaryPredicate<T>& predicate)
413 \brief Find items that match \a predicate.
415 \return A pointer to T.
422 \fn template<class T> T* BObjectList<T>::BinarySearch(const T& key,
423 CompareFunction func) const
424 \brief Search for \a key in the list of items using the supplied comparison
425 function via a binary search algorithm.
427 \param key The \a key to search for.
428 \param func The comparison function to use.
430 \return A pointer to T.
437 \fn template<class T> T* BObjectList<T>::BinarySearch(const T& key,
438 CompareFunctionWithState func, void* state) const
439 \brief Search for \a key in the list of items using the supplied comparison
440 function via a binary search algorithm.
442 \param key The \a key to search for.
443 \param func The comparison function to use.
444 \param state Additional information used to search for the item.
446 \return A pointer to T.
456 \name Reordering Items
464 \fn template<class T> void BObjectList<T>::SortItems(
465 CompareFunction function)
466 \brief Sort the items with the use of a supplied comparison \a function.
468 \param function The \a function used to sort the items.
475 \fn template<class T> void BObjectList<T>::SortItems(
476 CompareFunctionWithState function, void* state)
477 \brief Sort the items with the use of a supplied comparison \a function and
480 \param function The \a function used to sort the items.
481 \param state Additional information used to sort the items.
488 \fn template<class T> void BObjectList<T>::HSortItems(
489 CompareFunction function)
490 \brief Sort the items with the use of a supplied comparison \a function.
492 \param function The \a function used to sort the items.
499 \fn template<class T> void BObjectList<T>::HSortItems(
500 CompareFunctionWithState function, void* state)
502 \param function The \a function used to sort the items.
503 \param state Additional information used to sort the items.