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 47 48 49 50
|
#include <iostream>
#include <vector>
#include <algorithm>
namespace lock_utils
{
using lock_type = std::vector< std::vector<int> > ;
lock_type padded_lock( const lock_type& lock, std::size_t padded_size )
{
padded_size = std::max( padded_size, lock.size() ) ;
lock_type padded( padded_size, std::vector<int>(padded_size) ) ;
auto nrow = ( padded_size - lock.size() ) / 2 ;
for( const auto& row : lock )
{
const auto skipped_cols = padded_size > row.size() ? ( padded_size - row.size() ) / 2 : 0 ;
std::copy( std::begin(row), std::end(row), padded[nrow++].begin() + skipped_cols ) ;
}
return padded ;
}
std::ostream& operator<< ( std::ostream& stm, const lock_type& lock )
{
for( const auto& row : lock )
{
for( int v : row ) stm << bool(v) << ' ' ;
stm << '\n' ;
}
return stm ;
}
}
int main()
{
using namespace lock_utils ;
const lock_type lock { { 0, 1, 0 }, { 1, 1, 1 }, { 0, 0, 1 } } ;
std::cout << "lock:\n-----\n" << lock << '\n' ;
for( std::size_t sz : { 3, 5, 6, 8, 13, 16 } )
{
std::cout << "\npadded lock (" << sz << 'x' << sz
<< "):\n------------------\n" << padded_lock( lock, sz ) << '\n' ;
}
}
| |