root / gnuradio-core / src / lib / general / gr_vector_map.cc @ 9b2855a4
History | View | Annotate | Download (4 kB)
| 1 | ae75ab75 | Ben Reynwar | /* -*- c++ -*- */
|
|---|---|---|---|
| 2 | ae75ab75 | Ben Reynwar | /*
|
| 3 | ae75ab75 | Ben Reynwar | * Copyright 2012 Free Software Foundation, Inc. |
| 4 | ae75ab75 | Ben Reynwar | * |
| 5 | ae75ab75 | Ben Reynwar | * This file is part of GNU Radio |
| 6 | ae75ab75 | Ben Reynwar | * |
| 7 | ae75ab75 | Ben Reynwar | * GNU Radio is free software; you can redistribute it and/or modify |
| 8 | ae75ab75 | Ben Reynwar | * it under the terms of the GNU General Public License as published by |
| 9 | ae75ab75 | Ben Reynwar | * the Free Software Foundation; either version 3, or (at your option) |
| 10 | ae75ab75 | Ben Reynwar | * any later version. |
| 11 | ae75ab75 | Ben Reynwar | * |
| 12 | ae75ab75 | Ben Reynwar | * GNU Radio is distributed in the hope that it will be useful, |
| 13 | ae75ab75 | Ben Reynwar | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | ae75ab75 | Ben Reynwar | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | ae75ab75 | Ben Reynwar | * GNU General Public License for more details. |
| 16 | ae75ab75 | Ben Reynwar | * |
| 17 | ae75ab75 | Ben Reynwar | * You should have received a copy of the GNU General Public License |
| 18 | ae75ab75 | Ben Reynwar | * along with GNU Radio; see the file COPYING. If not, write to |
| 19 | ae75ab75 | Ben Reynwar | * the Free Software Foundation, Inc., 51 Franklin Street, |
| 20 | ae75ab75 | Ben Reynwar | * Boston, MA 02110-1301, USA. |
| 21 | ae75ab75 | Ben Reynwar | */ |
| 22 | ae75ab75 | Ben Reynwar | |
| 23 | ae75ab75 | Ben Reynwar | #ifdef HAVE_CONFIG_H
|
| 24 | ae75ab75 | Ben Reynwar | #include "config.h" |
| 25 | ae75ab75 | Ben Reynwar | #endif
|
| 26 | ae75ab75 | Ben Reynwar | |
| 27 | ae75ab75 | Ben Reynwar | #include <gr_vector_map.h> |
| 28 | ae75ab75 | Ben Reynwar | #include <gr_io_signature.h> |
| 29 | ae75ab75 | Ben Reynwar | #include <string.h> |
| 30 | ae75ab75 | Ben Reynwar | |
| 31 | ae75ab75 | Ben Reynwar | std::vector<int>
|
| 32 | d4fe4377 | Tom Rondeau | get_in_sizeofs(size_t item_size, std::vector<size_t> in_vlens) |
| 33 | d4fe4377 | Tom Rondeau | {
|
| 34 | ae75ab75 | Ben Reynwar | std::vector<int> in_sizeofs;
|
| 35 | 52c64845 | Donald Porges | for(unsigned int i = 0; i < in_vlens.size(); i++) { |
| 36 | d4fe4377 | Tom Rondeau | in_sizeofs.push_back(in_vlens[i]*item_size); |
| 37 | ae75ab75 | Ben Reynwar | } |
| 38 | ae75ab75 | Ben Reynwar | return in_sizeofs;
|
| 39 | ae75ab75 | Ben Reynwar | } |
| 40 | ae75ab75 | Ben Reynwar | |
| 41 | ae75ab75 | Ben Reynwar | std::vector<int>
|
| 42 | ae75ab75 | Ben Reynwar | get_out_sizeofs(size_t item_size, |
| 43 | d4fe4377 | Tom Rondeau | std::vector< std::vector< std::vector<size_t> > > mapping) |
| 44 | d4fe4377 | Tom Rondeau | {
|
| 45 | ae75ab75 | Ben Reynwar | std::vector<int> out_sizeofs;
|
| 46 | 52c64845 | Donald Porges | for(unsigned int i = 0; i < mapping.size(); i++) { |
| 47 | d4fe4377 | Tom Rondeau | out_sizeofs.push_back(mapping[i].size()*item_size); |
| 48 | ae75ab75 | Ben Reynwar | } |
| 49 | ae75ab75 | Ben Reynwar | return out_sizeofs;
|
| 50 | ae75ab75 | Ben Reynwar | } |
| 51 | ae75ab75 | Ben Reynwar | |
| 52 | ae75ab75 | Ben Reynwar | gr_vector_map_sptr |
| 53 | ae75ab75 | Ben Reynwar | gr_make_vector_map (size_t item_size, std::vector<size_t> in_vlens, |
| 54 | d4fe4377 | Tom Rondeau | std::vector< std::vector< std::vector<size_t> > > mapping) |
| 55 | ae75ab75 | Ben Reynwar | {
|
| 56 | d4fe4377 | Tom Rondeau | return gnuradio::get_initial_sptr(new gr_vector_map(item_size, |
| 57 | d4fe4377 | Tom Rondeau | in_vlens, |
| 58 | d4fe4377 | Tom Rondeau | mapping)); |
| 59 | ae75ab75 | Ben Reynwar | } |
| 60 | ae75ab75 | Ben Reynwar | |
| 61 | d4fe4377 | Tom Rondeau | gr_vector_map::gr_vector_map(size_t item_size, std::vector<size_t> in_vlens, |
| 62 | d4fe4377 | Tom Rondeau | std::vector< std::vector< std::vector<size_t> > > mapping) |
| 63 | d4fe4377 | Tom Rondeau | : gr_sync_block("vector_map",
|
| 64 | d4fe4377 | Tom Rondeau | gr_make_io_signaturev(in_vlens.size(), in_vlens.size(), |
| 65 | d4fe4377 | Tom Rondeau | get_in_sizeofs(item_size, in_vlens)), |
| 66 | d4fe4377 | Tom Rondeau | gr_make_io_signaturev(mapping.size(), mapping.size(), |
| 67 | d4fe4377 | Tom Rondeau | get_out_sizeofs(item_size, mapping))), |
| 68 | d4fe4377 | Tom Rondeau | d_item_size(item_size), d_in_vlens(in_vlens) |
| 69 | ae75ab75 | Ben Reynwar | {
|
| 70 | ae75ab75 | Ben Reynwar | set_mapping(mapping); |
| 71 | ae75ab75 | Ben Reynwar | } |
| 72 | ae75ab75 | Ben Reynwar | |
| 73 | ae75ab75 | Ben Reynwar | void
|
| 74 | ae75ab75 | Ben Reynwar | gr_vector_map::set_mapping(std::vector< std::vector< std::vector<size_t> > > mapping) {
|
| 75 | ae75ab75 | Ben Reynwar | // Make sure the contents of the mapping vectors are possible.
|
| 76 | d4fe4377 | Tom Rondeau | for(unsigned int i=0; i<mapping.size(); i++) { |
| 77 | d4fe4377 | Tom Rondeau | for(unsigned int j=0; j<mapping[i].size(); j++) { |
| 78 | d4fe4377 | Tom Rondeau | if(mapping[i][j].size() != 2) { |
| 79 | d4fe4377 | Tom Rondeau | throw std::runtime_error("Mapping must be of the form (out_mapping_stream1, out_mapping_stream2, ...), where out_mapping_stream1 is of the form (mapping_element1, mapping_element2, ...), where mapping_element1 is of the form (input_stream, input_element). This error is raised because a mapping_element vector does not contain exactly 2 items."); |
| 80 | d4fe4377 | Tom Rondeau | } |
| 81 | d4fe4377 | Tom Rondeau | unsigned int s = mapping[i][j][0]; |
| 82 | d4fe4377 | Tom Rondeau | unsigned int index = mapping[i][j][1]; |
| 83 | d4fe4377 | Tom Rondeau | if(s >= d_in_vlens.size()) {
|
| 84 | d4fe4377 | Tom Rondeau | throw std::runtime_error("Stream numbers in mapping must be less than the number of input streams."); |
| 85 | d4fe4377 | Tom Rondeau | } |
| 86 | d4fe4377 | Tom Rondeau | if((index < 0) || (index >= d_in_vlens[s])) { |
| 87 | d4fe4377 | Tom Rondeau | throw std::runtime_error ("Indices in mapping must be greater than 0 and less than the input vector lengths."); |
| 88 | d4fe4377 | Tom Rondeau | } |
| 89 | d4fe4377 | Tom Rondeau | } |
| 90 | d4fe4377 | Tom Rondeau | } |
| 91 | ae75ab75 | Ben Reynwar | gruel::scoped_lock guard(d_mutex); |
| 92 | ae75ab75 | Ben Reynwar | d_mapping = mapping; |
| 93 | ae75ab75 | Ben Reynwar | } |
| 94 | ae75ab75 | Ben Reynwar | |
| 95 | ae75ab75 | Ben Reynwar | int
|
| 96 | d4fe4377 | Tom Rondeau | gr_vector_map::work(int noutput_items,
|
| 97 | d4fe4377 | Tom Rondeau | gr_vector_const_void_star &input_items, |
| 98 | d4fe4377 | Tom Rondeau | gr_vector_void_star &output_items) |
| 99 | ae75ab75 | Ben Reynwar | {
|
| 100 | ae75ab75 | Ben Reynwar | const char **inv = (const char **) &input_items[0]; |
| 101 | ae75ab75 | Ben Reynwar | char **outv = (char **) &output_items[0]; |
| 102 | ae75ab75 | Ben Reynwar | |
| 103 | d4fe4377 | Tom Rondeau | for(unsigned int n = 0; n < (unsigned int)(noutput_items); n++) { |
| 104 | d4fe4377 | Tom Rondeau | for(unsigned int i = 0; i < d_mapping.size(); i++) { |
| 105 | d4fe4377 | Tom Rondeau | unsigned int out_vlen = d_mapping[i].size(); |
| 106 | d4fe4377 | Tom Rondeau | for(unsigned int j = 0; j < out_vlen; j++) { |
| 107 | d4fe4377 | Tom Rondeau | unsigned int s = d_mapping[i][j][0]; |
| 108 | d4fe4377 | Tom Rondeau | unsigned int k = d_mapping[i][j][1]; |
| 109 | d4fe4377 | Tom Rondeau | memcpy(outv[i] + out_vlen*d_item_size*n + |
| 110 | d4fe4377 | Tom Rondeau | d_item_size*j, inv[s] + d_in_vlens[s]*d_item_size*n + |
| 111 | d4fe4377 | Tom Rondeau | k*d_item_size, d_item_size); |
| 112 | d4fe4377 | Tom Rondeau | } |
| 113 | d4fe4377 | Tom Rondeau | } |
| 114 | ae75ab75 | Ben Reynwar | } |
| 115 | ae75ab75 | Ben Reynwar | |
| 116 | ae75ab75 | Ben Reynwar | return noutput_items;
|
| 117 | ae75ab75 | Ben Reynwar | } |