Skip to content

Instantly share code, notes, and snippets.

@LapysDev
Last active April 19, 2023 06:04
Show Gist options
  • Save LapysDev/2622053a7fea40e0ea384355d2f931d8 to your computer and use it in GitHub Desktop.
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
#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