Skip to content

Commit

Permalink
slightly simplified the string_hash implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
eteran committed Mar 22, 2024
1 parent 0ae5d85 commit aff5f6f
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions include/string_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,19 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
namespace edb {
namespace detail {

template <std::size_t N, std::size_t n>
constexpr std::enable_if_t<N <= 9 && n == 0, uint64_t> string_hash(const char (&)[N]) {
return 0;
template <std::size_t N, std::size_t Index = N - 1>
constexpr uint64_t string_hash(const char (&array)[N]) {
if constexpr (Index == 0) {
return 0;
} else {
return string_hash<N, Index - 1>(array) | ((array[Index - 1] & 0xffull) << (8 * (Index - 1)));
}
}

template <std::size_t N, std::size_t n = N - 1>
constexpr std::enable_if_t<N <= 9 && n != 0, uint64_t> string_hash(const char (&array)[N]) {
return string_hash<N, n - 1>(array) | ((array[n - 1] & 0xffull) << (8 * (n - 1)));
}

}

template <std::size_t N>
constexpr std::enable_if_t<N <= 9, uint64_t> string_hash(const char (&array)[N]) {
template <std::size_t N, class = std::enable_if_t<N <= 9>>
constexpr uint64_t string_hash(const char (&array)[N]) {
return detail::string_hash(array);
}

Expand Down

0 comments on commit aff5f6f

Please sign in to comment.