---
name: "libwget-hashmap"
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-hashmap"
---

# libwget-hashmap(3)

Hashmap functions

## NAME

libwget-hashmap - Hashmap functions

## SYNOPSIS

### Functions

**wget\_hashmap\_iterator** \* **wget\_hashmap\_iterator\_alloc** (**wget\_hashmap** \*h)\
 void **wget\_hashmap\_iterator\_free** (**wget\_hashmap\_iterator** \*\*iter)\
 void \* **wget\_hashmap\_iterator\_next** (**wget\_hashmap\_iterator** \*iter, void \*\*value)\
 **wget\_hashmap** \* **wget\_hashmap\_create** (int max, **wget\_hashmap\_hash\_fn** \*hash, **wget\_hashmap\_compare\_fn** \*cmp)\
 int **wget\_hashmap\_put** (**wget\_hashmap** \*h, const void \*key, const void \*value)\
 int **wget\_hashmap\_contains** (const **wget\_hashmap** \*h, const void \*key)\
 int **wget\_hashmap\_get** (const **wget\_hashmap** \*h, const void \*key, void \*\*value)\
 int **wget\_hashmap\_remove** (**wget\_hashmap** \*h, const void \*key)\
 int **wget\_hashmap\_remove\_nofree** (**wget\_hashmap** \*h, const void \*key)\
 void **wget\_hashmap\_free** (**wget\_hashmap** \*\*h)\
 void **wget\_hashmap\_clear** (**wget\_hashmap** \*h)\
 int **wget\_hashmap\_size** (const **wget\_hashmap** \*h)\
 int **wget\_hashmap\_browse** (const **wget\_hashmap** \*h, **wget\_hashmap\_browse\_fn** \*browse, void \*ctx)\
 void **wget\_hashmap\_setcmpfunc** (**wget\_hashmap** \*h, **wget\_hashmap\_compare\_fn** \*cmp)\
 int **wget\_hashmap\_sethashfunc** (**wget\_hashmap** \*h, **wget\_hashmap\_hash\_fn** \*hash)\
 void **wget\_hashmap\_set\_key\_destructor** (**wget\_hashmap** \*h, **wget\_hashmap\_key\_destructor** \*destructor)\
 void **wget\_hashmap\_set\_value\_destructor** (**wget\_hashmap** \*h, **wget\_hashmap\_value\_destructor** \*destructor)\
 void **wget\_hashmap\_set\_load\_factor** (**wget\_hashmap** \*h, float factor)\
 void **wget\_hashmap\_set\_resize\_factor** (**wget\_hashmap** \*h, float factor)\
 typedef struct **wget\_hashmap\_st** **wget\_hashmap**\
 Type of the hashmap.  typedef int **wget\_hashmap\_compare\_fn**(const void \*key1, const void \*key2)\
 Type of the hashmap compare function.  typedef unsigned int **wget\_hashmap\_hash\_fn**(const void \*key)\
 Type of the hashmap hash function.  typedef int **wget\_hashmap\_browse\_fn**(void \*ctx, const void \*key, void \*value)\
 Type of the hashmap browse callback function.  typedef void **wget\_hashmap\_key\_destructor**(void \*key)\
 Type of the hashmap key destructor function.  typedef void **wget\_hashmap\_value\_destructor**(void \*value)\
 Type of the hashmap value destructor function.

## Detailed Description

Hashmaps are key/value stores that perform at O(1) for insertion, searching and removing.

## Function Documentation

### wget\_hashmap\_iterator \* wget\_hashmap\_iterator\_alloc (wget\_hashmap \* h)

**Parameters**

*h* Hashmap

**Returns**

New iterator instance for `h`

Creates a hashmap iterator for `h`.

### void wget\_hashmap\_iterator\_free (wget\_hashmap\_iterator \*\* iter)

**Parameters**

*iter* Hashmap iterator

Free the given iterator `iter`.

### void \* wget\_hashmap\_iterator\_next (wget\_hashmap\_iterator \* iter, void \*\* value)

**Parameters**

*iter* Hashmap iterator \
 *value* Pointer to the value belonging to the returned key

**Returns**

Pointer to the key or NULL if no more elements left

Returns the next key / value in the hashmap. If all key/value pairs have been iterated over the function returns NULL and `value` is untouched.

When iterating over a hashmap, the order of returned key/value pairs is not defined.

### wget\_hashmap \* wget\_hashmap\_create (int max, wget\_hashmap\_hash\_fn \* hash, wget\_hashmap\_compare\_fn \* cmp)

**Parameters**

*max* Initial number of pre-allocated entries \
 *hash* Hash function to build hashes from elements \
 *cmp* Comparison function used to find elements

**Returns**

New hashmap instance

Create a new hashmap instance with initial size `max`. It should be free'd after use with **wget\_hashmap\_free()**.

Before the first insertion of an element, `hash` and `cmp` must be set. So if you use NULL values here, you have to call **wget\_hashmap\_setcmpfunc()** and/or wget\_hashmap\_hashcmpfunc() with appropriate function pointers. No doing so will result in undefined behavior (likely you'll see a segmentation fault).

### int wget\_hashmap\_put (wget\_hashmap \* h, const void \* key, const void \* value)

**Parameters**

*h* Hashmap to put data into \
 *key* Key to insert into `h` \
 *value* Value to insert into `h`

**Returns**

0 if inserted a new entry, 1 if entry existed, WGET\_E\_MEMORY if internal allocation failed

Insert a key/value pair into hashmap `h`.

`key` and `value` are *not* cloned, the hashmap takes 'ownership' of both.

If `key` already exists and the pointer values the old and the new key differ, the old key will be destroyed by calling the key destructor function (default is free()).

To realize a hashset (just keys without values), `value` may be NULL.

Neither `h` nor `key` must be NULL, else the return value will always be 0.

### int wget\_hashmap\_contains (const wget\_hashmap \* h, const void \* key)

**Parameters**

*h* Hashmap \
 *key* Key to search for

**Returns**

1 if `key` has been found, 0 if not found

Check if `key` exists in `h`.

### int wget\_hashmap\_get (const wget\_hashmap \* h, const void \* key, void \*\* value)

**Parameters**

*h* Hashmap \
 *key* Key to search for \
 *value* Value to be returned

**Returns**

1 if `key` has been found, 0 if not found

Get the value for a given key.

Neither `h` nor `key` must be NULL.

### int wget\_hashmap\_remove (wget\_hashmap \* h, const void \* key)

**Parameters**

*h* Hashmap \
 *key* Key to be removed

**Returns**

1 if `key` has been removed, 0 if not found

Remove `key` from hashmap `h`.

If `key` is found, the key and value destructor functions are called when removing the entry from the hashmap.

### int wget\_hashmap\_remove\_nofree (wget\_hashmap \* h, const void \* key)

**Parameters**

*h* Hashmap \
 *key* Key to be removed

**Returns**

1 if `key` has been removed, 0 if not found

Remove `key` from hashmap `h`.

Key and value destructor functions are *not* called when removing the entry from the hashmap.

### void wget\_hashmap\_free (wget\_hashmap \*\* h)

**Parameters**

*h* Hashmap to be free'd

Remove all entries from hashmap `h` and free the hashmap instance.

Key and value destructor functions are called for each entry in the hashmap.

### void wget\_hashmap\_clear (wget\_hashmap \* h)

**Parameters**

*h* Hashmap to be cleared

Remove all entries from hashmap `h`.

Key and value destructor functions are called for each entry in the hashmap.

### int wget\_hashmap\_size (const wget\_hashmap \* h)

**Parameters**

*h* Hashmap

**Returns**

Number of entries in hashmap `h`

Return the number of entries in the hashmap `h`.

### int wget\_hashmap\_browse (const wget\_hashmap \* h, wget\_hashmap\_browse\_fn \* browse, void \* ctx)

**Parameters**

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

**Returns**

Return value of the last call to `browse`

Call function `browse` for each element of hashmap `h` 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 either `h` or `browse` is NULL.

### void wget\_hashmap\_setcmpfunc (wget\_hashmap \* h, wget\_hashmap\_compare\_fn \* cmp)

**Parameters**

*h* Hashmap \
 *cmp* Comparison function used to find keys

Set the comparison function.

### int wget\_hashmap\_sethashfunc (wget\_hashmap \* h, wget\_hashmap\_hash\_fn \* hash)

**Parameters**

*h* Hashmap \
 *hash* Hash function used to hash keys

**Returns**

WGET\_E\_SUCCESS if set successfully, else WGET\_E\_MEMORY or WGET\_E\_INVALID

Set the key hash function.

The keys of all entries in the hashmap will be hashed again. This includes a memory allocation, so there is a possibility of failure.

### void wget\_hashmap\_set\_key\_destructor (wget\_hashmap \* h, wget\_hashmap\_key\_destructor \* destructor)

**Parameters**

*h* Hashmap \
 *destructor* Destructor function for keys

Set the key destructor function.

Default is free().

### void wget\_hashmap\_set\_value\_destructor (wget\_hashmap \* h, wget\_hashmap\_value\_destructor \* destructor)

**Parameters**

*h* Hashmap \
 *destructor* Destructor function for values

Set the value destructor function.

Default is free().

### void wget\_hashmap\_set\_load\_factor (wget\_hashmap \* h, float factor)

**Parameters**

*h* Hashmap \
 *factor* The load factor

Set the load factor function.

The load factor is determines when to resize the internal memory. 0.75 means 'resize if 75% or more of all slots are used'.

The resize strategy is set by wget\_hashmap\_set\_growth\_policy().

The resize (and rehashing) occurs earliest on the next insertion of a new key.

Default is 0.75.

### void wget\_hashmap\_set\_resize\_factor (wget\_hashmap \* h, float factor)

**Parameters**

*h* Hashmap \
 *factor* Hashmap growth factor

Set the factor for resizing the hashmap when it's load factor is reached.

The new size is 'factor \* oldsize'. If the new size is less or equal 0, the involved put function will do nothing and the internal state of the hashmap will not change.

Default is 2.

## Author

Generated automatically by Doxygen for wget2 from the source code.
