| Cobra | Inline Programs | lists, queues, and stacks | ||
|---|---|---|---|---|
| NAMEUsing Lists, Queues, and StacksSYNTAXlist_add_top(name, token) list_add_bot(name, token) list_append(name, token) list_bot(name) list_chop(name) list_del_top(name) list_del_bot(name) list_get_top(name) list_get_bot(name) list_len(name) list_new_tok() list_pop(name) list_pull(name, token) list_rel_tok(token) list_rel(name) list_tok() list_tok_rel(token) list_top(name) DESCRIPTIONThere are ten functions that can be used to manipulate collections of tokens, with synonyms for most of them. We list the synonyms in the scond To add a token to the start of list name:list_add_top(name, token) synonym: list_push(name, token)To add a token to end of list name: list_add_bot(name, token) synonym: list_append(name, token)To remove and release token from start of list name: list_del_top(name) synonym: list_pop(name)To remove and release token from end of list name: list_del_bot(name) synonym: list_chop(name)To return token at start of list name without removing it: list_get_top(name) synonym: list_top(name)To return token at end of list name without removing it: list_get_bot(name) synonym: list_bot(name)To return a new token, to use in lists list_new_tok() synonym: list_tok()To release a token never stored on any list: list_rel_tok(token) synonym: list_tok_rel(token)To remove list name: and release all tokens on it: list_rel(name) (no synonym)To return the number of tokens in list name: list_len(name) (no synonym)Lists do not need to be declared, they are created when first accessed. EXAMPLESThe typical use is illustrated by the following examples (a version of which can also be found in query-file rules/play/list_test.cobra):
	%{
		cnt++;
		n = list_tok();		# create a new list element
		n.seq = 1;		# set some of its attributes
		n.mark = cnt;
		if (.seq % 2)
		{	list_push(test, n);	# add to a list named 'test'
		} else
		{	list_append(test, n);
		}
	%}
	%{
		count = 10;
		while (count >= 0)
		{	count--;
			n = list_bot(test);
			print "removed from end: " n.mark "\n";
			list_chop(test);
		}
		n = list_top(test);
		while (n.seq > 0)	# check if we can traverse the list ourselves
		{	print "===> " n.mark "\n";
			n = n.nxt;
		}
		Stop;
	%}
	%{
		print cnt " ::: " list_len(test) "\n";
		cnt = 1;
		n = list_top(test);
		while (n.seq > 0)
		{	print cnt "..." n.mark "\n";
			list_pop(test);
			n = list_top(test);
			cnt++;
		}
		print "length: " list_len(test) "\n";
		list_rel(test);	# release
		Stop;
	%}
 | ||||
| Inline Programs Manual Tutorial | (Last Updated: 2 August 2023) | |||