Update array (extra) foreach implementation
This commit is contained in:
		
							parent
							
								
									624efada63
								
							
						
					
					
						commit
						472639a681
					
				| @ -6,17 +6,20 @@ | ||||
| #ifndef LIBCDSB_EXTRA_ARRAY_H | ||||
| #define LIBCDSB_EXTRA_ARRAY_H | ||||
| 
 | ||||
| typedef int (*array_foreach_callback)(void* value, ssize_t index, vtype type, void* data); | ||||
| 
 | ||||
| 
 | ||||
| #define array_get(x, s, index) libcdsb_array_get(x, s, index, 0) | ||||
| #define array_pop(x, s, index) libcdsb_array_get(x, s, index, 1) | ||||
| #define array_remove(s, index) libcdsb_array_get(0, s, index, 1) | ||||
| 
 | ||||
| #define array_foreach libcdsb_array_foreach | ||||
| #define array_foreach(x, data, callback) libcdsb_array_foreach(x, data, callback, 0) | ||||
| 
 | ||||
| extern ssize_t libcdsb_array_get(vtype_value* x, vtype_array* s, ssize_t index, _Bool cut) LIBCDSB_nt__ LIBCDSB_nn2__; | ||||
| 
 | ||||
| extern ssize_t libcdsb_array_find(const vtype_array* x, const void* value, vtype value_type) LIBCDSB_nt__ LIBCDSB_nn1__; | ||||
| extern ssize_t libcdsb_array_push(      vtype_array* x, const void* value, vtype value_type) LIBCDSB_nt__ LIBCDSB_nn1__; | ||||
| 
 | ||||
| extern int libcdsb_array_foreach(const vtype_array* x, void* data, int (*callback)(void* value, ssize_t index, vtype type, void* data)) LIBCDSB_nt__ LIBCDSB_nn13__; | ||||
| extern int libcdsb_array_foreach(vtype_array* x, void* data, array_foreach_callback, _Bool flush) LIBCDSB_nt__ LIBCDSB_nn13__; | ||||
| 
 | ||||
| #endif /* LIBCDSB_EXTRA_ARRAY_H */ | ||||
|  | ||||
| @ -76,7 +76,7 @@ ssize_t libcdsb_array_get(val_t* x, arr_t* s, ssize_t i, _Bool cut) { | ||||
| 
 | ||||
| /*#####################################################################################################################*/ | ||||
| 
 | ||||
| int libcdsb_array_foreach(const vtype_array* x, void* data, int (*callback)(void* value, ssize_t index, vtype type, void* data)) { | ||||
| int libcdsb_array_foreach(vtype_array* x, void* data, array_foreach_callback callback, _Bool flush) { | ||||
| 
 | ||||
|     void*  p; | ||||
|     void*  e; | ||||
| @ -86,14 +86,19 @@ int libcdsb_array_foreach(const vtype_array* x, void* data, int (*callback)(void | ||||
|     p = x->mem; | ||||
|     e = x->mem + x->size*vtype_size(x->type); | ||||
|     n = 0; | ||||
|     r = 0; | ||||
| 
 | ||||
|     while (p < e) { | ||||
|         if ((r = callback(p, n, x->type, data))) | ||||
|             return r; | ||||
|             break; | ||||
| 
 | ||||
|         p += vtype_size(x->type); | ||||
|         ++n; | ||||
|     } | ||||
| 
 | ||||
|     return 0; | ||||
|     if (flush) { | ||||
|         free(x->mem); | ||||
|         memset(x, 0, sizeof(*x)); | ||||
|     } | ||||
|     return r; | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user