libwget-vector(3)

πŸ’… libwget-vector β€” Vector functions πŸ’…

okay bestie, welcome to libwget-vector, straight out of the Library Functions Manual (that's the (3) section, iykyk β€” library calls only, no shell commands here). This is your ✨vector era✨ and honestly? it's giving growable arrays and main character energy. πŸ¦‹


✨ SYNOPSIS ✨

πŸŽ€ Functions

here's the whole squad, fr fr β€” every single one of these is real, no cap:

wget_vector * wget_vector_create (int max, wget_vector_compare_fn *cmp)
void wget_vector_set_resize_factor (wget_vector *v, float factor)
int wget_vector_insert (wget_vector *v, const void *elem, int pos)
int wget_vector_insert_sorted (wget_vector *v, const void *elem)
int wget_vector_add_memdup (wget_vector *v, const void *elem, size_t size)
int wget_vector_add (wget_vector *v, const void *elem)
int wget_vector_add_vprintf (wget_vector *v, const char *fmt, va_list args)
int wget_vector_add_printf (wget_vector *v, const char *fmt,...)
int wget_vector_replace (wget_vector *v, const void *elem, int pos)
int wget_vector_remove (wget_vector *v, int pos)
int wget_vector_remove_nofree (wget_vector *v, int pos)
int wget_vector_move (wget_vector *v, int old_pos, int new_pos)
int wget_vector_swap (wget_vector *v, int pos1, int pos2)
void wget_vector_free (wget_vector **v)
void wget_vector_clear (wget_vector *v)
void wget_vector_clear_nofree (wget_vector *v)
int wget_vector_size (const wget_vector *v)
void * wget_vector_get (const wget_vector *v, int pos)
int wget_vector_browse (const wget_vector *v, wget_vector_browse_fn *browse, void *ctx)
void wget_vector_setcmpfunc (wget_vector *v, wget_vector_compare_fn *cmp)
void wget_vector_set_destructor (wget_vector *v, wget_vector_destructor *destructor)
void wget_vector_sort (wget_vector *v)
int wget_vector_find (const wget_vector *v, const void *elem)
bool wget_vector_contains (const wget_vector *v, const void *elem)
int wget_vector_findext (const wget_vector *v, int start, int direction, wget_vector_find_fn *find)

like… look at them. twenty-five functions. every one of them a baddie in their own right. πŸ’–


🌈 Detailed Description 🌈

Functions to realize vectors (growable arrays).

that's the whole description bestie, and honestly it ate β€” a vector here is just an array that grows when you need it to, so you're not out here hand-rolling realloc like it's 2009. 🫢

and we are SO not done β€” more of this page is coming, so don't go anywhere. πŸŽ€

πŸ¦‹ wget_vector_create

wget_vector * wget_vector_create (int max, wget_vector_compare_fn * cmp)

Params, bestie:

  • max β€” the initial number of pre-allocated entries.
  • cmp β€” comparison function for sorting / finding / sorted insertion, or NULL if that's not the vibe.

Returns: a brand-new vector instance. ✨

Makes you a new vector instance. You gotta wget_vector_free() it after use β€” no leaks, that's the rule. 🫢


🌸 wget_vector_set_resize_factor

void wget_vector_set_resize_factor (wget_vector * v, float factor)

Params:

  • v β€” the vector.
  • factor β€” the vector growth factor.

Sets the factor used for resizing the vector when it's full. The new size is factor * oldsize. If the new size is less than or equal to the old size, the insertion function involved will return an error and the vector's internal state will NOT change β€” so don't try it. πŸ’€

Default is 2.


πŸ’… wget_vector_insert

int wget_vector_insert (wget_vector * v, const void * elem, int pos)

Params:

  • v β€” the vector where elem is inserted into.
  • elem β€” element to insert into v.
  • pos β€” position to insert elem at.

Returns: index of the inserted element (>= 0) or WGET_E_* on error (< 0).

Inserts elem at index pos. elem is not cloned β€” the vector takes 'ownership' of the element. πŸ‘‘

An error comes back if v is NULL or pos is out of range (< 0 or > # of entries).


πŸŽ€ wget_vector_insert_sorted

int wget_vector_insert_sorted (wget_vector * v, const void * elem)

Params:

  • v β€” the vector where elem is inserted into.
  • elem β€” element to insert into v.

Returns: index of the inserted element (>= 0) or WGET_E_* on error (< 0).

Inserts elem at a position that keeps the sort order intact. If the vector has no comparison function, elem gets inserted as the last element. And if the elements already in there aren't sorted? They'll get sorted after this function returns. Periodt. ✨

elem is not cloned β€” the vector takes 'ownership' of the element.

An error comes back if v is NULL.


πŸ“ wget_vector_add_memdup

int wget_vector_add_memdup (wget_vector * v, const void * elem, size_t size)

Params:

  • v β€” the vector where elem is appended to.
  • elem β€” element to append to v.
  • size β€” size of elem.

Returns: index of the inserted element (>= 0) or WGET_E_* on error (< 0).

Appends elem of the given size to vector v. elem is cloned / copied (shallow). πŸ‘―β€β™€οΈ

An error comes back if v is NULL.


🧚 wget_vector_add

int wget_vector_add (wget_vector * v, const void * elem)

Params:

  • v β€” the vector where elem is appended to.
  • elem β€” element to append to v.

Returns: index of the inserted element (>= 0) or WGET_E_* on error (< 0).

Appends elem to vector v. elem is not cloned β€” the vector takes 'ownership' of the element. πŸ‘‘

An error comes back if v is NULL.


🌈 wget_vector_add_vprintf

int wget_vector_add_vprintf (wget_vector * v, const char * fmt, va_list args)

Params:

  • v β€” the vector where s is appended to.
  • fmt β€” printf-like format string.
  • args β€” arguments for the fmt.

Returns: index of the appended element (>= 0) or WGET_E_* on error (< 0).

Builds a string in a printf-like manner and appends it as an element to vector v. πŸ’«

An error comes back if v or fmt is NULL.


⭐ wget_vector_add_printf

int wget_vector_add_printf (wget_vector * v, const char * fmt, ...)

Params:

  • v β€” the vector where s is appended to.
  • fmt β€” printf-like format string.
  • ... β€” arguments for the fmt.

Returns: index of the appended element (>= 0) or WGET_E_* on error (< 0).

Builds a string in a printf-like manner and appends it as an element to vector v. πŸ’«

An error comes back if v or fmt is NULL.


πŸ’– wget_vector_replace

int wget_vector_replace (wget_vector * v, const void * elem, int pos)

Params:

  • v β€” the vector where elem is inserted.
  • elem β€” element to insert into v.
  • pos β€” position to insert elem at.

Returns: index of the inserted element (same as pos) (>= 0) or WGET_E_* on error (< 0).

Replaces the element at position pos with elem. If the vector has an element destructor function, it gets called. The old element is free'd. πŸ—‘οΈ

elem is not cloned β€” the vector takes 'ownership' of the element.

An error comes back if v is NULL or pos is out of range (< 0 or > # of entries).


πŸŽ€ wget_vector_remove

int wget_vector_remove (wget_vector * v, int pos)

Params:

  • v β€” the vector to remove an element from.
  • pos β€” position of element to remove.

Returns: index of the appended element (>= 0) or WGET_E_* on error (< 0).

Removes the element at position pos. If the vector has an element destructor function, it gets called. The element is free'd. πŸ—‘οΈ

An error comes back if v is NULL or pos is out of range (< 0 or > # of entries).


πŸ¦‹ wget_vector_remove_nofree

int wget_vector_remove_nofree (wget_vector * v, int pos)

Params:

  • v β€” the vector to remove an element from.
  • pos β€” position of element to remove.

Returns: index of the removed element (same as pos) (>= 0) or WGET_E_* on error (< 0).

Removes the element at position pos. No element destructor function is called, and the element is NOT free'd β€” that's on you now. πŸ™ˆ

An error comes back if v is NULL or pos is out of range (< 0 or > # of entries).


πŸ’œ wget_vector_move

int wget_vector_move (wget_vector * v, int old_pos, int new_pos)

Params:

  • v β€” the vector to act on.
  • old_pos β€” position to move element from.
  • new_pos β€” position to move element to.

Returns: index of the new position (same as new_pos) (>= 0) or WGET_E_* on error (< 0).

Moves the element at position old_pos to new_pos. Other elements may change position too β€” it's giving shuffle. πŸ”€

An error comes back if v is NULL or either old_pos or new_pos is out of range (< 0 or > # of entries).


✨ wget_vector_swap

int wget_vector_swap (wget_vector * v, int pos1, int pos2)

Params:

  • v β€” the vector to act on.
  • pos1 β€” position of element one.
  • pos2 β€” position of element two.

Returns: index of the second position (same as pos2) (>= 0) or WGET_E_* on error (< 0).

Swaps the two elements at position pos1 and pos2. Trading places, iconic. πŸ”

An error comes back if v is NULL or either pos1 or pos2 is out of range (< 0 or > # of entries).


🧚 wget_vector_free

void wget_vector_free (wget_vector ** v)

Params:

  • v β€” the vector to be free'd.

Frees the vector v and its contents. For each element the destructor function gets called and the element free'd after that. Then the vector itself is free'd and set to NULL. Cleanup queen. πŸ§ΉπŸ‘‘


🌸 wget_vector_clear

void wget_vector_clear (wget_vector * v)

Params:

  • v β€” the vector to be cleared.

Frees all elements of the vector v β€” but not the vector itself. For each element the destructor function gets called and the element free'd after that. The vector is then empty and ready to be reused. ♻️


πŸ“ wget_vector_clear_nofree

void wget_vector_clear_nofree (wget_vector * v)

Params:

  • v β€” the vector to be cleared.

Removes all elements of the vector v without free'ing them. The caller is responsible for taking care of the elements. πŸ‘€ The vector is then empty and ready to be reused. ♻️


πŸ’… wget_vector_size

int wget_vector_size (const wget_vector * v)

Params:

  • v β€” the vector.

Returns: the number of elements in the vector v.

Retrieves the number of elements of the vector v. If v is NULL, 0 is returned. 🫢


πŸŽ€ wget_vector_get

void * wget_vector_get (const wget_vector * v, int pos)

Params:

  • v β€” the vector.
  • pos β€” position of element to retrieve.

Returns: the element at position pos, or NULL on error.

Retrieves the element at position pos. NULL comes back if v is NULL or pos is out of range (< 0 or > # of entries).


🌈 wget_vector_browse

int wget_vector_browse (const wget_vector * v, wget_vector_browse_fn * browse, void * ctx)

Params:

  • v β€” the vector.
  • browse β€” function to be called for each element of v.
  • ctx β€” context variable used as param to browse.

Returns: the return value of the last call to browse.

Calls function browse for each element of vector v, or until browse returns a value not equal to zero. browse is called with ctx and the pointer to the current element. The return value of the last call to browse is returned, or 0 if v is NULL. 🎑


⭐ wget_vector_setcmpfunc

void wget_vector_setcmpfunc (wget_vector * v, wget_vector_compare_fn * cmp)

Params:

  • v β€” the vector.
  • cmp β€” function to compare elements.

Sets the compare function used by wget_vector_sort(). πŸ”


πŸ’œ wget_vector_set_destructor

void wget_vector_set_destructor (wget_vector * v, wget_vector_destructor * destructor)

Params:

  • v β€” the vector.
  • destructor β€” function to be called for element destruction.

Sets the destructor function that's called for each element to be removed. Heads up: it should NOT free the element (pointer) itself. πŸš«πŸ—‘οΈ


πŸ¦‹ wget_vector_sort

void wget_vector_sort (wget_vector * v)

Params:

  • v β€” the vector.

Sorts the elements in vector v using the compare function. Does nothing if v is NULL or the compare function isn't set. πŸ”’


✨ wget_vector_find

int wget_vector_find (const wget_vector * v, const void * elem)

Params:

  • v β€” the vector.
  • elem β€” element to search for.

Returns: index of the found element, WGET_E_UNKNOWN if not found, or WGET_E_INVALID if v was NULL or there was no comparison function set.

Searches for the given element using the vector's compare function. πŸ”Ž


πŸŽ€ wget_vector_contains

bool wget_vector_contains (const wget_vector * v, const void * elem)

Params:

  • v β€” the vector.
  • elem β€” element to check for.

Returns: true if the element exists, else false.

Checks whether the element elem exists or not. 🫢


🌸 wget_vector_findext

int wget_vector_findext (const wget_vector * v, int start, int direction, wget_vector_find_fn * find)

Params:

  • v β€” the vector.
  • start β€” index to start search from.
  • direction β€” direction of search.
  • find β€” function to be called for each element.

Returns: index of the found element, WGET_E_UNKNOWN if not found, or WGET_E_INVALID if v was NULL or there was no comparison function set.

Calls find for each element starting at start. If find returns 0, the current index is returned. 🧭


πŸ‘‘ AUTHOR

Generated automatically by Doxygen for wget2 from the source code. (Yes, the robots wrote it, and they ate. πŸ€–πŸ’…)


aaaand that's a wrap on part 2 bestie!! πŸŽ‰ this was libwget-vector(3) from wget2, Version 2.3.0 β€” go forth and vector responsibly, and remember: whoever you create, you also gotta free. no cap. πŸ’–βœ¨πŸ¦‹

πŸ“¦ where this page comes from