Lolly 1.4.28
Loading...
Searching...
No Matches
lolly_tree.hpp
Go to the documentation of this file.
1
2/** \file lolly_lolly_tree.hpp
3 * \copyright GPLv3
4 * \details lolly_tree with data
5 * \author Darcy Shen
6 * \date 2024
7 */
8
9#pragma once
10
11#include "array.hpp"
12#include "basic.hpp"
13#include "list.hpp"
14#include "string.hpp"
15
16namespace lolly {
17namespace data {
18
19#define CHECK_COMPOUND(t) \
20 TM_DEBUG (ASSERT (t->op != 0, "an compound tree use 0 as operator"))
21
22template <typename T> class lolly_tree;
23template <typename T> class lolly_tree_rep;
24class blackbox;
25
26template <typename T> class lolly_tree {
28 // rep field can be atomic or compound or generic
29
30 inline lolly_tree () : rep (tm_new<lolly_tree_rep<T>> (string ())) {}
31 inline lolly_tree<T> (string l) : rep (tm_new<lolly_tree_rep<T>> (l)) {}
32 inline lolly_tree (const char* l) : rep (tm_new<lolly_tree_rep<T>> (l)) {}
33
34 inline lolly_tree (lolly_tree_rep<T>* rep2) : rep (rep2) { rep->ref_count++; }
35
36 inline lolly_tree (int l, int n= 0)
37 : rep (tm_new<lolly_tree_rep<T>> (l, array<lolly_tree<T>> (n))) {}
38
39 inline lolly_tree (int l, array<lolly_tree<T>> a)
40 : rep (tm_new<lolly_tree_rep<T>> (l, a)) {}
41
42 inline lolly_tree (lolly_tree<T> t, int n)
43 : rep (tm_new<lolly_tree_rep<T>> (t.rep->op, array<lolly_tree<T>> (n))) {
45 }
46
47 inline lolly_tree (int l, lolly_tree<T> t1)
48 : rep (tm_new<lolly_tree_rep<T>> (l, array<lolly_tree<T>> (1))) {
49 rep->a[0]= t1;
50 }
51
53 : rep (tm_new<lolly_tree_rep<T>> (l, array<lolly_tree<T>> (2))) {
54 rep->a[0]= t1;
55 rep->a[1]= t2;
56 }
57
60 : rep (tm_new<lolly_tree_rep<T>> (l, array<lolly_tree<T>> (3))) {
61 rep->a[0]= t1;
62 rep->a[1]= t2;
63 rep->a[2]= t3;
64 }
65
68 : rep (tm_new<lolly_tree_rep<T>> (l, array<lolly_tree<T>> (4))) {
69 rep->a[0]= t1;
70 rep->a[1]= t2;
71 rep->a[2]= t3;
72 rep->a[3]= t4;
73 }
74
77 : rep (tm_new<lolly_tree_rep<T>> (l, array<lolly_tree<T>> (5))) {
78 rep->a[0]= t1;
79 rep->a[1]= t2;
80 rep->a[2]= t3;
81 rep->a[3]= t4;
82 rep->a[4]= t5;
83 }
84
88 : rep (tm_new<lolly_tree_rep<T>> (l, array<lolly_tree<T>> (6))) {
89 rep->a[0]= t1;
90 rep->a[1]= t2;
91 rep->a[2]= t3;
92 rep->a[3]= t4;
93 rep->a[4]= t5;
94 rep->a[5]= t6;
95 }
96
100 : rep (tm_new<lolly_tree_rep<T>> (l, array<lolly_tree<T>> (7))) {
101 rep->a[0]= t1;
102 rep->a[1]= t2;
103 rep->a[2]= t3;
104 rep->a[3]= t4;
105 rep->a[4]= t5;
106 rep->a[5]= t6;
107 rep->a[6]= t7;
108 }
109
113 : rep (tm_new<lolly_tree_rep<T>> (l, array<lolly_tree<T>> (8))) {
114 rep->a[0]= t1;
115 rep->a[1]= t2;
116 rep->a[2]= t3;
117 rep->a[3]= t4;
118 rep->a[4]= t5;
119 rep->a[5]= t6;
120 rep->a[6]= t7;
121 rep->a[7]= t8;
122 }
123
124 inline lolly_tree<T>& operator[] (int i) {
125 CHECK_COMPOUND (*this);
126 return rep->a[i];
127 }
128
130 lolly_tree<T> r (rep->op, end - start);
131 for (int i= start; i < end; i++)
132 r[i - start]= rep->a[i];
133 return r;
134 }
135 lolly_tree<T>* begin () { return rep->a.begin (); }
136 lolly_tree<T>* end () { return rep->a.end (); }
137};
138CONCRETE_TEMPLATE_CODE (lolly_tree, typename, T)
139
141public:
142 // the tree_label of op=0 in TeXmacs is STRING
143 int op;
145 union {
146 string label;
148 };
149
150 inline lolly_tree_rep (string l) : op (0), label (l) {}
151 inline lolly_tree_rep (int l, array<lolly_tree<T>> a2) : op (l), a (a2) {}
152 inline ~lolly_tree_rep () {
153 if (op == 0) {
154 label.~string ();
155 }
156 else if (op > 0) {
157 a.~array ();
158 };
159 }
160 friend class lolly_tree<T>;
161};
162
163template <typename T>
164inline int
167 return N (t->a);
168}
169
170template <typename T>
171inline int
173 if (t->op == /*STRING*/ 0) return 0;
174 else return N (t->a);
175}
176
177template <typename T>
181 return t->a;
182}
183
184template <typename T>
185inline array<lolly_tree<T>>&
188 return t->a;
189}
190
191template <typename T>
192inline bool
194 return (t->op == 0);
195}
196
197template <typename T>
198inline bool
200 return ((t->op) > /*STRING*/ 0);
201}
202
203template <typename T>
204inline bool
206 return (t->op) < 0;
207}
208
209template <typename T>
210inline bool
212 return (t->op == lab) && (N (t) == 0);
213}
214
215template <typename T>
216inline bool
218 return (t->op != lab) || (N (t) != 0);
219}
220
221template <typename T>
222inline bool
224 return (t->op == /*STRING*/ 0) && (t->label == s);
225}
226
227template <typename T>
228inline bool
230 return (t->op != /*STRING*/ 0) || (t->label != s);
231}
232
233template <typename T>
234inline bool
235operator== (lolly_tree<T> t, const char* s) {
236 return (t->op == /*STRING*/ 0) && (t->label == s);
237}
238
239template <typename T>
240inline bool
241operator!= (lolly_tree<T> t, const char* s) {
242 return (t->op != /*STRING*/ 0) || (t->label != s);
243}
244
245template <typename T>
246inline bool
248 if (strong_equal (t, u)) return true;
249 return (t->op == u->op) &&
250 (is_atomic (t) ? (t->label == u->label) : (A (t) == A (u)));
251}
252
253template <typename T>
254inline bool
256 if (strong_equal (t, u)) return false;
257 return (t->op != u->op) ||
258 (is_atomic (t) ? (t->label != u->label) : (A (t) != A (u)));
259}
260
261template <typename T>
262inline bool
264 return t.operator->() == u.operator->();
265}
266
267template <typename T>
268inline bool
270 return (t->op == l) && (N (t) != 0);
271}
272
273template <typename T>
274inline bool
275is_func (lolly_tree<T> t, int l, int i) {
276 return (t->op == l) && (N (t) == i);
277}
278
279template <typename T>
280inline bool
282 return is_atomic (t) && is_bool (t->label);
283}
284
285template <typename T>
286inline bool
288 return is_atomic (t) && is_int (t->label);
289}
290
291template <typename T>
292inline bool
294 return is_atomic (t) && is_double (t->label);
295}
296
297template <typename T>
298inline bool
300 return is_atomic (t);
301}
302
303template <typename T>
304inline bool
306 if (is_atomic (t)) return as_bool (t->label);
307 else return false;
308}
309
310template <typename T>
311inline int
313 if (is_atomic (t)) return as_int (t->label);
314 else return 0;
315}
316
317template <typename T>
318inline long int
320 if (is_atomic (t)) return as_long_int (t->label);
321 else return 0;
322}
323
324template <typename T>
325inline double
327 if (is_atomic (t)) return as_double (t->label);
328 else return 0.0;
329}
330
331template <typename T>
332inline string
334 if (is_atomic (t)) return t->label;
335 else return "";
336}
337
338template <typename T> lolly_tree<T> copy (lolly_tree<T> t);
339
340template <typename T>
341inline lolly_tree<T>
343 int i;
344 if (is_atomic (t1)) t1= lolly_tree<T> (t2->op, t1);
345 if (is_atomic (t2)) t2= lolly_tree<T> (t1->op, t2);
346 lolly_tree<T> r (t1, N (t1) + N (t2));
347 for (i= 0; i < N (t1); i++)
348 r[i]= t1[i];
349 for (i= 0; i < N (t2); i++)
350 r[i + N (t1)]= t2[i];
351 return r;
352}
353
354template <typename T>
355inline lolly_tree<T>&
358 t->a << t2;
359 return t;
360}
361
362template <typename T>
363inline lolly_tree<T>&
366 t->a << a;
367 return t;
368}
369
370template <typename T>
373 if (is_atomic (t)) return out << t->label;
374 else if (is_compound (t)) {
375 int i, n= N (t);
376 out << as_string (t->op);
377 if (n == 0) return out << "()";
378 out << " (";
379 for (i= 0; i < n - 1; i++)
380 out << t[i] << ", ";
381 out << t[i] << ")";
382 return out;
383 }
384 return out;
385}
386
387template <typename T>
388inline lolly_tree<T>
390 if (is_atomic (t)) return lolly_tree<T> (copy (t->label));
391 else {
392 int i, n= N (t);
393 lolly_tree<T> t2 (t, n);
394 for (i= 0; i < n; i++)
395 t2[i]= copy (t[i]);
396 return t2;
397 }
398}
399
400} // namespace data
401} // namespace lolly
blackbox t1
blackbox t5
blackbox t3
blackbox t8
blackbox t7
blackbox t2
blackbox t6
blackbox t4
blackbox t[13]
A template class representing an array.
Definition array.hpp:95
#define CONCRETE_TEMPLATE_CODE(PTR, TT, T)
Macro used to define the implementation of a concrete smart pointer with reference counting for a sin...
Definition classdef.hpp:215
The list class represents a linked list.
Definition list.hpp:48
lolly_tree_rep(int l, array< lolly_tree< T > > a2)
array< lolly_tree< T > > a
lolly_tree< T > & operator[](int i)
lolly_tree< T > * begin()
lolly_tree(int l, int n=0)
lolly_tree< T > operator()(int start, int end)
lolly_tree(int l, lolly_tree< T > t1, lolly_tree< T > t2, lolly_tree< T > t3, lolly_tree< T > t4, lolly_tree< T > t5)
lolly_tree(int l, lolly_tree< T > t1, lolly_tree< T > t2, lolly_tree< T > t3, lolly_tree< T > t4, lolly_tree< T > t5, lolly_tree< T > t6, lolly_tree< T > t7, lolly_tree< T > t8)
lolly_tree(int l, lolly_tree< T > t1, lolly_tree< T > t2, lolly_tree< T > t3)
CONCRETE_TEMPLATE(lolly_tree, T)
lolly_tree(lolly_tree_rep< T > *rep2)
lolly_tree(int l, lolly_tree< T > t1, lolly_tree< T > t2, lolly_tree< T > t3, lolly_tree< T > t4, lolly_tree< T > t5, lolly_tree< T > t6)
lolly_tree(int l, lolly_tree< T > t1, lolly_tree< T > t2)
lolly_tree(const char *l)
lolly_tree(int l, array< lolly_tree< T > > a)
lolly_tree(int l, lolly_tree< T > t1, lolly_tree< T > t2, lolly_tree< T > t3, lolly_tree< T > t4)
lolly_tree< T > * end()
lolly_tree(int l, lolly_tree< T > t1)
lolly_tree(lolly_tree< T > t, int n)
lolly_tree(int l, lolly_tree< T > t1, lolly_tree< T > t2, lolly_tree< T > t3, lolly_tree< T > t4, lolly_tree< T > t5, lolly_tree< T > t6, lolly_tree< T > t7)
C * tm_new()
#define CHECK_COMPOUND(t)
array< lolly_tree< T > > A(lolly_tree< T > t)
array< lolly_tree< T > > & AR(lolly_tree< T > t)
bool operator!=(lolly_tree< T > t, int lab)
bool is_bool(lolly_tree< T > t)
bool as_bool(lolly_tree< T > t)
bool is_double(lolly_tree< T > t)
int as_int(lolly_tree< T > t)
int arity(lolly_tree< T > t)
bool is_string(lolly_tree< T > t)
double as_double(lolly_tree< T > t)
bool is_int(lolly_tree< T > t)
bool is_compound(lolly_tree< T > t)
bool strong_equal(lolly_tree< T > t, lolly_tree< T > u)
bool is_atomic(lolly_tree< T > t)
long int as_long_int(lolly_tree< T > t)
bool is_generic(lolly_tree< T > t)
int N(lolly_tree< T > t)
lolly_tree< T > operator*(lolly_tree< T > t1, lolly_tree< T > t2)
string to_string(lolly_tree< T > t)
lolly_tree< T > copy(lolly_tree< T > t)
bool is_func(lolly_tree< T > t, int l)
lolly_tree< T > & operator<<(lolly_tree< T > &t, lolly_tree< T > t2)
bool operator==(lolly_tree< T > t, int lab)
string as_string(int16_t i)
Definition string.cpp:272
Structure representing a concrete object with a reference count.
Definition classdef.hpp:24
base class of resources
Definition resource.hpp:23