ds: array: update iterator interface

This commit is contained in:
2025-10-29 14:34:38 +00:00
parent 144c3a4c68
commit d8abd54b89
2 changed files with 114 additions and 122 deletions

View File

@@ -16,50 +16,22 @@
B_DECLS_BEGIN;
#define B_TYPE_ARRAY (b_array_get_type())
#define B_TYPE_ARRAY (b_array_get_type())
#define B_TYPE_ARRAY_ITERATOR (b_array_iterator_get_type())
struct b_array_p;
B_DECLARE_TYPE(b_array);
B_DECLARE_TYPE(b_array_iterator);
B_TYPE_CLASS_DECLARATION_BEGIN(b_array)
B_TYPE_CLASS_DECLARATION_END(b_array)
/**
* Iterate through each object in an b_array.
*
* This should be used for read-only iterations only. Adding or removing objects
* while iterating though an array using b_array_foreach is NOT supported.
*
* @param it A pointer to an b_array_iterator. This iterator will contain the
* current object reference for the current loop iteration.
* @param array A pointer to the b_array to iterate over.
*/
#define b_array_foreach(it, array) \
for (int z__b_unique_name() = b_array_iterator_begin(array, it); \
(it)->value != NULL; b_array_iterator_next(it))
/**
* Iterator for traversing the contents of an b_array.
*
* The iterator provides the current b_object `value`, as well
* as the index `i` of that value within the array.
*
* Any members whose names begin with _ (underscore) are reserved
* and should not be accessed.
*/
typedef struct b_array_iterator {
b_iterator _base;
b_array *_a;
struct b_array_p *_a_p;
/** The index of the current value */
size_t i;
/** The current value */
b_object *value;
} b_array_iterator;
B_TYPE_CLASS_DECLARATION_BEGIN(b_array_iterator)
B_TYPE_CLASS_DECLARATION_END(b_array_iterator)
BLUE_API b_type b_array_get_type(void);
BLUE_API b_type b_array_iterator_get_type(void);
B_TYPE_DEFAULT_CONSTRUCTOR(b_array, B_TYPE_ARRAY);
@@ -241,46 +213,6 @@ BLUE_API size_t b_array_size(const b_array *array);
*/
BLUE_API size_t b_array_capacity(const b_array *array);
/**
* Initialise an b_array_iterator to pointer to the first object in an b_array.
* If the b_array is empty, then `it` will be an invalid iterator.
*
* @param array
* @param it
* @return Always returns 0.
*/
BLUE_API int b_array_iterator_begin(b_array *array, b_array_iterator *it);
/**
* Advances an b_array_iterator to pointer to the next object in an b_array.
* @param it The iterator to advance.
* @return True if the iterator contains a valid reference to an object, or
* False if the iterator has gone past the end of the array.
*/
BLUE_API bool b_array_iterator_next(b_array_iterator *it);
/**
* Removes the object pointed to by an b_array_iterator from its container
* b_array, and advances the iterator to the next object in the b_array.
*
* The reference count of the removed object will be decremented.
*
* @param it The iterator whose object should be removed.
* @return B_SUCCESS if the object was removed, or a status code describing the error that occurred.
*/
BLUE_API b_status b_array_iterator_erase(b_array_iterator *it);
/**
* Checks whether or not an iterator contains a valid reference to an object.
* An iterator will become invalid if it has moved past the end of the b_array
* it was iterating across, or if b_array_iterator_erase() was called on an
* iterator pointing to the last object in an b_array.
*
* @param it The iterator to check.
* @return True if the iterator is valid. False otherwise.
*/
BLUE_API bool b_array_iterator_is_valid(const b_array_iterator *it);
B_DECLS_END;
#endif