Skip to content

Commit

Permalink
rewrite vector subsettting in an analogous way to matrix subsetting
Browse files Browse the repository at this point in the history
  • Loading branch information
konrad.kraemer committed May 29, 2024
1 parent 3cc5137 commit b47df40
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 12 deletions.
85 changes: 81 additions & 4 deletions include/etr_bits/Subsetting/VectorSubsetting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,83 @@ vec
vecbool
*/

template <typename T, typename I>
inline void calcIndVector(T &vec, Indices &ind, const I *idx) {
if constexpr(is<I, bool>) {
if (*idx) {
ind.resize(vec.size());
for (std::size_t i = 0; i < vec.size(); i++)
ind[i] = i;
return;
} else {
ass(false, "Variable[FALSE] subsetting is not supported. Sorry");
return;
}
} 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>) {
ind.resize(1);
ind[0] = convertSizeSubsetting(*idx) - 1;
return;
} else if constexpr(IsAV<I>) {

using IndexType = ExtractDataType<I>::RetType;
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()])
sizeTrue++;
ind.resize(sizeTrue);
std::size_t counter = 0;
for (std::size_t i = 0; i < vec.size(); i++) {
if ((*idx)[i % idx->size()]) {
ind[counter] = i;
counter++;
}
}
} 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>) {
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 {
static_assert(sizeof(T) == 0, "Unknown type of index variable");
}

}
}

template <typename V, typename I>
requires IsVec<V>
inline auto subset(V &vec, I &&idx) {
using DataType = ExtractDataType<V>::RetType;
Subset<decltype(convert(vec).d), SubsetTrait> sub(vec);
calcIndVector(vec, sub.ind, &idx);
return Vec<DataType, decltype(convertSubset(vec)), SubVecTrait>(
std::move(sub));
}

template <typename V, typename I>
requires(IsRVec<V> || IsSubVec<V> || OperationVec<V>)
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));
}

/*
// NOTE: bool
template <typename T, typename I>
inline void calcIndBool(T &vec, Indices &ind, const I *idx) {
Expand Down Expand Up @@ -175,7 +252,7 @@ template <typename V, typename I>
IsSubVec<I> || OperationVec<I>)
inline const auto subset(V &&vec, I &&idx) {
using DataType = ExtractDataType<V>::RetType;
Subset<decltype(convert(vec).d), SubsetTrait> sub(vec);
Subset<const decltype(convert(vec).d), SubsetTrait> sub(vec);
calcIndVec(vec, sub.ind, &idx);
sub.setMatrix(false, 0, 0);
return Vec<DataType, decltype(convertSubset(vec)), SubVecTrait>(
Expand All @@ -184,17 +261,17 @@ inline const auto subset(V &&vec, I &&idx) {
template <typename V, typename I>
requires(IsRVec<V> || IsSubVec<V> || OperationVec<V> && IsVec<I>)
inline const auto subset(V &&vec, I &idx) {
inline const auto subset(V &&vec, I &&idx) {
using DataType = ExtractDataType<V>::RetType;
Subset<decltype(convert(vec).d), SubsetTrait> sub(vec);
Subset<const decltype(convert(vec).d), SubsetTrait> sub(vec);
calcIndVec(vec, sub.ind, &idx);
sub.setMatrix(false, 0, 0);
return Vec<DataType, decltype(convertSubset(vec)),
SubVecTrait>( // TODO: check whether here convertSubsetConst is
// required
std::move(sub));
}

*/
} // namespace etr

#endif // !DEBUG
31 changes: 23 additions & 8 deletions tests/Borrow_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,32 @@ void test_borrow() {
}

int main(int argc, char *argv[]) {
test_borrow();
Vec<double> ret;
//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) =
1; // TODO: int works. Implement the same call stack for double and bool
ret(coca(6, 2, 3)) = coca(1.2, 1.2, 1.2);
// ret(1) = 1.2;
// etr::subset(ret, 1) = etr::i2d(1);
//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);

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);

return 0;
}

0 comments on commit b47df40

Please sign in to comment.