Add list extra & generics
This commit is contained in:
		
							parent
							
								
									22001e480e
								
							
						
					
					
						commit
						9bd9440a49
					
				
							
								
								
									
										161
									
								
								src/list/extra.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										161
									
								
								src/list/extra.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,161 @@ | ||||
| /* This software is licensed by the MIT License, see LICENSE file */ | ||||
| /*                                Copyright © 2022 Gregory Lirent */ | ||||
| 
 | ||||
| #include "include.h" | ||||
| 
 | ||||
| /*#####################################################################################################################*/ | ||||
| 
 | ||||
| static void lnode_cut(val_t* x, list_t* s, lnode_t* cur) { | ||||
|     if (!is_null(x)) { | ||||
|         value_set(x, cur->node, cur->type, VF_WRITEABLE|VF_REMOVABLE); | ||||
|     } else vnode_free(&cur->node, cur->type); | ||||
| 
 | ||||
|     if (!is_null(cur->prev)) { | ||||
|         cur->prev->next = cur->next; | ||||
|     } else s->first = cur->next; | ||||
| 
 | ||||
|     if (!is_null(cur->next)) { | ||||
|         cur->next->prev = cur->prev; | ||||
|     } else s->last = cur->prev; | ||||
| 
 | ||||
|     free(cur); | ||||
| } | ||||
| 
 | ||||
| /*#####################################################################################################################*/ | ||||
| 
 | ||||
| ssize_t libcdsb_list_get(val_t* x, list_t* s, ssize_t i, _Bool cut) { | ||||
| 
 | ||||
|     ldir_t dir; | ||||
|     lnode_t* c; | ||||
|     size_t   n; | ||||
|     int    cmp; | ||||
| 
 | ||||
|     if (i < 0) { | ||||
|         n   = (i = ~i); | ||||
|         dir = LD_PREV; | ||||
|     } else { | ||||
|         n   = i; | ||||
|         dir = LD_NEXT; | ||||
|     } | ||||
| 
 | ||||
|     c = ldir_dir((lnode_t*)s, dir); | ||||
| 
 | ||||
|     memset(x, 0, sizeof(*x)); | ||||
| 
 | ||||
|     while (n && !is_null(c)) { | ||||
|         c = ldir_dir(c, dir); | ||||
|         --n; | ||||
|     } | ||||
| 
 | ||||
|     if (n || is_null(c)) return -1; | ||||
| 
 | ||||
|     if (!cut && !is_null(x)) { | ||||
|         value_set(x, &c->node, c->type, VF_WRITEABLE|VF_CHANGEABLE); | ||||
|     } else if (cut) lnode_cut(x, s, c); | ||||
| 
 | ||||
|     return i; | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| /*#####################################################################################################################*/ | ||||
| 
 | ||||
| ssize_t libcdsb_list_find(val_t* x, list_t* s, const void* v, vtype t, _Bool r, _Bool cut) { | ||||
|     ldir_t dir = r ? LD_PREV : LD_NEXT; | ||||
| 
 | ||||
|     lnode_t* c; | ||||
|     ssize_t  i; | ||||
|     int    cmp; | ||||
| 
 | ||||
|     c = ldir_dir((lnode_t*)s, dir); | ||||
|     i = 0; | ||||
| 
 | ||||
|     memset(x, 0, sizeof(*x)); | ||||
| 
 | ||||
|     while (!is_null(c)) { | ||||
|         cmp = vtype_compare(vnode_peek(c->node, c->type), c->type, v, t); | ||||
| 
 | ||||
|         if (cmp == 0) { | ||||
|             if (!cut && !is_null(x)) { | ||||
|                 value_set(x, &c->node, c->type, VF_WRITEABLE|VF_CHANGEABLE); | ||||
|             } else if (cut) lnode_cut(x, s, c); | ||||
| 
 | ||||
|             return i; | ||||
|         } | ||||
| 
 | ||||
|         c = ldir_dir(c, dir); | ||||
|         ++i; | ||||
|     } | ||||
| 
 | ||||
|     return -1; | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| size_t libcdsb_list_count(const list_t* s, const void* v, vtype t) { | ||||
| 
 | ||||
|     lnode_t* c; | ||||
|     size_t   n; | ||||
|     int    cmp; | ||||
| 
 | ||||
|     c = s->first; | ||||
|     n = 0; | ||||
| 
 | ||||
|     while (!is_null(c)) { | ||||
|         cmp = vtype_compare(vnode_peek(c->node, c->type), c->type, v, t); | ||||
| 
 | ||||
|         if (cmp == 0) ++n; | ||||
| 
 | ||||
|         c = c->next; | ||||
|     } | ||||
| 
 | ||||
|     return n; | ||||
| } | ||||
| 
 | ||||
| /*#####################################################################################################################*/ | ||||
| 
 | ||||
| 
 | ||||
| _Bool libcdsb_list_update(list_t* x, ssize_t i, const void* v, vtype t, int ins) { | ||||
| 
 | ||||
|     ldir_t dir; | ||||
|     lnode_t* c; | ||||
| 
 | ||||
|     if (i < 0) { | ||||
|         i   = ~i; | ||||
|         dir = LD_PREV; | ||||
|     } else dir = LD_NEXT; | ||||
| 
 | ||||
|     c = ldir_dir((lnode_t*)x, dir); | ||||
| 
 | ||||
|     while (i && !is_null(c)) { | ||||
|         c = ldir_dir(c, dir); | ||||
|         --i; | ||||
|     } | ||||
| 
 | ||||
|     if (i && dir == LD_PREV) { | ||||
|         c = x->first; | ||||
|     } else if (i) return false; | ||||
| 
 | ||||
|     if (is_null(c)) { | ||||
|         x->first = x->last = c = calloc(sizeof(*c), 1); | ||||
|     } else if (ins) { | ||||
|         lnode_t *x = malloc(sizeof(*x)); | ||||
| 
 | ||||
|         dir = (ins < 0) ? LD_PREV : LD_NEXT; | ||||
| 
 | ||||
|         ldir_dir(x, dir) = ldir_dir(c, dir); | ||||
|         ldir_inv(x, dir) = c; | ||||
| 
 | ||||
|         c = x; | ||||
| 
 | ||||
|         if (!is_null(ldir_dir(c, dir))) { | ||||
|             ldir_inv(ldir_dir(c, dir), dir) = c; | ||||
|         } | ||||
| 
 | ||||
|         ldir_dir(ldir_inv(c, dir), dir) = c; | ||||
| 
 | ||||
|     } else vnode_free(&c->node, c->type); | ||||
| 
 | ||||
|     c->node = vnode_create(v, t); | ||||
|     c->type = t; | ||||
| 
 | ||||
|     return true; | ||||
| } | ||||
							
								
								
									
										64
									
								
								src/list/generics.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								src/list/generics.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,64 @@ | ||||
| /* This software is licensed by the MIT License, see LICENSE file */ | ||||
| /*                                Copyright © 2022 Gregory Lirent */ | ||||
| 
 | ||||
| #include "include.h" | ||||
| 
 | ||||
| ssize_t libcdsb_list_find_pointer(val_t* x, list_t* s, const   void* v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, &v, vtypeof(&v), r, cut);  } | ||||
| ssize_t libcdsb_list_find_cstring(val_t* x, list_t* s, const   char* v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, &v, vtypeof(&v), r, cut);  } | ||||
| ssize_t libcdsb_list_find_string (val_t* x, list_t* s, const  str_t* v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s,  v, vtypeof( v), r, cut);  } | ||||
| ssize_t libcdsb_list_find_array  (val_t* x, list_t* s, const  arr_t* v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s,  v, vtypeof( v), r, cut);  } | ||||
| ssize_t libcdsb_list_find_list   (val_t* x, list_t* s, const list_t* v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s,  v, vtypeof( v), r, cut);  } | ||||
| ssize_t libcdsb_list_find_map    (val_t* x, list_t* s, const  map_t* v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s,  v, vtypeof( v), r, cut);  } | ||||
| ssize_t libcdsb_list_find_vset   (val_t* x, list_t* s, const  set_t* v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s,  v, vtypeof( v), r, cut);  } | ||||
| ssize_t libcdsb_list_find_boolean(val_t* x, list_t* s,         _Bool v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, &v, vtypeof(&v), r, cut);  } | ||||
| ssize_t libcdsb_list_find_int8   (val_t* x, list_t* s,          s8_t v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, &v, vtypeof(&v), r, cut);  } | ||||
| ssize_t libcdsb_list_find_int16  (val_t* x, list_t* s,         s16_t v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, &v, vtypeof(&v), r, cut);  } | ||||
| ssize_t libcdsb_list_find_int32  (val_t* x, list_t* s,         s32_t v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, &v, vtypeof(&v), r, cut);  } | ||||
| ssize_t libcdsb_list_find_int64  (val_t* x, list_t* s,         s64_t v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, &v, vtypeof(&v), r, cut);  } | ||||
| ssize_t libcdsb_list_find_uint8  (val_t* x, list_t* s,          u8_t v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, &v, vtypeof(&v), r, cut);  } | ||||
| ssize_t libcdsb_list_find_uint16 (val_t* x, list_t* s,         u16_t v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, &v, vtypeof(&v), r, cut);  } | ||||
| ssize_t libcdsb_list_find_uint32 (val_t* x, list_t* s,         u32_t v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, &v, vtypeof(&v), r, cut);  } | ||||
| ssize_t libcdsb_list_find_uint64 (val_t* x, list_t* s,         u64_t v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, &v, vtypeof(&v), r, cut);  } | ||||
| ssize_t libcdsb_list_find_float  (val_t* x, list_t* s,          fl_t v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, &v, vtypeof(&v), r, cut);  } | ||||
| ssize_t libcdsb_list_find_double (val_t* x, list_t* s,         dbl_t v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, &v, vtypeof(&v), r, cut);  } | ||||
| ssize_t libcdsb_list_find_ldouble(val_t* x, list_t* s,        ldbl_t v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, &v, vtypeof(&v), r, cut);  } | ||||
| 
 | ||||
| size_t libcdsb_list_count_pointer(const list_t* s, const   void* v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); } | ||||
| size_t libcdsb_list_count_cstring(const list_t* s, const   char* v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); } | ||||
| size_t libcdsb_list_count_string (const list_t* s, const  str_t* v) { return libcdsb_list_count((void*)s,  v, vtypeof( v)); } | ||||
| size_t libcdsb_list_count_array  (const list_t* s, const  arr_t* v) { return libcdsb_list_count((void*)s,  v, vtypeof( v)); } | ||||
| size_t libcdsb_list_count_list   (const list_t* s, const list_t* v) { return libcdsb_list_count((void*)s,  v, vtypeof( v)); } | ||||
| size_t libcdsb_list_count_map    (const list_t* s, const  map_t* v) { return libcdsb_list_count((void*)s,  v, vtypeof( v)); } | ||||
| size_t libcdsb_list_count_vset   (const list_t* s, const  set_t* v) { return libcdsb_list_count((void*)s,  v, vtypeof( v)); } | ||||
| size_t libcdsb_list_count_boolean(const list_t* s,         _Bool v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); } | ||||
| size_t libcdsb_list_count_int8   (const list_t* s,          s8_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); } | ||||
| size_t libcdsb_list_count_int16  (const list_t* s,         s16_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); } | ||||
| size_t libcdsb_list_count_int32  (const list_t* s,         s32_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); } | ||||
| size_t libcdsb_list_count_int64  (const list_t* s,         s64_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); } | ||||
| size_t libcdsb_list_count_uint8  (const list_t* s,          u8_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); } | ||||
| size_t libcdsb_list_count_uint16 (const list_t* s,         u16_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); } | ||||
| size_t libcdsb_list_count_uint32 (const list_t* s,         u32_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); } | ||||
| size_t libcdsb_list_count_uint64 (const list_t* s,         u64_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); } | ||||
| size_t libcdsb_list_count_float  (const list_t* s,          fl_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); } | ||||
| size_t libcdsb_list_count_double (const list_t* s,         dbl_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); } | ||||
| size_t libcdsb_list_count_ldouble(const list_t* s,        ldbl_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); } | ||||
| 
 | ||||
| _Bool libcdsb_list_update_pointer(list_t* x, ssize_t i, const   void* v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins);  } | ||||
| _Bool libcdsb_list_update_cstring(list_t* x, ssize_t i, const   char* v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins);  } | ||||
| _Bool libcdsb_list_update_string (list_t* x, ssize_t i, const  str_t* v, int ins) { return libcdsb_list_update(x, i,  v, vtypeof( v), ins);  } | ||||
| _Bool libcdsb_list_update_array  (list_t* x, ssize_t i, const  arr_t* v, int ins) { return libcdsb_list_update(x, i,  v, vtypeof( v), ins);  } | ||||
| _Bool libcdsb_list_update_list   (list_t* x, ssize_t i, const list_t* v, int ins) { return libcdsb_list_update(x, i,  v, vtypeof( v), ins);  } | ||||
| _Bool libcdsb_list_update_map    (list_t* x, ssize_t i, const  map_t* v, int ins) { return libcdsb_list_update(x, i,  v, vtypeof( v), ins);  } | ||||
| _Bool libcdsb_list_update_vset   (list_t* x, ssize_t i, const  set_t* v, int ins) { return libcdsb_list_update(x, i,  v, vtypeof( v), ins);  } | ||||
| _Bool libcdsb_list_update_boolean(list_t* x, ssize_t i,         _Bool v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins);  } | ||||
| _Bool libcdsb_list_update_int8   (list_t* x, ssize_t i,          s8_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins);  } | ||||
| _Bool libcdsb_list_update_int16  (list_t* x, ssize_t i,         s16_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins);  } | ||||
| _Bool libcdsb_list_update_int32  (list_t* x, ssize_t i,         s32_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins);  } | ||||
| _Bool libcdsb_list_update_int64  (list_t* x, ssize_t i,         s64_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins);  } | ||||
| _Bool libcdsb_list_update_uint8  (list_t* x, ssize_t i,          u8_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins);  } | ||||
| _Bool libcdsb_list_update_uint16 (list_t* x, ssize_t i,         u16_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins);  } | ||||
| _Bool libcdsb_list_update_uint32 (list_t* x, ssize_t i,         u32_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins);  } | ||||
| _Bool libcdsb_list_update_uint64 (list_t* x, ssize_t i,         u64_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins);  } | ||||
| _Bool libcdsb_list_update_float  (list_t* x, ssize_t i,          fl_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins);  } | ||||
| _Bool libcdsb_list_update_double (list_t* x, ssize_t i,         dbl_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins);  } | ||||
| _Bool libcdsb_list_update_ldouble(list_t* x, ssize_t i,        ldbl_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins);  } | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user