| [2aa6644] | 1 | /*BHEADER**********************************************************************
|
|---|
| 2 | * Copyright (c) 2008, Lawrence Livermore National Security, LLC.
|
|---|
| 3 | * Produced at the Lawrence Livermore National Laboratory.
|
|---|
| 4 | * This file is part of HYPRE. See file COPYRIGHT for details.
|
|---|
| 5 | *
|
|---|
| 6 | * HYPRE is free software; you can redistribute it and/or modify it under the
|
|---|
| 7 | * terms of the GNU Lesser General Public License (as published by the Free
|
|---|
| 8 | * Software Foundation) version 2.1 dated February 1999.
|
|---|
| 9 | *
|
|---|
| 10 | * $Revision: 2.4 $
|
|---|
| 11 | ***********************************************************************EHEADER*/
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 | #include "utilities.h"
|
|---|
| 15 |
|
|---|
| 16 | /*--------------------------------------------------------------------------
|
|---|
| 17 | * hypre_BinarySearch
|
|---|
| 18 | * to contain ordered nonnegative numbers
|
|---|
| 19 | * the routine returns the location of the value or -1
|
|---|
| 20 | *--------------------------------------------------------------------------*/
|
|---|
| 21 |
|
|---|
| 22 | int hypre_BinarySearch(int *list, int value, int list_length)
|
|---|
| 23 | {
|
|---|
| 24 | int low, high, m;
|
|---|
| 25 | int not_found = 1;
|
|---|
| 26 |
|
|---|
| 27 | low = 0;
|
|---|
| 28 | high = list_length-1;
|
|---|
| 29 | while (not_found && low <= high)
|
|---|
| 30 | {
|
|---|
| 31 | m = (low + high) / 2;
|
|---|
| 32 | if (value < list[m])
|
|---|
| 33 | {
|
|---|
| 34 | high = m - 1;
|
|---|
| 35 | }
|
|---|
| 36 | else if (value > list[m])
|
|---|
| 37 | {
|
|---|
| 38 | low = m + 1;
|
|---|
| 39 | }
|
|---|
| 40 | else
|
|---|
| 41 | {
|
|---|
| 42 | not_found = 0;
|
|---|
| 43 | return m;
|
|---|
| 44 | }
|
|---|
| 45 | }
|
|---|
| 46 | return -1;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | /*--------------------------------------------------------------------------
|
|---|
| 50 | * hypre_BigBinarySearch
|
|---|
| 51 | * to contain ordered nonnegative numbers
|
|---|
| 52 | * the routine returns the location of the value or -1
|
|---|
| 53 | *--------------------------------------------------------------------------*/
|
|---|
| 54 |
|
|---|
| 55 | int hypre_BigBinarySearch(HYPRE_BigInt *list, HYPRE_BigInt value, int list_length)
|
|---|
| 56 | {
|
|---|
| 57 | int low, high, m;
|
|---|
| 58 | int not_found = 1;
|
|---|
| 59 |
|
|---|
| 60 | low = 0;
|
|---|
| 61 | high = list_length-1;
|
|---|
| 62 | while (not_found && low <= high)
|
|---|
| 63 | {
|
|---|
| 64 | m = (low + high) / 2;
|
|---|
| 65 | if (value < list[m])
|
|---|
| 66 | {
|
|---|
| 67 | high = m - 1;
|
|---|
| 68 | }
|
|---|
| 69 | else if (value > list[m])
|
|---|
| 70 | {
|
|---|
| 71 | low = m + 1;
|
|---|
| 72 | }
|
|---|
| 73 | else
|
|---|
| 74 | {
|
|---|
| 75 | not_found = 0;
|
|---|
| 76 | return m;
|
|---|
| 77 | }
|
|---|
| 78 | }
|
|---|
| 79 | return -1;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|