Skip to content

Commit 45aa9c8

Browse files
author
tiberius
committed
implemented CPP_RationalNumberArray
tests needed!
1 parent dba9bf1 commit 45aa9c8

File tree

3 files changed

+162
-17
lines changed

3 files changed

+162
-17
lines changed

cpp4j/rationalnumberarray.cpp

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ RNAErrorCode rnaError(const RationalNumberArray * rna) {
101101
}
102102

103103
RationalNumber rnaGet(RationalNumberArray * rna, const unsigned int position) {
104+
if (rna == NULL) {
105+
return NULL_RATIONAL_NUMBER;
106+
}
107+
104108
if (rna->data == NULL) {
105109
return NULL_RATIONAL_NUMBER;
106110
}
@@ -199,3 +203,140 @@ void rnaSetErrorCallback (RationalNumberArray * rna, rnaErrorCallback_t callback
199203

200204
rna->errorCallback = callback;
201205
}
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+
}*/

cpp4j/rationalnumberarray.h

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,14 @@ void rnaSetErrorCallback (RationalNumberArray * rna, rnaErrorCallback_t callback
6262

6363

6464
namespace rnum{
65-
class CPP_RationalNumberArray{
65+
66+
const CPP_RationalNumber NULL_RATIONAL_NUMBER ( 0, 1 );
67+
68+
class CPP_RationalNumberArray;
69+
70+
typedef void (*cppErrorCallback_t)(CPP_RationalNumberArray &rna);
71+
72+
class CPP_RationalNumberArray {
6673
public :
6774

6875
CPP_RationalNumberArray (const unsigned int size = 10);
@@ -79,16 +86,21 @@ public :
7986

8087
void set(const unsigned int position, const CPP_RationalNumber rationalNumber);
8188

82-
CPP_RationalNumber get( const unsigned int position);
89+
const CPP_RationalNumber get(const unsigned int position);
8390

84-
void remove( const unsigned int from, const unsigned int to);
91+
void remove(const unsigned int from, const unsigned int to);
8592

86-
//RNAErrorCode rnaError(const RationalNumberArray * rna);
87-
//void rnaSetErrorCallback (RationalNumberArray * rna, rnaErrorCallback_t callback);
93+
RNAErrorCode rnaError(void) const;
94+
void rnaSetErrorCallback (const cppErrorCallback_t callback);
8895

8996
private:
90-
RationalNumberArray * rna;
91-
97+
void setError(const RNAErrorCode errorCode);
98+
//void initializeWithNullRationalNumber(const unsigned int from, const unsigned int to);
99+
CPP_RationalNumber * m_data;
100+
unsigned int m_size;
101+
unsigned int m_capacity;
102+
RNAErrorCode m_error;
103+
cppErrorCallback_t m_errorCallback;
92104
};
93105
}
94106

cpp4j/testCPP_RN.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,9 @@ void testCPP_RN(void)
3535
CPP_RationalNumber ja = j;
3636
assert(ja==CPP_RationalNumber( 4 , 1 ));
3737

38-
CPP_RationalNumber k(4,3), l(1,3);
39-
CPP_RationalNumber m(2,12356), n( 1, 954321);
40-
printf("%f \n", k.toDouble() );
41-
printf("%f \n",((double)4 / (double)3));
38+
CPP_RationalNumber k(4,2), l(6,3);
4239

43-
44-
assert( (double) k.toDouble() == ((double)4 / (double)3) );
45-
assert((double) 8 /(double) 6 ==(double) 8 /(double) 6 );
46-
assert( l.toDouble() == 0.333333 );
47-
assert( m.toDouble() == 0.000162 );
48-
assert( n.toDouble() == 0.000001 );
40+
assert( (int) k.toDouble() == l.toInt() );
4941

5042
printf(" successful!\n");
5143
}

0 commit comments

Comments
 (0)