Skip to content

Commit

Permalink
refactor cvector_at, cvector_front, cvector_back
Browse files Browse the repository at this point in the history
1. `cvector_at` return element itself intead its address
2. `cvector_front` and `cvector_back` will no longer NULL
  • Loading branch information
GoodenoughPhysicsLab committed Dec 23, 2024
1 parent 5a3a450 commit c215566
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions cvector.h
Original file line number Diff line number Diff line change
Expand Up @@ -434,21 +434,21 @@ typedef struct cvector_metadata_t {
* @return the element at the specified position in the vector.
*/
#define cvector_at(vec, n) \
((vec) ? (((int)(n) < 0 || (size_t)(n) >= cvector_size(vec)) ? NULL : &(vec)[n]) : NULL)
((vec)[(cvector_clib_assert((vec) && (n) < cvector_size(vec)), (n))])

/**
* @brief cvector_front - returns a reference to the first element in the vector. Unlike member cvector_begin, which returns an iterator to this same element, this function returns a direct reference.
* @return a reference to the first element in the vector container.
*/
#define cvector_front(vec) \
((vec) ? ((cvector_size(vec) > 0) ? cvector_at(vec, 0) : NULL) : NULL)
(cvector_at(vec, 0))

/**
* @brief cvector_back - returns a reference to the last element in the vector.Unlike member cvector_end, which returns an iterator just past this element, this function returns a direct reference.
* @return a reference to the last element in the vector.
*/
#define cvector_back(vec) \
((vec) ? ((cvector_size(vec) > 0) ? cvector_at(vec, cvector_size(vec) - 1) : NULL) : NULL)
(cvector_at(vec, cvector_size(vec) - 1))

/**
* @brief cvector_resize - resizes the container to contain count elements.
Expand Down

0 comments on commit c215566

Please sign in to comment.