-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathlist.h
98 lines (86 loc) · 3.26 KB
/
list.h
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/******************************************************************************
Copyright (c) 1992, 1995, 1996 Xerox Corporation. All rights reserved.
Portions of this code were written by Stephen White, aka ghond.
Use and copying of this software and preparation of derivative works based
upon this software are permitted. Any distribution of this software or
derivative works must comply with all applicable United States export
control laws. This software is made available AS IS, and Xerox Corporation
makes no warranty about the software, its performance or its conformity to
any specification. Any person obtaining a copy of this software is requested
to send their name and post office or electronic mail address to:
Pavel Curtis
Xerox PARC
3333 Coyote Hill Rd.
Palo Alto, CA 94304
*****************************************************************************/
#include "structures.h"
#include "streams.h"
extern Var new_list(int size);
extern void destroy_list(Var list);
extern Var list_dup(Var list);
extern Var listappend(Var list, Var value);
extern Var listinsert(Var list, Var value, int pos);
extern Var listdelete(Var list, int pos);
extern Var listset(Var list, Var value, int pos);
extern Var listrangeset(Var list, int from, int to, Var value);
extern Var listconcat(Var first, Var second);
extern Var setadd(Var list, Var value);
extern Var setremove(Var list, Var value);
extern Var sublist(Var list, int lower, int upper);
extern int listequal(Var lhs, Var rhs, int case_matters);
extern int list_sizeof(Var *list);
typedef int (*listfunc) (Var value, void *data, int first);
extern int listforeach(Var list, listfunc func, void *data);
extern Var strrangeset(Var list, int from, int to, Var value);
extern Var substr(Var str, int lower, int upper);
extern Var strget(Var str, int i);
extern const char *value2str(Var);
extern void unparse_value(Stream *, Var);
/*
* Returns the length of the given list `l'. Does *not* check to
* ensure `l' is, in fact, a list.
*/
static inline int32
listlength(Var l)
{
return l.v.list[0].v.num;
}
/*
* Wraps `v' in a list if it is not already a list. Consumes `v', so
* you may want to var_ref/var_dup `v'. Currently, this function is
* called by functions that operate on an object's parents, which can
* be either an object reference (TYPE_OBJ) or a list of object
* references (TYPE_LIST).
*/
static inline Var
enlist_var(Var v)
{
if (TYPE_LIST == v.type)
return v;
Var r = new_list(1);
r.v.list[1] = v;
return r;
}
/*
* Iterate over the values in the list `lst'. Sets `val' to each
* value, in turn. `itr' and `cnt' must be int variables. In the
* body of the statement, they hold the current index and total count,
* respectively. Use the macro as follows (assuming you already have
* a list in `items'):
* Var item;
* int i, c;
* FOR_EACH(item, items, i, c) {
* printf("%d of %d, item = %s\n", i, c, value_to_literal(item));
* }
*/
#define FOR_EACH(val, lst, idx, cnt) \
for (idx = 1, cnt = lst.v.list[0].v.num; \
idx <= cnt && (val = lst.v.list[idx], 1); \
idx++)
/*
* Pop the first value off `stck' and put it in `tp'.
*/
#define POP_TOP(tp, stck) \
tp = var_ref(stck.v.list[1]); \
stck = listdelete(stck, 1);