src/array_list.c

changeset 1429
6e0c3a8a914a
parent 1425
83284b289430
equal deleted inserted replaced
1428:0ac4aa1737fd 1429:6e0c3a8a914a
878 static int cx_arl_insert_iter( 878 static int cx_arl_insert_iter(
879 struct cx_iterator_s *iter, 879 struct cx_iterator_s *iter,
880 const void *elem, 880 const void *elem,
881 int prepend 881 int prepend
882 ) { 882 ) {
883 struct cx_list_s *list = iter->src_handle.m; 883 struct cx_list_s *list = iter->src_handle;
884 if (iter->index < list->collection.size) { 884 if (iter->index < list->collection.size) {
885 if (cx_arl_insert_element(list, 885 if (cx_arl_insert_element(list,
886 iter->index + 1 - prepend, elem) == NULL) { 886 iter->index + 1 - prepend, elem) == NULL) {
887 return 1; 887 return 1;
888 } 888 }
1089 } 1089 }
1090 } 1090 }
1091 1091
1092 static bool cx_arl_iter_valid(const void *it) { 1092 static bool cx_arl_iter_valid(const void *it) {
1093 const struct cx_iterator_s *iter = it; 1093 const struct cx_iterator_s *iter = it;
1094 const struct cx_list_s *list = iter->src_handle.c; 1094 const struct cx_list_s *list = iter->src_handle;
1095 return iter->index < list->collection.size; 1095 return iter->index < list->collection.size;
1096 } 1096 }
1097 1097
1098 static void *cx_arl_iter_current(const void *it) { 1098 static void *cx_arl_iter_current(const void *it) {
1099 const struct cx_iterator_s *iter = it; 1099 const struct cx_iterator_s *iter = it;
1102 1102
1103 static void cx_arl_iter_next(void *it) { 1103 static void cx_arl_iter_next(void *it) {
1104 struct cx_iterator_s *iter = it; 1104 struct cx_iterator_s *iter = it;
1105 if (iter->base.remove) { 1105 if (iter->base.remove) {
1106 iter->base.remove = false; 1106 iter->base.remove = false;
1107 cx_arl_remove(iter->src_handle.m, iter->index, 1, NULL); 1107 cx_arl_remove(iter->src_handle, iter->index, 1, NULL);
1108 iter->elem_count--; 1108 iter->elem_count--;
1109 } else { 1109 } else {
1110 iter->index++; 1110 iter->index++;
1111 iter->elem_handle = 1111 iter->elem_handle =
1112 ((char *) iter->elem_handle) 1112 ((char *) iter->elem_handle)
1113 + ((const struct cx_list_s *) iter->src_handle.c)->collection.elem_size; 1113 + ((const struct cx_list_s *) iter->src_handle)->collection.elem_size;
1114 } 1114 }
1115 } 1115 }
1116 1116
1117 static void cx_arl_iter_prev(void *it) { 1117 static void cx_arl_iter_prev(void *it) {
1118 struct cx_iterator_s *iter = it; 1118 struct cx_iterator_s *iter = it;
1119 const cx_array_list *list = iter->src_handle.c;
1120 if (iter->base.remove) { 1119 if (iter->base.remove) {
1121 iter->base.remove = false; 1120 iter->base.remove = false;
1122 cx_arl_remove(iter->src_handle.m, iter->index, 1, NULL); 1121 cx_arl_remove(iter->src_handle, iter->index, 1, NULL);
1123 iter->elem_count--; 1122 iter->elem_count--;
1124 } 1123 }
1125 iter->index--; 1124 iter->index--;
1125 cx_array_list *list = iter->src_handle;
1126 if (iter->index < list->base.collection.size) { 1126 if (iter->index < list->base.collection.size) {
1127 iter->elem_handle = ((char *) list->data) 1127 iter->elem_handle = ((char *) list->data)
1128 + iter->index * list->base.collection.elem_size; 1128 + iter->index * list->base.collection.elem_size;
1129 } 1129 }
1130 } 1130 }
1136 bool backwards 1136 bool backwards
1137 ) { 1137 ) {
1138 struct cx_iterator_s iter; 1138 struct cx_iterator_s iter;
1139 1139
1140 iter.index = index; 1140 iter.index = index;
1141 iter.src_handle.c = list; 1141 iter.src_handle = (void*)list;
1142 iter.elem_handle = cx_arl_at(list, index); 1142 iter.elem_handle = cx_arl_at(list, index);
1143 iter.elem_size = list->collection.elem_size; 1143 iter.elem_size = list->collection.elem_size;
1144 iter.elem_count = list->collection.size; 1144 iter.elem_count = list->collection.size;
1145 iter.base.valid = cx_arl_iter_valid; 1145 iter.base.valid = cx_arl_iter_valid;
1146 iter.base.current = cx_arl_iter_current; 1146 iter.base.current = cx_arl_iter_current;
1147 iter.base.next = backwards ? cx_arl_iter_prev : cx_arl_iter_next; 1147 iter.base.next = backwards ? cx_arl_iter_prev : cx_arl_iter_next;
1148 iter.base.remove = false; 1148 iter.base.remove = false;
1149 iter.base.mutating = false; 1149 iter.base.allow_remove = true;
1150 1150
1151 return iter; 1151 return iter;
1152 } 1152 }
1153 1153
1154 static cx_list_class cx_array_list_class = { 1154 static cx_list_class cx_array_list_class = {

mercurial