2007-01-15 12:09:25 -08:00
|
|
|
/*
|
|
|
|
This file is part of Warzone 2100.
|
|
|
|
Copyright (C) 1999-2004 Eidos Interactive
|
|
|
|
Copyright (C) 2005-2007 Warzone Resurrection Project
|
|
|
|
|
|
|
|
Warzone 2100 is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Warzone 2100 is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Warzone 2100; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2006-09-25 09:46:28 -07:00
|
|
|
/*! \file treap.h
|
|
|
|
* * \brief A balanced binary tree implementation
|
2007-06-28 10:47:08 -07:00
|
|
|
*
|
|
|
|
* Overhead for using the treap is -
|
|
|
|
* Overhead for the heap used by the treap :
|
|
|
|
* 24 bytes + 4 bytes per extension
|
|
|
|
* 12 bytes for the root
|
|
|
|
* 20 bytes per node
|
|
|
|
*/
|
|
|
|
#ifndef _treap_h
|
|
|
|
#define _treap_h
|
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
/* Turn on and off the treap debugging */
|
|
|
|
#ifdef DEBUG
|
2008-03-24 09:51:17 -07:00
|
|
|
// #define DEBUG_TREAP true
|
2007-06-28 10:47:08 -07:00
|
|
|
#else
|
|
|
|
#undef DEBUG_TREAP
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* Function type for the object compare
|
|
|
|
* return -1 for less
|
|
|
|
* 1 for more
|
|
|
|
* 0 for equal
|
|
|
|
*/
|
2008-07-14 15:09:31 -07:00
|
|
|
typedef int (*TREAP_CMP)(const void *key1, const void *key2);
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2008-07-14 13:41:22 -07:00
|
|
|
/// Forward declaration to allow pointers to this type
|
|
|
|
struct TREAP;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
/****************************************************************************************/
|
|
|
|
/* Function Protoypes */
|
|
|
|
/* */
|
|
|
|
/* These should not be called directly - use the macros below */
|
|
|
|
|
|
|
|
|
2006-09-16 10:41:40 -07:00
|
|
|
/**
|
|
|
|
* Store the location in C code at which a call to the heap was made
|
|
|
|
*
|
2008-07-14 13:41:22 -07:00
|
|
|
* \param fileName source filename
|
2006-09-16 10:41:40 -07:00
|
|
|
* \param lineNumber source file line number
|
|
|
|
*/
|
2008-07-14 13:41:22 -07:00
|
|
|
extern void treapSetCallPos(const char* fileName, int lineNumber);
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
/* Function type for object equality */
|
|
|
|
//typedef BOOL (*TREAP_EQUAL)(void *pObj1, void *pObj2);
|
|
|
|
|
2006-09-16 10:41:40 -07:00
|
|
|
/**
|
|
|
|
* Function to create a treap
|
|
|
|
*
|
|
|
|
* \param ppsTreap out-parameter which holds the created treap
|
|
|
|
* \param cmp comparison function to use
|
|
|
|
* \return true, if the treap creation was successfull
|
2007-06-28 10:47:08 -07:00
|
|
|
*/
|
2008-07-14 13:41:22 -07:00
|
|
|
extern BOOL treapCreate(struct TREAP **ppsTreap, TREAP_CMP cmp);
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
/* Add an object to a treap
|
|
|
|
*/
|
2008-07-14 13:41:22 -07:00
|
|
|
extern BOOL treapAdd(struct TREAP *psTreap, void *key, void *pObj);
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
/* Remove an object from the treap */
|
2008-07-14 13:41:22 -07:00
|
|
|
extern BOOL treapDel(struct TREAP *psTreap, void *key);
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
/* Find an object in a treap */
|
2008-07-14 13:41:22 -07:00
|
|
|
extern void *treapFind(struct TREAP *psTreap, const void *key);
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
/* Destroy a treap and release all the memory associated with it */
|
2008-07-14 13:41:22 -07:00
|
|
|
extern void treapDestroy(struct TREAP *psTreap);
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
/* Return the object with the smallest key in the treap
|
|
|
|
* This is useful if the objects in the treap need to be
|
|
|
|
* deallocated. i.e. getSmallest, delete from treap, free memory
|
|
|
|
*/
|
2008-07-14 13:41:22 -07:00
|
|
|
extern void *treapGetSmallest(struct TREAP *psTreap);
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
/****************************************************************************************/
|
|
|
|
/* Comparison Functions */
|
|
|
|
|
2006-11-24 17:26:05 -08:00
|
|
|
/* A useful comparison function - keys are char pointers */
|
2008-07-14 15:09:31 -07:00
|
|
|
extern int treapStringCmp(const void *key1, const void *key2);
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
/****************************************************************************************/
|
|
|
|
/* Macro definitions */
|
|
|
|
|
2006-08-25 09:58:49 -07:00
|
|
|
#ifdef DEBUG_TREAP
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
// debugging versions of the TREAP calls
|
2007-12-10 15:15:46 -08:00
|
|
|
#define TREAP_CREATE(ppsTreap, cmp) \
|
2007-06-28 10:47:08 -07:00
|
|
|
(treapSetCallPos(__FILE__, __LINE__), \
|
2007-12-10 15:15:46 -08:00
|
|
|
treapCreate(ppsTreap, cmp))
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
#define TREAP_ADD(psTreap, key, pObject) \
|
|
|
|
(treapSetCallPos(__FILE__, __LINE__), \
|
|
|
|
treapAdd(psTreap, key, pObject))
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
// release versions of the TREAP calls
|
2007-12-10 15:15:46 -08:00
|
|
|
#define TREAP_CREATE(ppsTreap, cmp) \
|
|
|
|
treapCreate(ppsTreap, cmp)
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
#define TREAP_ADD(psTreap, key, pObject) \
|
|
|
|
treapAdd(psTreap, key, pObject)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|