1 /*
2 $Id: listobj.h 2526 2021-03-14 23:02:07Z soci $
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 */
19 #ifndef LISTOBJ_H
20 #define LISTOBJ_H
21 #include "obj.h"
22 #include "values.h"
23 #include "stdbool.h"
24
25 extern struct Type *const LIST_OBJ;
26 extern struct Type *const TUPLE_OBJ;
27 extern struct Type *const ADDRLIST_OBJ;
28 extern struct Type *const COLONLIST_OBJ;
29
30 typedef struct List {
31 Obj v;
32 size_t len;
33 Obj **data;
34 union {
35 Obj *val[5];
36 struct {
37 size_t max;
38 int hash;
39 } s;
40 } u;
41 } List;
42 typedef struct List Tuple;
43 typedef struct List Addrlist;
44 typedef struct List Colonlist;
45
46 #define List(a) ((List *)(1 ? (a) : (Obj *)(List *)(a)))
47 #define Tuple(a) ((Tuple *)(1 ? (a) : (Obj *)(Tuple *)(a)))
48 #define Addrlist(a) ((Addrlist *)(1 ? (a) : (Obj *)(Addrlist *)(a)))
49 #define Colonlist(a) ((Colonlist *)(1 ? (a) : (Obj *)(Colonlist *)(a)))
50
51 extern Obj *const null_tuple;
52 extern Obj *const null_list;
53 extern Obj *const null_addrlist;
54
55 extern void listobj_init(void);
56 extern void listobj_names(void);
57 extern void listobj_destroy(void);
58
new_list(void)59 static inline MUST_CHECK List *new_list(void) {
60 return List(val_alloc(LIST_OBJ));
61 }
new_addrlist(void)62 static inline MUST_CHECK Addrlist *new_addrlist(void) {
63 return Addrlist(val_alloc(ADDRLIST_OBJ));
64 }
new_colonlist(void)65 static inline MUST_CHECK Colonlist *new_colonlist(void) {
66 return Colonlist(val_alloc(COLONLIST_OBJ));
67 }
68
69 struct sliceparam_s {
70 uval_t length;
71 ival_t offset, end, step;
72 };
73
74 struct indexoffs_s {
75 Obj *val;
76 size_t len;
77 size_t offs;
78 linepos_t epoint;
79 };
80
81 extern MUST_CHECK Obj *indexoffs(struct indexoffs_s *);
82 extern MUST_CHECK Obj *sliceparams(struct sliceparam_s *, const struct indexoffs_s *);
83 extern MUST_CHECK Tuple *new_tuple(size_t);
84 extern Obj **list_create_elements(List *, size_t);
85 extern MUST_CHECK bool list_extend(List *);
86 extern void list_shrink(List *, size_t);
87 #endif
88