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