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
|
/* -*- c++ -*- */
/*
* Copyright 2006,2009 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 <stdio.h>
#include <gruel/pmt_pool.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 ) delete 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_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_uniform_vector *
_uniform_vector(pmt_t x)
{
return dynamic_cast<pmt_uniform_vector*>(x.get());
}
static pmt_dict *
_dict(pmt_t x)
{
return dynamic_cast<pmt_dict*>(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);
}
////////////////////////////////////////////////////////////////////////////
// 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);
}
////////////////////////////////////////////////////////////////////////////
// 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
////////////////////////////////////////////////////////////////////////////
pmt_dict::pmt_dict()
: d_alist(PMT_NIL)
{
}
void
pmt_dict::set(pmt_t key, pmt_t value)
{
pmt_t p = pmt_assv(key, d_alist); // look for (key . value) pair
if (pmt_is_pair(p)){ // found existing pair...
pmt_set_cdr(p, value); // overrwrite cdr with new value
}
else { // not in the dict
d_alist = pmt_cons(pmt_cons(key, value), d_alist); // add new (key . value) pair
}
}
pmt_t
pmt_dict::ref(pmt_t key, pmt_t not_found) const
{
pmt_t p = pmt_assv(key, d_alist); // look for (key . value) pair
if (pmt_is_pair(p))
return pmt_cdr(p);
else
return not_found;
}
bool
pmt_dict::has_key(pmt_t key) const
{
return pmt_is_pair(pmt_assv(key, d_alist));
}
pmt_t
pmt_dict::items() const
{
return d_alist;
}
pmt_t
pmt_dict::keys() const
{
return pmt_map(pmt_car, d_alist);
}
pmt_t
pmt_dict::values() const
{
return pmt_map(pmt_cdr, d_alist);
}
bool
pmt_is_dict(pmt_t obj)
{
return obj->is_dict();
}
pmt_t
pmt_make_dict()
{
return pmt_t(new pmt_dict());
}
void
pmt_dict_set(pmt_t dict, pmt_t key, pmt_t value)
{
pmt_dict* d = _dict(dict);
if (!d)
throw pmt_wrong_type("pmt_dict_set", dict);
d->set(key, value);
}
bool
pmt_dict_has_key(pmt_t dict, pmt_t key)
{
pmt_dict* d = _dict(dict);
if (!d)
throw pmt_wrong_type("pmt_dict_has_key", dict);
return d->has_key(key);
}
pmt_t
pmt_dict_ref(pmt_t dict, pmt_t key, pmt_t not_found)
{
pmt_dict* d = _dict(dict);
if (!d)
throw pmt_wrong_type("pmt_dict_ref", dict);
return d->ref(key, not_found);
}
pmt_t
pmt_dict_items(pmt_t dict)
{
if (!dict->is_dict())
throw pmt_wrong_type("pmt_dict_items", dict);
return _dict(dict)->items();
}
pmt_t
pmt_dict_keys(pmt_t dict)
{
if (!dict->is_dict())
throw pmt_wrong_type("pmt_dict_keys", dict);
return _dict(dict)->keys();
}
pmt_t
pmt_dict_values(pmt_t dict)
{
if (!dict->is_dict())
throw pmt_wrong_type("pmt_dict_values", dict);
return _dict(dict)->values();
}
////////////////////////////////////////////////////////////////////////////
// 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);
}
////////////////////////////////////////////////////////////////////////////
// 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_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_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_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;
}
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;
}
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_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_dict) = %3zd\n", sizeof(pmt_dict));
printf("sizeof(pmt_uniform_vector) = %3zd\n", sizeof(pmt_uniform_vector));
}
} /* namespace pmt */
|