Browse Source

Remove Hash.cpp; replace str_hash usage with jessilib::text_hashi and jessilib::text_hash; updated jessilib

master
Jessica James 3 years ago
parent
commit
eaef6bf23f
  1. 1
      src/common/CMakeLists.txt
  2. 83
      src/common/Hash.cpp
  3. 14
      src/include/Jupiter/Config.h
  4. 2
      src/include/Jupiter/HTTP_QueryString.h
  5. 170
      src/include/Jupiter/Hash.h
  6. 7
      src/include/Jupiter/IRC_Client.h
  7. 2
      src/jessilib

1
src/common/CMakeLists.txt

@ -10,7 +10,6 @@ set(SOURCE_FILES
File.cpp
Functions.c
GenericCommand.cpp
Hash.cpp
HTTP_Server.cpp
INIConfig.cpp
IRC_Client.cpp

83
src/common/Hash.cpp

@ -1,83 +0,0 @@
/**
* Copyright (C) 2015-2016 Jessica James.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Written by Jessica James <jessica.aj@outlook.com>
*/
#include "Hash.h"
/** Constants */
constexpr uint32_t JUPITER_FNV_1_32_OFFSET_BASIS = 2166136261UL;
constexpr uint64_t JUPITER_FNV_1_64_OFFSET_BASIS = 14695981039346656037ULL;
constexpr uint32_t JUPITER_FNV_1_32_PRIME = 16777619UL;
constexpr uint64_t JUPITER_FNV_1_64_PRIME = 1099511628211ULL;
/** Fowler-Noll-Vo */
uint64_t Jupiter::fnv1(const uint8_t *data, const uint8_t *end)
{
uint64_t hash = JUPITER_FNV_1_64_OFFSET_BASIS;
while (data != end)
{
hash = hash * JUPITER_FNV_1_64_PRIME;
hash = hash ^ *data;
++data;
}
return hash;
}
uint64_t Jupiter::fnv1a(const uint8_t *data, const uint8_t *end)
{
uint64_t hash = JUPITER_FNV_1_64_OFFSET_BASIS;
while (data != end)
{
hash = hash ^ *data;
hash = hash * JUPITER_FNV_1_64_PRIME;
++data;
}
return hash;
}
uint32_t Jupiter::fnv1_32(const uint8_t *data, const uint8_t *end)
{
uint32_t hash = JUPITER_FNV_1_32_OFFSET_BASIS;
while (data != end)
{
hash = hash * JUPITER_FNV_1_32_PRIME;
hash = hash ^ *data;
++data;
}
return hash;
}
uint32_t Jupiter::fnv1a_32(const uint8_t *data, const uint8_t *end)
{
uint32_t hash = JUPITER_FNV_1_32_OFFSET_BASIS;
while (data != end)
{
hash = hash ^ *data;
hash = hash * JUPITER_FNV_1_32_PRIME;
++data;
}
return hash;
}

14
src/include/Jupiter/Config.h

@ -26,8 +26,8 @@
#include <unordered_map>
#include <memory>
#include "jessilib/unicode.hpp"
#include "Jupiter.h"
#include "Hash.h"
#include "Readable_String.h" // from_string
/** DLL Linkage Nagging */
@ -38,6 +38,14 @@
namespace Jupiter
{
#ifdef __cpp_lib_generic_unordered_lookup
using InMapKeyType = std::string_view;
#define JUPITER_WRAP_MAP_KEY(in_key) in_key
#else // We can't use std::string_view for InKeyType until GCC 11 & clang 12, and I still want to support GCC 9
using InMapKeyType = std::string;
#define JUPITER_WRAP_MAP_KEY(in_key) static_cast<Jupiter::InMapKeyType>(in_key)
#endif // __cpp_lib_generic_unordered_lookup
/**
* @brief Base class for all Config type files
*/
@ -45,8 +53,8 @@ namespace Jupiter
{
public:
/** Hash_Table type for sections */
using SectionHashTable = std::unordered_map<std::string, Config, str_hash<char>, std::equal_to<>>;
using ValuesHashTable = std::unordered_map<std::string, std::string, str_hash<char>, std::equal_to<>>;
using SectionHashTable = std::unordered_map<std::string, Config, jessilib::text_hashi, jessilib::text_equali>;
using ValuesHashTable = std::unordered_map<std::string, std::string, jessilib::text_hashi, jessilib::text_equali>;
using InKeyType = InMapKeyType;
Config() = default;

2
src/include/Jupiter/HTTP_QueryString.h

@ -41,7 +41,7 @@ namespace Jupiter
HTMLFormResponse() = delete;
inline HTMLFormResponse(std::string_view query_string) : HTMLFormResponse(query_string.data(), query_string.size()) {}
inline HTMLFormResponse(const char *ptr, size_t str_size);
using TableType = std::unordered_map<std::string, std::string, Jupiter::str_hash<char>, std::equal_to<>>;
using TableType = std::unordered_map<std::string, std::string, jessilib::text_hash, jessilib::text_equal>;
#ifdef __cpp_lib_generic_unordered_lookup
using InKeyType = std::string_view;
#else // We can't use std::string_view for InKeyType until GCC 11 & clang 12, and I still want to support GCC 9

170
src/include/Jupiter/Hash.h

@ -1,170 +0,0 @@
/**
* Copyright (C) 2015-2016 Jessica James.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Written by Jessica James <jessica.aj@outlook.com>
*/
#if !defined _HASH_H_HEADER
#define _HASH_H_HEADER
/**
* @file Hash.h
* @brief Defines some hashing algorithms.
*/
#include <cstdint>
#include <string_view>
#include "Jupiter.h"
namespace Jupiter
{
/** Sums */
template<typename T = char, typename R = uint64_t> inline R calcsum(const T *in_data, size_t in_length);
template<typename T = char, typename R = uint64_t> inline R calcsum(const std::basic_string_view<T> &str);
/** Fowler-Noll-Vo hash algorithms */
template<typename T> inline uint64_t fnv1(const T &data);
template<typename T = char> inline uint64_t fnv1(const T *str, size_t length);
template<typename T = char> inline uint64_t fnv1(const std::basic_string_view<T> &str);
template<typename T> inline uint32_t fnv1_32(const T &data);
template<typename T = char> inline uint32_t fnv1_32(const T *str, size_t length);
template<typename T = char> inline uint32_t fnv1_32(const std::basic_string_view<T> &str);
template<typename T> inline uint64_t fnv1a(const T &data);
template<typename T = char> inline uint64_t fnv1a(const T *str, size_t length);
template<typename T = char> inline uint64_t fnv1a(const std::basic_string_view<T> &str);
template<typename T> inline uint32_t fnv1a_32(const T &data);
template<typename T = char> inline uint32_t fnv1a_32(const T *str, size_t length);
template<typename T = char> inline uint32_t fnv1a_32(const std::basic_string_view<T> &str);
JUPITER_API uint64_t fnv1(const uint8_t *data, const uint8_t *end);
JUPITER_API uint64_t fnv1a(const uint8_t *data, const uint8_t *end);
JUPITER_API uint32_t fnv1_32(const uint8_t *data, const uint8_t *end);
JUPITER_API uint32_t fnv1a_32(const uint8_t *data, const uint8_t *end);
/** unordered_map helpers */
template<typename CharT>
struct str_hash {
using is_transparent = std::true_type;
// C++17 introduces a requirement that these two operators return the same values for same CharT type, but not
// any requirement that std::hash<> be able to accept both key types. This just ties them for convenience
auto operator()(std::basic_string_view<CharT> in_key) const noexcept {
return std::hash<std::basic_string_view<CharT>>()(in_key);
}
auto operator()(const std::basic_string<CharT>& in_key) const noexcept {
return std::hash<std::basic_string<CharT>>()(in_key);
}
};
#ifdef __cpp_lib_generic_unordered_lookup
using InMapKeyType = std::string_view;
#define JUPITER_WRAP_MAP_KEY(in_key) in_key
#else // We can't use std::string_view for InKeyType until GCC 11 & clang 12, and I still want to support GCC 9
using InMapKeyType = std::string;
#define JUPITER_WRAP_MAP_KEY(in_key) static_cast<Jupiter::InMapKeyType>(in_key)
#endif // __cpp_lib_generic_unordered_lookup
using default_hash_function = str_hash<char>;
}
/** Calcsum implementation */
template<typename T, typename R> inline R Jupiter::calcsum(const T *in_data, size_t in_length)
{
const uint8_t *itr = reinterpret_cast<const uint8_t *>(in_data);
const uint8_t *end = reinterpret_cast<const uint8_t *>(in_data + in_length);
R sum = 0;
while (itr != end)
{
sum += *itr;
++itr;
}
return sum;
}
template<typename T, typename R> inline R Jupiter::calcsum(const std::basic_string_view<T> &str)
{
return Jupiter::calcsum<T, R>(str.data(), str.size());
}
/** fnv1 implementation */
template<typename T> inline uint64_t Jupiter::fnv1(const T &data)
{
return Jupiter::fnv1(&data, &data + 1);
}
template<typename T> inline uint64_t Jupiter::fnv1(const T *data, size_t length)
{
return Jupiter::fnv1(reinterpret_cast<const uint8_t *>(data), reinterpret_cast<const uint8_t *>(data + length));
}
template<typename T> inline uint64_t Jupiter::fnv1(const std::basic_string_view<T> &data)
{
return Jupiter::fnv1(reinterpret_cast<const uint8_t *>(data.data()), reinterpret_cast<const uint8_t *>(data.data() + data.size()));
}
template<typename T> inline uint32_t Jupiter::fnv1_32(const T &data)
{
return Jupiter::fnv1_32(&data, &data + 1);
}
template<typename T> inline uint32_t Jupiter::fnv1_32(const T *data, size_t length)
{
return Jupiter::fnv1_32(reinterpret_cast<const uint8_t *>(data), reinterpret_cast<const uint8_t *>(data + length));
}
template<typename T> inline uint32_t Jupiter::fnv1_32(const std::basic_string_view<T> &data)
{
return Jupiter::fnv1_32(reinterpret_cast<const uint8_t *>(data.data()), reinterpret_cast<const uint8_t *>(data.data() + data.size()));
}
template<typename T> inline uint64_t Jupiter::fnv1a(const T &data)
{
return Jupiter::fnv1a(reinterpret_cast<const uint8_t *>(&data), reinterpret_cast<const uint8_t *>(&data + 1));
}
template<typename T> inline uint64_t Jupiter::fnv1a(const T *data, size_t length)
{
return Jupiter::fnv1a(reinterpret_cast<const uint8_t *>(data), reinterpret_cast<const uint8_t *>(data + length));
}
template<typename T> inline uint64_t Jupiter::fnv1a(const std::basic_string_view<T> &data)
{
return Jupiter::fnv1a(data.data(), data.size());
}
template<typename T> inline uint32_t Jupiter::fnv1a_32(const T &data)
{
return Jupiter::fnv1a_32(reinterpret_cast<const uint8_t *>(&data), reinterpret_cast<const uint8_t *>(&data + 1));
}
template<typename T> inline uint32_t Jupiter::fnv1a_32(const T *data, size_t length)
{
return Jupiter::fnv1a_32(reinterpret_cast<const uint8_t *>(data), reinterpret_cast<const uint8_t *>(data + length));
}
template<typename T> inline uint32_t Jupiter::fnv1a_32(const std::basic_string_view<T> &data)
{
return Jupiter::fnv1a_32(data.data(), data.size());
}
#endif // _HASH_H_HEADER

7
src/include/Jupiter/IRC_Client.h

@ -27,6 +27,7 @@
#include <cstdlib>
#include <cstdio>
#include <utility>
#include "jessilib/unicode.hpp"
#include "Jupiter.h"
#include "Thinker.h"
#include "IRC.h"
@ -314,7 +315,7 @@ namespace Jupiter
std::string m_prefixes;
};
using UserTableType = std::unordered_map<std::string, std::shared_ptr<Channel::User>, default_hash_function, std::equal_to<>>;
using UserTableType = std::unordered_map<std::string, std::shared_ptr<Channel::User>, jessilib::text_hashi, jessilib::text_equali>;
/**
* @brief Returns the name of the channel.
@ -428,8 +429,8 @@ namespace Jupiter
bool m_adding_names;
}; // Jupiter::IRC::Client::Channel class
using ChannelTableType = std::unordered_map<std::string, Client::Channel, default_hash_function, std::equal_to<>>;
using UserTableType = std::unordered_map<std::string, std::shared_ptr<Client::User>, default_hash_function, std::equal_to<>>;
using ChannelTableType = std::unordered_map<std::string, Client::Channel, jessilib::text_hashi, jessilib::text_equali>;
using UserTableType = std::unordered_map<std::string, std::shared_ptr<Client::User>, jessilib::text_hashi, jessilib::text_equali>;
/**
* @brief Returns the name of the primary config section this client reads from.

2
src/jessilib

@ -1 +1 @@
Subproject commit bce3bfefc6323754656d8153630b80155e4644e2
Subproject commit 8d3efe083522c94a0dcfd31f34a07130b824f215
Loading…
Cancel
Save