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.
27 #include <gruel/pmt.h>
30 #include <gruel/pmt_pool.h>
35 static const int CACHE_LINE_SIZE = 64; // good guess
37 # if (PMT_LOCAL_ALLOCATOR)
39 static pmt_pool global_pmt_pool(sizeof(pmt_pair), CACHE_LINE_SIZE);
42 pmt_base::operator new(size_t size)
44 void *p = global_pmt_pool.malloc();
46 // fprintf(stderr, "pmt_base::new p = %p\n", p);
47 assert((reinterpret_cast<intptr_t>(p) & (CACHE_LINE_SIZE - 1)) == 0);
52 pmt_base::operator delete(void *p, size_t size)
54 global_pmt_pool.free(p);
62 // nop -- out of line virtual destructor
65 ////////////////////////////////////////////////////////////////////////////
67 ////////////////////////////////////////////////////////////////////////////
69 pmt_exception::pmt_exception(const std::string &msg, pmt_t obj)
70 : logic_error(msg + ": " + pmt_write_string(obj))
74 pmt_wrong_type::pmt_wrong_type(const std::string &msg, pmt_t obj)
75 : pmt_exception(msg + ": wrong_type ", obj)
79 pmt_out_of_range::pmt_out_of_range(const std::string &msg, pmt_t obj)
80 : pmt_exception(msg + ": out of range ", obj)
84 pmt_notimplemented::pmt_notimplemented(const std::string &msg, pmt_t obj)
85 : pmt_exception(msg + ": notimplemented ", obj)
89 ////////////////////////////////////////////////////////////////////////////
91 ////////////////////////////////////////////////////////////////////////////
96 return dynamic_cast<pmt_symbol*>(x.get());
102 return dynamic_cast<pmt_integer*>(x.get());
108 return dynamic_cast<pmt_real*>(x.get());
114 return dynamic_cast<pmt_complex*>(x.get());
120 return dynamic_cast<pmt_pair*>(x.get());
126 return dynamic_cast<pmt_vector*>(x.get());
129 static pmt_uniform_vector *
130 _uniform_vector(pmt_t x)
132 return dynamic_cast<pmt_uniform_vector*>(x.get());
138 return dynamic_cast<pmt_dict*>(x.get());
144 return dynamic_cast<pmt_any*>(x.get());
147 ////////////////////////////////////////////////////////////////////////////
149 ////////////////////////////////////////////////////////////////////////////
151 const pmt_t PMT_T = pmt_t(new pmt_bool()); // singleton
152 const pmt_t PMT_F = pmt_t(new pmt_bool()); // singleton
153 const pmt_t PMT_NIL = pmt_t(new pmt_null()); // singleton
154 const pmt_t PMT_EOF = pmt_cons(PMT_NIL, PMT_NIL); // singleton
156 ////////////////////////////////////////////////////////////////////////////
158 ////////////////////////////////////////////////////////////////////////////
160 pmt_bool::pmt_bool(){}
163 pmt_is_true(pmt_t obj)
169 pmt_is_false(pmt_t obj)
175 pmt_is_bool(pmt_t obj)
177 return obj->is_bool();
181 pmt_from_bool(bool val)
183 return val ? PMT_T : PMT_F;
187 pmt_to_bool(pmt_t val)
193 throw pmt_wrong_type("pmt_to_bool", val);
196 ////////////////////////////////////////////////////////////////////////////
198 ////////////////////////////////////////////////////////////////////////////
200 static const unsigned int SYMBOL_HASH_TABLE_SIZE = 701;
201 static std::vector<pmt_t> s_symbol_hash_table(SYMBOL_HASH_TABLE_SIZE);
203 pmt_symbol::pmt_symbol(const std::string &name) : d_name(name){}
207 hash_string(const std::string &s)
212 for (std::string::const_iterator p = s.begin(); p != s.end(); ++p){
213 h = (h << 4) + (*p & 0xff);
224 pmt_is_symbol(const pmt_t& obj)
226 return obj->is_symbol();
230 pmt_string_to_symbol(const std::string &name)
232 unsigned hash = hash_string(name) % SYMBOL_HASH_TABLE_SIZE;
234 // Does a symbol with this name already exist?
235 for (pmt_t sym = s_symbol_hash_table[hash]; sym; sym = _symbol(sym)->next()){
236 if (name == _symbol(sym)->name())
237 return sym; // Yes. Return it
240 // Nope. Make a new one.
241 pmt_t sym = pmt_t(new pmt_symbol(name));
242 _symbol(sym)->set_next(s_symbol_hash_table[hash]);
243 s_symbol_hash_table[hash] = sym;
249 pmt_intern(const std::string &name)
251 return pmt_string_to_symbol(name);
255 pmt_symbol_to_string(const pmt_t& sym)
257 if (!sym->is_symbol())
258 throw pmt_wrong_type("pmt_symbol_to_string", sym);
260 return _symbol(sym)->name();
265 ////////////////////////////////////////////////////////////////////////////
267 ////////////////////////////////////////////////////////////////////////////
270 pmt_is_number(pmt_t x)
272 return x->is_number();
275 ////////////////////////////////////////////////////////////////////////////
277 ////////////////////////////////////////////////////////////////////////////
279 pmt_integer::pmt_integer(long value) : d_value(value) {}
282 pmt_is_integer(pmt_t x)
284 return x->is_integer();
289 pmt_from_long(long x)
291 return pmt_t(new pmt_integer(x));
297 pmt_integer* i = dynamic_cast<pmt_integer*>(x.get());
301 throw pmt_wrong_type("pmt_to_long", x);
304 ////////////////////////////////////////////////////////////////////////////
306 ////////////////////////////////////////////////////////////////////////////
308 pmt_real::pmt_real(double value) : d_value(value) {}
317 pmt_from_double(double x)
319 return pmt_t(new pmt_real(x));
323 pmt_to_double(pmt_t x)
326 return _real(x)->value();
328 return _integer(x)->value();
330 throw pmt_wrong_type("pmt_to_double", x);
333 ////////////////////////////////////////////////////////////////////////////
335 ////////////////////////////////////////////////////////////////////////////
337 pmt_complex::pmt_complex(std::complex<double> value) : d_value(value) {}
340 pmt_is_complex(pmt_t x)
342 return x->is_complex();
346 pmt_make_rectangular(double re, double im)
348 return pmt_t(new pmt_complex(std::complex<double>(re, im)));
352 pmt_to_complex(pmt_t x)
355 return _complex(x)->value();
357 return _real(x)->value();
359 return _integer(x)->value();
361 throw pmt_wrong_type("pmt_to_complex", x);
364 ////////////////////////////////////////////////////////////////////////////
366 ////////////////////////////////////////////////////////////////////////////
368 pmt_null::pmt_null() {}
369 pmt_pair::pmt_pair(const pmt_t& car, const pmt_t& cdr) : d_car(car), d_cdr(cdr) {}
372 pmt_is_null(const pmt_t& x)
378 pmt_is_pair(const pmt_t& obj)
380 return obj->is_pair();
384 pmt_cons(const pmt_t& x, const pmt_t& y)
386 return pmt_t(new pmt_pair(x, y));
390 pmt_car(const pmt_t& pair)
392 pmt_pair* p = dynamic_cast<pmt_pair*>(pair.get());
396 throw pmt_wrong_type("pmt_car", pair);
400 pmt_cdr(const pmt_t& pair)
402 pmt_pair* p = dynamic_cast<pmt_pair*>(pair.get());
406 throw pmt_wrong_type("pmt_cdr", pair);
410 pmt_set_car(pmt_t pair, pmt_t obj)
413 _pair(pair)->set_car(obj);
415 throw pmt_wrong_type("pmt_set_car", pair);
419 pmt_set_cdr(pmt_t pair, pmt_t obj)
422 _pair(pair)->set_cdr(obj);
424 throw pmt_wrong_type("pmt_set_cdr", pair);
427 ////////////////////////////////////////////////////////////////////////////
429 ////////////////////////////////////////////////////////////////////////////
431 pmt_vector::pmt_vector(size_t len, pmt_t fill)
434 for (size_t i = 0; i < len; i++)
439 pmt_vector::ref(size_t k) const
442 throw pmt_out_of_range("pmt_vector_ref", pmt_from_long(k));
447 pmt_vector::set(size_t k, pmt_t obj)
450 throw pmt_out_of_range("pmt_vector_set", pmt_from_long(k));
455 pmt_vector::fill(pmt_t obj)
457 for (size_t i = 0; i < length(); i++)
462 pmt_is_vector(pmt_t obj)
464 return obj->is_vector();
468 pmt_make_vector(size_t k, pmt_t fill)
470 return pmt_t(new pmt_vector(k, fill));
474 pmt_vector_ref(pmt_t vector, size_t k)
476 if (!vector->is_vector())
477 throw pmt_wrong_type("pmt_vector_ref", vector);
478 return _vector(vector)->ref(k);
482 pmt_vector_set(pmt_t vector, size_t k, pmt_t obj)
484 if (!vector->is_vector())
485 throw pmt_wrong_type("pmt_vector_set", vector);
486 _vector(vector)->set(k, obj);
490 pmt_vector_fill(pmt_t vector, pmt_t obj)
492 if (!vector->is_vector())
493 throw pmt_wrong_type("pmt_vector_set", vector);
494 _vector(vector)->fill(obj);
497 ////////////////////////////////////////////////////////////////////////////
498 // Uniform Numeric Vectors
499 ////////////////////////////////////////////////////////////////////////////
502 pmt_is_uniform_vector(pmt_t x)
504 return x->is_uniform_vector();
508 pmt_uniform_vector_elements(pmt_t vector, size_t &len)
510 if (!vector->is_uniform_vector())
511 throw pmt_wrong_type("pmt_uniform_vector_elements", vector);
512 return _uniform_vector(vector)->uniform_elements(len);
516 pmt_uniform_vector_writable_elements(pmt_t vector, size_t &len)
518 if (!vector->is_uniform_vector())
519 throw pmt_wrong_type("pmt_uniform_vector_writable_elements", vector);
520 return _uniform_vector(vector)->uniform_writable_elements(len);
523 ////////////////////////////////////////////////////////////////////////////
525 ////////////////////////////////////////////////////////////////////////////
533 pmt_dict::set(pmt_t key, pmt_t value)
535 pmt_t p = pmt_assv(key, d_alist); // look for (key . value) pair
536 if (pmt_is_pair(p)){ // found existing pair...
537 pmt_set_cdr(p, value); // overrwrite cdr with new value
539 else { // not in the dict
540 d_alist = pmt_cons(pmt_cons(key, value), d_alist); // add new (key . value) pair
545 pmt_dict::ref(pmt_t key, pmt_t not_found) const
547 pmt_t p = pmt_assv(key, d_alist); // look for (key . value) pair
555 pmt_dict::has_key(pmt_t key) const
557 return pmt_is_pair(pmt_assv(key, d_alist));
561 pmt_dict::items() const
567 pmt_dict::keys() const
569 return pmt_map(pmt_car, d_alist);
573 pmt_dict::values() const
575 return pmt_map(pmt_cdr, d_alist);
579 pmt_is_dict(pmt_t obj)
581 return obj->is_dict();
587 return pmt_t(new pmt_dict());
591 pmt_dict_set(pmt_t dict, pmt_t key, pmt_t value)
593 pmt_dict* d = _dict(dict);
595 throw pmt_wrong_type("pmt_dict_set", dict);
601 pmt_dict_has_key(pmt_t dict, pmt_t key)
603 pmt_dict* d = _dict(dict);
605 throw pmt_wrong_type("pmt_dict_has_key", dict);
607 return d->has_key(key);
611 pmt_dict_ref(pmt_t dict, pmt_t key, pmt_t not_found)
613 pmt_dict* d = _dict(dict);
615 throw pmt_wrong_type("pmt_dict_ref", dict);
617 return d->ref(key, not_found);
621 pmt_dict_items(pmt_t dict)
623 if (!dict->is_dict())
624 throw pmt_wrong_type("pmt_dict_items", dict);
626 return _dict(dict)->items();
630 pmt_dict_keys(pmt_t dict)
632 if (!dict->is_dict())
633 throw pmt_wrong_type("pmt_dict_keys", dict);
635 return _dict(dict)->keys();
639 pmt_dict_values(pmt_t dict)
641 if (!dict->is_dict())
642 throw pmt_wrong_type("pmt_dict_values", dict);
644 return _dict(dict)->values();
647 ////////////////////////////////////////////////////////////////////////////
649 ////////////////////////////////////////////////////////////////////////////
651 pmt_any::pmt_any(const boost::any &any) : d_any(any) {}
654 pmt_is_any(pmt_t obj)
656 return obj->is_any();
660 pmt_make_any(const boost::any &any)
662 return pmt_t(new pmt_any(any));
666 pmt_any_ref(pmt_t obj)
669 throw pmt_wrong_type("pmt_any_ref", obj);
670 return _any(obj)->ref();
674 pmt_any_set(pmt_t obj, const boost::any &any)
677 throw pmt_wrong_type("pmt_any_set", obj);
681 ////////////////////////////////////////////////////////////////////////////
683 ////////////////////////////////////////////////////////////////////////////
686 pmt_eq(const pmt_t& x, const pmt_t& y)
692 pmt_eqv(const pmt_t& x, const pmt_t& y)
697 if (x->is_integer() && y->is_integer())
698 return _integer(x)->value() == _integer(y)->value();
700 if (x->is_real() && y->is_real())
701 return _real(x)->value() == _real(y)->value();
703 if (x->is_complex() && y->is_complex())
704 return _complex(x)->value() == _complex(y)->value();
710 pmt_equal(const pmt_t& x, const pmt_t& y)
715 if (x->is_pair() && y->is_pair())
716 return pmt_equal(pmt_car(x), pmt_car(y)) && pmt_equal(pmt_cdr(x), pmt_cdr(y));
718 if (x->is_vector() && y->is_vector()){
719 pmt_vector *xv = _vector(x);
720 pmt_vector *yv = _vector(y);
721 if (xv->length() != yv->length())
724 for (unsigned i = 0; i < xv->length(); i++)
725 if (!pmt_equal(xv->_ref(i), yv->_ref(i)))
731 if (x->is_uniform_vector() && y->is_uniform_vector()){
732 pmt_uniform_vector *xv = _uniform_vector(x);
733 pmt_uniform_vector *yv = _uniform_vector(y);
734 if (xv->length() != yv->length())
738 if (memcmp(xv->uniform_elements(len_x),
739 yv->uniform_elements(len_y),
746 // FIXME add other cases here...
752 pmt_length(const pmt_t& x)
755 return _vector(x)->length();
757 if (x->is_uniform_vector())
758 return _uniform_vector(x)->length();
760 if (x->is_null()) return 0;
764 pmt_t it = pmt_cdr(x);
765 while (pmt_is_pair(it)){
773 throw pmt_wrong_type("pmt_length", x);
776 // FIXME dictionary length (number of entries)
778 throw pmt_wrong_type("pmt_length", x);
782 pmt_assq(pmt_t obj, pmt_t alist)
784 while (pmt_is_pair(alist)){
785 pmt_t p = pmt_car(alist);
786 if (!pmt_is_pair(p)) // malformed alist
789 if (pmt_eq(obj, pmt_car(p)))
792 alist = pmt_cdr(alist);
798 pmt_assv(pmt_t obj, pmt_t alist)
800 while (pmt_is_pair(alist)){
801 pmt_t p = pmt_car(alist);
802 if (!pmt_is_pair(p)) // malformed alist
805 if (pmt_eqv(obj, pmt_car(p)))
808 alist = pmt_cdr(alist);
814 pmt_assoc(pmt_t obj, pmt_t alist)
816 while (pmt_is_pair(alist)){
817 pmt_t p = pmt_car(alist);
818 if (!pmt_is_pair(p)) // malformed alist
821 if (pmt_equal(obj, pmt_car(p)))
824 alist = pmt_cdr(alist);
830 pmt_map(pmt_t proc(const pmt_t&), pmt_t list)
834 while(pmt_is_pair(list)){
835 r = pmt_cons(proc(pmt_car(list)), r);
836 list = pmt_cdr(list);
839 return pmt_reverse_x(r);
843 pmt_reverse(pmt_t listx)
848 while(pmt_is_pair(list)){
849 r = pmt_cons(pmt_car(list), r);
850 list = pmt_cdr(list);
852 if (pmt_is_null(list))
855 throw pmt_wrong_type("pmt_reverse", listx);
859 pmt_reverse_x(pmt_t list)
861 // FIXME do it destructively
862 return pmt_reverse(list);
866 pmt_nth(size_t n, pmt_t list)
868 pmt_t t = pmt_nthcdr(n, list);
876 pmt_nthcdr(size_t n, pmt_t list)
878 if (!(pmt_is_pair(list) || pmt_is_null(list)))
879 throw pmt_wrong_type("pmt_nthcdr", list);
882 if (pmt_is_pair(list)){
883 list = pmt_cdr(list);
887 if (pmt_is_null(list))
890 throw pmt_wrong_type("pmt_nthcdr: not a LIST", list);
896 pmt_memq(pmt_t obj, pmt_t list)
898 while (pmt_is_pair(list)){
899 if (pmt_eq(obj, pmt_car(list)))
901 list = pmt_cdr(list);
907 pmt_memv(pmt_t obj, pmt_t list)
909 while (pmt_is_pair(list)){
910 if (pmt_eqv(obj, pmt_car(list)))
912 list = pmt_cdr(list);
918 pmt_member(pmt_t obj, pmt_t list)
920 while (pmt_is_pair(list)){
921 if (pmt_equal(obj, pmt_car(list)))
923 list = pmt_cdr(list);
929 pmt_subsetp(pmt_t list1, pmt_t list2)
931 while (pmt_is_pair(list1)){
932 pmt_t p = pmt_car(list1);
933 if (pmt_is_false(pmt_memv(p, list2)))
935 list1 = pmt_cdr(list1);
941 pmt_list1(const pmt_t& x1)
943 return pmt_cons(x1, PMT_NIL);
947 pmt_list2(const pmt_t& x1, const pmt_t& x2)
949 return pmt_cons(x1, pmt_cons(x2, PMT_NIL));
953 pmt_list3(const pmt_t& x1, const pmt_t& x2, const pmt_t& x3)
955 return pmt_cons(x1, pmt_cons(x2, pmt_cons(x3, PMT_NIL)));
959 pmt_list4(const pmt_t& x1, const pmt_t& x2, const pmt_t& x3, const pmt_t& x4)
961 return pmt_cons(x1, pmt_cons(x2, pmt_cons(x3, pmt_cons(x4, PMT_NIL))));
965 pmt_list5(const pmt_t& x1, const pmt_t& x2, const pmt_t& x3, const pmt_t& x4, const pmt_t& x5)
967 return pmt_cons(x1, pmt_cons(x2, pmt_cons(x3, pmt_cons(x4, pmt_cons(x5, PMT_NIL)))));
971 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)
973 return pmt_cons(x1, pmt_cons(x2, pmt_cons(x3, pmt_cons(x4, pmt_cons(x5, pmt_cons(x6, PMT_NIL))))));
977 pmt_list_add(pmt_t list, const pmt_t& item)
979 return pmt_reverse(pmt_cons(item, pmt_reverse(list)));
985 return (pmt_car(pmt_car(pair)));
991 return pmt_car(pmt_cdr(pair));
997 return pmt_cdr(pmt_car(pair));
1001 pmt_cddr(pmt_t pair)
1003 return pmt_cdr(pmt_cdr(pair));
1007 pmt_caddr(pmt_t pair)
1009 return pmt_car(pmt_cdr(pmt_cdr(pair)));
1013 pmt_cadddr(pmt_t pair)
1015 return pmt_car(pmt_cdr(pmt_cdr(pmt_cdr(pair))));
1019 pmt_is_eof_object(pmt_t obj)
1021 return pmt_eq(obj, PMT_EOF);
1027 printf("sizeof(pmt_t) = %3zd\n", sizeof(pmt_t));
1028 printf("sizeof(pmt_base) = %3zd\n", sizeof(pmt_base));
1029 printf("sizeof(pmt_bool) = %3zd\n", sizeof(pmt_bool));
1030 printf("sizeof(pmt_symbol) = %3zd\n", sizeof(pmt_symbol));
1031 printf("sizeof(pmt_integer) = %3zd\n", sizeof(pmt_integer));
1032 printf("sizeof(pmt_real) = %3zd\n", sizeof(pmt_real));
1033 printf("sizeof(pmt_complex) = %3zd\n", sizeof(pmt_complex));
1034 printf("sizeof(pmt_null) = %3zd\n", sizeof(pmt_null));
1035 printf("sizeof(pmt_pair) = %3zd\n", sizeof(pmt_pair));
1036 printf("sizeof(pmt_vector) = %3zd\n", sizeof(pmt_vector));
1037 printf("sizeof(pmt_dict) = %3zd\n", sizeof(pmt_dict));
1038 printf("sizeof(pmt_uniform_vector) = %3zd\n", sizeof(pmt_uniform_vector));
1041 } /* namespace gruel */