zap

A library for algorithmics.

Note: This library is still in it's early stages and is NOT anywhere near being fully optimised.


Building and installation

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.

Documentation

fndbyte

Synopsis

#include <zap/base.h>
size_t zap_fndbtyte(void const * ptr,size_t num,unsigned char byte);

Description

Searches for the byte-value byte in the array pointed to by ptr within the bounds of num.

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.


fndchr

Synopsis

#include <zap/base.h>
size_t zap_fndchr(char const * str,char chr);

Description

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.


foreach

Synopsis

#include <zap/base.h>
void zap_foreach(void * ptr,size_t sz,size_t num,void (* fn)(void *));

Description

Iterates through the array pointed to by ptr, invoking the function fn with a pointer to the current element. Each pointer is equal to the laster pointer plus sz.

If the expression (sz * num) is not a valid object size, the behaviour is undefined.

If fn is not a valid function pointer, the behaviour is undefined.


memcmp

Synopsis

#include <zap/base.h>
int_least8_t zap_memcmp(void const * lptr,size_t num,void const * rptr);

Description

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.


memdup

Synopsis

#include <zap/base.h>
void * zap_memdup(void const * ptr,size_t num);

Description

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.


streq

Synopsis

#include <zap/base.h>
_Bool zap_streq(char const * lstr,char const * rstr);

Description

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.


strfill

Synopsis

#include <zap/base.h>
void zap_strfill(char * str,char chr);

Description

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.


strlen

Synopsis

#include <zap/base.h>
size_t zap_strlen(char const * str);

Description

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.