docs/Writerside/topics/list.h.md

changeset 1420
c6f55a2b3495
parent 1419
e46406fd1b3c
child 1421
809eb30cd621
equal deleted inserted replaced
1419:e46406fd1b3c 1420:c6f55a2b3495
98 return 0; 98 return 0;
99 } 99 }
100 } 100 }
101 ``` 101 ```
102 102
103 Also, just registering `regfree()` as destructor is not enough anymore, because the `regex_t` structure also needs to be freed. 103 Also, just registering `regfree()` as a destructor is not enough anymore because the `regex_t` structure also needs to be freed.
104 Therefore, we would need to wrap the calls to `regfree()` and `free()` into an own destructor, which we then register with the list. 104 Therefore, we would need to wrap the calls to `regfree()` and `free()` into an own destructor, which we then register with the list.
105 However, it should be clear by now that using `CX_STORE_POINTERS` is a bad choice for this use case to begin with. 105 However, it should be clear by now that using `CX_STORE_POINTERS` is a bad choice for this use case to begin with.
106 106
107 As a rule of thumb: if you allocate memory for an element that you immediately put into the list, consider storing the element directly. 107 As a rule of thumb: if you allocate memory for an element that you immediately put into the list, consider storing the element directly.
108 And if you are getting pointers to already allocated memory from somewhere else, and you just want to organize those elements in a list, then consider using `CX_STORE_POINTERS`. 108 And if you are getting pointers to already allocated memory from somewhere else, and you just want to organize those elements in a list, then consider using `CX_STORE_POINTERS`.
210 This function replaces the element at the specified `index` with the value pointed to by `elem`. 210 This function replaces the element at the specified `index` with the value pointed to by `elem`.
211 If the list is storing values directly (not pointers), the contents at the memory location `elem` will be copied into the list. 211 If the list is storing values directly (not pointers), the contents at the memory location `elem` will be copied into the list.
212 If the list is storing pointers, the pointer value itself will be stored. 212 If the list is storing pointers, the pointer value itself will be stored.
213 The function returns `0` on success and `1` if the index is out of bounds. 213 The function returns `0` on success and `1` if the index is out of bounds.
214 214
215 On the other hand, `cxListFind()` searches for the element pointed to by `elem` by comparing each element in the list with the list's compare function, 215 On the other hand, `cxListFind()` searches for the element pointed to by `elem` by comparing each element in the list with the list's compare function
216 and returns the first index when the element was found. 216 and returns the first index when the element was found.
217 Otherwise, the function returns the list size. 217 Otherwise, the function returns the list size.
218 218
219 The function `cxListFindRemove()` behaves like `cxListFind()`, except that it also removes the first occurrence of the element from the list. 219 The function `cxListFindRemove()` behaves like `cxListFind()`, except that it also removes the first occurrence of the element from the list.
220 This will _also_ call destructor functions, if specified, so take special care when you continue to use `elem`, or when the list is storing pointers and the element appears more than once in the list. 220 This will _also_ call destructor functions, if specified, so take special care when you continue to use `elem`, or when the list is storing pointers and the element appears more than once in the list.
221 221
222 The function `cxListContains()` returns `true`, if and only if `cxListFind()` would return a valid index. 222 The function `cxListContains()` returns `true` if and only if `cxListFind()` returns a valid index.
223 223
224 With `cxListIndexValid()` you can check the index returned by `cxListFind()` or `cxListFindRemove()`, 224 With `cxListIndexValid()` you can check the index returned by `cxListFind()` or `cxListFindRemove()`,
225 which is more convenient than comparing the return value if the return value of `cxListSize()`. 225 which is more convenient than comparing the return value if the return value of `cxListSize()`.
226 226
227 ## Remove 227 ## Remove
260 This will _also_ call destructor functions, if specified, so take special care when you continue to use `elem`. 260 This will _also_ call destructor functions, if specified, so take special care when you continue to use `elem`.
261 261
262 On the other hand, `cxListRemoveAndGet()` family of functions do not invoke the destructor functions 262 On the other hand, `cxListRemoveAndGet()` family of functions do not invoke the destructor functions
263 and instead copy the elements into the `targetbuf`, which must be large enough to hold the removed elements. 263 and instead copy the elements into the `targetbuf`, which must be large enough to hold the removed elements.
264 264
265 > Note, that when the list was initialized with `CX_STORE_POINTERS`, 265 > Note that when the list was initialized with `CX_STORE_POINTERS`,
266 > the elements that will be copied to the `targetbuf` are the _pointers_. 266 > the elements that will be copied to the `targetbuf` are the _pointers_.
267 > In contrast to other list functions, like `cxListAt()`, an automatic dereferencing does not happen here. 267 > In contrast to other list functions, like `cxListAt()`, an automatic dereferencing does not happen here.
268 >{style="note"} 268 >{style="note"}
269 269
270 The function `cxListClear()` simply removes all elements from the list, invoking the destructor functions. 270 The function `cxListClear()` simply removes all elements from the list, invoking the destructor functions.
271 It behaves equivalently, but is usually more efficient than calling `cxListRemove()` for every index. 271 It behaves equivalently but is usually more efficient than calling `cxListRemove()` for every index.
272 272
273 ## Iterators 273 ## Iterators
274 274
275 ```C 275 ```C
276 #include <cx/list.h> 276 #include <cx/list.h>
296 that start and the first or the last element in the list and iterate through the entire list. 296 that start and the first or the last element in the list and iterate through the entire list.
297 297
298 The functions `cxListIteratorAt()` and `cxListBackwardsIteratorAt()` start with the element at the specified index 298 The functions `cxListIteratorAt()` and `cxListBackwardsIteratorAt()` start with the element at the specified index
299 and iterate until the end, or the beginning of the list, respectively. 299 and iterate until the end, or the beginning of the list, respectively.
300 300
301 The functions with `Mut` in are equivalently, except that they create a [mutating iterator](iterator.h.md#mutating-iterators). 301 The functions with `Mut` in are equivalent, except that they create a [mutating iterator](iterator.h.md#mutating-iterators).
302 Removing elements via a mutating iterator will cause an invocation of the [destructor functions](collection.h.md#destructor-functions) for the removed element. 302 Removing elements via a mutating iterator will cause an invocation of the [destructor functions](collection.h.md#destructor-functions) for the removed element.
303 303
304 If is safe to specify an out-of-bounds index, or a `NULL` pointer, in which cases the returned iterator will behave like an iterator over an empty list. 304 It is safe to specify an out-of-bounds index, or a `NULL` pointer, in which cases the returned iterator will behave like an iterator over an empty list.
305 305
306 ## Reorder 306 ## Reorder
307 307
308 ```C 308 ```C
309 #include <cx/list.h> 309 #include <cx/list.h>
316 ``` 316 ```
317 317
318 The function `cxListSwap()` swaps two elements specified by the indices `i` and `j`. 318 The function `cxListSwap()` swaps two elements specified by the indices `i` and `j`.
319 The return value is non-zero if one of the indices is out-of-bounds. 319 The return value is non-zero if one of the indices is out-of-bounds.
320 320
321 The function `cxListReverse()` reorders all elements, so that they appear in exactly the opposite order after invoking this function. 321 The function `cxListReverse()` reorders all elements so that they appear in exactly the opposite order after invoking this function.
322 322
323 The function `cxListSort()` sorts all elements with respect to the list's compare function, 323 The function `cxListSort()` sorts all elements with respect to the list's compare function,
324 unless the list is already sorted (cf. `cxCollectionSorted()`), in which case the function immediately returns. 324 unless the list is already sorted (cf. `cxCollectionSorted()`), in which case the function immediately returns.
325 325
326 Default UCX implementations of the list interface make use of [small buffer optimizations](install.md#small-buffer-optimizations) when swapping elements. 326 Default UCX implementations of the list interface make use of [small buffer optimizations](install.md#small-buffer-optimizations) when swapping elements.
327 327
328 > An invocation of `cxListSort` sets the `sorted` flag of the [collection](collection.h.md). 328 > An invocation of `cxListSort` sets the `sorted` flag of the [collection](collection.h.md).
329 > Implementations usually make use of this flag to optimize search operations, if possible. 329 > Implementations usually make use of this flag to optimize search operations, if possible.
330 > For example, the [array list](array_list.h.md) implementation will use binary search 330 > For example, the [array list](array_list.h.md) implementation will use binary search
331 > for `cxListFind()` and similar operations, when the list is sorted. 331 > for `cxListFind()` and similar operations when the list is sorted.
332 332
333 ## Compare 333 ## Compare
334 334
335 ```C 335 ```C
336 #include <cx/list.h> 336 #include <cx/list.h>
344 and you could even compare lists that are storing pointers with lists that are not storing pointers. 344 and you could even compare lists that are storing pointers with lists that are not storing pointers.
345 345
346 However, the optimized list-internal compare implementation is only used when both the compare functions and the list classes are identical. 346 However, the optimized list-internal compare implementation is only used when both the compare functions and the list classes are identical.
347 Otherwise, `cxListCompare()` will behave as if you were iterating through both lists and manually comparing the elements. 347 Otherwise, `cxListCompare()` will behave as if you were iterating through both lists and manually comparing the elements.
348 348
349 The return value of `cxListCompare()` is zero, if the lists are element-wise equivalent. 349 The return value of `cxListCompare()` is zero if the lists are element-wise equivalent.
350 If they are not, the non-zero return value equals the return value of the used compare function for the first pair of elements that are not equal. 350 If they are not, the non-zero return value equals the return value of the used compare function for the first pair of elements that are not equal.
351 351
352 ## Dispose 352 ## Dispose
353 353
354 ```C 354 ```C
436 | `sort` | Sort all elements in the list with respect to the list's compare function. | 436 | `sort` | Sort all elements in the list with respect to the list's compare function. |
437 | `compare` | Only function pointer that may be `NULL`, in which case an unoptimized fallback is used. You can implement an optimized compare function that compares the list of another list of the same class. | 437 | `compare` | Only function pointer that may be `NULL`, in which case an unoptimized fallback is used. You can implement an optimized compare function that compares the list of another list of the same class. |
438 | `reverse` | Reorders all elements in the list so that they appear in exactly the opposite order. | 438 | `reverse` | Reorders all elements in the list so that they appear in exactly the opposite order. |
439 | `iterator` | Creates an iterator starting at the specified index. The Boolean argument indicates whether iteration is supposed to traverse backwards. | 439 | `iterator` | Creates an iterator starting at the specified index. The Boolean argument indicates whether iteration is supposed to traverse backwards. |
440 440
441 > If you initialize your list with `cx_list_init()` you do not have to worry about making a 441 > If you initialize your list with `cx_list_init()`, you do not have to worry about making a
442 > difference between storing pointers and storing elements, because your implementation will 442 > difference between storing pointers and storing elements, because your implementation will
443 > be automatically wrapped. 443 > be automatically wrapped.
444 > This means you only have to handle the one single case described above. 444 > This means you only have to handle the one single case described above.
445 445
446 ### Default Class Function Implementations 446 ### Default Class Function Implementations

mercurial