-
Notifications
You must be signed in to change notification settings - Fork 0
/
rangesearch.cpp
45 lines (42 loc) · 1.33 KB
/
rangesearch.cpp
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
#include <vector>
/**
* rangeSearch searches the range with the low and high and returns all the elements
* within the range
* @param A: the vector with the values
* @param low: the lower bound of the range
* @param high: the upper bound of the range
* @param inclusivity: a number to change the ranges
* -1 - (low, high]
* 0 - [low, high]
* 1 - (low, high)
* 2 - [low, high)
*/
std::vector<int> &rangeSearch(std::vector<int> &A, int low, int high, int inclusivity) {
std::vector<int> rangeElements{};
if (inclusivity == -1) {
for (auto &elem: A) {
if (elem > low && elem <= high) {
rangeElements.emplace_back(elem);
}
}
} else if (inclusivity == 0) {
for (auto &elem: A) {
if (elem >= low && elem <= high) {
rangeElements.emplace_back(elem);
}
}
} else if (inclusivity == 1) {
for (auto &elem: A) {
if (elem > low && elem < high) {
rangeElements.emplace_back(elem);
}
}
} else if (inclusivity == 2) {
for (auto &elem: A) {
if (elem >= low && elem <= high) {
rangeElements.emplace_back(elem);
}
}
}
return rangeElements;
}