C++-equivalent to incompletely initialized arrays in C? - c++

When I transform code from C to C++ I sometimes encounter language constructs that are C, but compatible with C++. Usually I want to transform the code in the least intrusive way. But I have one case where I find that very difficult:
In C you can declare an array and initializing... well... parts of it using "designators", the rest is zeroed out (Edit: I wrote "left to randomness" here, first):
int data[7] = {
[2] = 7,
[4] = 9,
};
This is not valid C++-code, though (luckily). So I will have to use a different strategy.
While I can see a non-intrusive way in C++11:
static const map<int,int> data = { {2,7}, {4,9} };
what should I do when C++11-features are not available yet?
Can I circumvent a runtime initialization?
Is there a way to initialize similar kind of mapping in a "literal" way?
What is least intrusive to the code that uses data?

Well unless the size of the array is totally insane, you can always do this
int data[7] = {
0,
0,
7, // #2
0,
9 // #4
// the rest will be 0-initialized
};
Works in compile time too

If uniform initialization is not available, the std::map<int, int> could be initialized using boost::assign::map_list_of:
#include <boost/assign/list_of.hpp>
static const std::map<int,int> data = boost::assign::map_list_of(2,7)(4,9);

Rather than using map<int, int> you can backport std:array (or a minimal equivalent) from C++11, and use a Boost.Assign-style builder facility:
#include <cstddef>
template<typename T, size_t N> struct array { T data[N]; };
template<typename T, size_t N> struct build_array: public array<T, N> {
build_array &operator()(size_t i, const T &t) {
this->data[i] = t;
return *this;
}
};
array<int, 7> data_array = build_array<int, 7>()(2, 7)(4, 9);
int (&data)[7] = data_array.data;

Why can't you do:
int data[7];
data[2] = 7;
data[4] = 9;
looks very-very similar =)

If you do not want to use boost::assign
You can create it's simple analogue:
template<class T1, class T2>
std::map<T1, T2> cre(std::map<T1, T2> & m)
{
return std::map<T1, T2>();
}
template<class T1, class T2>
std::map<T1, T2> & ass(std::map<T1, T2> & m, T1 const & p1, T2 const & p2)
{
m[p1] = p2;
return m;
}
std::map<int, int> data = ass(ass(cre(data), 2, 3), 7, 6);

Related

Implementing an efficient and convenient constructor for a C++ vector class template

I'm trying to implement a constructor for a C++ vector class template that is both efficient and convenient to use. The latter is, of course, somewhat subjective — I'm aiming at something like Vec2D myVec = Vec2D({1.0, 2.0}).
To start with, I'm thinking about a class template for fixed-length vectors, so no immediate use for std::vector I'd say. With template <typename T, unsigned short n>, two options to store the elements of the vector would be T mElements[n] or std::array<T, n> mElements. I would go with the latter (same storage and some added benefits compared to the former).
Now, on to the constructor (and the question) — what should be its parameter? The following options come to mind:
Using std::array<T, n> initElements would require the use of double curved brackets for initialisation as it is an aggregate, i.e. Vec2D myVec = Vec2D({{1.0, 2.0}}). Omitting the outer curly brackets might still compile, though results in a warning. Additionally, if we were to generalise this to a 2D array, e.g. for a matrix class template, it would require quadruple curved brackets (or triple when omitting the outer pair again, taking a warning for granted). Not so convenient.
Using T initElems[] would require e.g. double dElems[2] = {1.0, 2.0} followed by Vec2D myVec = Vec2D(dElems), it is not possible to directly pass {1.0, 2.0} as argument. Not so convenient.
Using std::initializer_list<T>, which would allow Vec2D myVec{1.0, 2.0}. This also nicely generalises to a 2D array. However, I don't see how one would use this as a constructor when overloading operators, say operator +.
Using std::vector<T>. This allows the use of Vec2D myVec = Vec2D({1.0, 2.0}), nicely generalises to 2D arrays, and is easy to use in overloaded operators. However, it does not seem very efficient.
The (intentionally basic) code below reflects the last option. Are there alternatives which are more efficient, without losing convenience?
template <typename T, unsigned short n>
class Vector {
public:
std::array<T, n> mElements;
// Constructor
Vector(std::vector<T> initElements) {
for (unsigned short k = 0; k < n; k++) {
mElements[k] = initElements[k];
}
}
// Overloaded operator +
Vector operator + (const Vector& rightVector) const {
std::vector<T> sumVec(n);
for (unsigned short k = 0; k < n; k++) {
sumVec[k] = mElements[k] + rightVector.mElements[k];
}
return Vector(sumVec);
}
};
With the usage
using Vec2D = Vector<double, 2>;
Vec2D myVec = Vec2D({1.0, 2.0});
You could also make use of parameter packs, which (combined with some nice polymorphism) can enable you to do stuff like this:
Vector<int, 3> v1{std::vector<int>{1, 2, 3}};
Vector<double, 3> v2 = {5., 6., 7.};
Vector<float, 3> v3 = v1 + v2;
Where Vector is defined as follows:
#include <vector>
#include <array>
#include <algorithm>
#include <cassert>
template <typename T, size_t n>
struct Vector : std::array<T, n>
{
/* Default constructor, needed as recursion endpoint */
Vector() = default;
/* Recursive constructor that takes an arbitrary number of arguments
* Dangerous: only adds the last n arguments */
template <typename... Ts>
Vector(T v, Ts... args) : Vector(args...) { addItem(v); };
/* Vector constructor that takes an arbitrary std::vector v
* Dangerous: only adds the first n items */
template <typename T2>
explicit Vector(const std::vector<T2>& v) : added{std::min(v.size(), n)}
{
assert(v.size() == n);
for (size_t i = 0; i < added; i++)
{
(*this)[i] = (T)v[i];
}
}
/* Copy constructor: takes a Vector of any type, but of the same length */
template <typename T2>
Vector(const std::array<T2, n>& v) : added{n}
{
for (size_t i = 0; i < added; i++)
{
(*this)[i] = (T)v[i];
}
}
/* Example of a polymorphic addition function */
template <typename T2>
Vector<T, n> operator+(const Vector<T2, n>& v)
{
Vector<T, n> vr{*this};
for (size_t i = 0; i < n; i++)
{
vr[i] += (T)v[i];
}
return vr;
}
private:
size_t added{0};
/* Needed for recursive constructor */
void addItem(const T& t)
{
added++;
if (added <= n) { (*this)[n - added] = t; }
else { assert(false); }
}
};
The most convienient way to make this would be to use a deduction guide, changing your given usage example:
using Vec2D = Vector<double, 2>;
Vec2D myVec = Vec2D({1.0, 2.0});
into something much simpler:
Vector<double, 2> myVec = { 1.0, 2.0 };
// or even
// Vector myVec = { 1.0, 2.0 };
Enabling a usage similar to std::array.
Arguably the easiest, and most efficient way to create a statically sized vector that allows this would be to use a non-standard C++ __attriubute__.
Templating the T __attribute__((vector_size(N))), we end up with the following:
using ushort = unsigned short;
template <typename T, ushort N>
using Vector = T __attribute__((
vector_size(sizeof(T) * N) // the number of bytes in a single `T`, multiplied by the number of elements, `N`
));
int main() {
using vector_t = Vector<double const, 2>;
vector_t x = { 1.0, 2.0 };
vector_t y = { 9.0, 3.0 };
vector_t z = x + y;
std::wcout << z[0] << ", " << z[1] << '\n'; // "10, 5\n"
}
Okay, okay, I'm joking, don't do that, let's not touch upon the world of attributes, compiler extensions, and nonportable code.
The simplest way to make it as convenient to use as the underlying std::array, would be to either, create a type deduction guide, inherit from std::array, or to not implement a constructor, the latter two allow the std::array constructor to take effect, as the class has no constructor on its own.
I think, in this case, you might be able to simply inherit from std::array, without pissing off every C++ developer on SO.
Something along the lines of:
#include <iostream>
#include <vector>
#include <array>
using u16 = unsigned short const;
template <typename T, u16 N>
struct Vector : std::array<T, N> {
Vector<T, N> operator + (Vector<T, N> & rightVector) {
decltype(auto) self = *this;
Vector<T, N> sumVec;
for ( ushort i = 0; i < N; ++i ) {
sumVec[i] = self[i] + rightVector[i];
}
return sumVec;
}
Vector operator += (Vector const& rightVector) {
decltype(auto) self = *this;
for ( ushort i = 0; i < N; ++i ) {
self[i] += rightVector[i];
}
return this;
}
Vector operator ++ () {
decltype(auto) self = *this;
for ( T& item : self ) {
++item;
}
return this;
}
Vector operator ++ (int const) {
decltype(auto) self = *this;
Vector temporary = self;
++self;
return temporary;
}
Vector operator - (Vector const& rightVector) const {
decltype(auto) self = *this;
Vector<T, N> sumVec;
for ( ushort i = 0; i < N; ++i ) {
sumVec[i] = self[i] - rightVector[i];
}
return Vector(sumVec);
}
};
int main() {
using vector_t = Vector<double, 2>;
vector_t x = { 1.0, 2.0 };
vector_t y = { 9.0, 3.0 };
vector_t z = x + y; // { 10.0. 4.0 }
std::wcout << z[0] << ", " << z[1] << '\n';
}
Although, you might want to use a few std::enable_ifs or assertions to make sure that you only create Vectors of a numerical type, as that seems to be what you want to use.
This should have the same memory usage as a std::array. It doesn't initialize any extra types, in contrast to your example that had constructed std::vectors everywhere.
Does this fit your intended usage and goals?
You could directly use the parameters to initialise the base class
Untested code.
template <typename... Args>
Vector(Args...&& args) : mEVector(std::forward<Args>(args)...) { };

constexpr vector push_back or how to constexpr all the things

There is a good talk by Jason Turner and Ben Deane from C++Now 2017 called "Constexpr all the things" which also gives a constexpr vector implementation. I was dabbling with the idea myself, for educational purposes. My constexpr vector was pure in the sense that pushing back to it would return a new vector with added element.
During the talk, I saw a push_back implementation tat looks like more or less following:
constexpr void push_back(T const& e) {
if(size_ >= Size)
throw std::range_error("can't use more than Size");
else {
storage_[size_++] = e;
}
}
They were taking the element by value and moving it but, I don't think this is the source of my problems. The thing I want to know is, how this function could be used in a constexpr context? This is not a const member function, it modifies the state. I don think it is possible to do something like
constexpr cv::vector<int> v1;
v1.push_back(42);
And if this is not possible, how could we use this thing in constexpr context and achieve the goal of the task using this vector, namely compile-time JSON parsing?
Here is my version, so that you can see both my new vector returning version and the version from the talk. (Note that performance, perfect forwarding etc. concerns are omitted)
#include <cstdint>
#include <array>
#include <type_traits>
namespace cx {
template <typename T, std::size_t Size = 10>
struct vector {
using iterator = typename std::array<T, Size>::iterator;
using const_iterator = typename std::array<T, Size>::const_iterator;
constexpr vector(std::initializer_list<T> const& l) {
for(auto& t : l) {
if(size_++ < Size)
storage_[size_] = std::move(t);
else
break;
}
}
constexpr vector(vector const& o, T const& t) {
storage_ = o.storage_;
size_ = o.size_;
storage_[size_++] = t;
}
constexpr auto begin() const { return storage_.begin(); }
constexpr auto end() const { return storage_.begin() + size_; }
constexpr auto size() const { return size_; }
constexpr void push_back(T const& e) {
if(size_ >= Size)
throw std::range_error("can't use more than Size");
else {
storage_[size_++] = e;
}
}
std::array<T, Size> storage_{};
std::size_t size_{};
};
}
template <typename T>
constexpr auto make_vector(std::initializer_list<T> const& l) {
return cx::vector<int>{l};
}
template <typename T>
constexpr auto push_back(cx::vector<T> const& o, T const& t) {
return cx::vector<int>{o, t};
}
int main() {
constexpr auto v1 = make_vector({1, 2, 3});
static_assert(v1.size() == 3);
constexpr auto v2 = push_back(v1, 4);
static_assert(v2.size() == 4);
static_assert(std::is_same_v<decltype(v1), decltype(v2)>);
// v1.push_back(4); fails on a constexpr context
}
So, this thing made me realize there is probably something deep that I don' know about constexpr. So, recapping the question; how such a constexpr vector could offer a mutating push_back like that in a constexpr context? Seems like it is not working in a constexpr context right now. If push_back in a constexpr context is not intended to begin with, how can you call it a constexpr vector and use it for compile-time JSON parsing?
Your definition of vector is correct, but you can't modify constexpr objects. They are well and truly constant. Instead, do compile-time calculations inside constexpr functions (the output of which can then be assigned to constexpr objects).
For example, we can write a function range, which produces a vector of numbers from 0 to n. It uses push_back, and we can assign the result to a constexpr vector in main.
constexpr vector<int> range(int n) {
vector<int> v{};
for(int i = 0; i < n; i++) {
v.push_back(i);
}
return v;
}
int main() {
constexpr vector<int> v = range(10);
}
Your return cx::vector<int>{o, t}; will produce a compilation error when o and t are of types cx::vector<T> and T respectively, because those are different types, while all elements of std::initializer_list<T> should be of same type (o is not expanded into a list of its elements).
If you're merely after your 'pure' implementation of push_back, then you can make do with standard arrays:
#include <array>
template <typename T, std::size_t N>
constexpr auto push_back(std::array<T, N> const& oldArr, T const& el) {
std::array<T, N+1> newArr{};
std::copy(begin(oldArr), end(oldArr), begin(newArr));
newArr[N] = el;
return newArr;
}
int main() {
constexpr auto a1 = std::to_array({1, 2, 3});
static_assert(a1.size() == 3);
constexpr auto a2 = push_back(a1, 4);
static_assert(a2.size() == 4);
// This assert will still fail though, because push_back's implementation
// above not only returns new array, but also a new type.
// For example, std::array<int, 3> is not the same type as std::array<int, 4>
//static_assert(std::is_same_v<decltype(a1), decltype(a2)>);
}

Nesting variadic template

I have two classes that utilize initialize lists. One is a Vector styled class that hold a list of values.
Vec.h:
template< typename T, int nDimensions = 2 >
class Vec{
private:
std::array< T, nDimensions > elements_;
public:
template <typename... U>
Vec(U... ts) : elements_{ ts... } {}
}
Then when used:
typedef Vec< int, 2 > Vec2i;
Vec2i twoi = { 1,2 };
Vec2i twoi2 = twoi; //I have copy constructor and other methods finished.
This class works all fine and dandy, however when I try to use this class with my Matrix styled class, I cant figure out the correct syntax for its constructor.
template< typename T, int X,int Y>
class Mat {
private:
std::array< Vec<T, X>, Y > elements_;
}
Id like to use it like so:
typedef Mat<int, 3,3> Mat3i;
Mat3i threemat = { {1,2,3},
{4,5,6},
{7,8,9}};
Now, iv tried using initializer lists as the constructor with some success, but I cant figure out the syntax to pass the sub lists along.
Mat(std::initializer_list<Vec<T, X>> values) {
for (auto& t : values) {
//What goes here?
}
}
Iv also tried iteration over the list and assigning them manually, but thats a no go.
Its also worth noting that its important that these classes have consecutive chunks of memory for there lists, and no other variables. Otherwise id be using other types instead of std::array. (For casting and union purposes.)
I'm debating if I need to reinterpret cast each value as a Vec then copy over the values.
As alternative:
Mat(std::initializer_list<Vec<T, X>> values) {
std::copy(values.begin(), values.end(), elements_.begin());
}
I'm an idiot.
Mat(std::initializer_list<Vec<T, X>> values) {
int i = 0;
for (auto& t : values) {
elements_[i++] = t;
}
}

Clean ways to write multiple 'for' loops

For an array with multiple dimensions, we usually need to write a for loop for each of its dimensions. For example:
vector< vector< vector<int> > > A;
for (int k=0; k<A.size(); k++)
{
for (int i=0; i<A[k].size(); i++)
{
for (int j=0; j<A[k][i].size(); j++)
{
do_something_on_A(A[k][i][j]);
}
}
}
double B[10][8][5];
for (int k=0; k<10; k++)
{
for (int i=0; i<8; i++)
{
for (int j=0; j<5; j++)
{
do_something_on_B(B[k][i][j]);
}
}
}
You see this kind of for-for-for loops in our code frequently. How do I use macros to define the for-for-for loops so that I don't need to re-write this kind of code every time? Is there a better way to do this?
The first thing is that you don't use such a data structure. If
you need a three dimensional matrix, you define one:
class Matrix3D
{
int x;
int y;
int z;
std::vector<int> myData;
public:
// ...
int& operator()( int i, int j, int k )
{
return myData[ ((i * y) + j) * z + k ];
}
};
Or if you want to index using [][][], you need an operator[]
which returns a proxy.
Once you've done this, if you find that you constantly have to
iterate as you've presented, you expose an iterator which will
support it:
class Matrix3D
{
// as above...
typedef std::vector<int>::iterator iterator;
iterator begin() { return myData.begin(); }
iterator end() { return myData.end(); }
};
Then you just write:
for ( Matrix3D::iterator iter = m.begin(); iter != m.end(); ++ iter ) {
// ...
}
(or just:
for ( auto& elem: m ) {
}
if you have C++11.)
And if you need the three indexes during such iterations, it's
possible to create an iterator which exposes them:
class Matrix3D
{
// ...
class iterator : private std::vector<int>::iterator
{
Matrix3D const* owner;
public:
iterator( Matrix3D const* owner,
std::vector<int>::iterator iter )
: std::vector<int>::iterator( iter )
, owner( owner )
{
}
using std::vector<int>::iterator::operator++;
// and so on for all of the iterator operations...
int i() const
{
((*this) - owner->myData.begin()) / (owner->y * owner->z);
}
// ...
};
};
Using a macro to hide the for loops can be a lot confusing, just to save few characters. I'd use range-for loops instead:
for (auto& k : A)
for (auto& i : k)
for (auto& j : i)
do_something_on_A(j);
Of course you can replace auto& with const auto& if you are, in fact, not modifying the data.
Something like this can help:
template <typename Container, typename Function>
void for_each3d(const Container &container, Function function)
{
for (const auto &i: container)
for (const auto &j: i)
for (const auto &k: j)
function(k);
}
int main()
{
vector< vector< vector<int> > > A;
for_each3d(A, [](int i){ std::cout << i << std::endl; });
double B[10][8][5] = { /* ... */ };
for_each3d(B, [](double i){ std::cout << i << std::endl; });
}
In order to make it N-ary we need some template magic. First of all we should create SFINAE structure to distinguish whether this value or container. The default implementation for values, and specialisations for arrays and each of the container types. How #Zeta notes, we can determine the standard containers by the nested iterator type (ideally we should check whether the type can be used with range-base for or not).
template <typename T>
struct has_iterator
{
template <typename C>
constexpr static std::true_type test(typename C::iterator *);
template <typename>
constexpr static std::false_type test(...);
constexpr static bool value = std::is_same<
std::true_type, decltype(test<typename std::remove_reference<T>::type>(0))
>::value;
};
template <typename T>
struct is_container : has_iterator<T> {};
template <typename T>
struct is_container<T[]> : std::true_type {};
template <typename T, std::size_t N>
struct is_container<T[N]> : std::true_type {};
template <class... Args>
struct is_container<std::vector<Args...>> : std::true_type {};
Implementation of for_each is straightforward. The default function will call function:
template <typename Value, typename Function>
typename std::enable_if<!is_container<Value>::value, void>::type
rfor_each(const Value &value, Function function)
{
function(value);
}
And the specialisation will call itself recursively:
template <typename Container, typename Function>
typename std::enable_if<is_container<Container>::value, void>::type
rfor_each(const Container &container, Function function)
{
for (const auto &i: container)
rfor_each(i, function);
}
And voila:
int main()
{
using namespace std;
vector< vector< vector<int> > > A;
A.resize(3, vector<vector<int> >(3, vector<int>(3, 5)));
rfor_each(A, [](int i){ std::cout << i << ", "; });
// 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
std::cout << std::endl;
double B[3][3] = { { 1. } };
rfor_each(B, [](double i){ std::cout << i << ", "; });
// 1, 0, 0, 0, 0, 0, 0, 0, 0,
}
Also this will not work for pointers (arrays allocated in heap).
Most of the answers simply demonstrate how C++ can be twisted into incomprehensible syntactic extensions, IMHO.
By defining whatever templates or macros, you just force other programmers to understand bits of obfuscated code designed to hide other bits of obfuscated code.
You will force every guy who reads your code to have template expertise, just to avoid doing your job of defining objects with clear semantics.
If you decided to use raw data like 3 dimensional arrays, just live with it, or else define a class that gives some understandable meaning to your data.
for (auto& k : A)
for (auto& i : k)
for (auto& current_A : i)
do_something_on_A(current_A);
is just consistent with the cryptic definition of a vector of vector of vector of int with no explicit semantics.
#include "stdio.h"
#define FOR(i, from, to) for(int i = from; i < to; ++i)
#define TRIPLE_FOR(i, j, k, i_from, i_to, j_from, j_to, k_from, k_to) FOR(i, i_from, i_to) FOR(j, j_from, j_to) FOR(k, k_from, k_to)
int main()
{
TRIPLE_FOR(i, j, k, 0, 3, 0, 4, 0, 2)
{
printf("i: %d, j: %d, k: %d\n", i, j, k);
}
return 0;
}
UPDATE: I know, that you asked for it, but you'd better not use that :)
One idea is to write an iterable pseudo-container class that "contains" the set of all multi-index tuples you'll index over. No implementation here because it'll take too long but the idea is that you should be able to write...
multi_index mi (10, 8, 5);
// The pseudo-container whose iterators give {0,0,0}, {0,0,1}, ...
for (auto i : mi)
{
// In here, use i[0], i[1] and i[2] to access the three index values.
}
I see many answers here that work recursively, detecting if the input is a container or not. Instead, why not detect if the current layer is the same type as the function takes? It's far simpler, and allows for more powerful functions:
//This is roughly what we want for values
template<class input_type, class func_type>
void rfor_each(input_type&& input, func_type&& func)
{ func(input);}
//This is roughly what we want for containers
template<class input_type, class func_type>
void rfor_each(input_type&& input, func_type&& func)
{ for(auto&& i : input) rfor_each(i, func);}
However, this (obviously) gives us ambiguity errors. So we use SFINAE to detect if the current input fits in the function or not
//Compiler knows to only use this if it can pass input to func
template<class input_type, class func_type>
auto rfor_each(input_type&& input, func_type&& func) ->decltype(func(input))
{ return func(input);}
//Otherwise, it always uses this one
template<class input_type, class func_type>
void rfor_each(input_type&& input, func_type&& func)
{ for(auto&& i : input) rfor_each(i, func);}
This now handles the containers correctly, but the compiler still considers this ambiguous for input_types that can be passed to the function. So we use a standard C++03 trick to make it prefer the first function over the second, of also passing a zero, and making the one we prefer accept and int, and the other takes ...
template<class input_type, class func_type>
auto rfor_each(input_type&& input, func_type&& func, int) ->decltype(func(input))
{ return func(input);}
//passing the zero causes it to look for a function that takes an int
//and only uses ... if it absolutely has to
template<class input_type, class func_type>
void rfor_each(input_type&& input, func_type&& func, ...)
{ for(auto&& i : input) rfor_each(i, func, 0);}
That's it. Six, relatively simple lines of code, and you can iterate over values, rows, or any other sub-unit, unlike all of the other answers.
#include <iostream>
int main()
{
std::cout << std::endl;
double B[3][3] = { { 1.2 } };
rfor_each(B[1], [](double&v){v = 5;}); //iterate over doubles
auto write = [](double (&i)[3]) //iterate over rows
{
std::cout << "{";
for(double d : i)
std::cout << d << ", ";
std::cout << "}\n";
};
rfor_each(B, write );
};
Proof of compilation and execution here and here
If you wanted a more convenient syntax in C++11, you could add a macro. (Following is untested)
template<class container>
struct container_unroller {
container& c;
container_unroller(container& c_) :c(c_) {}
template<class lambda>
void operator <=(lambda&& l) {rfor_each(c, l);}
};
#define FOR_NESTED(type, index, container) container_unroller(container) <= [](type& index)
//note that this can't handle functions, function pointers, raw arrays, or other complex bits
int main() {
double B[3][3] = { { 1.2 } };
FOR_NESTED(double, v, B) {
std::cout << v << ", ";
}
}
I caveat this answer with the following statement: this would only work if you were operating on an actual array - it wouldn't work for your example using std::vector.
If you are performing the same operation on every element of a multi-dimensional array, without caring about the position of each item, then you can take advantage of the fact that arrays are placed in contiguous memory locations, and treat the whole thing as one big one-dimensional array. For example, if we wanted to multiply every element by 2.0 in your second example:
double B[3][3][3];
// ... set the values somehow
double* begin = &B[0][0][0]; // get a pointer to the first element
double* const end = &B[3][0][0]; // get a (const) pointer past the last element
for (; end > begin; ++begin) {
(*begin) *= 2.0;
}
Note that using the above approach also allows the use of some "proper" C++ techniques:
double do_something(double d) {
return d * 2.0;
}
...
double B[3][3][3];
// ... set the values somehow
double* begin = &B[0][0][0]; // get a pointer to the first element
double* end = &B[3][0][0]; // get a pointer past the last element
std::transform(begin, end, begin, do_something);
I don't generally advise this approach (preferring something like Jefffrey's answer), as it relies on having defined sizes for your arrays, but in some cases it can be useful.
I was kind of shocked that no one proposed some arithmetic-magic based loop to do the work. Since C. Wang is looking for a solution with no nested loops, I'll propose one:
double B[10][8][5];
int index = 0;
while (index < (10 * 8 * 5))
{
const int x = index % 10,
y = (index / 10) % 10,
z = index / 100;
do_something_on_B(B[x][y][z]);
++index;
}
Well, this approach isn't elegant and flexible, so we could pack all the process into a template function:
template <typename F, typename T, int X, int Y, int Z>
void iterate_all(T (&xyz)[X][Y][Z], F func)
{
const int limit = X * Y * Z;
int index = 0;
while (index < limit)
{
const int x = index % X,
y = (index / X) % Y,
z = index / (X * Y);
func(xyz[x][y][z]);
++index;
}
}
This template function can be expressed in the form of nested loops as well:
template <typename F, typename T, int X, int Y, int Z>
void iterate_all(T (&xyz)[X][Y][Z], F func)
{
for (auto &yz : xyz)
{
for (auto &z : yz)
{
for (auto &v : z)
{
func(v);
}
}
}
}
And can be used providing a 3D array of arbitrary size plus the function name, letting the parameter deduction do the hard work of counting the size of each dimension:
int main()
{
int A[10][8][5] = {{{0, 1}, {2, 3}}, {{4, 5}, {6, 7}}};
int B[7][99][8] = {{{0, 1}, {2, 3}}, {{4, 5}, {6, 7}}};
iterate_all(A, do_something_on_A);
iterate_all(B, do_something_on_B);
return 0;
}
Towards more generic
But once again, it lacks of flexibility 'cause it only works for 3D arrays, but using SFINAE we can do the work for arrays of an arbitrary dimension, first we need a template function which iterates arrays of rank 1:
template<typename F, typename A>
typename std::enable_if< std::rank<A>::value == 1 >::type
iterate_all(A &xyz, F func)
{
for (auto &v : xyz)
{
func(v);
}
}
And another one which iterates arrays of any rank, doing the recursion:
template<typename F, typename A>
typename std::enable_if< std::rank<A>::value != 1 >::type
iterate_all(A &xyz, F func)
{
for (auto &v : xyz)
{
iterate_all(v, func);
}
}
This allows us to iterate all the elements in all the dimensions of a arbitrary-dimensions arbitrary-sized array.
Working with std::vector
For the multiple nested vector, the solution ressembles the one of arbitrary-dimensions arbitrary-sized array, but without SFINAE: First we will need a template function that iterates std::vectors and calls the desired function:
template <typename F, typename T, template<typename, typename> class V>
void iterate_all(V<T, std::allocator<T>> &xyz, F func)
{
for (auto &v : xyz)
{
func(v);
}
}
And another template function that iterates any kind of vector of vectors and calls himself:
template <typename F, typename T, template<typename, typename> class V>
void iterate_all(V<V<T, std::allocator<T>>, std::allocator<V<T, std::allocator<T>>>> &xyz, F func)
{
for (auto &v : xyz)
{
iterate_all(v, func);
}
}
Regardless of the nesting level, iterate_all will call the vector-of-vectors version unless the vector-of-values version is a better match thus ending the recursivity.
int main()
{
using V0 = std::vector< std::vector< std::vector<int> > >;
using V1 = std::vector< std::vector< std::vector< std::vector< std::vector<int> > > > >;
V0 A0 = {{{0, 1}, {2, 3}}, {{4, 5}, {6, 7}}};
V1 A1 = {{{{{9, 8}, {7, 6}}, {{5, 4}, {3, 2}}}}};
iterate_all(A0, do_something_on_A);
iterate_all(A1, do_something_on_A);
return 0;
}
I think that the function body is pretty simple and straight-forward... I wonder if the compiler could unroll this loops (I'm almost sure that most compilers could unroll the first example).
See live demo here.
Hope it helps.
Use something along these lines (its pseudo-code, but the idea stays the same). You extract the pattern to loop once, and apply a different function each time.
doOn( structure A, operator o)
{
for (int k=0; k<A.size(); k++)
{
for (int i=0; i<A[k].size(); i++)
{
for (int j=0; j<A[k][i].size(); j++)
{
o.actOn(A[k][i][j]);
}
}
}
}
doOn(a, function12)
doOn(a, function13)
Stick with the nested for loops!
All the methods suggested here have disadvantages in terms of either readability or flexibility.
What happens if you need to use the results of an inner loop for the processing in the outer loop? What happens if you need a value from the outer loop within your inner loop? Most of the "encapsulation" methods fail here.
Trust me I have seen several attempts to "clean up" nested for loops and in the end it turns out that the nested loop is actually the cleanest and most flexible solution.
One technique I've used is templates. E.g.:
template<typename T> void do_something_on_A(std::vector<T> &vec) {
for (auto& i : vec) { // can use a simple for loop in C++03
do_something_on_A(i);
}
}
void do_something_on_A(int &val) {
// this is where your `do_something_on_A` method goes
}
Then you simply call do_something_on_A(A) in your main code. The template function gets created once for each dimension, the first time with T = std::vector<std::vector<int>>, the second time with with T = std::vector<int>.
You could make this more generic using std::function (or function-like objects in C++03) as a second argument if you want:
template<typename T> void do_something_on_vec(std::vector<T> &vec, std::function &func) {
for (auto& i : vec) { // can use a simple for loop in C++03
do_something_on_vec(i, func);
}
}
template<typename T> void do_something_on_vec(T &val, std::function &func) {
func(val);
}
Then call it like:
do_something_on_vec(A, std::function(do_something_on_A));
This works even though the functions have the same signature because the first function is a better match for anything with std::vector in the type.
You could generate indices in one loop like this (A, B, C are dimensions):
int A = 4, B = 3, C = 3;
for(int i=0; i<A*B*C; ++i)
{
int a = i/(B*C);
int b = (i-((B*C)*(i/(B*C))))/C;
int c = i%C;
}
One thing you may want to try if you only have statements in the inner-most loop - and your concern is more about the overly verbose nature of the code - is to use a different whitespace scheme. This will only work if you can state your for loops compactly enough so that they all fit on one line.
For your first example, I would rewrite it as:
vector< vector< vector<int> > > A;
int i,j,k;
for(k=0;k<A.size();k++) for(i=0;i<A[k].size();i++) for(j=0;j<A[k][i].size();j++) {
do_something_on_A(A[k][i][j]);
}
This is kinda pushing it because you are calling functions in the outer loops which is equivalent to putting statements in them. I have removed all unnecessary white-space and it may be passible.
The second example is much better:
double B[10][8][5];
int i,j,k;
for(k=0;k<10;k++) for(i=0;i<8;i++) for(j=0;j<5;j++) {
do_something_on_B(B[k][i][j]);
}
This may be different whitespace convention than you like to use, but it achieves a compact result that nonetheless does not require any knowledge beyond C/C++ (such as macro conventions) and does not require any trickery like macros.
If you really want a macro, you could then take this a step further with something like:
#define FOR3(a,b,c,d,e,f,g,h,i) for(a;b;c) for(d;e;f) for(g;h;i)
which would change the second example to:
double B[10][8][5];
int i,j,k;
FOR3(k=0,k<10,k++,i=0,i<8,i++,j=0,j<5,j++) {
do_something_on_B(B[k][i][j]);
}
and the first example fares better too:
vector< vector< vector<int> > > A;
int i,j,k;
FOR3(k=0,k<A.size(),k++,i=0,i<A[k].size(),i++,j=0,j<A[k][i].size(),j++) {
do_something_on_A(A[k][i][j]);
}
Hopefully you can tell fairly easily which statements go with which for statements. Also, beware the commas, now you can't use them in a single clause of any of the fors.
Here is a C++11 implementation that handles everything iterable. Other solutions restrict themselves to containers with ::iterator typedefs or arrays: but a for_each is about iteration, not being a container.
I also isolate the SFINAE to a single spot in the is_iterable trait. The dispatching (between elements and iterables) is done via tag dispatching, which I find is a clearer solution.
The containers and the functions applied to elements are all perfect forwarded, allowing both const and non-const access to the ranges and functors.
#include <utility>
#include <iterator>
The template function I am implementing. Everything else could go into a details namespace:
template<typename C, typename F>
void for_each_flat( C&& c, F&& f );
Tag dispatching is much cleaner than SFINAE. These two are used for iterable objects and non iterable objects respectively. The last iteration of the first could use perfect forwarding, but I am lazy:
template<typename C, typename F>
void for_each_flat_helper( C&& c, F&& f, std::true_type /*is_iterable*/ ) {
for( auto&& x : std::forward<C>(c) )
for_each_flat(std::forward<decltype(x)>(x), f);
}
template<typename D, typename F>
void for_each_flat_helper( D&& data, F&& f, std::false_type /*is_iterable*/ ) {
std::forward<F>(f)(std::forward<D>(data));
}
This is some boilerplate required in order to write is_iterable. I do argument dependent lookup on begin and end in a detail namespace. This emulates what a for( auto x : y ) loop does reasonably well:
namespace adl_aux {
using std::begin; using std::end;
template<typename C> decltype( begin( std::declval<C>() ) ) adl_begin(C&&);
template<typename C> decltype( end( std::declval<C>() ) ) adl_end(C&&);
}
using adl_aux::adl_begin;
using adl_aux::adl_end;
The TypeSink is useful to test if code is valid. You do TypeSink< decltype( code ) > and if the code is valid, the expression is void. If the code is not valid, SFINAE kicks in and the specialization is blocked:
template<typename> struct type_sink {typedef void type;};
template<typename T> using TypeSink = typename type_sink<T>::type;
template<typename T, typename=void>
struct is_iterable:std::false_type{};
template<typename T>
struct is_iterable<T, TypeSink< decltype( adl_begin( std::declval<T>() ) ) >>:std::true_type{};
I only test for begin. An adl_end test could also be done.
The final implementation of for_each_flat ends up being extremely simple:
template<typename C, typename F>
void for_each_flat( C&& c, F&& f ) {
for_each_flat_helper( std::forward<C>(c), std::forward<F>(f), is_iterable<C>() );
}
Live example
This is way down at the bottom: feel free to poach for the top answers, which are solid. I just wanted a few better techniques to be used!
Firstly, you shouldn't use a vector of vectors of vectors. Each vector is guaranteed to have contiguous memory, but the "global" memory of a vector of vectors isn't (and probably won't be). You should use the standard library type array instead of C-style arrays as well.
using std::array;
array<array<array<double, 5>, 8>, 10> B;
for (int k=0; k<10; k++)
for (int i=0; i<8; i++)
for (int j=0; j<5; j++)
do_something_on_B(B[k][i][j]);
// or, if you really don't like that, at least do this:
for (int k=0; k<10; k++) {
for (int i=0; i<8; i++) {
for (int j=0; j<5; j++) {
do_something_on_B(B[k][i][j]);
}
}
}
Better yet though, you could define a simple 3D matrix class:
#include <stdexcept>
#include <array>
using std::size_t;
template <size_t M, size_t N, size_t P>
class matrix3d {
static_assert(M > 0 && N > 0 && P > 0,
"Dimensions must be greater than 0.");
std::array<std::array<std::array<double, P>, N>, M> contents;
public:
double& at(size_t i, size_t j, size_t k)
{
if (i >= M || j >= N || k >= P)
throw out_of_range("Index out of range.");
return contents[i][j][k];
}
double& operator(size_t i, size_t j, size_t k)
{
return contents[i][j][k];
}
};
int main()
{
matrix3d<10, 8, 5> B;
for (int k=0; k<10; k++)
for (int i=0; i<8; i++)
for (int j=0; j<5; j++)
do_something_on_B(B(i,j,k));
return 0;
}
You could go further and make it fully const-correct, add matrix multiplication (proper and element-wise), multiplication by vectors, etc. You could even generalise it to different types (I'd make it template if you mainly use doubles).
You could also add proxy objects so you can do B[i] or B[i][j]. They could return vectors (in the mathematical sense) and matrices full of double&, potentially?

How to initialize an array when using a template

I created an array template for my personal use.
template <typename T, int size>
struct Vector {
T data[size];
};
I tried to intialize the data like so:
Vector<unsigned char, 10> test;
test.data[] = {0,1,2,3,4,5,6,7,8,9};
My compiler ended up complaining something about "expected expression." Does anyone know what I'm doing? I want to be able to use this style of initialization where you give it the entire array definition at once instead of using a for loop to init the elements individually.
Since your class is an aggregate, you can initialize it with the usual brace syntax:
Vector<int, 3> x = { { 1, 2, 3 } };
The exact same thing applies to std::array<int, 3>.
In the new standard, C++11, you can use std::initalizer_list to get the desired result, see the below example.
#include <iostream>
#include <algorithm>
template <typename T, int size>
struct Vector {
T data[size];
Vector<T, size> (std::initializer_list<T> _data) {
std::copy (_data.begin (), _data.end (), data);
}
// ...
Vector<T, size>& operator= (std::initializer_list<T> const& _data) {
std::copy (_data.begin (), _data.end (), data);
return *this;
}
};
int
main (int argc, char *argv[])
{
Vector<int, 10> v ({1,2,3,4,5,6}); // std::initializer_list
v = {9,8,7,6,5,4,3,2,1,0}; // operator=
}
If you are working with a standard prior to C++11 it's a bit more of a hassle really, and your best bet is to implement functions similar to those available when using std::vector.
#include <iostream>
#include <algorithm>
template <typename T, int size>
struct Vector {
T _data[size];
Vector (T* begin, T* end) {
std::copy (begin, end, _data);
}
// ...
void assign (T* begin, T* end) {
std::copy (begin, end, _data);
}
};
int
main (int argc, char *argv[])
{
int A1[4] = {1,2,3,4};
int A2[5] = {99,88,77,66,55};
Vector<int, 10> v1 (A1, A1+4);
// ...
v1.assign (A2, A2+5);
}
You have to supply the type and size of the array when defining the variable:
Vector<int, 10> test;
You can not however assign to the member array like a normal array. You have to assign each element separately:
for (int i = 0; i < 10; i++)
test.data[i] = i; // If instantiated with type "int"
You can only initialize an array at the point you are defining it:
Vector<unsigned char, 10> test;
There's your array, you are done defining it, your chance to initialize it has passed.
Edit: Seeing Mat's answer, memo to me: I have to read up on C++11, and soon... :-/
Edit 2: I just gave the information on what was wrong. Kerrek SB has the information on how to do it right. ;-)