Skip to content

Commit

Permalink
testing of other operator=
Browse files Browse the repository at this point in the history
  • Loading branch information
Konrad1991 committed Jun 3, 2024
1 parent 0702f59 commit d1a48af
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 29 deletions.
2 changes: 1 addition & 1 deletion include/etr_bits/BinaryCalculations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ struct BinaryOperation {
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) // issue: needs move constructor
&other) // TODO: needs move constructor
: l(other.l), r(other.r) {}

auto operator[](std::size_t i) const {
Expand Down
23 changes: 11 additions & 12 deletions include/etr_bits/Subsetting/VectorSubsetting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ vecbool

template <typename T, typename I>
inline void calcIndVector(T &vec, Indices &ind, const I *idx) {
if constexpr(is<I, bool>) {
if constexpr (is<I, bool>) {
if (*idx) {
ind.resize(vec.size());
for (std::size_t i = 0; i < vec.size(); i++)
Expand All @@ -25,19 +25,19 @@ inline void calcIndVector(T &vec, Indices &ind, const I *idx) {
ass(false, "Variable[FALSE] subsetting is not supported. Sorry");
return;
}
} else if constexpr(is<I, int>) {
} else if constexpr (is<I, int>) {
ind.resize(1);
ind[0] = *idx - 1;
ass(ind[0] >= 0, "invalid index argument");
return;
} else if constexpr(is<I, double>) {
} else if constexpr (is<I, double>) {
ind.resize(1);
ind[0] = convertSizeSubsetting(*idx) - 1;
return;
} else if constexpr(IsAV<I>) {
} else if constexpr (IsAV<I>) {

using IndexType = ExtractDataType<I>::RetType;
if constexpr(is<IndexType, bool>) {
if constexpr (is<IndexType, bool>) {
std::size_t sizeTrue = 0;
for (std::size_t i = 0; i < vec.size(); i++)
if ((*idx)[i % idx->size()])
Expand All @@ -50,22 +50,21 @@ inline void calcIndVector(T &vec, Indices &ind, const I *idx) {
counter++;
}
}
} else if constexpr(is<IndexType, int>) {
} else if constexpr (is<IndexType, int>) {
ind.resize(idx->size());
for (std::size_t i = 0; i < idx->size(); i++) {
std::size_t sizeTIdx = (*idx)[i] - 1;
ind[i] = sizeTIdx;
}
} else if constexpr(is<IndexType, double>) {
} else if constexpr (is<IndexType, double>) {
ind.resize(idx->size());
for (std::size_t i = 0; i < idx->size(); i++) {
std::size_t sizeTIdx = static_cast<std::size_t>((*idx)[i]) - 1;
ind[i] = sizeTIdx;
}
} else {
} else {
static_assert(sizeof(T) == 0, "Unknown type of index variable");
}

}
}

Expand All @@ -76,7 +75,7 @@ inline auto subset(V &vec, I &&idx) {
Subset<decltype(convert(vec).d), SubsetTrait> sub(vec);
calcIndVector(vec, sub.ind, &idx);
return Vec<DataType, decltype(convertSubset(vec)), SubVecTrait>(
std::move(sub));
std::move(sub));
}

template <typename V, typename I>
Expand All @@ -85,8 +84,8 @@ inline auto subset(V &&vec, I &&idx) {
using DataType = ExtractDataType<V>::RetType;
Subset<const decltype(convert(vec).d), SubsetTrait> sub(vec);
calcIndVector(vec, sub.ind, &idx);
return Vec<DataType, decltype(convertSubsetConst(vec)), SubVecTrait>(
std::move(sub));
return Vec<DataType, decltype(convertSubsetConst(vec)), SubVecTrait>(
std::move(sub));
}

/*
Expand Down
102 changes: 102 additions & 0 deletions include/etr_bits/Vector/AssignmentOperator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,108 @@
#include "VectorClass.hpp"
#include <type_traits>

/*
template <typename TD> Vec &operator=(const TD &&otherVec) {
static_assert(!isUnaryOP::value, "Cannot assign to unary calculation");
static_assert(!isBinaryOP::value, "Cannot assign to binary calculation");
static_assert(!isRVec::value,
"Cannot assign to a r value. E.g. c(1, 2, 3) <- 3");
// NOTE: arithmetic otherVecut
if constexpr (std::is_arithmetic_v<TD>) {
if constexpr (is<T, TD>) {
// NOTE: T == TD
if constexpr (isSubset::value) {
for (std::size_t i = 0; i < d.ind.size(); i++) {
d[i] = otherVec;
}
} else if constexpr (isBorrow::value) {
d.sz = 1;
d[0] = otherVec;
} else {
d.resize(1);
d[0] = otherVec;
}
// NOTE: T != TD
} else {
if constexpr (isSubset::value) {
for (std::size_t i = 0; i < d.ind.size(); i++) {
d[i] = static_cast<T>(otherVec);
}
} else if constexpr (isBorrow::value) {
d.sz = 1;
d[0] = static_cast<T>(otherVec);
} else {
d.resize(1);
d[0] = static_cast<T>(otherVec);
}
}
} else { // NOTE: vector otherVecut
using RetTypeOtherVec =
std::remove_reference<decltype(otherVec.d)>::type::RetType;
using isT = std::is_same<RetTypeOtherVec, T>;
if constexpr (isBuffer::value) {
temp.resize(otherVec.size());
if constexpr (isT::value) {
for (std::size_t i = 0; i < otherVec.size(); i++) {
temp[i] = otherVec[i];
}
} else {
for (std::size_t i = 0; i < otherVec.size(); i++) {
temp[i] = static_cast<T>(otherVec[i]);
}
}
d.moveit(temp);
} else if constexpr (isBorrow::value) {
ass(otherVec.size() <= d.capacity,
"number of items to replace is not a multiple of replacement length");
temp.resize(otherVec.size());
for (std::size_t i = 0; i < otherVec.size(); i++)
temp[i] = otherVec[i];
d.sz = otherVec.size();
for (std::size_t i = 0; i < otherVec.size(); i++)
d[i] = temp[i];
} else if constexpr (isBorrowSEXP::value) {
temp.resize(otherVec.size());
if constexpr (isT::value) {
for (std::size_t i = 0; i < otherVec.size(); i++) {
temp[i] = otherVec[i];
}
} else {
for (std::size_t i = 0; i < otherVec.size(); i++) {
temp[i] = static_cast<T>(otherVec[i]);
}
}
if (otherVec.size() > this->size())
d.resize(otherVec.size());
d.moveit(temp);
} else if constexpr (isSubset::value) {
ass(otherVec.size() == d.ind.size(),
"number of items to replace is not a multiple of replacement length");
temp.resize(otherVec.size());
if constexpr (isT::value) {
for (std::size_t i = 0; i < otherVec.size(); i++) {
temp[i] = otherVec[i];
}
} else {
for (std::size_t i = 0; i < otherVec.size(); i++) {
temp[i] = static_cast<T>(otherVec[i]);
}
}
for (std::size_t i = 0; i < d.ind.size(); i++) {
d[i % d.ind.size()] = temp[i];
}
}
if (otherVec.d.im()) {
d.setMatrix(true, otherVec.d.nr(), otherVec.d.nc());
}
}
return *this;
}
*/

template <typename TD>
requires std::is_same_v<TD, T>
Vec &operator=(const TD inp) {
Expand Down
10 changes: 9 additions & 1 deletion include/etr_bits/Vector/Constructors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ explicit Vec(const BinaryOperation<L2, R2, OperationTrait> &&inp) : d(inp) {
d.setMatrix(inp.mp);
}
template <typename L2, typename R2, typename OperationTrait,
typename DetailTrait> // only for comparison!
typename DetailTrait> // NOTE: only for comparison!
explicit Vec(const BinaryOperation<L2, R2, OperationTrait, DetailTrait> &&inp)
: d(inp) {
using TypeTrait = OperationTrait;
Expand Down Expand Up @@ -125,6 +125,7 @@ explicit Vec(std::size_t rows, std::size_t cols, const double value)
// other vector constructors
template <typename T2, typename R2, typename Trait2>
Vec(const Vec<T2, R2, Trait2> &other_vec) { // : d()
// std::cout << "constructor test 5" << std::endl;
using TypeTrait = Trait2;
using CaseTrait = Trait2;
if constexpr (isBorrow::value) { // issue: is this safe???
Expand All @@ -150,6 +151,8 @@ template <typename T2, typename R2, typename Trait2>
requires IsVec<const Vec<T2, R2, Trait2>>
Vec(const Vec<T2, R2, Trait2>
&&other_vec) { // issue: improve. Use move her : d()
// std::cout << "constructor test 6" << std::endl;

using TypeTrait = Trait2;
using CaseTrait = Trait2;
if constexpr (isBorrow::value) { // issue: is this safe???
Expand Down Expand Up @@ -179,6 +182,8 @@ Vec(const Vec<T2, R2, Trait2>
template <typename T2, typename R2, typename Trait2>
requires(IsRVec<const Vec<T2, R2, Trait2>> && !std::is_same_v<T, T2>)
Vec(const Vec<T2, R2, Trait2> &&other_vec) {
// std::cout << "constructor test 7" << std::endl;

using TypeTrait = Trait2;
using CaseTrait = Trait2;
if constexpr (isBorrow::value) {
Expand Down Expand Up @@ -208,6 +213,8 @@ Vec(const Vec<T2, R2, Trait2> &&other_vec) {
template <typename T2, typename R2, typename Trait2>
requires(IsRVec<const Vec<T2, R2, Trait2>> && std::is_same_v<T, T2>)
Vec(Vec<T2, R2, Trait2> &&other_vec) {
// std::cout << "constructor test 8" << std::endl;

using TypeTrait = Trait2;
using CaseTrait = Trait2;
if constexpr (isBorrow::value) {
Expand Down Expand Up @@ -253,6 +260,7 @@ Vec(BaseType *ptr, std::size_t rows, std::size_t cols) : d(rows * cols) {
template <typename T2>
requires std::is_same_v<T2, bool>
explicit Vec(const Vec<T2> &other_vec) : d() {
// std::cout << "constructor test 5" << std::endl;
d.resize(other_vec.size());
for (std::size_t i = 0; i < d.size(); i++)
d[i] = other_vec[i];
Expand Down
3 changes: 2 additions & 1 deletion include/etr_bits/Vector/VectorClass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ namespace etr {

template <typename T, typename R, typename Trait> struct Vec {
using Type = T;
/* using RetType = T; */
using TypeTrait = Trait;
using CaseTrait = Trait;
R d;
Buffer<T> temp;
using DType = R;
using RetType = std::remove_reference<decltype(d)>::type::RetType;
using typeTraitD = std::remove_reference<decltype(d)>::type::TypeTrait;
Expand All @@ -28,6 +28,7 @@ template <typename T, typename R, typename Trait> struct Vec {
using caseTraitD = std::remove_reference<decltype(d)>::type::CaseTrait;
using isUnaryOP = std::is_same<caseTraitD, UnaryTrait>;
using isBinaryOP = std::is_same<caseTraitD, BinaryTrait>;
using isRVec = std::is_same<CaseTrait, RVecTrait>;

RetType getRetType() const { return RetType{}; }

Expand Down
8 changes: 6 additions & 2 deletions tests/Benchmarks.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#define STANDALONE_ETR
#include "../include/etr.hpp"
using etr;
using namespace etr;

int main(int argc, char *argv[]) { return 0; }
int main(int argc, char *argv[]) {
Vec<double> v = coca(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
print(subset(v / v, coca(1, 2, 3) + 1));
return 0;
}
21 changes: 9 additions & 12 deletions tests/Borrow_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,32 +123,29 @@ void test_borrow() {
}

int main(int argc, char *argv[]) {
//test_borrow();
Vec<double> ret; // TODO: check that a subset is only made using an allocated object
test_borrow();
Vec<double>
ret; // TODO: check that a subset is only made using an allocated object
ret = etr::vector_numeric(etr::i2d(20));
//printTAST<decltype(subset(ret, 1))>();
ret(1) = 2;
// printTAST<decltype(subset(ret, 1))>();
ret(1) = 2;
ret(2) = true;
ret(3) = 3.14;

// TODO: int works. Implement the same call stack for double and bool
// TODO: this is a problam. Check that each class: Buffer, Borrow,
// BorrowSEXP etc. can be assigned with the result of anither class
print(ret);

std::cout << std::boolalpha << (matrix(colon(1, 25), 5, 5)).im() << std::endl;
return 0;
sexp b;
b = coca(i2d(1), i2d(5));
auto test = subset(colon(1, 6) + 0, b);
printTAST<decltype(test)>();
printTAST<decltype(test.d)>();

sexp c;

c = subset(matrix(colon(1, 25), 5, 5), b, b);
print(c);
b = subset((colon(i2d(1), i2d(6)) + i2d(0)), b);
print(b);

b = subset((colon(i2d(1), i2d(6)) + i2d(1)),b + b);
b = subset((colon(i2d(1), i2d(6)) + i2d(1)), b + b);

return 0;
}

0 comments on commit d1a48af

Please sign in to comment.