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 char *msg, pmt_t obj)
40 : d_msg(msg), d_obj(obj)
44 pmt_wrong_type::pmt_wrong_type(const char *msg, pmt_t obj)
45 : pmt_exception(msg, obj)
49 pmt_out_of_range::pmt_out_of_range(const char *msg, pmt_t obj)
50 : pmt_exception(msg, obj)
54 ////////////////////////////////////////////////////////////////////////////
56 ////////////////////////////////////////////////////////////////////////////
61 return dynamic_cast<pmt_symbol*>(x.get());
67 return dynamic_cast<pmt_integer*>(x.get());
73 return dynamic_cast<pmt_real*>(x.get());
79 return dynamic_cast<pmt_complex*>(x.get());
85 return dynamic_cast<pmt_pair*>(x.get());
91 return dynamic_cast<pmt_vector*>(x.get());
94 static pmt_uniform_vector *
95 _uniform_vector(pmt_t x)
97 return dynamic_cast<pmt_uniform_vector*>(x.get());
103 return dynamic_cast<pmt_dict*>(x.get());
106 ////////////////////////////////////////////////////////////////////////////
108 ////////////////////////////////////////////////////////////////////////////
110 const pmt_t PMT_BOOL_T = pmt_t(new pmt_bool()); // singleton
111 const pmt_t PMT_BOOL_F = pmt_t(new pmt_bool()); // singleton
113 pmt_bool::pmt_bool(){}
116 pmt_is_true(pmt_t obj)
118 return obj != PMT_BOOL_F;
122 pmt_is_false(pmt_t obj)
124 return obj == PMT_BOOL_F;
128 pmt_is_bool(pmt_t obj)
130 return obj->is_bool();
134 pmt_from_bool(bool val)
136 return val ? PMT_BOOL_T : PMT_BOOL_F;
140 pmt_to_bool(pmt_t val)
142 if (val == PMT_BOOL_T)
144 if (val == PMT_BOOL_F)
146 throw pmt_wrong_type("pmt_to_bool", val);
149 ////////////////////////////////////////////////////////////////////////////
151 ////////////////////////////////////////////////////////////////////////////
153 static const unsigned int SYMBOL_HASH_TABLE_SIZE = 701;
154 static std::vector<pmt_t> s_symbol_hash_table(SYMBOL_HASH_TABLE_SIZE);
156 pmt_symbol::pmt_symbol(const std::string &name) : d_name(name){}
160 hash_string(const std::string &s)
165 for (std::string::const_iterator p = s.begin(); p != s.end(); p++){
166 h = (h << 4) + (*p & 0xff);
177 pmt_is_symbol(pmt_t obj)
179 return obj->is_symbol();
183 pmt_string_to_symbol(const std::string &name)
185 unsigned hash = hash_string(name) % SYMBOL_HASH_TABLE_SIZE;
187 // Does a symbol with this name already exist?
188 for (pmt_t sym = s_symbol_hash_table[hash]; sym; sym = _symbol(sym)->next()){
189 if (name == _symbol(sym)->name())
190 return sym; // Yes. Return it
193 // Nope. Make a new one.
194 pmt_t sym = pmt_t(new pmt_symbol(name));
195 _symbol(sym)->set_next(s_symbol_hash_table[hash]);
196 s_symbol_hash_table[hash] = sym;
201 pmt_symbol_to_string(pmt_t sym)
203 if (!sym->is_symbol())
204 throw pmt_wrong_type("pmt_symbol_to_string", sym);
206 return _symbol(sym)->name();
209 ////////////////////////////////////////////////////////////////////////////
211 ////////////////////////////////////////////////////////////////////////////
214 pmt_is_number(pmt_t x)
216 return x->is_number();
219 ////////////////////////////////////////////////////////////////////////////
221 ////////////////////////////////////////////////////////////////////////////
223 pmt_integer::pmt_integer(long value) : d_value(value) {}
226 pmt_is_integer(pmt_t x)
228 return x->is_integer();
233 pmt_from_long(long x)
235 return pmt_t(new pmt_integer(x));
242 return _integer(x)->value();
244 throw pmt_wrong_type("pmt_to_long", x);
247 ////////////////////////////////////////////////////////////////////////////
249 ////////////////////////////////////////////////////////////////////////////
251 pmt_real::pmt_real(double value) : d_value(value) {}
260 pmt_from_double(double x)
262 return pmt_t(new pmt_real(x));
266 pmt_to_double(pmt_t x)
269 return _real(x)->value();
271 return _integer(x)->value();
273 throw pmt_wrong_type("pmt_to_double", x);
276 ////////////////////////////////////////////////////////////////////////////
278 ////////////////////////////////////////////////////////////////////////////
280 pmt_complex::pmt_complex(std::complex<double> value) : d_value(value) {}
283 pmt_is_complex(pmt_t x)
285 return x->is_complex();
289 pmt_make_rectangular(double re, double im)
291 return pmt_t(new pmt_complex(std::complex<double>(re, im)));
295 pmt_to_complex(pmt_t x)
298 return _complex(x)->value();
300 return _real(x)->value();
302 return _integer(x)->value();
304 throw pmt_wrong_type("pmt_to_complex", x);
307 ////////////////////////////////////////////////////////////////////////////
309 ////////////////////////////////////////////////////////////////////////////
311 const pmt_t PMT_NIL = pmt_t(new pmt_null()); // singleton
313 pmt_null::pmt_null() {}
314 pmt_pair::pmt_pair(pmt_t car, pmt_t cdr) : d_car(car), d_cdr(cdr) {}
323 pmt_is_pair(pmt_t obj)
325 return obj->is_pair();
329 pmt_cons(pmt_t x, pmt_t y)
331 return pmt_t(new pmt_pair(x, y));
338 return _pair(pair)->car();
340 throw pmt_wrong_type("pmt_car", pair);
347 return _pair(pair)->cdr();
349 throw pmt_wrong_type("pmt_cdr", pair);
353 pmt_set_car(pmt_t pair, pmt_t obj)
356 _pair(pair)->set_car(obj);
358 throw pmt_wrong_type("pmt_set_car", pair);
362 pmt_set_cdr(pmt_t pair, pmt_t obj)
365 _pair(pair)->set_cdr(obj);
367 throw pmt_wrong_type("pmt_set_cdr", pair);
370 ////////////////////////////////////////////////////////////////////////////
372 ////////////////////////////////////////////////////////////////////////////
374 pmt_vector::pmt_vector(size_t len, pmt_t fill)
377 for (size_t i = 0; i < len; i++)
382 pmt_vector::ref(size_t k) const
385 throw pmt_out_of_range("pmt_vector_ref", pmt_from_long(k));
390 pmt_vector::set(size_t k, pmt_t obj)
393 throw pmt_out_of_range("pmt_vector_set", pmt_from_long(k));
398 pmt_vector::fill(pmt_t obj)
400 for (size_t i = 0; i < length(); i++)
405 pmt_is_vector(pmt_t obj)
407 return obj->is_vector();
411 pmt_make_vector(size_t k, pmt_t fill)
413 return pmt_t(new pmt_vector(k, fill));
417 pmt_vector_ref(pmt_t vector, size_t k)
419 if (!vector->is_vector())
420 throw pmt_wrong_type("pmt_vector_ref", vector);
421 return _vector(vector)->ref(k);
425 pmt_vector_set(pmt_t vector, size_t k, pmt_t obj)
427 if (!vector->is_vector())
428 throw pmt_wrong_type("pmt_vector_set", vector);
429 _vector(vector)->set(k, obj);
433 pmt_vector_fill(pmt_t vector, pmt_t obj)
435 if (!vector->is_vector())
436 throw pmt_wrong_type("pmt_vector_set", vector);
437 _vector(vector)->fill(obj);
440 ////////////////////////////////////////////////////////////////////////////
441 // Uniform Numeric Vectors
442 ////////////////////////////////////////////////////////////////////////////
445 pmt_is_uniform_vector(pmt_t x)
447 return x->is_uniform_vector();
451 pmt_uniform_vector_elements(pmt_t vector, size_t &len)
453 if (!vector->is_uniform_vector())
454 throw pmt_wrong_type("pmt_uniform_vector_elements", vector);
455 return _uniform_vector(vector)->uniform_elements(len);
459 pmt_uniform_vector_writeable_elements(pmt_t vector, size_t &len)
461 if (!vector->is_uniform_vector())
462 throw pmt_wrong_type("pmt_uniform_vector_writeable_elements", vector);
463 return _uniform_vector(vector)->uniform_writeable_elements(len);
466 ////////////////////////////////////////////////////////////////////////////
468 ////////////////////////////////////////////////////////////////////////////
476 pmt_dict::set(pmt_t key, pmt_t value)
478 pmt_t p = pmt_assv(key, d_alist); // look for (key . value) pair
479 if (pmt_is_pair(p)){ // found existing pair...
480 pmt_set_cdr(p, value); // overrwrite cdr with new value
482 else { // not in the dict
483 d_alist = pmt_cons(pmt_cons(key, value), d_alist); // add new (key . value) pair
488 pmt_dict::ref(pmt_t key, pmt_t not_found) const
490 pmt_t p = pmt_assv(key, d_alist); // look for (key . value) pair
498 pmt_dict::has_key(pmt_t key) const
500 return pmt_is_pair(pmt_assv(key, d_alist));
504 pmt_dict::items() const
510 pmt_dict::keys() const
512 return pmt_map(pmt_car, d_alist);
516 pmt_dict::values() const
518 return pmt_map(pmt_cdr, d_alist);
522 pmt_is_dict(pmt_t obj)
524 return obj->is_dict();
530 return pmt_t(new pmt_dict());
534 pmt_dict_set(pmt_t dict, pmt_t key, pmt_t value)
536 if (!dict->is_dict())
537 throw pmt_wrong_type("pmt_dict_set", dict);
539 _dict(dict)->set(key, value);
543 pmt_dict_has_key(pmt_t dict, pmt_t key)
545 if (!dict->is_dict())
546 throw pmt_wrong_type("pmt_dict_has_key", dict);
548 return _dict(dict)->has_key(key);
552 pmt_dict_ref(pmt_t dict, pmt_t key, pmt_t not_found)
554 if (!dict->is_dict())
555 throw pmt_wrong_type("pmt_dict_ref", dict);
557 return _dict(dict)->ref(key, not_found);
561 pmt_dict_items(pmt_t dict)
563 if (!dict->is_dict())
564 throw pmt_wrong_type("pmt_dict_items", dict);
566 return _dict(dict)->items();
570 pmt_dict_keys(pmt_t dict)
572 if (!dict->is_dict())
573 throw pmt_wrong_type("pmt_dict_keys", dict);
575 return _dict(dict)->keys();
579 pmt_dict_values(pmt_t dict)
581 if (!dict->is_dict())
582 throw pmt_wrong_type("pmt_dict_values", dict);
584 return _dict(dict)->values();
587 ////////////////////////////////////////////////////////////////////////////
589 ////////////////////////////////////////////////////////////////////////////
592 pmt_eq(pmt_t x, pmt_t y)
598 pmt_eqv(pmt_t x, pmt_t y)
603 if (x->is_integer() && y->is_integer())
604 return _integer(x)->value() == _integer(y)->value();
606 if (x->is_real() && y->is_real())
607 return _real(x)->value() == _real(y)->value();
609 if (x->is_complex() && y->is_complex())
610 return _complex(x)->value() == _complex(y)->value();
616 pmt_equal(pmt_t x, pmt_t y)
621 if (x->is_pair() && y->is_pair())
622 return pmt_equal(pmt_car(x), pmt_car(y)) && pmt_equal(pmt_cdr(x), pmt_cdr(y));
624 if (x->is_vector() && y->is_vector()){
625 pmt_vector *xv = _vector(x);
626 pmt_vector *yv = _vector(y);
627 if (xv->length() != yv->length())
630 for (unsigned i = 0; i < xv->length(); i++)
631 if (!pmt_equal(xv->_ref(i), yv->_ref(i)))
637 if (x->is_uniform_vector() && y->is_uniform_vector()){
638 pmt_uniform_vector *xv = _uniform_vector(x);
639 pmt_uniform_vector *yv = _uniform_vector(y);
640 if (xv->length() != yv->length())
644 if (memcmp(xv->uniform_elements(len_x),
645 yv->uniform_elements(len_y),
652 // FIXME add other cases here...
661 return _vector(x)->length();
663 if (x->is_uniform_vector())
664 return _uniform_vector(x)->length();
667 // FIXME dictionary length (number of entries)
669 throw pmt_wrong_type("pmt_length", x);
673 pmt_assq(pmt_t obj, pmt_t alist)
675 while (pmt_is_pair(alist)){
676 pmt_t p = pmt_car(alist);
677 if (!pmt_is_pair(p)) // malformed alist
680 if (pmt_eq(obj, pmt_car(p)))
683 alist = pmt_cdr(alist);
689 pmt_assv(pmt_t obj, pmt_t alist)
691 while (pmt_is_pair(alist)){
692 pmt_t p = pmt_car(alist);
693 if (!pmt_is_pair(p)) // malformed alist
696 if (pmt_eqv(obj, pmt_car(p)))
699 alist = pmt_cdr(alist);
705 pmt_assoc(pmt_t obj, pmt_t alist)
707 while (pmt_is_pair(alist)){
708 pmt_t p = pmt_car(alist);
709 if (!pmt_is_pair(p)) // malformed alist
712 if (pmt_equal(obj, pmt_car(p)))
715 alist = pmt_cdr(alist);
721 pmt_map(pmt_t proc(pmt_t), pmt_t list)
725 while(pmt_is_pair(list)){
726 r = pmt_cons(proc(pmt_car(list)), r);
727 list = pmt_cdr(list);
730 return pmt_reverse_x(r);
734 pmt_reverse(pmt_t listx)
739 while(pmt_is_pair(list)){
740 r = pmt_cons(pmt_car(list), r);
741 list = pmt_cdr(list);
743 if (pmt_is_null(list))
746 throw pmt_wrong_type("pmt_reverse", listx);
750 pmt_reverse_x(pmt_t list)
752 // FIXME do it destructively
753 return pmt_reverse(list);