src/linked_list.c

changeset 1429
6e0c3a8a914a
parent 1426
3a89b31f0724
equal deleted inserted replaced
1428:0ac4aa1737fd 1429:6e0c3a8a914a
1124 return iter->elem_handle != NULL; 1124 return iter->elem_handle != NULL;
1125 } 1125 }
1126 1126
1127 static void cx_ll_iter_next(void *it) { 1127 static void cx_ll_iter_next(void *it) {
1128 struct cx_iterator_s *iter = it; 1128 struct cx_iterator_s *iter = it;
1129 cx_linked_list *ll = iter->src_handle;
1129 if (iter->base.remove) { 1130 if (iter->base.remove) {
1130 iter->base.remove = false; 1131 iter->base.remove = false;
1131 struct cx_list_s *list = iter->src_handle.m; 1132 struct cx_list_s *list = iter->src_handle;
1132 cx_linked_list *ll = iter->src_handle.m;
1133 char *node = iter->elem_handle; 1133 char *node = iter->elem_handle;
1134 iter->elem_handle = CX_LL_PTR(node, ll->loc_next); 1134 iter->elem_handle = CX_LL_PTR(node, ll->loc_next);
1135 cx_invoke_destructor(list, node + ll->loc_data); 1135 cx_invoke_destructor(list, node + ll->loc_data);
1136 cx_linked_list_remove(&ll->begin, &ll->end, 1136 cx_linked_list_remove(&ll->begin, &ll->end,
1137 ll->loc_prev, ll->loc_next, node); 1137 ll->loc_prev, ll->loc_next, node);
1138 list->collection.size--; 1138 list->collection.size--;
1139 iter->elem_count--; 1139 iter->elem_count--;
1140 cxFree(list->collection.allocator, node); 1140 cxFree(list->collection.allocator, node);
1141 } else { 1141 } else {
1142 const cx_linked_list *ll = iter->src_handle.c;
1143 iter->index++; 1142 iter->index++;
1144 void *node = iter->elem_handle; 1143 void *node = iter->elem_handle;
1145 iter->elem_handle = CX_LL_PTR(node, ll->loc_next); 1144 iter->elem_handle = CX_LL_PTR(node, ll->loc_next);
1146 } 1145 }
1147 } 1146 }
1148 1147
1149 static void cx_ll_iter_prev(void *it) { 1148 static void cx_ll_iter_prev(void *it) {
1150 struct cx_iterator_s *iter = it; 1149 struct cx_iterator_s *iter = it;
1150 cx_linked_list *ll = iter->src_handle;
1151 if (iter->base.remove) { 1151 if (iter->base.remove) {
1152 iter->base.remove = false; 1152 iter->base.remove = false;
1153 struct cx_list_s *list = iter->src_handle.m; 1153 struct cx_list_s *list = iter->src_handle;
1154 cx_linked_list *ll = iter->src_handle.m;
1155 char *node = iter->elem_handle; 1154 char *node = iter->elem_handle;
1156 iter->elem_handle = CX_LL_PTR(node, ll->loc_prev); 1155 iter->elem_handle = CX_LL_PTR(node, ll->loc_prev);
1157 iter->index--; 1156 iter->index--;
1158 cx_invoke_destructor(list, node + ll->loc_data); 1157 cx_invoke_destructor(list, node + ll->loc_data);
1159 cx_linked_list_remove(&ll->begin, &ll->end, 1158 cx_linked_list_remove(&ll->begin, &ll->end,
1160 ll->loc_prev, ll->loc_next, node); 1159 ll->loc_prev, ll->loc_next, node);
1161 list->collection.size--; 1160 list->collection.size--;
1162 iter->elem_count--; 1161 iter->elem_count--;
1163 cxFree(list->collection.allocator, node); 1162 cxFree(list->collection.allocator, node);
1164 } else { 1163 } else {
1165 const cx_linked_list *ll = iter->src_handle.c;
1166 iter->index--; 1164 iter->index--;
1167 char *node = iter->elem_handle; 1165 char *node = iter->elem_handle;
1168 iter->elem_handle = CX_LL_PTR(node, ll->loc_prev); 1166 iter->elem_handle = CX_LL_PTR(node, ll->loc_prev);
1169 } 1167 }
1170 } 1168 }
1171 1169
1172 static void *cx_ll_iter_current(const void *it) { 1170 static void *cx_ll_iter_current(const void *it) {
1173 const struct cx_iterator_s *iter = it; 1171 const struct cx_iterator_s *iter = it;
1174 const cx_linked_list *ll = iter->src_handle.c; 1172 const cx_linked_list *ll = iter->src_handle;
1175 char *node = iter->elem_handle; 1173 char *node = iter->elem_handle;
1176 return node + ll->loc_data; 1174 return node + ll->loc_data;
1177 } 1175 }
1178 1176
1179 static CxIterator cx_ll_iterator( 1177 static CxIterator cx_ll_iterator(
1181 size_t index, 1179 size_t index,
1182 bool backwards 1180 bool backwards
1183 ) { 1181 ) {
1184 CxIterator iter; 1182 CxIterator iter;
1185 iter.index = index; 1183 iter.index = index;
1186 iter.src_handle.c = list; 1184 iter.src_handle = (void*)list;
1187 iter.elem_handle = cx_ll_node_at((const cx_linked_list *) list, index); 1185 iter.elem_handle = cx_ll_node_at((const cx_linked_list *) list, index);
1188 iter.elem_size = list->collection.elem_size; 1186 iter.elem_size = list->collection.elem_size;
1189 iter.elem_count = list->collection.size; 1187 iter.elem_count = list->collection.size;
1190 iter.base.valid = cx_ll_iter_valid; 1188 iter.base.valid = cx_ll_iter_valid;
1191 iter.base.current = cx_ll_iter_current; 1189 iter.base.current = cx_ll_iter_current;
1192 iter.base.next = backwards ? cx_ll_iter_prev : cx_ll_iter_next; 1190 iter.base.next = backwards ? cx_ll_iter_prev : cx_ll_iter_next;
1193 iter.base.mutating = false; 1191 iter.base.allow_remove = true;
1194 iter.base.remove = false; 1192 iter.base.remove = false;
1195 return iter; 1193 return iter;
1196 } 1194 }
1197 1195
1198 static int cx_ll_insert_iter( 1196 static int cx_ll_insert_iter(
1199 CxIterator *iter, 1197 CxIterator *iter,
1200 const void *elem, 1198 const void *elem,
1201 int prepend 1199 int prepend
1202 ) { 1200 ) {
1203 struct cx_list_s *list = iter->src_handle.m; 1201 struct cx_list_s *list = iter->src_handle;
1204 cx_linked_list *ll = iter->src_handle.m; 1202 cx_linked_list *ll = iter->src_handle;
1205 void *node = iter->elem_handle; 1203 void *node = iter->elem_handle;
1206 if (node != NULL) { 1204 if (node != NULL) {
1207 assert(prepend >= 0 && prepend <= 1); 1205 assert(prepend >= 0 && prepend <= 1);
1208 void *choice[2] = {node, CX_LL_PTR(node, ll->loc_prev)}; 1206 void *choice[2] = {node, CX_LL_PTR(node, ll->loc_prev)};
1209 int result = cx_ll_insert_at(list, choice[prepend], elem); 1207 int result = cx_ll_insert_at(list, choice[prepend], elem);

mercurial