/*
 *  btree64k.h
 *
 *  Binary tree header file for jimslide
 *  copyright Jim Leonard 1999
 */

#ifndef __BTREE64K_H_INCLUDED
#define __BTREE64K_H_INCLUDED

#include <stdlib.h>

#define MEMCMP(buf1,buf2,size) memcmp(buf1, buf2, size)
#if 0
extern int (*memcmpFn)(void *, void *, int);
#endif

#define SIZE_BT_POINTER sizeof(void *)
extern size_t bt_entrySize;
extern size_t bt_keySize;
extern size_t bt_nodeSize;

/*
 *  for this pointer structure if node[0] == 0 then it is a NULL pointer
 */
struct BT_Node_s
   {
   struct BT_Node_s *gt_node;
   struct BT_Node_s *lt_node;
   unsigned char entry[1]; /* stub */
   };
typedef struct BT_Node_s BT_Node;
#define SIZE_BT_NODE_HEADER (8)


struct BTree_s
   {
   unsigned int entries;
   unsigned int maxEntries;
   BT_Node *nextNode;
   unsigned char data[1]; /* stub */
   };
typedef struct BTree_s BTree;
#define SIZE_BT_HEADER (12)



struct SList_s
   {
   unsigned int entries;  /* number of entries */
   unsigned int size;     /* size in bytes */
   unsigned char data[1]; /* the data itself */
   };
typedef struct SList_s SList;
#define SIZE_SL_HEADER (8)


/*
 *  Function:     BT_SetBTAttributes
 *
 *  Parameters:
 *       entrysize - size of the structure that is added to the btree
 *       keysize   - length of the key value on an entry.  The key must
 *                   be the first part of the structure
 *
 *  Returns:   1 if success, 0 if failure
 *
 *  Comments:
 */
int BT_SetBTAttributes(size_t entrySize, size_t keySize);

/*
 *  Function:     BT_Init
 *
 *  Parameters:
 *
 *  Returns:   pointer to root of a new binary tree or NULL if failure
 *
 *  Comments:
 *       BT_SetBTAttributes must have previously been called (successfully).
 */
BTree *BT_Init(int numEntries);
void BT_Reset(BTree *btree);


 /*
 *  Function: BT_Free
 *
 *  Parameters:
 *          btree - pointer to btree to be deallocated
 *  Returns:
 *
 *  Comments:
 */
void BT_Free(BTree *btree);

/*
 *  Function: BTAddEntry
 *
 *  Parameters:
 *          btree - pointer to base
 *          entry - pointer to item to be added to btree
 *          overwrite - overwrite flag: if set allow overwrite of entries with
 *                      same key.
 *
 *  Returns: 1 if successful add, 2 if successful overwrite, 0 if an error
 *       occurs, or -1 if an entry with same
 *       key already exists and overwrite flag is not set.
 *
 *  Comments:
 */
int BT_AddEntry(BTree *btree, void *entry, int overWrite);

 /*
 *  Function: BT_FindEntry
 *
 *  Parameters:
 *          bree - base binary tree struct
 *          key - pointer to key value to find
 *  Returns: pointer to entry in btree.  The value portion may be updated
 *       but the key may not!
 *
 *  Comments: changing the key section of this can be devastating to the
 *       structure!  Don't do it!
 */
void *BT_FindEntry(BTree *btree, void *key);



int BT_SetBTAttributes(size_t entrySize, size_t keySize);
BTree *BT_Realloc(BTree *btree);
int BT_NumEntries(BTree *btree);
size_t BT_SizeBtree (BTree *btree);

SList *SL_Init(BTree *btree);
void SL_Free(SList *slist);
void *SL_FindEntry(SList *slist, void *key);
int SL_NumEntries(SList *slist);
size_t SL_SizeSlist (SList *slist);
size_t SL_UncompressedSizeSlist (SList *slist);
SList *SL_Malloc(int numEntries);
void SL_Reset(BTree *btree, SList *slist);
void BT_CvtToLL(BTree *btree);

#endif

