A library for algorithmics.
Note: This library is still in it's early stages and is NOT anywhere near being fully optimised.
The provided makefile has been tested to work with both GNU Make and BSD Make.
The default target builds the static library file (located at zap/libzap.a). The target clean removes object files, whilst purge removes object files and the static library file.
Currently, zap doesn't support being compiled as a shared library out of the box, but the makefile could be modified to allow this.
The install target installs the headers to $(HDRDIR) and the library file to $(LIBDIR).
Instructions for building the test program may be found on the first line of test.c.
#include <zap/math.h>
signed char zap_abs_c(signed char val);
int zap_abs_i(int val);
long zap_abs_l(long val);
long long zap_abs_ll(long long val);
short zap_abs_s(short val);
Returns the absolute value of val.
#include <zap/math.h>
signed char zap_fma_c(signed char a,signed char b,signed char c);
int zap_fma_i(int a,int b,int c);
long zap_fma_l(long a,long b,long c);
long long zap_fma_ll(long long a,long long b,long long c);
short zap_fma_s(short a,short b,short c);
unsigned char zap_fma_uc(unsigned char a,unsigned char b,unsigned char c);
unsigned int zap_fma_ui(unsigned int a,unsigned int b,unsigned int c);
unsigned long zap_fma_ul( unsigned long a,unsigned long b,unsigned long c);
unsigned long long zap_fma_ull(unsigned long long a,unsigned long long b,unsigned long long c);
unsigned short zap_fma_us( unsigned short a,unsigned short b,unsigned short c);
Returns the the fused multiply-add of a, b, and c.
#include <zap/mem.h>
size_t zap_fndbtyte(void const * ptr,size_t num,unsigned char byte);
Searches for the byte-value byte in the array pointed to by ptr within the bounds of num.
If ptr is not a valid pointer to a null-terminated string, the behaviour is undefined.
If num is larger (but not smaller) than the number of bytes in the array, the behaviour is undefined.
If the byte-value is found within the domain, the position of it's first occurrence (starting at zero) is returned. Otherwise, SIZE_MAX is returned.
#include <zap/mem.h>
size_t zap_fndchr(char const * str,char chr);
Searches for the character chr in the string str.
If the character is found in the string, the position of it's first occurrence (starting at zero) is returned. Otherwise, SIZE_MAX is returned.
If str is not a valid pointer to a null-terminated string, the behaviour is undefined.
#include <zap/mem.h>
void zap_foreach(void * ptr,size_t sz,size_t num,void (* fn)(void *),void * * rem);
Iterates through the array pointed to by ptr, invoking the function fn with a pointer to the current element. Each pointer value after the first is equal to the previous plus sz.
If the pointed-to object of rem is not a null pointer, the iterations are started from that address. If fn throws an exception, the current position is written to the pointed-to object of rem (unless rem is a null pointer) and foreach returns the control-flow to the caller via this exception.
If the expression (sz * num)
is not a valid object size, the behaviour is undefined.
If fn is not a valid pointer to a function with C language linkage, the behaviour is undefined.
If rem is not a null pointer and the pointed-to object is not initialised to NULL before the call to foreach, the behaviour is undefined.
#include <zap/mem.h>
int_least8_t zap_memcmp(void const * lptr,size_t num,void const * rptr);
Compares num-bytes of the arrays pointed to by lptr and rptr.
The returned value is determined by the first byte found to be different in the two arrays. If the byte has a larger value in lptr, a negative (less than zero) value is returned. If it's the other way, a positive (greater than zero) value is returned. Otherwise (the arrays where represented the same), zero is returned.
If lptr or rptr (or both) are not valid pointers to arrays, the behaviour is undefined.
If num is larger (but not smaller) than the number of bytes of the smallest array, the behaviour is undefined.
#include <zap/mem.h>
void zap_memcpy(void const * in,size_t num,void * out);
Copies num-bytes from the buffer pointed to by in into the same relative position in the buffer pointer to by out.
If in or out (or both) are not valid pointers to arrays, the behaviour is undefined.
If num is larger (but not smaller) than the number of bytes of the smallest array, the behaviour is undefined.
#include <zap/mem.h>
void * zap_memdup(void const * ptr,size_t num);
Copies num-bytes from the array pointed to by ptr into a newly-allocated array. The new array is allocated by malloc.
The returned value is a pointer to the new array.
If num is larger (but not smaller) than the number of bytes in the array, the behaviour is undefined.
#include <zap/mem.h>
bool zap_memeq(void const * lptr,size_t num,void * rptr);
Checks num-bytes of the buffers pointed to by lptr and rptr for equality.
If any two bytes are found to be different in the two buffers, false is returned. Otherwise, true is returned.
If lptr or rptr (or both) are not valid pointers to arrays, the behaviour is undefined.
If num is larger (but not smaller) than the number of bytes of the smallest array, the behaviour is undefined.
#include <zap/mem.h>
void zap_memfill(void const * ptr,size_t num,unsigned char byte);
Fills num-bytes of the buffer pointed to by ptr with the representation of byte.
If ptr is not a valid pointer to an array, the behaviour is undefined.
If num is larger (but not smaller) than the number of bytes of in the array, the behaviour is undefined.
#include <zap/mem.h>
bool zap_streq(char const * lstr,char const * rstr);
Checks the equality of the strings lstr and rstr.
If one of the strings has a length different from the other, or if any character in the two strings is different from the other (at the same offset), true is returned. Otherwise, false is returned.
If lstr or rstr (or both) are not valid pointers to null-terminated strings, the behaviour is undefined.
#include <zap/mem.h>
void zap_strfill(char * str,char chr);
Writes the character chr to every valid position in the string str, excluding that of the null-terminator.
If str is not a valid pointer to a null-terminated string, the behaviour is undefined.
#include <zap/mem.h>
size_t zap_strlen(char const * str);
Counts the number of characters in the string str.
Returns the number of characters (excluding the null-terminator) in the string.
If str is not a valid pointer to a null-terminated string, the behaviour is undefined.