, /++: , RAII, , , , , , -, .
, . , (, ), . , , , , .
, , , , ( ):
class cell {
public:
void setBacktrace( unsigned int value );
unsigned int getBacktrace() const;
private:
uint16_t data;
};
. :
cell c;
c.setBacktrace( 5 );
std::cout << c.getBacktrace() << std::endl;
. , std::vector . , , :
class maze {
public:
maze( size_t width, size_t height );
~maze();
cell getCell( size_t row, size_t col ) const;
void setCell( size_t row, size_t col, cell c );
private:
size_t width_;
size_t height_;
cell * data_;
};
: . , ,
. const, . , :
const.
:
maze::maze( size_t width, size_t height )
: width_( width ), height_( height ), data_( new cell[width*height] )
{
}
maze::~maze()
{
delete [] data_;
}
. width_, height_ data_ , .
({}). , ( ), , .
, , . , , . , .
. :
int main() {
maze m( 10, 50 );
cell c;
c.setBacktrace( 5 );
m.setCell( 3, 4, c);
assert( m.getCell( 3, 4 ).getBacktrace() == 5 );
}
, . operator(), , (++ FAQ lite ):
class maze {
public:
cell const & operator()( size_t row, size_t col ) const;
cell & operator()( size_t row, size_t col );
};
:
int main()
{
maze m( 10, 10 );
m( 3, 4 ).setBacktrace( 5 );
assert( m( 3, 4 ).getBacktrace() == 5 );
}
, , . :
cell const & maze::operator()( size_t row, size_t col ) const
{
return *data_[ row + col*width_ ];
}
cell & maze::operator()( size_t row, size_t col )
{
return *data_[ row + col*width_ ];
}
, , , , , .
, , , , , - 2D-. , :
template <typename T>
class array_2d
{
public:
array_2d( size_t width, size_t height );
~array_2d();
T const & operator()( size_t row, size_t col ) const;
T & operator()( size_t row, size_t col );
private:
size_t width_;
size_t height_;
T* data_;
};
:
int main()
{
array_2d<cell> maze( 10, 10 );
maze( 3, 4 ).setBacktrace( 5 );
}
, :
template <typename T>
array_2d<T>::array_2d( size_t width, size_t height )
: width_( width ), height_( height ), data_( new T[ width*height ] )
{
}
. , ?
, . , ( , , , ). POD ( ), , :
struct big_cell
{
unsigned int backtrack;
unsigned int solution;
unsigned int borders;
unsigned int walls;
};
:
int main()
{
array_2d<big_cell> maze( 10, 10 );
maze( 3, 4 ).backtrack = 5;
assert( maze( 3, 4 ).backtrack == 5 );
}
, . . . 32 ( 16, ):
struct medium_cell
{
unsigned char backtrack;
unsigned char solution;
unsigned char borders;
unsigned char walls;
};
2 , , , ( , ). , 32- . 32- 16- .
, , ++: . , - :
struct small_cell {
uint16_t backtrack : 4;
uint16_t solution : 4;
uint16_t borders : 4;
uint16_t walls : 4;
};
:
int main()
{
small_cell c;
c.solution = 5;
c.backtrack = 3;
}
16 , . 2d-, . .
#include <time.h>
template <typename CellStruct>
void test()
{
array_2d< CellStruct > maze;
}
void print_test( std::string const & test, clock_t ticks )
{
std::cout << "Test: " << test << " took " << ticks
<< " ticks, or " << ticks / CLOCKS_PER_SEC << " seconds."
<< std::endl;
}
int main()
{
clock_t init = clock();
test< big_cell >();
clock_t after_big = clock();
test< medium_cell >();
clock_t after_med = clock();
test< small_cell >();
clock_t end = clock();
print_result( "big_cell", after_big - init );
print_result( "medium_cell", after_med - after_big );
print_result( "small_cell", end - after_med );
}
, . , , , .