//-----------------------------------------------------------------------------
// 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 112 columns wide.
//-----------------------------------------------------------------------------

#include "map_ulla.h"

__FMACRO_BEGIN__
//-----------------------------------------------------------------------------
// macro	map_ulls_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 (const unsigned long long *) key array pointer
//
// returns	(int) -2 = error, no map, bad type
//		(int) -1 = no member found
//		(int)  0 = found a member with no data
//		(int) data type of found member
//-----------------------------------------------------------------------------
#define map_ulls_find_exact(m,k) map_ulla_find((m),(k),~(size_t)0,0)

//-----------------------------------------------------------------------------
// macro	map_ulla_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 (const unsigned long long *) key array pointer
//		3 (size_t) key length
//
// returns	(int) -2 = error, no map, bad type
//		(int) -1 = no member found
//		(int)  0 = found a member with no data
//		(int) data type of found member
//-----------------------------------------------------------------------------
#define map_ulla_find_exact(m,k,l) map_ulla_find((m),(k),(l),0)
__FMACRO_END__

__FMACRO_BEGIN__
//-----------------------------------------------------------------------------
// macro	map_ulls_find_exact_lo
//
// purpose	Find a member with an exactly matching key of the specific
//		array key type for this mapping.  If there is more than
//		one member of the same key, select the one in the lowest
//		position.
//
// arguments	1 (MAP) mapping handle
//		2 (const unsigned long long *) key array pointer
//
// returns	(int) -2 = error, no map, bad type
//		(int) -1 = no member found
//		(int)  0 = found a member with no data
//		(int) data type of found member
//-----------------------------------------------------------------------------
#define map_ulls_find_exact_lo(m,k) map_ulla_find((m),(k),~(size_t)0,-1)

//-----------------------------------------------------------------------------
// function	map_ulla_find_exact_lo
//
// purpose	Find a member with an exactly matching key of the specific
//		array key type for this mapping.  If there is more than
//		one member of the same key, select the one in the lowest
//		position.
//
// arguments	1 (MAP) mapping handle
//		2 (const unsigned long long *) key array pointer
//		3 (size_t) key length
//
// returns	(int) -2 = error, no map, bad type
//		(int) -1 = no member found
//		(int)  0 = found a member with no data
//		(int) data type of found member
//-----------------------------------------------------------------------------
#define map_ulla_find_exact_lo(m,k,l) map_ulla_find((m),(k),(l),-1)
__FMACRO_END__

__FMACRO_BEGIN__
//-----------------------------------------------------------------------------
// macro	map_ulls_find_lo_or_near
//
// purpose	Find a member with an exactly matching key of the specific
//		array key type for this mapping, or if there is no exactly
//		matching key, select the nearest lower key.  If there is
//		more than one exactly matching key, select the one in the
//		lowest position.
//
// arguments	1 (MAP) mapping handle
//		2 (const unsigned long long *) key array pointer
//
// returns	(int) -2 = error, no map, bad type
//		(int) -1 = no member found
//		(int)  0 = found a member with no data
//		(int) data type of found member
//-----------------------------------------------------------------------------
#define map_ulls_find_lo_or_near(m,k) map_ulla_find((m),(k),~(size_t)0,-2)

//-----------------------------------------------------------------------------
// macro	map_ulla_find_lo_or_near
//
// purpose	Find a member with an exactly matching key of the specific
//		array key type for this mapping, or if there is no exactly
//		matching key, select the nearest lower key.  If there is
//		more than one exactly matching key, select the one in the
//		lowest position.
//
// arguments	1 (MAP) mapping handle
//		2 (const unsigned long long *) key array pointer
//		3 (size_t) key length
//
// returns	(int) -2 = error, no map, bad type
//		(int) -1 = no member found
//		(int)  0 = found a member with no data
//		(int) data type of found member
//-----------------------------------------------------------------------------
#define map_ulla_find_lo_or_near(m,k,l) map_ulla_find((m),(k),(l),-2)
__FMACRO_END__

__FMACRO_BEGIN__
//-----------------------------------------------------------------------------
// macro	map_ulls_find_exact_hi
//
// purpose	Find a member with an exactly matching key of the specific
//		array key type for this mapping.  If there is more than
//		one member of the same key, select the one in the highest
//		position.
//
// arguments	1 (MAP) mapping handle
//		2 (const unsigned long long *) key array pointer
//
// returns	(int) -2 = error, no map, bad type
//		(int) -1 = no member found
//		(int)  0 = found a member with no data
//		(int) data type of found member
//-----------------------------------------------------------------------------
#define map_ulls_find_exact_hi(m,k) map_ulla_find((m),(k),~(size_t)0,1)

//-----------------------------------------------------------------------------
// macro	map_ulla_find_exact_hi
//
// purpose	Find a member with an exactly matching key of the specific
//		array key type for this mapping.  If there is more than
//		one member of the same key, select the one in the highest
//		position.
//
// arguments	1 (MAP) mapping handle
//		2 (const unsigned long long *) key array pointer
//		3 (size_t) key length
//
// returns	(int) -2 = error, no map, bad type
//		(int) -1 = no member found
//		(int)  0 = found a member with no data
//		(int) data type of found member
//-----------------------------------------------------------------------------
#define map_ulla_find_exact_hi(m,k,l) map_ulla_find((m),(k),(l),1)
__FMACRO_END__

__FMACRO_BEGIN__
//-----------------------------------------------------------------------------
// macro	map_ulls_find_hi_or_near
//
// purpose	Find a member with an exactly matching key of the specific
//		array key type for this mapping, or if there is no exactly
//		matching key, select the nearest higher key.  If there is
//		more than one exactly matching key, select the one in the
//		highest position.
//
// arguments	1 (MAP) mapping handle
//		2 (const unsigned long long *) key array pointer
//
// returns	(int) -2 = error, no map, bad type
//		(int) -1 = no member found
//		(int)  0 = found a member with no data
//		(int) data type of found member
//-----------------------------------------------------------------------------
#define map_ulls_find_hi_or_near(m,k) map_ulla_find((m),(k),~(size_t)0,2)

//-----------------------------------------------------------------------------
// macro	map_ulla_find_hi_or_near
//
// purpose	Find a member with an exactly matching key of the specific
//		array key type for this mapping, or if there is no exactly
//		matching key, select the nearest higher key.  If there is
//		more than one exactly matching key, select the one in the
//		highest position.
//
// arguments	1 (MAP) mapping handle
//		2 (const unsigned long long *) key array pointer
//		3 (size_t) key length
//
// returns	(int) -2 = error, no map, bad type
//		(int) -1 = no member found
//		(int)  0 = found a member with no data
//		(int) data type of found member
//-----------------------------------------------------------------------------
#define map_ulla_find_hi_or_near(m,k,l) map_ulla_find((m),(k),(l),2)
__FMACRO_END__

#include <alloca.h>

__FMACRO_BEGIN__
//-----------------------------------------------------------------------------
// macro	map_ulls_find
//
// purpose	Find a member 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 member 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 member in the
//		high direction.
//
// arguments	1 (MAP) mapping handle
//		2 (const unsigned long long *) array key pointer
//		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 member found
//		(int)  0 = found a member with no data
//		(int) data type of found member
//-----------------------------------------------------------------------------
#define map_ulls_find(m,k,d) map_ulla_find((m),(k),~(size_t)0,(d))
__FMACRO_END__

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	map_ulla_find
//
// purpose	Find a member 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 member 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 member in the
//		high direction.
//
// arguments	1 (MAP) mapping handle
//		2 (const unsigned long long *) array key pointer
//		3 (size_t) array key length
//		4 (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 member found
//		(int)  0 = found a member with no data
//		(int) data type of found member
//-----------------------------------------------------------------------------
int
map_ulla_find (
    MAP			arg_map
    ,
    const unsigned long long *	arg_key_ptr
    ,
    size_t		arg_key_len
    ,
    int			arg_dir_near
   )
__PROTO_END__
{
    struct map_node_ulla *	node_p		;
    struct map_link_ulla *	finder_p	;
    size_t		len		;

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

    //-- If the length is not given, determine it as zero terminated string.
    if ( arg_key_len == ~ (size_t) 0U ) arg_key_len = map_ulla_length( arg_key_ptr );

    //-- Set up a search link to pass the key.
    len = sizeof (struct map_link_ulla) + arg_key_len * sizeof (unsigned long long);
    if ( ! ( finder_p = alloca( len ) ) ) return -1;
    finder_p->keylen = arg_key_len;
    map_ulla_copy_array( finder_p->key, arg_key_ptr, arg_key_len );

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

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


