1/* 2 * Copyright 2007, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Niels Sascha Reedijk <niels.reedijk@gmail.com> 7 * 8 * Proofreading: 9 * David Weizades <ddewbofh@hotmail.com> 10 * Thom Holwerda <slakje@quicknet.nl> 11 * John Drinkwater <jdrinkwater@gmail.com> 12 * 13 * Corresponds to: 14 * /trunk/headers/os/support/List.h rev 19972 15 * /trunk/src/kits/support/List.cpp rev 18649 16 */ 17 18/*! 19 \file List.h 20 \brief Defines the BList class. 21*/ 22 23/*! 24 \class BList 25 \ingroup support 26 \ingroup libbe 27 \brief An ordered container that is designed to hold generic \c void * 28 objects. 29 30 This class is designed to be used for a variety of tasks. Unlike similar 31 implementations in other libraries, this class is not based on templates 32 and as such is inherently not typed. So it will be the job of the programmer 33 to make sure proper data is entered since the compiler cannot check this by 34 itself. 35 36 BList contains a list of items that will grow and shrink depending on how 37 many items are in it. So you will not have to do any of the memory management 38 nor any ordering. These properties makes it useful in a whole range of 39 situations such as the interface kit within the BListView class. 40 41 A note on the ownership of the objects might come in handy. BList never 42 assumes ownership of the objects. As such, removing items from the list will 43 only remove the entries from the list; it will not delete the items 44 themselves. Similarly, you should also make sure that before you might delete 45 an object that is in a list, you will have to remove it from the list first. 46 47 \warning This class is not thread-safe. 48 49 The class implements methods to add, remove, reorder, retrieve, and query 50 items as well as some advanced methods which let you perform a task on all the 51 items in the list. 52*/ 53 54/*! 55 \fn BList::BList(int32 count = 20) 56 \brief Create a new list with a number of empty slots. 57 58 The memory management of this class allocates new memory per block. The 59 \c count parameter can be tweaked to determine the size of these blocks. 60 In general, if you know your list is only going to contain a certain number of 61 items at most, you can pass that value. If you expect your list to have very 62 few items, it is safe to choose a low number. This is to prevent the list from 63 taking up unneeded memory. If you expect the list to contain a large number 64 of items, choose a higher value. Every time the memory is full, all the items 65 have to be copied into a new piece of allocated memory, which is an expensive 66 operation. 67 68 If you are unsure, you do not have to worry too much. Just make sure you do 69 not use a lot of lists, and as long as the list is not used in one of the 70 performance critical parts of the code, you are safe to go with the default 71 values. 72 73 \param count The size of the blocks allocated in memory. 74*/ 75 76/*! 77 \fn BList::BList(const BList& anotherList) 78 \brief Copy constructor. Copy a complete list into this one. 79*/ 80 81/*! 82 \fn BList::~BList() 83 \brief Destroy the list. 84 85 Please note that as BList does not assume ownership of the objects, 86 only the list will be freed, not the objects that are held in it. 87*/ 88 89/*! 90 \fn BList& BList::operator=(const BList &list) 91 \brief Copy another list into this object. 92*/ 93 94/*! 95 \name Adding and Removing Items 96*/ 97 98//! @{ 99 100/*! 101 \fn bool BList::AddItem(void *item, int32 index) 102 \brief Add an item at a certain position. 103 104 \param item The item to add. 105 \param index The place in the list. 106 \retval true The item was added. 107 \retval false Item was not added. Either the index is negative or invalid, 108 or resizing the list failed. 109 \see AddItem(void *item) 110*/ 111 112/*! 113 \fn bool BList::AddItem(void *item) 114 \brief Append an item to the list. 115 116 \param item The item to add. 117 \retval true The item was appended. 118 \retval false Item was not appended, since resizing the list failed. 119 \see AddItem(void *item, int32 index) 120*/ 121 122/*! 123 \fn bool BList::AddList(const BList *list, int32 index) 124 \brief Add items from another list to this list at a certain position. 125 126 Note that the \a list parameter is \c const, so the original list will not be 127 altered. 128 129 \param list The list to be added. 130 \param index The position in the current list where the new item(s) should be 131 put. 132 \retval true The list was added. 133 \retval false Failed to insert the list, due to the fact that resizing our 134 list failed. 135 \see AddList(const BList *list) 136*/ 137 138/*! 139 \fn bool BList::AddList(const BList *list) 140 \brief Append a list to this list. 141 142 Note that the \a list parameter is a \c const, so the original list will not 143 be altered. 144 145 \param list The list to be appended. 146 \retval true The list was appended. 147 \retval false Failed to append the list, due to the fact that resizing of our 148 list failed. 149 \see AddList(const BList *list, int32 index) 150*/ 151 152/*! 153 \fn bool BList::RemoveItem(void *item) 154 \brief Remove an item from the list. 155 156 \param item The item that should be removed. 157 \retval true The item was found and removed. 158 \retval false The item was not in this list and thus not removed. 159 \see RemoveItem(int32 index) 160*/ 161 162/*! 163 \fn void * BList::RemoveItem(int32 index) 164 \brief Remove the item at \a index from the list. 165 166 \param index The item that should be removed. 167 \return The pointer to the item that was removed, or \c NULL in case the 168 index was invalid. 169 \see RemoveItem(void *item) 170*/ 171 172/*! 173 \fn bool BList::RemoveItems(int32 index, int32 count) 174 \brief Remove a number of items starting at a certain position. 175 176 If the count parameter is larger than the number of items in the list, 177 all the items from the offset to the end will be removed. 178 179 \param index The offset in the list where removal should start. 180 \param count The number of items to remove. 181 \retval true Removal succeeded. 182 \retval false Failed to remove the items because the index was invalid. 183*/ 184 185/*! 186 \fn bool BList::ReplaceItem(int32 index, void *newItem) 187 \brief Replace an item with another one. 188 189 \param index The offset in the list where to put the item. 190 \param newItem The new item to put in the list. 191 \retval true Item replaced. 192 \retval false The index was invalid. 193*/ 194 195/*! 196 \fn void BList::MakeEmpty() 197 \brief Clear all the items from the list. 198 199 Please note that this does not free the items. 200*/ 201 202//! @} 203 204/*! 205 \name Reordering Items 206*/ 207 208//! @{ 209 210/*! 211 \fn void BList::SortItems(int (*compareFunc)(const void *, const void *)) 212 \brief Sort the items with the use of a supplied comparison function. 213 214 The function should take two \c const pointers as arguments and should return 215 an integer. 216 217 For an example, see the Compare(const BString *, const BString *) function. 218*/ 219 220/*! 221 \fn bool BList::SwapItems(int32 indexA, int32 indexB) 222 \brief Swap two items. 223 224 \param indexA The first item. 225 \param indexB The second item. 226 \retval true Swap succeeded. 227 \retval false Swap failed because one of the indexes was invalid. 228*/ 229 230/*! 231 \fn bool BList::MoveItem(int32 fromIndex, int32 toIndex) 232 \brief Move an item to a new place 233 234 This moves a list item from position A to position B, moving the appropriate 235 block of list elements to make up for the move. For example, in the array: 236 \verbatim 237A B C D E F G H I J 238 \endverbatim 239 240 Moving 1(B)->6(G) would result in this: 241 \verbatim 242A C D E F G B H I J 243 \endverbatim 244 245 \param fromIndex The original location. 246 \param toIndex The new location. 247 \retval true Move succeeded. 248 \retval false Move failed due to the indexes being invalid. 249*/ 250 251//! @} 252 253/*! 254 \name Retrieving Items 255*/ 256 257//! @{ 258 259/*! 260 \fn void *BList::ItemAt(int32 index) const 261 \brief Get an item. 262 263 \param index The item to retrieve. 264 \return A pointer to the item in that position, or \c NULL if the index is 265 out of bounds. 266 \see ItemAtFast(int32 index) const 267*/ 268 269/*! 270 \fn void *BList::FirstItem() const 271 \brief Get the first item. 272 273 \return A pointer to the first item or \c NULL if the list is empty. 274 \see LastItem() const 275*/ 276 277/*! 278 \fn void *BList::ItemAtFast(int32 index) const 279 \brief Get an item. 280 281 This method does not perform any boundary checks when it retrieves an item. 282 Use this method in a performance critical area of your program where you are 283 sure you will not get an invalid item. 284 285 \return A pointer to the item. 286*/ 287 288/*! 289 \fn void *BList::LastItem() const 290 \brief Get the last item. 291 \return A pointer to the last item or \c NULL if the list is empty. 292 \see FirstItem() const 293*/ 294 295/*! 296 \fn void *BList::Items() const 297 \brief Return the internal list of objects. 298 299 This method will return a pointer to the internal pointer list. This means 300 that you should be careful what you are doing, since you are working with the 301 internals of the class directly. 302 303 It is not a good idea to make any changes to the list, since that will mess 304 up the internal consistency. 305 306 \warning If there is anything you want, for which you need the list of 307 objects, please realize that that probably means that what you want to do is 308 a bad idea to begin with and that you should avoid this method. The list of 309 objects does not belong to you. See also DoForEach() for an alternate 310 method. 311 \return The internal list of pointers. 312*/ 313 314//! @} 315 316/*! 317 \name Querying for Items 318*/ 319 320//! @{ 321 322/*! 323 \fn bool BList::HasItem(void *item) const 324 \brief Check if an item is in the list. 325*/ 326 327/*! 328 \fn int32 BList::IndexOf(void *item) const 329 \brief Get the index of an item. 330 331 \return The index of the item, or -1 when the item is not in the list. 332*/ 333 334/*! 335 \fn int32 BList::CountItems() const 336 \brief Get the number of items in the list. 337*/ 338 339/*! 340 \fn bool BList::IsEmpty() const 341 \brief Check if there are items in the list. 342*/ 343 344//! @} 345 346/*! 347 \name Iterating over the List 348*/ 349 350//! @{ 351 352/*! 353 \fn void BList::DoForEach(bool (*func)(void* item)) 354 \brief Perform an action on every item in the list. 355 356 If one of the actions on the items fails it means that the \a func function 357 returned \c false and the processing of the list will be stopped. 358 359 \param func A function that takes a \c void * argument and returns a boolean. 360 \see DoForEach(bool (*func)(void* item, void* arg2), void *arg2) 361*/ 362 363/*! 364 \fn void BList::DoForEach(bool (*func)(void* item, void* arg2), void *arg2) 365 \brief Perform an action on every item in the list with an argument. 366 367 If one of the actions on the items fails it means that the \a func function 368 returned \c false and the processing of the list will be stopped. 369 370 \param func A function with the first \c void * argument being the item 371 and the second \c void * being the argument that you supply. It should 372 return a boolean value on whether it succeeded or not. 373 \param arg2 An argument to supply to \a func. 374 \see DoForEach(bool (*func)(void* item)) 375*/ 376 377//! @} 378