mdcore  0.1.5
Defines | Functions | Variables
/home/pedro/work/mdcore/src/btree.c File Reference
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "errs.h"
#include "btree.h"

Defines

#define error(id)   ( btree_err = errs_register( id , btree_err_msg[-(id)] , __LINE__ , __FUNCTION__ , __FILE__ ) )

Functions

int btree_delete (struct btree *b, int key, void **res)
 Delete a key from the btree.
int btree_find (struct btree *b, int key, void **res)
 Retrieve the data for a given key.
int btree_dump (struct btree *b, FILE *out)
 Dump the given btree.
int btree_map (struct btree *b, int(*func)(void *, void *), void *data)
 Apply a given function to all data in a btree.
int btree_insert (struct btree *b, int key, void *data)
 Insert a key/data pair into the given btree.
int btree_releasenode (struct btree *b, struct btree_node *n)
 Return a btree_node to the btree's cache.
struct btree_nodebtree_getnode (struct btree *b)
 get a btree_node from the btree's cache.
int btree_init (struct btree *b)
 Initialize the given btree.

Variables

char * btree_err_msg [4]
int btree_err = btree_err_ok

Define Documentation

#define error (   id)    ( btree_err = errs_register( id , btree_err_msg[-(id)] , __LINE__ , __FUNCTION__ , __FILE__ ) )

Function Documentation

int btree_delete ( struct btree b,
int  key,
void **  res 
)

Delete a key from the btree.

Parameters:
bthe btree.
keythe key to search for and remove
resa pointer to a pointer in which the address of the data will be written, if found (may be NULL)
Returns:
1 if the key was found, 0 if not and < 0 on error (see btree_err).
int btree_dump ( struct btree b,
FILE *  out 
)

Dump the given btree.

Parameters:
bthe btree.
outa pointer to the FILE in which to dump the btree.
int btree_find ( struct btree b,
int  key,
void **  res 
)

Retrieve the data for a given key.

Parameters:
bthe btree in which to search.
keythe key to search for.
resa pointer to a pointer in which the address of the data will be written.
Returns:
1 if found, 0 if not and < 0 on err (see btree_err).

Looks for the given key and copies its data pointer to the address given by res.

struct btree_node* btree_getnode ( struct btree b) [read]

get a btree_node from the btree's cache.

Parameters:
thebtree.
Returns:
A pointer to a btree_node or NULL if an error occured (see btree_err).
int btree_init ( struct btree b)

Initialize the given btree.

Parameters:
bthe btree.
Returns:
btree_err_ok or < 0 on error.
int btree_insert ( struct btree b,
int  key,
void *  data 
)

Insert a key/data pair into the given btree.

Parameters:
bthe btree.
keythe integer key.
dataa pointer to the data associated with key.
Returns:
btree_err_ok or < 0 on error (see btree_err).

If a node with the given key already exists, the data pointer is replaced.

int btree_map ( struct btree b,
int(*)(void *, void *)  func,
void *  data 
)

Apply a given function to all data in a btree.

Parameters:
bthe btree.
functhe funciton, which should be of the type btree_maptype.
dataa pointer that will be passed to func with each call.
Returns:
btree_err_ok or < 0 on error (see btree_err).

If func returns < 0 for any node, the traversal is interrupted and an error is returned.

int btree_releasenode ( struct btree b,
struct btree_node n 
)

Return a btree_node to the btree's cache.

Parameters:
bthe btree.
nthe btree_node
Returns:
A pointer to a btree_node or NULL if an error occured (see btree_err).

Variable Documentation

ID of the last error.

char* btree_err_msg[4]
Initial value:
 {
        "Nothing bad happened.",
    "An unexpected NULL pointer was encountered.",
    "A call to malloc failed, probably due to insufficient memory.",
    "The user-supplied mapping function returned an error."
        }
 All Data Structures Files Functions Variables Typedefs Enumerator Defines