[ox/std] Make HashMap use StringView for string lookups

This commit is contained in:
Gary Talent 2023-11-28 21:57:47 -06:00
parent 709dc36d0b
commit 7008ebfb40
4 changed files with 104 additions and 79 deletions

View File

@ -79,9 +79,6 @@ static_assert(isBasicString_v<ox::BasicString<0ul>>);
static_assert(isBasicString_v<ox::BasicString<8ul>>);
static_assert(isBasicString_v<ox::String>);
template<typename T>
constexpr bool isOxString_v = isBString_v<T> || isBasicString_v<T>;
template<typename T>
consteval bool isOxVector(const T*) noexcept {
return false;

View File

@ -23,35 +23,10 @@ concept CommonRefWith = ox::is_same_v<typename ox::remove_reference_t<const T&>,
template<typename T, typename U>
concept same_as = ox::is_same_v<T, T>;
template<std::size_t SmallStringSize>
class BasicString;
template<std::size_t sz>
class BString;
class StringView;
namespace detail {
constexpr auto isOxString(const auto*) noexcept {
return false;
}
template<std::size_t sz>
constexpr auto isOxString(const BasicString<sz>*) noexcept {
return true;
}
template<std::size_t sz>
constexpr auto isOxString(const BString<sz>*) noexcept {
return true;
}
constexpr auto isOxString(const StringView*) noexcept {
return true;
}
}
template<typename T>
concept OxString_c = isOxString_v<T>;
template<typename T>
concept OxString_c = detail::isOxString(static_cast<T*>(nullptr));
concept Integral_c = ox::is_integral_v<T>;
}

View File

@ -9,6 +9,7 @@
#pragma once
#include "algorithm.hpp"
#include "stringview.hpp"
#include "strops.hpp"
#include "vector.hpp"
@ -21,6 +22,21 @@ class HashMap {
using value_t = T;
private:
// Maybe StringView. If K (or KK for MaybeSV) is a string type,
// MaybeSV::type/MaybeSV_t is a StringView.
// This avoids creating unnecessary Strings when taking a StringView or C
// string in lookups.
template<typename KK, bool isStr = isOxString_v<KK>>
struct MaybeSV {
using type = KK;
};
template<typename KK>
struct MaybeSV<KK, true> {
using type = ox::StringView;
};
template<typename KK>
using MaybeSV_t = MaybeSV<KK>::type;
struct Pair {
K key = {};
T value{};
@ -31,62 +47,55 @@ class HashMap {
public:
explicit constexpr HashMap(std::size_t size = 100);
constexpr HashMap(const HashMap &other);
constexpr HashMap(HashMap const&other);
constexpr HashMap(HashMap &&other) noexcept;
constexpr ~HashMap();
constexpr bool operator==(const HashMap &other) const;
constexpr bool operator==(HashMap const&other) const;
constexpr HashMap &operator=(const HashMap &other);
constexpr HashMap &operator=(HashMap const&other);
constexpr HashMap &operator=(HashMap &&other) noexcept;
/**
* K is assumed to be a null terminated string.
*/
constexpr T &operator[](const K &key);
constexpr T &operator[](MaybeSV_t<K> const&key);
/**
* K is assumed to be a null terminated string.
*/
constexpr Result<T*> at(const K &key) noexcept;
constexpr Result<T*> at(MaybeSV_t<K> const&key) noexcept;
/**
* K is assumed to be a null terminated string.
*/
constexpr Result<const T*> at(const K &key) const noexcept;
constexpr Result<const T*> at(MaybeSV_t<K> const&key) const noexcept;
constexpr void erase(const K &key);
constexpr void erase(MaybeSV_t<K> const&key);
constexpr bool contains(const K &key) const noexcept;
[[nodiscard]]
constexpr bool contains(MaybeSV_t<K> const&key) const noexcept;
[[nodiscard]]
constexpr std::size_t size() const noexcept;
[[nodiscard]]
constexpr const Vector<K> &keys() const noexcept;
constexpr Vector<K> const&keys() const noexcept;
constexpr void clear();
private:
constexpr void expand();
/**
* K is assumed to be a null terminated string.
*/
constexpr static uint64_t hash(auto) noexcept;
constexpr static uint64_t hash(Integral_c auto) noexcept;
constexpr static uint64_t hash(StringView const&) noexcept;
/**
* K is assumed to be a null terminated string.
*/
constexpr Pair *const&access(const Vector<Pair*> &pairs, const K &key) const;
template<typename KK>
constexpr Pair *const&access(Vector<Pair*> const&pairs, KK const&key) const;
/**
* K is assumed to be a null terminated string.
*/
constexpr Pair *&access(Vector<Pair*> &pairs, const K &key);
template<typename KK>
constexpr Pair *&access(Vector<Pair*> &pairs, KK const&key);
};
@ -95,7 +104,7 @@ constexpr HashMap<K, T>::HashMap(std::size_t size): m_pairs(size) {
}
template<typename K, typename T>
constexpr HashMap<K, T>::HashMap(const HashMap<K, T> &other) {
constexpr HashMap<K, T>::HashMap(HashMap<K, T> const&other) {
m_pairs = other.m_pairs;
}
@ -111,7 +120,7 @@ constexpr HashMap<K, T>::~HashMap() {
}
template<typename K, typename T>
constexpr bool HashMap<K, T>::operator==(const HashMap &other) const {
constexpr bool HashMap<K, T>::operator==(HashMap const&other) const {
if (m_keys != other.m_keys) {
return false;
}
@ -125,7 +134,7 @@ constexpr bool HashMap<K, T>::operator==(const HashMap &other) const {
}
template<typename K, typename T>
constexpr HashMap<K, T> &HashMap<K, T>::operator=(const HashMap<K, T> &other) {
constexpr HashMap<K, T> &HashMap<K, T>::operator=(HashMap<K, T> const&other) {
if (this != &other) {
clear();
m_keys = other.m_keys;
@ -145,7 +154,7 @@ constexpr HashMap<K, T> &HashMap<K, T>::operator=(HashMap<K, T> &&other) noexcep
}
template<typename K, typename T>
constexpr T &HashMap<K, T>::operator[](const K &k) {
constexpr T &HashMap<K, T>::operator[](MaybeSV_t<K> const&k) {
auto &p = access(m_pairs, k);
if (p == nullptr) {
if (static_cast<double>(m_pairs.size()) * 0.7 <
@ -154,13 +163,13 @@ constexpr T &HashMap<K, T>::operator[](const K &k) {
}
p = new Pair;
p->key = k;
m_keys.push_back(k);
m_keys.emplace_back(k);
}
return p->value;
}
template<typename K, typename T>
constexpr Result<T*> HashMap<K, T>::at(const K &k) noexcept {
constexpr Result<T*> HashMap<K, T>::at(MaybeSV_t<K> const&k) noexcept {
auto p = access(m_pairs, k);
if (!p) {
return {nullptr, OxError(1, "value not found for given key")};
@ -169,7 +178,7 @@ constexpr Result<T*> HashMap<K, T>::at(const K &k) noexcept {
}
template<typename K, typename T>
constexpr Result<const T*> HashMap<K, T>::at(const K &k) const noexcept {
constexpr Result<const T*> HashMap<K, T>::at(MaybeSV_t<K> const&k) const noexcept {
auto p = access(m_pairs, k);
if (!p) {
return {nullptr, OxError(1, "value not found for given key")};
@ -178,7 +187,7 @@ constexpr Result<const T*> HashMap<K, T>::at(const K &k) const noexcept {
}
template<typename K, typename T>
constexpr void HashMap<K, T>::erase(const K &k) {
constexpr void HashMap<K, T>::erase(MaybeSV_t<K> const&k) {
if (!contains(k)) {
return;
}
@ -196,7 +205,7 @@ constexpr void HashMap<K, T>::erase(const K &k) {
}
template<typename K, typename T>
constexpr bool HashMap<K, T>::contains(const K &k) const noexcept {
constexpr bool HashMap<K, T>::contains(MaybeSV_t<K> const&k) const noexcept {
return access(m_pairs, k) != nullptr;
}
@ -206,7 +215,7 @@ constexpr std::size_t HashMap<K, T>::size() const noexcept {
}
template<typename K, typename T>
constexpr const Vector<K> &HashMap<K, T>::keys() const noexcept {
constexpr Vector<K> const&HashMap<K, T>::keys() const noexcept {
return m_keys;
}
@ -230,24 +239,28 @@ constexpr void HashMap<K, T>::expand() {
}
template<typename K, typename T>
constexpr uint64_t HashMap<K, T>::hash(auto k) noexcept {
constexpr uint64_t HashMap<K, T>::hash(Integral_c auto k) noexcept {
uint64_t sum = 1;
if constexpr(is_integral_v<decltype(k)>) {
for (auto i = 0u; i < sizeof(K); ++i) {
const auto shift = i * 8;
const auto v = static_cast<uint64_t>(k >> shift & 0xff);
sum += (sum + v) * 7 * sum;
}
} else {
for (auto i = 0u; k[i]; ++i) {
sum += ((sum + static_cast<uint64_t>(k[i])) * 7) * sum;
}
for (auto i = 0u; i < sizeof(K); ++i) {
const auto shift = i * 8;
const auto v = static_cast<uint64_t>(k >> shift & 0xff);
sum += (sum + v) * 7 * sum;
}
return sum;
}
template<typename K, typename T>
constexpr typename HashMap<K, T>::Pair *const&HashMap<K, T>::access(const Vector<Pair*> &pairs, const K &k) const {
constexpr uint64_t HashMap<K, T>::hash(StringView const&k) noexcept {
uint64_t sum = 1;
for (auto i = 0u; i < k.len(); ++i) {
sum += ((sum + static_cast<uint64_t>(k[i])) * 7) * sum;
}
return sum;
}
template<typename K, typename T>
template<typename KK>
constexpr typename HashMap<K, T>::Pair *const&HashMap<K, T>::access(Vector<Pair*> const&pairs, KK const&k) const {
auto h = static_cast<std::size_t>(hash(k) % pairs.size());
while (true) {
const auto &p = pairs[h];
@ -260,12 +273,13 @@ constexpr typename HashMap<K, T>::Pair *const&HashMap<K, T>::access(const Vector
}
template<typename K, typename T>
constexpr typename HashMap<K, T>::Pair *&HashMap<K, T>::access(Vector<Pair*> &pairs, const K &k) {
template<typename KK>
constexpr typename HashMap<K, T>::Pair *&HashMap<K, T>::access(Vector<Pair*> &pairs, KK const&k) {
auto h = static_cast<std::size_t>(hash(k) % pairs.size());
while (true) {
auto &p = pairs[h];
bool matches = [&] {
if constexpr (is_integral_v<K>) {
if constexpr (is_integral_v<KK>) {
return p == nullptr || k == p->key;
} else {
return p == nullptr || ox_strcmp(p->key, k) == 0;

View File

@ -266,4 +266,43 @@ constexpr bool is_move_constructible(bool) {
template<class T>
constexpr bool is_move_constructible_v = detail::is_move_constructible<T>(0);
// is String?
template<std::size_t SmallStringSize>
class BasicString;
template<std::size_t sz>
class BString;
class StringLiteral;
class StringView;
namespace detail {
constexpr auto isOxString(const auto*) noexcept {
return false;
}
template<std::size_t sz>
constexpr auto isOxString(const BasicString<sz>*) noexcept {
return true;
}
template<std::size_t sz>
constexpr auto isOxString(const BString<sz>*) noexcept {
return true;
}
constexpr auto isOxString(const StringLiteral*) noexcept {
return true;
}
constexpr auto isOxString(const StringView*) noexcept {
return true;
}
}
template<typename T>
constexpr auto isOxString_v = detail::isOxString(static_cast<T*>(nullptr));
}