fixed null terminated char* hash (unsafe)
This commit is contained in:
@@ -26,8 +26,7 @@ LL_Type sum(const VT<T>& v) {
|
||||
template<class T, template<typename ...> class VT>
|
||||
//types::GetFPType<T>
|
||||
double avg(const VT<T>& v) {
|
||||
return static_cast<types::GetFPType<T>>(
|
||||
sum<T>(v) / static_cast<long double>(v.size));
|
||||
return (sum<T>(v) / static_cast<double>(v.size));
|
||||
}
|
||||
|
||||
template<class T, template<typename ...> class VT>
|
||||
|
||||
+26
-3
@@ -1,18 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <type_traits>
|
||||
#include <tuple>
|
||||
#include "types.h"
|
||||
// only works for 64 bit systems
|
||||
constexpr size_t _FNV_offset_basis = 14695981039346656037ULL;
|
||||
constexpr size_t _FNV_prime = 1099511628211ULL;
|
||||
|
||||
inline size_t append_bytes(const unsigned char* _First) noexcept {
|
||||
size_t _Val = _FNV_offset_basis;
|
||||
for (; *_First; ++_First) {
|
||||
_Val ^= static_cast<size_t>(*_First);
|
||||
_Val *= _FNV_prime;
|
||||
}
|
||||
|
||||
return _Val;
|
||||
}
|
||||
|
||||
inline size_t append_bytes(const astring_view& view) noexcept {
|
||||
return append_bytes(view.str);
|
||||
}
|
||||
|
||||
|
||||
template <class ...Types>
|
||||
struct hasher {
|
||||
template <size_t i = 0> typename std::enable_if< i == sizeof...(Types),
|
||||
size_t>::type hashi(const std::tuple<Types...>& record) const {
|
||||
return 0;
|
||||
return 534235245539ULL;
|
||||
}
|
||||
|
||||
template <size_t i = 0> typename std::enable_if< i < sizeof ...(Types),
|
||||
size_t>::type hashi(const std::tuple<Types...>& record) const {
|
||||
using current_type = typename std::decay<typename std::tuple_element<i, std::tuple<Types...>>::type>::type;
|
||||
return std::hash<current_type>()(std::get<i>(record)) ^ hashi<i+1>(record);
|
||||
if constexpr (is_cstr<current_type>())
|
||||
return append_bytes((const unsigned char*)std::get<i>(record)) ^ hashi<i + 1>(record);
|
||||
else
|
||||
return std::hash<current_type>()(std::get<i>(record)) ^ hashi<i+1>(record);
|
||||
}
|
||||
size_t operator()(const std::tuple<Types...>& record) const {
|
||||
return hashi(record);
|
||||
|
||||
+4
-4
@@ -333,9 +333,9 @@ int test_main()
|
||||
//printf("sibal: %p %p\n", aa, bb);
|
||||
|
||||
const char* qs[]= {
|
||||
"CREATE TABLE test(a INT, b INT, c INT, d INT);",
|
||||
"COPY OFFSET 2 INTO test FROM 'c:/Users/sunyi/Desktop/AQuery2/data/test2.csv' ON SERVER USING DELIMITERS ',';",
|
||||
"SELECT (a + b), a,b,c FROM test ;",
|
||||
"CREATE TABLE network(src VARCHAR(3), dst VARCHAR(3), len INT, _time INT);",
|
||||
"COPY OFFSET 2 INTO network FROM 'c:/Users/sunyi/Desktop/AQuery2/data/network.csv' ON SERVER USING DELIMITERS ',';",
|
||||
"SELECT src, dst, len, _time FROM network ORDER BY src, dst, _time",
|
||||
};
|
||||
n_recv = sizeof(qs)/(sizeof (char*));
|
||||
n_recvd = const_cast<char**>(qs);
|
||||
@@ -354,7 +354,7 @@ int test_main()
|
||||
cxt->log("handle: %p\n", handle);
|
||||
if (handle) {
|
||||
cxt->log("inner\n");
|
||||
code_snippet c = reinterpret_cast<code_snippet>(dlsym(handle, "dll_ZF5Shg"));
|
||||
code_snippet c = reinterpret_cast<code_snippet>(dlsym(handle, "dll_54aqwy"));
|
||||
cxt->log("routine: %p\n", c);
|
||||
if (c) {
|
||||
cxt->log("inner\n");
|
||||
|
||||
@@ -169,6 +169,51 @@ namespace types {
|
||||
using GetLongType = typename GetLongTypeImpl<typename std::decay<T>::type>::type;
|
||||
}
|
||||
|
||||
struct astring_view {
|
||||
const unsigned char* str = 0;
|
||||
constexpr astring_view(const char* str) :
|
||||
str((const unsigned char*)(str)) {}
|
||||
constexpr astring_view(const signed char* str) :
|
||||
str((const unsigned char*)(str)) {}
|
||||
constexpr astring_view(const unsigned char* str) :
|
||||
str(str) {}
|
||||
constexpr astring_view() = default;
|
||||
|
||||
bool operator==(const astring_view& r) const {
|
||||
auto this_str = str;
|
||||
auto other_str = r.str;
|
||||
while (*this_str && *other_str) {
|
||||
if (*this_str != *other_str)
|
||||
return false;
|
||||
this_str++;
|
||||
other_str++;
|
||||
}
|
||||
return !(*this_str || *other_str);
|
||||
}
|
||||
operator const char* () const {
|
||||
return reinterpret_cast<const char*>(str);
|
||||
}
|
||||
operator const unsigned char* () const {
|
||||
return reinterpret_cast<const unsigned char*>(str);
|
||||
}
|
||||
operator const signed char* () const {
|
||||
return reinterpret_cast<const signed char*>(str);
|
||||
}
|
||||
};
|
||||
|
||||
template<class T>
|
||||
constexpr bool is_cstr() {
|
||||
using namespace std;
|
||||
typedef decay_t<T> dT;
|
||||
return is_same_v<dT, const char*> ||
|
||||
is_same_v<dT, char*> ||
|
||||
is_same_v<dT, const signed char*> ||
|
||||
is_same_v<dT, signed char*> ||
|
||||
is_same_v<dT, const unsigned char*> ||
|
||||
is_same_v<dT, unsigned char*> ||
|
||||
is_same_v<dT, astring_view>;
|
||||
}
|
||||
|
||||
#define getT(i, t) std::tuple_element_t<i, std::tuple<t...>>
|
||||
template <template<typename ...> class T, typename ...Types>
|
||||
struct applyTemplates {
|
||||
|
||||
@@ -47,7 +47,7 @@ public:
|
||||
_Ty* container;
|
||||
uint32_t size, capacity;
|
||||
typedef _Ty* iterator_t;
|
||||
typedef _Ty value_t;
|
||||
typedef std::conditional_t<is_cstr<_Ty>(), astring_view, _Ty> value_t;
|
||||
vector_type(const uint32_t& size) : size(size), capacity(size) {
|
||||
container = (_Ty*)malloc(size * sizeof(_Ty));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user