Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1805 | yogev_ezra | 1 | #define AR_CHUNK_SIZE 64 |
2 | |||
3 | // |
||
4 | template |
||
5 | class MCArray |
||
6 | { |
||
7 | protected: |
||
8 | TYPE * _dataPtr; |
||
9 | int _elementsCount; |
||
10 | int _capacity; |
||
11 | |||
12 | public: |
||
13 | MCArray(); |
||
14 | ~MCArray(); |
||
15 | virtual int Add( const TYPE &element ); |
||
16 | TYPE & GetAt( int Ndx ); |
||
17 | TYPE & operator [] ( int Ndx ); |
||
18 | int Find( int startNdx, TYPE & element ); |
||
19 | int RemoveAt( int Ndx ); |
||
20 | void Clear(void); |
||
21 | int GetCount(void); |
||
22 | }; |
||
23 | |||
24 | // |
||
25 | |||
26 | |||
27 | // |
||
28 | template |
||
29 | MCArray |
||
30 | { |
||
31 | // устанавливаем переменные |
||
32 | this->_dataPtr = NULL; |
||
33 | this->_capacity = 0; |
||
34 | this->_elementsCount = 0; |
||
35 | } |
||
36 | |||
37 | |||
38 | // |
||
39 | template |
||
40 | MCArray |
||
41 | { |
||
42 | // |
||
43 | this->_capacity = 0; |
||
44 | this->_elementsCount = 0; |
||
45 | // |
||
46 | if ( this->_dataPtr != NULL ) |
||
47 | { |
||
48 | delete this->_dataPtr; |
||
49 | } |
||
50 | } |
||
51 | |||
52 | |||
53 | // |
||
54 | template |
||
55 | int MCArray |
||
56 | { |
||
57 | TYPE * dPtr; |
||
58 | |||
59 | // есть ли место? |
||
60 | if ( this->_elementsCount >= this->_capacity ) |
||
61 | { |
||
62 | // занимаем ещё памяти |
||
63 | dPtr = new TYPE [this->_capacity + AR_CHUNK_SIZE]; |
||
64 | // проверка |
||
65 | if ( dPtr == NULL ) |
||
66 | { |
||
67 | // |
||
68 | return -1; |
||
69 | } |
||
70 | |||
71 | if ( this->_capacity > 0 ) |
||
72 | { |
||
73 | // скопируем существующие данные на новое место |
||
74 | memcpy( dPtr, this->_dataPtr, sizeof(TYPE) * this->_capacity ); |
||
75 | // удалим старую копию данных |
||
76 | delete this->_dataPtr; |
||
77 | } |
||
78 | // скорректируем размер |
||
79 | this->_capacity += AR_CHUNK_SIZE; |
||
80 | // скорректируем указатель на данные |
||
81 | this->_dataPtr = dPtr; |
||
82 | } |
||
83 | |||
84 | // копируем элемент в массив |
||
85 | memcpy( this->_dataPtr + this->_elementsCount, &element, sizeof(TYPE) ); |
||
86 | |||
87 | // увеличиваем счётчик элементов |
||
88 | return ++this->_elementsCount; |
||
89 | } |
||
90 | |||
91 | |||
92 | // |
||
93 | template |
||
94 | TYPE & MCArray |
||
95 | { |
||
96 | //assert( Ndx >= 0 && Ndx < this->_elementsCount ); |
||
97 | return this->_dataPtr[Ndx]; |
||
98 | } |
||
99 | |||
100 | |||
101 | // |
||
102 | template |
||
103 | TYPE & MCArray |
||
104 | { |
||
105 | return this->GetAt( Ndx ); |
||
106 | } |
||
107 | |||
108 | |||
109 | // |
||
110 | template |
||
111 | int MCArray |
||
112 | { |
||
113 | int i; |
||
114 | |||
115 | if ( startNdx < 0 || startNdx >= this->_elementsCount ) |
||
116 | { |
||
117 | return -1; |
||
118 | } |
||
119 | |||
120 | for ( i = startNdx; i < this->_elementsCount; i++ ) |
||
121 | { |
||
122 | if ( element == this->_dataPtr[i] ) |
||
123 | { |
||
124 | return i; |
||
125 | } |
||
126 | } |
||
127 | |||
128 | return -1; |
||
129 | } |
||
130 | |||
131 | |||
132 | // |
||
133 | template |
||
134 | int MCArray |
||
135 | { |
||
136 | int mn; |
||
137 | |||
138 | if ( Ndx < 0 || Ndx >= this->_elementsCount ) |
||
139 | { |
||
140 | return 0; |
||
141 | } |
||
142 | |||
143 | mn = this->_elementsCount - Ndx; |
||
144 | |||
145 | if ( mn != 1 ) |
||
146 | { |
||
147 | memcpy( this->_dataPtr + Ndx, this->_dataPtr + Ndx + 1, sizeof(TYPE) * ( mn - 1 ) ); |
||
148 | } |
||
149 | |||
150 | this->_elementsCount--; |
||
151 | return 1; |
||
152 | } |
||
153 | |||
154 | |||
155 | // |
||
156 | template |
||
157 | void MCArray |
||
158 | { |
||
159 | this->_elementsCount = 0; |
||
160 | } |
||
161 | |||
162 | // |
||
163 | template |
||
164 | int MCArray |
||
165 | { |
||
166 | return this->_elementsCount; |
||
167 | }>>>> |