[ox/std] Make HashMap::at return a pointer instead of reference

This commit is contained in:
Gary Talent 2022-05-28 03:18:10 -05:00
parent dfd6670dfe
commit addc2233e7
2 changed files with 65 additions and 60 deletions

View File

@ -39,32 +39,32 @@ ClArgs::ClArgs(int argc, const char **args) noexcept {
bool ClArgs::getBool(const char *arg, bool defaultValue) const noexcept { bool ClArgs::getBool(const char *arg, bool defaultValue) const noexcept {
auto [value, err] = m_ints.at(arg); auto [value, err] = m_ints.at(arg);
return !err ? value : defaultValue; return !err ? *value : defaultValue;
} }
String ClArgs::getString(const char *arg, const char *defaultValue) const noexcept { String ClArgs::getString(const char *arg, const char *defaultValue) const noexcept {
auto [value, err] = m_strings.at(arg); auto [value, err] = m_strings.at(arg);
return !err ? value : defaultValue; return !err ? *value : defaultValue;
} }
int ClArgs::getInt(const char *arg, int defaultValue) const noexcept { int ClArgs::getInt(const char *arg, int defaultValue) const noexcept {
auto [value, err] = m_ints.at(arg); auto [value, err] = m_ints.at(arg);
return !err ? value : defaultValue; return !err ? *value : defaultValue;
} }
Result<bool> ClArgs::getBool(const char *arg) const noexcept { Result<bool> ClArgs::getBool(const char *arg) const noexcept {
oxRequire(out, m_bools.at(arg)); oxRequire(out, m_bools.at(arg));
return out; return *out;
} }
Result<String> ClArgs::getString(const char *argName) const noexcept { Result<String> ClArgs::getString(const char *argName) const noexcept {
oxRequire(out, m_strings.at(argName)); oxRequire(out, m_strings.at(argName));
return out; return *out;
} }
Result<int> ClArgs::getInt(const char *arg) const noexcept { Result<int> ClArgs::getInt(const char *arg) const noexcept {
oxRequire(out, m_ints.at(arg)); oxRequire(out, m_ints.at(arg));
return out; return *out;
} }
} }

View File

@ -29,87 +29,89 @@ class HashMap {
Vector<Pair*> m_pairs; Vector<Pair*> m_pairs;
public: public:
explicit HashMap(std::size_t size = 100); explicit constexpr HashMap(std::size_t size = 100);
HashMap(const HashMap &other); constexpr HashMap(const HashMap &other);
HashMap(HashMap &&other); constexpr HashMap(HashMap &&other) noexcept;
~HashMap(); constexpr ~HashMap();
bool operator==(const HashMap &other) const; constexpr bool operator==(const HashMap &other) const;
HashMap &operator=(const HashMap &other); constexpr HashMap &operator=(const HashMap &other);
HashMap &operator=(HashMap &&other); constexpr HashMap &operator=(HashMap &&other);
/** /**
* K is assumed to be a null terminated string. * K is assumed to be a null terminated string.
*/ */
T &operator[](K key); constexpr T &operator[](K key);
/** /**
* K is assumed to be a null terminated string. * K is assumed to be a null terminated string.
*/ */
Result<T&> at(K key) noexcept; constexpr Result<T*> at(K key) noexcept;
/** /**
* K is assumed to be a null terminated string. * K is assumed to be a null terminated string.
*/ */
Result<const T&> at(K key) const noexcept; constexpr Result<const T*> at(K key) const noexcept;
void erase(const K &key); constexpr void erase(const K &key);
bool contains(K key) const noexcept; constexpr bool contains(K key) const noexcept;
std::size_t size() const noexcept; [[nodiscard]]
constexpr std::size_t size() const noexcept;
const Vector<K> &keys() const noexcept; [[nodiscard]]
constexpr const Vector<K> &keys() const noexcept;
private: private:
void expand(); constexpr void expand();
/** /**
* K is assumed to be a null terminated string. * K is assumed to be a null terminated string.
*/ */
static uint64_t hash(K, int len = 0xFFFF) noexcept; constexpr static uint64_t hash(K, int len = 0xFFFF) noexcept;
/** /**
* K is assumed to be a null terminated string. * K is assumed to be a null terminated string.
*/ */
Pair *const&access(const Vector<Pair*> &pairs, K key) const; constexpr Pair *const&access(const Vector<Pair*> &pairs, K key) const;
/** /**
* K is assumed to be a null terminated string. * K is assumed to be a null terminated string.
*/ */
Pair *&access(Vector<Pair*> &pairs, K key); constexpr Pair *&access(Vector<Pair*> &pairs, K key);
void clear(); constexpr void clear();
}; };
template<typename K, typename T> template<typename K, typename T>
HashMap<K, T>::HashMap(std::size_t size): m_pairs(size) { constexpr HashMap<K, T>::HashMap(std::size_t size): m_pairs(size) {
} }
template<typename K, typename T> template<typename K, typename T>
HashMap<K, T>::HashMap(const HashMap<K, T> &other) { constexpr HashMap<K, T>::HashMap(const HashMap<K, T> &other) {
m_pairs = other.m_pairs; m_pairs = other.m_pairs;
} }
template<typename K, typename T> template<typename K, typename T>
HashMap<K, T>::HashMap(HashMap<K, T> &&other) { constexpr HashMap<K, T>::HashMap(HashMap<K, T> &&other) noexcept {
m_keys = std::move(other.m_keys); m_keys = std::move(other.m_keys);
m_pairs = std::move(other.m_pairs); m_pairs = std::move(other.m_pairs);
} }
template<typename K, typename T> template<typename K, typename T>
HashMap<K, T>::~HashMap() { constexpr HashMap<K, T>::~HashMap() {
clear(); clear();
} }
template<typename K, typename T> template<typename K, typename T>
bool HashMap<K, T>::operator==(const HashMap &other) const { constexpr bool HashMap<K, T>::operator==(const HashMap &other) const {
if (m_keys != other.m_keys) { if (m_keys != other.m_keys) {
return false; return false;
} }
@ -123,7 +125,7 @@ bool HashMap<K, T>::operator==(const HashMap &other) const {
} }
template<typename K, typename T> template<typename K, typename T>
HashMap<K, T> &HashMap<K, T>::operator=(const HashMap<K, T> &other) { constexpr HashMap<K, T> &HashMap<K, T>::operator=(const HashMap<K, T> &other) {
if (this != &other) { if (this != &other) {
clear(); clear();
m_keys = other.m_keys; m_keys = other.m_keys;
@ -133,7 +135,7 @@ HashMap<K, T> &HashMap<K, T>::operator=(const HashMap<K, T> &other) {
} }
template<typename K, typename T> template<typename K, typename T>
HashMap<K, T> &HashMap<K, T>::operator=(HashMap<K, T> &&other) { constexpr HashMap<K, T> &HashMap<K, T>::operator=(HashMap<K, T> &&other) {
if (this != &other) { if (this != &other) {
clear(); clear();
m_keys = std::move(other.m_keys); m_keys = std::move(other.m_keys);
@ -143,7 +145,7 @@ HashMap<K, T> &HashMap<K, T>::operator=(HashMap<K, T> &&other) {
} }
template<typename K, typename T> template<typename K, typename T>
T &HashMap<K, T>::operator[](K k) { constexpr T &HashMap<K, T>::operator[](K k) {
auto &p = access(m_pairs, k); auto &p = access(m_pairs, k);
if (p == nullptr) { if (p == nullptr) {
if (m_pairs.size() * 0.7 < m_keys.size()) { if (m_pairs.size() * 0.7 < m_keys.size()) {
@ -157,22 +159,32 @@ T &HashMap<K, T>::operator[](K k) {
} }
template<typename K, typename T> template<typename K, typename T>
Result<T&> HashMap<K, T>::at(K k) noexcept { constexpr Result<T*> HashMap<K, T>::at(K k) noexcept {
auto p = access(m_pairs, k); auto p = access(m_pairs, k);
if (!p) { if (!p) {
AllocAlias<T> v; return {nullptr, OxError(1)};
return {*reinterpret_cast<T*>(&v), OxError(1)};
} }
return p->value; return &p->value;
} }
template<typename K, typename T> template<typename K, typename T>
void HashMap<K, T>::erase(const K &k) { constexpr Result<const T*> HashMap<K, T>::at(K k) const noexcept {
auto p = access(m_pairs, k);
if (!p) {
return {nullptr, OxError(1)};
}
return &p->value;
}
template<typename K, typename T>
constexpr void HashMap<K, T>::erase(const K &k) {
if (!contains(k)) { if (!contains(k)) {
return; return;
} }
auto h = hash(k) % m_pairs.size(); auto h = hash(k) % m_pairs.size();
auto hashStr = reinterpret_cast<char*>(&h); char hashStr[sizeof(h) + 1];
ox_memcpy(hashStr, &h, sizeof(h));
hashStr[sizeof(h)] = 0;
while (true) { while (true) {
const auto &p = m_pairs[h]; const auto &p = m_pairs[h];
if (p == nullptr || ox_strcmp(p->key, k) == 0) { if (p == nullptr || ox_strcmp(p->key, k) == 0) {
@ -184,34 +196,23 @@ void HashMap<K, T>::erase(const K &k) {
} }
oxIgnoreError(m_keys.erase(ox::find(m_keys.begin(), m_keys.end(), k))); oxIgnoreError(m_keys.erase(ox::find(m_keys.begin(), m_keys.end(), k)));
} }
template<typename K, typename T> template<typename K, typename T>
Result<const T&> HashMap<K, T>::at(K k) const noexcept { constexpr bool HashMap<K, T>::contains(K k) const noexcept {
auto p = access(m_pairs, k);
if (!p) {
AllocAlias<T> v;
return {*reinterpret_cast<T*>(&v), OxError(1)};
}
return p->value;
}
template<typename K, typename T>
bool HashMap<K, T>::contains(K k) const noexcept {
return access(m_pairs, k) != nullptr; return access(m_pairs, k) != nullptr;
} }
template<typename K, typename T> template<typename K, typename T>
std::size_t HashMap<K, T>::size() const noexcept { constexpr std::size_t HashMap<K, T>::size() const noexcept {
return m_keys.size(); return m_keys.size();
} }
template<typename K, typename T> template<typename K, typename T>
const Vector<K> &HashMap<K, T>::keys() const noexcept { constexpr const Vector<K> &HashMap<K, T>::keys() const noexcept {
return m_keys; return m_keys;
} }
template<typename K, typename T> template<typename K, typename T>
void HashMap<K, T>::expand() { constexpr void HashMap<K, T>::expand() {
Vector<Pair*> r; Vector<Pair*> r;
for (std::size_t i = 0; i < m_keys.size(); ++i) { for (std::size_t i = 0; i < m_keys.size(); ++i) {
auto k = m_keys[i]; auto k = m_keys[i];
@ -221,7 +222,7 @@ void HashMap<K, T>::expand() {
} }
template<typename K, typename T> template<typename K, typename T>
uint64_t HashMap<K, T>::hash(K k, int len) noexcept { constexpr uint64_t HashMap<K, T>::hash(K k, int len) noexcept {
uint64_t sum = 1; uint64_t sum = 1;
for (auto i = 0u; i < static_cast<uint64_t>(len) && k[i]; ++i) { for (auto i = 0u; i < static_cast<uint64_t>(len) && k[i]; ++i) {
sum += ((sum + static_cast<uint64_t>(k[i])) * 7) * sum; sum += ((sum + static_cast<uint64_t>(k[i])) * 7) * sum;
@ -230,9 +231,11 @@ uint64_t HashMap<K, T>::hash(K k, int len) noexcept {
} }
template<typename K, typename T> template<typename K, typename T>
typename HashMap<K, T>::Pair *const&HashMap<K, T>::access(const Vector<Pair*> &pairs, K k) const { constexpr typename HashMap<K, T>::Pair *const&HashMap<K, T>::access(const Vector<Pair*> &pairs, K k) const {
auto h = hash(k) % pairs.size(); auto h = hash(k) % pairs.size();
auto hashStr = reinterpret_cast<char*>(&h); char hashStr[sizeof(h) + 1];
ox_memcpy(hashStr, &h, sizeof(h));
hashStr[sizeof(h)] = 0;
while (true) { while (true) {
const auto &p = pairs[h]; const auto &p = pairs[h];
if (p == nullptr || ox_strcmp(p->key, k) == 0) { if (p == nullptr || ox_strcmp(p->key, k) == 0) {
@ -244,9 +247,11 @@ typename HashMap<K, T>::Pair *const&HashMap<K, T>::access(const Vector<Pair*> &p
} }
template<typename K, typename T> template<typename K, typename T>
typename HashMap<K, T>::Pair *&HashMap<K, T>::access(Vector<Pair*> &pairs, K k) { constexpr typename HashMap<K, T>::Pair *&HashMap<K, T>::access(Vector<Pair*> &pairs, K k) {
auto h = hash(k) % pairs.size(); auto h = hash(k) % pairs.size();
auto hashStr = reinterpret_cast<char*>(&h); char hashStr[sizeof(h) + 1];
ox_memcpy(hashStr, &h, sizeof(h));
hashStr[sizeof(h)] = 0;
while (true) { while (true) {
auto &p = pairs[h]; auto &p = pairs[h];
if (p == nullptr || ox_strcmp(p->key, k) == 0) { if (p == nullptr || ox_strcmp(p->key, k) == 0) {
@ -258,7 +263,7 @@ typename HashMap<K, T>::Pair *&HashMap<K, T>::access(Vector<Pair*> &pairs, K k)
} }
template<typename K, typename T> template<typename K, typename T>
void HashMap<K, T>::clear() { constexpr void HashMap<K, T>::clear() {
for (std::size_t i = 0; i < m_pairs.size(); i++) { for (std::size_t i = 0; i < m_pairs.size(); i++) {
delete m_pairs[i]; delete m_pairs[i];
} }