3 * Copyright 2006,2009,2010 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
27 #include <gruel/pmt.h>
29 #include <gruel/msg_accepter.h>
30 #include <gruel/pmt_pool.h>
36 static const int CACHE_LINE_SIZE = 64; // good guess
38 # if (PMT_LOCAL_ALLOCATOR)
40 static pmt_pool global_pmt_pool(sizeof(pmt_pair), CACHE_LINE_SIZE);
43 pmt_base::operator new(size_t size)
45 void *p = global_pmt_pool.malloc();
47 // fprintf(stderr, "pmt_base::new p = %p\n", p);
48 assert((reinterpret_cast<intptr_t>(p) & (CACHE_LINE_SIZE - 1)) == 0);
53 pmt_base::operator delete(void *p, size_t size)
55 global_pmt_pool.free(p);
60 void intrusive_ptr_add_ref(pmt_base* p) { ++(p->count_); }
61 void intrusive_ptr_release(pmt_base* p) { if (--(p->count_) == 0 ) delete p; }
65 // nop -- out of line virtual destructor
68 ////////////////////////////////////////////////////////////////////////////
70 ////////////////////////////////////////////////////////////////////////////
72 pmt_exception::pmt_exception(const std::string &msg, pmt_t obj)
73 : logic_error(msg + ": " + pmt_write_string(obj))
77 pmt_wrong_type::pmt_wrong_type(const std::string &msg, pmt_t obj)
78 : pmt_exception(msg + ": wrong_type ", obj)
82 pmt_out_of_range::pmt_out_of_range(const std::string &msg, pmt_t obj)
83 : pmt_exception(msg + ": out of range ", obj)
87 pmt_notimplemented::pmt_notimplemented(const std::string &msg, pmt_t obj)
88 : pmt_exception(msg + ": notimplemented ", obj)
92 ////////////////////////////////////////////////////////////////////////////
94 ////////////////////////////////////////////////////////////////////////////
99 return dynamic_cast<pmt_symbol*>(x.get());
105 return dynamic_cast<pmt_integer*>(x.get());
111 return dynamic_cast<pmt_uint64*>(x.get());
117 return dynamic_cast<pmt_real*>(x.get());
123 return dynamic_cast<pmt_complex*>(x.get());
129 return dynamic_cast<pmt_pair*>(x.get());
135 return dynamic_cast<pmt_vector*>(x.get());
141 return dynamic_cast<pmt_tuple*>(x.get());
144 static pmt_uniform_vector *
145 _uniform_vector(pmt_t x)
147 return dynamic_cast<pmt_uniform_vector*>(x.get());
153 return dynamic_cast<pmt_any*>(x.get());
156 ////////////////////////////////////////////////////////////////////////////
158 ////////////////////////////////////////////////////////////////////////////
160 const pmt_t PMT_T = pmt_t(new pmt_bool()); // singleton
161 const pmt_t PMT_F = pmt_t(new pmt_bool()); // singleton
162 const pmt_t PMT_NIL = pmt_t(new pmt_null()); // singleton
163 const pmt_t PMT_EOF = pmt_cons(PMT_NIL, PMT_NIL); // singleton
165 ////////////////////////////////////////////////////////////////////////////
167 ////////////////////////////////////////////////////////////////////////////
169 pmt_bool::pmt_bool(){}
172 pmt_is_true(pmt_t obj)
178 pmt_is_false(pmt_t obj)
184 pmt_is_bool(pmt_t obj)
186 return obj->is_bool();
190 pmt_from_bool(bool val)
192 return val ? PMT_T : PMT_F;
196 pmt_to_bool(pmt_t val)
202 throw pmt_wrong_type("pmt_to_bool", val);
205 ////////////////////////////////////////////////////////////////////////////
207 ////////////////////////////////////////////////////////////////////////////
209 static const unsigned int SYMBOL_HASH_TABLE_SIZE = 701;
210 static std::vector<pmt_t> s_symbol_hash_table(SYMBOL_HASH_TABLE_SIZE);
212 pmt_symbol::pmt_symbol(const std::string &name) : d_name(name){}
216 hash_string(const std::string &s)
221 for (std::string::const_iterator p = s.begin(); p != s.end(); ++p){
222 h = (h << 4) + (*p & 0xff);
233 pmt_is_symbol(const pmt_t& obj)
235 return obj->is_symbol();
239 pmt_string_to_symbol(const std::string &name)
241 unsigned hash = hash_string(name) % SYMBOL_HASH_TABLE_SIZE;
243 // Does a symbol with this name already exist?
244 for (pmt_t sym = s_symbol_hash_table[hash]; sym; sym = _symbol(sym)->next()){
245 if (name == _symbol(sym)->name())
246 return sym; // Yes. Return it
249 // Nope. Make a new one.
250 pmt_t sym = pmt_t(new pmt_symbol(name));
251 _symbol(sym)->set_next(s_symbol_hash_table[hash]);
252 s_symbol_hash_table[hash] = sym;
258 pmt_intern(const std::string &name)
260 return pmt_string_to_symbol(name);
264 pmt_symbol_to_string(const pmt_t& sym)
266 if (!sym->is_symbol())
267 throw pmt_wrong_type("pmt_symbol_to_string", sym);
269 return _symbol(sym)->name();
274 ////////////////////////////////////////////////////////////////////////////
276 ////////////////////////////////////////////////////////////////////////////
279 pmt_is_number(pmt_t x)
281 return x->is_number();
284 ////////////////////////////////////////////////////////////////////////////
286 ////////////////////////////////////////////////////////////////////////////
288 pmt_integer::pmt_integer(long value) : d_value(value) {}
291 pmt_is_integer(pmt_t x)
293 return x->is_integer();
298 pmt_from_long(long x)
300 return pmt_t(new pmt_integer(x));
306 pmt_integer* i = dynamic_cast<pmt_integer*>(x.get());
310 throw pmt_wrong_type("pmt_to_long", x);
313 ////////////////////////////////////////////////////////////////////////////
315 ////////////////////////////////////////////////////////////////////////////
317 pmt_uint64::pmt_uint64(uint64_t value) : d_value(value) {}
320 pmt_is_uint64(pmt_t x)
322 return x->is_uint64();
327 pmt_from_uint64(uint64_t x)
329 return pmt_t(new pmt_uint64(x));
333 pmt_to_uint64(pmt_t x)
336 return _uint64(x)->value();
339 long tmp = _integer(x)->value();
341 return (uint64_t) tmp;
344 throw pmt_wrong_type("pmt_to_uint64", x);
347 ////////////////////////////////////////////////////////////////////////////
349 ////////////////////////////////////////////////////////////////////////////
351 pmt_real::pmt_real(double value) : d_value(value) {}
360 pmt_from_double(double x)
362 return pmt_t(new pmt_real(x));
366 pmt_to_double(pmt_t x)
369 return _real(x)->value();
371 return _integer(x)->value();
373 throw pmt_wrong_type("pmt_to_double", x);
376 ////////////////////////////////////////////////////////////////////////////
378 ////////////////////////////////////////////////////////////////////////////
380 pmt_complex::pmt_complex(std::complex<double> value) : d_value(value) {}
383 pmt_is_complex(pmt_t x)
385 return x->is_complex();
389 pmt_make_rectangular(double re, double im)
391 return pmt_t(new pmt_complex(std::complex<double>(re, im)));
395 pmt_to_complex(pmt_t x)
398 return _complex(x)->value();
400 return _real(x)->value();
402 return _integer(x)->value();
404 throw pmt_wrong_type("pmt_to_complex", x);
407 ////////////////////////////////////////////////////////////////////////////
409 ////////////////////////////////////////////////////////////////////////////
411 pmt_null::pmt_null() {}
412 pmt_pair::pmt_pair(const pmt_t& car, const pmt_t& cdr) : d_car(car), d_cdr(cdr) {}
415 pmt_is_null(const pmt_t& x)
421 pmt_is_pair(const pmt_t& obj)
423 return obj->is_pair();
427 pmt_cons(const pmt_t& x, const pmt_t& y)
429 return pmt_t(new pmt_pair(x, y));
433 pmt_car(const pmt_t& pair)
435 pmt_pair* p = dynamic_cast<pmt_pair*>(pair.get());
439 throw pmt_wrong_type("pmt_car", pair);
443 pmt_cdr(const pmt_t& pair)
445 pmt_pair* p = dynamic_cast<pmt_pair*>(pair.get());
449 throw pmt_wrong_type("pmt_cdr", pair);
453 pmt_set_car(pmt_t pair, pmt_t obj)
456 _pair(pair)->set_car(obj);
458 throw pmt_wrong_type("pmt_set_car", pair);
462 pmt_set_cdr(pmt_t pair, pmt_t obj)
465 _pair(pair)->set_cdr(obj);
467 throw pmt_wrong_type("pmt_set_cdr", pair);
470 ////////////////////////////////////////////////////////////////////////////
472 ////////////////////////////////////////////////////////////////////////////
474 pmt_vector::pmt_vector(size_t len, pmt_t fill)
477 for (size_t i = 0; i < len; i++)
482 pmt_vector::ref(size_t k) const
485 throw pmt_out_of_range("pmt_vector_ref", pmt_from_long(k));
490 pmt_vector::set(size_t k, pmt_t obj)
493 throw pmt_out_of_range("pmt_vector_set", pmt_from_long(k));
498 pmt_vector::fill(pmt_t obj)
500 for (size_t i = 0; i < length(); i++)
505 pmt_is_vector(pmt_t obj)
507 return obj->is_vector();
511 pmt_make_vector(size_t k, pmt_t fill)
513 return pmt_t(new pmt_vector(k, fill));
517 pmt_vector_ref(pmt_t vector, size_t k)
519 if (!vector->is_vector())
520 throw pmt_wrong_type("pmt_vector_ref", vector);
521 return _vector(vector)->ref(k);
525 pmt_vector_set(pmt_t vector, size_t k, pmt_t obj)
527 if (!vector->is_vector())
528 throw pmt_wrong_type("pmt_vector_set", vector);
529 _vector(vector)->set(k, obj);
533 pmt_vector_fill(pmt_t vector, pmt_t obj)
535 if (!vector->is_vector())
536 throw pmt_wrong_type("pmt_vector_set", vector);
537 _vector(vector)->fill(obj);
540 ////////////////////////////////////////////////////////////////////////////
542 ////////////////////////////////////////////////////////////////////////////
544 pmt_tuple::pmt_tuple(size_t len)
550 pmt_tuple::ref(size_t k) const
553 throw pmt_out_of_range("pmt_tuple_ref", pmt_from_long(k));
558 pmt_is_tuple(pmt_t obj)
560 return obj->is_tuple();
564 pmt_tuple_ref(const pmt_t &tuple, size_t k)
566 if (!tuple->is_tuple())
567 throw pmt_wrong_type("pmt_tuple_ref", tuple);
568 return _tuple(tuple)->ref(k);
571 // for (i=0; i < 10; i++)
572 // make_constructor()
577 return pmt_t(new pmt_tuple(0));
581 pmt_make_tuple(const pmt_t &e0)
583 pmt_tuple *t = new pmt_tuple(1);
589 pmt_make_tuple(const pmt_t &e0, const pmt_t &e1)
591 pmt_tuple *t = new pmt_tuple(2);
598 pmt_make_tuple(const pmt_t &e0, const pmt_t &e1, const pmt_t &e2)
600 pmt_tuple *t = new pmt_tuple(3);
608 pmt_make_tuple(const pmt_t &e0, const pmt_t &e1, const pmt_t &e2, const pmt_t &e3)
610 pmt_tuple *t = new pmt_tuple(4);
619 pmt_make_tuple(const pmt_t &e0, const pmt_t &e1, const pmt_t &e2, const pmt_t &e3, const pmt_t &e4)
621 pmt_tuple *t = new pmt_tuple(5);
631 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)
633 pmt_tuple *t = new pmt_tuple(6);
644 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)
646 pmt_tuple *t = new pmt_tuple(7);
658 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)
660 pmt_tuple *t = new pmt_tuple(8);
673 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)
675 pmt_tuple *t = new pmt_tuple(9);
689 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)
691 pmt_tuple *t = new pmt_tuple(10);
706 pmt_to_tuple(const pmt_t &x)
708 if (x->is_tuple()) // already one
711 size_t len = pmt_length(x);
712 pmt_tuple *t = new pmt_tuple(len);
716 for (size_t i = 0; i < len; i++)
717 t->_set(i, _vector(x)->ref(i));
723 for (size_t i = 0; i < len; i++){
724 t->_set(i, pmt_car(y));
730 throw pmt_wrong_type("pmt_to_tuple", x);
735 ////////////////////////////////////////////////////////////////////////////
736 // Uniform Numeric Vectors
737 ////////////////////////////////////////////////////////////////////////////
740 pmt_is_uniform_vector(pmt_t x)
742 return x->is_uniform_vector();
746 pmt_uniform_vector_elements(pmt_t vector, size_t &len)
748 if (!vector->is_uniform_vector())
749 throw pmt_wrong_type("pmt_uniform_vector_elements", vector);
750 return _uniform_vector(vector)->uniform_elements(len);
754 pmt_uniform_vector_writable_elements(pmt_t vector, size_t &len)
756 if (!vector->is_uniform_vector())
757 throw pmt_wrong_type("pmt_uniform_vector_writable_elements", vector);
758 return _uniform_vector(vector)->uniform_writable_elements(len);
761 ////////////////////////////////////////////////////////////////////////////
763 ////////////////////////////////////////////////////////////////////////////
766 * This is an a-list implementation.
768 * When we need better performance for large dictionaries, consider implementing
769 * persistent Red-Black trees as described in "Purely Functional Data Structures",
770 * Chris Okasaki, 1998, section 3.3.
774 pmt_is_dict(const pmt_t &obj)
776 return pmt_is_null(obj) || pmt_is_pair(obj);
786 pmt_dict_add(const pmt_t &dict, const pmt_t &key, const pmt_t &value)
788 if (pmt_is_null(dict))
789 return pmt_acons(key, value, PMT_NIL);
791 if (pmt_dict_has_key(dict, key))
792 return pmt_acons(key, value, pmt_dict_delete(dict, key));
794 return pmt_acons(key, value, dict);
798 pmt_dict_delete(const pmt_t &dict, const pmt_t &key)
800 if (pmt_is_null(dict))
803 if (pmt_eqv(pmt_caar(dict), key))
804 return pmt_cdr(dict);
806 return pmt_cons(pmt_car(dict), pmt_dict_delete(pmt_cdr(dict), key));
810 pmt_dict_ref(const pmt_t &dict, const pmt_t &key, const pmt_t ¬_found)
812 pmt_t p = pmt_assv(key, dict); // look for (key . value) pair
820 pmt_dict_has_key(const pmt_t &dict, const pmt_t &key)
822 return pmt_is_pair(pmt_assv(key, dict));
826 pmt_dict_items(pmt_t dict)
828 if (!pmt_is_dict(dict))
829 throw pmt_wrong_type("pmt_dict_values", dict);
831 return dict; // equivalent to dict in the a-list case
835 pmt_dict_keys(pmt_t dict)
837 if (!pmt_is_dict(dict))
838 throw pmt_wrong_type("pmt_dict_keys", dict);
840 return pmt_map(pmt_car, dict);
844 pmt_dict_values(pmt_t dict)
846 if (!pmt_is_dict(dict))
847 throw pmt_wrong_type("pmt_dict_keys", dict);
849 return pmt_map(pmt_cdr, dict);
852 ////////////////////////////////////////////////////////////////////////////
854 ////////////////////////////////////////////////////////////////////////////
856 pmt_any::pmt_any(const boost::any &any) : d_any(any) {}
859 pmt_is_any(pmt_t obj)
861 return obj->is_any();
865 pmt_make_any(const boost::any &any)
867 return pmt_t(new pmt_any(any));
871 pmt_any_ref(pmt_t obj)
874 throw pmt_wrong_type("pmt_any_ref", obj);
875 return _any(obj)->ref();
879 pmt_any_set(pmt_t obj, const boost::any &any)
882 throw pmt_wrong_type("pmt_any_set", obj);
886 ////////////////////////////////////////////////////////////////////////////
887 // msg_accepter -- built from "any"
888 ////////////////////////////////////////////////////////////////////////////
891 pmt_is_msg_accepter(const pmt_t &obj)
893 if (!pmt_is_any(obj))
896 boost::any r = pmt_any_ref(obj);
897 return boost::any_cast<gruel::msg_accepter_sptr>(&r) != 0;
900 //! make a msg_accepter
902 pmt_make_msg_accepter(gruel::msg_accepter_sptr ma)
904 return pmt_make_any(ma);
907 //! Return underlying msg_accepter
908 gruel::msg_accepter_sptr
909 pmt_msg_accepter_ref(const pmt_t &obj)
912 return boost::any_cast<gruel::msg_accepter_sptr>(pmt_any_ref(obj));
914 catch (boost::bad_any_cast &e){
915 throw pmt_wrong_type("pmt_msg_accepter_ref", obj);
920 ////////////////////////////////////////////////////////////////////////////
921 // Binary Large Object -- currently a u8vector
922 ////////////////////////////////////////////////////////////////////////////
927 // return pmt_is_u8vector(x);
928 return pmt_is_uniform_vector(x);
932 pmt_make_blob(const void *buf, size_t len_in_bytes)
934 return pmt_init_u8vector(len_in_bytes, (const uint8_t *) buf);
938 pmt_blob_data(pmt_t blob)
941 return pmt_uniform_vector_elements(blob, len);
945 pmt_blob_length(pmt_t blob)
948 pmt_uniform_vector_elements(blob, len);
953 ////////////////////////////////////////////////////////////////////////////
955 ////////////////////////////////////////////////////////////////////////////
958 pmt_eq(const pmt_t& x, const pmt_t& y)
964 pmt_eqv(const pmt_t& x, const pmt_t& y)
969 if (x->is_integer() && y->is_integer())
970 return _integer(x)->value() == _integer(y)->value();
972 if (x->is_uint64() && y->is_uint64())
973 return _uint64(x)->value() == _uint64(y)->value();
975 if (x->is_real() && y->is_real())
976 return _real(x)->value() == _real(y)->value();
978 if (x->is_complex() && y->is_complex())
979 return _complex(x)->value() == _complex(y)->value();
985 pmt_eqv_raw(pmt_base *x, pmt_base *y)
990 if (x->is_integer() && y->is_integer())
991 return _integer(x)->value() == _integer(y)->value();
993 if (x->is_uint64() && y->is_uint64())
994 return _uint64(x)->value() == _uint64(y)->value();
996 if (x->is_real() && y->is_real())
997 return _real(x)->value() == _real(y)->value();
999 if (x->is_complex() && y->is_complex())
1000 return _complex(x)->value() == _complex(y)->value();
1006 pmt_equal(const pmt_t& x, const pmt_t& y)
1011 if (x->is_pair() && y->is_pair())
1012 return pmt_equal(pmt_car(x), pmt_car(y)) && pmt_equal(pmt_cdr(x), pmt_cdr(y));
1014 if (x->is_vector() && y->is_vector()){
1015 pmt_vector *xv = _vector(x);
1016 pmt_vector *yv = _vector(y);
1017 if (xv->length() != yv->length())
1020 for (unsigned i = 0; i < xv->length(); i++)
1021 if (!pmt_equal(xv->_ref(i), yv->_ref(i)))
1027 if (x->is_tuple() && y->is_tuple()){
1028 pmt_tuple *xv = _tuple(x);
1029 pmt_tuple *yv = _tuple(y);
1030 if (xv->length() != yv->length())
1033 for (unsigned i = 0; i < xv->length(); i++)
1034 if (!pmt_equal(xv->_ref(i), yv->_ref(i)))
1040 if (x->is_uniform_vector() && y->is_uniform_vector()){
1041 pmt_uniform_vector *xv = _uniform_vector(x);
1042 pmt_uniform_vector *yv = _uniform_vector(y);
1043 if (xv->length() != yv->length())
1046 size_t len_x, len_y;
1047 if (memcmp(xv->uniform_elements(len_x),
1048 yv->uniform_elements(len_y),
1055 // FIXME add other cases here...
1061 pmt_length(const pmt_t& x)
1064 return _vector(x)->length();
1066 if (x->is_uniform_vector())
1067 return _uniform_vector(x)->length();
1070 return _tuple(x)->length();
1077 pmt_t it = pmt_cdr(x);
1078 while (pmt_is_pair(it)){
1082 if (pmt_is_null(it))
1085 // not a proper list
1086 throw pmt_wrong_type("pmt_length", x);
1089 // FIXME dictionary length (number of entries)
1091 throw pmt_wrong_type("pmt_length", x);
1095 pmt_assq(pmt_t obj, pmt_t alist)
1097 while (pmt_is_pair(alist)){
1098 pmt_t p = pmt_car(alist);
1099 if (!pmt_is_pair(p)) // malformed alist
1102 if (pmt_eq(obj, pmt_car(p)))
1105 alist = pmt_cdr(alist);
1111 * This avoids a bunch of shared_pointer reference count manipulation.
1114 pmt_assv_raw(pmt_base *obj, pmt_base *alist)
1116 while (alist->is_pair()){
1117 pmt_base *p = ((pmt_pair *)alist)->d_car.get();
1118 if (!p->is_pair()) // malformed alist
1121 if (pmt_eqv_raw(obj, ((pmt_pair *)p)->d_car.get()))
1122 return ((pmt_pair *)alist)->d_car;
1124 alist = (((pmt_pair *)alist)->d_cdr).get();
1132 pmt_assv(pmt_t obj, pmt_t alist)
1134 return pmt_assv_raw(obj.get(), alist.get());
1140 pmt_assv(pmt_t obj, pmt_t alist)
1142 while (pmt_is_pair(alist)){
1143 pmt_t p = pmt_car(alist);
1144 if (!pmt_is_pair(p)) // malformed alist
1147 if (pmt_eqv(obj, pmt_car(p)))
1150 alist = pmt_cdr(alist);
1159 pmt_assoc(pmt_t obj, pmt_t alist)
1161 while (pmt_is_pair(alist)){
1162 pmt_t p = pmt_car(alist);
1163 if (!pmt_is_pair(p)) // malformed alist
1166 if (pmt_equal(obj, pmt_car(p)))
1169 alist = pmt_cdr(alist);
1175 pmt_map(pmt_t proc(const pmt_t&), pmt_t list)
1179 while(pmt_is_pair(list)){
1180 r = pmt_cons(proc(pmt_car(list)), r);
1181 list = pmt_cdr(list);
1184 return pmt_reverse_x(r);
1188 pmt_reverse(pmt_t listx)
1193 while(pmt_is_pair(list)){
1194 r = pmt_cons(pmt_car(list), r);
1195 list = pmt_cdr(list);
1197 if (pmt_is_null(list))
1200 throw pmt_wrong_type("pmt_reverse", listx);
1204 pmt_reverse_x(pmt_t list)
1206 // FIXME do it destructively
1207 return pmt_reverse(list);
1211 pmt_nth(size_t n, pmt_t list)
1213 pmt_t t = pmt_nthcdr(n, list);
1221 pmt_nthcdr(size_t n, pmt_t list)
1223 if (!(pmt_is_pair(list) || pmt_is_null(list)))
1224 throw pmt_wrong_type("pmt_nthcdr", list);
1227 if (pmt_is_pair(list)){
1228 list = pmt_cdr(list);
1232 if (pmt_is_null(list))
1235 throw pmt_wrong_type("pmt_nthcdr: not a LIST", list);
1241 pmt_memq(pmt_t obj, pmt_t list)
1243 while (pmt_is_pair(list)){
1244 if (pmt_eq(obj, pmt_car(list)))
1246 list = pmt_cdr(list);
1252 pmt_memv(pmt_t obj, pmt_t list)
1254 while (pmt_is_pair(list)){
1255 if (pmt_eqv(obj, pmt_car(list)))
1257 list = pmt_cdr(list);
1263 pmt_member(pmt_t obj, pmt_t list)
1265 while (pmt_is_pair(list)){
1266 if (pmt_equal(obj, pmt_car(list)))
1268 list = pmt_cdr(list);
1274 pmt_subsetp(pmt_t list1, pmt_t list2)
1276 while (pmt_is_pair(list1)){
1277 pmt_t p = pmt_car(list1);
1278 if (pmt_is_false(pmt_memv(p, list2)))
1280 list1 = pmt_cdr(list1);
1286 pmt_list1(const pmt_t& x1)
1288 return pmt_cons(x1, PMT_NIL);
1292 pmt_list2(const pmt_t& x1, const pmt_t& x2)
1294 return pmt_cons(x1, pmt_cons(x2, PMT_NIL));
1298 pmt_list3(const pmt_t& x1, const pmt_t& x2, const pmt_t& x3)
1300 return pmt_cons(x1, pmt_cons(x2, pmt_cons(x3, PMT_NIL)));
1304 pmt_list4(const pmt_t& x1, const pmt_t& x2, const pmt_t& x3, const pmt_t& x4)
1306 return pmt_cons(x1, pmt_cons(x2, pmt_cons(x3, pmt_cons(x4, PMT_NIL))));
1310 pmt_list5(const pmt_t& x1, const pmt_t& x2, const pmt_t& x3, const pmt_t& x4, const pmt_t& x5)
1312 return pmt_cons(x1, pmt_cons(x2, pmt_cons(x3, pmt_cons(x4, pmt_cons(x5, PMT_NIL)))));
1316 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)
1318 return pmt_cons(x1, pmt_cons(x2, pmt_cons(x3, pmt_cons(x4, pmt_cons(x5, pmt_cons(x6, PMT_NIL))))));
1322 pmt_list_add(pmt_t list, const pmt_t& item)
1324 return pmt_reverse(pmt_cons(item, pmt_reverse(list)));
1328 pmt_caar(pmt_t pair)
1330 return (pmt_car(pmt_car(pair)));
1334 pmt_cadr(pmt_t pair)
1336 return pmt_car(pmt_cdr(pair));
1340 pmt_cdar(pmt_t pair)
1342 return pmt_cdr(pmt_car(pair));
1346 pmt_cddr(pmt_t pair)
1348 return pmt_cdr(pmt_cdr(pair));
1352 pmt_caddr(pmt_t pair)
1354 return pmt_car(pmt_cdr(pmt_cdr(pair)));
1358 pmt_cadddr(pmt_t pair)
1360 return pmt_car(pmt_cdr(pmt_cdr(pmt_cdr(pair))));
1364 pmt_is_eof_object(pmt_t obj)
1366 return pmt_eq(obj, PMT_EOF);
1372 printf("sizeof(pmt_t) = %3zd\n", sizeof(pmt_t));
1373 printf("sizeof(pmt_base) = %3zd\n", sizeof(pmt_base));
1374 printf("sizeof(pmt_bool) = %3zd\n", sizeof(pmt_bool));
1375 printf("sizeof(pmt_symbol) = %3zd\n", sizeof(pmt_symbol));
1376 printf("sizeof(pmt_integer) = %3zd\n", sizeof(pmt_integer));
1377 printf("sizeof(pmt_uint64) = %3zd\n", sizeof(pmt_uint64));
1378 printf("sizeof(pmt_real) = %3zd\n", sizeof(pmt_real));
1379 printf("sizeof(pmt_complex) = %3zd\n", sizeof(pmt_complex));
1380 printf("sizeof(pmt_null) = %3zd\n", sizeof(pmt_null));
1381 printf("sizeof(pmt_pair) = %3zd\n", sizeof(pmt_pair));
1382 printf("sizeof(pmt_vector) = %3zd\n", sizeof(pmt_vector));
1383 printf("sizeof(pmt_uniform_vector) = %3zd\n", sizeof(pmt_uniform_vector));
1386 } /* namespace pmt */