Skip to content

Commit

Permalink
adding an erase method
Browse files Browse the repository at this point in the history
  • Loading branch information
eteran committed Mar 26, 2018
1 parent 3c95724 commit 6c7c94e
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions vector.h
Expand Up @@ -89,6 +89,26 @@ do { \
vector_set_size((vec), vector_size(vec) - 1); \
} while(0)

/**
* @brief vector_erase - removes the element at index i from the vector
* @param vec - the vector
* @param i - index of element to remove
* @return void
*/
#define vector_erase(vec, i) \
do { \
if (vec) { \
const size_t __sz = vector_size(vec); \
if ((i) < __sz) { \
vector_set_size((vec), __sz - 1); \
size_t __x; \
for (__x = (i); __x < (__sz - 1); ++__x) { \
(vec)[__x] = (vec)[__x + 1]; \
} \
} \
} \
} while(0)

/**
* @brief vector_free - frees all memory associated with the vector
* @param vec - the vector
Expand All @@ -105,18 +125,18 @@ do { \
/**
* @brief vector_begin - returns an iterator to first element of the vector
* @param vec - the vector
* @return a pointer to the first element
* @return a pointer to the first element (or NULL)
*/
#define vector_begin(vec) \
(vec)

/**
* @brief vector_end - returns an iterator to one past the last element of the vector
* @param vec - the vector
* @return a pointer to one past the last element
* @return a pointer to one past the last element (or NULL)
*/
#define vector_end(vec) \
&((vec)[vector_size(vec)])
((vec) ? &((vec)[vector_size(vec)]) : NULL)


/**
Expand Down

0 comments on commit 6c7c94e

Please sign in to comment.