forked from curif/oop4c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathooCollection.c
111 lines (98 loc) · 1.88 KB
/
ooCollection.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* ooCollection.c
*
* Created on: Aug 7, 2014
* Author: fcuri
*/
#include "ooCollection.h"
//Get Iterator
ooPropertyGetD(ooObj, Iterator) {
oCollIterator *i = ooNew(oCollIterator, i, this);
ooRaiseIf(!i, Malloc);
return(i);
}
//Add element
ooCollection *ooMethodD( Add, void *o) {
if (!this->arr || this->count + 1 > this->len) {
void *new = realloc(this->arr, (this->len+10) * sizeof(void*));
ooRaiseIf(!new, Malloc);
this->len += 10;
this->arr = new;
}
this->arr[this->count] = o;
this->count++;
return(this);
}
//Remove element
ooBoolean ooMethodD(Remove, void *o) {
int f, g, found=-1;
if (!this->count) {
return(ooFalse);
}
for (f=0; f<this->count; f++) {
if (o == this->arr[f]) {
found = f;
break;
}
}
if (found>=0) {
for (g = f+1; g < this->count; g++) {
this->arr[g-1] = this->arr[g];
}
this->arr[this->count-1]=NULL;
this->count--;
}
return(found >= 0);
}
//Get Count
ooPropertyGetD( int, Count) {
return(this->count);
}
//Get an element
void *ooMethodD(Index, int idx) {
if (idx > this->count - 1) {
return(NULL);
}
return(this->arr[idx]);
}
//Constructor
ooCtorD() {
ooAgregatorD();
return;
}
//Destructor
ooDtorD() {
if (this->arr) {
free(this->arr);
}
return;
}
//Iterator -----------------
//Has next element?
ooPropertyGet(oCollIterator, ooBoolean, HasNext) {
if (!this->coll) {
return(ooFalse);
}
return(this->idxCurrent < this->coll->GetCount(this->coll));
}
//Get Next Element
ooPropertyGet(oCollIterator, void *, Next) {
if (!this->GetHasNext(this)) {
return NULL;
}
this->idxCurrent++;
return this->GetCurrent(this);
}
//Get current element
ooPropertyGet(oCollIterator, void *, Current) {
return(this->coll->Index(this->coll, this->idxCurrent));
}
ooCtor(oCollIterator, ooCollection *coll) {
ooIterator(oCollIterator);
if (ooIsAgregable(coll)) {
this->coll = coll;
}
}
ooDtor(oCollIterator) {
return;
}