sk_new() -- SSLeay 0.6.6 -- February 1997

NAME

sk_new, sk_free, sk_pop_free, sk_insert, sk_delete, sk_delete_ptr,
sk_find, sk_push, sk_unshift, sk_shift, sk_pop -- stack manipulation routines

SYNOPSIS

#include "stack.h"

long sk_num(sk)
STACK *sk;

char *sk_value(sk,n)
STACK *sk;
unsigned int n;

STACK *sk_new(c)
int (*c)();

void sk_free(sk)
STACK *sk;

void sk_pop_free(sk,func)
STACK *st; void (*func)();

int sk_insert(sk, data, where)
STACK *sk;
char *data;
int where;

char *sk_delete(st,loc)
STACK *st;
int loc;

char *sk_delete_ptr(st,p)
STACK *st;
char *p;

int sk_find(st,data)
STACK *st;
char *data;

int sk_push(st,data)
STACK *st;
char *data;

int sk_unshift(st,data)
STACK *st;
char *data;

char *sk_shift(st)
STACK *st;

char *sk_pop(st)
STACK *st;

DESCRIPTION

All of these functions operate on a STACK:

typedef struct stack_st
	{
	unsigned int num;
	char **data;
	int sorted;

	unsigned int num_alloc;
	int (*comp)();
	} STACK;

num holds the number of elements in the stack, data is the array of elements. sorted is 1 if the list has been sorted, 0 if not.

num_alloc is the number of slots for elements that have been allocated in data. When num becomes larger than num_alloc, data is realloced to a larger size.

If comp is set, it is a function that is used to compare 2 of the items in the stack. The function should return -1, 0 or 1, depending on the ordering.

Due to the way element pointers are kept in a malloc()ed array, the most efficient way to use this structure is to add and delete elements from the end via sk_pop() and sk_push(). If you wish to do lookups sk_find() is quite efficient since it will sort the stack (if required) and then do a binary search to lookup the requested item. This sorting is done for you automatically if it has not been done since the last time an element was added. Do remember that if you do an sk_find(), the order of the elements will change.

sk_num returns the number of elements in sk. It is actually a macro:

#define sk_num(sk)	((sk)->num)

sk_value returns the nth element of sk. It is actually a macro:

#define sk_value(sk,n)	((sk)->data[n])

sk_new creates a new stack. If c, the comparison function, is not specified, the various functions that operate on a sorted STACK will not work (sk_find()). NULL is returned on failure.

sk_free frees a STACK structure. The elements in the stack will not be freed so one should pop and free all elements from the stack before calling this function or call sk_pop_free() instead.

sk_pop_free calls func for each element on the stack, passing the element as the argument. sk_free is then called to free the STACK structure.

sk_insert inserts data into sk at location where. If where is larger that the number of elements in the stack, the element is put at the end. This function is used by many of the other stack manipulation routines. It returns 0 on failure, otherwise the number of elements in the new stack.

sk_delete removes the item at location loc from the stack and returns it. Returns NULL if the loc is out of range.

sk_delete_ptr deletes the data item pointed to by p, if it is in the stack, and returns it. NULL is returned if the element is not in the stack.

sk_find returns the location that contains a value that is either equal or greater than data. If the comparison function was not set, this function does not operate. This function actually qsort()s the stack if it is not in order and then uses bsearch() to do the initial search. If the comparison function is not set, -1 is returned.

sk_push appends data to the stack. 0 is returned if there is a failure (due to a malloc failure), else 1. This is the same as sk_insert(st,data,sk_num(st)).

sk_unshift prepends data to the front (location 0) of the stack. This is the same as sk_insert(st,data,0).

sk_shift deletes from the stack the first element in the stack and returns it. This is the same as sk_delete(st,0).

sk_pop deletes the last element on the stack and returns it. This is the same as sk_delete(st,sk_num(sk)-1).