-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Thomas Applencourt
committed
Jul 26, 2023
1 parent
b11c00d
commit 70905f4
Showing
4 changed files
with
22 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,37 @@ | ||
#pragma once | ||
|
||
template<class ForwardIterator, class ForwardIteratorBool> | ||
void binary_searchs(ForwardIterator first, ForwardIterator last, ForwardIterator v_first, | ||
ForwardIterator v_last, ForwardIterator v, ForwardIteratorBool i) { | ||
// Return: `*v` ∈? [first, last) | ||
// Precondition: `v` ∈ `v_first`, `v_last`. | ||
void binary_searchs_imp(ForwardIterator first, ForwardIterator last, ForwardIterator v_first, | ||
ForwardIterator v_last, ForwardIterator v, ForwardIteratorBool i) { | ||
// Return: *v ∈? [first, last). | ||
// Precondition: v ∈ [v_first, v_last) | ||
|
||
// pos of `*v` inside `v` array using binary search | ||
// pos is pointer ∈ [first, last) pointing `*v`. | ||
auto pos = std::lower_bound(first, last, *v); | ||
// Find it or not find it? | ||
*i = (pos != last && !(*v < *pos)); | ||
// Find new values to search for | ||
// Lazy: only set i, if found | ||
if(pos != last && !(*v < *pos)) *i = true; | ||
|
||
// Find the new `v` to search for | ||
// v_first v v_last | ||
// |-----------|----------------| | ||
{ | ||
// Botom Half [v_first, v) | ||
// End the recursion when v_first == v, we search for all our values | ||
// if distance == 1, need to search for v_first | ||
auto d = std::distance(v_first, v); | ||
if(pos != first && d > 1) binary_searchs(first, pos, v_first, v - 1, v - d / 2, i - d / 2); | ||
if(pos != first && d > 0) | ||
binary_searchs_imp(first, pos, v_first, v - 1, v - (d + 1) / 2, i - (d + 1) / 2); | ||
} | ||
{ | ||
// Top Half (v, v_last) | ||
// We cannot deference v_last, so we stop the recursion when d == 1 | ||
auto d = std::distance(v, v_last); | ||
if(pos != last && d > 1) binary_searchs(pos, last, v + 1, v_last, v + d / 2, i + d / 2); | ||
if(pos != last && d > 1) binary_searchs_imp(pos, last, v + 1, v_last, v + d / 2, i + d / 2); | ||
} | ||
} | ||
|
||
template<class T1, class T2, class T3> | ||
void binary_searchs(T1& enumerable, T2& values, T3& result) { | ||
binary_searchs(enumerable.begin(), enumerable.end(), values.begin(), values.end(), | ||
values.begin() + values.size() / 2, result.begin() + values.size() / 2); | ||
template<class T1, class T2> | ||
void binary_searchs(T1& enumerable, T1& values, T2& result) { | ||
binary_searchs_imp(enumerable.begin(), enumerable.end(), values.begin(), values.end(), | ||
values.begin() + values.size() / 2, result.begin() + values.size() / 2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters