/** \file util.h Generic utilities library. All containers in this library except strinb_buffer_t are written so that they don't allocate any memory until the first element is inserted into them. That way it is known to be very cheap to initialize various containers at startup, supporting the fish notion of doing as much lazy initalization as possible. */ #ifndef FISH_UTIL_H #define FISH_UTIL_H #include #include #include typedef void (*func_ptr_t)(); typedef union { long long_val; void *ptr_val; func_ptr_t func_val; } anything_t; /** Data structure for an automatically resizing dynamically allocated queue, */ typedef struct dyn_queue { /** Start of the array */ void **start; /** End of the array*/ void **stop; /** Where to insert elements */ void **put_pos; /** Where to remove elements */ void **get_pos; } dyn_queue_t; /** Internal struct used by hash_table_t. */ typedef struct { /** Hash key*/ void *key; /** Value */ void *data; } hash_struct_t; /** Data structure for the hash table implementaion. A hash table allows for retrieval and removal of any element in O(1), so long as a proper hash function is supplied. The hash table is implemented using a single hash function and element storage directly in the array. When a collision occurs, the hashtable iterates until a zero element is found. When the table is 75% full, it will automatically reallocate itself. This reallocation takes O(n) time. The table is guaranteed to never be more than 75% full or less than 30% full (Unless the table is nearly empty). Its size is always a Mersenne number. */ typedef struct hash_table { /** The array containing the data */ hash_struct_t *arr; /** A simple one item cache. This should always point to the index of the last item to be used */ int cache; /** Number of elements */ int count; /** Length of array */ int size; /** Hash function */ int (*hash_func)( void *key ); /** Comparison function */ int (*compare_func)( void *key1, void *key2 ); } hash_table_t; /** Data structure for an automatically resizing dynamically allocated priority queue. A priority queue allows quick retrieval of the smallest element of a set (This implementation uses O(log n) time). This implementation uses a heap for storing the queue. */ typedef struct priority_queue { /** Array contining the data */ void **arr; /** Number of elements*/ int count; /** Length of array */ int size; /** Comparison function */ int (*compare)(void *e1, void *e2); } priority_queue_t; /** Array list struct. A dynamically growing list that supports stack operations. */ typedef struct array_list { /** Array containing the data */ anything_t *arr; /** Internal cursor position of the array_list_t. This is the position to append elements at. This is also what the array_list_t considers to be its true size, as reported by al_get_count(), etc. Calls to e.g. al_insert will preserve the values of all elements up to pos. */ size_t pos; /** Amount of memory allocated in arr, expressed in number of elements. */ size_t size; } array_list_t; /** Linked list node. */ typedef struct _ll_node { /** Next node */ struct _ll_node *next, /** Previous node */ *prev; /** Node data */ void *data; } ll_node_t; /** Buffer for concatenating arbitrary data. */ typedef struct buffer { char *buff; /**