---
name: "libwget-vector"
section: "3"
platform: "linux"
package: "wget2-dev"
version: "2.2.0+ds-1+deb13u1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/libwget-vector"
---

# libwget-vector(3)

Vector functions

## NAME

libwget-vector - Vector functions

## SYNOPSIS

### Functions

**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)\

## Detailed Description

Functions to realize vectors (growable arrays).

## Function Documentation

### wget\_vector \* wget\_vector\_create (int max, wget\_vector\_compare\_fn \* cmp)

**Parameters**

*max* Initial number of pre-allocated entries. \
 *cmp* Comparison function for sorting/finding/sorted insertion or NULL.

**Returns**

New vector instance

Create a new vector instance, to be free'd after use with **wget\_vector\_free()**.

### void wget\_vector\_set\_resize\_factor (wget\_vector \* v, float factor)

**Parameters**

*v* Vector \
 *factor* Vector growth factor

Set the factor for resizing the vector when it is full.

The new size is 'factor \* oldsize'. If the new size is less or equal the old size, the involved insertion function will return an error and the internal state of the vector will not change.

Default is 2.

### int wget\_vector\_insert (wget\_vector \* v, const void \* elem, int pos)

**Parameters**

*v* Vector where `elem` is inserted into \
 *elem* Element to insert into `v` \
 *pos* Position to insert `elem` at

**Returns**

Index of inserted element (\>= 0) or WGET\_E\_\* on error (\< 0)

Insert `elem` of at index `pos`.

`elem` is *not* cloned, the vector takes 'ownership' of the element.

An error is returned if `v` is NULL or `pos` is out of range (\< 0 or \> # of entries).

### int wget\_vector\_insert\_sorted (wget\_vector \* v, const void \* elem)

**Parameters**

*v* Vector where `elem` is inserted into \
 *elem* Element to insert into `v`

**Returns**

Index of inserted element (\>= 0) or WGET\_E\_\* on error (\< 0)

Insert `elem` of at a position that keeps the sort order of the elements. If the vector has no comparison function, `elem` will be inserted as the last element. If the elements in the vector are not sorted, they will be sorted after returning from this function.

`elem` is *not* cloned, the vector takes 'ownership' of the element.

An error is returned if `v` is NULL.

### int wget\_vector\_add\_memdup (wget\_vector \* v, const void \* elem, size\_t size)

**Parameters**

*v* Vector where `elem` is appended to \
 *elem* Element to append to a `v` \
 *size* Size of `elem`

**Returns**

Index of inserted element (\>= 0) or WGET\_E\_\* on error (\< 0)

Append `elem` of given `size` to vector `v`.

`elem` is cloned / copied (shallow).

An error is returned if `v` is NULL.

### int wget\_vector\_add (wget\_vector \* v, const void \* elem)

**Parameters**

*v* Vector where `elem` is appended to \
 *elem* Element to append to a `v`

**Returns**

Index of inserted element (\>= 0) or WGET\_E\_\* on error (\< 0)

Append `elem` to vector `v`.

`elem` is *not* cloned, the vector takes 'ownership' of the element.

An error is returned if `v` is NULL.

### int wget\_vector\_add\_vprintf (wget\_vector \* v, const char \* fmt, va\_list args)

**Parameters**

*v* Vector where `s` is appended to \
 *fmt* Printf-like format string \
 *args* Arguments for the `fmt`

**Returns**

Index of appended element (\>= 0) or WGET\_E\_\* on error (\< 0)

Construct string in a printf-like manner and append it as an element to vector `v`.

An error is returned if `v` or `fmt` is NULL.

### int wget\_vector\_add\_printf (wget\_vector \* v, const char \* fmt, ...)

**Parameters**

*v* Vector where `s` is appended to \
 *fmt* Printf-like format string \
 *...* Arguments for the `fmt`

**Returns**

Index of appended element (\>= 0) or WGET\_E\_\* on error (\< 0)

Construct string in a printf-like manner and append it as an element to vector `v`.

An error is returned if `v` or `fmt` is NULL.

### int wget\_vector\_replace (wget\_vector \* v, const void \* elem, int pos)

**Parameters**

*v* Vector where `elem` is inserted \
 *elem* Element to insert into `v` \
 *pos* Position to insert `elem` at

**Returns**

Index of inserted element (same as `pos`) (\>= 0) or WGET\_E\_\* on error (\< 0)

Replace the element at position `pos` with `elem`. If the vector has an element destructor function, this is called. The old element is free'd.

`elem` is *not* cloned, the vector takes 'ownership' of the element.

An error is returned if `v` is NULL or `pos` is out of range (\< 0 or \> # of entries).

### int wget\_vector\_remove (wget\_vector \* v, int pos)

**Parameters**

*v* Vector to remove an element from \
 *pos* Position of element to remove

**Returns**

Index of appended element (\>= 0) or WGET\_E\_\* on error (\< 0)

Remove the element at position `pos`. If the vector has an element destructor function, this is called. The element is free'd.

An error is returned if `v` is NULL or `pos` is out of range (\< 0 or \> # of entries).

### int wget\_vector\_remove\_nofree (wget\_vector \* v, int pos)

**Parameters**

*v* Vector to remove an element from \
 *pos* Position of element to remove

**Returns**

Index of removed element (same as `pos`) (\>= 0) or WGET\_E\_\* on error (\< 0)

Remove the element at position `pos`. No element destructor function is called, the element is not free'd.

An error is returned if `v` is NULL or `pos` is out of range (\< 0 or \> # of entries).

### int wget\_vector\_move (wget\_vector \* v, int old\_pos, int new\_pos)

**Parameters**

*v* Vector to act on \
 *old\_pos* Position to move element from \
 *new\_pos* Position to move element to

**Returns**

Index of new position (same as `new_pos`) (\>= 0) or WGET\_E\_\* on error (\< 0)

Move the element at position `old_pos` to `new_pos`.

Other elements may change the position.

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

### int wget\_vector\_swap (wget\_vector \* v, int pos1, int pos2)

**Parameters**

*v* Vector to act on \
 *pos1* Position of element one \
 *pos2* Position of element two

**Returns**

Index of second position (same as `pos2`) (\>= 0) or WGET\_E\_\* on error (\< 0)

Swap the two elements at position `pos1` and `pos2`.

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

### void wget\_vector\_free (wget\_vector \*\* v)

**Parameters**

*v* Vector to be free'd

Free the vector `v` and it's contents.

For each element the destructor function is called and the element free'd thereafter. Then the vector itself is free'd and set to NULL.

### void wget\_vector\_clear (wget\_vector \* v)

**Parameters**

*v* Vector to be cleared

Free all elements of the vector `v` but not the vector itself.

For each element the destructor function is called and the element free'd thereafter. The vector is then empty and can be reused.

### void wget\_vector\_clear\_nofree (wget\_vector \* v)

**Parameters**

*v* Vector to be cleared

Remove all elements of the vector `v` without free'ing them. The caller is responsible to care for the elements.

The vector is then empty and can be reused.

### int wget\_vector\_size (const wget\_vector \* v)

**Parameters**

*v* Vector

**Returns**

The number of elements in the vector `v`

Retrieve the number of elements of the vector `v`. If `v` is NULL, 0 is returned.

### void \* wget\_vector\_get (const wget\_vector \* v, int pos)

**Parameters**

*v* Vector \
 *pos* Position of element to retrieve

**Returns**

The element at position `pos` or NULL on error

Retrieve the element at position `pos`.

NULL is returned if `v` is NULL or `pos` is out of range (\< 0 or \> # of entries).

### int wget\_vector\_browse (const wget\_vector \* v, wget\_vector\_browse\_fn \* browse, void \* ctx)

**Parameters**

*v* Vector \
 *browse* Function to be called for each element of `v` \
 *ctx* Context variable use as param to `browse`

**Returns**

Return value of the last call to `browse`

Call 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.

### void wget\_vector\_setcmpfunc (wget\_vector \* v, wget\_vector\_compare\_fn \* cmp)

**Parameters**

*v* Vector \
 *cmp* Function to compare elements

Set the compare function used by **wget\_vector\_sort()**.

### void wget\_vector\_set\_destructor (wget\_vector \* v, wget\_vector\_destructor \* destructor)

**Parameters**

*v* Vector \
 *destructor* Function to be called for element destruction

Set the destructor function that is called for each element to be removed. It should not free the element (pointer) itself.

### void wget\_vector\_sort (wget\_vector \* v)

**Parameters**

*v* Vector

Sort the elements in vector `v` using the compare function. Do nothing if `v` is NULL or the compare function is not set.

### int wget\_vector\_find (const wget\_vector \* v, const void \* elem)

**Parameters**

*v* 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 compare function of the vector.

### bool wget\_vector\_contains (const wget\_vector \* v, const void \* elem)

**Parameters**

*v* Vector \
 *elem* Element to check for

**Returns**

True if element exists, else false

Checks whether the element `elem` exists or not.

### int wget\_vector\_findext (const wget\_vector \* v, int start, int direction, wget\_vector\_find\_fn \* find)

**Parameters**

*v* 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

Call `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.
