[ox/std] Replace HashMap::operator[] const with HashMap::at const

This commit is contained in:
Gary Talent 2021-04-01 02:32:50 -05:00
parent 392351ba48
commit 291027e41e
3 changed files with 44 additions and 43 deletions

View File

@ -39,15 +39,17 @@ ClArgs::ClArgs(int argc, const char **args) {
} }
bool ClArgs::getBool(const char *arg) const { bool ClArgs::getBool(const char *arg) const {
return m_bools[arg]; auto out = m_ints.at(arg);
return out.value ? *out.value : false;
} }
String ClArgs::getString(const char *argName, const char *defaultArg) const { String ClArgs::getString(const char *argName, const char *defaultArg) const {
return m_strings.contains(argName) ? m_strings[argName].c_str() : defaultArg; return m_strings.contains(argName) ? m_strings.at(argName).value->c_str() : defaultArg;
} }
int ClArgs::getInt(const char *arg) const { int ClArgs::getInt(const char *arg) const {
return m_ints[arg]; auto out = m_ints.at(arg);
return out.value ? *out.value : 0;
} }
} }

View File

@ -29,21 +29,6 @@ target_link_libraries(
OxMetalClaw OxMetalClaw
) )
if(OX_BUILD_EXEC STREQUAL "ON")
#add_executable(
# oxfstool
# toollib.cpp
# oxfstool.cpp
#)
#set_target_properties(oxfstool PROPERTIES OUTPUT_NAME oxfs)
#target_link_libraries(
# oxfstool
# OxFS
# OxMetalClaw
# OxStd
#)
endif()
install( install(
FILES FILES
filestore/filestoretemplate.hpp filestore/filestoretemplate.hpp

View File

@ -24,8 +24,8 @@ class HashMap {
K key = {}; K key = {};
T value = {}; T value = {};
}; };
mutable Vector<K> m_keys; Vector<K> m_keys;
mutable Vector<Pair*> m_pairs; Vector<Pair*> m_pairs;
public: public:
explicit HashMap(std::size_t size = 100); explicit HashMap(std::size_t size = 100);
@ -38,11 +38,6 @@ class HashMap {
HashMap &operator=(HashMap &other); HashMap &operator=(HashMap &other);
/**
* K is assumed to be a null terminated string.
*/
const T &operator[](K key) const;
/** /**
* K is assumed to be a null terminated string. * K is assumed to be a null terminated string.
*/ */
@ -53,6 +48,11 @@ class HashMap {
*/ */
T &at(K key); T &at(K key);
/**
* K is assumed to be a null terminated string.
*/
Result<const T*> at(K key) const;
bool contains(K key) const; bool contains(K key) const;
std::size_t size() const noexcept; std::size_t size() const noexcept;
@ -60,7 +60,7 @@ class HashMap {
const Vector<K> &keys() const noexcept; const Vector<K> &keys() const noexcept;
private: private:
void expand() const; void expand();
/** /**
* K is assumed to be a null terminated string. * K is assumed to be a null terminated string.
@ -70,7 +70,12 @@ class HashMap {
/** /**
* 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) const; Pair *const&access(const Vector<Pair*> &pairs, K key) const;
/**
* K is assumed to be a null terminated string.
*/
Pair *&access(Vector<Pair*> &pairs, K key);
}; };
@ -112,20 +117,6 @@ HashMap<K, T> &HashMap<K, T>::operator=(HashMap<K, T> &other) {
return *this; return *this;
} }
template<typename K, typename T>
const T &HashMap<K, T>::operator[](K k) const {
auto &p = access(m_pairs, k);
if (p == nullptr) {
if (m_pairs.size() * 0.7 < m_keys.size()) {
expand();
}
p = new Pair;
p->key = k;
m_keys.push_back(k);
}
return p->value;
}
template<typename K, typename T> template<typename K, typename T>
T &HashMap<K, T>::operator[](K k) { T &HashMap<K, T>::operator[](K k) {
auto &p = access(m_pairs, k); auto &p = access(m_pairs, k);
@ -145,6 +136,15 @@ T &HashMap<K, T>::at(K k) {
return operator[](k); return operator[](k);
} }
template<typename K, typename T>
Result<const T*> HashMap<K, T>::at(K k) const {
auto p = access(m_pairs, k);
if (!p) {
return {nullptr, OxError(1)};
}
return &p->value;
}
template<typename K, typename T> template<typename K, typename T>
bool HashMap<K, T>::contains(K k) const { bool HashMap<K, T>::contains(K k) const {
return access(m_pairs, k) != nullptr; return access(m_pairs, k) != nullptr;
@ -161,7 +161,7 @@ const Vector<K> &HashMap<K, T>::keys() const noexcept {
} }
template<typename K, typename T> template<typename K, typename T>
void HashMap<K, T>::expand() const { 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];
@ -180,7 +180,21 @@ 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 *&HashMap<K, T>::access(Vector<Pair*> &pairs, K k) const { typename HashMap<K, T>::Pair *const&HashMap<K, T>::access(const Vector<Pair*> &pairs, K k) const {
auto h = hash(k) % pairs.size();
auto hashStr = reinterpret_cast<char*>(&h);
while (1) {
const auto &p = pairs[h];
if (p == nullptr || ox_strcmp(p->key, k) == 0) {
return p;
} else {
h = hash(hashStr, 8) % pairs.size();
}
}
}
template<typename K, typename T>
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); auto hashStr = reinterpret_cast<char*>(&h);
while (1) { while (1) {