Last active
April 19, 2023 06:04
-
-
Save LapysDev/2622053a7fea40e0ea384355d2f931d8 to your computer and use it in GitHub Desktop.
π Gradient Search: Algorithm to search through an array from multiple evenly spread points (or gradient stops) simultaneously
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
#include <cstddef> | |
/* ... --> search<π>(β¦) */ | |
template <std::size_t count, typename type> | |
bool search(type const& key, type const array[], std::size_t length) noexcept { | |
union { | |
type const *stop; // ->> for elements missed via `0u != count % length` | |
type const *stops[count]; | |
}; | |
// ... | |
stop = array + (count * (length / count)); | |
for (type const *const end = array + length; stop != end; ++stop) | |
if (key == *stop) return true; | |
// ... | |
length /= count; | |
for (type const **stop = stops + count; stop != stops; array += length) | |
*--stop = array; | |
while (length--) { | |
for (type const **stop = stops + count; stop != stops; ) | |
if (key == *((*--stop)++)) return true; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment