//-----------------------------------------------------------------------------
// 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/map
// 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.
//-----------------------------------------------------------------------------

#include "map_ui.h"

//-----------------------------------------------------------------------------
// function	map_ui_compare_alt_opt (internal)
//
// purpose	Compare the scalar keys in two AVL tree nodes.  This function
//		is not globally exported.  It is used only to establish the
//		comparison method for the AVL tree used to hold this mapping.
//
// arguments	1 (struct map_link_ui *)
//		2 (struct map_link_ui *)
//		3 (void *) alternate compare function
//
// returns	(int) comparison status, -1, 0, 1
//-----------------------------------------------------------------------------
static
int
map_ui_compare_alt_opt (
    avl_link *		arg_one
    ,
    avl_link *		arg_two
    ,
    AVL *		arg_avl
    )
{
    return ( * map_cmp_func( arg_avl ) )
	    ( ((struct map_link_ui *) (void *) arg_one)->key,
	      ((struct map_link_ui *) (void *) arg_two)->key,
		map_from_avl( arg_avl )->options );
}

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function     map_ui_cmp_func_opt
//
// purpose      Set the comparison function to the caller supplied function
//		with a 3rd argument option included.
//
// arguments	1 (MAP) mapping to set function for
//		2 ((*)(unsigned int,unsigned int,int) function ptr
//		3 (int) option to pass along
//
// comparison	1 (unsigned int) key value 1
//  arguments	2 (unsigned int) key value 2
//		3 (int) option being passed along
//
// comparison	(int) -1: key 1 <  key 2
//    returns	(int)  0: key 1 == key 2
//		(int)  1: key 1  > key 2
//
// returns      (int) -1 : error
//              (int)  0 : OK
//-----------------------------------------------------------------------------
int
map_ui_cmp_func_opt (
    MAP         arg_map
    ,
    int		(* arg_compare)( unsigned int, unsigned int, int )
    ,
    int		arg_opt
    )
__PROTO_END__
{
    //-- Make sure the mapping is empty.
    map_empty( arg_map );

    //-- Set up for custom comparison.
    arg_map->options = arg_opt;
    arg_map->cmp_func = (int (*)()) arg_compare;

    //-- Re-initialize the AVL layer.
    if ( ! avl_init( & (arg_map->avl_tree), struct map_node_ui, link, map_ui_compare_alt_opt ) ) return -1;
    return 0;
}


