1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
//
template < typename T, typename V > void make_rectangular( std::vector<T>&, const V& ) { }
template < typename T, typename V > void make_rectangular( std::vector<std::vector<T>>& matrix, const V& defaultValue )
{
static const auto neq_size = []( const auto& a, const auto& b ) { return a.size() != b.size() ; } ;
if( std::adjacent_find( std::begin(matrix), std::end(matrix), neq_size ) != std::end(matrix) ) { // if true, it is non rectangular and must be filled
struct Max
{
Max(): max{0} { }
void operator()(std::vector<T>& element)
{
if ( element.size() > max )
max = element.size();
}
std::size_t max;
};
Max m = std::for_each(matrix.begin(), matrix.end(), Max());
for (auto& s : matrix)
s.resize(m.max, defaultValue);
}
for( auto& v : matrix ) make_rectangular(v,defaultValue) ;
}
template < typename T, typename V > void make_rectangular( std::vector<std::vector<std::vector<T>>>& matrix, const V& defaultValue )
{
static const auto neq_size = []( const auto& a, const auto& b ) { return a.size() != b.size() ; } ;
if( std::adjacent_find( std::begin(matrix), std::end(matrix), neq_size ) != std::end(matrix) ) { // if true, it is non rectangular and must be filled
struct Max
{
Max(): max{0} { }
void operator()(std::vector<std::vector<T>>& element)
{
if ( element.size() > max )
max = element.size();
}
std::size_t max;
};
Max m = std::for_each(matrix.begin(), matrix.end(), Max());
for (auto& s : matrix)
s.resize(m.max);
}
for( auto& v : matrix ) make_rectangular(v,defaultValue) ;
} // end template make_rectangular
|