util  list.h at [e02ae59ffd]

File wgsync/src/list.h artifact ba4baffdd8 part of check-in e02ae59ffd


#ifndef _ll_ffirst
#define _ll_ffirst _cat(first_,_ll_rec)
#endif

#ifndef _ll_flast
#define _ll_flast _cat(last_,_ll_rec)
#endif

#ifndef _ll_fnext
#define _ll_fnext _cat(next_,_ll_rec)
#endif

#define _LL_fn(act) _cat(_cat(_ll_ns,_),_cat(_cat(act,_), _ll_rec))
#ifndef _ll_dropfn
#define _ll_dropfn _LL_fn(drop)
#endif

#ifndef _ll_pushfn
#define _ll_pushfn _LL_fn(push)
#endif

#ifndef _ll_newfn
#define _ll_newfn _LL_fn(new)
#endif

#ifndef _ll_delete
#define _ll_delete free
#	ifdef _ll_impl
#		include <stdlib.h>
#	endif
#endif

#ifdef _ll_impl
#	define _LL_impl(...) __VA_ARGS__
#	include "def.h"
#else
#	define _LL_impl(...) ;
#endif

void _ll_pushfn
(_ll_box* box, _ll_obj* obj) _LL_impl({
	if (box -> _ll_flast)
		box -> _ll_flast -> _ll_fnext = obj;
	else box -> _ll_ffirst = obj;
	box -> _ll_flast = obj;
})

_ll_obj* _ll_newfn
(_ll_box* box) _LL_impl({
	_ll_obj* o = calloc(1, sizeof(_ll_obj));
	_ll_pushfn(box, o);
	return o;
})

void _ll_dropfn
(_ll_box* box, _ll_obj* obj) _LL_impl({
	if(box -> _ll_ffirst == obj) {
		if(box -> _ll_flast == obj) {
			box -> _ll_ffirst = box -> _ll_flast = null;
		} else {
			box -> _ll_ffirst = obj -> _ll_fnext;
		}
	} else {
		_ll_obj* a;
		if(box -> _ll_flast == obj) {
			_ll_iter (box, a) {
				if(a->_ll_fnext == obj) {
					box -> _ll_flast = a;
					a -> _ll_fnext = null;
					goto found1;
				}
			}
			_fatal("BUG in last elt deletion routine");
			found1 :;
		} else /* in the middle */ {
			_ll_iter (box, a) {
				if(a->_ll_fnext == obj) {
					a->_ll_fnext = obj -> _ll_fnext;
					goto found2;
				}
			}
			_fatal("BUG in elt deletion routine");
			found2 :;
		}
	}
	_ll_delete (obj);
})

#undef _LL_fn
#undef _LL_impl

#undef _ll_pushfn
#undef _ll_newfn
#undef _ll_dropfn

#undef _ll_ffirst
#undef _ll_flast
#undef _ll_ns
#undef _ll_box
#undef _ll_obj
#undef _ll_rec
#undef _ll_iter
#undef _ll_delete