OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
phash.c File Reference
#include "phash.h"
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for phash.c:

Go to the source code of this file.

Classes

struct  phash_bucket
 
struct  phash
 

Macros

#define PHASH_IMPLEMENTATION   1002001
 
#define PHASH_IMPLEMENTATION_STR   "1.2.0"
 
#define PHASH_API_VERSION   1002000
 
#define PHASH_API_VERSION_STR   "1.2.0"
 
#define PHASH_INITIAL_SIZE   64
 
#define PHASH_FILLPCT   66
 

Functions

phashphash_create (uint32_t options)
 
int phash_destroy (phash *h)
 
int phash_remove (phash *h, const char *key)
 
void * phash_get (phash *h, const char *key)
 
int phash_set (phash *h, const char *key, void *value)
 
int phash_rewind (phash *h)
 
int phash_next (phash *h, const char **key, void **value)
 
int phash_size (phash *h)
 
const char * phash_version ()
 

Macro Definition Documentation

◆ PHASH_API_VERSION

#define PHASH_API_VERSION   1002000

Definition at line 4 of file phash.c.

◆ PHASH_API_VERSION_STR

#define PHASH_API_VERSION_STR   "1.2.0"

Definition at line 5 of file phash.c.

◆ PHASH_FILLPCT

#define PHASH_FILLPCT   66

How full to get before doubling the size of the hash, 0-99.

This counts values, not actual buckets filled.

Definition at line 27 of file phash.c.

◆ PHASH_IMPLEMENTATION

#define PHASH_IMPLEMENTATION   1002001

Definition at line 2 of file phash.c.

◆ PHASH_IMPLEMENTATION_STR

#define PHASH_IMPLEMENTATION_STR   "1.2.0"

Definition at line 3 of file phash.c.

◆ PHASH_INITIAL_SIZE

#define PHASH_INITIAL_SIZE   64

Size to initialize hash to upon creation.

This has to be 2^n due to some bit math cheating ("index & size-1" to do modulus division to wrap the target bucket).

Definition at line 19 of file phash.c.

Function Documentation

◆ phash_create()

phash* phash_create ( uint32_t  options)

Initialize a phash object.

Parameters
[in]optionsBitwise OR'd option flags. Currently unused.
Returns
Pointer to malloc'd phash.

Definition at line 107 of file phash.c.

◆ phash_destroy()

int phash_destroy ( phash h)

Destroy a phash object, free'ing the memory used.

Parameters
[in]hPointer to phash to be destroyed.
Returns
0 on success.

Definition at line 136 of file phash.c.

◆ phash_get()

void* phash_get ( phash h,
const char *  key 
)

Find a pointer associated with the given string.

Parameters
[in]hPointer to phash to be searched.
[in]keyKey for which to search.
Returns
Pointer to value, or NULL if key is not found.

Definition at line 205 of file phash.c.

◆ phash_next()

int phash_next ( phash h,
const char **  key,
void **  value 
)

Retrieves the next key-value pair in the phash. The order in which the pointers are returned shall be consistent but may be arbitrary.

Consistent refers only to running identical code and rewinding and traversing the data. Adding or removing pointers may modify the order in which keys are returned.

On end-of-hash, the hash is rewind()'d and the state of the key and value inputs are set to NULL.

Parameters
[in]hPointer to phash to be traversed.
[out]keyPointer to fill with the next key, NULL to ignore.
[out]valuePointer to fill with the next value, NULL to ignore.
Returns
-1 on error, positive 1 on end-of-hash, and 0 on success.

Definition at line 275 of file phash.c.

◆ phash_remove()

int phash_remove ( phash h,
const char *  key 
)

Remove a pointer associated with the given string.

Parameters
[in]hPointer to phash to be modified.
[in]keyKey for which to remove.
Returns
0 on success.

Definition at line 146 of file phash.c.

◆ phash_rewind()

int phash_rewind ( phash h)

Rewind iterator for traversing all the keys and values.

Parameters
[in]hPointer to phash to be reset.
Returns
number of items currently in the phash.

Definition at line 269 of file phash.c.

◆ phash_set()

int phash_set ( phash h,
const char *  key,
void *  value 
)

Add or overwrite a pointer, associating it with the given key.

Parameters
[in]hPointer to phash to be modified.
[in]keyNull-terminated string with which to associate the new pointer.
[in]valueNew pointer to store..
Returns
0 on add, 1 on overwrite, and -1 on error.

Definition at line 224 of file phash.c.

◆ phash_size()

int phash_size ( phash h)

Retrieves the number of key-value pairs in the hash object.

Parameters
[in]hPointer to shash to be analyzed.

Definition at line 312 of file phash.c.

◆ phash_version()

const char* phash_version ( )

Returns the source code version and the implemented API version.

No assumptions are made about the format of the return value.

Definition at line 316 of file phash.c.