//-----------------------------------------------------------------------------
// Copyright © 2003 - Philip Howard - All rights reserved
//
// This program 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.
//
// This program 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 this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//-----------------------------------------------------------------------------
// package	libh/avl
// homepage	http://libh.slashusr.org/
//-----------------------------------------------------------------------------
// author	Philip Howard
// email	libh at ipal dot org
// homepage	http://phil.ipal.org/
//-----------------------------------------------------------------------------
// This file is best viewed using a fixed spaced font such as Courier
// and in a display at least 120 columns wide.
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// G. M. Adelson-Velskii and E. M. Landis are the inventors of the algorithm
// bearing their initials, AVL.  Their algorithm for height-balanced binary
// search trees is the basis of the code in this package.  Some modification
// is made to the balancing operations, and features are added for practical
// purposes.
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// file		avl_lib.h
//
// purpose	Define resources for the AVL binary tree package
//		that are only needed by the library code itself.
//-----------------------------------------------------------------------------
#ifndef __AVL_LIB_H__
#define __AVL_LIB_H__

//-----------------------------------------------------------------------------
// headers
//-----------------------------------------------------------------------------
#include <limits.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "libh_common.h"

//-----------------------------------------------------------------------------
// configuration
//-----------------------------------------------------------------------------
#ifndef AVL_COUNT_ENABLE
#define AVL_COUNT_ENABLE		0
#endif

#ifndef AVL_CHECK_ARGS
#define AVL_CHECK_ARGS			1
#endif

#ifndef AVL_CHECK_INTERNAL_ARGS
#define AVL_CHECK_INTERNAL_ARGS		1
#endif

#ifndef AVL_CLEAN_NODE
#define AVL_CLEAN_NODE			1
#endif

#ifndef AVL_PARANOID_LOGIC
#define AVL_PARANOID_LOGIC		1
#endif

#include "libh/avl.h"

//-----------------------------------------------------------------------------
// structs
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// macros
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// macro	avl_compare_keys
//
// purpose	Call the key comparison function for the binary tree pointed
//		to by the variable arg_tree.
//
// arguments	1 (avl_link *) key link struct pointer 1
//		2 (avl_link *) key link struct pointer 2
//
// returns	(int) value returned by the comparison function
//-----------------------------------------------------------------------------
#define avl_compare_keys	(*(arg_tree->compare_f))

//-----------------------------------------------------------------------------
// macro	avl_link_to_node
//
// purpose	Converts a pointer to a link into a pointer to a node.
//
// description	Tree link pointers always point to the sub-struct that
//		contains the links.  The AVL API uses pointers that point
//		to the nodes the calling program works with, which can
//		contain many link fields for putting the node in many trees
//		at the same time.  When a tree is initialized, the type of
//		node and name of link field are used to determine the offset
//		between the node and the link field used for that tree.
//
// arguments	1 (AVL *) pointer to binary tree
//		2 (avl_link *) link pointer to be converted
//
// returns	(void *) pointer to node
//-----------------------------------------------------------------------------
#define avl_link_to_node(t,p) ((void*)(((char*)(p))-((t)->offset)))

//-----------------------------------------------------------------------------
// macro	avl_node_to_link
//
// purpose	Converts a pointer to a node into a pointer to a link.
//
// arguments	1 (AVL *) pointer to binary tree
//		2 (void *) node pointer to be converted
//
// returns	(avl_link *) pointer to link
//-----------------------------------------------------------------------------
#define avl_node_to_link(t,p) ((avl_link*)((void*)(((char*)(p))+((t)->offset))))

//-----------------------------------------------------------------------------
// count macros
//-----------------------------------------------------------------------------
#if AVL_COUNT_ENABLE

#define avl_count_dec_nodes(r)		(--((r)->count_nodes))
#define avl_count_inc_nodes(r)		(++((r)->count_nodes))
#define avl_count_get_nodes(r)		((r)->count_nodes)
#define avl_count_set_nodes(r,n)	(((r)->count_nodes)=(n))

#else /* AVL_COUNT_ENABLE */

#define avl_count_dec_nodes(r)
#define avl_count_inc_nodes(r)
#define avl_count_get_nodes(r)		0
#define avl_count_set_nodes(r,n)

#endif /* AVL_COUNT_ENABLE */

//-----------------------------------------------------------------------------
// functions
//-----------------------------------------------------------------------------
#endif /* __AVL_LIB_H__ */

