@@ -101,6 +101,10 @@ RNAErrorCode rnaError(const RationalNumberArray * rna) {
101
101
}
102
102
103
103
RationalNumber rnaGet (RationalNumberArray * rna, const unsigned int position) {
104
+ if (rna == NULL ) {
105
+ return NULL_RATIONAL_NUMBER;
106
+ }
107
+
104
108
if (rna->data == NULL ) {
105
109
return NULL_RATIONAL_NUMBER;
106
110
}
@@ -199,3 +203,140 @@ void rnaSetErrorCallback (RationalNumberArray * rna, rnaErrorCallback_t callback
199
203
200
204
rna->errorCallback = callback;
201
205
}
206
+
207
+ rnum::CPP_RationalNumberArray::CPP_RationalNumberArray (const unsigned int size) {
208
+ this ->m_data = new CPP_RationalNumber[size];
209
+
210
+ this ->m_size = 0 ;
211
+ this ->m_capacity = size;
212
+ this ->m_errorCallback = NULL ;
213
+
214
+ if (this ->m_data == NULL )
215
+ setError (rnaAllocFailed);
216
+ else
217
+ setError (rnaNoError);
218
+
219
+ }
220
+
221
+ rnum::CPP_RationalNumberArray::~CPP_RationalNumberArray (void ) {
222
+ if (this ->m_data != NULL ) {
223
+ // free the array
224
+ delete (this ->m_data );
225
+ this ->m_data = NULL ;
226
+ }
227
+ }
228
+
229
+ void rnum::CPP_RationalNumberArray::resize (const unsigned int newSize) {
230
+ if (this ->m_size == newSize) // nothing to do
231
+ return ;
232
+
233
+ CPP_RationalNumber * newData = new CPP_RationalNumber[newSize];
234
+
235
+ if (this ->m_size > newSize) // truncate
236
+ {
237
+ for (unsigned int i = 0 ; i < newSize; i++) {
238
+ newData[i] = this ->m_data [i];
239
+ }
240
+ }
241
+ else // append
242
+ {
243
+ for (unsigned int i = 0 ; i < this ->m_size ; i++) {
244
+ newData[i] = this ->m_data [i];
245
+ }
246
+ }
247
+
248
+ // don't leak
249
+ delete (this ->m_data );
250
+
251
+ this ->m_data = newData;
252
+ this ->m_capacity = newSize;
253
+ setError (rnaNoError);
254
+ }
255
+
256
+ unsigned int rnum::CPP_RationalNumberArray::size (void ) {
257
+ return this ->m_size ;
258
+ }
259
+
260
+ unsigned int rnum::CPP_RationalNumberArray::capacity (void ) {
261
+ return this ->m_capacity ;
262
+ }
263
+
264
+ void rnum::CPP_RationalNumberArray::add (const rnum::CPP_RationalNumber newRationalNumber) {
265
+ if (this ->m_size < this ->m_capacity ) {
266
+ this ->m_data [this ->m_size ++] = newRationalNumber;
267
+ setError (rnaNoError);
268
+ return ;
269
+ }
270
+
271
+ // resize array and retry
272
+ const unsigned int oldCapacity = this ->m_capacity ;
273
+ resize (oldCapacity + INCREMENTATION);
274
+ assert (this ->m_capacity == oldCapacity + INCREMENTATION);
275
+ add (newRationalNumber);
276
+ }
277
+
278
+ void rnum::CPP_RationalNumberArray::set (const unsigned int position, const rnum::CPP_RationalNumber rationalNumber) {
279
+ if (position > this ->m_size )
280
+ resize (position + 1 );
281
+
282
+ this ->m_data [position] = rationalNumber;
283
+ this ->m_size = position+1 ;
284
+ }
285
+
286
+ const rnum::CPP_RationalNumber rnum::CPP_RationalNumberArray::get (const unsigned int position) {
287
+ if (position >= this ->m_size ) {
288
+ setError (rnaInvalidIndex);
289
+ return NULL_RATIONAL_NUMBER;
290
+ }
291
+
292
+ setError (rnaNoError);
293
+ return this ->m_data [position];
294
+ }
295
+
296
+ void rnum::CPP_RationalNumberArray::remove (const unsigned int from, const unsigned int to) {
297
+ // this->m_
298
+
299
+ if (to < from) {
300
+ setError (rnaInvalidIndex);
301
+ return ;
302
+ }
303
+
304
+ unsigned int offset = (to - from) + 1 ;
305
+
306
+ // overwrite (from .. to) with subsequent elements;
307
+ for (unsigned int i = from; i < this ->m_capacity ; i++)
308
+ if (i+offset <= this ->m_size )
309
+ this ->m_data [i] = this ->m_data [i+offset];
310
+ else
311
+ break ;
312
+
313
+ // calculate new size
314
+ this ->m_size = (this ->m_size > offset) ? this ->m_size - offset : 0 ;
315
+
316
+ setError (rnaNoError);
317
+
318
+ for (unsigned int i = this ->m_size ; i < this ->m_capacity ; i++)
319
+ this ->m_data [i] = CPP_RationalNumber ();
320
+ }
321
+
322
+ RNAErrorCode rnum::CPP_RationalNumberArray::rnaError (void ) const {
323
+ return this ->m_error ;
324
+ }
325
+
326
+ void rnum::CPP_RationalNumberArray::rnaSetErrorCallback (const rnum::cppErrorCallback_t callback) {
327
+ this ->m_errorCallback = callback;
328
+ }
329
+
330
+ void rnum::CPP_RationalNumberArray::setError (const RNAErrorCode errorCode) {
331
+ this ->m_error = errorCode;
332
+
333
+ if (this ->m_error != rnaNoError && this ->m_errorCallback != NULL )
334
+ this ->m_errorCallback (*this );
335
+ }
336
+
337
+
338
+ /* void rnum::CPP_RationalNumberArray::initializeWithNullRationalNumber(const unsigned int from, const unsigned int to) {
339
+ for (unsigned int i = from; i < to; i++) {
340
+ this->data[i] = NULL_RATIONAL_NUMBER;
341
+ }
342
+ }*/
0 commit comments