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