forked from swissmicros/SDKdemo
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathlist.h
404 lines (346 loc) · 12.1 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#ifndef LIST_H
#define LIST_H
// ****************************************************************************
// list.h DB48X project
// ****************************************************************************
//
// File Description:
//
// RPL list objects
//
// A list is a sequence of distinct objects
//
//
//
//
//
//
// ****************************************************************************
// (C) 2022 Christophe de Dinechin <[email protected]>
// This software is licensed under the terms outlined in LICENSE.txt
// ****************************************************************************
// This file is part of DB48X.
//
// DB48X is free software: you can redistribute it and/or modify
// it under the terms outlined in the LICENSE.txt file
//
// DB48X is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// ****************************************************************************
//
// Payload format:
//
// A list is a sequence of bytes containing:
// - The type ID
// - The LEB128-encoded length of the payload
// - Each object in the list in turn
//
// To save space, there is no explicit marker for the end of list
#include "command.h"
#include "object.h"
#include "text.h"
GCP(list);
GCP(unit);
GCP(symbol);
GCP(expression);
struct list : text
// ----------------------------------------------------------------------------
// RPL list type
// ----------------------------------------------------------------------------
{
list(id type, gcbytes bytes, size_t len): text(type, bytes, len)
{ }
template <typename... Args>
list(id type, const gcp<Args> & ...args): text(type, utf8(""), 0)
{
byte *p = (byte *) payload();
size_t sz = required_args_memory(args...);
p = leb128(p, sz);
copy(p, args...);
}
static size_t required_memory(id i, gcbytes UNUSED bytes, size_t len)
{
return text::required_memory(i, bytes, len);
}
static list_p make(gcbytes bytes, size_t len)
{
return rt.make<list>(bytes, len);
}
static list_p make(id ty, gcbytes bytes, size_t len)
{
return rt.make<list>(ty, bytes, len);
}
template<typename ...Args>
static list_p make(id ty, const gcp<Args> &...args)
{
return rt.make<list>(ty, args...);
}
template<typename ...Args>
static list_p make(const gcp<Args> &...args)
{
return rt.make<list>(ID_list, args...);
}
template <typename Arg>
static size_t required_args_memory(const gcp<Arg> &arg)
{
return arg->size();
}
template <typename Arg, typename ...Args>
static size_t required_args_memory(const gcp<Arg> &arg,
const gcp<Args> &...args)
{
return arg->size() + required_args_memory(args...);
}
template<typename ...Args>
static size_t required_memory(id i, const gcp<Args> &...args)
{
size_t sz = required_args_memory(args...);
return leb128size(i) + leb128size(sz) + sz;
}
template<typename Arg>
static void copy(byte *p, const gcp<Arg> &arg)
{
size_t sz = arg->size();
memmove(p, +arg, sz);
}
template<typename Arg, typename ...Args>
static void copy(byte *p, const gcp<Arg> &arg, const gcp<Args> &...args)
{
size_t sz = arg->size();
memmove(p, +arg, sz);
p += sz;
copy(p, args...);
}
object_p objects(size_t *size = nullptr) const
{
return object_p(value(size));
}
list_p names(bool units = false, id type = ID_list) const;
bool names_enumerate(size_t depth,
bool with_units,
list_g &forbidden) const;
static bool names_insert(size_t depth,
symbol_p sym,
unit_p unit,
list_g &forbidden);
// Iterator, built in a way that is robust to garbage collection in loops
struct iterator
{
typedef object_p value_type;
typedef size_t difference_type;
explicit iterator(list_p list, bool atend = false)
: size(0),
first(list->objects(&size)),
index(atend ? size : 0) {}
explicit iterator(list_p list, size_t skip)
: size(0),
first(list->objects(&size)),
index(0)
{
while (skip && index < size)
{
operator++();
skip--;
}
}
iterator() : size(0), first(), index(0) {}
public:
iterator& operator++()
{
if (index < size)
{
object_p obj = +first + index;
size_t objsize = obj->size();
ASSERT(index + objsize <= size);
index += objsize;
}
return *this;
}
iterator operator++(int)
{
iterator prev = *this;
++(*this);
return prev;
}
bool operator==(const iterator &other) const
{
return !first || !other.first ||
(index == other.index &&
+first == +other.first &&
size == other.size);
}
bool operator!=(const iterator &other) const
{
return !(*this == other);
}
value_type operator*() const
{
return index < size ? +first+ index : nullptr;
}
public:
size_t size;
object_g first;
size_t index;
};
iterator begin() const { return iterator(this); }
iterator end() const { return iterator(this, true); }
size_t items() const
// ------------------------------------------------------------------------
// Return number of items in the list
// ------------------------------------------------------------------------
{
size_t result = 0;
for (object_p obj UNUSED : *this)
result++;
return result;
}
bool expand_without_size(size_t *size = nullptr) const;
bool expand() const;
bool expand_deep(uint32_t which) const;
// ------------------------------------------------------------------------
// Expand items to the stack, and return number of them
// ------------------------------------------------------------------------
object_p operator[](size_t index) const
// ------------------------------------------------------------------------
// Return the n-th element in the list
// ------------------------------------------------------------------------
{
return at(index);
}
object_p at(size_t index) const
// ------------------------------------------------------------------------
// Return the n-th element in the list
// ------------------------------------------------------------------------
{
return *iterator(this, index);
}
template<typename ...args>
object_p at(size_t index, args... rest) const
// ------------------------------------------------------------------------
// N-dimensional array access
// ------------------------------------------------------------------------
{
object_p inner = at(index);
if (!inner)
return nullptr;
id type = inner->type();
if (type != ID_array && type != ID_list)
return nullptr;
list_p list = list_p(inner);
return list->at(rest...);
}
object_p head() const;
list_p tail() const;
// ------------------------------------------------------------------------
// Return the head and tail of a list
// ------------------------------------------------------------------------
// Apply an algebraic function to all elements in list
list_p map(object_p prg) const;
list_p map(algebraic_fn fn) const;
list_p map(arithmetic_fn fn, algebraic_r y) const;
list_p map(algebraic_r x, arithmetic_fn fn) const;
static list_p map(algebraic_fn fn, list_r x)
{
return x->map(fn);
}
static list_p map(arithmetic_fn fn, list_r x, algebraic_r y)
{
return x->map(fn, y);
}
static list_p map(arithmetic_fn fn, algebraic_r x, list_r y)
{
return y->map(x, fn);
}
static object::result push_list_from_stack(uint depth, id ty = ID_list);
static list_p list_from_stack(uint depth, id ty = ID_list);
// ------------------------------------------------------------------------
// Convert `depth` items to a list
// ------------------------------------------------------------------------
// Append data to a list
list_p append(list_p a) const;
list_p append(object_p o) const;
// Remove a range in the list
list_p remove(size_t start, size_t length = 1) const;
// Reduce and filter operations
object_p reduce(object_p prg) const;
list_p filter(object_p prg) const;
// Build a list by combining two subsequent items
list_p pair_map(object_p prg) const;
object_p map_as_object(object_p prg) const { return map(prg); }
object_p filter_as_object(object_p prg) const { return filter(prg); }
// Element substitution
static algebraic_p where(algebraic_r expr, algebraic_r args);
static object_p substitute(object_p expr, object_p args);
list_p substitute(symbol_r name, object_r value, size_t len) const;
list_p substitute(symbol_r name, object_r value) const;
list_p substitute(expression_r assign) const;
list_p substitute(list_r assignments) const;
list_p substitute(object_r repl) const;
// Extract a sublist
list_p extract(object_r first, object_r last) const;
// Find a symbol in a list
symbol_p contains(symbol_p sym) const;
public:
// Shared code for parsing and rendering, taking delimiters as input
static result list_parse(id type, parser &p, unicode open, unicode close);
intptr_t list_render(renderer &r, unicode open, unicode close) const;
grob_p graph(grapher &g, size_t rows, size_t cols, bool mat) const;
public:
OBJECT_DECL(list);
PARSE_DECL(list);
EVAL_DECL(list);
RENDER_DECL(list);
GRAPH_DECL(list);
HELP_DECL(list);
};
typedef const list *list_p;
COMMAND_DECLARE(ToList,~1);
COMMAND_DECLARE(FromList,1);
COMMAND_DECLARE(Size,1);
COMMAND_DECLARE(Get,2);
COMMAND_DECLARE(Put,3);
COMMAND_DECLARE(GetI,2);
COMMAND_DECLARE(PutI,3);
COMMAND_DECLARE(Sort,1);
COMMAND_DECLARE(QuickSort,1);
COMMAND_DECLARE(ReverseSort,1);
COMMAND_DECLARE(ReverseQuickSort,1);
COMMAND_DECLARE(ReverseList,1);
COMMAND_DECLARE(Head,1);
COMMAND_DECLARE(Tail,1);
COMMAND_DECLARE(Map,2);
COMMAND_DECLARE(Reduce,2);
COMMAND_DECLARE(Filter, 2);
COMMAND_DECLARE(DoList, ~1);
COMMAND_DECLARE(DoSubs, ~2);
COMMAND_DECLARE(NSub, 0);
COMMAND_DECLARE(EndSub, 0);
COMMAND_DECLARE(ListSum,1);
COMMAND_DECLARE(ListProduct,1);
COMMAND_DECLARE(ListDifferences,1);
COMMAND_DECLARE(XVars, 1);
COMMAND_DECLARE(LName, 1);
inline list_g operator+(list_r x, list_r y)
// ----------------------------------------------------------------------------
// Concatenate two lists, leveraging text concatenation
// ----------------------------------------------------------------------------
{
text_r xt = (text_r) x;
text_r yt = (text_r) y;
return list_p(+(xt + yt));
}
inline list_g operator*(list_r x, uint y)
// ----------------------------------------------------------------------------
// Repeat a list, leveraging text repetition
// ----------------------------------------------------------------------------
{
text_r xt = (text_r) x;
return list_p(+(xt * y));
}
int value_compare(object_p *xp, object_p *yp);
int memory_compare(object_p *xp, object_p *yp);
// ----------------------------------------------------------------------------
// Value and memory comparison for sorting
// ----------------------------------------------------------------------------
#endif // LIST_H