summaryrefslogtreecommitdiff
path: root/gr-blocks/lib/vector_insert_X_impl.cc.t
blob: adf31fe05aae5a3f2828747e559add5a75f57d28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* -*- c++ -*- */
/*
 * Copyright 2012 Free Software Foundation, Inc.
 * 
 * This file is part of GNU Radio
 * 
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 * 
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

// @WARNING@

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <@NAME_IMPL@.h>
#include <algorithm>
#include <gr_io_signature.h>
#include <stdexcept>
#include <stdio.h>

namespace gr {
  namespace blocks {

    @NAME@::sptr
    @NAME@::make(const std::vector<@TYPE@> &data, int periodicity, int offset)
    {
      return gnuradio::get_initial_sptr
        (new @NAME_IMPL@(data, periodicity, offset));
    }

    @NAME_IMPL@::@NAME_IMPL@(const std::vector<@TYPE@> &data,
                             int periodicity, int offset)
    : gr_block("@BASE_NAME@",
               gr_make_io_signature(1, 1, sizeof(@TYPE@)),
               gr_make_io_signature(1, 1, sizeof(@TYPE@))),
      d_data(data),
      d_offset(offset),
      d_periodicity(periodicity)
    {
      //printf("INITIAL: periodicity = %d, offset = %d\n", periodicity, offset);
      // some sanity checks
      assert(offset < periodicity);
      assert(offset >= 0);
      assert((size_t)periodicity > data.size());
    }

    @NAME_IMPL@::~@NAME_IMPL@()
    {}

    int
    @NAME_IMPL@::general_work(int noutput_items,
                              gr_vector_int &ninput_items,
                              gr_vector_const_void_star &input_items,
                              gr_vector_void_star &output_items)
    {
      @TYPE@ *out = (@TYPE@ *)output_items[0];
      const @TYPE@ *in = (const @TYPE@ *)input_items[0];

      int ii(0), oo(0);

      while((oo < noutput_items) && (ii < ninput_items[0])) {
        //printf("oo = %d, ii = %d, d_offset = %d, noutput_items = %d, ninput_items[0] = %d", oo, ii, d_offset, noutput_items, ninput_items[0]);
        //printf(", d_periodicity = %d\n", d_periodicity);
    
        if(d_offset >= ((int)d_data.size())) { // if we are in the copy region
          int max_copy = std::min(std::min(noutput_items - oo, ninput_items[0] - ii),
                                  d_periodicity - d_offset);
          //printf("copy %d from input\n", max_copy);
          memcpy( &out[oo], &in[ii], sizeof(@TYPE@)*max_copy );
          //printf(" * memcpy returned.\n");
          ii += max_copy;
          oo += max_copy;
          d_offset = (d_offset + max_copy)%d_periodicity;
        } 
        else { // if we are in the insertion region
          int max_copy = std::min(noutput_items - oo, ((int)d_data.size()) - d_offset);
          //printf("copy %d from d_data[%d] to out[%d]\n", max_copy, d_offset, oo);
          memcpy(&out[oo], &d_data[d_offset], sizeof(@TYPE@)*max_copy);
          //printf(" * memcpy returned.\n");
          oo += max_copy; 
          d_offset = (d_offset + max_copy)%d_periodicity;
          //printf(" ## (inelse) oo = %d, d_offset = %d\n", oo, d_offset);
        }
    
        //printf(" # exit else, on to next loop.\n");
      }
      //printf(" # got out of loop\n");

      //printf("consume = %d, produce = %d\n", ii, oo);
      consume_each(ii);
      return oo;
    }

  } /* namespace blocks */
} /* namespace gr */