3 * Copyright 2006 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.
33 static const int CACHE_LINE_SIZE = 64; // good guess
35 # if (PMT_LOCAL_ALLOCATOR)
37 static pmt_pool global_pmt_pool(sizeof(pmt_pair), CACHE_LINE_SIZE);
40 pmt_base::operator new(size_t size)
42 void *p = global_pmt_pool.malloc();
44 // fprintf(stderr, "pmt_base::new p = %p\n", p);
45 assert((reinterpret_cast<intptr_t>(p) & (CACHE_LINE_SIZE - 1)) == 0);
50 pmt_base::operator delete(void *p, size_t size)
52 global_pmt_pool.free(p);
60 // nop -- out of line virtual destructor
63 ////////////////////////////////////////////////////////////////////////////
65 ////////////////////////////////////////////////////////////////////////////
67 pmt_exception::pmt_exception(const std::string &msg, pmt_t obj)
68 : logic_error(msg + ": " + pmt_write_string(obj))
72 pmt_wrong_type::pmt_wrong_type(const std::string &msg, pmt_t obj)
73 : pmt_exception(msg + ": wrong_type ", obj)
77 pmt_out_of_range::pmt_out_of_range(const std::string &msg, pmt_t obj)
78 : pmt_exception(msg + ": out of range ", obj)
82 pmt_notimplemented::pmt_notimplemented(const std::string &msg, pmt_t obj)
83 : pmt_exception(msg + ": notimplemented ", obj)
87 ////////////////////////////////////////////////////////////////////////////
89 ////////////////////////////////////////////////////////////////////////////
94 return dynamic_cast<pmt_symbol*>(x.get());
100 return dynamic_cast<pmt_integer*>(x.get());
106 return dynamic_cast<pmt_real*>(x.get());
112 return dynamic_cast<pmt_complex*>(x.get());
118 return dynamic_cast<pmt_pair*>(x.get());
124 return dynamic_cast<pmt_vector*>(x.get());
127 static pmt_uniform_vector *
128 _uniform_vector(pmt_t x)
130 return dynamic_cast<pmt_uniform_vector*>(x.get());
136 return dynamic_cast<pmt_dict*>(x.get());
142 return dynamic_cast<pmt_any*>(x.get());
145 ////////////////////////////////////////////////////////////////////////////
147 ////////////////////////////////////////////////////////////////////////////
149 const pmt_t PMT_T = pmt_t(new pmt_bool()); // singleton
150 const pmt_t PMT_F = pmt_t(new pmt_bool()); // singleton
151 const pmt_t PMT_NIL = pmt_t(new pmt_null()); // singleton
152 const pmt_t PMT_EOF = pmt_cons(PMT_NIL, PMT_NIL); // singleton
154 ////////////////////////////////////////////////////////////////////////////
156 ////////////////////////////////////////////////////////////////////////////
158 pmt_bool::pmt_bool(){}
161 pmt_is_true(pmt_t obj)
167 pmt_is_false(pmt_t obj)
173 pmt_is_bool(pmt_t obj)
175 return obj->is_bool();
179 pmt_from_bool(bool val)
181 return val ? PMT_T : PMT_F;
185 pmt_to_bool(pmt_t val)
191 throw pmt_wrong_type("pmt_to_bool", val);
194 ////////////////////////////////////////////////////////////////////////////
196 ////////////////////////////////////////////////////////////////////////////
198 static const unsigned int SYMBOL_HASH_TABLE_SIZE = 701;
199 static std::vector<pmt_t> s_symbol_hash_table(SYMBOL_HASH_TABLE_SIZE);
201 pmt_symbol::pmt_symbol(const std::string &name) : d_name(name){}
205 hash_string(const std::string &s)
210 for (std::string::const_iterator p = s.begin(); p != s.end(); ++p){
211 h = (h << 4) + (*p & 0xff);
222 pmt_is_symbol(const pmt_t& obj)
224 return obj->is_symbol();
228 pmt_string_to_symbol(const std::string &name)
230 unsigned hash = hash_string(name) % SYMBOL_HASH_TABLE_SIZE;
232 // Does a symbol with this name already exist?
233 for (pmt_t sym = s_symbol_hash_table[hash]; sym; sym = _symbol(sym)->next()){
234 if (name == _symbol(sym)->name())
235 return sym; // Yes. Return it
238 // Nope. Make a new one.
239 pmt_t sym = pmt_t(new pmt_symbol(name));
240 _symbol(sym)->set_next(s_symbol_hash_table[hash]);
241 s_symbol_hash_table[hash] = sym;
247 pmt_intern(const std::string &name)
249 return pmt_string_to_symbol(name);
253 pmt_symbol_to_string(const pmt_t& sym)
255 if (!sym->is_symbol())
256 throw pmt_wrong_type("pmt_symbol_to_string", sym);
258 return _symbol(sym)->name();
263 ////////////////////////////////////////////////////////////////////////////
265 ////////////////////////////////////////////////////////////////////////////
268 pmt_is_number(pmt_t x)
270 return x->is_number();
273 ////////////////////////////////////////////////////////////////////////////
275 ////////////////////////////////////////////////////////////////////////////
277 pmt_integer::pmt_integer(long value) : d_value(value) {}
280 pmt_is_integer(pmt_t x)
282 return x->is_integer();
287 pmt_from_long(long x)
289 return pmt_t(new pmt_integer(x));
295 pmt_integer* i = dynamic_cast<pmt_integer*>(x.get());
299 throw pmt_wrong_type("pmt_to_long", x);
302 ////////////////////////////////////////////////////////////////////////////
304 ////////////////////////////////////////////////////////////////////////////
306 pmt_real::pmt_real(double value) : d_value(value) {}
315 pmt_from_double(double x)
317 return pmt_t(new pmt_real(x));
321 pmt_to_double(pmt_t x)
324 return _real(x)->value();
326 return _integer(x)->value();
328 throw pmt_wrong_type("pmt_to_double", x);
331 ////////////////////////////////////////////////////////////////////////////
333 ////////////////////////////////////////////////////////////////////////////
335 pmt_complex::pmt_complex(std::complex<double> value) : d_value(value) {}
338 pmt_is_complex(pmt_t x)
340 return x->is_complex();
344 pmt_make_rectangular(double re, double im)
346 return pmt_t(new pmt_complex(std::complex<double>(re, im)));
350 pmt_to_complex(pmt_t x)
353 return _complex(x)->value();
355 return _real(x)->value();
357 return _integer(x)->value();
359 throw pmt_wrong_type("pmt_to_complex", x);
362 ////////////////////////////////////////////////////////////////////////////
364 ////////////////////////////////////////////////////////////////////////////
366 pmt_null::pmt_null() {}
367 pmt_pair::pmt_pair(const pmt_t& car, const pmt_t& cdr) : d_car(car), d_cdr(cdr) {}
370 pmt_is_null(const pmt_t& x)
376 pmt_is_pair(const pmt_t& obj)
378 return obj->is_pair();
382 pmt_cons(const pmt_t& x, const pmt_t& y)
384 return pmt_t(new pmt_pair(x, y));
388 pmt_car(const pmt_t& pair)
390 pmt_pair* p = dynamic_cast<pmt_pair*>(pair.get());
394 throw pmt_wrong_type("pmt_car", pair);
398 pmt_cdr(const pmt_t& pair)
400 pmt_pair* p = dynamic_cast<pmt_pair*>(pair.get());
404 throw pmt_wrong_type("pmt_cdr", pair);
408 pmt_set_car(pmt_t pair, pmt_t obj)
411 _pair(pair)->set_car(obj);
413 throw pmt_wrong_type("pmt_set_car", pair);
417 pmt_set_cdr(pmt_t pair, pmt_t obj)
420 _pair(pair)->set_cdr(obj);
422 throw pmt_wrong_type("pmt_set_cdr", pair);
425 ////////////////////////////////////////////////////////////////////////////
427 ////////////////////////////////////////////////////////////////////////////
429 pmt_vector::pmt_vector(size_t len, pmt_t fill)
432 for (size_t i = 0; i < len; i++)
437 pmt_vector::ref(size_t k) const
440 throw pmt_out_of_range("pmt_vector_ref", pmt_from_long(k));
445 pmt_vector::set(size_t k, pmt_t obj)
448 throw pmt_out_of_range("pmt_vector_set", pmt_from_long(k));
453 pmt_vector::fill(pmt_t obj)
455 for (size_t i = 0; i < length(); i++)
460 pmt_is_vector(pmt_t obj)
462 return obj->is_vector();
466 pmt_make_vector(size_t k, pmt_t fill)
468 return pmt_t(new pmt_vector(k, fill));
472 pmt_vector_ref(pmt_t vector, size_t k)
474 if (!vector->is_vector())
475 throw pmt_wrong_type("pmt_vector_ref", vector);
476 return _vector(vector)->ref(k);
480 pmt_vector_set(pmt_t vector, size_t k, pmt_t obj)
482 if (!vector->is_vector())
483 throw pmt_wrong_type("pmt_vector_set", vector);
484 _vector(vector)->set(k, obj);
488 pmt_vector_fill(pmt_t vector, pmt_t obj)
490 if (!vector->is_vector())
491 throw pmt_wrong_type("pmt_vector_set", vector);
492 _vector(vector)->fill(obj);
495 ////////////////////////////////////////////////////////////////////////////
496 // Uniform Numeric Vectors
497 ////////////////////////////////////////////////////////////////////////////
500 pmt_is_uniform_vector(pmt_t x)
502 return x->is_uniform_vector();
506 pmt_uniform_vector_elements(pmt_t vector, size_t &len)
508 if (!vector->is_uniform_vector())
509 throw pmt_wrong_type("pmt_uniform_vector_elements", vector);
510 return _uniform_vector(vector)->uniform_elements(len);
514 pmt_uniform_vector_writable_elements(pmt_t vector, size_t &len)
516 if (!vector->is_uniform_vector())
517 throw pmt_wrong_type("pmt_uniform_vector_writable_elements", vector);
518 return _uniform_vector(vector)->uniform_writable_elements(len);
521 ////////////////////////////////////////////////////////////////////////////
523 ////////////////////////////////////////////////////////////////////////////
531 pmt_dict::set(pmt_t key, pmt_t value)
533 pmt_t p = pmt_assv(key, d_alist); // look for (key . value) pair
534 if (pmt_is_pair(p)){ // found existing pair...
535 pmt_set_cdr(p, value); // overrwrite cdr with new value
537 else { // not in the dict
538 d_alist = pmt_cons(pmt_cons(key, value), d_alist); // add new (key . value) pair
543 pmt_dict::ref(pmt_t key, pmt_t not_found) const
545 pmt_t p = pmt_assv(key, d_alist); // look for (key . value) pair
553 pmt_dict::has_key(pmt_t key) const
555 return pmt_is_pair(pmt_assv(key, d_alist));
559 pmt_dict::items() const
565 pmt_dict::keys() const
567 return pmt_map(pmt_car, d_alist);
571 pmt_dict::values() const
573 return pmt_map(pmt_cdr, d_alist);
577 pmt_is_dict(pmt_t obj)
579 return obj->is_dict();
585 return pmt_t(new pmt_dict());
589 pmt_dict_set(pmt_t dict, pmt_t key, pmt_t value)
591 pmt_dict* d = _dict(dict);
593 throw pmt_wrong_type("pmt_dict_set", dict);
599 pmt_dict_has_key(pmt_t dict, pmt_t key)
601 pmt_dict* d = _dict(dict);
603 throw pmt_wrong_type("pmt_dict_has_key", dict);
605 return d->has_key(key);
609 pmt_dict_ref(pmt_t dict, pmt_t key, pmt_t not_found)
611 pmt_dict* d = _dict(dict);
613 throw pmt_wrong_type("pmt_dict_ref", dict);
615 return d->ref(key, not_found);
619 pmt_dict_items(pmt_t dict)
621 if (!dict->is_dict())
622 throw pmt_wrong_type("pmt_dict_items", dict);
624 return _dict(dict)->items();
628 pmt_dict_keys(pmt_t dict)
630 if (!dict->is_dict())
631 throw pmt_wrong_type("pmt_dict_keys", dict);
633 return _dict(dict)->keys();
637 pmt_dict_values(pmt_t dict)
639 if (!dict->is_dict())
640 throw pmt_wrong_type("pmt_dict_values", dict);
642 return _dict(dict)->values();
645 ////////////////////////////////////////////////////////////////////////////
647 ////////////////////////////////////////////////////////////////////////////
649 pmt_any::pmt_any(const boost::any &any) : d_any(any) {}
652 pmt_is_any(pmt_t obj)
654 return obj->is_any();
658 pmt_make_any(const boost::any &any)
660 return pmt_t(new pmt_any(any));
664 pmt_any_ref(pmt_t obj)
667 throw pmt_wrong_type("pmt_any_ref", obj);
668 return _any(obj)->ref();
672 pmt_any_set(pmt_t obj, const boost::any &any)
675 throw pmt_wrong_type("pmt_any_set", obj);
679 ////////////////////////////////////////////////////////////////////////////
681 ////////////////////////////////////////////////////////////////////////////
684 pmt_eq(const pmt_t& x, const pmt_t& y)
690 pmt_eqv(const pmt_t& x, const pmt_t& y)
695 if (x->is_integer() && y->is_integer())
696 return _integer(x)->value() == _integer(y)->value();
698 if (x->is_real() && y->is_real())
699 return _real(x)->value() == _real(y)->value();
701 if (x->is_complex() && y->is_complex())
702 return _complex(x)->value() == _complex(y)->value();
708 pmt_equal(const pmt_t& x, const pmt_t& y)
713 if (x->is_pair() && y->is_pair())
714 return pmt_equal(pmt_car(x), pmt_car(y)) && pmt_equal(pmt_cdr(x), pmt_cdr(y));
716 if (x->is_vector() && y->is_vector()){
717 pmt_vector *xv = _vector(x);
718 pmt_vector *yv = _vector(y);
719 if (xv->length() != yv->length())
722 for (unsigned i = 0; i < xv->length(); i++)
723 if (!pmt_equal(xv->_ref(i), yv->_ref(i)))
729 if (x->is_uniform_vector() && y->is_uniform_vector()){
730 pmt_uniform_vector *xv = _uniform_vector(x);
731 pmt_uniform_vector *yv = _uniform_vector(y);
732 if (xv->length() != yv->length())
736 if (memcmp(xv->uniform_elements(len_x),
737 yv->uniform_elements(len_y),
744 // FIXME add other cases here...
750 pmt_length(const pmt_t& x)
753 return _vector(x)->length();
755 if (x->is_uniform_vector())
756 return _uniform_vector(x)->length();
758 if (x->is_null()) return 0;
762 pmt_t it = pmt_cdr(x);
763 while (pmt_is_pair(it)){
771 throw pmt_wrong_type("pmt_length", x);
774 // FIXME dictionary length (number of entries)
776 throw pmt_wrong_type("pmt_length", x);
780 pmt_assq(pmt_t obj, pmt_t alist)
782 while (pmt_is_pair(alist)){
783 pmt_t p = pmt_car(alist);
784 if (!pmt_is_pair(p)) // malformed alist
787 if (pmt_eq(obj, pmt_car(p)))
790 alist = pmt_cdr(alist);
796 pmt_assv(pmt_t obj, pmt_t alist)
798 while (pmt_is_pair(alist)){
799 pmt_t p = pmt_car(alist);
800 if (!pmt_is_pair(p)) // malformed alist
803 if (pmt_eqv(obj, pmt_car(p)))
806 alist = pmt_cdr(alist);
812 pmt_assoc(pmt_t obj, pmt_t alist)
814 while (pmt_is_pair(alist)){
815 pmt_t p = pmt_car(alist);
816 if (!pmt_is_pair(p)) // malformed alist
819 if (pmt_equal(obj, pmt_car(p)))
822 alist = pmt_cdr(alist);
828 pmt_map(pmt_t proc(const pmt_t&), pmt_t list)
832 while(pmt_is_pair(list)){
833 r = pmt_cons(proc(pmt_car(list)), r);
834 list = pmt_cdr(list);
837 return pmt_reverse_x(r);
841 pmt_reverse(pmt_t listx)
846 while(pmt_is_pair(list)){
847 r = pmt_cons(pmt_car(list), r);
848 list = pmt_cdr(list);
850 if (pmt_is_null(list))
853 throw pmt_wrong_type("pmt_reverse", listx);
857 pmt_reverse_x(pmt_t list)
859 // FIXME do it destructively
860 return pmt_reverse(list);
864 pmt_nth(size_t n, pmt_t list)
866 pmt_t t = pmt_nthcdr(n, list);
874 pmt_nthcdr(size_t n, pmt_t list)
876 if (!(pmt_is_pair(list) || pmt_is_null(list)))
877 throw pmt_wrong_type("pmt_nthcdr", list);
880 if (pmt_is_pair(list)){
881 list = pmt_cdr(list);
885 if (pmt_is_null(list))
888 throw pmt_wrong_type("pmt_nthcdr: not a LIST", list);
894 pmt_memq(pmt_t obj, pmt_t list)
896 while (pmt_is_pair(list)){
897 if (pmt_eq(obj, pmt_car(list)))
899 list = pmt_cdr(list);
905 pmt_memv(pmt_t obj, pmt_t list)
907 while (pmt_is_pair(list)){
908 if (pmt_eqv(obj, pmt_car(list)))
910 list = pmt_cdr(list);
916 pmt_member(pmt_t obj, pmt_t list)
918 while (pmt_is_pair(list)){
919 if (pmt_equal(obj, pmt_car(list)))
921 list = pmt_cdr(list);
927 pmt_subsetp(pmt_t list1, pmt_t list2)
929 while (pmt_is_pair(list1)){
930 pmt_t p = pmt_car(list1);
931 if (pmt_is_false(pmt_memv(p, list2)))
933 list1 = pmt_cdr(list1);
939 pmt_list1(const pmt_t& x1)
941 return pmt_cons(x1, PMT_NIL);
945 pmt_list2(const pmt_t& x1, const pmt_t& x2)
947 return pmt_cons(x1, pmt_cons(x2, PMT_NIL));
951 pmt_list3(const pmt_t& x1, const pmt_t& x2, const pmt_t& x3)
953 return pmt_cons(x1, pmt_cons(x2, pmt_cons(x3, PMT_NIL)));
957 pmt_list4(const pmt_t& x1, const pmt_t& x2, const pmt_t& x3, const pmt_t& x4)
959 return pmt_cons(x1, pmt_cons(x2, pmt_cons(x3, pmt_cons(x4, PMT_NIL))));
963 pmt_list5(const pmt_t& x1, const pmt_t& x2, const pmt_t& x3, const pmt_t& x4, const pmt_t& x5)
965 return pmt_cons(x1, pmt_cons(x2, pmt_cons(x3, pmt_cons(x4, pmt_cons(x5, PMT_NIL)))));
969 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)
971 return pmt_cons(x1, pmt_cons(x2, pmt_cons(x3, pmt_cons(x4, pmt_cons(x5, pmt_cons(x6, PMT_NIL))))));
975 pmt_list_add(pmt_t list, const pmt_t& item)
977 return pmt_reverse(pmt_cons(item, pmt_reverse(list)));
983 return (pmt_car(pmt_car(pair)));
989 return pmt_car(pmt_cdr(pair));
995 return pmt_cdr(pmt_car(pair));
1001 return pmt_cdr(pmt_cdr(pair));
1005 pmt_caddr(pmt_t pair)
1007 return pmt_car(pmt_cdr(pmt_cdr(pair)));
1011 pmt_cadddr(pmt_t pair)
1013 return pmt_car(pmt_cdr(pmt_cdr(pmt_cdr(pair))));
1017 pmt_is_eof_object(pmt_t obj)
1019 return pmt_eq(obj, PMT_EOF);
1025 printf("sizeof(pmt_t) = %3zd\n", sizeof(pmt_t));
1026 printf("sizeof(pmt_base) = %3zd\n", sizeof(pmt_base));
1027 printf("sizeof(pmt_bool) = %3zd\n", sizeof(pmt_bool));
1028 printf("sizeof(pmt_symbol) = %3zd\n", sizeof(pmt_symbol));
1029 printf("sizeof(pmt_integer) = %3zd\n", sizeof(pmt_integer));
1030 printf("sizeof(pmt_real) = %3zd\n", sizeof(pmt_real));
1031 printf("sizeof(pmt_complex) = %3zd\n", sizeof(pmt_complex));
1032 printf("sizeof(pmt_null) = %3zd\n", sizeof(pmt_null));
1033 printf("sizeof(pmt_pair) = %3zd\n", sizeof(pmt_pair));
1034 printf("sizeof(pmt_vector) = %3zd\n", sizeof(pmt_vector));
1035 printf("sizeof(pmt_dict) = %3zd\n", sizeof(pmt_dict));
1036 printf("sizeof(pmt_uniform_vector) = %3zd\n", sizeof(pmt_uniform_vector));