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 2, 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.
32 // nop -- out of line virtual destructor
35 ////////////////////////////////////////////////////////////////////////////
37 ////////////////////////////////////////////////////////////////////////////
39 pmt_exception::pmt_exception(const std::string &msg, pmt_t obj)
40 : logic_error(msg + ": " + pmt_write_string(obj))
44 pmt_wrong_type::pmt_wrong_type(const std::string &msg, pmt_t obj)
45 : pmt_exception(msg + ": wrong_type ", obj)
49 pmt_out_of_range::pmt_out_of_range(const std::string &msg, pmt_t obj)
50 : pmt_exception(msg + ": out of range ", obj)
54 pmt_notimplemented::pmt_notimplemented(const std::string &msg, pmt_t obj)
55 : pmt_exception(msg + ": notimplemented ", obj)
59 ////////////////////////////////////////////////////////////////////////////
61 ////////////////////////////////////////////////////////////////////////////
66 return dynamic_cast<pmt_symbol*>(x.get());
72 return dynamic_cast<pmt_integer*>(x.get());
78 return dynamic_cast<pmt_real*>(x.get());
84 return dynamic_cast<pmt_complex*>(x.get());
90 return dynamic_cast<pmt_pair*>(x.get());
96 return dynamic_cast<pmt_vector*>(x.get());
99 static pmt_uniform_vector *
100 _uniform_vector(pmt_t x)
102 return dynamic_cast<pmt_uniform_vector*>(x.get());
108 return dynamic_cast<pmt_dict*>(x.get());
111 ////////////////////////////////////////////////////////////////////////////
113 ////////////////////////////////////////////////////////////////////////////
115 const pmt_t PMT_BOOL_T = pmt_t(new pmt_bool()); // singleton
116 const pmt_t PMT_BOOL_F = pmt_t(new pmt_bool()); // singleton
118 pmt_bool::pmt_bool(){}
121 pmt_is_true(pmt_t obj)
123 return obj != PMT_BOOL_F;
127 pmt_is_false(pmt_t obj)
129 return obj == PMT_BOOL_F;
133 pmt_is_bool(pmt_t obj)
135 return obj->is_bool();
139 pmt_from_bool(bool val)
141 return val ? PMT_BOOL_T : PMT_BOOL_F;
145 pmt_to_bool(pmt_t val)
147 if (val == PMT_BOOL_T)
149 if (val == PMT_BOOL_F)
151 throw pmt_wrong_type("pmt_to_bool", val);
154 ////////////////////////////////////////////////////////////////////////////
156 ////////////////////////////////////////////////////////////////////////////
158 static const unsigned int SYMBOL_HASH_TABLE_SIZE = 701;
159 static std::vector<pmt_t> s_symbol_hash_table(SYMBOL_HASH_TABLE_SIZE);
161 pmt_symbol::pmt_symbol(const std::string &name) : d_name(name){}
165 hash_string(const std::string &s)
170 for (std::string::const_iterator p = s.begin(); p != s.end(); p++){
171 h = (h << 4) + (*p & 0xff);
182 pmt_is_symbol(pmt_t obj)
184 return obj->is_symbol();
188 pmt_string_to_symbol(const std::string &name)
190 unsigned hash = hash_string(name) % SYMBOL_HASH_TABLE_SIZE;
192 // Does a symbol with this name already exist?
193 for (pmt_t sym = s_symbol_hash_table[hash]; sym; sym = _symbol(sym)->next()){
194 if (name == _symbol(sym)->name())
195 return sym; // Yes. Return it
198 // Nope. Make a new one.
199 pmt_t sym = pmt_t(new pmt_symbol(name));
200 _symbol(sym)->set_next(s_symbol_hash_table[hash]);
201 s_symbol_hash_table[hash] = sym;
207 pmt_intern(const std::string &name)
209 return pmt_string_to_symbol(name);
213 pmt_symbol_to_string(pmt_t sym)
215 if (!sym->is_symbol())
216 throw pmt_wrong_type("pmt_symbol_to_string", sym);
218 return _symbol(sym)->name();
223 ////////////////////////////////////////////////////////////////////////////
225 ////////////////////////////////////////////////////////////////////////////
228 pmt_is_number(pmt_t x)
230 return x->is_number();
233 ////////////////////////////////////////////////////////////////////////////
235 ////////////////////////////////////////////////////////////////////////////
237 pmt_integer::pmt_integer(long value) : d_value(value) {}
240 pmt_is_integer(pmt_t x)
242 return x->is_integer();
247 pmt_from_long(long x)
249 return pmt_t(new pmt_integer(x));
256 return _integer(x)->value();
258 throw pmt_wrong_type("pmt_to_long", x);
261 ////////////////////////////////////////////////////////////////////////////
263 ////////////////////////////////////////////////////////////////////////////
265 pmt_real::pmt_real(double value) : d_value(value) {}
274 pmt_from_double(double x)
276 return pmt_t(new pmt_real(x));
280 pmt_to_double(pmt_t x)
283 return _real(x)->value();
285 return _integer(x)->value();
287 throw pmt_wrong_type("pmt_to_double", x);
290 ////////////////////////////////////////////////////////////////////////////
292 ////////////////////////////////////////////////////////////////////////////
294 pmt_complex::pmt_complex(std::complex<double> value) : d_value(value) {}
297 pmt_is_complex(pmt_t x)
299 return x->is_complex();
303 pmt_make_rectangular(double re, double im)
305 return pmt_t(new pmt_complex(std::complex<double>(re, im)));
309 pmt_to_complex(pmt_t x)
312 return _complex(x)->value();
314 return _real(x)->value();
316 return _integer(x)->value();
318 throw pmt_wrong_type("pmt_to_complex", x);
321 ////////////////////////////////////////////////////////////////////////////
323 ////////////////////////////////////////////////////////////////////////////
325 const pmt_t PMT_NIL = pmt_t(new pmt_null()); // singleton
327 pmt_null::pmt_null() {}
328 pmt_pair::pmt_pair(pmt_t car, pmt_t cdr) : d_car(car), d_cdr(cdr) {}
337 pmt_is_pair(pmt_t obj)
339 return obj->is_pair();
343 pmt_cons(pmt_t x, pmt_t y)
345 return pmt_t(new pmt_pair(x, y));
352 return _pair(pair)->car();
354 throw pmt_wrong_type("pmt_car", pair);
361 return _pair(pair)->cdr();
363 throw pmt_wrong_type("pmt_cdr", pair);
367 pmt_set_car(pmt_t pair, pmt_t obj)
370 _pair(pair)->set_car(obj);
372 throw pmt_wrong_type("pmt_set_car", pair);
376 pmt_set_cdr(pmt_t pair, pmt_t obj)
379 _pair(pair)->set_cdr(obj);
381 throw pmt_wrong_type("pmt_set_cdr", pair);
384 ////////////////////////////////////////////////////////////////////////////
386 ////////////////////////////////////////////////////////////////////////////
388 pmt_vector::pmt_vector(size_t len, pmt_t fill)
391 for (size_t i = 0; i < len; i++)
396 pmt_vector::ref(size_t k) const
399 throw pmt_out_of_range("pmt_vector_ref", pmt_from_long(k));
404 pmt_vector::set(size_t k, pmt_t obj)
407 throw pmt_out_of_range("pmt_vector_set", pmt_from_long(k));
412 pmt_vector::fill(pmt_t obj)
414 for (size_t i = 0; i < length(); i++)
419 pmt_is_vector(pmt_t obj)
421 return obj->is_vector();
425 pmt_make_vector(size_t k, pmt_t fill)
427 return pmt_t(new pmt_vector(k, fill));
431 pmt_vector_ref(pmt_t vector, size_t k)
433 if (!vector->is_vector())
434 throw pmt_wrong_type("pmt_vector_ref", vector);
435 return _vector(vector)->ref(k);
439 pmt_vector_set(pmt_t vector, size_t k, pmt_t obj)
441 if (!vector->is_vector())
442 throw pmt_wrong_type("pmt_vector_set", vector);
443 _vector(vector)->set(k, obj);
447 pmt_vector_fill(pmt_t vector, pmt_t obj)
449 if (!vector->is_vector())
450 throw pmt_wrong_type("pmt_vector_set", vector);
451 _vector(vector)->fill(obj);
454 ////////////////////////////////////////////////////////////////////////////
455 // Uniform Numeric Vectors
456 ////////////////////////////////////////////////////////////////////////////
459 pmt_is_uniform_vector(pmt_t x)
461 return x->is_uniform_vector();
465 pmt_uniform_vector_elements(pmt_t vector, size_t &len)
467 if (!vector->is_uniform_vector())
468 throw pmt_wrong_type("pmt_uniform_vector_elements", vector);
469 return _uniform_vector(vector)->uniform_elements(len);
473 pmt_uniform_vector_writeable_elements(pmt_t vector, size_t &len)
475 if (!vector->is_uniform_vector())
476 throw pmt_wrong_type("pmt_uniform_vector_writeable_elements", vector);
477 return _uniform_vector(vector)->uniform_writeable_elements(len);
480 ////////////////////////////////////////////////////////////////////////////
482 ////////////////////////////////////////////////////////////////////////////
490 pmt_dict::set(pmt_t key, pmt_t value)
492 pmt_t p = pmt_assv(key, d_alist); // look for (key . value) pair
493 if (pmt_is_pair(p)){ // found existing pair...
494 pmt_set_cdr(p, value); // overrwrite cdr with new value
496 else { // not in the dict
497 d_alist = pmt_cons(pmt_cons(key, value), d_alist); // add new (key . value) pair
502 pmt_dict::ref(pmt_t key, pmt_t not_found) const
504 pmt_t p = pmt_assv(key, d_alist); // look for (key . value) pair
512 pmt_dict::has_key(pmt_t key) const
514 return pmt_is_pair(pmt_assv(key, d_alist));
518 pmt_dict::items() const
524 pmt_dict::keys() const
526 return pmt_map(pmt_car, d_alist);
530 pmt_dict::values() const
532 return pmt_map(pmt_cdr, d_alist);
536 pmt_is_dict(pmt_t obj)
538 return obj->is_dict();
544 return pmt_t(new pmt_dict());
548 pmt_dict_set(pmt_t dict, pmt_t key, pmt_t value)
550 if (!dict->is_dict())
551 throw pmt_wrong_type("pmt_dict_set", dict);
553 _dict(dict)->set(key, value);
557 pmt_dict_has_key(pmt_t dict, pmt_t key)
559 if (!dict->is_dict())
560 throw pmt_wrong_type("pmt_dict_has_key", dict);
562 return _dict(dict)->has_key(key);
566 pmt_dict_ref(pmt_t dict, pmt_t key, pmt_t not_found)
568 if (!dict->is_dict())
569 throw pmt_wrong_type("pmt_dict_ref", dict);
571 return _dict(dict)->ref(key, not_found);
575 pmt_dict_items(pmt_t dict)
577 if (!dict->is_dict())
578 throw pmt_wrong_type("pmt_dict_items", dict);
580 return _dict(dict)->items();
584 pmt_dict_keys(pmt_t dict)
586 if (!dict->is_dict())
587 throw pmt_wrong_type("pmt_dict_keys", dict);
589 return _dict(dict)->keys();
593 pmt_dict_values(pmt_t dict)
595 if (!dict->is_dict())
596 throw pmt_wrong_type("pmt_dict_values", dict);
598 return _dict(dict)->values();
601 ////////////////////////////////////////////////////////////////////////////
603 ////////////////////////////////////////////////////////////////////////////
606 pmt_eq(pmt_t x, pmt_t y)
612 pmt_eqv(pmt_t x, pmt_t y)
617 if (x->is_integer() && y->is_integer())
618 return _integer(x)->value() == _integer(y)->value();
620 if (x->is_real() && y->is_real())
621 return _real(x)->value() == _real(y)->value();
623 if (x->is_complex() && y->is_complex())
624 return _complex(x)->value() == _complex(y)->value();
630 pmt_equal(pmt_t x, pmt_t y)
635 if (x->is_pair() && y->is_pair())
636 return pmt_equal(pmt_car(x), pmt_car(y)) && pmt_equal(pmt_cdr(x), pmt_cdr(y));
638 if (x->is_vector() && y->is_vector()){
639 pmt_vector *xv = _vector(x);
640 pmt_vector *yv = _vector(y);
641 if (xv->length() != yv->length())
644 for (unsigned i = 0; i < xv->length(); i++)
645 if (!pmt_equal(xv->_ref(i), yv->_ref(i)))
651 if (x->is_uniform_vector() && y->is_uniform_vector()){
652 pmt_uniform_vector *xv = _uniform_vector(x);
653 pmt_uniform_vector *yv = _uniform_vector(y);
654 if (xv->length() != yv->length())
658 if (memcmp(xv->uniform_elements(len_x),
659 yv->uniform_elements(len_y),
666 // FIXME add other cases here...
675 return _vector(x)->length();
677 if (x->is_uniform_vector())
678 return _uniform_vector(x)->length();
681 // FIXME dictionary length (number of entries)
683 throw pmt_wrong_type("pmt_length", x);
687 pmt_assq(pmt_t obj, pmt_t alist)
689 while (pmt_is_pair(alist)){
690 pmt_t p = pmt_car(alist);
691 if (!pmt_is_pair(p)) // malformed alist
694 if (pmt_eq(obj, pmt_car(p)))
697 alist = pmt_cdr(alist);
703 pmt_assv(pmt_t obj, pmt_t alist)
705 while (pmt_is_pair(alist)){
706 pmt_t p = pmt_car(alist);
707 if (!pmt_is_pair(p)) // malformed alist
710 if (pmt_eqv(obj, pmt_car(p)))
713 alist = pmt_cdr(alist);
719 pmt_assoc(pmt_t obj, pmt_t alist)
721 while (pmt_is_pair(alist)){
722 pmt_t p = pmt_car(alist);
723 if (!pmt_is_pair(p)) // malformed alist
726 if (pmt_equal(obj, pmt_car(p)))
729 alist = pmt_cdr(alist);
735 pmt_map(pmt_t proc(pmt_t), pmt_t list)
739 while(pmt_is_pair(list)){
740 r = pmt_cons(proc(pmt_car(list)), r);
741 list = pmt_cdr(list);
744 return pmt_reverse_x(r);
748 pmt_reverse(pmt_t listx)
753 while(pmt_is_pair(list)){
754 r = pmt_cons(pmt_car(list), r);
755 list = pmt_cdr(list);
757 if (pmt_is_null(list))
760 throw pmt_wrong_type("pmt_reverse", listx);
764 pmt_reverse_x(pmt_t list)
766 // FIXME do it destructively
767 return pmt_reverse(list);
771 pmt_nth(size_t n, pmt_t list)
773 pmt_t t = pmt_nthcdr(n, list);
781 pmt_nthcdr(size_t n, pmt_t list)
783 if (!(pmt_is_null(list) || pmt_is_pair(list)))
784 throw pmt_wrong_type("pmt_nthcdr", list);
787 if (pmt_is_pair(list)){
788 list = pmt_cdr(list);
792 if (pmt_is_null(list))
795 throw pmt_wrong_type("pmt_nthcdr: not a LIST", list);
801 pmt_memq(pmt_t obj, pmt_t list)
803 while (pmt_is_pair(list)){
804 if (pmt_eq(obj, pmt_car(list)))
806 list = pmt_cdr(list);
812 pmt_memv(pmt_t obj, pmt_t list)
814 while (pmt_is_pair(list)){
815 if (pmt_eqv(obj, pmt_car(list)))
817 list = pmt_cdr(list);
823 pmt_member(pmt_t obj, pmt_t list)
825 while (pmt_is_pair(list)){
826 if (pmt_equal(obj, pmt_car(list)))
828 list = pmt_cdr(list);
834 pmt_subsetp(pmt_t list1, pmt_t list2)
836 while (pmt_is_pair(list1)){
837 pmt_t p = pmt_car(list1);
838 if (pmt_is_false(pmt_memv(p, list2)))
840 list1 = pmt_cdr(list1);