Lift
Library of parallel computing primitives for GPUs and multi-core CPUs
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
binsearch.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015, NVIDIA CORPORATION
3  * Copyright (c) 2015, Nuno Subtil <subtil@gmail.com>
4  * Copyright (c) 2015, Roche Molecular Systems Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * * Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #pragma once
31 
32 #include "../types.h"
33 
34 namespace lift {
35 
36 // perform a binary search on a sorted array
37 // returns the index of any element in the array that is equal to val, or -1 if not found
38 template <typename T>
39 CUDA_HOST_DEVICE uint32 binary_search(const T *data, uint32 size, const T val)
40 {
41  uint32 first = 0;
42  uint32 last = size;
43 
44  while(last - first > 0)
45  {
46  uint32 i = first + ((last - first) / 2);
47 
48  if (data[i] == val)
49  return i;
50 
51  if (data[i] < val)
52  {
53  if (i == first)
54  {
55  first++;
56  } else {
57  first = i;
58  }
59  } else {
60  if (i == last)
61  {
62  last--;
63  } else {
64  last = i;
65  }
66  }
67  }
68 
69  return uint32(-1);
70 }
71 
72 // perform a binary search on a sorted array
73 // return the index of the first element that is >= val
74 template <typename T>
75 CUDA_HOST_DEVICE uint32 lower_bound(const T *data, uint32 size, const T val)
76 {
77  uint32 first = 0;
78  uint32 last = size;
79 
80  while(last - first > 0)
81  {
82  uint32 i = first + ((last - first) / 2);
83 
84  if (data[i] < val)
85  {
86  if (i == first)
87  {
88  first++;
89  } else {
90  first = i;
91  }
92  } else {
93  if (i == last)
94  {
95  last--;
96  } else {
97  last = i;
98  }
99  }
100  }
101 
102  return last;
103 }
104 
105 } // namespace lift
uint32_t uint32
Definition: types.h:43
CUDA_HOST_DEVICE uint32 binary_search(const T *data, uint32 size, const T val)
Definition: binsearch.h:39
CUDA_HOST_DEVICE uint32 lower_bound(const T *data, uint32 size, const T val)
Definition: binsearch.h:75
#define CUDA_HOST_DEVICE
Definition: local_memory.h:51