-
Notifications
You must be signed in to change notification settings - Fork 311
/
Copy pathcstl_list_iterator.c
233 lines (196 loc) · 8.46 KB
/
cstl_list_iterator.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* The implement of list iterator.
* Copyright (C) 2008 - 2012 Wangbo
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author e-mail: activesys.wb@gmail.com
* activesys@sina.com.cn
*/
/** include section **/
#include <cstl/cstl_def.h>
#include <cstl/cstl_alloc.h>
#include <cstl/cstl_types.h>
#include <cstl/citerator.h>
#include <cstl/cfunctional.h>
#include <cstl/cstring.h>
#include <cstl/clist.h>
#include "cstl_list_aux.h"
/** local constant declaration and local macro section **/
/** local data type declaration and local struct, union, enum section **/
/** local function prototype section **/
/** exported global variable definition section **/
/** local global variable definition section **/
/** exported function implementation section **/
/**
* Create list iterator.
*/
list_iterator_t _create_list_iterator(void)
{
list_iterator_t it_iter;
_LIST_ITERATOR_COREPOS(it_iter) = NULL;
_ITERATOR_CONTAINER(it_iter) = NULL;
_LIST_ITERATOR_CONTAINER_TYPE(it_iter) = _LIST_CONTAINER;
_LIST_ITERATOR_ITERATOR_TYPE(it_iter) = _BIDIRECTIONAL_ITERATOR;
return it_iter;
}
/**
* Get data value referenced by iterator.
*/
void _list_iterator_get_value(list_iterator_t it_iter, void* pv_value)
{
assert(pv_value != NULL);
assert(_list_iterator_belong_to_list(_LIST_ITERATOR_CONTAINER(it_iter), it_iter));
assert(!iterator_equal(it_iter, list_end(_LIST_ITERATOR_CONTAINER(it_iter))));
/* char* */
if (strncmp(_GET_LIST_TYPE_BASENAME(_LIST_ITERATOR_CONTAINER(it_iter)), _C_STRING_TYPE, _TYPE_NAME_SIZE) == 0) {
*(char**)pv_value = (char*)string_c_str((string_t*)((_listnode_t*)_LIST_ITERATOR_COREPOS(it_iter))->_pby_data);
} else {
bool_t b_result = _GET_LIST_TYPE_SIZE(_LIST_ITERATOR_CONTAINER(it_iter));
_GET_LIST_TYPE_COPY_FUNCTION(_LIST_ITERATOR_CONTAINER(it_iter))(
pv_value, ((_listnode_t*)_LIST_ITERATOR_COREPOS(it_iter))->_pby_data, &b_result);
assert(b_result);
}
}
/**
* Set data value referenced by iterator.
*/
void _list_iterator_set_value(list_iterator_t it_iter, const void* cpv_value)
{
assert(cpv_value != NULL);
assert(_list_iterator_belong_to_list(_LIST_ITERATOR_CONTAINER(it_iter), it_iter));
assert(!iterator_equal(it_iter, list_end(_LIST_ITERATOR_CONTAINER(it_iter))));
/* char* */
if (strncmp(_GET_LIST_TYPE_BASENAME(_LIST_ITERATOR_CONTAINER(it_iter)), _C_STRING_TYPE, _TYPE_NAME_SIZE) == 0) {
string_assign_cstr((string_t*)((_listnode_t*)_LIST_ITERATOR_COREPOS(it_iter))->_pby_data, (char*)cpv_value);
} else {
bool_t b_result = _GET_LIST_TYPE_SIZE(_LIST_ITERATOR_CONTAINER(it_iter));
_GET_LIST_TYPE_COPY_FUNCTION(_LIST_ITERATOR_CONTAINER(it_iter))(
((_listnode_t*)_LIST_ITERATOR_COREPOS(it_iter))->_pby_data, cpv_value, &b_result);
assert(b_result);
}
}
/**
* Get data value pointer referenced by iterator.
*/
const void* _list_iterator_get_pointer(list_iterator_t it_iter)
{
assert(_list_iterator_belong_to_list(_LIST_ITERATOR_CONTAINER(it_iter), it_iter));
assert(!iterator_equal(it_iter, list_end(_LIST_ITERATOR_CONTAINER(it_iter))));
/* char* */
if (strncmp(_GET_LIST_TYPE_BASENAME(_LIST_ITERATOR_CONTAINER(it_iter)), _C_STRING_TYPE, _TYPE_NAME_SIZE) == 0) {
return string_c_str((string_t*)((_listnode_t*)_LIST_ITERATOR_COREPOS(it_iter))->_pby_data);
} else {
return ((_listnode_t*)_LIST_ITERATOR_COREPOS(it_iter))->_pby_data;
}
}
/**
* Get data value pointer referenced by iterator, but ignore char*.
*/
const void* _list_iterator_get_pointer_ignore_cstr(list_iterator_t it_iter)
{
assert(_list_iterator_belong_to_list(_LIST_ITERATOR_CONTAINER(it_iter), it_iter));
assert(!iterator_equal(it_iter, list_end(_LIST_ITERATOR_CONTAINER(it_iter))));
return ((_listnode_t*)_LIST_ITERATOR_COREPOS(it_iter))->_pby_data;
}
/**
* Return iterator reference next element.
*/
list_iterator_t _list_iterator_next(list_iterator_t it_iter)
{
assert(_list_iterator_belong_to_list(_LIST_ITERATOR_CONTAINER(it_iter), it_iter));
assert(!iterator_equal(it_iter, list_end(_LIST_ITERATOR_CONTAINER(it_iter))));
_LIST_ITERATOR_COREPOS(it_iter) = (_byte_t*)(((_listnode_t*)_LIST_ITERATOR_COREPOS(it_iter))->_pt_next);
return it_iter;
}
/**
* Return iterator reference previous element.
*/
list_iterator_t _list_iterator_prev(list_iterator_t it_iter)
{
assert(_list_iterator_belong_to_list(_LIST_ITERATOR_CONTAINER(it_iter), it_iter));
assert(!iterator_equal(it_iter, list_begin(_LIST_ITERATOR_CONTAINER(it_iter))));
_LIST_ITERATOR_COREPOS(it_iter) = (_byte_t*)(((_listnode_t*)_LIST_ITERATOR_COREPOS(it_iter))->_pt_prev);
return it_iter;
}
/**
* Test the two list iterator are equal.
*/
bool_t _list_iterator_equal(list_iterator_t it_first, list_iterator_t it_second)
{
assert(_iterator_same_type(it_first, it_second));
assert(_LIST_ITERATOR_CONTAINER(it_first) == _LIST_ITERATOR_CONTAINER(it_second));
assert(_list_iterator_belong_to_list(_LIST_ITERATOR_CONTAINER(it_first), it_first));
assert(_list_iterator_belong_to_list(_LIST_ITERATOR_CONTAINER(it_second), it_second));
return _LIST_ITERATOR_COREPOS(it_first) == _LIST_ITERATOR_COREPOS(it_second) ? true : false;
}
/**
* Calculate distance between two iterators.
*/
int _list_iterator_distance(list_iterator_t it_first, list_iterator_t it_second)
{
_listnode_t* pt_node = NULL; /* the iterate pointer */
int n_distance = 0; /* the distance of two iterator */
assert(_iterator_same_type(it_first, it_second));
assert(_LIST_ITERATOR_CONTAINER(it_first) == _LIST_ITERATOR_CONTAINER(it_second));
assert(_list_iterator_belong_to_list(_LIST_ITERATOR_CONTAINER(it_first), it_first));
assert(_list_iterator_belong_to_list(_LIST_ITERATOR_CONTAINER(it_second), it_second));
/* if the end > begin then distance is greater zero */
if (_list_iterator_before(it_first, it_second)) {
for (pt_node = (_listnode_t*)_LIST_ITERATOR_COREPOS(it_first);
pt_node != (_listnode_t*)_LIST_ITERATOR_COREPOS(it_second);
pt_node = pt_node->_pt_next) {
n_distance++;
}
return n_distance;
} else if (_list_iterator_before(it_second, it_first)) {
for (pt_node = (_listnode_t*)_LIST_ITERATOR_COREPOS(it_second);
pt_node != (_listnode_t*)_LIST_ITERATOR_COREPOS(it_first);
pt_node = pt_node->_pt_next) {
n_distance++;
}
return -n_distance;
} else {
return 0;
}
}
/**
* Test the first iterator is before the second.
*/
bool_t _list_iterator_before(list_iterator_t it_first, list_iterator_t it_second)
{
_listnode_t* pt_node = NULL; /* current list node pointer */
assert(_iterator_same_type(it_first, it_second));
assert(_LIST_ITERATOR_CONTAINER(it_first) == _LIST_ITERATOR_CONTAINER(it_second));
assert(_list_iterator_belong_to_list(_LIST_ITERATOR_CONTAINER(it_first), it_first));
assert(_list_iterator_belong_to_list(_LIST_ITERATOR_CONTAINER(it_second), it_second));
/* if the first node equal to second node is false */
if (_LIST_ITERATOR_COREPOS(it_first) == _LIST_ITERATOR_COREPOS(it_second)) {
return false;
}
/* loop from first iterator */
for (pt_node = (_listnode_t*)_LIST_ITERATOR_COREPOS(it_first);
pt_node != _LIST_ITERATOR_CONTAINER(it_first)->_pt_node;
pt_node = pt_node->_pt_next) {
/* if meet the second iterator then the first before second */
if (pt_node == (_listnode_t*)_LIST_ITERATOR_COREPOS(it_second)) {
return true;
}
}
/* if second iterator is end the return true */
return (_listnode_t*)_LIST_ITERATOR_COREPOS(it_second) == _LIST_ITERATOR_CONTAINER(it_second)->_pt_node ? true : false;
}
/** local function implementation section **/
/** eof **/