181071f5eSNiels Sascha Reedijk/* 2c4b9309aSJohn Scipione * Copyright 2007-2014 Haiku, Inc. All rights reserved. 381071f5eSNiels Sascha Reedijk * Distributed under the terms of the MIT License. 481071f5eSNiels Sascha Reedijk * 502d0a2eaSNiels Sascha Reedijk * Authors: 655f7db1bSNiels Sascha Reedijk * Niels Sascha Reedijk, niels.reedijk@gmail.com 7c4b9309aSJohn Scipione * John Scipione, jscipione@gmail.com 802d0a2eaSNiels Sascha Reedijk * 902d0a2eaSNiels Sascha Reedijk * Proofreading: 1055f7db1bSNiels Sascha Reedijk * David Weizades, ddewbofh@hotmail.com 1155f7db1bSNiels Sascha Reedijk * Thom Holwerda, slakje@quicknet.nl 1255f7db1bSNiels Sascha Reedijk * John Drinkwater, jdrinkwater@gmail.com 1302d0a2eaSNiels Sascha Reedijk * 1481071f5eSNiels Sascha Reedijk * Corresponds to: 154679af27SNiels Sascha Reedijk * headers/os/support/List.h hrev47422 164679af27SNiels Sascha Reedijk * src/kits/support/List.cpp hrev47422 1781071f5eSNiels Sascha Reedijk */ 189d54b143SNiels Sascha Reedijk 1955f7db1bSNiels Sascha Reedijk 209d54b143SNiels Sascha Reedijk/*! 219d54b143SNiels Sascha Reedijk \file List.h 22820dca4dSJohn Scipione \ingroup support 2341611c9cSJohn Scipione \ingroup libbe 24a38ee20fSNiels Sascha Reedijk \brief Defines the BList class. 259d54b143SNiels Sascha Reedijk*/ 269d54b143SNiels Sascha Reedijk 2755f7db1bSNiels Sascha Reedijk 289d54b143SNiels Sascha Reedijk/*! 299d54b143SNiels Sascha Reedijk \class BList 309d54b143SNiels Sascha Reedijk \ingroup support 3141611c9cSJohn Scipione \ingroup libbe 32a38ee20fSNiels Sascha Reedijk \brief An ordered container that is designed to hold generic \c void* 33a38ee20fSNiels Sascha Reedijk objects. 349d54b143SNiels Sascha Reedijk 359d54b143SNiels Sascha Reedijk This class is designed to be used for a variety of tasks. Unlike similar 3602d0a2eaSNiels Sascha Reedijk implementations in other libraries, this class is not based on templates 3702d0a2eaSNiels Sascha Reedijk and as such is inherently not typed. So it will be the job of the programmer 388b44d0dbSNiels Sascha Reedijk to make sure proper data is entered since the compiler cannot check this by 3902d0a2eaSNiels Sascha Reedijk itself. 409d54b143SNiels Sascha Reedijk 4102d0a2eaSNiels Sascha Reedijk BList contains a list of items that will grow and shrink depending on how 4255f7db1bSNiels Sascha Reedijk many items are in it. So you will not have to do any of the memory 4355f7db1bSNiels Sascha Reedijk management nor any ordering. These properties makes it useful in a whole 4455f7db1bSNiels Sascha Reedijk range of situations such as the interface kit within the BListView class. 459d54b143SNiels Sascha Reedijk 4602d0a2eaSNiels Sascha Reedijk A note on the ownership of the objects might come in handy. BList never 478b44d0dbSNiels Sascha Reedijk assumes ownership of the objects. As such, removing items from the list will 488b44d0dbSNiels Sascha Reedijk only remove the entries from the list; it will not delete the items 4955f7db1bSNiels Sascha Reedijk themselves. Similarly, you should also make sure that before you might 5055f7db1bSNiels Sascha Reedijk delete an object that is in a list, you will have to remove it from the list 5155f7db1bSNiels Sascha Reedijk first. 529d54b143SNiels Sascha Reedijk 539d54b143SNiels Sascha Reedijk \warning This class is not thread-safe. 549d54b143SNiels Sascha Reedijk 558b44d0dbSNiels Sascha Reedijk The class implements methods to add, remove, reorder, retrieve, and query 5655f7db1bSNiels Sascha Reedijk items as well as some advanced methods which let you perform a task on all 5755f7db1bSNiels Sascha Reedijk the items in the list. 58c4b9309aSJohn Scipione 59c4b9309aSJohn Scipione \see BObjectList for a templated version of BList that adds type safety, 60c4b9309aSJohn Scipione optional object ownership, search, and insert operations. 61c4b9309aSJohn Scipione 62c4b9309aSJohn Scipione \since BeOS R3 639d54b143SNiels Sascha Reedijk*/ 649d54b143SNiels Sascha Reedijk 65c4b9309aSJohn Scipione 669d54b143SNiels Sascha Reedijk/*! 679d54b143SNiels Sascha Reedijk \fn BList::BList(int32 count = 20) 689d54b143SNiels Sascha Reedijk \brief Create a new list with a number of empty slots. 699d54b143SNiels Sascha Reedijk 709d54b143SNiels Sascha Reedijk The memory management of this class allocates new memory per block. The 7102d0a2eaSNiels Sascha Reedijk \c count parameter can be tweaked to determine the size of these blocks. 7255f7db1bSNiels Sascha Reedijk In general, if you know your list is only going to contain a certain number 7355f7db1bSNiels Sascha Reedijk of items at most, you can pass that value. If you expect your list to have 7455f7db1bSNiels Sascha Reedijk very few items, it is safe to choose a low number. This is to prevent the 7555f7db1bSNiels Sascha Reedijk list from taking up unneeded memory. If you expect the list to contain a 7655f7db1bSNiels Sascha Reedijk large number of items, choose a higher value. Every time the memory is full, 7755f7db1bSNiels Sascha Reedijk all the items have to be copied into a new piece of allocated memory, which 7855f7db1bSNiels Sascha Reedijk is an expensive operation. 799d54b143SNiels Sascha Reedijk 808b44d0dbSNiels Sascha Reedijk If you are unsure, you do not have to worry too much. Just make sure you do 818b44d0dbSNiels Sascha Reedijk not use a lot of lists, and as long as the list is not used in one of the 829d54b143SNiels Sascha Reedijk performance critical parts of the code, you are safe to go with the default 8302d0a2eaSNiels Sascha Reedijk values. 849d54b143SNiels Sascha Reedijk 8502d0a2eaSNiels Sascha Reedijk \param count The size of the blocks allocated in memory. 86c4b9309aSJohn Scipione 87c4b9309aSJohn Scipione \since BeOS R3 889d54b143SNiels Sascha Reedijk*/ 899d54b143SNiels Sascha Reedijk 90c4b9309aSJohn Scipione 919d54b143SNiels Sascha Reedijk/*! 92c4b9309aSJohn Scipione \fn BList::BList(const BList& other) 93a38ee20fSNiels Sascha Reedijk \brief Copy constructor. Copy a complete list into this one. 94c4b9309aSJohn Scipione 95c4b9309aSJohn Scipione \param other The list to copy from. 96c4b9309aSJohn Scipione 97c4b9309aSJohn Scipione \since BeOS R3 989d54b143SNiels Sascha Reedijk*/ 999d54b143SNiels Sascha Reedijk 100c4b9309aSJohn Scipione 1019d54b143SNiels Sascha Reedijk/*! 1029d54b143SNiels Sascha Reedijk \fn BList::~BList() 1039d54b143SNiels Sascha Reedijk \brief Destroy the list. 1049d54b143SNiels Sascha Reedijk 1059d54b143SNiels Sascha Reedijk Please note that as BList does not assume ownership of the objects, 1069d54b143SNiels Sascha Reedijk only the list will be freed, not the objects that are held in it. 107c4b9309aSJohn Scipione 108c4b9309aSJohn Scipione \since BeOS R3 1099d54b143SNiels Sascha Reedijk*/ 1109d54b143SNiels Sascha Reedijk 111c4b9309aSJohn Scipione 1129d54b143SNiels Sascha Reedijk/*! 113c4b9309aSJohn Scipione \name Operators 1149d54b143SNiels Sascha Reedijk*/ 1159d54b143SNiels Sascha Reedijk 116c4b9309aSJohn Scipione 117c4b9309aSJohn Scipione//! @{ 118c4b9309aSJohn Scipione 119c4b9309aSJohn Scipione 120c4b9309aSJohn Scipione/*! 121c4b9309aSJohn Scipione \fn BList& BList::operator=(const BList &other) 122c4b9309aSJohn Scipione \brief Copy another list into this object. 123c4b9309aSJohn Scipione 124c4b9309aSJohn Scipione \since BeOS R3 125c4b9309aSJohn Scipione*/ 126c4b9309aSJohn Scipione 127c4b9309aSJohn Scipione 128c4b9309aSJohn Scipione/*! 129c4b9309aSJohn Scipione \fn bool BList::operator==(const BList& other) const 130c4b9309aSJohn Scipione \brief Returns whether or not the BList and \a other are equal. 131c4b9309aSJohn Scipione 132c4b9309aSJohn Scipione Equal means that they are the same object or their contents are the same. 133c4b9309aSJohn Scipione 134c4b9309aSJohn Scipione \return \c true if the lists are equal, \c false if they are NOT equal. 135c4b9309aSJohn Scipione 136c4b9309aSJohn Scipione \since Haiku R1 137c4b9309aSJohn Scipione*/ 138c4b9309aSJohn Scipione 139c4b9309aSJohn Scipione 140c4b9309aSJohn Scipione/*! 141c4b9309aSJohn Scipione \fn bool BList::operator!=(const BList& other) const 142c4b9309aSJohn Scipione \brief Returns whether or not the BList and \a other are NOT equal. 143c4b9309aSJohn Scipione 144c4b9309aSJohn Scipione \return \c true if the lists are NOT equal, \c false if they are equal. 145c4b9309aSJohn Scipione 146c4b9309aSJohn Scipione \since Haiku R1 147c4b9309aSJohn Scipione*/ 148c4b9309aSJohn Scipione 149c4b9309aSJohn Scipione 150c4b9309aSJohn Scipione//! @} 151c4b9309aSJohn Scipione 152c4b9309aSJohn Scipione 1539d54b143SNiels Sascha Reedijk/*! 1548b44d0dbSNiels Sascha Reedijk \name Adding and Removing Items 1559d54b143SNiels Sascha Reedijk*/ 1569d54b143SNiels Sascha Reedijk 157c4b9309aSJohn Scipione 1589d54b143SNiels Sascha Reedijk//! @{ 1599d54b143SNiels Sascha Reedijk 160c4b9309aSJohn Scipione 1619d54b143SNiels Sascha Reedijk/*! 1629d54b143SNiels Sascha Reedijk \fn bool BList::AddItem(void* item, int32 index) 163c4b9309aSJohn Scipione \brief Add \a item at the specified \a index. 1649d54b143SNiels Sascha Reedijk 165c4b9309aSJohn Scipione \param item The \a item to add. 166c4b9309aSJohn Scipione \param index The place in the list to add the \a item. 167c4b9309aSJohn Scipione 168c4b9309aSJohn Scipione \return Whether or not the item was added. 1699d54b143SNiels Sascha Reedijk \retval true The item was added. 17002d0a2eaSNiels Sascha Reedijk \retval false Item was not added. Either the index is negative or invalid, 1719d54b143SNiels Sascha Reedijk or resizing the list failed. 172c4b9309aSJohn Scipione 173c4b9309aSJohn Scipione \see AddItem(void*) 174c4b9309aSJohn Scipione 175c4b9309aSJohn Scipione \since BeOS R3 1769d54b143SNiels Sascha Reedijk*/ 1779d54b143SNiels Sascha Reedijk 178c4b9309aSJohn Scipione 1799d54b143SNiels Sascha Reedijk/*! 1809d54b143SNiels Sascha Reedijk \fn bool BList::AddItem(void* item) 181c4b9309aSJohn Scipione \brief Append the \a item to the end of the list. 1829d54b143SNiels Sascha Reedijk 183c4b9309aSJohn Scipione \param item The item to append. 184c4b9309aSJohn Scipione 185c4b9309aSJohn Scipione \return Whether or not the \a item was appended. 186c4b9309aSJohn Scipione \retval true The \a item was appended. 187c4b9309aSJohn Scipione \retval false \a item was not appended, since resizing the BList failed. 188c4b9309aSJohn Scipione 189c4b9309aSJohn Scipione \see AddItem(void*, int32) 190c4b9309aSJohn Scipione 191c4b9309aSJohn Scipione \since BeOS R3 1929d54b143SNiels Sascha Reedijk*/ 1939d54b143SNiels Sascha Reedijk 194c4b9309aSJohn Scipione 1959d54b143SNiels Sascha Reedijk/*! 1969d54b143SNiels Sascha Reedijk \fn bool BList::AddList(const BList* list, int32 index) 197c4b9309aSJohn Scipione \brief Add a \a list of items to this list at the specified \a index. 1989d54b143SNiels Sascha Reedijk 19955f7db1bSNiels Sascha Reedijk Note that the \a list parameter is \c const, so the original list will not 20055f7db1bSNiels Sascha Reedijk be altered. 2019d54b143SNiels Sascha Reedijk 202c4b9309aSJohn Scipione \param list The \a list to be added. 203c4b9309aSJohn Scipione \param index The position in the current \a list where the new item(s) 204c4b9309aSJohn Scipione are added. 205c4b9309aSJohn Scipione 206c4b9309aSJohn Scipione \return Whether or not the \a list was added. 207c4b9309aSJohn Scipione \retval true The \a list was added. 208c4b9309aSJohn Scipione \retval false Failed to insert the \a list resizing failed. 209c4b9309aSJohn Scipione 210c4b9309aSJohn Scipione \see AddList(const BList*) 211c4b9309aSJohn Scipione 212c4b9309aSJohn Scipione \since BeOS R3 2139d54b143SNiels Sascha Reedijk*/ 2149d54b143SNiels Sascha Reedijk 215c4b9309aSJohn Scipione 2169d54b143SNiels Sascha Reedijk/*! 2179d54b143SNiels Sascha Reedijk \fn bool BList::AddList(const BList* list) 218c4b9309aSJohn Scipione \brief Append a \a list of items to this list. 2199d54b143SNiels Sascha Reedijk 22002d0a2eaSNiels Sascha Reedijk Note that the \a list parameter is a \c const, so the original list will not 221a38ee20fSNiels Sascha Reedijk be altered. 2229d54b143SNiels Sascha Reedijk 223c4b9309aSJohn Scipione \param list The \a list to be added. 224c4b9309aSJohn Scipione 225c4b9309aSJohn Scipione \return Whether or not the \a list was added. 226c4b9309aSJohn Scipione \retval true The \a list was appended. 227c4b9309aSJohn Scipione \retval false Failed to append the list, resizing failed. 228c4b9309aSJohn Scipione 229c4b9309aSJohn Scipione \see AddList(const BList*, int32) 230c4b9309aSJohn Scipione 231c4b9309aSJohn Scipione \since BeOS R3 2329d54b143SNiels Sascha Reedijk*/ 2339d54b143SNiels Sascha Reedijk 234c4b9309aSJohn Scipione 2359d54b143SNiels Sascha Reedijk/*! 2369d54b143SNiels Sascha Reedijk \fn bool BList::RemoveItem(void* item) 237c4b9309aSJohn Scipione \brief Remove \a item from the list. 2389d54b143SNiels Sascha Reedijk 239c4b9309aSJohn Scipione \param item The \a item to be removed. 240c4b9309aSJohn Scipione 241c4b9309aSJohn Scipione \return Whether or not the \a item was removed. 242c4b9309aSJohn Scipione \retval true The \a item was found and removed. 243c4b9309aSJohn Scipione \retval false The \a item was not in this list and thus not removed. 244c4b9309aSJohn Scipione 245c4b9309aSJohn Scipione \see RemoveItem(int32) 246c4b9309aSJohn Scipione 247c4b9309aSJohn Scipione \since BeOS R3 2489d54b143SNiels Sascha Reedijk*/ 2499d54b143SNiels Sascha Reedijk 250c4b9309aSJohn Scipione 2519d54b143SNiels Sascha Reedijk/*! 2529d54b143SNiels Sascha Reedijk \fn void* BList::RemoveItem(int32 index) 253a38ee20fSNiels Sascha Reedijk \brief Remove the item at \a index from the list. 2549d54b143SNiels Sascha Reedijk 255c4b9309aSJohn Scipione \param index The \a index of the item to be removed. 256c4b9309aSJohn Scipione 257c4b9309aSJohn Scipione \return The pointer to the item that was removed, or \c NULL if the 258c4b9309aSJohn Scipione \a index was invalid. 259c4b9309aSJohn Scipione 260c4b9309aSJohn Scipione \see RemoveItem(void*) 261c4b9309aSJohn Scipione 262c4b9309aSJohn Scipione \since BeOS R3 2639d54b143SNiels Sascha Reedijk*/ 2649d54b143SNiels Sascha Reedijk 265c4b9309aSJohn Scipione 2669d54b143SNiels Sascha Reedijk/*! 2679d54b143SNiels Sascha Reedijk \fn bool BList::RemoveItems(int32 index, int32 count) 2689d54b143SNiels Sascha Reedijk \brief Remove a number of items starting at a certain position. 2699d54b143SNiels Sascha Reedijk 2709d54b143SNiels Sascha Reedijk If the count parameter is larger than the number of items in the list, 2719d54b143SNiels Sascha Reedijk all the items from the offset to the end will be removed. 2729d54b143SNiels Sascha Reedijk 2739d54b143SNiels Sascha Reedijk \param index The offset in the list where removal should start. 2749d54b143SNiels Sascha Reedijk \param count The number of items to remove. 275c4b9309aSJohn Scipione 276c4b9309aSJohn Scipione \return Whether or not the items were removed. 2779d54b143SNiels Sascha Reedijk \retval true Removal succeeded. 2789d54b143SNiels Sascha Reedijk \retval false Failed to remove the items because the index was invalid. 279c4b9309aSJohn Scipione 280c4b9309aSJohn Scipione \since BeOS R3 2819d54b143SNiels Sascha Reedijk*/ 2829d54b143SNiels Sascha Reedijk 283c4b9309aSJohn Scipione 2849d54b143SNiels Sascha Reedijk/*! 285c4b9309aSJohn Scipione \fn bool BList::ReplaceItem(int32 index, void* item) 2868b44d0dbSNiels Sascha Reedijk \brief Replace an item with another one. 2879d54b143SNiels Sascha Reedijk 288c4b9309aSJohn Scipione \param index The offset in the list where to put the \a item. 289c4b9309aSJohn Scipione \param item The new \a item to put in the list. 290c4b9309aSJohn Scipione 291c4b9309aSJohn Scipione \return Whether or not the item was replaced. 292c4b9309aSJohn Scipione \retval true The item was replaced. 293c4b9309aSJohn Scipione \retval false The \a index was invalid. 294c4b9309aSJohn Scipione 295c4b9309aSJohn Scipione \since Haiku R1 2969d54b143SNiels Sascha Reedijk*/ 2979d54b143SNiels Sascha Reedijk 298c4b9309aSJohn Scipione 2999d54b143SNiels Sascha Reedijk/*! 3009d54b143SNiels Sascha Reedijk \fn void BList::MakeEmpty() 3019d54b143SNiels Sascha Reedijk \brief Clear all the items from the list. 3029d54b143SNiels Sascha Reedijk 303c4b9309aSJohn Scipione \note This does not free the items. 304c4b9309aSJohn Scipione 305c4b9309aSJohn Scipione \since BeOS R3 3069d54b143SNiels Sascha Reedijk*/ 3079d54b143SNiels Sascha Reedijk 308c4b9309aSJohn Scipione 3099d54b143SNiels Sascha Reedijk//! @} 3109d54b143SNiels Sascha Reedijk 311c4b9309aSJohn Scipione 3129d54b143SNiels Sascha Reedijk/*! 3138b44d0dbSNiels Sascha Reedijk \name Reordering Items 3149d54b143SNiels Sascha Reedijk*/ 3159d54b143SNiels Sascha Reedijk 316c4b9309aSJohn Scipione 3179d54b143SNiels Sascha Reedijk//! @{ 3189d54b143SNiels Sascha Reedijk 319c4b9309aSJohn Scipione 3209d54b143SNiels Sascha Reedijk/*! 3219d54b143SNiels Sascha Reedijk \fn void BList::SortItems(int (*compareFunc)(const void*, const void*)) 3229d54b143SNiels Sascha Reedijk \brief Sort the items with the use of a supplied comparison function. 3239d54b143SNiels Sascha Reedijk 32455f7db1bSNiels Sascha Reedijk The function should take two \c const pointers as arguments and should 32555f7db1bSNiels Sascha Reedijk return an integer. 3269d54b143SNiels Sascha Reedijk 3279d54b143SNiels Sascha Reedijk For an example, see the Compare(const BString *, const BString *) function. 328c4b9309aSJohn Scipione 329c4b9309aSJohn Scipione \since BeOS R3 3309d54b143SNiels Sascha Reedijk*/ 3319d54b143SNiels Sascha Reedijk 332c4b9309aSJohn Scipione 3339d54b143SNiels Sascha Reedijk/*! 3349d54b143SNiels Sascha Reedijk \fn bool BList::SwapItems(int32 indexA, int32 indexB) 335c4b9309aSJohn Scipione \brief Swap the items at \a indexA and \a indexB. 3369d54b143SNiels Sascha Reedijk 3379d54b143SNiels Sascha Reedijk \param indexA The first item. 3389d54b143SNiels Sascha Reedijk \param indexB The second item. 339c4b9309aSJohn Scipione 340c4b9309aSJohn Scipione \return Whether or not the items were swapped. 3419d54b143SNiels Sascha Reedijk \retval true Swap succeeded. 3428b44d0dbSNiels Sascha Reedijk \retval false Swap failed because one of the indexes was invalid. 343c4b9309aSJohn Scipione 344c4b9309aSJohn Scipione \since Haiku R1 3459d54b143SNiels Sascha Reedijk*/ 3469d54b143SNiels Sascha Reedijk 347c4b9309aSJohn Scipione 3489d54b143SNiels Sascha Reedijk/*! 3499d54b143SNiels Sascha Reedijk \fn bool BList::MoveItem(int32 fromIndex, int32 toIndex) 350c4b9309aSJohn Scipione \brief Move the item at \a fromIndex to the position of \a toIndex. 3519d54b143SNiels Sascha Reedijk 35202d0a2eaSNiels Sascha Reedijk This moves a list item from position A to position B, moving the appropriate 3539d54b143SNiels Sascha Reedijk block of list elements to make up for the move. For example, in the array: 3549d54b143SNiels Sascha Reedijk\verbatim 3559d54b143SNiels Sascha ReedijkA B C D E F G H I J 3569d54b143SNiels Sascha Reedijk\endverbatim 357a38ee20fSNiels Sascha Reedijk 35802d0a2eaSNiels Sascha Reedijk Moving 1(B)->6(G) would result in this: 3599d54b143SNiels Sascha Reedijk\verbatim 3609d54b143SNiels Sascha ReedijkA C D E F G B H I J 3619d54b143SNiels Sascha Reedijk\endverbatim 3629d54b143SNiels Sascha Reedijk 3639d54b143SNiels Sascha Reedijk \param fromIndex The original location. 3649d54b143SNiels Sascha Reedijk \param toIndex The new location. 365c4b9309aSJohn Scipione 366c4b9309aSJohn Scipione \return Whether or not the items were moved. 3679d54b143SNiels Sascha Reedijk \retval true Move succeeded. 36802d0a2eaSNiels Sascha Reedijk \retval false Move failed due to the indexes being invalid. 369c4b9309aSJohn Scipione 370c4b9309aSJohn Scipione \since Haiku R1 3719d54b143SNiels Sascha Reedijk*/ 3729d54b143SNiels Sascha Reedijk 373c4b9309aSJohn Scipione 3749d54b143SNiels Sascha Reedijk//! @} 3759d54b143SNiels Sascha Reedijk 376c4b9309aSJohn Scipione 3779d54b143SNiels Sascha Reedijk/*! 3788b44d0dbSNiels Sascha Reedijk \name Retrieving Items 3799d54b143SNiels Sascha Reedijk*/ 3809d54b143SNiels Sascha Reedijk 381c4b9309aSJohn Scipione 3829d54b143SNiels Sascha Reedijk//! @{ 3839d54b143SNiels Sascha Reedijk 384c4b9309aSJohn Scipione 3859d54b143SNiels Sascha Reedijk/*! 3869d54b143SNiels Sascha Reedijk \fn void* BList::ItemAt(int32 index) const 387c4b9309aSJohn Scipione \brief Return a pointer to the item at the given \a index. 3889d54b143SNiels Sascha Reedijk 3899d54b143SNiels Sascha Reedijk \param index The item to retrieve. 390c4b9309aSJohn Scipione 391c4b9309aSJohn Scipione \return A pointer to the item in that position, or \c NULL if the 392c4b9309aSJohn Scipione \a index is out of bounds. 393c4b9309aSJohn Scipione 394a38ee20fSNiels Sascha Reedijk \see ItemAtFast(int32 index) const 395c4b9309aSJohn Scipione 396c4b9309aSJohn Scipione \since BeOS R3 3979d54b143SNiels Sascha Reedijk*/ 3989d54b143SNiels Sascha Reedijk 399c4b9309aSJohn Scipione 4009d54b143SNiels Sascha Reedijk/*! 4019d54b143SNiels Sascha Reedijk \fn void* BList::FirstItem() const 402c4b9309aSJohn Scipione \brief Return a pointer to the first item in the list. 403a38ee20fSNiels Sascha Reedijk 40402d0a2eaSNiels Sascha Reedijk \return A pointer to the first item or \c NULL if the list is empty. 405c4b9309aSJohn Scipione 40602d0a2eaSNiels Sascha Reedijk \see LastItem() const 407c4b9309aSJohn Scipione 408c4b9309aSJohn Scipione \since BeOS R3 4099d54b143SNiels Sascha Reedijk*/ 4109d54b143SNiels Sascha Reedijk 411c4b9309aSJohn Scipione 4129d54b143SNiels Sascha Reedijk/*! 4139d54b143SNiels Sascha Reedijk \fn void* BList::ItemAtFast(int32 index) const 414c4b9309aSJohn Scipione \brief Return a pointer to the item at \a index. 4159d54b143SNiels Sascha Reedijk 4168b44d0dbSNiels Sascha Reedijk This method does not perform any boundary checks when it retrieves an item. 4179d54b143SNiels Sascha Reedijk Use this method in a performance critical area of your program where you are 4188b44d0dbSNiels Sascha Reedijk sure you will not get an invalid item. 4199d54b143SNiels Sascha Reedijk 4209d54b143SNiels Sascha Reedijk \return A pointer to the item. 421c4b9309aSJohn Scipione 422c4b9309aSJohn Scipione \since Haiku R1 4239d54b143SNiels Sascha Reedijk*/ 4249d54b143SNiels Sascha Reedijk 425c4b9309aSJohn Scipione 4269d54b143SNiels Sascha Reedijk/*! 4279d54b143SNiels Sascha Reedijk \fn void* BList::LastItem() const 428c4b9309aSJohn Scipione \brief Return a pointer to the last item in the list. 429c4b9309aSJohn Scipione 43002d0a2eaSNiels Sascha Reedijk \return A pointer to the last item or \c NULL if the list is empty. 431c4b9309aSJohn Scipione 43202d0a2eaSNiels Sascha Reedijk \see FirstItem() const 433c4b9309aSJohn Scipione 434c4b9309aSJohn Scipione \since BeOS R3 4359d54b143SNiels Sascha Reedijk*/ 4369d54b143SNiels Sascha Reedijk 437c4b9309aSJohn Scipione 4389d54b143SNiels Sascha Reedijk/*! 4399d54b143SNiels Sascha Reedijk \fn void* BList::Items() const 4409d54b143SNiels Sascha Reedijk \brief Return the internal list of objects. 4419d54b143SNiels Sascha Reedijk 442a38ee20fSNiels Sascha Reedijk This method will return a pointer to the internal pointer list. This means 44355f7db1bSNiels Sascha Reedijk that you should be careful what you are doing, since you are working with 44455f7db1bSNiels Sascha Reedijk the internals of the class directly. 4459d54b143SNiels Sascha Reedijk 44602d0a2eaSNiels Sascha Reedijk It is not a good idea to make any changes to the list, since that will mess 44702d0a2eaSNiels Sascha Reedijk up the internal consistency. 4489d54b143SNiels Sascha Reedijk 44902d0a2eaSNiels Sascha Reedijk \warning If there is anything you want, for which you need the list of 450c4b9309aSJohn Scipione objects, please realize that that probably means that what you 451c4b9309aSJohn Scipione want to do is a bad idea to begin with and that you should avoid 452c4b9309aSJohn Scipione this method. The list of objects does not belong to you. 453c4b9309aSJohn Scipione 4549d54b143SNiels Sascha Reedijk \return The internal list of pointers. 455c4b9309aSJohn Scipione 456c4b9309aSJohn Scipione \sa DoForEach() for an alternate method. 457c4b9309aSJohn Scipione 458c4b9309aSJohn Scipione \since BeOS R3 4599d54b143SNiels Sascha Reedijk*/ 4609d54b143SNiels Sascha Reedijk 461c4b9309aSJohn Scipione 4629d54b143SNiels Sascha Reedijk//! @} 4639d54b143SNiels Sascha Reedijk 464c4b9309aSJohn Scipione 4659d54b143SNiels Sascha Reedijk/*! 466c4b9309aSJohn Scipione \name Querying Items 4679d54b143SNiels Sascha Reedijk*/ 4689d54b143SNiels Sascha Reedijk 469c4b9309aSJohn Scipione 4709d54b143SNiels Sascha Reedijk//! @{ 4719d54b143SNiels Sascha Reedijk 472c4b9309aSJohn Scipione 4739d54b143SNiels Sascha Reedijk/*! 4749d54b143SNiels Sascha Reedijk \fn bool BList::HasItem(void* item) const 475c4b9309aSJohn Scipione \brief Return whether or not \a item is in the list. 476c4b9309aSJohn Scipione 477c4b9309aSJohn Scipione \return \c true if the \a item was in the list, \c false otherwise. 478c4b9309aSJohn Scipione 479c4b9309aSJohn Scipione \since BeOS R3 4809d54b143SNiels Sascha Reedijk*/ 4819d54b143SNiels Sascha Reedijk 482c4b9309aSJohn Scipione 4839d54b143SNiels Sascha Reedijk/*! 4844679af27SNiels Sascha Reedijk \fn bool BList::HasItem(const void *item) const 4854679af27SNiels Sascha Reedijk \brief Return whether or not \a item is in the list. 4864679af27SNiels Sascha Reedijk 4874679af27SNiels Sascha Reedijk \return \c true if the \a item was in the list, \c false otherwise. 4884679af27SNiels Sascha Reedijk 4894679af27SNiels Sascha Reedijk \since Haiku R1 4904679af27SNiels Sascha Reedijk*/ 4914679af27SNiels Sascha Reedijk 4924679af27SNiels Sascha Reedijk 4934679af27SNiels Sascha Reedijk/*! 4949d54b143SNiels Sascha Reedijk \fn int32 BList::IndexOf(void* item) const 495c4b9309aSJohn Scipione \brief Return the index of \a item. 4969d54b143SNiels Sascha Reedijk 4979d54b143SNiels Sascha Reedijk \return The index of the item, or -1 when the item is not in the list. 498c4b9309aSJohn Scipione 499c4b9309aSJohn Scipione \since BeOS R3 5009d54b143SNiels Sascha Reedijk*/ 5019d54b143SNiels Sascha Reedijk 502c4b9309aSJohn Scipione 5039d54b143SNiels Sascha Reedijk/*! 5044679af27SNiels Sascha Reedijk \fn int32 BList::IndexOf(const void *item) const 5054679af27SNiels Sascha Reedijk \brief Return the index of \a item. 5064679af27SNiels Sascha Reedijk 5074679af27SNiels Sascha Reedijk \return The index of the item, or -1 when the item is not in the list. 5084679af27SNiels Sascha Reedijk 5094679af27SNiels Sascha Reedijk \since Haiku R1 5104679af27SNiels Sascha Reedijk*/ 5114679af27SNiels Sascha Reedijk 5124679af27SNiels Sascha Reedijk 5134679af27SNiels Sascha Reedijk/*! 5149d54b143SNiels Sascha Reedijk \fn int32 BList::CountItems() const 515c4b9309aSJohn Scipione \brief Returns the number of items in the list. 516c4b9309aSJohn Scipione 517c4b9309aSJohn Scipione \return The number of items in the list as an int32. 518c4b9309aSJohn Scipione 519c4b9309aSJohn Scipione \since BeOS R3 5209d54b143SNiels Sascha Reedijk*/ 5219d54b143SNiels Sascha Reedijk 522c4b9309aSJohn Scipione 5239d54b143SNiels Sascha Reedijk/*! 5249d54b143SNiels Sascha Reedijk \fn bool BList::IsEmpty() const 525c4b9309aSJohn Scipione \brief Return whether or not there are items in the list. 526c4b9309aSJohn Scipione 527c4b9309aSJohn Scipione \return \c true if the list was empty, \c false otherwise. 528c4b9309aSJohn Scipione 529c4b9309aSJohn Scipione \since BeOS R3 5309d54b143SNiels Sascha Reedijk*/ 5319d54b143SNiels Sascha Reedijk 532c4b9309aSJohn Scipione 5339d54b143SNiels Sascha Reedijk//! @} 5349d54b143SNiels Sascha Reedijk 535c4b9309aSJohn Scipione 5369d54b143SNiels Sascha Reedijk/*! 537c4b9309aSJohn Scipione \name Iterating Over Items 5389d54b143SNiels Sascha Reedijk*/ 5399d54b143SNiels Sascha Reedijk 540c4b9309aSJohn Scipione 5419d54b143SNiels Sascha Reedijk//! @{ 5429d54b143SNiels Sascha Reedijk 543c4b9309aSJohn Scipione 5449d54b143SNiels Sascha Reedijk/*! 5459d54b143SNiels Sascha Reedijk \fn void BList::DoForEach(bool (*func)(void* item)) 5469d54b143SNiels Sascha Reedijk \brief Perform an action on every item in the list. 5479d54b143SNiels Sascha Reedijk 548*4b0d8831SAdrien Destugues Iterates over all items in the list, and calls the \a func function on each of them, 549*4b0d8831SAdrien Destugues until the function returns \c true. 5509d54b143SNiels Sascha Reedijk 551*4b0d8831SAdrien Destugues \param func A pointer to a function that takes a \c void* list item, and 552*4b0d8831SAdrien Destugues returns a bool indicating if the iteration should stop. 553c4b9309aSJohn Scipione 554c4b9309aSJohn Scipione \see DoForEach(bool (*func)(void*, void*), void*) 555c4b9309aSJohn Scipione 556c4b9309aSJohn Scipione \since BeOS R3 5579d54b143SNiels Sascha Reedijk*/ 5589d54b143SNiels Sascha Reedijk 559c4b9309aSJohn Scipione 5609d54b143SNiels Sascha Reedijk/*! 5619d54b143SNiels Sascha Reedijk \fn void BList::DoForEach(bool (*func)(void* item, void* arg2), void* arg2) 5629d54b143SNiels Sascha Reedijk \brief Perform an action on every item in the list with an argument. 5639d54b143SNiels Sascha Reedijk 564*4b0d8831SAdrien Destugues The iteration stops when the \a func function returns \c true. 565*4b0d8831SAdrien Destugues This can be used to implement a linear search of the list, for example: 566*4b0d8831SAdrien Destugues 567*4b0d8831SAdrien Destugues \code{.cpp} 568*4b0d8831SAdrien Destugues bool compareFunc(void* _item, void* arg2) { 569*4b0d8831SAdrien Destugues Item* item = (Item*)_item; 570*4b0d8831SAdrien Destugues Args* args = (Args*)arg2; 571*4b0d8831SAdrien Destugues if (item->Matches(args->pattern)) { 572*4b0d8831SAdrien Destugues args->result = item; 573*4b0d8831SAdrien Destugues return true; 574*4b0d8831SAdrien Destugues } 575*4b0d8831SAdrien Destugues return false; 576*4b0d8831SAdrien Destugues } 577*4b0d8831SAdrien Destugues 578*4b0d8831SAdrien Destugues Args args = {0}; 579*4b0d8831SAdrien Destugues list.DoForEach(compareFunc, &args); 580*4b0d8831SAdrien Destugues if (args->result != NULL) { 581*4b0d8831SAdrien Destugues // Found it! 582*4b0d8831SAdrien Destugues } 583*4b0d8831SAdrien Destugues \endcode 5849d54b143SNiels Sascha Reedijk 58502d0a2eaSNiels Sascha Reedijk \param func A function with the first \c void* argument being the item 586*4b0d8831SAdrien Destugues and the second \c void* being the argument that you supply. 587a38ee20fSNiels Sascha Reedijk \param arg2 An argument to supply to \a func. 588c4b9309aSJohn Scipione 589c4b9309aSJohn Scipione \see DoForEach(bool (*func)(void*)) 590c4b9309aSJohn Scipione 591c4b9309aSJohn Scipione \since BeOS R3 5929d54b143SNiels Sascha Reedijk*/ 5939d54b143SNiels Sascha Reedijk 594c4b9309aSJohn Scipione 5959d54b143SNiels Sascha Reedijk//! @} 596