I want to define n-dimensional data structure using std::vector. I have a problem with definition of operator(). Lets have a look at a sample 2D structure
class my_data {
public:
my_data (size_t N, size_t M) : tab(N*M), _N(N), _M(M) {}
const double& operator() (size_t i, size_t j) const {
return tab.at(i * M + j);
}
double& operator() (size_t i, size_t j) {
return tab.at(i * M + j);
}
private:
std::vector<double> tab;
size_t _N;
size_t _M;
};
I would like to define such a structures for any dimension using templates , but I don't know if it is possible. So basically what I would like to have is something like that
my_data<4> vec (1,2,3,4);
defines 4D 'tensor' with sizes 1,2,3,4 (number of all elements 1*2*3*4). You can now access and change values by typing
vec (0,0,0,0) = 2.0;
The below solution uses some of the C++14 and C++1z features, but they can be easily ported to C++11:
#include <vector>
#include <utility>
#include <array>
#include <cstddef>
namespace detail
{
template <typename T, typename S>
class my_data;
template <typename T, std::size_t... Is>
class my_data<T, std::index_sequence<Is...>>
{
public:
my_data(decltype(Is)... size)
: t((size * ...)), s{{ size... }}
{}
T& operator()(decltype(Is)... i)
{
return t.at(index({{ i... }}));
}
const T& operator()(decltype(Is)... i) const
{
return t.at(index({{ i... }}));
}
private:
std::size_t index(const std::array<std::size_t, sizeof...(Is)>& a) const
{
std::size_t ind = a[0];
for (std::size_t i = 1; i < a.size(); ++i)
{
ind = ind * s[i] + a[i];
}
return ind;
}
std::vector<T> t;
const std::array<std::size_t, sizeof...(Is)> s;
};
}
template <typename T, std::size_t N>
using my_data = detail::my_data<T, std::make_index_sequence<N>>;
Test:
int main()
{
my_data<double, 4> vec(1,2,3,4);
vec(0,0,0,0) = 2.0;
}
DEMO
DEMO (C++11)
Related
I have a method that takes an index-able object as a template parameter, something like:
template <typename OBJ>
int foo(int n, OBJ o)
{
int x = 0;
for (int i = 0; i < n; ++i) {
x += o[i];
}
return x;
}
Is there a way I can pass a lambda function in for the o parameter? In other words, having the lambda be call-able via the [] operator rather than the () operator?
template<class F>
struct square_bracket_invoke_t {
F f;
template<class T>
auto operator[](T&& t)const
-> typename std::result_of< F const&(T&&) >::type
{ return f(std::forward<T>(t)); }
};
template<class F>
square_bracket_invoke_t< typename std::decay<F>::type >
make_square_bracket_invoke( F&& f ) {
return {std::forward<F>(f)};
}
Live example.
Code is C++11 and has basically zero overhead.
int main() {
std::cout << foo( 6, make_square_bracket_invoke([](int x){ return x; } ) ) << "\n";
}
result is 0+1+2+3+4+5 aka 15.
Is this a good idea? Maybe. But why stop there?
For max amusement:
const auto idx_is = make_square_bracket_invoke([](auto&&f){return make_square_bracket_invoke(decltype(f)(f));});
int main() {
std::cout << foo( 6, idx_is[[](int x){ return x; }] ) << "\n";
}
You can do that by:
Creating a class template, a functor, that has the operator[] defined.
Implementing the operator[] in terms of the operator() of a std::function.
Storing the lambda in a wrapped std::function as a member variable of the class template.
Here's a demonstrative program.
#include <iostream>
#include <functional>
template <typename OBJ>
int foo(int n, OBJ o)
{
int x = 0;
for (int i = 0; i < n; ++i) {
x += o[i];
}
return x;
}
template <typename> struct Functor;
template <typename R> struct Functor<R(int)>
{
using ftype = std::function<R(int)>;
Functor(ftype f) : f_(f) {}
R operator[](int i) const { return f_(i); }
ftype f_;
};
int main()
{
Functor<int(int)> f = {[](int i) -> int {return i*i;}};
std::cout << foo(10, f) << std::endl;
}
and its output
285
Live demo
PS
Functor is not the appropriate name here. It does not overload the function call operator. I suspect there is a more appropriate name.
Well, if it helps, here's a way to forward a wrapper class's operator[] to your lambda's operator().
template<class F>
struct SubscriptWrapper_t {
F f_;
template<class T> auto operator[](T const& t_) const -> decltype(f_(t_)) {
return f_(t_);
}
};
template<class F>
SubscriptWrapper_t<typename std::decay<F>::type> SubscriptWrapper(F&& f_) {
return{std::forward<F>(f_)};
}
I use wrappers like this a lot. They're convenient, and they don't seem to have any computational overhead, at least when compiled by GCC. You can make one for at or even make one for find.
EDIT: Updated for C++11 (and updated to be able to return a reference)
A sketch of a wrapper type that would do this.
template<typename UnaryFunction>
class index_wrapper
{
public:
index_wrapper(UnaryFunction func) : func(std::move(func)) {}
template<typename T>
std::invoke_result_t<UnaryFunction, T> operator[](T&& t)
{ return func(std::forward<T>(t)); }
private:
UnaryFunction func;
};
With usage
#include <iostream>
template <typename OBJ>
int foo(int n, OBJ o)
{
int x = 0;
for (int i = 0; i < n; ++i) {
x += o[i];
}
return x;
}
int main()
{
index_wrapper f([](int i) -> int { return i*i; });
std::cout << foo(10, f) << std::endl;
}
You might want to restrict it to a single parameter type, so that you can provide member type aliases similar to std::vector::reference et.al.
I am trying to implement Matrix Addition using expression templates. I am facing some trouble. Here is my matrix code:
#include<iostream>
#include<vector>
#include<cassert>
template <typename T>
class MatrixExpression {
public:
double operator[](size_t i) const { return static_cast<T const&>(*this)[i];}
size_t size()const { return static_cast<T const&>(*this).size(); }
};
template<typename T>
class Matrix:public MatrixExpression<Matrix<T>>
{
std::vector<std::vector<T>> mat;
public:
Matrix(std::size_t m, std::size_t n):mat(m,std::vector<T>(n)){}
class Proxy
{
std::vector<T> vec;
public:
Proxy(std::vector<T> vec):vec(vec){ }
T operator[](std::size_t i){ return vec[i];}
//T &operator[](std::size_t i){ return vec[i];}
std::size_t size() const{ return vec.size(); }
};
Proxy operator[](std::size_t i) const { return Proxy(mat[i]); }
//Proxy &operator[](std::size_t i) { return Proxy(mat[i]); }
size_t size() const { return mat.size(); }
Matrix(std::initializer_list<std::initializer_list<T>> lst)
{
int m=0,n=0;
for(auto l:lst )
{
for(auto v:l)
{
n++;
}
m++;
}
int i=0,j=0;
mat(m,std::vector<T>(n));
for(auto l:lst )
{
for(auto v:l)
{
mat[i].push_back(v);
}
i++;
}
}
Matrix(MatrixExpression<T> const& matx):mat(matx.size(),std::vector<T>(matx[0].size))
{
for(int i=0;i<matx.size();i++)
{
for(int j=0;j<matx[0].size();j++)
{
mat[i][j] = matx[i][j];
}
}
}
};
template<typename T, typename X, typename Y>
class MatrixSum:public MatrixExpression<MatrixSum<T,X,Y>>
{
X const& x;
Y const& y;
public:
MatrixSum(X const& x1, Y const& y1):x(x1),y(y1){
assert(x1.size()==y1.size());
assert(x1[0].size()==y1[0].size());
}
class ProxySum
{
std::vector<T> vec1,vec2;
public:
ProxySum(std::vector<T> vec1,std::vector<T> vec2):vec1(vec1),vec2(vec2){ }
T operator[](std::size_t i){ return vec1[i] + vec2[i];}
//T &operator[](std::size_t i){ return vec1[i] + vec2[i];}
std::size_t size() const{ return vec1[0].size(); }
};
ProxySum operator[](std::size_t i) const { return ProxySum(x[i],y[i]); }
//ProxySum &operator[](std::size_t i){ return ProxySum(x[i],y[i]); }
size_t size() const { return x.size(); }
};
template<typename T,typename X,typename Y>
MatrixSum<T,X,Y>
operator+(X const& x, Y const& y)
{
return MatrixSum<T,X,Y>(x,y);
}
I am getting two errors when using the Matrix class. First is the operator+ does not exist for Matrix (I used int from testing) even though I have implemented operator overloading for '+', and another error is in the second constructor for Matrix. It says that the call I have made for the constructor of mat variable is invalid.But vectors do have such constructor
1) The following line is not a valid C++ syntax:
mat(m,std::vector<T>(n));
You should initialize mat member object in the constructor's initialization list, like this (assuming the outermost initializer_list is not empty):
Matrix(std::initializer_list<std::initializer_list<T>> lst) : mat(lst.size(), std::vector<T>(begin(lst)->size()))
2) As for the operator + you provided:
template<typename T,typename X,typename Y>
MatrixSum<T,X,Y>
operator+(X const& x, Y const& y)
{
return MatrixSum<T,X,Y>(x,y);
}
Note that T template parameter is non-deducible, so the compiler cannot figure it out and thus cannot use this operator. The only way to call it would be like this:
matrix1.operator +<some_type>(matrix2);
...which is probably not what you want.
The right way would be to try and compute T at compile-time, based on X and Y types, using some metaprogramming.
I have a tensor classes of rank N which wrap data stored in an array. For example, a rank-3 tensor would have dimensions (d0,d1,d2) and a unique element would be accessed with the multi-index (i0,i1,i2) from the underlying array of length d0*d1*d2. If d0=d1=d2=10, i0=1, i1=2, i2=3, then element 123 of the array would be accessed.
I've implemented a recursively defined class which computes single array index from the multi-index as follows:
template<size_t N>
class TensorIndex : TensorIndex<N-1> {
private:
size_t d;
public:
template<typename...Ds>
TensorIndex( size_t d0, Ds...ds ) : TensorIndex<N-1>( ds... ), d(d0) {}
template<typename...Is>
size_t index( size_t i0, Is...is ) {
return i0+d*TensorIndex<N-1>::index(is...);
}
};
template<>
struct TensorIndex<1> {
TensorIndex( size_t ) {}
size_t index( size_t i ) { return i; }
};
Which reverses the desired order.
TensorIndex<3> g(10,10,10);
std::cout << g.index(1,2,3) << std::endl;
outputs 321. What would be a simple way to reverse the order of the arguments for the constructor and index functions?
Edit:
I tried implementing using the suggested approach of reversing the variadic arguments, but this was suboptimal as it required reversing the arguments for both index and the constructor and the necessary helper functions for these two cases would appear slightly different. The initializer list answer looks more straightforward.
No need of recursion nor to reverse, you can use initializer-list to call an evaluation function that accumulates index from left to right. The function object called in the initalizer-list should have a non-void return type :
#include <cstddef>
#include <iostream>
using namespace std;
template<size_t N>
class TensorIndex {
public:
template<typename... Args>
TensorIndex(Args... args) : dims{static_cast<size_t>(args)...}
{
static_assert(sizeof...(Args) == N,
"incorrect number of arguments for TensorIndex constructor");
}
template<typename... Args>
size_t index(Args... args) {
static_assert(sizeof...(Args) == N,
"incorrect number of arguments for TensorIndex::index()");
IndexEval eval{dims};
Pass pass{eval(args)...}; // evaluate from left to right : initializer-list
return eval.get_res();
}
private:
const size_t dims[N];
class IndexEval {
size_t k = 0;
size_t res = 0;
const size_t* dims;
public:
IndexEval(const size_t* dims) : dims{dims} {}
size_t operator()(size_t i) {
return res = res * dims[k++] + i;
}
size_t get_res() const { return res; }
};
struct Pass {
template<typename... Args> Pass(Args...) {}
};
};
int main()
{
TensorIndex<3> g(10, 10, 10);
cout << g.index(1, 2, 3) << endl;
}
I have found this piece of code in other topic
template<size_t N, size_t I = 0>
struct hash_calc
{
static constexpr size_t apply(const char(&s)[N])
{
return (hash_calc < N, I + 1 >::apply(s) ^ s[I]) * 16777619u;
};
};
template<size_t N>
struct hash_calc<N, N>
{
static constexpr size_t apply(const char(&s)[N])
{
return 2166136261u;
};
};
template<size_t N>
constexpr size_t hash(const char(&s)[N])
{
return hash_calc<N>::apply(s);
}
First of all, I am totally confused why it does not end being an infinite recursive call? From what I understand, first hash_calc<N, I> is always going to call itself, what causes it to break at the hash_calc<N, N> when I reaches N? The template instantiation for hash_calc<N, I> does not fail when N == I.
Second - i tried to copy paste the hash_calc<N, N> struct over hash_calc<N, I> which causes compile error error: 'hash_calc' is not a class template struct hash_calc<N, N>. Why is that?!
Edit:
The modified code below does not compile under msvc nor gcc. I have just put one template over other. What happened?
template<size_t N>
struct hash_calc<N, N>
{
static constexpr size_t apply(const char(&s)[N])
{
return 2166136261u;
};
};
template<size_t N, size_t I = 0>
struct hash_calc
{
static constexpr size_t apply(const char(&s)[N])
{
return (hash_calc < N, I + 1 >::apply(s) ^ s[I]) * 16777619u;
};
};
template<size_t N>
constexpr size_t hashq(const char(&s)[N])
{
return hash_calc<N>::apply(s);
}
I am totally confused why it does not end being an infinite recursive call?
So let's look at it, step by step.
hashq<N>(const char(&s)[N]) calls hash_calc<N>::apply(s);.
That's:
template<size_t N, size_t I = 0>
struct hash_calc
{
static constexpr size_t apply(const char(&s)[N])
{
return (hash_calc < N, I + 1 >::apply(s) ^ s[I]) * 16777619u;
};
};
with I==0.
Now hash_calc < N, I + 1 >::apply(s) is called, with I==1, this goes on until I==N. That's when
template<size_t N>
struct hash_calc<N, N>
{
static constexpr size_t apply(const char(&s)[N])
{
return 2166136261u;
};
};
is called. And the recursion ends.
Why is that?!
template<size_t N, size_t I = 0>
struct hash_calc
{
static constexpr size_t apply(const char(&s)[N])
{
return (hash_calc < N, I + 1 >::apply(s) ^ s[I]) * 16777619u;
};
};
is the more general case, it handles any pair of <N,I>, whereas
template<size_t N>
struct hash_calc<N, N>
{
static constexpr size_t apply(const char(&s)[N])
{
return 2166136261u;
};
};
is its specialisation, it can only handle the case of <N,N>.
The compiler first needs the general case and then the specialiation.
So i have something like that:
template<unsigned int W,unsigned int H>
class Class
{
int data[W][H];
Class(const (&_data)[W][H])
{
for (int x=0;x<W;x++)
for (int y=0;y<H;y++)
data[x][y] = _data[x][y];
}
template<class... args>
Class()
{
/// black magic
}
}
What could i replace the "black magic", so the second constructor will accept W*H ints?
Example:
Class<3,2> class1(1,2,3,4,5,6);
There are alternative answers which might be more practical and simpler to implement, but I'll show how you could actually do this with a compile-time for-loop for purposes of demonstrating black-magic.
Here is a compile-time for-loop.
/* Compile-time for-loop up to N. */
template <std::size_t N>
struct For {
/* Call f<I>(args...) N times. */
template <typename F, typename... Args>
void operator()(F &&f, Args &&... args) const {
Impl<0, N>()(std::forward<F>(f), std::forward<Args>(args)...);
}
private:
/* Forward declaration. */
template <std::size_t I, std::size_t End>
struct Impl;
/* Base case. Do nothing. */
template <std::size_t End>
struct Impl<End, End> {
template <typename F, typename... Args>
void operator()(F &&, Args &&...) const { /* Do nothing. */ }
}; // Impl<End, End>
/* Recursive case. Call f<I>(args...), then recurse into next step. */
template <std::size_t I, std::size_t End>
struct Impl {
template <typename F, typename... Args>
void operator()(F &&f, Args &&... args) const {
std::forward<F>(f).template operator()<I>(std::forward<Args>(args)...);
Impl<I + 1, End>()(std::forward<F>(f), std::forward<Args>(args)...);
}
}; // Impl<I, End>
}; // For<N>
Here is a simple use case of it.
struct Print {
template <std::size_t I>
void operator()(int x, int y) const {
std::cout << "Iteration " << I << ": " << x << ' ' << y << std::endl;
}
}; // Print
For<3>()(Print(), 1, 2);
Outputs
Iteration 0: 1 2
Iteration 1: 1 2
Iteration 2: 1 2
Now with this we can nest this compile-time for-loop just like how we could nest a run-time for-loop. Here is the Matrix class using this For<> template.
/* Defines an M by N Matrix, (Row by Col). */
template <std::size_t M, std::size_t N>
class Matrix {
public:
/* Our underlying M by N matrix. */
using Data = std::array<std::array<int, N>, M>;
/* Construct off of M * N arguments. */
template <typename... Args>
Matrix(Args &&... args) {
static_assert(sizeof...(Args) == M * N,
"The number of arguments provided must be M * N.");
ForEach(AssignImpl(),
data_,
std::forward_as_tuple(std::forward<Args>(args)...));
}
/* Print each element out to std::cout. */
void Write(std::ostream &strm) const {
ForEach(WriteImpl(), strm, data_);
}
private:
/* Our outer for loop. Call InnerFor() M times.
Resembles: 'for (std::size_t i = 0 ; i < M; ++i) {' */
template <typename F, typename... Args>
void ForEach(F &&f, Args &&... args) const {
For<M>()(InnerFor(), std::forward<F>(f), std::forward<Args>(args)...);
}
/* Our inner for loop. Call ForBody() N times.
Resembles: 'for (std::size_t j = 0; j < N; ++j) {' */
struct InnerFor {
template <std::size_t I, typename F, typename... Args>
void operator()(F &&f, Args &&... args) const {
For<N>()(ForBody<I>(),
std::forward<F>(f),
std::forward<Args>(args)...);
}
}; // InnerFor
/* The body of our for loop. Call f<I, J>(args...); */
template <std::size_t I>
struct ForBody {
template <std::size_t J, typename F, typename... Args>
void operator()(F &&f, Args &&... args) const {
std::forward<F>(f)
.template operator()<I, J>(std::forward<Args>(args)...);
}
}; // ForBody<I>
/* Given the M by N array and a tuple of length M * N, assign the array. */
struct AssignImpl {
template <std::size_t I, std::size_t J, typename Arg>
void operator()(Data &data, Arg &&arg) const {
data[I][J] = std::get<I * N + J>(std::forward<Arg>(arg));
}
}; // AssignImpl
/* Given an output stream and our data, print the data at (I, J). */
struct WriteImpl {
template <std::size_t I, std::size_t J>
void operator()(std::ostream &strm, const Data &data) const {
strm << data[I][J] << std::endl;
}
}; // WriteImpl
/* Our underlying M by N array. */
Data data_;
}; // Matrix
Here is a quick demonstration of construction and writing to std::cout.
int main() {
Matrix<3, 2> matrix{101, 102,
201, 202,
301, 302};
matrix.Write(std::cout);
}
First of all, there's some syntax errors in your example as missing semicolon after class declaration and the constructors being private.
Apart from that, if you want to store the numbers in row-major order, then you should declare your matrix/2d-array as int data[H][W] (height first, then width).
To store the values from a variadic pack you can simply expand them inside the ctor of a container, e.g. std::array, and make use of list-initialization.
template <typename... Args>
Class(Args&&... args) {
const std::array<int, H * W> temp{std::forward<Args>(args)...};
/* ... */
};
I've also used universal references and perfect forwarding to preserve the reference type of the pack.
To populate your 2d-array you simply have to iterate over all elements in temp and store them in the member array.
for (std::size_t h = 0; h != H; ++h)
for (std::size_t w = 0; w != W; ++w)
data[h][w] = temp[h * W + w];
See a full example here
This works:
#include <array>
#include <utility>
#include <iostream>
template<unsigned W,unsigned H>
struct Foo
{
std::array<std::array<int, H>, W> data;
template<typename... Args>
Foo(Args&&... args):
data{ std::forward<Args>(args)... }
{}
};
int main()
{
Foo<2,2> x(1,2,3,4);
for( auto&& a : x.data ) {
for( unsigned z : a ) {
std::cout << z << ",";
}
std::cout << "\n";
}
but exposes the order of storage in your inner array.
std::array is a syntactic sugar wrapped around raw C arrays.
you could use std::initializer_list as a constructor parameter, since your array only is of type int[][].
Class(std::initializer_list<int> il)
{
if(il.size() < W*H)
throw string("Insufficient elements");//if lesser no. of elements are given.
auto it = il.begin();// iterator to the first element of the list.
for(int i =0;i< W;++i)
{
for(int j =0; j < H;++j)
{
data[i][j] = *it++;// increment the iterator
}
}
}
call site will look like this:
Class<3,2> c{1,2,3,4,5,6};
or
Class<3,2> c = {1,2,3,4,5,6};
if more elements are given then extras are discarded. initializer_list can give type safety, and will give you a diagnostic if narrowing is found.