rcx

miscellaneous C library
git clone git://git.rr3.xyz/rcx
Log | Files | Refs | README | LICENSE

decls.h (2710B)


      1 LIST_STATIC UNUSED int LIST_METHOD(resize)(LIST_T **l, usize cap);
      2 LIST_STATIC UNUSED int LIST_METHOD(reserve)(LIST_T **l, usize n);
      3 LIST_STATIC UNUSED int LIST_METHOD(insarr)(LIST_T **dst, usize i, LIST_T *src, usize n);
      4 LIST_STATIC UNUSED LIST_T LIST_METHOD(del)(LIST_T **l, usize i);
      5 
      6 static inline UNUSED void
      7 LIST_METHOD(free)(LIST_T **l) {
      8 	if (*l) {
      9 		int e = errno;
     10 		LIST_FREE(LIST_HDR(*l));
     11 		errno = e;
     12 	}
     13 	*l = 0;
     14 }
     15 
     16 static inline UNUSED usize
     17 LIST_METHOD(len)(LIST_T **l) {
     18 	return *l ? LIST_HDR(*l)->len : 0;
     19 }
     20 
     21 static inline UNUSED usize
     22 LIST_METHOD(cap)(LIST_T **l) {
     23 	return *l ? LIST_HDR(*l)->cap : 0;
     24 }
     25 
     26 static inline UNUSED LIST_T *
     27 LIST_METHOD(ptr)(LIST_T **l, isize i) {
     28 	usize len = LIST_METHOD(len)(l);
     29 	if (i < 0) i += len;
     30 	LIST_ASSERT(0 <= i && i < len,
     31 		STRINGIFY(LIST_METHOD(ptr))" called with i out of bounds");
     32 	return &(*l)[i];
     33 }
     34 
     35 static inline UNUSED LIST_T
     36 LIST_METHOD(get)(LIST_T **l, isize i) {
     37 	usize len = LIST_METHOD(len)(l);
     38 	if (i < 0) i += len;
     39 	LIST_ASSERT(0 <= i && i < len,
     40 		STRINGIFY(LIST_METHOD(get))" called with i out of bounds");
     41 	return (*l)[i];
     42 }
     43 
     44 static inline UNUSED void
     45 LIST_METHOD(set)(LIST_T **l, isize i, LIST_T e) {
     46 	usize len = LIST_METHOD(len)(l);
     47 	if (i < 0) i += len;
     48 	LIST_ASSERT(0 <= i && i < len,
     49 		STRINGIFY(LIST_METHOD(set))" called with i out of bounds");
     50 	(*l)[i] = e;
     51 }
     52 
     53 static inline UNUSED int
     54 LIST_METHOD(ins)(LIST_T **l, usize i, LIST_T e) {
     55 	return LIST_METHOD(insarr)(l, i, &e, 1);
     56 }
     57 
     58 static inline UNUSED int
     59 LIST_METHOD(inslst)(LIST_T **dst, usize i, LIST_T *src) {
     60 	return LIST_METHOD(insarr)(dst, i, src, LIST_METHOD(len)(&src));
     61 }
     62 
     63 static inline UNUSED int
     64 LIST_METHOD(catarr)(LIST_T **dst, LIST_T *src, usize n) {
     65 	return LIST_METHOD(insarr)(dst, LIST_METHOD(len)(dst), src, n);
     66 }
     67 
     68 static inline UNUSED int
     69 LIST_METHOD(catlst)(LIST_T **dst, LIST_T *src) {
     70 	return LIST_METHOD(catarr)(dst, src, LIST_METHOD(len)(&src));
     71 }
     72 
     73 static inline UNUSED int
     74 LIST_METHOD(push)(LIST_T **l, LIST_T e) {
     75 	return LIST_METHOD(catarr)(l, &e, 1);
     76 }
     77 
     78 static inline UNUSED LIST_T
     79 LIST_METHOD(pop)(LIST_T **l) {
     80 	LIST_ASSERT(LIST_METHOD(len)(l) > 0,
     81 		STRINGIFY(LIST_METHOD(pop))" called on empty list");
     82 	return LIST_METHOD(del)(l, LIST_HDR(*l)->len - 1);
     83 }
     84 
     85 static inline UNUSED LIST_T
     86 LIST_METHOD(peek)(LIST_T **l) {
     87 	LIST_ASSERT(LIST_METHOD(len)(l) > 0,
     88 		STRINGIFY(LIST_METHOD(peek))" called on empty list");
     89 	return (*l)[LIST_HDR(*l)->len - 1];
     90 }
     91 
     92 static inline UNUSED void
     93 LIST_METHOD(trunc)(LIST_T **l, usize n) {
     94 	LIST_ASSERT(n <= LIST_METHOD(len)(l),
     95 		STRINGIFY(LIST_METHOD(trunc))" called with n out of bounds");
     96 	if (*l) LIST_HDR(*l)->len = n;
     97 }
     98 
     99 static inline UNUSED void
    100 LIST_METHOD(clr)(LIST_T **l) {
    101 	LIST_METHOD(trunc)(l, 0);
    102 }