-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIterator.c
More file actions
166 lines (143 loc) · 3.13 KB
/
Copy pathIterator.c
File metadata and controls
166 lines (143 loc) · 3.13 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "Iterator.h"
struct _Iterator_{
void * object;
void * current;
int currError;
IT_BOOL front;
};
struct _FunctionIterator_{
int (*initFront)(void*);
void * (*next)(void*,int*);
int (*initBack)(void*);
void * (*prev)(void*,int*);
};
/* FunctionIterator */
FunctionIterator newFunctionIterator(int (*initFront)(void *),void * (*next)(void *,int*),int (*initBack)(void *),void * (*prev)(void*,int*)){
struct _FunctionIterator_ * fi = NULL;
if((initFront && next) || (initBack && prev)){
fi = malloc(sizeof(*fi));
if(fi){
if(initBack && prev){
fi->prev=prev;
fi->initBack=initBack;
}else{
fi->prev=NULL;
fi->initBack=NULL;
}
if(initFront && next){
fi->initFront=initFront;
fi->next=next;
}else{
fi->next=NULL;
fi->initFront=NULL;
}
}
}
return fi;
}
int deleteFunctionIterator(FunctionIterator fi){
int error = 1;
if(fi){
free(fi);
error = 0;
}
return error;
}
/* Iterator */
Iterator newIterator(void * object,IT_BOOL front){
struct _Iterator_ * i = NULL;
struct _FunctionIterator_ * fi;
int error = 0;
if(object){
fi = *(struct _FunctionIterator_ **)object;
if((front && fi->initFront && fi->next) || (!front && fi->initBack && fi->prev)){
i = malloc(sizeof(*i));
if(i){
switch(front){
case IT_TRUE:
error=fi->initFront(object);
if(!error){
i->current=fi->next(object,&(i->currError));
}
break;
case IT_FALSE:
error=fi->initBack(object);
if(!error){
i->current=fi->prev(object,&(i->currError));
}
break;
}
i->front=front;
i->object=object;
if(error){
free(i);
i=NULL;
}
}
}
}
return (void *)i;
}
void * nextIterator(Iterator i,int * error){
struct _Iterator_ * it = i;
void * res = NULL;
struct _FunctionIterator_ * fi;
int err = -1;
if(i){
fi=*(struct _FunctionIterator_ **)(it->object);
if(fi->next){
if(!it->front){
fi->next(it->object,NULL);
fi->next(it->object,&(it->currError));
it->front=IT_TRUE;
}
res = it->current;
err = it->currError;
it->current = fi->next(it->object,&(it->currError));
}
}
if(error)
*error=err;
return res;
}
void * prevIterator(Iterator i,int * error){
struct _Iterator_ * it = i;
void * res = NULL;
struct _FunctionIterator_ * fi;
int err = -1;
if(i){
fi=*(struct _FunctionIterator_ **)(it->object);
if(fi->prev){
if(it->front){
fi->prev(it->object,NULL);
fi->prev(it->object,&(it->currError));
it->front=IT_FALSE;
}
res = it->current;
err = it->currError;
it->current = fi->prev(it->object,&(it->currError));
}
}
if(error)
*error=err;
return res;
}
void * getIterator(Iterator i,int * error){
void * res = NULL;
int err = -1;
if(i){
res=((struct _Iterator_ *)i)->current;
err=((struct _Iterator_ *)i)->currError;
}
if(error)
*error=err;
return res;
}
int deleteIterator(Iterator i){
int error = 1;
if(i){
error = 0;
free(i);
}
return error;
}