File: mlist.c

package info (click to toggle)
glam2 1064-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 956 kB
  • sloc: ansic: 6,925; xml: 757; asm: 74; makefile: 54; sh: 11
file content (63 lines) | stat: -rw-r--r-- 1,777 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "mlist.h"

ml_type MkMList(long M, long N)
/******************************************************************
  N = number of lists; M = number of elements.
  sets all N lists to empty and creates the list of free cells.
 ******************************************************************/
{
	ml_type	L;
	long	i;

	MEW(L,1,multi_list_type);
	L->M = M; L->N = N;
	NEW(L->first,N+2,long);	/* all set to zero = empty lists */
	MEW(L->item,M+2,long); MEW(L->next,M+2,long);
	for(L->free=1, i=1; i < M; i++){
		L->next[i]=i+1;
	}
	L->next[i] = 0;
	return L;
}

long	Add2MList(register long i, register long n, register ml_type L)
/******************************************************************
 add item i to the nth list.
 ******************************************************************/
 {
	register long	new,t;

	if(n > L->N || n <= 0) mlist_error("list number out of range");
	if((new = L->free) == 0) mlist_error("out of memory");
	L->free = L->next[new]; 
	t = L->first[n];		/* first[n] = t[?:?]->  */
	L->item[new] = i;
	L->next[new] = t;		/** new[item:t]-> t[?:?]-> **/
	L->first[n] = new; 		/** first[n] = new[i:t]-> t[?:?]-> */
        return 0;
}

long	GetListMList(register long *list, register long n, 
	register ml_type L)
/******************************************************************
 convert list to contain the nth list in L; returns list length.
 WARNING: assumes list[ ] is long enough.
 ******************************************************************/
{
	register long	i,c;
	
	for(i=0, c = L->first[n] ; c != 0; c=L->next[c]){
		list[i++] = L->item[c];
	}
	return i;
}

void    NilMList(ml_type L)
{
	free(L->first);
	free(L->item);
	free(L->next);
	free(L);
}

void    mlist_error(char *s){ fprintf(stderr,"mlist: %s\n",s); exit(1); }