diff options
Diffstat (limited to '')
-rw-r--r-- | src/position.cpp | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/src/position.cpp b/src/position.cpp new file mode 100644 index 0000000..6a9eb10 --- /dev/null +++ b/src/position.cpp | |||
@@ -0,0 +1,85 @@ | |||
1 | #include "position.h" | ||
2 | |||
3 | #include <string.h> | ||
4 | #include <stdarg.h> | ||
5 | #include <exception> | ||
6 | |||
7 | Position::Position() : | ||
8 | iDims( 0 ), | ||
9 | aiValues( 0 ) | ||
10 | { | ||
11 | } | ||
12 | |||
13 | Position::Position( int iDims ) : | ||
14 | iDims( iDims ), | ||
15 | aiValues( 0 ) | ||
16 | { | ||
17 | aiValues = new int[iDims]; | ||
18 | memset( aiValues, 0, sizeof(int)*iDims ); | ||
19 | } | ||
20 | |||
21 | Position::Position( int iDims, int iX, ...) : | ||
22 | iDims( iDims ), | ||
23 | aiValues( 0 ) | ||
24 | { | ||
25 | aiValues = new int[iDims]; | ||
26 | memset( aiValues, 0, sizeof(int)*iDims ); | ||
27 | |||
28 | aiValues[0] = iX; | ||
29 | va_list ap; | ||
30 | va_start( ap, iX ); | ||
31 | for( int j = 1; j < iDims; j++ ) | ||
32 | { | ||
33 | aiValues[j] = va_arg( ap, int ); | ||
34 | } | ||
35 | va_end( ap ); | ||
36 | } | ||
37 | |||
38 | Position::Position( const Position &rhs ) : | ||
39 | iDims( rhs.iDims ), | ||
40 | aiValues( 0 ) | ||
41 | { | ||
42 | aiValues = new int[iDims]; | ||
43 | memcpy( aiValues, rhs.aiValues, sizeof(int)*iDims ); | ||
44 | } | ||
45 | |||
46 | Position::~Position() | ||
47 | { | ||
48 | delete[] aiValues; | ||
49 | } | ||
50 | |||
51 | int Position::operator[]( int iIdx ) const | ||
52 | { | ||
53 | if( iIdx < 0 ) | ||
54 | throw std::exception(); | ||
55 | else if( iIdx >= iDims ) | ||
56 | throw std::exception(); | ||
57 | |||
58 | return aiValues[iIdx]; | ||
59 | } | ||
60 | |||
61 | int &Position::operator[]( int iIdx ) | ||
62 | { | ||
63 | if( iIdx < 0 ) | ||
64 | throw std::exception(); | ||
65 | else if( iIdx >= iDims ) | ||
66 | throw std::exception(); | ||
67 | |||
68 | return aiValues[iIdx]; | ||
69 | } | ||
70 | |||
71 | Position &Position::operator=( const Position &rhs ) | ||
72 | { | ||
73 | delete[] aiValues; | ||
74 | iDims = rhs.iDims; | ||
75 | aiValues = new int[iDims]; | ||
76 | memcpy( aiValues, rhs.aiValues, sizeof(int)*iDims ); | ||
77 | |||
78 | return *this; | ||
79 | } | ||
80 | |||
81 | int Position::getDims() const | ||
82 | { | ||
83 | return iDims; | ||
84 | } | ||
85 | |||