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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
|
/* -*- c++ -*- */
/*
* Copyright 2006,2009,2010 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* GNU Radio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* GNU Radio 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. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <vector>
#include <gruel/pmt.h>
#include "pmt_int.h"
#include <gruel/msg_accepter.h>
#include <gruel/pmt_pool.h>
#include <stdio.h>
#include <string.h>
namespace pmt {
static const int CACHE_LINE_SIZE = 64; // good guess
# if (PMT_LOCAL_ALLOCATOR)
static pmt_pool global_pmt_pool(sizeof(pmt_pair), CACHE_LINE_SIZE);
void *
pmt_base::operator new(size_t size)
{
void *p = global_pmt_pool.malloc();
// fprintf(stderr, "pmt_base::new p = %p\n", p);
assert((reinterpret_cast<intptr_t>(p) & (CACHE_LINE_SIZE - 1)) == 0);
return p;
}
void
pmt_base::operator delete(void *p, size_t size)
{
global_pmt_pool.free(p);
}
#endif
void intrusive_ptr_add_ref(pmt_base* p) { ++(p->count_); }
void intrusive_ptr_release(pmt_base* p) {
if (--(p->count_) == 0 ){
//make a copy of deleter before we delete its container, p
boost::function<void(pmt_base *)> deleter = p->deleter_;
deleter(p);
}
}
pmt_base::~pmt_base()
{
// nop -- out of line virtual destructor
}
////////////////////////////////////////////////////////////////////////////
// Exceptions
////////////////////////////////////////////////////////////////////////////
pmt_exception::pmt_exception(const std::string &msg, pmt_t obj)
: logic_error(msg + ": " + pmt_write_string(obj))
{
}
pmt_wrong_type::pmt_wrong_type(const std::string &msg, pmt_t obj)
: pmt_exception(msg + ": wrong_type ", obj)
{
}
pmt_out_of_range::pmt_out_of_range(const std::string &msg, pmt_t obj)
: pmt_exception(msg + ": out of range ", obj)
{
}
pmt_notimplemented::pmt_notimplemented(const std::string &msg, pmt_t obj)
: pmt_exception(msg + ": notimplemented ", obj)
{
}
////////////////////////////////////////////////////////////////////////////
// Dynamic Casts
////////////////////////////////////////////////////////////////////////////
static pmt_symbol *
_symbol(pmt_t x)
{
return dynamic_cast<pmt_symbol*>(x.get());
}
static pmt_integer *
_integer(pmt_t x)
{
return dynamic_cast<pmt_integer*>(x.get());
}
static pmt_uint64 *
_uint64(pmt_t x)
{
return dynamic_cast<pmt_uint64*>(x.get());
}
static pmt_real *
_real(pmt_t x)
{
return dynamic_cast<pmt_real*>(x.get());
}
static pmt_complex *
_complex(pmt_t x)
{
return dynamic_cast<pmt_complex*>(x.get());
}
static pmt_pair *
_pair(pmt_t x)
{
return dynamic_cast<pmt_pair*>(x.get());
}
static pmt_vector *
_vector(pmt_t x)
{
return dynamic_cast<pmt_vector*>(x.get());
}
static pmt_tuple *
_tuple(pmt_t x)
{
return dynamic_cast<pmt_tuple*>(x.get());
}
static pmt_uniform_vector *
_uniform_vector(pmt_t x)
{
return dynamic_cast<pmt_uniform_vector*>(x.get());
}
static pmt_any *
_any(pmt_t x)
{
return dynamic_cast<pmt_any*>(x.get());
}
////////////////////////////////////////////////////////////////////////////
// Globals
////////////////////////////////////////////////////////////////////////////
const pmt_t PMT_T = pmt_t(new pmt_bool()); // singleton
const pmt_t PMT_F = pmt_t(new pmt_bool()); // singleton
const pmt_t PMT_NIL = pmt_t(new pmt_null()); // singleton
const pmt_t PMT_EOF = pmt_cons(PMT_NIL, PMT_NIL); // singleton
////////////////////////////////////////////////////////////////////////////
// Booleans
////////////////////////////////////////////////////////////////////////////
pmt_bool::pmt_bool(){}
bool
pmt_is_true(pmt_t obj)
{
return obj != PMT_F;
}
bool
pmt_is_false(pmt_t obj)
{
return obj == PMT_F;
}
bool
pmt_is_bool(pmt_t obj)
{
return obj->is_bool();
}
pmt_t
pmt_from_bool(bool val)
{
return val ? PMT_T : PMT_F;
}
bool
pmt_to_bool(pmt_t val)
{
if (val == PMT_T)
return true;
if (val == PMT_F)
return false;
throw pmt_wrong_type("pmt_to_bool", val);
}
////////////////////////////////////////////////////////////////////////////
// Symbols
////////////////////////////////////////////////////////////////////////////
static const unsigned int SYMBOL_HASH_TABLE_SIZE = 701;
static std::vector<pmt_t> s_symbol_hash_table(SYMBOL_HASH_TABLE_SIZE);
pmt_symbol::pmt_symbol(const std::string &name) : d_name(name){}
static unsigned int
hash_string(const std::string &s)
{
unsigned int h = 0;
unsigned int g = 0;
for (std::string::const_iterator p = s.begin(); p != s.end(); ++p){
h = (h << 4) + (*p & 0xff);
g = h & 0xf0000000;
if (g){
h = h ^ (g >> 24);
h = h ^ g;
}
}
return h;
}
bool
pmt_is_symbol(const pmt_t& obj)
{
return obj->is_symbol();
}
pmt_t
pmt_string_to_symbol(const std::string &name)
{
unsigned hash = hash_string(name) % SYMBOL_HASH_TABLE_SIZE;
// Does a symbol with this name already exist?
for (pmt_t sym = s_symbol_hash_table[hash]; sym; sym = _symbol(sym)->next()){
if (name == _symbol(sym)->name())
return sym; // Yes. Return it
}
// Nope. Make a new one.
pmt_t sym = pmt_t(new pmt_symbol(name));
_symbol(sym)->set_next(s_symbol_hash_table[hash]);
s_symbol_hash_table[hash] = sym;
return sym;
}
// alias...
pmt_t
pmt_intern(const std::string &name)
{
return pmt_string_to_symbol(name);
}
const std::string
pmt_symbol_to_string(const pmt_t& sym)
{
if (!sym->is_symbol())
throw pmt_wrong_type("pmt_symbol_to_string", sym);
return _symbol(sym)->name();
}
////////////////////////////////////////////////////////////////////////////
// Number
////////////////////////////////////////////////////////////////////////////
bool
pmt_is_number(pmt_t x)
{
return x->is_number();
}
////////////////////////////////////////////////////////////////////////////
// Integer
////////////////////////////////////////////////////////////////////////////
pmt_integer::pmt_integer(long value) : d_value(value) {}
bool
pmt_is_integer(pmt_t x)
{
return x->is_integer();
}
pmt_t
pmt_from_long(long x)
{
return pmt_t(new pmt_integer(x));
}
long
pmt_to_long(pmt_t x)
{
pmt_integer* i = dynamic_cast<pmt_integer*>(x.get());
if ( i )
return i->value();
throw pmt_wrong_type("pmt_to_long", x);
}
////////////////////////////////////////////////////////////////////////////
// Uint64
////////////////////////////////////////////////////////////////////////////
pmt_uint64::pmt_uint64(uint64_t value) : d_value(value) {}
bool
pmt_is_uint64(pmt_t x)
{
return x->is_uint64();
}
pmt_t
pmt_from_uint64(uint64_t x)
{
return pmt_t(new pmt_uint64(x));
}
uint64_t
pmt_to_uint64(pmt_t x)
{
if(x->is_uint64())
return _uint64(x)->value();
if(x->is_integer())
{
long tmp = _integer(x)->value();
if(tmp >= 0)
return (uint64_t) tmp;
}
throw pmt_wrong_type("pmt_to_uint64", x);
}
////////////////////////////////////////////////////////////////////////////
// Real
////////////////////////////////////////////////////////////////////////////
pmt_real::pmt_real(double value) : d_value(value) {}
bool
pmt_is_real(pmt_t x)
{
return x->is_real();
}
pmt_t
pmt_from_double(double x)
{
return pmt_t(new pmt_real(x));
}
double
pmt_to_double(pmt_t x)
{
if (x->is_real())
return _real(x)->value();
if (x->is_integer())
return _integer(x)->value();
throw pmt_wrong_type("pmt_to_double", x);
}
////////////////////////////////////////////////////////////////////////////
// Complex
////////////////////////////////////////////////////////////////////////////
pmt_complex::pmt_complex(std::complex<double> value) : d_value(value) {}
bool
pmt_is_complex(pmt_t x)
{
return x->is_complex();
}
pmt_t
pmt_make_rectangular(double re, double im)
{
return pmt_t(new pmt_complex(std::complex<double>(re, im)));
}
std::complex<double>
pmt_to_complex(pmt_t x)
{
if (x->is_complex())
return _complex(x)->value();
if (x->is_real())
return _real(x)->value();
if (x->is_integer())
return _integer(x)->value();
throw pmt_wrong_type("pmt_to_complex", x);
}
////////////////////////////////////////////////////////////////////////////
// Pairs
////////////////////////////////////////////////////////////////////////////
pmt_null::pmt_null() {}
pmt_pair::pmt_pair(const pmt_t& car, const pmt_t& cdr) : d_car(car), d_cdr(cdr) {}
bool
pmt_is_null(const pmt_t& x)
{
return x == PMT_NIL;
}
bool
pmt_is_pair(const pmt_t& obj)
{
return obj->is_pair();
}
pmt_t
pmt_cons(const pmt_t& x, const pmt_t& y)
{
return pmt_t(new pmt_pair(x, y));
}
pmt_t
pmt_car(const pmt_t& pair)
{
pmt_pair* p = dynamic_cast<pmt_pair*>(pair.get());
if ( p )
return p->car();
throw pmt_wrong_type("pmt_car", pair);
}
pmt_t
pmt_cdr(const pmt_t& pair)
{
pmt_pair* p = dynamic_cast<pmt_pair*>(pair.get());
if ( p )
return p->cdr();
throw pmt_wrong_type("pmt_cdr", pair);
}
void
pmt_set_car(pmt_t pair, pmt_t obj)
{
if (pair->is_pair())
_pair(pair)->set_car(obj);
else
throw pmt_wrong_type("pmt_set_car", pair);
}
void
pmt_set_cdr(pmt_t pair, pmt_t obj)
{
if (pair->is_pair())
_pair(pair)->set_cdr(obj);
else
throw pmt_wrong_type("pmt_set_cdr", pair);
}
////////////////////////////////////////////////////////////////////////////
// Vectors
////////////////////////////////////////////////////////////////////////////
pmt_vector::pmt_vector(size_t len, pmt_t fill)
: d_v(len)
{
for (size_t i = 0; i < len; i++)
d_v[i] = fill;
}
pmt_t
pmt_vector::ref(size_t k) const
{
if (k >= length())
throw pmt_out_of_range("pmt_vector_ref", pmt_from_long(k));
return d_v[k];
}
void
pmt_vector::set(size_t k, pmt_t obj)
{
if (k >= length())
throw pmt_out_of_range("pmt_vector_set", pmt_from_long(k));
d_v[k] = obj;
}
void
pmt_vector::fill(pmt_t obj)
{
for (size_t i = 0; i < length(); i++)
d_v[i] = obj;
}
bool
pmt_is_vector(pmt_t obj)
{
return obj->is_vector();
}
pmt_t
pmt_make_vector(size_t k, pmt_t fill)
{
return pmt_t(new pmt_vector(k, fill));
}
pmt_t
pmt_vector_ref(pmt_t vector, size_t k)
{
if (!vector->is_vector())
throw pmt_wrong_type("pmt_vector_ref", vector);
return _vector(vector)->ref(k);
}
void
pmt_vector_set(pmt_t vector, size_t k, pmt_t obj)
{
if (!vector->is_vector())
throw pmt_wrong_type("pmt_vector_set", vector);
_vector(vector)->set(k, obj);
}
void
pmt_vector_fill(pmt_t vector, pmt_t obj)
{
if (!vector->is_vector())
throw pmt_wrong_type("pmt_vector_set", vector);
_vector(vector)->fill(obj);
}
////////////////////////////////////////////////////////////////////////////
// Tuples
////////////////////////////////////////////////////////////////////////////
pmt_tuple::pmt_tuple(size_t len)
: d_v(len)
{
}
pmt_t
pmt_tuple::ref(size_t k) const
{
if (k >= length())
throw pmt_out_of_range("pmt_tuple_ref", pmt_from_long(k));
return d_v[k];
}
bool
pmt_is_tuple(pmt_t obj)
{
return obj->is_tuple();
}
pmt_t
pmt_tuple_ref(const pmt_t &tuple, size_t k)
{
if (!tuple->is_tuple())
throw pmt_wrong_type("pmt_tuple_ref", tuple);
return _tuple(tuple)->ref(k);
}
// for (i=0; i < 10; i++)
// make_constructor()
pmt_t
pmt_make_tuple()
{
return pmt_t(new pmt_tuple(0));
}
pmt_t
pmt_make_tuple(const pmt_t &e0)
{
pmt_tuple *t = new pmt_tuple(1);
t->_set(0, e0);
return pmt_t(t);
}
pmt_t
pmt_make_tuple(const pmt_t &e0, const pmt_t &e1)
{
pmt_tuple *t = new pmt_tuple(2);
t->_set(0, e0);
t->_set(1, e1);
return pmt_t(t);
}
pmt_t
pmt_make_tuple(const pmt_t &e0, const pmt_t &e1, const pmt_t &e2)
{
pmt_tuple *t = new pmt_tuple(3);
t->_set(0, e0);
t->_set(1, e1);
t->_set(2, e2);
return pmt_t(t);
}
pmt_t
pmt_make_tuple(const pmt_t &e0, const pmt_t &e1, const pmt_t &e2, const pmt_t &e3)
{
pmt_tuple *t = new pmt_tuple(4);
t->_set(0, e0);
t->_set(1, e1);
t->_set(2, e2);
t->_set(3, e3);
return pmt_t(t);
}
pmt_t
pmt_make_tuple(const pmt_t &e0, const pmt_t &e1, const pmt_t &e2, const pmt_t &e3, const pmt_t &e4)
{
pmt_tuple *t = new pmt_tuple(5);
t->_set(0, e0);
t->_set(1, e1);
t->_set(2, e2);
t->_set(3, e3);
t->_set(4, e4);
return pmt_t(t);
}
pmt_t
pmt_make_tuple(const pmt_t &e0, const pmt_t &e1, const pmt_t &e2, const pmt_t &e3, const pmt_t &e4, const pmt_t &e5)
{
pmt_tuple *t = new pmt_tuple(6);
t->_set(0, e0);
t->_set(1, e1);
t->_set(2, e2);
t->_set(3, e3);
t->_set(4, e4);
t->_set(5, e5);
return pmt_t(t);
}
pmt_t
pmt_make_tuple(const pmt_t &e0, const pmt_t &e1, const pmt_t &e2, const pmt_t &e3, const pmt_t &e4, const pmt_t &e5, const pmt_t &e6)
{
pmt_tuple *t = new pmt_tuple(7);
t->_set(0, e0);
t->_set(1, e1);
t->_set(2, e2);
t->_set(3, e3);
t->_set(4, e4);
t->_set(5, e5);
t->_set(6, e6);
return pmt_t(t);
}
pmt_t
pmt_make_tuple(const pmt_t &e0, const pmt_t &e1, const pmt_t &e2, const pmt_t &e3, const pmt_t &e4, const pmt_t &e5, const pmt_t &e6, const pmt_t &e7)
{
pmt_tuple *t = new pmt_tuple(8);
t->_set(0, e0);
t->_set(1, e1);
t->_set(2, e2);
t->_set(3, e3);
t->_set(4, e4);
t->_set(5, e5);
t->_set(6, e6);
t->_set(7, e7);
return pmt_t(t);
}
pmt_t
pmt_make_tuple(const pmt_t &e0, const pmt_t &e1, const pmt_t &e2, const pmt_t &e3, const pmt_t &e4, const pmt_t &e5, const pmt_t &e6, const pmt_t &e7, const pmt_t &e8)
{
pmt_tuple *t = new pmt_tuple(9);
t->_set(0, e0);
t->_set(1, e1);
t->_set(2, e2);
t->_set(3, e3);
t->_set(4, e4);
t->_set(5, e5);
t->_set(6, e6);
t->_set(7, e7);
t->_set(8, e8);
return pmt_t(t);
}
pmt_t
pmt_make_tuple(const pmt_t &e0, const pmt_t &e1, const pmt_t &e2, const pmt_t &e3, const pmt_t &e4, const pmt_t &e5, const pmt_t &e6, const pmt_t &e7, const pmt_t &e8, const pmt_t &e9)
{
pmt_tuple *t = new pmt_tuple(10);
t->_set(0, e0);
t->_set(1, e1);
t->_set(2, e2);
t->_set(3, e3);
t->_set(4, e4);
t->_set(5, e5);
t->_set(6, e6);
t->_set(7, e7);
t->_set(8, e8);
t->_set(9, e9);
return pmt_t(t);
}
pmt_t
pmt_to_tuple(const pmt_t &x)
{
if (x->is_tuple()) // already one
return x;
size_t len = pmt_length(x);
pmt_tuple *t = new pmt_tuple(len);
pmt_t r = pmt_t(t);
if (x->is_vector()){
for (size_t i = 0; i < len; i++)
t->_set(i, _vector(x)->ref(i));
return r;
}
if (x->is_pair()){
pmt_t y = x;
for (size_t i = 0; i < len; i++){
t->_set(i, pmt_car(y));
y = pmt_cdr(y);
}
return r;
}
throw pmt_wrong_type("pmt_to_tuple", x);
}
////////////////////////////////////////////////////////////////////////////
// Uniform Numeric Vectors
////////////////////////////////////////////////////////////////////////////
bool
pmt_is_uniform_vector(pmt_t x)
{
return x->is_uniform_vector();
}
const void *
pmt_uniform_vector_elements(pmt_t vector, size_t &len)
{
if (!vector->is_uniform_vector())
throw pmt_wrong_type("pmt_uniform_vector_elements", vector);
return _uniform_vector(vector)->uniform_elements(len);
}
void *
pmt_uniform_vector_writable_elements(pmt_t vector, size_t &len)
{
if (!vector->is_uniform_vector())
throw pmt_wrong_type("pmt_uniform_vector_writable_elements", vector);
return _uniform_vector(vector)->uniform_writable_elements(len);
}
////////////////////////////////////////////////////////////////////////////
// Dictionaries
////////////////////////////////////////////////////////////////////////////
/*
* This is an a-list implementation.
*
* When we need better performance for large dictionaries, consider implementing
* persistent Red-Black trees as described in "Purely Functional Data Structures",
* Chris Okasaki, 1998, section 3.3.
*/
bool
pmt_is_dict(const pmt_t &obj)
{
return pmt_is_null(obj) || pmt_is_pair(obj);
}
pmt_t
pmt_make_dict()
{
return PMT_NIL;
}
pmt_t
pmt_dict_add(const pmt_t &dict, const pmt_t &key, const pmt_t &value)
{
if (pmt_is_null(dict))
return pmt_acons(key, value, PMT_NIL);
if (pmt_dict_has_key(dict, key))
return pmt_acons(key, value, pmt_dict_delete(dict, key));
return pmt_acons(key, value, dict);
}
pmt_t
pmt_dict_delete(const pmt_t &dict, const pmt_t &key)
{
if (pmt_is_null(dict))
return dict;
if (pmt_eqv(pmt_caar(dict), key))
return pmt_cdr(dict);
return pmt_cons(pmt_car(dict), pmt_dict_delete(pmt_cdr(dict), key));
}
pmt_t
pmt_dict_ref(const pmt_t &dict, const pmt_t &key, const pmt_t ¬_found)
{
pmt_t p = pmt_assv(key, dict); // look for (key . value) pair
if (pmt_is_pair(p))
return pmt_cdr(p);
else
return not_found;
}
bool
pmt_dict_has_key(const pmt_t &dict, const pmt_t &key)
{
return pmt_is_pair(pmt_assv(key, dict));
}
pmt_t
pmt_dict_items(pmt_t dict)
{
if (!pmt_is_dict(dict))
throw pmt_wrong_type("pmt_dict_values", dict);
return dict; // equivalent to dict in the a-list case
}
pmt_t
pmt_dict_keys(pmt_t dict)
{
if (!pmt_is_dict(dict))
throw pmt_wrong_type("pmt_dict_keys", dict);
return pmt_map(pmt_car, dict);
}
pmt_t
pmt_dict_values(pmt_t dict)
{
if (!pmt_is_dict(dict))
throw pmt_wrong_type("pmt_dict_keys", dict);
return pmt_map(pmt_cdr, dict);
}
////////////////////////////////////////////////////////////////////////////
// Any
////////////////////////////////////////////////////////////////////////////
pmt_any::pmt_any(const boost::any &any) : d_any(any) {}
bool
pmt_is_any(pmt_t obj)
{
return obj->is_any();
}
pmt_t
pmt_make_any(const boost::any &any)
{
return pmt_t(new pmt_any(any));
}
boost::any
pmt_any_ref(pmt_t obj)
{
if (!obj->is_any())
throw pmt_wrong_type("pmt_any_ref", obj);
return _any(obj)->ref();
}
void
pmt_any_set(pmt_t obj, const boost::any &any)
{
if (!obj->is_any())
throw pmt_wrong_type("pmt_any_set", obj);
_any(obj)->set(any);
}
////////////////////////////////////////////////////////////////////////////
// msg_accepter -- built from "any"
////////////////////////////////////////////////////////////////////////////
bool
pmt_is_msg_accepter(const pmt_t &obj)
{
if (!pmt_is_any(obj))
return false;
boost::any r = pmt_any_ref(obj);
return boost::any_cast<gruel::msg_accepter_sptr>(&r) != 0;
}
//! make a msg_accepter
pmt_t
pmt_make_msg_accepter(gruel::msg_accepter_sptr ma)
{
return pmt_make_any(ma);
}
//! Return underlying msg_accepter
gruel::msg_accepter_sptr
pmt_msg_accepter_ref(const pmt_t &obj)
{
try {
return boost::any_cast<gruel::msg_accepter_sptr>(pmt_any_ref(obj));
}
catch (boost::bad_any_cast &e){
throw pmt_wrong_type("pmt_msg_accepter_ref", obj);
}
}
////////////////////////////////////////////////////////////////////////////
// Binary Large Object -- currently a u8vector
////////////////////////////////////////////////////////////////////////////
bool
pmt_is_blob(pmt_t x)
{
// return pmt_is_u8vector(x);
return pmt_is_uniform_vector(x);
}
pmt_t
pmt_make_blob(const void *buf, size_t len_in_bytes)
{
return pmt_init_u8vector(len_in_bytes, (const uint8_t *) buf);
}
const void *
pmt_blob_data(pmt_t blob)
{
size_t len;
return pmt_uniform_vector_elements(blob, len);
}
size_t
pmt_blob_length(pmt_t blob)
{
size_t len;
pmt_uniform_vector_elements(blob, len);
return len;
}
////////////////////////////////////////////////////////////////////////////
// General Functions
////////////////////////////////////////////////////////////////////////////
bool
pmt_eq(const pmt_t& x, const pmt_t& y)
{
return x == y;
}
bool
pmt_eqv(const pmt_t& x, const pmt_t& y)
{
if (x == y)
return true;
if (x->is_integer() && y->is_integer())
return _integer(x)->value() == _integer(y)->value();
if (x->is_uint64() && y->is_uint64())
return _uint64(x)->value() == _uint64(y)->value();
if (x->is_real() && y->is_real())
return _real(x)->value() == _real(y)->value();
if (x->is_complex() && y->is_complex())
return _complex(x)->value() == _complex(y)->value();
return false;
}
bool
pmt_eqv_raw(pmt_base *x, pmt_base *y)
{
if (x == y)
return true;
if (x->is_integer() && y->is_integer())
return _integer(x)->value() == _integer(y)->value();
if (x->is_uint64() && y->is_uint64())
return _uint64(x)->value() == _uint64(y)->value();
if (x->is_real() && y->is_real())
return _real(x)->value() == _real(y)->value();
if (x->is_complex() && y->is_complex())
return _complex(x)->value() == _complex(y)->value();
return false;
}
bool
pmt_equal(const pmt_t& x, const pmt_t& y)
{
if (pmt_eqv(x, y))
return true;
if (x->is_pair() && y->is_pair())
return pmt_equal(pmt_car(x), pmt_car(y)) && pmt_equal(pmt_cdr(x), pmt_cdr(y));
if (x->is_vector() && y->is_vector()){
pmt_vector *xv = _vector(x);
pmt_vector *yv = _vector(y);
if (xv->length() != yv->length())
return false;
for (unsigned i = 0; i < xv->length(); i++)
if (!pmt_equal(xv->_ref(i), yv->_ref(i)))
return false;
return true;
}
if (x->is_tuple() && y->is_tuple()){
pmt_tuple *xv = _tuple(x);
pmt_tuple *yv = _tuple(y);
if (xv->length() != yv->length())
return false;
for (unsigned i = 0; i < xv->length(); i++)
if (!pmt_equal(xv->_ref(i), yv->_ref(i)))
return false;
return true;
}
if (x->is_uniform_vector() && y->is_uniform_vector()){
pmt_uniform_vector *xv = _uniform_vector(x);
pmt_uniform_vector *yv = _uniform_vector(y);
if (xv->length() != yv->length())
return false;
size_t len_x, len_y;
if (memcmp(xv->uniform_elements(len_x),
yv->uniform_elements(len_y),
len_x) == 0)
return true;
return true;
}
// FIXME add other cases here...
return false;
}
size_t
pmt_length(const pmt_t& x)
{
if (x->is_vector())
return _vector(x)->length();
if (x->is_uniform_vector())
return _uniform_vector(x)->length();
if (x->is_tuple())
return _tuple(x)->length();
if (x->is_null())
return 0;
if (x->is_pair()) {
size_t length=1;
pmt_t it = pmt_cdr(x);
while (pmt_is_pair(it)){
length++;
it = pmt_cdr(it);
}
if (pmt_is_null(it))
return length;
// not a proper list
throw pmt_wrong_type("pmt_length", x);
}
// FIXME dictionary length (number of entries)
throw pmt_wrong_type("pmt_length", x);
}
pmt_t
pmt_assq(pmt_t obj, pmt_t alist)
{
while (pmt_is_pair(alist)){
pmt_t p = pmt_car(alist);
if (!pmt_is_pair(p)) // malformed alist
return PMT_F;
if (pmt_eq(obj, pmt_car(p)))
return p;
alist = pmt_cdr(alist);
}
return PMT_F;
}
/*
* This avoids a bunch of shared_pointer reference count manipulation.
*/
pmt_t
pmt_assv_raw(pmt_base *obj, pmt_base *alist)
{
while (alist->is_pair()){
pmt_base *p = ((pmt_pair *)alist)->d_car.get();
if (!p->is_pair()) // malformed alist
return PMT_F;
if (pmt_eqv_raw(obj, ((pmt_pair *)p)->d_car.get()))
return ((pmt_pair *)alist)->d_car;
alist = (((pmt_pair *)alist)->d_cdr).get();
}
return PMT_F;
}
#if 1
pmt_t
pmt_assv(pmt_t obj, pmt_t alist)
{
return pmt_assv_raw(obj.get(), alist.get());
}
#else
pmt_t
pmt_assv(pmt_t obj, pmt_t alist)
{
while (pmt_is_pair(alist)){
pmt_t p = pmt_car(alist);
if (!pmt_is_pair(p)) // malformed alist
return PMT_F;
if (pmt_eqv(obj, pmt_car(p)))
return p;
alist = pmt_cdr(alist);
}
return PMT_F;
}
#endif
pmt_t
pmt_assoc(pmt_t obj, pmt_t alist)
{
while (pmt_is_pair(alist)){
pmt_t p = pmt_car(alist);
if (!pmt_is_pair(p)) // malformed alist
return PMT_F;
if (pmt_equal(obj, pmt_car(p)))
return p;
alist = pmt_cdr(alist);
}
return PMT_F;
}
pmt_t
pmt_map(pmt_t proc(const pmt_t&), pmt_t list)
{
pmt_t r = PMT_NIL;
while(pmt_is_pair(list)){
r = pmt_cons(proc(pmt_car(list)), r);
list = pmt_cdr(list);
}
return pmt_reverse_x(r);
}
pmt_t
pmt_reverse(pmt_t listx)
{
pmt_t list = listx;
pmt_t r = PMT_NIL;
while(pmt_is_pair(list)){
r = pmt_cons(pmt_car(list), r);
list = pmt_cdr(list);
}
if (pmt_is_null(list))
return r;
else
throw pmt_wrong_type("pmt_reverse", listx);
}
pmt_t
pmt_reverse_x(pmt_t list)
{
// FIXME do it destructively
return pmt_reverse(list);
}
pmt_t
pmt_nth(size_t n, pmt_t list)
{
pmt_t t = pmt_nthcdr(n, list);
if (pmt_is_pair(t))
return pmt_car(t);
else
return PMT_NIL;
}
pmt_t
pmt_nthcdr(size_t n, pmt_t list)
{
if (!(pmt_is_pair(list) || pmt_is_null(list)))
throw pmt_wrong_type("pmt_nthcdr", list);
while (n > 0){
if (pmt_is_pair(list)){
list = pmt_cdr(list);
n--;
continue;
}
if (pmt_is_null(list))
return PMT_NIL;
else
throw pmt_wrong_type("pmt_nthcdr: not a LIST", list);
}
return list;
}
pmt_t
pmt_memq(pmt_t obj, pmt_t list)
{
while (pmt_is_pair(list)){
if (pmt_eq(obj, pmt_car(list)))
return list;
list = pmt_cdr(list);
}
return PMT_F;
}
pmt_t
pmt_memv(pmt_t obj, pmt_t list)
{
while (pmt_is_pair(list)){
if (pmt_eqv(obj, pmt_car(list)))
return list;
list = pmt_cdr(list);
}
return PMT_F;
}
pmt_t
pmt_member(pmt_t obj, pmt_t list)
{
while (pmt_is_pair(list)){
if (pmt_equal(obj, pmt_car(list)))
return list;
list = pmt_cdr(list);
}
return PMT_F;
}
bool
pmt_subsetp(pmt_t list1, pmt_t list2)
{
while (pmt_is_pair(list1)){
pmt_t p = pmt_car(list1);
if (pmt_is_false(pmt_memv(p, list2)))
return false;
list1 = pmt_cdr(list1);
}
return true;
}
pmt_t
pmt_list1(const pmt_t& x1)
{
return pmt_cons(x1, PMT_NIL);
}
pmt_t
pmt_list2(const pmt_t& x1, const pmt_t& x2)
{
return pmt_cons(x1, pmt_cons(x2, PMT_NIL));
}
pmt_t
pmt_list3(const pmt_t& x1, const pmt_t& x2, const pmt_t& x3)
{
return pmt_cons(x1, pmt_cons(x2, pmt_cons(x3, PMT_NIL)));
}
pmt_t
pmt_list4(const pmt_t& x1, const pmt_t& x2, const pmt_t& x3, const pmt_t& x4)
{
return pmt_cons(x1, pmt_cons(x2, pmt_cons(x3, pmt_cons(x4, PMT_NIL))));
}
pmt_t
pmt_list5(const pmt_t& x1, const pmt_t& x2, const pmt_t& x3, const pmt_t& x4, const pmt_t& x5)
{
return pmt_cons(x1, pmt_cons(x2, pmt_cons(x3, pmt_cons(x4, pmt_cons(x5, PMT_NIL)))));
}
pmt_t
pmt_list6(const pmt_t& x1, const pmt_t& x2, const pmt_t& x3, const pmt_t& x4, const pmt_t& x5, const pmt_t& x6)
{
return pmt_cons(x1, pmt_cons(x2, pmt_cons(x3, pmt_cons(x4, pmt_cons(x5, pmt_cons(x6, PMT_NIL))))));
}
pmt_t
pmt_list_add(pmt_t list, const pmt_t& item)
{
return pmt_reverse(pmt_cons(item, pmt_reverse(list)));
}
pmt_t
pmt_caar(pmt_t pair)
{
return (pmt_car(pmt_car(pair)));
}
pmt_t
pmt_cadr(pmt_t pair)
{
return pmt_car(pmt_cdr(pair));
}
pmt_t
pmt_cdar(pmt_t pair)
{
return pmt_cdr(pmt_car(pair));
}
pmt_t
pmt_cddr(pmt_t pair)
{
return pmt_cdr(pmt_cdr(pair));
}
pmt_t
pmt_caddr(pmt_t pair)
{
return pmt_car(pmt_cdr(pmt_cdr(pair)));
}
pmt_t
pmt_cadddr(pmt_t pair)
{
return pmt_car(pmt_cdr(pmt_cdr(pmt_cdr(pair))));
}
bool
pmt_is_eof_object(pmt_t obj)
{
return pmt_eq(obj, PMT_EOF);
}
void
pmt_dump_sizeof()
{
printf("sizeof(pmt_t) = %3zd\n", sizeof(pmt_t));
printf("sizeof(pmt_base) = %3zd\n", sizeof(pmt_base));
printf("sizeof(pmt_bool) = %3zd\n", sizeof(pmt_bool));
printf("sizeof(pmt_symbol) = %3zd\n", sizeof(pmt_symbol));
printf("sizeof(pmt_integer) = %3zd\n", sizeof(pmt_integer));
printf("sizeof(pmt_uint64) = %3zd\n", sizeof(pmt_uint64));
printf("sizeof(pmt_real) = %3zd\n", sizeof(pmt_real));
printf("sizeof(pmt_complex) = %3zd\n", sizeof(pmt_complex));
printf("sizeof(pmt_null) = %3zd\n", sizeof(pmt_null));
printf("sizeof(pmt_pair) = %3zd\n", sizeof(pmt_pair));
printf("sizeof(pmt_vector) = %3zd\n", sizeof(pmt_vector));
printf("sizeof(pmt_uniform_vector) = %3zd\n", sizeof(pmt_uniform_vector));
}
/*
* ------------------------------------------------------------------------
* advanced
* ------------------------------------------------------------------------
*/
void
pmt_set_deleter(pmt_t obj, boost::function<void(pmt_base *)> &deleter)
{
obj->deleter_ = (deleter)? deleter : &pmt_base::default_deleter;
}
} /* namespace pmt */
|