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(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(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));
296 return _integer(x)->value();
298 throw pmt_wrong_type("pmt_to_long", x);
301 ////////////////////////////////////////////////////////////////////////////
303 ////////////////////////////////////////////////////////////////////////////
305 pmt_real::pmt_real(double value) : d_value(value) {}
314 pmt_from_double(double x)
316 return pmt_t(new pmt_real(x));
320 pmt_to_double(pmt_t x)
323 return _real(x)->value();
325 return _integer(x)->value();
327 throw pmt_wrong_type("pmt_to_double", x);
330 ////////////////////////////////////////////////////////////////////////////
332 ////////////////////////////////////////////////////////////////////////////
334 pmt_complex::pmt_complex(std::complex<double> value) : d_value(value) {}
337 pmt_is_complex(pmt_t x)
339 return x->is_complex();
343 pmt_make_rectangular(double re, double im)
345 return pmt_t(new pmt_complex(std::complex<double>(re, im)));
349 pmt_to_complex(pmt_t x)
352 return _complex(x)->value();
354 return _real(x)->value();
356 return _integer(x)->value();
358 throw pmt_wrong_type("pmt_to_complex", x);
361 ////////////////////////////////////////////////////////////////////////////
363 ////////////////////////////////////////////////////////////////////////////
365 pmt_null::pmt_null() {}
366 pmt_pair::pmt_pair(pmt_t car, pmt_t cdr) : d_car(car), d_cdr(cdr) {}
375 pmt_is_pair(pmt_t obj)
377 return obj->is_pair();
381 pmt_cons(pmt_t x, pmt_t y)
383 return pmt_t(new pmt_pair(x, y));
390 return _pair(pair)->car();
392 throw pmt_wrong_type("pmt_car", pair);
399 return _pair(pair)->cdr();
401 throw pmt_wrong_type("pmt_cdr", pair);
405 pmt_set_car(pmt_t pair, pmt_t obj)
408 _pair(pair)->set_car(obj);
410 throw pmt_wrong_type("pmt_set_car", pair);
414 pmt_set_cdr(pmt_t pair, pmt_t obj)
417 _pair(pair)->set_cdr(obj);
419 throw pmt_wrong_type("pmt_set_cdr", pair);
422 ////////////////////////////////////////////////////////////////////////////
424 ////////////////////////////////////////////////////////////////////////////
426 pmt_vector::pmt_vector(size_t len, pmt_t fill)
429 for (size_t i = 0; i < len; i++)
434 pmt_vector::ref(size_t k) const
437 throw pmt_out_of_range("pmt_vector_ref", pmt_from_long(k));
442 pmt_vector::set(size_t k, pmt_t obj)
445 throw pmt_out_of_range("pmt_vector_set", pmt_from_long(k));
450 pmt_vector::fill(pmt_t obj)
452 for (size_t i = 0; i < length(); i++)
457 pmt_is_vector(pmt_t obj)
459 return obj->is_vector();
463 pmt_make_vector(size_t k, pmt_t fill)
465 return pmt_t(new pmt_vector(k, fill));
469 pmt_vector_ref(pmt_t vector, size_t k)
471 if (!vector->is_vector())
472 throw pmt_wrong_type("pmt_vector_ref", vector);
473 return _vector(vector)->ref(k);
477 pmt_vector_set(pmt_t vector, size_t k, pmt_t obj)
479 if (!vector->is_vector())
480 throw pmt_wrong_type("pmt_vector_set", vector);
481 _vector(vector)->set(k, obj);
485 pmt_vector_fill(pmt_t vector, pmt_t obj)
487 if (!vector->is_vector())
488 throw pmt_wrong_type("pmt_vector_set", vector);
489 _vector(vector)->fill(obj);
492 ////////////////////////////////////////////////////////////////////////////
493 // Uniform Numeric Vectors
494 ////////////////////////////////////////////////////////////////////////////
497 pmt_is_uniform_vector(pmt_t x)
499 return x->is_uniform_vector();
503 pmt_uniform_vector_elements(pmt_t vector, size_t &len)
505 if (!vector->is_uniform_vector())
506 throw pmt_wrong_type("pmt_uniform_vector_elements", vector);
507 return _uniform_vector(vector)->uniform_elements(len);
511 pmt_uniform_vector_writeable_elements(pmt_t vector, size_t &len)
513 if (!vector->is_uniform_vector())
514 throw pmt_wrong_type("pmt_uniform_vector_writeable_elements", vector);
515 return _uniform_vector(vector)->uniform_writeable_elements(len);
518 ////////////////////////////////////////////////////////////////////////////
520 ////////////////////////////////////////////////////////////////////////////
528 pmt_dict::set(pmt_t key, pmt_t value)
530 pmt_t p = pmt_assv(key, d_alist); // look for (key . value) pair
531 if (pmt_is_pair(p)){ // found existing pair...
532 pmt_set_cdr(p, value); // overrwrite cdr with new value
534 else { // not in the dict
535 d_alist = pmt_cons(pmt_cons(key, value), d_alist); // add new (key . value) pair
540 pmt_dict::ref(pmt_t key, pmt_t not_found) const
542 pmt_t p = pmt_assv(key, d_alist); // look for (key . value) pair
550 pmt_dict::has_key(pmt_t key) const
552 return pmt_is_pair(pmt_assv(key, d_alist));
556 pmt_dict::items() const
562 pmt_dict::keys() const
564 return pmt_map(pmt_car, d_alist);
568 pmt_dict::values() const
570 return pmt_map(pmt_cdr, d_alist);
574 pmt_is_dict(pmt_t obj)
576 return obj->is_dict();
582 return pmt_t(new pmt_dict());
586 pmt_dict_set(pmt_t dict, pmt_t key, pmt_t value)
588 if (!dict->is_dict())
589 throw pmt_wrong_type("pmt_dict_set", dict);
591 _dict(dict)->set(key, value);
595 pmt_dict_has_key(pmt_t dict, pmt_t key)
597 if (!dict->is_dict())
598 throw pmt_wrong_type("pmt_dict_has_key", dict);
600 return _dict(dict)->has_key(key);
604 pmt_dict_ref(pmt_t dict, pmt_t key, pmt_t not_found)
606 if (!dict->is_dict())
607 throw pmt_wrong_type("pmt_dict_ref", dict);
609 return _dict(dict)->ref(key, not_found);
613 pmt_dict_items(pmt_t dict)
615 if (!dict->is_dict())
616 throw pmt_wrong_type("pmt_dict_items", dict);
618 return _dict(dict)->items();
622 pmt_dict_keys(pmt_t dict)
624 if (!dict->is_dict())
625 throw pmt_wrong_type("pmt_dict_keys", dict);
627 return _dict(dict)->keys();
631 pmt_dict_values(pmt_t dict)
633 if (!dict->is_dict())
634 throw pmt_wrong_type("pmt_dict_values", dict);
636 return _dict(dict)->values();
639 ////////////////////////////////////////////////////////////////////////////
641 ////////////////////////////////////////////////////////////////////////////
643 pmt_any::pmt_any(const boost::any &any) : d_any(any) {}
646 pmt_is_any(pmt_t obj)
648 return obj->is_any();
652 pmt_make_any(const boost::any &any)
654 return pmt_t(new pmt_any(any));
658 pmt_any_ref(pmt_t obj)
661 throw pmt_wrong_type("pmt_any_ref", obj);
662 return _any(obj)->ref();
666 pmt_any_set(pmt_t obj, const boost::any &any)
669 throw pmt_wrong_type("pmt_any_set", obj);
673 ////////////////////////////////////////////////////////////////////////////
675 ////////////////////////////////////////////////////////////////////////////
678 pmt_eq(pmt_t x, pmt_t y)
684 pmt_eqv(pmt_t x, pmt_t y)
689 if (x->is_integer() && y->is_integer())
690 return _integer(x)->value() == _integer(y)->value();
692 if (x->is_real() && y->is_real())
693 return _real(x)->value() == _real(y)->value();
695 if (x->is_complex() && y->is_complex())
696 return _complex(x)->value() == _complex(y)->value();
702 pmt_equal(pmt_t x, pmt_t y)
707 if (x->is_pair() && y->is_pair())
708 return pmt_equal(pmt_car(x), pmt_car(y)) && pmt_equal(pmt_cdr(x), pmt_cdr(y));
710 if (x->is_vector() && y->is_vector()){
711 pmt_vector *xv = _vector(x);
712 pmt_vector *yv = _vector(y);
713 if (xv->length() != yv->length())
716 for (unsigned i = 0; i < xv->length(); i++)
717 if (!pmt_equal(xv->_ref(i), yv->_ref(i)))
723 if (x->is_uniform_vector() && y->is_uniform_vector()){
724 pmt_uniform_vector *xv = _uniform_vector(x);
725 pmt_uniform_vector *yv = _uniform_vector(y);
726 if (xv->length() != yv->length())
730 if (memcmp(xv->uniform_elements(len_x),
731 yv->uniform_elements(len_y),
738 // FIXME add other cases here...
747 return _vector(x)->length();
749 if (x->is_uniform_vector())
750 return _uniform_vector(x)->length();
752 if (x->is_pair() || x->is_null()) {
754 while (pmt_is_pair(x)){
762 throw pmt_wrong_type("pmt_length", x);
765 // FIXME dictionary length (number of entries)
767 throw pmt_wrong_type("pmt_length", x);
771 pmt_assq(pmt_t obj, pmt_t alist)
773 while (pmt_is_pair(alist)){
774 pmt_t p = pmt_car(alist);
775 if (!pmt_is_pair(p)) // malformed alist
778 if (pmt_eq(obj, pmt_car(p)))
781 alist = pmt_cdr(alist);
787 pmt_assv(pmt_t obj, pmt_t alist)
789 while (pmt_is_pair(alist)){
790 pmt_t p = pmt_car(alist);
791 if (!pmt_is_pair(p)) // malformed alist
794 if (pmt_eqv(obj, pmt_car(p)))
797 alist = pmt_cdr(alist);
803 pmt_assoc(pmt_t obj, pmt_t alist)
805 while (pmt_is_pair(alist)){
806 pmt_t p = pmt_car(alist);
807 if (!pmt_is_pair(p)) // malformed alist
810 if (pmt_equal(obj, pmt_car(p)))
813 alist = pmt_cdr(alist);
819 pmt_map(pmt_t proc(pmt_t), pmt_t list)
823 while(pmt_is_pair(list)){
824 r = pmt_cons(proc(pmt_car(list)), r);
825 list = pmt_cdr(list);
828 return pmt_reverse_x(r);
832 pmt_reverse(pmt_t listx)
837 while(pmt_is_pair(list)){
838 r = pmt_cons(pmt_car(list), r);
839 list = pmt_cdr(list);
841 if (pmt_is_null(list))
844 throw pmt_wrong_type("pmt_reverse", listx);
848 pmt_reverse_x(pmt_t list)
850 // FIXME do it destructively
851 return pmt_reverse(list);
855 pmt_nth(size_t n, pmt_t list)
857 pmt_t t = pmt_nthcdr(n, list);
865 pmt_nthcdr(size_t n, pmt_t list)
867 if (!(pmt_is_null(list) || pmt_is_pair(list)))
868 throw pmt_wrong_type("pmt_nthcdr", list);
871 if (pmt_is_pair(list)){
872 list = pmt_cdr(list);
876 if (pmt_is_null(list))
879 throw pmt_wrong_type("pmt_nthcdr: not a LIST", list);
885 pmt_memq(pmt_t obj, pmt_t list)
887 while (pmt_is_pair(list)){
888 if (pmt_eq(obj, pmt_car(list)))
890 list = pmt_cdr(list);
896 pmt_memv(pmt_t obj, pmt_t list)
898 while (pmt_is_pair(list)){
899 if (pmt_eqv(obj, pmt_car(list)))
901 list = pmt_cdr(list);
907 pmt_member(pmt_t obj, pmt_t list)
909 while (pmt_is_pair(list)){
910 if (pmt_equal(obj, pmt_car(list)))
912 list = pmt_cdr(list);
918 pmt_subsetp(pmt_t list1, pmt_t list2)
920 while (pmt_is_pair(list1)){
921 pmt_t p = pmt_car(list1);
922 if (pmt_is_false(pmt_memv(p, list2)))
924 list1 = pmt_cdr(list1);
932 return pmt_cons(x1, PMT_NIL);
936 pmt_list2(pmt_t x1, pmt_t x2)
938 return pmt_cons(x1, pmt_cons(x2, PMT_NIL));
942 pmt_list3(pmt_t x1, pmt_t x2, pmt_t x3)
944 return pmt_cons(x1, pmt_cons(x2, pmt_cons(x3, PMT_NIL)));
948 pmt_list4(pmt_t x1, pmt_t x2, pmt_t x3, pmt_t x4)
950 return pmt_cons(x1, pmt_cons(x2, pmt_cons(x3, pmt_cons(x4, PMT_NIL))));
954 pmt_list5(pmt_t x1, pmt_t x2, pmt_t x3, pmt_t x4, pmt_t x5)
956 return pmt_cons(x1, pmt_cons(x2, pmt_cons(x3, pmt_cons(x4, pmt_cons(x5, PMT_NIL)))));
960 pmt_list6(pmt_t x1, pmt_t x2, pmt_t x3, pmt_t x4, pmt_t x5, pmt_t x6)
962 return pmt_cons(x1, pmt_cons(x2, pmt_cons(x3, pmt_cons(x4, pmt_cons(x5, pmt_cons(x6, PMT_NIL))))));
966 pmt_list_add(pmt_t list, pmt_t item)
968 return pmt_reverse(pmt_cons(item, pmt_reverse(list)));
974 return (pmt_car(pmt_car(pair)));
980 return pmt_car(pmt_cdr(pair));
986 return pmt_cdr(pmt_car(pair));
992 return pmt_cdr(pmt_cdr(pair));
996 pmt_caddr(pmt_t pair)
998 return pmt_car(pmt_cdr(pmt_cdr(pair)));
1002 pmt_cadddr(pmt_t pair)
1004 return pmt_car(pmt_cdr(pmt_cdr(pmt_cdr(pair))));
1008 pmt_is_eof_object(pmt_t obj)
1010 return pmt_eq(obj, PMT_EOF);
1016 printf("sizeof(pmt_t) = %3zd\n", sizeof(pmt_t));
1017 printf("sizeof(pmt_base) = %3zd\n", sizeof(pmt_base));
1018 printf("sizeof(pmt_bool) = %3zd\n", sizeof(pmt_bool));
1019 printf("sizeof(pmt_symbol) = %3zd\n", sizeof(pmt_symbol));
1020 printf("sizeof(pmt_integer) = %3zd\n", sizeof(pmt_integer));
1021 printf("sizeof(pmt_real) = %3zd\n", sizeof(pmt_real));
1022 printf("sizeof(pmt_complex) = %3zd\n", sizeof(pmt_complex));
1023 printf("sizeof(pmt_null) = %3zd\n", sizeof(pmt_null));
1024 printf("sizeof(pmt_pair) = %3zd\n", sizeof(pmt_pair));
1025 printf("sizeof(pmt_vector) = %3zd\n", sizeof(pmt_vector));
1026 printf("sizeof(pmt_dict) = %3zd\n", sizeof(pmt_dict));
1027 printf("sizeof(pmt_uniform_vector) = %3zd\n", sizeof(pmt_uniform_vector));