-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathbinsearch.c
108 lines (92 loc) · 3.09 KB
/
binsearch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*BHEADER**********************************************************************
* Copyright (c) 2017, Lawrence Livermore National Security, LLC.
* Produced at the Lawrence Livermore National Laboratory.
* Written by Ulrike Yang ([email protected]) et al. CODE-LLNL-738-322.
* This file is part of AMG. See files README and COPYRIGHT for details.
*
* AMG 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.
*
* This software is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF MERCHANTIBILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the terms and conditions of the
* GNU General Public License for more details.
*
***********************************************************************EHEADER*/
#include "_hypre_utilities.h"
/*--------------------------------------------------------------------------
* hypre_BinarySearch
* to contain ordered nonnegative numbers
* the routine returns the location of the value or -1
*--------------------------------------------------------------------------*/
HYPRE_Int hypre_BinarySearch(HYPRE_Int *list, HYPRE_Int value, HYPRE_Int list_length)
{
HYPRE_Int low, high, m;
HYPRE_Int not_found = 1;
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;
}
/*--------------------------------------------------------------------------
* 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
*--------------------------------------------------------------------------*/
HYPRE_Int hypre_BinarySearch2(HYPRE_Int *list, HYPRE_Int value, HYPRE_Int low, HYPRE_Int high, HYPRE_Int *spot)
{
HYPRE_Int m;
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;
}
}
/* not found (high = low-1) - so insert at low */
*spot = low;
return -1;
}
/*--------------------------------------------------------------------------
* Equivalent to C++ std::lower_bound
*--------------------------------------------------------------------------*/
HYPRE_Int *hypre_LowerBound( HYPRE_Int *first, HYPRE_Int *last, HYPRE_Int value )
{
HYPRE_Int *it;
size_t count = last - first, step;
while (count > 0) {
it = first; step = count/2; it += step;
if (*it < value) {
first = ++it;
count -= step + 1;
}
else count = step;
}
return first;
}