1999-05-08 01:47:52 +08:00
|
|
|
/*BHEADER**********************************************************************
|
2008-07-18 09:34:48 +08:00
|
|
|
* Copyright (c) 2008, Lawrence Livermore National Security, LLC.
|
2006-07-28 07:26:57 +08:00
|
|
|
* Produced at the Lawrence Livermore National Laboratory.
|
2008-07-18 09:34:48 +08:00
|
|
|
* This file is part of HYPRE. See file COPYRIGHT for details.
|
1999-05-08 01:47:52 +08:00
|
|
|
*
|
2008-07-18 09:34:48 +08:00
|
|
|
* HYPRE is free software; you can redistribute it and/or modify it under the
|
|
|
|
|
* terms of the GNU Lesser General Public License (as published by the Free
|
|
|
|
|
* Software Foundation) version 2.1 dated February 1999.
|
1999-05-08 01:47:52 +08:00
|
|
|
*
|
|
|
|
|
* $Revision$
|
2006-07-28 07:26:57 +08:00
|
|
|
***********************************************************************EHEADER*/
|
|
|
|
|
|
2007-11-14 05:20:21 +08:00
|
|
|
|
1999-05-08 01:47:52 +08:00
|
|
|
|
2006-11-15 09:08:13 +08:00
|
|
|
#include "_hypre_utilities.h"
|
1999-05-08 01:47:52 +08:00
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------
|
|
|
|
|
* hypre_BinarySearch
|
|
|
|
|
* performs a binary search for value on array list where list needs
|
|
|
|
|
* to contain ordered nonnegative numbers
|
|
|
|
|
* the routine returns the location of the value or -1
|
|
|
|
|
*--------------------------------------------------------------------------*/
|
|
|
|
|
|
2010-12-21 03:27:44 +08:00
|
|
|
HYPRE_Int hypre_BinarySearch(HYPRE_Int *list, HYPRE_Int value, HYPRE_Int list_length)
|
1999-05-08 01:47:52 +08:00
|
|
|
{
|
2010-12-21 03:27:44 +08:00
|
|
|
HYPRE_Int low, high, m;
|
|
|
|
|
HYPRE_Int not_found = 1;
|
1999-05-08 01:47:52 +08:00
|
|
|
|
|
|
|
|
low = 0;
|
|
|
|
|
high = list_length-1;
|
|
|
|
|
while (not_found && low <= high)
|
|
|
|
|
{
|
|
|
|
|
m = (low + high) / 2;
|
|
|
|
|
if (value < list[m])
|
|
|
|
|
{
|
|
|
|
|
high = m - 1;
|
|
|
|
|
}
|
|
|
|
|
else if (value > list[m])
|
|
|
|
|
{
|
|
|
|
|
low = m + 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
not_found = 0;
|
|
|
|
|
return m;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-05 08:04:22 +08:00
|
|
|
/*--------------------------------------------------------------------------
|
|
|
|
|
* hypre_BinarySearch2
|
|
|
|
|
* this one is a bit more robust:
|
|
|
|
|
* avoids overflow of m as can happen above when (low+high) overflows
|
|
|
|
|
* lets user specifiy high and low bounds for array (so a subset
|
|
|
|
|
of array can be used)
|
|
|
|
|
* if not found, then spot returns where is should be inserted
|
|
|
|
|
|
|
|
|
|
*--------------------------------------------------------------------------*/
|
|
|
|
|
|
2010-12-21 03:27:44 +08:00
|
|
|
HYPRE_Int hypre_BinarySearch2(HYPRE_Int *list, HYPRE_Int value, HYPRE_Int low, HYPRE_Int high, HYPRE_Int *spot)
|
2007-01-05 08:04:22 +08:00
|
|
|
{
|
|
|
|
|
|
2010-12-21 03:27:44 +08:00
|
|
|
HYPRE_Int m;
|
2007-01-05 08:04:22 +08:00
|
|
|
|
|
|
|
|
while (low <= high)
|
|
|
|
|
{
|
|
|
|
|
m = low + (high - low)/2;
|
|
|
|
|
|
|
|
|
|
if (value < list[m])
|
|
|
|
|
high = m - 1;
|
|
|
|
|
else if (value > list[m])
|
|
|
|
|
low = m + 1;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
*spot = m;
|
|
|
|
|
return m;
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-01-20 07:21:44 +08:00
|
|
|
/* not found (high = low-1) - so insert at low */
|
2007-01-05 08:04:22 +08:00
|
|
|
*spot = low;
|
2007-01-20 07:21:44 +08:00
|
|
|
|
2007-01-05 08:04:22 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|