Skip to content

Commit

Permalink
start refactoring cpp code
Browse files Browse the repository at this point in the history
  • Loading branch information
Konrad1991 committed Dec 20, 2024
1 parent 17c4508 commit 82f6e75
Show file tree
Hide file tree
Showing 11 changed files with 895 additions and 922 deletions.
23 changes: 23 additions & 0 deletions .development/test_new.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
/*
g++ test_new.cpp -std=c++20 -g -Wall -Wpedantic -Wextra -Wconversion
-Wconversion -Wunitialized -Wshadow -Wmissing-declarations
-Wimplicit-fallthrough
-Wduplicated-cond
-Wsign-compare
-Wformat
-Wnull-dereference
-Wdeprecated
-Wstrict-aliasing
*/
#define STANDALONE_ETR
#include "../inst/include/etr_bits/Core.hpp"
#include "../inst/include/etr_bits/BufferVector.hpp"
#include "../inst/include/etr_bits/BinaryCalculations.hpp"
#include "../inst/include/etr_bits/UnaryCalculations.hpp"
using namespace etr;

int main() {
Expand All @@ -11,5 +25,14 @@ int main() {
Borrow<double> b(ptr, 5);
std::cout << b << std::endl;

Vec<double> vec(SI{10});
std::cout << vec.size() << std::endl;
for (auto& i: vec) i = 3.14;
vec = -(vec + vec);
std::cout << vec << std::endl;


vec[0] = 10;

delete[] ptr;
}
54 changes: 13 additions & 41 deletions inst/include/etr_bits/BinaryCalculations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,14 @@

#include "Core.hpp"

// TODO: what is needed in order that for loops work with Operations
/*
RetType* p;
auto begin() const {
return It<RetType>{p};
}
auto end() const {
return It<RetType>{p + this -> size()};
}
*/

namespace etr {

template <typename L, typename R, typename Trait, typename CTrait>
template <typename L, typename R, typename BTrait>
struct BinaryOperation {
using Type = DoubleTrait;
using TypeTrait = Trait;
using CaseTrait = BinaryTrait;
using RetType = typename CTrait::RetType;
using Trait = BTrait;
// TODO: the identification of RetType misses the fact that it is bool if
// BTrait is part of the Comparison Traits
using RetType = std::common_type_t<typename L::RetType, typename R::RetType>;
const L &l;
const R &r;
using typeTraitL = L;
Expand All @@ -39,12 +28,10 @@ struct BinaryOperation {
} else {
return l.im() || r.im();
}
return false; // TODO: correct?
return false;
}
std::size_t nc() const {
if constexpr (IsArithV<L>) {
// TODO: check whether one has to add here the test whether R and L are
// matrices. Same is true for nr
return r.nc();
} else if constexpr (IsArithV<R>) {
return l.nc();
Expand Down Expand Up @@ -88,14 +75,14 @@ struct BinaryOperation {
// TODO: are all of those constructors required?
// Actually only BinaryOperation(const L &l_, const R &r_) : l(l_), r(r_) {}
// is used.
BinaryOperation(const BinaryOperation &other) : l(other.l), r(other.r) {}
BinaryOperation(const BinaryOperation &&other)
: l(std::move(other.l)), r(std::move(other.r)) {}
// BinaryOperation(const BinaryOperation &other) : l(other.l), r(other.r) {}
// BinaryOperation(const BinaryOperation &&other)
// : l(std::move(other.l)), r(std::move(other.r)) {}
BinaryOperation(const L &l_, const R &r_) : l(l_), r(r_) {}
template <typename LType, typename RType, typename TraitOther>
BinaryOperation(const BinaryOperation<LType, RType, TraitOther>
&other) // TODO: needs move constructor
: l(other.l), r(other.r) {}
// template <typename LType, typename RType, typename TraitOther>
// BinaryOperation(const BinaryOperation<LType, RType, TraitOther>
// &other) // TODO: needs move constructor
// : l(other.l), r(other.r) {}

auto operator[](std::size_t i) const {
constexpr bool isDoubleL = IsArithV<L>;
Expand Down Expand Up @@ -132,21 +119,6 @@ struct BinaryOperation {
mp.setMatrix(mp_.ismatrix, mp_.rows, mp_.cols);
}

template <typename AV> static std::size_t getSize(AV &av) {
using TyL = typename ReRef<typeTraitL>::type;
using TyR = typename ReRef<typeTraitR>::type;
return TyL::getSize(av) > TyR::getSize(av) ? TyL::getSize(av)
: TyR::getSize(av);
}
template <typename AV>
static RetType getVal(AV &av,
std::size_t VecIdx) { // issue: how to handle scalar
// types? Or temporary types?
using TyL = typename ReRef<typeTraitL>::type;
using TyR = typename ReRef<typeTraitR>::type;
return f(TyL::template getVal<AV>(av, VecIdx % TyL::getSize(av)),
TyR::template getVal<AV>(av, VecIdx % TyR::getSize(av)));
}
};

template <typename L, typename R> auto operator+(const L &l, const R &r) {
Expand Down
31 changes: 17 additions & 14 deletions inst/include/etr_bits/Core/BaseStore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ template <typename T, typename BaseTrait> struct BaseStore {
};
#endif
BaseStore(std::size_t sz_)
: sz(sz_), capacity(static_cast<std::size_t>(sz_ * 1.15)) {
: sz(sz_), capacity(static_cast<std::size_t>(static_cast<double>(sz_)* 1.15)) {
ass<"Size has to be larger than 0!">(sz_ > 0);
p = new T[capacity];
for (std::size_t i = 0; i < capacity; i++) {
Expand All @@ -111,7 +111,7 @@ template <typename T, typename BaseTrait> struct BaseStore {
}
BaseStore(int sz_)
: sz(static_cast<std::size_t>(sz_)),
capacity(static_cast<std::size_t>(sz_ * 1.15)) {
capacity(static_cast<std::size_t>(static_cast<double>(sz_) * 1.15)) {
ass<"Size has to be larger than 0!!">(sz_ > 0);
p = new T[capacity];
for (std::size_t i = 0; i < sz; i++)
Expand Down Expand Up @@ -140,7 +140,7 @@ template <typename T, typename BaseTrait> struct BaseStore {
}

template <typename TInp>
requires(IsArithV<ReRef<TInp>>)
requires(IsArithV<TInp>)
void fill(TInp &&val) {
if constexpr (IS<T, TInp>) {
std::fill(p, p + sz, val);
Expand All @@ -150,7 +150,9 @@ template <typename T, typename BaseTrait> struct BaseStore {
}
}

template <typename TInp> void fill(TInp &&inp) {
template <typename TInp>
requires (!IsArithV<TInp>)
void fill(TInp &&inp) {
ass<"cannot use fill with vectors of different lengths">(inp.size() == sz);
using DataType =
typename ExtractDataType<ReRef<TInp>>::RetType;
Expand Down Expand Up @@ -251,12 +253,12 @@ template <typename T, typename BaseTrait> struct BaseStore {
p = nullptr;
}
sz = size;
capacity = static_cast<std::size_t>(1.15 * sz);
capacity = static_cast<std::size_t>(1.15 * static_cast<double>(sz));
p = new T[capacity];
allocated = true;
}
void resize(std::size_t newSize) {
ass<"Size has to be larger than 0!!!">(newSize >= 0);
ass<"Size has to be larger than 0!!!">(newSize > 0);
if (!allocated) {
init(newSize);
fill(T());
Expand All @@ -265,7 +267,7 @@ template <typename T, typename BaseTrait> struct BaseStore {
if (newSize > capacity) {
ass<"try to delete nullptr">(p != nullptr);
delete[] p;
capacity = static_cast<std::size_t>(newSize * 1.15);
capacity = static_cast<std::size_t>(static_cast<double>(newSize) * 1.15);
p = new T[capacity];
sz = newSize;
allocated = true;
Expand All @@ -278,25 +280,26 @@ template <typename T, typename BaseTrait> struct BaseStore {
}

RetType operator[](std::size_t idx) const {
// TODO: can ass be faster?
ass<"No memory was allocated">(allocated);
ass<"Error: out of boundaries --> value below 1">(idx >= 0);
ass<"Error: out of boundaries --> value beyond size of vector">(idx < sz);
// NOTE: negative idx leads to overflow and
// is than the max possible value of std::size_t
ass<"Error: out of boundaries">(idx < sz);
return p[idx];
}

// TODO: check with static_asssert that this is never called for const T
// TODO: update the assertions for all operator[]
RetType &operator[](std::size_t idx) {
ass<"No memory was allocated">(allocated);
ass<"Error: out of boundaries --> value below 1">(idx >= 0);
// NOTE: negative idx leads to overflow and
// is than the max possible value of std::size_t
ass<"Error: out of boundaries --> value beyond size of vector">(idx < sz);
return p[idx];
}

template <typename L2> void moveit(L2 &other) {
T *temporary = other.p;
int tempSize = other.sz;
int tempCapacity = other.capacity;
std::size_t tempSize = other.sz;
std::size_t tempCapacity = other.capacity;
other.p = this->p;
other.sz = this->sz;
other.capacity = this->capacity;
Expand Down
12 changes: 6 additions & 6 deletions inst/include/etr_bits/Core/Borrow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ template <typename T, typename BorrowTrait> struct Borrow {
};
Borrow(std::size_t r, std::size_t c) = delete;
Borrow(std::size_t r, std::size_t c, const double value) = delete;
Borrow(T *p, std::size_t sz) {
this->p = p;
this->sz = sz;
Borrow(T *p_, std::size_t sz_) {
this->p = p_;
this->sz = sz_;
capacity = sz;
this->allocated = true;
}

void init(T *p, std::size_t sz) {
this->p = p;
this->sz = sz;
void init(T *p_, std::size_t sz_) {
this->p = p_;
this->sz = sz_;
capacity = sz;
this->allocated = true;
}
Expand Down
71 changes: 53 additions & 18 deletions inst/include/etr_bits/Core/Concepts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,20 @@ concept isBID = requires {
requires IS<T, bool> || IS<T, int> ||
IS<T, double>;
};

// Test R class
// ===================================================================
template <typename T>
concept IsSubset = requires {
typename ReRef<T>::type::Trait;
requires IS<typename ReRef<T>::type::Trait, SubsetTrait>;
};
template <typename T>
concept IsSubsetClass = requires {
typename ReRef<T>::type::Trait;
requires IS<typename ReRef<T>::type::Trait, SubsetClassTrait>;
};
template <typename T>
concept IsLBuffer = requires {
typename ReRef<T>::type::Trait;
requires IS<typename ReRef<T>::type::Trait, LBufferTrait>;
Expand All @@ -30,13 +38,21 @@ concept IsRBuffer = requires {
typename ReRef<T>::type::Trait;
requires IS<typename ReRef<T>::type::Trait, RBufferTrait>;
};
template <typename T>
concept IsBorrow = requires {
typename ReRef<T>::type::Trait;
requires IS<typename ReRef<T>::type::Trait, BorrowTrait>;
};
template <typename T>
concept IsBorrowSEXP = requires {
typename ReRef<T>::type::Trait;
requires IS<typename ReRef<T>::type::Trait, BorrowSEXPTrait>;
};

template <typename T>
concept IsUnary = requires {
typename ReRef<decltype(T{})>::type::Trait;
requires IS<
typename ReRef<decltype(T{})>::type::Trait,
SinusTrait> ||
requires IS<typename ReRef<decltype(T{})>::type::Trait, SinusTrait> ||
IS<typename ReRef<decltype(T{})>::type::Trait, ASinusTrait> ||
IS<typename ReRef<decltype(T{})>::type::Trait, SinusHTrait> ||
IS<typename ReRef<decltype(T{})>::type::Trait, CosinusTrait> ||
Expand All @@ -52,22 +68,22 @@ concept IsUnary = requires {
};
template <typename T>
concept IsBinary = requires {
typename ReRef<decltype(T{})>::type::Trait;
requires IS<
typename ReRef<decltype(T{})>::type::Trait,
PlusTrait> ||
IS<typename ReRef<decltype(T{})>::type::Trait, MinusTrait> ||
IS<typename ReRef<decltype(T{})>::type::Trait, TimesTrait> ||
IS<typename ReRef<decltype(T{})>::type::Trait, DivideTrait> ||
IS<typename ReRef<decltype(T{})>::type::Trait, PowTrait> ||
IS<typename ReRef<decltype(T{})>::type::Trait, EqualTrait> ||
IS<typename ReRef<decltype(T{})>::type::Trait, SmallerTrait> ||
IS<typename ReRef<decltype(T{})>::type::Trait, SmallerEqualTrait> ||
IS<typename ReRef<decltype(T{})>::type::Trait, LargerTrait> ||
IS<typename ReRef<decltype(T{})>::type::Trait, LargerEqualTrait> ||
IS<typename ReRef<decltype(T{})>::type::Trait, UnEqualTrait>;
typename ReRef<T>::type::Trait;
requires IS<typename ReRef<T>::type::Trait, PlusTrait> ||
IS<typename ReRef<T>::type::Trait, MinusTrait> ||
IS<typename ReRef<T>::type::Trait, TimesTrait> ||
IS<typename ReRef<T>::type::Trait, DivideTrait> ||
IS<typename ReRef<T>::type::Trait, PowTrait> ||
IS<typename ReRef<T>::type::Trait, EqualTrait> ||
IS<typename ReRef<T>::type::Trait, SmallerTrait> ||
IS<typename ReRef<T>::type::Trait, SmallerEqualTrait> ||
IS<typename ReRef<T>::type::Trait, LargerTrait> ||
IS<typename ReRef<T>::type::Trait, LargerEqualTrait> ||
IS<typename ReRef<T>::type::Trait, UnEqualTrait>;
};

// Test Vec class
// ===================================================================
template <typename T>
concept IsLBufferVec = requires(T t) {
typename T::DType;
Expand All @@ -84,6 +100,9 @@ concept IsSubsetVec = requires(T t) {
requires IsSubset<typename T::DType>;
};

// Test Input class
// ===================================================================
// Vector
template <typename T>
struct is_vec: std::false_type {};

Expand All @@ -95,13 +114,29 @@ inline constexpr bool is_vec_v = is_vec <T>::value;
template <typename T>
concept IsVec = is_vec_v<T>;


template <typename T>
concept IsRVec = requires(T t) {
typename T::DType;
requires IsVec<T> && IsRBuffer<typename T::DType>;
};


// Binary
template <typename T>
struct is_binary_operation : std::false_type {};

template <typename L2, typename R2, typename OperationTrait>
struct is_binary_operation<BinaryOperation<L2, R2, OperationTrait>>
: std::bool_constant<IsBinary<OperationTrait>> {};

template <typename T>
inline constexpr bool is_binary_operation_v = is_binary_operation<T>::value;

template <typename T>
concept IsBinaryOperation = is_binary_operation_v<T>;



} // namespace etr

#endif
11 changes: 9 additions & 2 deletions inst/include/etr_bits/Core/Types.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// TODO: add the iterator to each class: Buffer, Subset, Binary, Unary, Borrow, BorrowSEXP
// Directly use the iterator methods begin, end etc. in Vec
// Replace BaseStore with Buffer.
// Each class has to possess RetType and Trait


#ifndef TYPES_ETR_H
#define TYPES_ETR_H

Expand All @@ -13,13 +19,14 @@ template <typename T = double, typename BorrowSEXPTrait = BorrowSEXPTrait>
struct BorrowSEXP;
template <typename T, typename BorrowTrait = BorrowTrait> struct Borrow;
template <typename T, typename SubsetTrait = SubsetTrait> struct Subset;
template <typename L, typename R, typename Trait> struct SubsetClass;
template <typename T, typename BufferTrait = LBufferTrait>
struct Buffer;
template <typename T, typename R = Buffer<T>>
struct Vec;
template <typename L, typename R, typename Trait = BinaryTrait>
template <typename L, typename R, typename BTrait = BinaryTrait>
struct BinaryOperation;
template <typename I, typename Trait = UnaryTrait>
template <typename I, typename UTrait = UnaryTrait>
struct UnaryOperation;

template <typename T> struct ExtractDataType;
Expand Down
Loading

0 comments on commit 82f6e75

Please sign in to comment.