Classes | Typedefs | Functions
zarray.h File Reference
#include <stddef.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for zarray.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  zarray
 

Typedefs

typedef struct zarray zarray_t
 

Functions

static int iceillog2 (int v)
 
static void zarray_add (zarray_t *za, const void *p)
 
static void zarray_add_all (zarray_t *dest, const zarray_t *source)
 
static void zarray_clear (zarray_t *za)
 
static int zarray_contains (const zarray_t *za, const void *p)
 
static zarray_tzarray_copy (const zarray_t *za)
 
static size_t zarray_copy_data (const zarray_t *za, void *buffer, size_t buffer_bytes)
 
static zarray_tzarray_copy_subset (const zarray_t *za, int start_idx, int end_idx_exclusive)
 
static zarray_tzarray_create (size_t el_sz)
 
static void zarray_destroy (zarray_t *za)
 
static void zarray_ensure_capacity (zarray_t *za, int capacity)
 
static void zarray_get (const zarray_t *za, int idx, void *p)
 
static void zarray_get_volatile (const zarray_t *za, int idx, void *p)
 
static int zarray_index_of (const zarray_t *za, const void *p)
 
static void zarray_insert (zarray_t *za, int idx, const void *p)
 
static void zarray_map (zarray_t *za, void(*f)(void *))
 
static void zarray_remove_index (zarray_t *za, int idx, int shuffle)
 
static int zarray_remove_value (zarray_t *za, const void *p, int shuffle)
 
static void zarray_set (zarray_t *za, int idx, const void *p, void *outp)
 
static int zarray_size (const zarray_t *za)
 
static void zarray_sort (zarray_t *za, int(*compar)(const void *, const void *))
 
static void zarray_truncate (zarray_t *za, int sz)
 
void zarray_vmap (zarray_t *za, void(*f)())
 
int zstrcmp (const void *a_pp, const void *b_pp)
 

Typedef Documentation

typedef struct zarray zarray_t

Defines a structure which acts as a resize-able array ala Java's ArrayList.

Definition at line 48 of file zarray.h.

Function Documentation

static int iceillog2 ( int  v)
static

Definition at line 101 of file zarray.h.

static void zarray_add ( zarray_t za,
const void *  p 
)
inlinestatic

Adds a new element to the end of the supplied array, and sets its value (by copying) from the data pointed to by the supplied pointer 'p'. Automatically ensures that enough storage space is available for the new element.

Definition at line 185 of file zarray.h.

static void zarray_add_all ( zarray_t dest,
const zarray_t source 
)
inlinestatic

Add all elements from 'source' into 'dest'. el_size must be the same for both lists

Definition at line 472 of file zarray.h.

static void zarray_clear ( zarray_t za)
inlinestatic

Removes all elements from the array and sets its size to zero. Pointers to any data elements obtained i.e. by zarray_get_volatile() will no longer be valid.

Definition at line 391 of file zarray.h.

static int zarray_contains ( const zarray_t za,
const void *  p 
)
inlinestatic

Determines whether any element in the array has a value which matches the data pointed to by 'p'.

Returns 1 if a match was found anywhere in the array, else 0.

Definition at line 403 of file zarray.h.

static zarray_t* zarray_copy ( const zarray_t za)
inlinestatic

Allocate a new zarray that contains a copy of the data in the argument.

Definition at line 88 of file zarray.h.

static size_t zarray_copy_data ( const zarray_t za,
void *  buffer,
size_t  buffer_bytes 
)
inlinestatic

Copies the memory array used internally by zarray to store its owned elements to the address pointed by 'buffer'. It is the caller's responsibility to allocate zarray_size()*el_sz bytes for the copy to be stored and to free the memory when no longer needed. The memory allocated at 'buffer' and the internal zarray storage must not overlap. 'buffer_bytes' should be the size of the 'buffer' memory space, in bytes, and must be at least zarray_size()*el_sz.

Returns the number of bytes copied into 'buffer'.

Definition at line 246 of file zarray.h.

static zarray_t* zarray_copy_subset ( const zarray_t za,
int  start_idx,
int  end_idx_exclusive 
)
inlinestatic

Allocate a new zarray that contains a subset of the original elements. NOTE: end index is EXCLUSIVE, that is one past the last element you want.

Definition at line 118 of file zarray.h.

static zarray_t* zarray_create ( size_t  el_sz)
inlinestatic

Creates and returns a variable array structure capable of holding elements of the specified size. It is the caller's responsibility to call zarray_destroy() on the returned array when it is no longer needed.

Definition at line 63 of file zarray.h.

static void zarray_destroy ( zarray_t za)
inlinestatic

Frees all resources associated with the variable array structure which was created by zarray_create(). After calling, 'za' will no longer be valid for storage.

Definition at line 76 of file zarray.h.

static void zarray_ensure_capacity ( zarray_t za,
int  capacity 
)
inlinestatic

Returns 1 if zarray_size(za) == 0, returns 0 otherwise. Allocates enough internal storage in the supplied variable array structure to guarantee that the supplied number of elements (capacity) can be safely stored.

Definition at line 164 of file zarray.h.

static void zarray_get ( const zarray_t za,
int  idx,
void *  p 
)
inlinestatic

Retrieves the element from the supplied array located at the zero-based index of 'idx' and copies its value into the variable pointed to by the pointer 'p'.

Definition at line 201 of file zarray.h.

static void zarray_get_volatile ( const zarray_t za,
int  idx,
void *  p 
)
inlinestatic

Similar to zarray_get(), but returns a "live" pointer to the internal storage, avoiding a memcpy. This pointer is not valid across operations which might move memory around (i.e. zarray_remove_value(), zarray_remove_index(), zarray_insert(), zarray_sort(), zarray_clear()). 'p' should be a pointer to the pointer which will be set to the internal address.

Definition at line 218 of file zarray.h.

static int zarray_index_of ( const zarray_t za,
const void *  p 
)
inlinestatic

Find the index of an element, or return -1 if not found. Remember that p is a pointer to the element.

Definition at line 453 of file zarray.h.

static void zarray_insert ( zarray_t za,
int  idx,
const void *  p 
)
inlinestatic

Creates a new entry and inserts it into the array so that it will have the index 'idx' (i.e. before the item which currently has that index). The value of the new entry is set to (copied from) the data pointed to by 'p'. 'idx' can be one larger than the current max index to place the new item at the end of the array, or zero to add it to an empty array.

Definition at line 322 of file zarray.h.

static void zarray_map ( zarray_t za,
void(*)(void *)  f 
)
inlinestatic

Calls the supplied function for every element in the array in index order. The map function will be passed a pointer to each element in turn and must have the following format:

void map_function(element_type *element)

Definition at line 365 of file zarray.h.

static void zarray_remove_index ( zarray_t za,
int  idx,
int  shuffle 
)
inlinestatic

Removes the entry at index 'idx'. If shuffle is true, the last element in the array will be placed in the newly-open space; if false, the zarray is compacted.

Definition at line 260 of file zarray.h.

static int zarray_remove_value ( zarray_t za,
const void *  p,
int  shuffle 
)
inlinestatic

Remove the entry whose value is equal to the value pointed to by 'p'. If shuffle is true, the last element in the array will be placed in the newly-open space; if false, the zarray is compacted. At most one element will be removed.

Note that objects will be compared using memcmp over the full size of the value. If the value is a struct that contains padding, differences in the padding bytes can cause comparisons to fail. Thus, it remains best practice to bzero all structs so that the padding is set to zero.

Returns the number of elements removed (0 or 1).

Definition at line 299 of file zarray.h.

static void zarray_set ( zarray_t za,
int  idx,
const void *  p,
void *  outp 
)
inlinestatic

Sets the value of the current element at index 'idx' by copying its value from the data pointed to by 'p'. The previous value of the changed element will be copied into the data pointed to by 'outp' if it is not null.

Definition at line 345 of file zarray.h.

static int zarray_size ( const zarray_t za)
inlinestatic

Retrieves the number of elements currently being contained by the passed array, which may be different from its capacity. The index of the last element in the array will be one less than the returned value.

Definition at line 136 of file zarray.h.

static void zarray_sort ( zarray_t za,
int(*)(const void *, const void *)  compar 
)
inlinestatic

Uses qsort() to sort the elements contained by the array in ascending order. Uses the supplied comparison function to determine the appropriate order.

The comparison function will be passed a pointer to two elements to be compared and should return a measure of the difference between them (see strcmp()). I.e. it should return a negative number if the first element is 'less than' the second, zero if they are equivalent, and a positive number if the first element is 'greater than' the second. The function should have the following format:

int comparison_function(const element_type *first, const element_type *second)

zstrcmp() can be used as the comparison function for string elements, which will call strcmp() internally.

Definition at line 432 of file zarray.h.

static void zarray_truncate ( zarray_t za,
int  sz 
)
inlinestatic

Definition at line 228 of file zarray.h.

void zarray_vmap ( zarray_t za,
void(*)()  f 
)

Calls the supplied function for every element in the array in index order. HOWEVER values are passed to the function, not pointers to values. In the case where the zarray stores object pointers, zarray_vmap allows you to pass in the object's destroy function (or free) directly. Can only be used with zarray's which contain pointer data. The map function should have the following format:

void map_function(element_type *element)

Definition at line 51 of file zarray.c.

int zstrcmp ( const void *  a_pp,
const void *  b_pp 
)

A comparison function for comparing strings which can be used by zarray_sort() to sort arrays with char* elements.

Definition at line 40 of file zarray.c.



apriltags2
Author(s): Danylo Malyuta
autogenerated on Fri Oct 19 2018 04:02:33