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