1 /* -*- c++ -*- */
2 /*
3  * Copyright 2012,2013 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
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)
10  * any later version.
11  *
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.
16  *
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.
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #include "vector_map_impl.h"
28 #include <gnuradio/io_signature.h>
29 #include <string.h>
30 
31 namespace gr {
32 namespace blocks {
33 
get_in_sizeofs(size_t item_size,std::vector<size_t> in_vlens)34 std::vector<int> get_in_sizeofs(size_t item_size, std::vector<size_t> in_vlens)
35 {
36     std::vector<int> in_sizeofs;
37     for (unsigned int i = 0; i < in_vlens.size(); i++) {
38         in_sizeofs.push_back(in_vlens[i] * item_size);
39     }
40     return in_sizeofs;
41 }
42 
get_out_sizeofs(size_t item_size,std::vector<std::vector<std::vector<size_t>>> mapping)43 std::vector<int> get_out_sizeofs(size_t item_size,
44                                  std::vector<std::vector<std::vector<size_t>>> mapping)
45 {
46     std::vector<int> out_sizeofs;
47     for (unsigned int i = 0; i < mapping.size(); i++) {
48         out_sizeofs.push_back(mapping[i].size() * item_size);
49     }
50     return out_sizeofs;
51 }
52 
make(size_t item_size,std::vector<size_t> in_vlens,std::vector<std::vector<std::vector<size_t>>> mapping)53 vector_map::sptr vector_map::make(size_t item_size,
54                                   std::vector<size_t> in_vlens,
55                                   std::vector<std::vector<std::vector<size_t>>> mapping)
56 {
57     return gnuradio::get_initial_sptr(new vector_map_impl(item_size, in_vlens, mapping));
58 }
59 
vector_map_impl(size_t item_size,std::vector<size_t> in_vlens,std::vector<std::vector<std::vector<size_t>>> mapping)60 vector_map_impl::vector_map_impl(size_t item_size,
61                                  std::vector<size_t> in_vlens,
62                                  std::vector<std::vector<std::vector<size_t>>> mapping)
63     : sync_block(
64           "vector_map",
65           io_signature::makev(
66               in_vlens.size(), in_vlens.size(), get_in_sizeofs(item_size, in_vlens)),
67           io_signature::makev(
68               mapping.size(), mapping.size(), get_out_sizeofs(item_size, mapping))),
69       d_item_size(item_size),
70       d_in_vlens(in_vlens)
71 {
72     set_mapping(mapping);
73 }
74 
~vector_map_impl()75 vector_map_impl::~vector_map_impl() {}
76 
set_mapping(std::vector<std::vector<std::vector<size_t>>> mapping)77 void vector_map_impl::set_mapping(std::vector<std::vector<std::vector<size_t>>> mapping)
78 {
79     // Make sure the contents of the mapping vectors are possible.
80     for (unsigned int i = 0; i < mapping.size(); i++) {
81         for (unsigned int j = 0; j < mapping[i].size(); j++) {
82             if (mapping[i][j].size() != 2) {
83                 throw std::runtime_error(
84                     "Mapping must be of the form (out_mapping_stream1, "
85                     "out_mapping_stream2, ...), where out_mapping_stream1 is of the form "
86                     "(mapping_element1, mapping_element2, ...), where mapping_element1 "
87                     "is of the form (input_stream, input_element).  This error is raised "
88                     "because a mapping_element vector does not contain exactly 2 items.");
89             }
90             unsigned int s = mapping[i][j][0];
91             unsigned int index = mapping[i][j][1];
92             if (s >= d_in_vlens.size()) {
93                 throw std::runtime_error("Stream numbers in mapping must be less than "
94                                          "the number of input streams.");
95             }
96             if (index >= d_in_vlens[s]) {
97                 throw std::runtime_error("Indices in mapping must be greater than 0 and "
98                                          "less than the input vector lengths.");
99             }
100         }
101     }
102     gr::thread::scoped_lock guard(d_mutex);
103     d_mapping = mapping;
104 }
105 
work(int noutput_items,gr_vector_const_void_star & input_items,gr_vector_void_star & output_items)106 int vector_map_impl::work(int noutput_items,
107                           gr_vector_const_void_star& input_items,
108                           gr_vector_void_star& output_items)
109 {
110     const char** inv = (const char**)&input_items[0];
111     char** outv = (char**)&output_items[0];
112 
113     for (unsigned int n = 0; n < (unsigned int)(noutput_items); n++) {
114         for (unsigned int i = 0; i < d_mapping.size(); i++) {
115             unsigned int out_vlen = d_mapping[i].size();
116             for (unsigned int j = 0; j < out_vlen; j++) {
117                 unsigned int s = d_mapping[i][j][0];
118                 unsigned int k = d_mapping[i][j][1];
119                 memcpy(outv[i] + out_vlen * d_item_size * n + d_item_size * j,
120                        inv[s] + d_in_vlens[s] * d_item_size * n + k * d_item_size,
121                        d_item_size);
122             }
123         }
124     }
125 
126     return noutput_items;
127 }
128 
129 } /* namespace blocks */
130 } /* namespace gr */
131