//-----------------------------------------------------------------------------
// 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_sl.h"

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	map_sl_find
//
// purpose	Find a node with a matching key, or possibly a nearby key.
//
//		If the influential direction is none, return the first exact
//		match encountered, or return none.
//
//		If the influential direction is low, return the lowest exact
//		match encountered, or if there is no exact match: if the
//		near option is specified, choose the nearest node in the
//		low direction.
//
//		If the influential direction is high, return the lowest exact
//		match encountered, or if there is no exact match: if the
//		near option is specified, choose the nearest node in the
//		high direction.
//
// arguments	1 (MAP) mapping handle
//		2 (signed long) scalar key
//		3 (int) direction and near option, -2, -1, 0, 1, 2
//			-2 = choose lowest exact, or nearest lower
//			-1 = choose lowest exact only
//			 0 = choose first exact only
//			 1 = choose highest exact only
//			 2 = choose highest exact, or nearest higher
//
// returns	(int) -2 = error, no map, bad type
//		(int) -1 = no node found
//		(int)  0 = found a node with no data
//		(int) data type of found node
//-----------------------------------------------------------------------------
int
map_sl_find (
    MAP			arg_map
    ,
    signed long		arg_key
    ,
    int			arg_dir_near
    )
__PROTO_END__
{
    struct map_node_sl *	node_p	;
    struct map_link_sl	finder	;

    //-- Make sure we have a correct mapping.
    if ( ! arg_map ) return -2;
    if ( arg_map->key_type != MAP_DATA_SL ) return -2;

    //-- Set up a search link to pass the key.
    finder.key = arg_key;

    //-- Let the AVL layer do the search.
    node_p = avl_find( & (arg_map->avl_tree), & finder, arg_dir_near );

    //-- Return the found data type or -1 if none found.
    return ( node_p ) ? node_p->data.type : -1;
}

__FMACRO_BEGIN__
//-----------------------------------------------------------------------------
// macro	map_sl_find_exact
//
// purpose	Find a member with an exactly matching key of the specific
//		array key type for this mapping.  If multiple members match,
//		this function does not define what particular member will be
//		selected.
//
// arguments	1 (MAP) mapping handle
//		2 (signed long) scalar key type
//
// returns	(int) -2 = error, no map, bad type
//		(int) -1 = no node found
//		(int)  0 = found a node with no data
//		(int) data type of found node
//-----------------------------------------------------------------------------
#define map_sl_find_exact(m,k) map_sl_find((m),(k),0)

//-----------------------------------------------------------------------------
// macro	map_sl_find_exact_lo
//
// purpose	Find a node with an exactly matching key of the specific
//		scalar key type for this mapping.  If there is more than
//		one node of the same key, select the one in the lowest
//		position.
//
// arguments	1 (MAP) mapping handle
//		2 (signed long) scalar key type
//
// returns	(int) -2 = error, no map, bad type
//		(int) -1 = no node found
//		(int)  0 = found a node with no data
//		(int) data type of found node
//-----------------------------------------------------------------------------
#define map_sl_find_exact_lo(m,k) map_sl_find((m),(k),-1)

//-----------------------------------------------------------------------------
// macro	map_sl_find_lo_or_near
//
// purpose	Find a node with an exactly matching key, or if no exactly
//		matching key present, a node with nearest lower value, of
//		the specific scalar key type for this mapping.  If there is
//		more than one node of the same key, select the one in the
//		lowest position.
//
// arguments	1 (MAP) mapping handle
//		2 (signed long) scalar key type
//
// returns	(int) -2 = error, no map, bad type
//		(int) -1 = no node found
//		(int)  0 = found a node with no data
//		(int) data type of found node
//-----------------------------------------------------------------------------
#define map_sl_find_lo_or_near(m,k) map_sl_find((m),(k),-2)

//-----------------------------------------------------------------------------
// macro	map_sl_find_exact_hi
//
// purpose	Find a node with an exactly matching key of the specific
//		scalar key type for this mapping.  If there is more than
//		one node of the same key, select the one in the highest
//		position.
//
// arguments	1 (MAP) mapping handle
//		2 (signed long) scalar key type
//
// returns	(int) -2 = error, no map, bad type
//		(int) -1 = no node found
//		(int)  0 = found a node with no data
//		(int) data type of found node
//-----------------------------------------------------------------------------
#define map_sl_find_exact_hi(m,k) map_sl_find((m),(k),1)

//-----------------------------------------------------------------------------
// macro	map_sl_find_hi_or_near
//
// purpose	Find a node with an exactly matching key, or if no exactly
//		matching key present, a node with nearest higher value, of
//		the specific scalar key type for this mapping.  If there is
//		more than one node of the same key, select the one in the
//		highest position.
//
// arguments	1 (MAP) mapping handle
//		2 (signed long) scalar key type
//
// returns	(int) -2 = error, no map, bad type
//		(int) -1 = no node found
//		(int)  0 = found a node with no data
//		(int) data type of found node
//-----------------------------------------------------------------------------
#define map_sl_find_hi_or_near(m,k) map_sl_find((m),(k),2)
__FMACRO_END__


