This commit is contained in:
2022-09-18 07:39:39 +08:00
63 changed files with 10806 additions and 10806 deletions
+171 -171
View File
@@ -1,171 +1,171 @@
#pragma once
#include "types.h"
#include <utility>
#include <limits>
#include <deque>
#include <cmath>
#undef max
#undef min
template <class T, template<typename ...> class VT>
size_t count(const VT<T>& v) {
return v.size;
}
template <class T>
constexpr static inline size_t count(const T&) { return 1; }
// TODO: Specializations for dt/str/none
template<class T, template<typename ...> class VT>
types::GetLongType<T> sum(const VT<T>& v) {
types::GetLongType<T> ret = 0;
for (const auto& _v : v)
ret += _v;
return ret;
}
template<class T, template<typename ...> class VT>
types::GetFPType<T> avg(const VT<T>& v) {
return static_cast<types::GetFPType<T>>(
sum<T>(v) / static_cast<long double>(v.size));
}
template<class T, template<typename ...> class VT>
VT<double> sqrt(const VT<T>& v) {
VT<double> ret {v.size};
for (uint32_t i = 0; i < v.size; ++i){
ret[i] = sqrt(v[i]);
}
return ret;
}
template <class T, template<typename ...> class VT>
T max(const VT<T>& v) {
T max_v = std::numeric_limits<T>::min();
for (const auto& _v : v)
max_v = max_v > _v ? max_v : _v;
return max_v;
}
template <class T, template<typename ...> class VT>
T min(const VT<T>& v) {
T min_v = std::numeric_limits<T>::max();
for (const auto& _v : v)
min_v = min_v < _v ? min_v : _v;
return min_v;
}
template<class T, template<typename ...> class VT>
decayed_t<VT,T> mins(const VT<T>& arr) {
const uint32_t& len = arr.size;
std::deque<std::pair<T, uint32_t>> cache;
decayed_t<VT,T> ret(len);
T min = std::numeric_limits<T>::max();
for (int i = 0; i < len; ++i) {
if (arr[i] < min)
min = arr[i];
ret[i] = min;
}
return ret;
}
template<class T, template<typename ...> class VT>
decayed_t<VT,T> maxs(const VT<T>& arr) {
const uint32_t& len = arr.size;
decayed_t<VT,T> ret(len);
T max = std::numeric_limits<T>::min();
for (int i = 0; i < len; ++i) {
if (arr[i] > max)
max = arr[i];
ret[i] = max;
}
return ret;
}
template<class T, template<typename ...> class VT>
decayed_t<VT,T> minw(uint32_t w, const VT<T>& arr) {
const uint32_t& len = arr.size;
decayed_t<VT,T> ret{len};
std::deque<std::pair<T, uint32_t>> cache;
for (int i = 0; i < len; ++i) {
if (!cache.empty() && cache.front().second == i - w) cache.pop_front();
while (!cache.empty() && cache.back().first > arr[i]) cache.pop_back();
cache.push_back({ arr[i], i });
ret[i] = cache.front().first;
}
return ret;
}
template<class T, template<typename ...> class VT>
decayed_t<VT,T> maxw(uint32_t w, const VT<T>& arr) {
const uint32_t& len = arr.size;
decayed_t<VT, T> ret(len);
std::deque<std::pair<T, uint32_t>> cache;
for (int i = 0; i < len; ++i) {
if (!cache.empty() && cache.front().second == i - w) cache.pop_front();
while (!cache.empty() && cache.back().first > arr[i]) cache.pop_back();
cache.push_back({ arr[i], i });
arr[i] = cache.front().first;
}
return ret;
}
template<class T, template<typename ...> class VT>
decayed_t<VT, types::GetLongType<T>> sums(const VT<T>& arr) {
const uint32_t& len = arr.size;
decayed_t<VT, types::GetLongType<T>> ret(len);
uint32_t i = 0;
if(len) ret[i++] = arr[0];
for (; i < len; ++i)
ret[i] = ret[i-1] + arr[i];
return ret;
}
template<class T, template<typename ...> class VT>
decayed_t<VT, types::GetFPType<T>> avgs(const VT<T>& arr) {
const uint32_t& len = arr.size;
typedef types::GetFPType<T> FPType;
decayed_t<VT, FPType> ret(len);
uint32_t i = 0;
types::GetLongType<T> s;
if(len) s = ret[i++] = arr[0];
for (; i < len; ++i)
ret[i] = (s+=arr[i])/(FPType)(i+1);
return ret;
}
template<class T, template<typename ...> class VT>
decayed_t<VT, types::GetLongType<T>> sumw(uint32_t w, const VT<T>& arr) {
const uint32_t& len = arr.size;
decayed_t<VT, types::GetLongType<T>> ret(len);
uint32_t i = 0;
w = w > len ? len : w;
if(len) ret[i++] = arr[0];
for (; i < w; ++i)
ret[i] = ret[i-1] + arr[i];
for (; i < len; ++i)
ret[i] = ret[i-1] + arr[i] - arr[i-w];
return ret;
}
template<class T, template<typename ...> class VT>
decayed_t<VT, types::GetFPType<types::GetLongType<T>>> avgw(uint32_t w, const VT<T>& arr) {
typedef types::GetFPType<types::GetLongType<T>> FPType;
const uint32_t& len = arr.size;
decayed_t<VT, FPType> ret(len);
uint32_t i = 0;
types::GetLongType<T> s{};
w = w > len ? len : w;
if(len) s = ret[i++] = arr[0];
for (; i < w; ++i)
ret[i] = (s += arr[i])/(FPType)(i+1);
for (; i < len; ++i)
ret[i] = ret[i-1] + (arr[i] - arr[i-w])/(FPType)w;
return ret;
}
template <class T> constexpr inline T count(const T& v) { return 1; }
template <class T> constexpr inline T max(const T& v) { return v; }
template <class T> constexpr inline T min(const T& v) { return v; }
template <class T> constexpr inline T avg(const T& v) { return v; }
template <class T> constexpr inline T sum(const T& v) { return v; }
template <class T> constexpr inline T maxw(uint32_t, const T& v) { return v; }
template <class T> constexpr inline T minw(uint32_t, const T& v) { return v; }
template <class T> constexpr inline T avgw(uint32_t, const T& v) { return v; }
template <class T> constexpr inline T sumw(uint32_t, const T& v) { return v; }
template <class T> constexpr inline T maxs(const T& v) { return v; }
template <class T> constexpr inline T mins(const T& v) { return v; }
template <class T> constexpr inline T avgs(const T& v) { return v; }
template <class T> constexpr inline T sums(const T& v) { return v; }
#pragma once
#include "types.h"
#include <utility>
#include <limits>
#include <deque>
#include <cmath>
#undef max
#undef min
template <class T, template<typename ...> class VT>
size_t count(const VT<T>& v) {
return v.size;
}
template <class T>
constexpr static inline size_t count(const T&) { return 1; }
// TODO: Specializations for dt/str/none
template<class T, template<typename ...> class VT>
types::GetLongType<T> sum(const VT<T>& v) {
types::GetLongType<T> ret = 0;
for (const auto& _v : v)
ret += _v;
return ret;
}
template<class T, template<typename ...> class VT>
types::GetFPType<T> avg(const VT<T>& v) {
return static_cast<types::GetFPType<T>>(
sum<T>(v) / static_cast<long double>(v.size));
}
template<class T, template<typename ...> class VT>
VT<double> sqrt(const VT<T>& v) {
VT<double> ret {v.size};
for (uint32_t i = 0; i < v.size; ++i){
ret[i] = sqrt(v[i]);
}
return ret;
}
template <class T, template<typename ...> class VT>
T max(const VT<T>& v) {
T max_v = std::numeric_limits<T>::min();
for (const auto& _v : v)
max_v = max_v > _v ? max_v : _v;
return max_v;
}
template <class T, template<typename ...> class VT>
T min(const VT<T>& v) {
T min_v = std::numeric_limits<T>::max();
for (const auto& _v : v)
min_v = min_v < _v ? min_v : _v;
return min_v;
}
template<class T, template<typename ...> class VT>
decayed_t<VT,T> mins(const VT<T>& arr) {
const uint32_t& len = arr.size;
std::deque<std::pair<T, uint32_t>> cache;
decayed_t<VT,T> ret(len);
T min = std::numeric_limits<T>::max();
for (int i = 0; i < len; ++i) {
if (arr[i] < min)
min = arr[i];
ret[i] = min;
}
return ret;
}
template<class T, template<typename ...> class VT>
decayed_t<VT,T> maxs(const VT<T>& arr) {
const uint32_t& len = arr.size;
decayed_t<VT,T> ret(len);
T max = std::numeric_limits<T>::min();
for (int i = 0; i < len; ++i) {
if (arr[i] > max)
max = arr[i];
ret[i] = max;
}
return ret;
}
template<class T, template<typename ...> class VT>
decayed_t<VT,T> minw(uint32_t w, const VT<T>& arr) {
const uint32_t& len = arr.size;
decayed_t<VT,T> ret{len};
std::deque<std::pair<T, uint32_t>> cache;
for (int i = 0; i < len; ++i) {
if (!cache.empty() && cache.front().second == i - w) cache.pop_front();
while (!cache.empty() && cache.back().first > arr[i]) cache.pop_back();
cache.push_back({ arr[i], i });
ret[i] = cache.front().first;
}
return ret;
}
template<class T, template<typename ...> class VT>
decayed_t<VT,T> maxw(uint32_t w, const VT<T>& arr) {
const uint32_t& len = arr.size;
decayed_t<VT, T> ret(len);
std::deque<std::pair<T, uint32_t>> cache;
for (int i = 0; i < len; ++i) {
if (!cache.empty() && cache.front().second == i - w) cache.pop_front();
while (!cache.empty() && cache.back().first > arr[i]) cache.pop_back();
cache.push_back({ arr[i], i });
arr[i] = cache.front().first;
}
return ret;
}
template<class T, template<typename ...> class VT>
decayed_t<VT, types::GetLongType<T>> sums(const VT<T>& arr) {
const uint32_t& len = arr.size;
decayed_t<VT, types::GetLongType<T>> ret(len);
uint32_t i = 0;
if(len) ret[i++] = arr[0];
for (; i < len; ++i)
ret[i] = ret[i-1] + arr[i];
return ret;
}
template<class T, template<typename ...> class VT>
decayed_t<VT, types::GetFPType<T>> avgs(const VT<T>& arr) {
const uint32_t& len = arr.size;
typedef types::GetFPType<T> FPType;
decayed_t<VT, FPType> ret(len);
uint32_t i = 0;
types::GetLongType<T> s;
if(len) s = ret[i++] = arr[0];
for (; i < len; ++i)
ret[i] = (s+=arr[i])/(FPType)(i+1);
return ret;
}
template<class T, template<typename ...> class VT>
decayed_t<VT, types::GetLongType<T>> sumw(uint32_t w, const VT<T>& arr) {
const uint32_t& len = arr.size;
decayed_t<VT, types::GetLongType<T>> ret(len);
uint32_t i = 0;
w = w > len ? len : w;
if(len) ret[i++] = arr[0];
for (; i < w; ++i)
ret[i] = ret[i-1] + arr[i];
for (; i < len; ++i)
ret[i] = ret[i-1] + arr[i] - arr[i-w];
return ret;
}
template<class T, template<typename ...> class VT>
decayed_t<VT, types::GetFPType<types::GetLongType<T>>> avgw(uint32_t w, const VT<T>& arr) {
typedef types::GetFPType<types::GetLongType<T>> FPType;
const uint32_t& len = arr.size;
decayed_t<VT, FPType> ret(len);
uint32_t i = 0;
types::GetLongType<T> s{};
w = w > len ? len : w;
if(len) s = ret[i++] = arr[0];
for (; i < w; ++i)
ret[i] = (s += arr[i])/(FPType)(i+1);
for (; i < len; ++i)
ret[i] = ret[i-1] + (arr[i] - arr[i-w])/(FPType)w;
return ret;
}
template <class T> constexpr inline T count(const T& v) { return 1; }
template <class T> constexpr inline T max(const T& v) { return v; }
template <class T> constexpr inline T min(const T& v) { return v; }
template <class T> constexpr inline T avg(const T& v) { return v; }
template <class T> constexpr inline T sum(const T& v) { return v; }
template <class T> constexpr inline T maxw(uint32_t, const T& v) { return v; }
template <class T> constexpr inline T minw(uint32_t, const T& v) { return v; }
template <class T> constexpr inline T avgw(uint32_t, const T& v) { return v; }
template <class T> constexpr inline T sumw(uint32_t, const T& v) { return v; }
template <class T> constexpr inline T maxs(const T& v) { return v; }
template <class T> constexpr inline T mins(const T& v) { return v; }
template <class T> constexpr inline T avgs(const T& v) { return v; }
template <class T> constexpr inline T sums(const T& v) { return v; }
+6 -6
View File
@@ -1,6 +1,6 @@
// Hint files help the Visual Studio IDE interpret Visual C++ identifiers
// such as names of functions and macros.
// For more information see https://go.microsoft.com/fwlink/?linkid=865984
#define Ops(o) template<typename T> vector_type<typename types::Coercion<_Ty, T>::type> operator##o(const vector_type<T>& r) { [[likely]] if (r.size == size) { return add(r); } else if (r.size == 1 || size == 1) { const bool lr = size == 1; const uint32_t& _size = lr ? r.size : size; const auto& _container = lr ? r.container : container; const auto& scalar = *(lr ? container : r.container); vector_type<typename types::Coercion<_Ty, T>::type> ret(_size); for (int i = 0; i < _size; ++i) ret[i] = _container[i] o scalar; return ret; } }
#define Op(o, x) template<typename T> vector_type<typename types::Coercion<_Ty, T>::type> inline x(const vector_type<T>& r) { vector_type<typename types::Coercion<_Ty, T>::type> ret(size); for (int i = 0; i < size; ++i) ret[i] = container[i] o r[i]; return ret; }
#define _Make_Ops(M) M(+, add) M(-, minus) M(*, multi) M(/, div) M(%, mod) M(&, and) M(|, or) M(^, xor)
// Hint files help the Visual Studio IDE interpret Visual C++ identifiers
// such as names of functions and macros.
// For more information see https://go.microsoft.com/fwlink/?linkid=865984
#define Ops(o) template<typename T> vector_type<typename types::Coercion<_Ty, T>::type> operator##o(const vector_type<T>& r) { [[likely]] if (r.size == size) { return add(r); } else if (r.size == 1 || size == 1) { const bool lr = size == 1; const uint32_t& _size = lr ? r.size : size; const auto& _container = lr ? r.container : container; const auto& scalar = *(lr ? container : r.container); vector_type<typename types::Coercion<_Ty, T>::type> ret(_size); for (int i = 0; i < _size; ++i) ret[i] = _container[i] o scalar; return ret; } }
#define Op(o, x) template<typename T> vector_type<typename types::Coercion<_Ty, T>::type> inline x(const vector_type<T>& r) { vector_type<typename types::Coercion<_Ty, T>::type> ret(size); for (int i = 0; i < size; ++i) ret[i] = container[i] o r[i]; return ret; }
#define _Make_Ops(M) M(+, add) M(-, minus) M(*, multi) M(/, div) M(%, mod) M(&, and) M(|, or) M(^, xor)
+52 -52
View File
@@ -1,53 +1,53 @@
#pragma once
#include <vector_type>
#include <utility>
#include <thread>
#include <chrono>
class GC {
template<class T>
using vector = vector_type<T>;
template<class ...T>
using tuple = std::tuple<T...>;
size_t current_size, max_size, interval, forced_clean;
bool running, alive;
// ptr, dealloc, ref, sz
vector<tuple<void*, void (*)(void*)>> q;
std::thread handle;
void gc()
{
}
void reg(void* v, uint32_t ref, uint32_t sz,
void(*f)(void*) = [](void* v) {free (v); }) {
current_size += sz;
if (current_size > max_size)
gc();
q.push_back({ v, f });
}
void daemon() {
using namespace std::chrono;
while (alive) {
if (running) {
gc();
std::this_thread::sleep_for(microseconds(interval));
}
else {
std::this_thread::sleep_for(10ms);
}
}
}
void start_deamon() {
handle = std::thread(&daemon);
alive = true;
}
void terminate_daemon() {
running = false;
alive = false;
using namespace std::chrono;
if (handle.joinable()) {
std::this_thread::sleep_for(microseconds(1000 + std::max(static_cast<size_t>(10000), interval)));
handle.join();
}
}
#pragma once
#include <vector_type>
#include <utility>
#include <thread>
#include <chrono>
class GC {
template<class T>
using vector = vector_type<T>;
template<class ...T>
using tuple = std::tuple<T...>;
size_t current_size, max_size, interval, forced_clean;
bool running, alive;
// ptr, dealloc, ref, sz
vector<tuple<void*, void (*)(void*)>> q;
std::thread handle;
void gc()
{
}
void reg(void* v, uint32_t ref, uint32_t sz,
void(*f)(void*) = [](void* v) {free (v); }) {
current_size += sz;
if (current_size > max_size)
gc();
q.push_back({ v, f });
}
void daemon() {
using namespace std::chrono;
while (alive) {
if (running) {
gc();
std::this_thread::sleep_for(microseconds(interval));
}
else {
std::this_thread::sleep_for(10ms);
}
}
}
void start_deamon() {
handle = std::thread(&daemon);
alive = true;
}
void terminate_daemon() {
running = false;
alive = false;
using namespace std::chrono;
if (handle.joinable()) {
std::this_thread::sleep_for(microseconds(1000 + std::max(static_cast<size_t>(10000), interval)));
handle.join();
}
}
};
+20 -20
View File
@@ -1,20 +1,20 @@
#pragma once
#include <functional>
#include <tuple>
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;
}
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);
}
size_t operator()(const std::tuple<Types...>& record) const {
return hashi(record);
}
};
#pragma once
#include <functional>
#include <tuple>
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;
}
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);
}
size_t operator()(const std::tuple<Types...>& record) const {
return hashi(record);
}
};
+81 -81
View File
@@ -1,81 +1,81 @@
#pragma once
#include "types.h"
#include <cstdio>
#include <string>
#include <limits>
#include <cstring>
template <class ...Types>
std::string generate_printf_string(const char* sep = " ", const char* end = "\n") {
std::string str;
((str += types::printf_str[types::Types<value_type_r<Types>>::getType()], str += sep), ...);
const auto trim = str.size() - strlen(sep);
if (trim > 0)
str.resize(trim);
str += end;
return str;
}
template<class T>
inline decltype(auto) print_hook(const T& v){
return v;
}
template<>
inline decltype(auto) print_hook<bool>(const bool& v) {
return v? "true" : "false";
}
#ifdef __SIZEOF_INT128__
constexpr struct __int128__struct{
uint64_t low, high;
// constexpr bool operator==(__int128_t x) const{
// return (x>>64) == high and (x&0xffffffffffffffffull) == low;
// }
bool operator==(__int128_t x) const{
return *((const __int128_t*) this) == x;
}
}__int128_max_v = {0x0000000000000000ull, 0x8000000000000000ull};
inline const char* get_int128str(__int128_t v, char* buf){
bool neg = false;
if (v < 0) {
if(__int128_max_v == v)
return "-170141183460469231731687303715884105728";
v = -v;
neg = true;
}
do {
*--buf = v%10 + '0';
v /= 10;
} while(v);
if (neg) *--buf = '-';
return buf;
}
inline const char* get_uint128str(__uint128_t v, char* buf){
do {
*--buf = v%10 + '0';
v /= 10;
} while(v);
return buf;
}
extern char* gbuf;
void setgbuf(char* buf = 0);
template<>
inline decltype(auto) print_hook<__int128_t>(const __int128_t& v) {
*(gbuf+=40) = 0;
return get_int128str(v, gbuf++);
}
template<>
inline decltype(auto) print_hook<__uint128_t>(const __uint128_t& v) {
*(gbuf+=40) = 0;
return get_uint128str(v, gbuf++);
}
#else
#define setgbuf()
#endif
#pragma once
#include "types.h"
#include <cstdio>
#include <string>
#include <limits>
#include <cstring>
template <class ...Types>
std::string generate_printf_string(const char* sep = " ", const char* end = "\n") {
std::string str;
((str += types::printf_str[types::Types<value_type_r<Types>>::getType()], str += sep), ...);
const auto trim = str.size() - strlen(sep);
if (trim > 0)
str.resize(trim);
str += end;
return str;
}
template<class T>
inline decltype(auto) print_hook(const T& v){
return v;
}
template<>
inline decltype(auto) print_hook<bool>(const bool& v) {
return v? "true" : "false";
}
#ifdef __SIZEOF_INT128__
constexpr struct __int128__struct{
uint64_t low, high;
// constexpr bool operator==(__int128_t x) const{
// return (x>>64) == high and (x&0xffffffffffffffffull) == low;
// }
bool operator==(__int128_t x) const{
return *((const __int128_t*) this) == x;
}
}__int128_max_v = {0x0000000000000000ull, 0x8000000000000000ull};
inline const char* get_int128str(__int128_t v, char* buf){
bool neg = false;
if (v < 0) {
if(__int128_max_v == v)
return "-170141183460469231731687303715884105728";
v = -v;
neg = true;
}
do {
*--buf = v%10 + '0';
v /= 10;
} while(v);
if (neg) *--buf = '-';
return buf;
}
inline const char* get_uint128str(__uint128_t v, char* buf){
do {
*--buf = v%10 + '0';
v /= 10;
} while(v);
return buf;
}
extern char* gbuf;
void setgbuf(char* buf = 0);
template<>
inline decltype(auto) print_hook<__int128_t>(const __int128_t& v) {
*(gbuf+=40) = 0;
return get_int128str(v, gbuf++);
}
template<>
inline decltype(auto) print_hook<__uint128_t>(const __uint128_t& v) {
*(gbuf+=40) = 0;
return get_uint128str(v, gbuf++);
}
#else
#define setgbuf()
#endif
+79 -79
View File
@@ -1,79 +1,79 @@
#ifndef _AQUERY_H
#define _AQUERY_H
#include "table.h"
#include <unordered_map>
enum Log_level {
LOG_INFO,
LOG_ERROR,
LOG_SILENT
};
enum Backend_Type {
BACKEND_AQuery,
BACKEND_MonetDB,
BACKEND_MariaDB
};
struct Config{
int running, new_query, server_mode,
backend_type, has_dll, n_buffers;
int buffer_sizes[];
};
struct Session{
struct Statistic{
size_t total_active;
size_t cnt_object;
size_t total_alloc;
} stats;
void* memory_map;
};
struct Context{
typedef int (*printf_type) (const char *format, ...);
void* module_function_maps = 0;
Config* cfg;
int n_buffers, *sz_bufs;
void **buffers;
void* alt_server = 0;
Log_level log_level = LOG_INFO;
Session current;
#ifdef THREADING
void* thread_pool;
#endif
printf_type print = printf;
Context();
virtual ~Context();
template <class ...Types>
void log(Types... args) {
if (log_level == LOG_INFO)
print(args...);
}
template <class ...Types>
void err(Types... args) {
if (log_level <= LOG_ERROR)
print(args...);
}
void init_session();
void end_session();
void* get_module_function(const char*);
std::unordered_map<const char*, void*> tables;
std::unordered_map<const char*, uColRef *> cols;
};
#ifdef _WIN32
#define __DLLEXPORT__ __declspec(dllexport) __stdcall
#else
#define __DLLEXPORT__
#endif
#define __AQEXPORT__(_Ty) extern "C" _Ty __DLLEXPORT__
typedef void (*deallocator_t) (void*);
#endif
#ifndef _AQUERY_H
#define _AQUERY_H
#include "table.h"
#include <unordered_map>
enum Log_level {
LOG_INFO,
LOG_ERROR,
LOG_SILENT
};
enum Backend_Type {
BACKEND_AQuery,
BACKEND_MonetDB,
BACKEND_MariaDB
};
struct Config{
int running, new_query, server_mode,
backend_type, has_dll, n_buffers;
int buffer_sizes[];
};
struct Session{
struct Statistic{
size_t total_active;
size_t cnt_object;
size_t total_alloc;
} stats;
void* memory_map;
};
struct Context{
typedef int (*printf_type) (const char *format, ...);
void* module_function_maps = 0;
Config* cfg;
int n_buffers, *sz_bufs;
void **buffers;
void* alt_server = 0;
Log_level log_level = LOG_INFO;
Session current;
#ifdef THREADING
void* thread_pool;
#endif
printf_type print = printf;
Context();
virtual ~Context();
template <class ...Types>
void log(Types... args) {
if (log_level == LOG_INFO)
print(args...);
}
template <class ...Types>
void err(Types... args) {
if (log_level <= LOG_ERROR)
print(args...);
}
void init_session();
void end_session();
void* get_module_function(const char*);
std::unordered_map<const char*, void*> tables;
std::unordered_map<const char*, uColRef *> cols;
};
#ifdef _WIN32
#define __DLLEXPORT__ __declspec(dllexport) __stdcall
#else
#define __DLLEXPORT__
#endif
#define __AQEXPORT__(_Ty) extern "C" _Ty __DLLEXPORT__
typedef void (*deallocator_t) (void*);
#endif
+18 -18
View File
@@ -1,19 +1,19 @@
#pragma once
#include "vector_type.hpp"
#include <algorithm>
#include <stdint.h>
template <class Comparator, typename T = uint32_t>
class priority_vector : public vector_type<T> {
const Comparator comp;
public:
priority_vector(Comparator comp = std::less<T>{}) :
comp(comp), vector_type<T>(0) {}
void emplace_back(T val) {
vector_type<T>::emplace_back(val);
std::push_heap(container, container + size, comp);
}
void pop_back() {
std::pop_heap(container, container + size, comp);
--size;
}
#pragma once
#include "vector_type.hpp"
#include <algorithm>
#include <stdint.h>
template <class Comparator, typename T = uint32_t>
class priority_vector : public vector_type<T> {
const Comparator comp;
public:
priority_vector(Comparator comp = std::less<T>{}) :
comp(comp), vector_type<T>(0) {}
void emplace_back(T val) {
vector_type<T>::emplace_back(val);
std::push_heap(container, container + size, comp);
}
void pop_back() {
std::pop_heap(container, container + size, comp);
--size;
}
};
+369 -369
View File
@@ -1,369 +1,369 @@
#include "pch.hpp"
#include "../csv.h"
#include <iostream>
#include <string>
#include <chrono>
#include "libaquery.h"
#include "monetdb_conn.h"
#ifdef THREADING
#include "threading.h"
#endif
#ifdef _WIN32
#include "winhelper.h"
#else
#include <dlfcn.h>
#include <fcntl.h>
#include <sys/mman.h>
struct SharedMemory
{
int hFileMap;
void* pData;
SharedMemory(const char* fname) {
hFileMap = open(fname, O_RDWR, 0);
if (hFileMap != -1)
pData = mmap(NULL, 8, PROT_READ | PROT_WRITE, MAP_SHARED, hFileMap, 0);
else
pData = 0;
}
void FreeMemoryMap() {
}
};
#endif
#include "aggregations.h"
typedef int (*code_snippet)(void*);
typedef void (*module_init_fn)(Context*);
int test_main();
int n_recv = 0;
char** n_recvd = nullptr;
extern "C" void __DLLEXPORT__ receive_args(int argc, char**argv){
n_recv = argc;
n_recvd = argv;
}
enum BinaryInfo_t {
MSVC, MSYS, GCC, CLANG, AppleClang
};
extern "C" int __DLLEXPORT__ binary_info() {
#if defined(_MSC_VER) && !defined (__llvm__)
return MSVC;
#elif defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
return MSYS;
#elif defined(__clang__)
return CLANG;
#elif defined(__GNUC__)
return GCC;
#endif
}
__AQEXPORT__(bool) have_hge(){
#if defined(__MONETDB_CONN_H__)
return Server::havehge();
#else
return false;
#endif
}
Context::Context() {
current.memory_map = new std::unordered_map<void*, deallocator_t>;
init_session();
}
Context::~Context() {
auto memmap = (std::unordered_map<void*, deallocator_t>*) this->current.memory_map;
delete memmap;
}
void Context::init_session(){
if (log_level == LOG_INFO){
memset(&(this->current.stats), 0, sizeof(Session::Statistic));
}
auto memmap = (std::unordered_map<void*, deallocator_t>*) this->current.memory_map;
memmap->clear();
}
void Context::end_session(){
auto memmap = (std::unordered_map<void*, deallocator_t>*) this->current.memory_map;
for (auto& mem : *memmap) {
mem.second(mem.first);
}
memmap->clear();
}
void* Context::get_module_function(const char* fname){
auto fmap = static_cast<std::unordered_map<std::string, void*>*>
(this->module_function_maps);
printf("%p\n", fmap->find("mydiv")->second);
for (const auto& [key, value] : *fmap){
printf("%s %p\n", key.c_str(), value);
}
auto ret = fmap->find(fname);
return ret == fmap->end() ? nullptr : ret->second;
}
void initialize_module(const char* module_name, void* module_handle, Context* cxt){
auto _init_module = reinterpret_cast<module_init_fn>(dlsym(module_handle, "init_session"));
if (_init_module) {
_init_module(cxt);
}
else {
printf("Warning: module %s have no session support.\n", module_name);
}
}
int dll_main(int argc, char** argv, Context* cxt){
Config *cfg = reinterpret_cast<Config *>(argv[0]);
std::unordered_map<std::string, void*> user_module_map;
if (cxt->module_function_maps == 0)
cxt->module_function_maps = new std::unordered_map<std::string, void*>();
auto module_fn_map =
static_cast<std::unordered_map<std::string, void*>*>(cxt->module_function_maps);
auto buf_szs = cfg->buffer_sizes;
void** buffers = (void**)malloc(sizeof(void*) * cfg->n_buffers);
for (int i = 0; i < cfg->n_buffers; i++)
buffers[i] = static_cast<void *>(argv[i + 1]);
cxt->buffers = buffers;
cxt->cfg = cfg;
cxt->n_buffers = cfg->n_buffers;
cxt->sz_bufs = buf_szs;
cxt->alt_server = NULL;
while(cfg->running){
if (cfg->new_query) {
void *handle = 0;
void *user_module_handle = 0;
if (cfg->backend_type == BACKEND_MonetDB){
if (cxt->alt_server == 0)
cxt->alt_server = new Server(cxt);
Server* server = reinterpret_cast<Server*>(cxt->alt_server);
if(n_recv > 0){
if (cfg->backend_type == BACKEND_AQuery || cfg->has_dll) {
handle = dlopen("./dll.so", RTLD_LAZY);
}
for (const auto& module : user_module_map){
initialize_module(module.first.c_str(), module.second, cxt);
}
cxt->init_session();
for(int i = 0; i < n_recv; ++i)
{
//printf("%s, %d\n", n_recvd[i], n_recvd[i][0] == 'Q');
switch(n_recvd[i][0]){
case 'Q': // SQL query for monetdbe
{
server->exec(n_recvd[i] + 1);
printf("Exec Q%d: %s", i, n_recvd[i]);
}
break;
case 'P': // Postprocessing procedure
if(handle && !server->haserror()) {
code_snippet c = reinterpret_cast<code_snippet>(dlsym(handle, n_recvd[i]+1));
c(cxt);
}
break;
case 'M': // Load Module
{
auto mname = n_recvd[i] + 1;
user_module_handle = dlopen(mname, RTLD_LAZY);
//getlasterror
if (!user_module_handle)
#ifndef _MSC_VER
puts(dlerror());
#else
printf("Fatal Error: Module %s failed to load with error code %d.\n", mname, GetLastError());
#endif
user_module_map[mname] = user_module_handle;
initialize_module(mname, user_module_handle, cxt);
}
break;
case 'F': // Register Function in Module
{
auto fname = n_recvd[i] + 1;
printf("F:: %s: %p, %p\n", fname, user_module_handle, dlsym(user_module_handle, fname));
module_fn_map->insert_or_assign(fname, dlsym(user_module_handle, fname));
printf("F::: %p\n", module_fn_map->find("mydiv") != module_fn_map->end() ? module_fn_map->find("mydiv")->second : nullptr);
}
break;
case 'U': // Unload Module
{
auto mname = n_recvd[i] + 1;
auto it = user_module_map.find(mname);
if (user_module_handle == it->second)
user_module_handle = 0;
dlclose(it->second);
user_module_map.erase(it);
}
break;
}
}
if(handle) {
dlclose(handle);
handle = 0;
}
cxt->end_session();
n_recv = 0;
}
if(server->last_error == nullptr){
// TODO: Add feedback to prompt.
}
else{
server->last_error = nullptr;
continue;
}
}
// puts(cfg->has_dll ? "true" : "false");
if (cfg->backend_type == BACKEND_AQuery) {
handle = dlopen("./dll.so", RTLD_LAZY);
code_snippet c = reinterpret_cast<code_snippet>(dlsym(handle, "dllmain"));
c(cxt);
}
if (handle) dlclose(handle);
cfg->new_query = 0;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
return 0;
}
int launcher(int argc, char** argv){
#ifdef _WIN32
constexpr char sep = '\\';
#else
constexpr char sep = '/';
#endif
std::string str = " ";
std::string pwd = "";
if (argc > 0)
pwd = argv[0];
auto pos = pwd.find_last_of(sep);
if (pos == std::string::npos)
pos = 0;
pwd = pwd.substr(0, pos);
for (int i = 1; i < argc; i++){
str += argv[i];
str += " ";
}
str = std::string("cd ") + pwd + std::string("&& python3 ./prompt.py ") + str;
return system(str.c_str());
}
extern "C" int __DLLEXPORT__ main(int argc, char** argv) {
#ifdef __AQ_BUILD_LAUNCHER__
return launcher(argc, argv);
#endif
puts("running");
Context* cxt = new Context();
cxt->log("%d %s\n", argc, argv[1]);
#ifdef THREADING
auto tp = new ThreadPool();
cxt->thread_pool = tp;
#endif
const char* shmname;
if (argc < 0)
return dll_main(argc, argv, cxt);
else if (argc <= 1)
return test_main();
else
shmname = argv[1];
SharedMemory shm = SharedMemory(shmname);
if (!shm.pData)
return 1;
bool &running = static_cast<bool*>(shm.pData)[0],
&ready = static_cast<bool*>(shm.pData)[1];
using namespace std::chrono_literals;
cxt->log("running: %s\n", running? "true":"false");
cxt->log("ready: %s\n", ready? "true":"false");
while (running) {
std::this_thread::sleep_for(1ms);
if(ready){
cxt->log("running: %s\n", running? "true":"false");
cxt->log("ready: %s\n", ready? "true":"false");
void* handle = dlopen("./dll.so", RTLD_LAZY);
cxt->log("handle: %p\n", handle);
if (handle) {
cxt->log("inner\n");
code_snippet c = reinterpret_cast<code_snippet>(dlsym(handle, "dllmain"));
cxt->log("routine: %p\n", c);
if (c) {
cxt->log("inner\n");
cxt->err("return: %d\n", c(cxt));
}
}
ready = false;
}
}
shm.FreeMemoryMap();
return 0;
}
#include "utils.h"
#include "table_ext_monetdb.hpp"
int test_main()
{
Context* cxt = new Context();
if (cxt->alt_server == 0)
cxt->alt_server = new Server(cxt);
Server* server = reinterpret_cast<Server*>(cxt->alt_server);
//TableInfo<int, float> table("sibal");
//int col0[] = { 1,2,3,4,5 };
//float col1[] = { 5.f, 4.f, 3.f, 2.f, 1.f };
//table.get_col<0>().initfrom(5, col0, "a");
//table.get_col<1>().initfrom(5, col1, "b");
//table.monetdb_append_table(server);
//
//server->exec("select * from sibal;");
//auto aa = server->getCol(0);
//auto bb = server->getCol(1);
//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 ;",
};
n_recv = sizeof(qs)/(sizeof (char*));
n_recvd = const_cast<char**>(qs);
if (n_recv > 0) {
for (int i = 0; i < n_recv; ++i)
{
server->exec(n_recvd[i]);
printf("Exec Q%d: %s\n", i, n_recvd[i]);
}
n_recv = 0;
}
cxt->log_level = LOG_INFO;
puts(cpp_17 ?"true":"false");
void* handle = dlopen("./dll.so", RTLD_LAZY);
cxt->log("handle: %p\n", handle);
if (handle) {
cxt->log("inner\n");
code_snippet c = reinterpret_cast<code_snippet>(dlsym(handle, "dll_ZF5Shg"));
cxt->log("routine: %p\n", c);
if (c) {
cxt->log("inner\n");
cxt->log("return: %d\n", c(cxt));
}
dlclose(handle);
}
//static_assert(std::is_same_v<decltype(fill_integer_array<5, 1>()), std::integer_sequence<bool, 1,1,1,1,1>>, "");
return 0;
}
#include "pch.hpp"
#include "../csv.h"
#include <iostream>
#include <string>
#include <chrono>
#include "libaquery.h"
#include "monetdb_conn.h"
#ifdef THREADING
#include "threading.h"
#endif
#ifdef _WIN32
#include "winhelper.h"
#else
#include <dlfcn.h>
#include <fcntl.h>
#include <sys/mman.h>
struct SharedMemory
{
int hFileMap;
void* pData;
SharedMemory(const char* fname) {
hFileMap = open(fname, O_RDWR, 0);
if (hFileMap != -1)
pData = mmap(NULL, 8, PROT_READ | PROT_WRITE, MAP_SHARED, hFileMap, 0);
else
pData = 0;
}
void FreeMemoryMap() {
}
};
#endif
#include "aggregations.h"
typedef int (*code_snippet)(void*);
typedef void (*module_init_fn)(Context*);
int test_main();
int n_recv = 0;
char** n_recvd = nullptr;
extern "C" void __DLLEXPORT__ receive_args(int argc, char**argv){
n_recv = argc;
n_recvd = argv;
}
enum BinaryInfo_t {
MSVC, MSYS, GCC, CLANG, AppleClang
};
extern "C" int __DLLEXPORT__ binary_info() {
#if defined(_MSC_VER) && !defined (__llvm__)
return MSVC;
#elif defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
return MSYS;
#elif defined(__clang__)
return CLANG;
#elif defined(__GNUC__)
return GCC;
#endif
}
__AQEXPORT__(bool) have_hge(){
#if defined(__MONETDB_CONN_H__)
return Server::havehge();
#else
return false;
#endif
}
Context::Context() {
current.memory_map = new std::unordered_map<void*, deallocator_t>;
init_session();
}
Context::~Context() {
auto memmap = (std::unordered_map<void*, deallocator_t>*) this->current.memory_map;
delete memmap;
}
void Context::init_session(){
if (log_level == LOG_INFO){
memset(&(this->current.stats), 0, sizeof(Session::Statistic));
}
auto memmap = (std::unordered_map<void*, deallocator_t>*) this->current.memory_map;
memmap->clear();
}
void Context::end_session(){
auto memmap = (std::unordered_map<void*, deallocator_t>*) this->current.memory_map;
for (auto& mem : *memmap) {
mem.second(mem.first);
}
memmap->clear();
}
void* Context::get_module_function(const char* fname){
auto fmap = static_cast<std::unordered_map<std::string, void*>*>
(this->module_function_maps);
printf("%p\n", fmap->find("mydiv")->second);
for (const auto& [key, value] : *fmap){
printf("%s %p\n", key.c_str(), value);
}
auto ret = fmap->find(fname);
return ret == fmap->end() ? nullptr : ret->second;
}
void initialize_module(const char* module_name, void* module_handle, Context* cxt){
auto _init_module = reinterpret_cast<module_init_fn>(dlsym(module_handle, "init_session"));
if (_init_module) {
_init_module(cxt);
}
else {
printf("Warning: module %s have no session support.\n", module_name);
}
}
int dll_main(int argc, char** argv, Context* cxt){
Config *cfg = reinterpret_cast<Config *>(argv[0]);
std::unordered_map<std::string, void*> user_module_map;
if (cxt->module_function_maps == 0)
cxt->module_function_maps = new std::unordered_map<std::string, void*>();
auto module_fn_map =
static_cast<std::unordered_map<std::string, void*>*>(cxt->module_function_maps);
auto buf_szs = cfg->buffer_sizes;
void** buffers = (void**)malloc(sizeof(void*) * cfg->n_buffers);
for (int i = 0; i < cfg->n_buffers; i++)
buffers[i] = static_cast<void *>(argv[i + 1]);
cxt->buffers = buffers;
cxt->cfg = cfg;
cxt->n_buffers = cfg->n_buffers;
cxt->sz_bufs = buf_szs;
cxt->alt_server = NULL;
while(cfg->running){
if (cfg->new_query) {
void *handle = 0;
void *user_module_handle = 0;
if (cfg->backend_type == BACKEND_MonetDB){
if (cxt->alt_server == 0)
cxt->alt_server = new Server(cxt);
Server* server = reinterpret_cast<Server*>(cxt->alt_server);
if(n_recv > 0){
if (cfg->backend_type == BACKEND_AQuery || cfg->has_dll) {
handle = dlopen("./dll.so", RTLD_LAZY);
}
for (const auto& module : user_module_map){
initialize_module(module.first.c_str(), module.second, cxt);
}
cxt->init_session();
for(int i = 0; i < n_recv; ++i)
{
//printf("%s, %d\n", n_recvd[i], n_recvd[i][0] == 'Q');
switch(n_recvd[i][0]){
case 'Q': // SQL query for monetdbe
{
server->exec(n_recvd[i] + 1);
printf("Exec Q%d: %s", i, n_recvd[i]);
}
break;
case 'P': // Postprocessing procedure
if(handle && !server->haserror()) {
code_snippet c = reinterpret_cast<code_snippet>(dlsym(handle, n_recvd[i]+1));
c(cxt);
}
break;
case 'M': // Load Module
{
auto mname = n_recvd[i] + 1;
user_module_handle = dlopen(mname, RTLD_LAZY);
//getlasterror
if (!user_module_handle)
#ifndef _MSC_VER
puts(dlerror());
#else
printf("Fatal Error: Module %s failed to load with error code %d.\n", mname, GetLastError());
#endif
user_module_map[mname] = user_module_handle;
initialize_module(mname, user_module_handle, cxt);
}
break;
case 'F': // Register Function in Module
{
auto fname = n_recvd[i] + 1;
printf("F:: %s: %p, %p\n", fname, user_module_handle, dlsym(user_module_handle, fname));
module_fn_map->insert_or_assign(fname, dlsym(user_module_handle, fname));
printf("F::: %p\n", module_fn_map->find("mydiv") != module_fn_map->end() ? module_fn_map->find("mydiv")->second : nullptr);
}
break;
case 'U': // Unload Module
{
auto mname = n_recvd[i] + 1;
auto it = user_module_map.find(mname);
if (user_module_handle == it->second)
user_module_handle = 0;
dlclose(it->second);
user_module_map.erase(it);
}
break;
}
}
if(handle) {
dlclose(handle);
handle = 0;
}
cxt->end_session();
n_recv = 0;
}
if(server->last_error == nullptr){
// TODO: Add feedback to prompt.
}
else{
server->last_error = nullptr;
continue;
}
}
// puts(cfg->has_dll ? "true" : "false");
if (cfg->backend_type == BACKEND_AQuery) {
handle = dlopen("./dll.so", RTLD_LAZY);
code_snippet c = reinterpret_cast<code_snippet>(dlsym(handle, "dllmain"));
c(cxt);
}
if (handle) dlclose(handle);
cfg->new_query = 0;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
return 0;
}
int launcher(int argc, char** argv){
#ifdef _WIN32
constexpr char sep = '\\';
#else
constexpr char sep = '/';
#endif
std::string str = " ";
std::string pwd = "";
if (argc > 0)
pwd = argv[0];
auto pos = pwd.find_last_of(sep);
if (pos == std::string::npos)
pos = 0;
pwd = pwd.substr(0, pos);
for (int i = 1; i < argc; i++){
str += argv[i];
str += " ";
}
str = std::string("cd ") + pwd + std::string("&& python3 ./prompt.py ") + str;
return system(str.c_str());
}
extern "C" int __DLLEXPORT__ main(int argc, char** argv) {
#ifdef __AQ_BUILD_LAUNCHER__
return launcher(argc, argv);
#endif
puts("running");
Context* cxt = new Context();
cxt->log("%d %s\n", argc, argv[1]);
#ifdef THREADING
auto tp = new ThreadPool();
cxt->thread_pool = tp;
#endif
const char* shmname;
if (argc < 0)
return dll_main(argc, argv, cxt);
else if (argc <= 1)
return test_main();
else
shmname = argv[1];
SharedMemory shm = SharedMemory(shmname);
if (!shm.pData)
return 1;
bool &running = static_cast<bool*>(shm.pData)[0],
&ready = static_cast<bool*>(shm.pData)[1];
using namespace std::chrono_literals;
cxt->log("running: %s\n", running? "true":"false");
cxt->log("ready: %s\n", ready? "true":"false");
while (running) {
std::this_thread::sleep_for(1ms);
if(ready){
cxt->log("running: %s\n", running? "true":"false");
cxt->log("ready: %s\n", ready? "true":"false");
void* handle = dlopen("./dll.so", RTLD_LAZY);
cxt->log("handle: %p\n", handle);
if (handle) {
cxt->log("inner\n");
code_snippet c = reinterpret_cast<code_snippet>(dlsym(handle, "dllmain"));
cxt->log("routine: %p\n", c);
if (c) {
cxt->log("inner\n");
cxt->err("return: %d\n", c(cxt));
}
}
ready = false;
}
}
shm.FreeMemoryMap();
return 0;
}
#include "utils.h"
#include "table_ext_monetdb.hpp"
int test_main()
{
Context* cxt = new Context();
if (cxt->alt_server == 0)
cxt->alt_server = new Server(cxt);
Server* server = reinterpret_cast<Server*>(cxt->alt_server);
//TableInfo<int, float> table("sibal");
//int col0[] = { 1,2,3,4,5 };
//float col1[] = { 5.f, 4.f, 3.f, 2.f, 1.f };
//table.get_col<0>().initfrom(5, col0, "a");
//table.get_col<1>().initfrom(5, col1, "b");
//table.monetdb_append_table(server);
//
//server->exec("select * from sibal;");
//auto aa = server->getCol(0);
//auto bb = server->getCol(1);
//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 ;",
};
n_recv = sizeof(qs)/(sizeof (char*));
n_recvd = const_cast<char**>(qs);
if (n_recv > 0) {
for (int i = 0; i < n_recv; ++i)
{
server->exec(n_recvd[i]);
printf("Exec Q%d: %s\n", i, n_recvd[i]);
}
n_recv = 0;
}
cxt->log_level = LOG_INFO;
puts(cpp_17 ?"true":"false");
void* handle = dlopen("./dll.so", RTLD_LAZY);
cxt->log("handle: %p\n", handle);
if (handle) {
cxt->log("inner\n");
code_snippet c = reinterpret_cast<code_snippet>(dlsym(handle, "dll_ZF5Shg"));
cxt->log("routine: %p\n", c);
if (c) {
cxt->log("inner\n");
cxt->log("return: %d\n", c(cxt));
}
dlclose(handle);
}
//static_assert(std::is_same_v<decltype(fill_integer_array<5, 1>()), std::integer_sequence<bool, 1,1,1,1,1>>, "");
return 0;
}
+631 -631
View File
File diff suppressed because it is too large Load Diff
+79 -79
View File
@@ -1,79 +1,79 @@
// TODO: Think of a way of decoupling table.h part and
// monetdbe.h part to speed up compilation.
#ifndef __TABLE_EXT_MONETDB_HPP__
#define __TABLE_EXT_MONETDB_HPP__
#include "table.h"
#include "monetdb_conn.h"
#include "monetdbe.h"
inline constexpr monetdbe_types AQType_2_monetdbe[] = {
monetdbe_int32_t, monetdbe_float, monetdbe_str, monetdbe_double, monetdbe_int64_t,
#ifdef HAVE_HGE
monetdbe_int128_t,
#else
monetdbe_int64_t,
#endif
monetdbe_int16_t, monetdbe_date, monetdbe_time, monetdbe_int8_t,
monetdbe_int32_t, monetdbe_int64_t,
#ifdef HAVE_HGE
monetdbe_int128_t,
#else
monetdbe_int64_t,
#endif
monetdbe_int16_t, monetdbe_int8_t, monetdbe_bool, monetdbe_int64_t, monetdbe_int64_t, monetdbe_int64_t
};
template<class ...Ts>
void TableInfo<Ts ...>::monetdb_append_table(void* srv, const char* alt_name) {
if (!alt_name){
alt_name = this->name;
}
monetdbe_column** monetdbe_cols = new monetdbe_column * [sizeof...(Ts)];
uint32_t i = 0;
const auto get_col = [&monetdbe_cols, &i](auto v) {
monetdbe_cols[i++] = (monetdbe_column*)v->monetdb_get_col();
};
(get_col((ColRef<Ts>*)(colrefs + i)), ...);
std::string create_table_str = "CREATE TABLE ";
create_table_str += alt_name;
create_table_str += " (";
i = 0;
const auto get_name_type = [&i, *this]() {
return std::string(colrefs[i++].name) + ' ';
};
create_table_str += ((get_name_type() + types::SQL_Type[types::Types<Ts>::getType()] + ", ") + ... + std::string(""));
auto last_comma = create_table_str.find_last_of(',');
if (last_comma != static_cast<decltype(last_comma)>(-1)) {
create_table_str[last_comma] = ')';
Server* server = (Server*)srv;
server->exec(create_table_str.c_str());
if (!server->last_error) {
auto err = monetdbe_append(*((monetdbe_database*)server->server), "sys", alt_name, monetdbe_cols, sizeof...(Ts));
if (err)
puts(err);
return;
}
}
puts("Error! Empty table.");
}
template<class Type>
void* ColRef<Type>::monetdb_get_col() {
auto aq_type = AQType_2_monetdbe[types::Types<Type>::getType()];
monetdbe_column* col = (monetdbe_column*)malloc(sizeof(monetdbe_column));
col->type = aq_type;
col->count = this->size;
col->data = this->container;
col->name = const_cast<char*>(this->name);
return col;
}
#endif
// TODO: Think of a way of decoupling table.h part and
// monetdbe.h part to speed up compilation.
#ifndef __TABLE_EXT_MONETDB_HPP__
#define __TABLE_EXT_MONETDB_HPP__
#include "table.h"
#include "monetdb_conn.h"
#include "monetdbe.h"
inline constexpr monetdbe_types AQType_2_monetdbe[] = {
monetdbe_int32_t, monetdbe_float, monetdbe_str, monetdbe_double, monetdbe_int64_t,
#ifdef HAVE_HGE
monetdbe_int128_t,
#else
monetdbe_int64_t,
#endif
monetdbe_int16_t, monetdbe_date, monetdbe_time, monetdbe_int8_t,
monetdbe_int32_t, monetdbe_int64_t,
#ifdef HAVE_HGE
monetdbe_int128_t,
#else
monetdbe_int64_t,
#endif
monetdbe_int16_t, monetdbe_int8_t, monetdbe_bool, monetdbe_int64_t, monetdbe_int64_t, monetdbe_int64_t
};
template<class ...Ts>
void TableInfo<Ts ...>::monetdb_append_table(void* srv, const char* alt_name) {
if (!alt_name){
alt_name = this->name;
}
monetdbe_column** monetdbe_cols = new monetdbe_column * [sizeof...(Ts)];
uint32_t i = 0;
const auto get_col = [&monetdbe_cols, &i](auto v) {
monetdbe_cols[i++] = (monetdbe_column*)v->monetdb_get_col();
};
(get_col((ColRef<Ts>*)(colrefs + i)), ...);
std::string create_table_str = "CREATE TABLE ";
create_table_str += alt_name;
create_table_str += " (";
i = 0;
const auto get_name_type = [&i, *this]() {
return std::string(colrefs[i++].name) + ' ';
};
create_table_str += ((get_name_type() + types::SQL_Type[types::Types<Ts>::getType()] + ", ") + ... + std::string(""));
auto last_comma = create_table_str.find_last_of(',');
if (last_comma != static_cast<decltype(last_comma)>(-1)) {
create_table_str[last_comma] = ')';
Server* server = (Server*)srv;
server->exec(create_table_str.c_str());
if (!server->last_error) {
auto err = monetdbe_append(*((monetdbe_database*)server->server), "sys", alt_name, monetdbe_cols, sizeof...(Ts));
if (err)
puts(err);
return;
}
}
puts("Error! Empty table.");
}
template<class Type>
void* ColRef<Type>::monetdb_get_col() {
auto aq_type = AQType_2_monetdbe[types::Types<Type>::getType()];
monetdbe_column* col = (monetdbe_column*)malloc(sizeof(monetdbe_column));
col->type = aq_type;
col->count = this->size;
col->data = this->container;
col->name = const_cast<char*>(this->name);
return col;
}
#endif
+237 -237
View File
@@ -1,237 +1,237 @@
#ifndef _TYPES_H
#define _TYPES_H
#include <cstdint>
#include <type_traits>
#include <string>
#include <tuple>
#ifdef _MSC_VER
#define __restrict__ __restrict
#endif
template <class T>
constexpr static inline bool is_vector(const T&) {
return false;
}
template <class T>
struct is_vector_impl : std::false_type {};
template <class T>
constexpr static bool is_vector_type = is_vector_impl<T>::value;
namespace types {
enum Type_t {
AINT32, AFLOAT, ASTR, ADOUBLE, ALDOUBLE, AINT64, AINT128, AINT16, ADATE, ATIME, AINT8,
AUINT32, AUINT64, AUINT128, AUINT16, AUINT8, ABOOL, VECTOR, NONE, ERROR
};
static constexpr const char* printf_str[] = { "%d", "%f", "%s", "%lf", "%Lf", "%ld", "%d", "%hi", "%s", "%s", "%c",
"%u", "%lu", "%s", "%hu", "%hhu", "%s", "%s", "Vector<%s>", "NULL", "ERROR" };
static constexpr const char* SQL_Type[] = { "INT", "REAL", "TEXT", "DOUBLE", "DOUBLE", "BIGINT", "HUGEINT", "SMALLINT", "DATE", "TIME", "TINYINT",
"INT", "BIGINT", "HUGEINT", "SMALLINT", "TINYINT", "BIGINT", "BOOL", "BIGINT", "NULL", "ERROR"};
// TODO: deal with data/time <=> str/uint conversion
struct date_t {
uint32_t val = 0;
date_t(const char* d) {
}
std::string toString() const;
};
struct time_t {
uint32_t val = 0;
time_t(const char* d) {
}
std::string toString() const;
};
template <typename T>
struct Types {
typedef T type;
constexpr Types() noexcept = default;
#ifdef __SIZEOF_INT128__
#define F_INT128(__F_) __F_(__int128_t, AINT128) \
__F_(__uint128_t, AUINT128)
#define ULL_Type __uint128_t
#define LL_Type __int128_t
#else
#define F_INT128(__F_)
#define ULL_Type unsigned long long
#define LL_Type long long
#endif
#define ConnectTypes(f) \
f(int, AINT32) \
f(float, AFLOAT) \
f(const char*, ASTR) \
f(double, ADOUBLE) \
f(long double, ALDOUBLE) \
f(long, AINT64) \
f(short, AINT16) \
f(date_t, ADATE) \
f(time_t, ATIME) \
f(unsigned char, AINT8) \
f(unsigned int, AUINT32) \
f(unsigned long, AUINT64) \
f(unsigned short, AUINT16) \
f(unsigned char, AUINT8) \
f(bool, ABOOL) \
F_INT128(f)
inline constexpr static Type_t getType() {
#define TypeConnect(x, y) if constexpr(std::is_same<x, T>::value) return y; else
ConnectTypes(TypeConnect)
if constexpr (is_vector_type<T>)
return VECTOR;
else
return NONE;
}
};
#define ATypeSize(t, at) sizeof(t),
static constexpr size_t AType_sizes[] = { ConnectTypes(ATypeSize) 1 };
#define Cond(c, x, y) typename std::conditional<c, x, y>::type
#define Comp(o) (sizeof(T1) o sizeof(T2))
#define Same(x, y) (std::is_same_v<x, y>)
#define __U(x) std::is_unsigned<x>::value
#define Fp(x) std::is_floating_point<x>::value
template <class T1, class T2>
struct Coercion {
using t1 = Cond(Comp(<= ), Cond(Comp(== ), Cond(Fp(T1), T1, Cond(Fp(T2), T2, Cond(__U(T1), T2, T1))), T2), T1);
using t2 = Cond(Same(T1, T2), T1, Cond(Same(T1, const char*) || Same(T2, const char*), const char*, void));
using type = Cond(Same(t2, void), Cond(Same(T1, date_t) && Same(T2, time_t) || Same(T1, time_t) && Same(T2, time_t), void, t1), t2);
};
#define __Eq(x) (sizeof(T) == sizeof(x))
template<class T>
struct GetFPTypeImpl {
using type = Cond(__Eq(float), float, Cond(__Eq(double), double, double));
};
template<class T>
using GetFPType = typename GetFPTypeImpl<typename std::decay<T>::type>::type;
template<class T>
struct GetLongTypeImpl {
using type = Cond(__U(T), ULL_Type, Cond(Fp(T), double, LL_Type));
};
template<class T>
using GetLongType = typename GetLongTypeImpl<typename std::decay<T>::type>::type;
}
#define getT(i, t) std::tuple_element_t<i, std::tuple<t...>>
template <template<typename ...> class T, typename ...Types>
struct applyTemplates {
using type = T<Types...>;
};
template <class lT, template <typename ...> class rT>
struct transTypes_s;
template <template<typename ...> class lT, typename ...T, template<typename ...> class rT>
struct transTypes_s<lT<T...>, rT> {
using type = rT<T...>;
};
// static_assert(std::is_same<transTypes<std::tuple<int, float>, std::unordered_map>, std::unordered_map<int, float>>::value);
template <class lT, template <typename ...> class rT>
using transTypes = typename transTypes_s<lT, rT>::type;
template <class ...Types>
struct record_types {};
template <class ...Types>
using record = std::tuple<Types...>;
template <class T>
struct decayS {
using type = typename std::decay<T>::type;
};
template<template<typename ...> class T, typename ...Types>
struct decayS <T<Types...>>{
using type = T<typename std::decay<Types>::type ...>;
};
template <class T>
using decays = typename decayS<typename std::decay<T>::type>::type;
template <class T>
using decay_inner = typename decayS<T>::type;
template <class, template <class...> class T>
struct instance_of_impl : std::false_type {};
template <class ...T1, template <class ...> class T2>
struct instance_of_impl<T2<T1...>, T2> : std::true_type {};
template <class T1, class T2>
struct same_class_impl : std::false_type {};
template <class ...T1s, class ...T2s, template <class...> class T1>
struct same_class_impl<T1<T1s...>, T1<T2s...>> : std::true_type {};
template <class T1, class T2>
bool same_class = same_class_impl<T1, T2>::value;
template <class T1, template <class...> class T2>
bool instance_of = instance_of_impl<T1, T2>::value;
template <class lT, template <typename ...> class rT>
using transTypes = typename transTypes_s<lT, rT>::type;
template <class lT, class vT, template <vT ...> class rT>
struct transValues_s;
template <class vT, template<class, vT ...> class lT, vT ...T, template<vT ...> class rT>
struct transValues_s<lT<vT, T...>, vT, rT> {
using type = rT<T...>;
};
#include <utility>
template <class vT, int i, template <vT ...> class rT>
using transValues = typename transValues_s<std::make_integer_sequence<vT, i>, vT, rT>::type;
template <int i, template <int ...> class rT>
using applyIntegerSequence = typename transValues_s<std::make_integer_sequence<int, i>, int, rT>::type;
template <template <class ...> class T, class ...Types>
struct decayed_impl{ typedef T<Types...> type;};
template <template <typename ...> class VT, class ...Types>
using decayed_t = typename decayed_impl<VT, Types...>::type;
template <class First = void, class...Rest>
struct get_first_impl {
typedef First first;
constexpr static size_t rest_len = sizeof...(Rest);
typedef get_first_impl<Rest...> rest;
};
template <class ...T>
using get_first = typename get_first_impl<T...>::first;
template <class T>
struct value_type_impl { typedef T type; };
template <template <class...> class VT, class ...V>
struct value_type_impl<VT<V...>> { typedef get_first<V...> type; };
template <class T>
using value_type = typename value_type_impl<T>::type;
template <class ...T>
using get_first = typename get_first_impl<T...>::first;
template <class T>
struct value_type_rec_impl { typedef T type; };
template <template <class...> class VT, class ...V>
struct value_type_rec_impl<VT<V...>> { typedef typename value_type_rec_impl<get_first<int>>::type type; };
template <class T>
using value_type_r = typename value_type_rec_impl<T>::type;
template <class T>
struct nullval_impl { constexpr static T value = 0; };
template <class T>
constexpr static T nullval = nullval_impl<T>::value;
#include <limits>
template <>
struct nullval_impl<int> { constexpr static int value = std::numeric_limits<int>::min(); };
template<>
struct nullval_impl<float> { constexpr static float value = -std::numeric_limits<float>::quiet_NaN(); };
template<>
struct nullval_impl<double> { constexpr static double value = -std::numeric_limits<double>::quiet_NaN(); };
constexpr size_t sum_type(size_t a[], size_t sz) {
size_t ret = 0;
for (int i = 0; i < sz; ++i)
ret += a[i];
return ret;
}
template<class Types, class ...T1>
constexpr size_t sum_type() {
size_t t[] = {std::is_same_v<Types, T1> ...};
return sum_type(t, sizeof...(T1));
}
template<class ...T1, class ...Types>
constexpr size_t count_type(std::tuple<Types...>* ts) {
size_t t[] = {sum_type<Types, T1...>() ...};
return sum_type(t, sizeof...(Types));
}
#endif // !_TYPES_H
#ifndef _TYPES_H
#define _TYPES_H
#include <cstdint>
#include <type_traits>
#include <string>
#include <tuple>
#ifdef _MSC_VER
#define __restrict__ __restrict
#endif
template <class T>
constexpr static inline bool is_vector(const T&) {
return false;
}
template <class T>
struct is_vector_impl : std::false_type {};
template <class T>
constexpr static bool is_vector_type = is_vector_impl<T>::value;
namespace types {
enum Type_t {
AINT32, AFLOAT, ASTR, ADOUBLE, ALDOUBLE, AINT64, AINT128, AINT16, ADATE, ATIME, AINT8,
AUINT32, AUINT64, AUINT128, AUINT16, AUINT8, ABOOL, VECTOR, NONE, ERROR
};
static constexpr const char* printf_str[] = { "%d", "%f", "%s", "%lf", "%Lf", "%ld", "%d", "%hi", "%s", "%s", "%c",
"%u", "%lu", "%s", "%hu", "%hhu", "%s", "%s", "Vector<%s>", "NULL", "ERROR" };
static constexpr const char* SQL_Type[] = { "INT", "REAL", "TEXT", "DOUBLE", "DOUBLE", "BIGINT", "HUGEINT", "SMALLINT", "DATE", "TIME", "TINYINT",
"INT", "BIGINT", "HUGEINT", "SMALLINT", "TINYINT", "BIGINT", "BOOL", "BIGINT", "NULL", "ERROR"};
// TODO: deal with data/time <=> str/uint conversion
struct date_t {
uint32_t val = 0;
date_t(const char* d) {
}
std::string toString() const;
};
struct time_t {
uint32_t val = 0;
time_t(const char* d) {
}
std::string toString() const;
};
template <typename T>
struct Types {
typedef T type;
constexpr Types() noexcept = default;
#ifdef __SIZEOF_INT128__
#define F_INT128(__F_) __F_(__int128_t, AINT128) \
__F_(__uint128_t, AUINT128)
#define ULL_Type __uint128_t
#define LL_Type __int128_t
#else
#define F_INT128(__F_)
#define ULL_Type unsigned long long
#define LL_Type long long
#endif
#define ConnectTypes(f) \
f(int, AINT32) \
f(float, AFLOAT) \
f(const char*, ASTR) \
f(double, ADOUBLE) \
f(long double, ALDOUBLE) \
f(long, AINT64) \
f(short, AINT16) \
f(date_t, ADATE) \
f(time_t, ATIME) \
f(unsigned char, AINT8) \
f(unsigned int, AUINT32) \
f(unsigned long, AUINT64) \
f(unsigned short, AUINT16) \
f(unsigned char, AUINT8) \
f(bool, ABOOL) \
F_INT128(f)
inline constexpr static Type_t getType() {
#define TypeConnect(x, y) if constexpr(std::is_same<x, T>::value) return y; else
ConnectTypes(TypeConnect)
if constexpr (is_vector_type<T>)
return VECTOR;
else
return NONE;
}
};
#define ATypeSize(t, at) sizeof(t),
static constexpr size_t AType_sizes[] = { ConnectTypes(ATypeSize) 1 };
#define Cond(c, x, y) typename std::conditional<c, x, y>::type
#define Comp(o) (sizeof(T1) o sizeof(T2))
#define Same(x, y) (std::is_same_v<x, y>)
#define __U(x) std::is_unsigned<x>::value
#define Fp(x) std::is_floating_point<x>::value
template <class T1, class T2>
struct Coercion {
using t1 = Cond(Comp(<= ), Cond(Comp(== ), Cond(Fp(T1), T1, Cond(Fp(T2), T2, Cond(__U(T1), T2, T1))), T2), T1);
using t2 = Cond(Same(T1, T2), T1, Cond(Same(T1, const char*) || Same(T2, const char*), const char*, void));
using type = Cond(Same(t2, void), Cond(Same(T1, date_t) && Same(T2, time_t) || Same(T1, time_t) && Same(T2, time_t), void, t1), t2);
};
#define __Eq(x) (sizeof(T) == sizeof(x))
template<class T>
struct GetFPTypeImpl {
using type = Cond(__Eq(float), float, Cond(__Eq(double), double, double));
};
template<class T>
using GetFPType = typename GetFPTypeImpl<typename std::decay<T>::type>::type;
template<class T>
struct GetLongTypeImpl {
using type = Cond(__U(T), ULL_Type, Cond(Fp(T), double, LL_Type));
};
template<class T>
using GetLongType = typename GetLongTypeImpl<typename std::decay<T>::type>::type;
}
#define getT(i, t) std::tuple_element_t<i, std::tuple<t...>>
template <template<typename ...> class T, typename ...Types>
struct applyTemplates {
using type = T<Types...>;
};
template <class lT, template <typename ...> class rT>
struct transTypes_s;
template <template<typename ...> class lT, typename ...T, template<typename ...> class rT>
struct transTypes_s<lT<T...>, rT> {
using type = rT<T...>;
};
// static_assert(std::is_same<transTypes<std::tuple<int, float>, std::unordered_map>, std::unordered_map<int, float>>::value);
template <class lT, template <typename ...> class rT>
using transTypes = typename transTypes_s<lT, rT>::type;
template <class ...Types>
struct record_types {};
template <class ...Types>
using record = std::tuple<Types...>;
template <class T>
struct decayS {
using type = typename std::decay<T>::type;
};
template<template<typename ...> class T, typename ...Types>
struct decayS <T<Types...>>{
using type = T<typename std::decay<Types>::type ...>;
};
template <class T>
using decays = typename decayS<typename std::decay<T>::type>::type;
template <class T>
using decay_inner = typename decayS<T>::type;
template <class, template <class...> class T>
struct instance_of_impl : std::false_type {};
template <class ...T1, template <class ...> class T2>
struct instance_of_impl<T2<T1...>, T2> : std::true_type {};
template <class T1, class T2>
struct same_class_impl : std::false_type {};
template <class ...T1s, class ...T2s, template <class...> class T1>
struct same_class_impl<T1<T1s...>, T1<T2s...>> : std::true_type {};
template <class T1, class T2>
bool same_class = same_class_impl<T1, T2>::value;
template <class T1, template <class...> class T2>
bool instance_of = instance_of_impl<T1, T2>::value;
template <class lT, template <typename ...> class rT>
using transTypes = typename transTypes_s<lT, rT>::type;
template <class lT, class vT, template <vT ...> class rT>
struct transValues_s;
template <class vT, template<class, vT ...> class lT, vT ...T, template<vT ...> class rT>
struct transValues_s<lT<vT, T...>, vT, rT> {
using type = rT<T...>;
};
#include <utility>
template <class vT, int i, template <vT ...> class rT>
using transValues = typename transValues_s<std::make_integer_sequence<vT, i>, vT, rT>::type;
template <int i, template <int ...> class rT>
using applyIntegerSequence = typename transValues_s<std::make_integer_sequence<int, i>, int, rT>::type;
template <template <class ...> class T, class ...Types>
struct decayed_impl{ typedef T<Types...> type;};
template <template <typename ...> class VT, class ...Types>
using decayed_t = typename decayed_impl<VT, Types...>::type;
template <class First = void, class...Rest>
struct get_first_impl {
typedef First first;
constexpr static size_t rest_len = sizeof...(Rest);
typedef get_first_impl<Rest...> rest;
};
template <class ...T>
using get_first = typename get_first_impl<T...>::first;
template <class T>
struct value_type_impl { typedef T type; };
template <template <class...> class VT, class ...V>
struct value_type_impl<VT<V...>> { typedef get_first<V...> type; };
template <class T>
using value_type = typename value_type_impl<T>::type;
template <class ...T>
using get_first = typename get_first_impl<T...>::first;
template <class T>
struct value_type_rec_impl { typedef T type; };
template <template <class...> class VT, class ...V>
struct value_type_rec_impl<VT<V...>> { typedef typename value_type_rec_impl<get_first<int>>::type type; };
template <class T>
using value_type_r = typename value_type_rec_impl<T>::type;
template <class T>
struct nullval_impl { constexpr static T value = 0; };
template <class T>
constexpr static T nullval = nullval_impl<T>::value;
#include <limits>
template <>
struct nullval_impl<int> { constexpr static int value = std::numeric_limits<int>::min(); };
template<>
struct nullval_impl<float> { constexpr static float value = -std::numeric_limits<float>::quiet_NaN(); };
template<>
struct nullval_impl<double> { constexpr static double value = -std::numeric_limits<double>::quiet_NaN(); };
constexpr size_t sum_type(size_t a[], size_t sz) {
size_t ret = 0;
for (int i = 0; i < sz; ++i)
ret += a[i];
return ret;
}
template<class Types, class ...T1>
constexpr size_t sum_type() {
size_t t[] = {std::is_same_v<Types, T1> ...};
return sum_type(t, sizeof...(T1));
}
template<class ...T1, class ...Types>
constexpr size_t count_type(std::tuple<Types...>* ts) {
size_t t[] = {sum_type<Types, T1...>() ...};
return sum_type(t, sizeof...(Types));
}
#endif // !_TYPES_H
+15 -15
View File
@@ -1,16 +1,16 @@
#pragma once
#include <string>
#include <ctime>
#if ((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || __cplusplus >= 201703L)
constexpr static bool cpp_17 = true;
#else
constexpr static bool cpp_17 = false;
#endif
template <class T>
inline const char* str(const T& v) {
return "";
}
template <>
inline const char* str(const bool& v) {
return v ? "true" : "false";
#pragma once
#include <string>
#include <ctime>
#if ((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || __cplusplus >= 201703L)
constexpr static bool cpp_17 = true;
#else
constexpr static bool cpp_17 = false;
#endif
template <class T>
inline const char* str(const T& v) {
return "";
}
template <>
inline const char* str(const bool& v) {
return v ? "true" : "false";
}
+325 -325
View File
@@ -1,325 +1,325 @@
/*
* Bill Sun 2022
*/
#ifndef _VECTOR_TYPE
#define _VECTOR_TYPE
#include <cstring>
#include <cstdlib>
#include <cstdint>
#include <initializer_list>
#include "types.h"
#pragma pack(push, 1)
template <typename _Ty>
class vector_type {
public:
typedef vector_type<_Ty> Decayed_t;
void inline _copy(const vector_type<_Ty>& vt) {
// quick init while using malloc
//if (capacity > 0) free(container);
this->size = vt.size;
this->capacity = vt.capacity;
if (capacity) {
// puts("copy");
this->container = (_Ty*)malloc(size * sizeof(_Ty));
memcpy(container, vt.container, sizeof(_Ty) * size);
}
else {
this->container = vt.container;
}
}
void inline _move(vector_type<_Ty>&& vt) {
if (capacity > 0) free(container);
this->size = vt.size;
this->capacity = vt.capacity;
this->container = vt.container;
// puts("move");
vt.size = vt.capacity = 0;
vt.container = 0;
}
public:
_Ty* container;
uint32_t size, capacity;
typedef _Ty* iterator_t;
typedef _Ty value_t;
vector_type(const uint32_t& size) : size(size), capacity(size) {
container = (_Ty*)malloc(size * sizeof(_Ty));
}
constexpr vector_type(std::initializer_list<_Ty> _l) {
size = capacity = _l.size();
_Ty* _container = this->container = (_Ty*)malloc(sizeof(_Ty) * _l.size());
for (const auto& l : _l) {
*(_container++) = l;
}
}
constexpr vector_type() noexcept : size(0), capacity(0), container(0) {};
constexpr vector_type(_Ty* container, uint32_t len) noexcept : size(len), capacity(0), container(container) {};
constexpr explicit vector_type(const vector_type<_Ty>& vt) noexcept : capacity(0) {
_copy(vt);
}
constexpr vector_type(vector_type<_Ty>&& vt) noexcept : capacity(0) {
_move(std::move(vt));
}
// size >= capacity ==> readonly vector
constexpr vector_type(const uint32_t size, void* data) :
size(size), capacity(0), container(static_cast<_Ty*>(data)) {}
vector_type<_Ty>& operator =(const _Ty& vt) {
if (!container) {
container = (_Ty*)malloc(sizeof(_Ty));
capacity = 1;
}
size = 1;
container[0] = vt;
return *this;
}
// template<template <typename ...> class VT, class T>
// vector_type<_Ty>& operator =(VT<T>&& vt) {
// this->container = (_Ty*)vt.container;
// this->size = vt.size;
// this->capacity = vt.capacity;
// vt.capacity = 0; // rvalue's
// return *this;
// }
vector_type<_Ty>& operator =(const vector_type<_Ty>& vt) {
_copy(vt);
return *this;
}
vector_type<_Ty>& operator =(vector_type<_Ty>&& vt) {
_move(std::move(vt));
return *this;
}
template <template <class> class VT>
vector_type<_Ty>& operator =(const VT<_Ty>& vt) {
if (capacity > 0) free(container);
container = static_cast<_Ty*>(malloc(vt.size * sizeof(_Ty)));
size = vt.size;
capacity = size;
for(uint32_t i = 0; i < size; ++i)
container[i] = vt[i];
return *this;
}
inline void grow() {
if (size >= capacity) { // geometric growth
uint32_t new_capacity = size + 1 + (size >> 1);
_Ty* n_container = (_Ty*)malloc(new_capacity * sizeof(_Ty));
memcpy(n_container, container, sizeof(_Ty) * size);
memset(n_container + size, 0, sizeof(_Ty) * (new_capacity - size));
if (capacity)
free(container);
container = n_container;
capacity = new_capacity;
}
}
void emplace_back(const _Ty& _val) {
grow();
container[size++] = _val;
}
void emplace_back(_Ty&& _val) {
grow();
container[size++] = std::move(_val);
}
iterator_t erase(iterator_t _it) {
#ifdef DEBUG
// Do bound checks
if (!(size && capicity && container &&
_it >= container && (_it - container) < size))
return 0;
#endif
iterator_t curr = _it + 1, end = _it + --size;
while (curr < end)
*(curr - 1) = *(curr++);
return _it;
}
iterator_t begin() const {
return container;
}
iterator_t end() const {
return container + size;
}
iterator_t find(const _Ty item) const {
iterator_t curr = begin(), _end = end();
while (curr != _end && *(curr++) != item);
return curr;
}
inline _Ty& operator[](const uint32_t _i) const {
return container[_i];
}
void shrink_to_fit() {
if (size && capacity != size) {
capacity = size;
_Ty* _container = (_Ty*)malloc(sizeof(_Ty) * size);
memcpy(_container, container, sizeof(_Ty) * size);
free(container);
container = _container;
}
}
_Ty& back() {
return container[size - 1];
}
void qpop() {
size = size ? size - 1 : size;
}
void pop_resize() {
if (size) {
--size;
if (capacity > (size << 1))
{
_Ty* new_container = (_Ty*)malloc(sizeof(_Ty) * size);
memcpy(new_container, container, sizeof(_Ty) * size);
free(container);
container = new_container;
capacity = size;
}
}
}
_Ty pop() {
return container[--size];
}
void merge(vector_type<_Ty>& _other) {
if (capacity < this->size + _other.size)
{
_Ty* new_container = (_Ty*)malloc(sizeof(_Ty) * (_other.size + this->size));
capacity = this->size + _other.size;
memcpy(new_container, container, sizeof(_Ty) * this->size);
memcpy(new_container + this->size, _other.container, sizeof(_Ty) * _other.size);
free(container);
container = new_container;
}
else
memcpy(container + this->size, _other.container, sizeof(_Ty) * _other.size);
size = this->size + _other.size;
}
template<class _Iter>
void merge(_Iter begin, _Iter end) {
unsigned int dist = static_cast<unsigned int>(std::distance(begin, end));
if (capacity < this->size + dist) {
_Ty* new_container = (_Ty*)malloc(sizeof(_Ty) * (dist + this->size));
capacity = this->size + dist;
memcpy(new_container, container, sizeof(_Ty) * this->size);
free(container);
container = new_container;
}
for (int i = 0; i < dist; ++i) {
container[i + this->size] = *(begin + i);
}
size = this->size + dist;
}
void out(uint32_t n = 4, const char* sep = " ") const;
vector_type<_Ty> subvec_memcpy(uint32_t start, uint32_t end) const {
vector_type<_Ty> subvec(end - start);
memcpy(subvec.container, container + start, sizeof(_Ty) * (end - start));
return subvec;
}
inline vector_type<_Ty> subvec(uint32_t start, uint32_t end) const {
return vector_type<_Ty>(container + start, end - start);
}
vector_type<_Ty> subvec_deep(uint32_t start, uint32_t end) const {
uint32_t len = end - start;
vector_type<_Ty> subvec(len);
for (uint32_t i = 0; i < len; ++i)
subvec[i] = container[i];
return subvec;
}
inline vector_type<_Ty> subvec(uint32_t start = 0) { return subvec(start, size); }
inline vector_type<_Ty> subvec_memcpy(uint32_t start = 0) { return subvec_memcpy(start, size); }
inline vector_type<_Ty> subvec_deep(uint32_t start = 0) { return subvec_deep(start, size); }
~vector_type() {
if (capacity > 0) free(container);
container = 0; size = capacity = 0;
}
#define Op(o, x) \
template<typename T>\
vector_type<typename types::Coercion<_Ty, T>::type> inline x(const vector_type<T>& r) const {\
vector_type<typename types::Coercion<_Ty, T>::type> ret(size);\
for (int i = 0; i < size; ++i)\
ret[i] = container[i] o r[i];\
return ret;\
}
#define Opeq(o, x) \
template<typename T>\
inline vector_type<typename types::Coercion<_Ty, T>::type>& x##eq(const vector_type<T>& r) {\
for (int i = 0; i < size; ++i)\
container[i] = container[i] o##= r[i];\
return *this;\
}
#define Ops(o, x) \
template<typename T>\
vector_type<typename types::Coercion<_Ty, T>::type> operator o (const vector_type<T>& r) const {\
/*[[likely]] if (r.size == size) {*/\
return x(r);\
/*}*/\
}
#define Opseq(o, x) \
template<typename T>\
vector_type<typename types::Coercion<_Ty, T>::type> operator o##= (const vector_type<T>& r) {\
/*[[likely]] if (r.size == size) {*/\
return x##eq(r);\
/*}*/\
}
#define _Make_Ops(M) \
M(+, add) \
M(-, minus) \
M(*, multi) \
M(/, div) \
M(%, mod)
_Make_Ops(Op)
_Make_Ops(Opeq)
_Make_Ops(Ops)
_Make_Ops(Opseq)
};
template <>
class vector_type<void> {
public:
void* container;
uint32_t size, capacity;
typedef void* iterator_t;
vector_type(uint32_t size) : size(size), capacity(size) {
container = (void*)malloc(size);
}
template<typename _Ty>
constexpr vector_type(std::initializer_list<_Ty> _l) {
size = capacity = _l.size();
this->container = malloc(sizeof(_Ty) * _l.size());
_Ty* _container = (_Ty*)this->container;
for (const auto& l : _l) {
*(_container++) = l;
}
}
constexpr vector_type() : size(0), capacity(0), container(0) {};
void* get(uint32_t i, types::Type_t atype){
return static_cast<void*>(static_cast<char*>(container) + (i * types::AType_sizes[atype]));
}
void operator[](const uint32_t& i);
vector_type<void> subvec(uint32_t, uint32_t);
vector_type<void> subvec_memcpy(uint32_t, uint32_t);
vector_type<void> subvec_deep(uint32_t, uint32_t);
vector_type<void> subvec(uint32_t);
vector_type<void> subvec_memcpy(uint32_t);
vector_type<void> subvec_deep(uint32_t);
};
#pragma pack(pop)
#endif
/*
* Bill Sun 2022
*/
#ifndef _VECTOR_TYPE
#define _VECTOR_TYPE
#include <cstring>
#include <cstdlib>
#include <cstdint>
#include <initializer_list>
#include "types.h"
#pragma pack(push, 1)
template <typename _Ty>
class vector_type {
public:
typedef vector_type<_Ty> Decayed_t;
void inline _copy(const vector_type<_Ty>& vt) {
// quick init while using malloc
//if (capacity > 0) free(container);
this->size = vt.size;
this->capacity = vt.capacity;
if (capacity) {
// puts("copy");
this->container = (_Ty*)malloc(size * sizeof(_Ty));
memcpy(container, vt.container, sizeof(_Ty) * size);
}
else {
this->container = vt.container;
}
}
void inline _move(vector_type<_Ty>&& vt) {
if (capacity > 0) free(container);
this->size = vt.size;
this->capacity = vt.capacity;
this->container = vt.container;
// puts("move");
vt.size = vt.capacity = 0;
vt.container = 0;
}
public:
_Ty* container;
uint32_t size, capacity;
typedef _Ty* iterator_t;
typedef _Ty value_t;
vector_type(const uint32_t& size) : size(size), capacity(size) {
container = (_Ty*)malloc(size * sizeof(_Ty));
}
constexpr vector_type(std::initializer_list<_Ty> _l) {
size = capacity = _l.size();
_Ty* _container = this->container = (_Ty*)malloc(sizeof(_Ty) * _l.size());
for (const auto& l : _l) {
*(_container++) = l;
}
}
constexpr vector_type() noexcept : size(0), capacity(0), container(0) {};
constexpr vector_type(_Ty* container, uint32_t len) noexcept : size(len), capacity(0), container(container) {};
constexpr explicit vector_type(const vector_type<_Ty>& vt) noexcept : capacity(0) {
_copy(vt);
}
constexpr vector_type(vector_type<_Ty>&& vt) noexcept : capacity(0) {
_move(std::move(vt));
}
// size >= capacity ==> readonly vector
constexpr vector_type(const uint32_t size, void* data) :
size(size), capacity(0), container(static_cast<_Ty*>(data)) {}
vector_type<_Ty>& operator =(const _Ty& vt) {
if (!container) {
container = (_Ty*)malloc(sizeof(_Ty));
capacity = 1;
}
size = 1;
container[0] = vt;
return *this;
}
// template<template <typename ...> class VT, class T>
// vector_type<_Ty>& operator =(VT<T>&& vt) {
// this->container = (_Ty*)vt.container;
// this->size = vt.size;
// this->capacity = vt.capacity;
// vt.capacity = 0; // rvalue's
// return *this;
// }
vector_type<_Ty>& operator =(const vector_type<_Ty>& vt) {
_copy(vt);
return *this;
}
vector_type<_Ty>& operator =(vector_type<_Ty>&& vt) {
_move(std::move(vt));
return *this;
}
template <template <class> class VT>
vector_type<_Ty>& operator =(const VT<_Ty>& vt) {
if (capacity > 0) free(container);
container = static_cast<_Ty*>(malloc(vt.size * sizeof(_Ty)));
size = vt.size;
capacity = size;
for(uint32_t i = 0; i < size; ++i)
container[i] = vt[i];
return *this;
}
inline void grow() {
if (size >= capacity) { // geometric growth
uint32_t new_capacity = size + 1 + (size >> 1);
_Ty* n_container = (_Ty*)malloc(new_capacity * sizeof(_Ty));
memcpy(n_container, container, sizeof(_Ty) * size);
memset(n_container + size, 0, sizeof(_Ty) * (new_capacity - size));
if (capacity)
free(container);
container = n_container;
capacity = new_capacity;
}
}
void emplace_back(const _Ty& _val) {
grow();
container[size++] = _val;
}
void emplace_back(_Ty&& _val) {
grow();
container[size++] = std::move(_val);
}
iterator_t erase(iterator_t _it) {
#ifdef DEBUG
// Do bound checks
if (!(size && capicity && container &&
_it >= container && (_it - container) < size))
return 0;
#endif
iterator_t curr = _it + 1, end = _it + --size;
while (curr < end)
*(curr - 1) = *(curr++);
return _it;
}
iterator_t begin() const {
return container;
}
iterator_t end() const {
return container + size;
}
iterator_t find(const _Ty item) const {
iterator_t curr = begin(), _end = end();
while (curr != _end && *(curr++) != item);
return curr;
}
inline _Ty& operator[](const uint32_t _i) const {
return container[_i];
}
void shrink_to_fit() {
if (size && capacity != size) {
capacity = size;
_Ty* _container = (_Ty*)malloc(sizeof(_Ty) * size);
memcpy(_container, container, sizeof(_Ty) * size);
free(container);
container = _container;
}
}
_Ty& back() {
return container[size - 1];
}
void qpop() {
size = size ? size - 1 : size;
}
void pop_resize() {
if (size) {
--size;
if (capacity > (size << 1))
{
_Ty* new_container = (_Ty*)malloc(sizeof(_Ty) * size);
memcpy(new_container, container, sizeof(_Ty) * size);
free(container);
container = new_container;
capacity = size;
}
}
}
_Ty pop() {
return container[--size];
}
void merge(vector_type<_Ty>& _other) {
if (capacity < this->size + _other.size)
{
_Ty* new_container = (_Ty*)malloc(sizeof(_Ty) * (_other.size + this->size));
capacity = this->size + _other.size;
memcpy(new_container, container, sizeof(_Ty) * this->size);
memcpy(new_container + this->size, _other.container, sizeof(_Ty) * _other.size);
free(container);
container = new_container;
}
else
memcpy(container + this->size, _other.container, sizeof(_Ty) * _other.size);
size = this->size + _other.size;
}
template<class _Iter>
void merge(_Iter begin, _Iter end) {
unsigned int dist = static_cast<unsigned int>(std::distance(begin, end));
if (capacity < this->size + dist) {
_Ty* new_container = (_Ty*)malloc(sizeof(_Ty) * (dist + this->size));
capacity = this->size + dist;
memcpy(new_container, container, sizeof(_Ty) * this->size);
free(container);
container = new_container;
}
for (int i = 0; i < dist; ++i) {
container[i + this->size] = *(begin + i);
}
size = this->size + dist;
}
void out(uint32_t n = 4, const char* sep = " ") const;
vector_type<_Ty> subvec_memcpy(uint32_t start, uint32_t end) const {
vector_type<_Ty> subvec(end - start);
memcpy(subvec.container, container + start, sizeof(_Ty) * (end - start));
return subvec;
}
inline vector_type<_Ty> subvec(uint32_t start, uint32_t end) const {
return vector_type<_Ty>(container + start, end - start);
}
vector_type<_Ty> subvec_deep(uint32_t start, uint32_t end) const {
uint32_t len = end - start;
vector_type<_Ty> subvec(len);
for (uint32_t i = 0; i < len; ++i)
subvec[i] = container[i];
return subvec;
}
inline vector_type<_Ty> subvec(uint32_t start = 0) { return subvec(start, size); }
inline vector_type<_Ty> subvec_memcpy(uint32_t start = 0) { return subvec_memcpy(start, size); }
inline vector_type<_Ty> subvec_deep(uint32_t start = 0) { return subvec_deep(start, size); }
~vector_type() {
if (capacity > 0) free(container);
container = 0; size = capacity = 0;
}
#define Op(o, x) \
template<typename T>\
vector_type<typename types::Coercion<_Ty, T>::type> inline x(const vector_type<T>& r) const {\
vector_type<typename types::Coercion<_Ty, T>::type> ret(size);\
for (int i = 0; i < size; ++i)\
ret[i] = container[i] o r[i];\
return ret;\
}
#define Opeq(o, x) \
template<typename T>\
inline vector_type<typename types::Coercion<_Ty, T>::type>& x##eq(const vector_type<T>& r) {\
for (int i = 0; i < size; ++i)\
container[i] = container[i] o##= r[i];\
return *this;\
}
#define Ops(o, x) \
template<typename T>\
vector_type<typename types::Coercion<_Ty, T>::type> operator o (const vector_type<T>& r) const {\
/*[[likely]] if (r.size == size) {*/\
return x(r);\
/*}*/\
}
#define Opseq(o, x) \
template<typename T>\
vector_type<typename types::Coercion<_Ty, T>::type> operator o##= (const vector_type<T>& r) {\
/*[[likely]] if (r.size == size) {*/\
return x##eq(r);\
/*}*/\
}
#define _Make_Ops(M) \
M(+, add) \
M(-, minus) \
M(*, multi) \
M(/, div) \
M(%, mod)
_Make_Ops(Op)
_Make_Ops(Opeq)
_Make_Ops(Ops)
_Make_Ops(Opseq)
};
template <>
class vector_type<void> {
public:
void* container;
uint32_t size, capacity;
typedef void* iterator_t;
vector_type(uint32_t size) : size(size), capacity(size) {
container = (void*)malloc(size);
}
template<typename _Ty>
constexpr vector_type(std::initializer_list<_Ty> _l) {
size = capacity = _l.size();
this->container = malloc(sizeof(_Ty) * _l.size());
_Ty* _container = (_Ty*)this->container;
for (const auto& l : _l) {
*(_container++) = l;
}
}
constexpr vector_type() : size(0), capacity(0), container(0) {};
void* get(uint32_t i, types::Type_t atype){
return static_cast<void*>(static_cast<char*>(container) + (i * types::AType_sizes[atype]));
}
void operator[](const uint32_t& i);
vector_type<void> subvec(uint32_t, uint32_t);
vector_type<void> subvec_memcpy(uint32_t, uint32_t);
vector_type<void> subvec_deep(uint32_t, uint32_t);
vector_type<void> subvec(uint32_t);
vector_type<void> subvec_memcpy(uint32_t);
vector_type<void> subvec_deep(uint32_t);
};
#pragma pack(pop)
#endif
+40 -40
View File
@@ -1,40 +1,40 @@
#include "pch.hpp"
#ifdef _WIN32
#include "winhelper.h"
#include <Windows.h>
void* dlopen(const char* lib, int)
{
return LoadLibraryA(lib);
}
void* dlsym(void* handle, const char* proc)
{
return reinterpret_cast<void*>(GetProcAddress(static_cast<HMODULE>(handle), proc));
}
int dlclose(void* handle)
{
return FreeLibrary(static_cast<HMODULE>(handle));
}
SharedMemory::SharedMemory(const char* fname)
{
this->hFileMap = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 2, fname);
if (this->hFileMap)
this->pData = MapViewOfFile(this->hFileMap, FILE_MAP_ALL_ACCESS, 0, 0, 2);
else
this->pData = NULL;
}
void SharedMemory::FreeMemoryMap()
{
if (this->hFileMap)
if (this->pData)
UnmapViewOfFile(this->pData);
if (this->hFileMap)
CloseHandle(this->hFileMap);
}
#endif
#include "pch.hpp"
#ifdef _WIN32
#include "winhelper.h"
#include <Windows.h>
void* dlopen(const char* lib, int)
{
return LoadLibraryA(lib);
}
void* dlsym(void* handle, const char* proc)
{
return reinterpret_cast<void*>(GetProcAddress(static_cast<HMODULE>(handle), proc));
}
int dlclose(void* handle)
{
return FreeLibrary(static_cast<HMODULE>(handle));
}
SharedMemory::SharedMemory(const char* fname)
{
this->hFileMap = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 2, fname);
if (this->hFileMap)
this->pData = MapViewOfFile(this->hFileMap, FILE_MAP_ALL_ACCESS, 0, 0, 2);
else
this->pData = NULL;
}
void SharedMemory::FreeMemoryMap()
{
if (this->hFileMap)
if (this->pData)
UnmapViewOfFile(this->pData);
if (this->hFileMap)
CloseHandle(this->hFileMap);
}
#endif
+16 -16
View File
@@ -1,16 +1,16 @@
#ifndef _WINHELPER_H
#define _WINHELPER_H
#ifdef _WIN32
static constexpr int RTLD_LAZY = 1;
void* dlopen(const char*, int);
void* dlsym(void*, const char*);
int dlclose(void*);
struct SharedMemory
{
void* hFileMap;
void* pData;
SharedMemory(const char*);
void FreeMemoryMap();
};
#endif
#endif
#ifndef _WINHELPER_H
#define _WINHELPER_H
#ifdef _WIN32
static constexpr int RTLD_LAZY = 1;
void* dlopen(const char*, int);
void* dlsym(void*, const char*);
int dlclose(void*);
struct SharedMemory
{
void* hFileMap;
void* pData;
SharedMemory(const char*);
void FreeMemoryMap();
};
#endif
#endif