-
Notifications
You must be signed in to change notification settings - Fork 312
/
Copy pathcstl_slist_iterator.c
215 lines (179 loc) · 7.7 KB
/
cstl_slist_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
/*
* The implementation of slist 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/cslist.h>
#include "cstl_slist_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 slist iterator.
*/
slist_iterator_t _create_slist_iterator(void)
{
slist_iterator_t it_iter;
_SLIST_ITERATOR_ITERATOR_TYPE(it_iter) = _FORWARD_ITERATOR;
_SLIST_ITERATOR_CONTAINER_TYPE(it_iter) = _SLIST_CONTAINER;
_ITERATOR_CONTAINER(it_iter) = NULL;
_SLIST_ITERATOR_COREPOS(it_iter) = NULL;
return it_iter;
}
/**
* Get data value referenced by iterator.
*/
void _slist_iterator_get_value(slist_iterator_t it_iter, void* pv_value)
{
assert(pv_value != NULL);
assert(_slist_iterator_belong_to_slist(_SLIST_ITERATOR_CONTAINER(it_iter), it_iter));
assert(!iterator_equal(it_iter, slist_end(_SLIST_ITERATOR_CONTAINER(it_iter))));
/* char* */
if (strncmp(_GET_SLIST_TYPE_BASENAME(_SLIST_ITERATOR_CONTAINER(it_iter)), _C_STRING_TYPE, _TYPE_NAME_SIZE) == 0) {
*(char**)pv_value = (char*)string_c_str((string_t*)((_slistnode_t*)_SLIST_ITERATOR_COREPOS(it_iter))->_pby_data);
} else {
bool_t b_result = _GET_SLIST_TYPE_SIZE(_SLIST_ITERATOR_CONTAINER(it_iter));
_GET_SLIST_TYPE_COPY_FUNCTION(_SLIST_ITERATOR_CONTAINER(it_iter))(
pv_value, ((_slistnode_t*)_SLIST_ITERATOR_COREPOS(it_iter))->_pby_data, &b_result);
assert(b_result);
}
}
/**
* Set data value referenced by iterator.
*/
void _slist_iterator_set_value(slist_iterator_t it_iter, const void* cpv_value)
{
assert(cpv_value != NULL);
assert(_slist_iterator_belong_to_slist(_SLIST_ITERATOR_CONTAINER(it_iter), it_iter));
assert(!iterator_equal(it_iter, slist_end(_SLIST_ITERATOR_CONTAINER(it_iter))));
/* char* */
if (strncmp(_GET_SLIST_TYPE_BASENAME(_SLIST_ITERATOR_CONTAINER(it_iter)), _C_STRING_TYPE, _TYPE_NAME_SIZE) == 0) {
string_assign_cstr((string_t*)((_slistnode_t*)_SLIST_ITERATOR_COREPOS(it_iter))->_pby_data, (char*)cpv_value);
} else {
bool_t b_result = _GET_SLIST_TYPE_SIZE(_SLIST_ITERATOR_CONTAINER(it_iter));
_GET_SLIST_TYPE_COPY_FUNCTION(_SLIST_ITERATOR_CONTAINER(it_iter))(
((_slistnode_t*)_SLIST_ITERATOR_COREPOS(it_iter))->_pby_data, cpv_value, &b_result);
assert(b_result);
}
}
/**
* Test the two slist iterator are equal.
*/
bool_t _slist_iterator_equal(slist_iterator_t it_first, slist_iterator_t it_second)
{
assert(_iterator_same_type(it_first, it_second));
assert(_SLIST_ITERATOR_CONTAINER(it_first) == _SLIST_ITERATOR_CONTAINER(it_second));
assert(_slist_iterator_belong_to_slist(_SLIST_ITERATOR_CONTAINER(it_first), it_first));
assert(_slist_iterator_belong_to_slist(_SLIST_ITERATOR_CONTAINER(it_second), it_second));
return _SLIST_ITERATOR_COREPOS(it_first) == _SLIST_ITERATOR_COREPOS(it_second) ? true : false;
}
/**
* Get data value pointer referenced by iterator.
*/
const void* _slist_iterator_get_pointer(slist_iterator_t it_iter)
{
assert(_slist_iterator_belong_to_slist(_SLIST_ITERATOR_CONTAINER(it_iter), it_iter));
assert(!iterator_equal(it_iter, slist_end(_SLIST_ITERATOR_CONTAINER(it_iter))));
/* char* */
if (strncmp(_GET_SLIST_TYPE_BASENAME(_SLIST_ITERATOR_CONTAINER(it_iter)), _C_STRING_TYPE, _TYPE_NAME_SIZE) == 0) {
return string_c_str((string_t*)((_slistnode_t*)_SLIST_ITERATOR_COREPOS(it_iter))->_pby_data);
} else {
return ((_slistnode_t*)_SLIST_ITERATOR_COREPOS(it_iter))->_pby_data;
}
}
/**
* Get data value pointer referenced by iterator, but ignore char*.
*/
const void* _slist_iterator_get_pointer_ignore_cstr(slist_iterator_t it_iter)
{
assert(_slist_iterator_belong_to_slist(_SLIST_ITERATOR_CONTAINER(it_iter), it_iter));
assert(!iterator_equal(it_iter, slist_end(_SLIST_ITERATOR_CONTAINER(it_iter))));
return ((_slistnode_t*)_SLIST_ITERATOR_COREPOS(it_iter))->_pby_data;
}
/**
* Return iterator reference next element.
*/
slist_iterator_t _slist_iterator_next(slist_iterator_t it_iter)
{
assert(_slist_iterator_belong_to_slist(_SLIST_ITERATOR_CONTAINER(it_iter), it_iter));
assert(!iterator_equal(it_iter, slist_end(_SLIST_ITERATOR_CONTAINER(it_iter))));
_SLIST_ITERATOR_COREPOS(it_iter) = (_byte_t*)(((_slistnode_t*)_SLIST_ITERATOR_COREPOS(it_iter))->_pt_next);
return it_iter;
}
/**
* Calculate distance between two iterators.
*/
int _slist_iterator_distance(slist_iterator_t it_first, slist_iterator_t it_second)
{
int n_distance = 0;
_slistnode_t* pt_node = NULL;
assert(_iterator_same_type(it_first, it_second));
assert(_SLIST_ITERATOR_CONTAINER(it_first) == _SLIST_ITERATOR_CONTAINER(it_second));
assert(_slist_iterator_belong_to_slist(_SLIST_ITERATOR_CONTAINER(it_first), it_first));
assert(_slist_iterator_belong_to_slist(_SLIST_ITERATOR_CONTAINER(it_second), it_second));
if (_slist_iterator_before(it_first, it_second)) {
for (pt_node = (_slistnode_t*)_SLIST_ITERATOR_COREPOS(it_first);
pt_node != (_slistnode_t*)_SLIST_ITERATOR_COREPOS(it_second);
pt_node = pt_node->_pt_next) {
n_distance++;
}
return n_distance;
} else if (_slist_iterator_before(it_second, it_first)) {
for (pt_node = (_slistnode_t*)_SLIST_ITERATOR_COREPOS(it_second);
pt_node != (_slistnode_t*)_SLIST_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 _slist_iterator_before(slist_iterator_t it_first, slist_iterator_t it_second)
{
_slistnode_t* pt_node = NULL;
assert(_iterator_same_type(it_first, it_second));
assert(_SLIST_ITERATOR_CONTAINER(it_first) == _SLIST_ITERATOR_CONTAINER(it_second));
assert(_slist_iterator_belong_to_slist(_SLIST_ITERATOR_CONTAINER(it_first), it_first));
assert(_slist_iterator_belong_to_slist(_SLIST_ITERATOR_CONTAINER(it_second), it_second));
if (_SLIST_ITERATOR_COREPOS(it_first) == _SLIST_ITERATOR_COREPOS(it_second)) {
return false;
}
for (pt_node = (_slistnode_t*)_SLIST_ITERATOR_COREPOS(it_first); pt_node != NULL; pt_node = pt_node->_pt_next) {
if (pt_node == (_slistnode_t*)_SLIST_ITERATOR_COREPOS(it_second)) {
return true;
}
}
return _SLIST_ITERATOR_COREPOS(it_second) == NULL ? true : false;
}
/** local function implementation section **/
/** eof **/