Skip to content

Commit

Permalink
Replace push_back/reserve/back_inserter to ctor(size)/resize for opt.
Browse files Browse the repository at this point in the history
  • Loading branch information
fenrir-naru committed Jan 23, 2025
1 parent c3f9d0f commit 8bd1fd9
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 84 deletions.
4 changes: 2 additions & 2 deletions ext/gps_pvt/sdr/Replica/Replica_wrap.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2677,8 +2677,8 @@ SWIG_From_int (int value)
}


struct GPS_CA_Code : public TimeBasedSignalGenerator<int, tick_t> {
typedef TimeBasedSignalGenerator<int, tick_t> super_t;
struct GPS_CA_Code : public TimeBasedSignalGenerator<GPS_CA_Code, int, tick_t> {
typedef TimeBasedSignalGenerator<GPS_CA_Code, int, tick_t> super_t;
typedef typename GPS_Signal<tick_t>::CA_Code code_t;
code_t code;
int phase_cycle_int;
Expand Down
35 changes: 25 additions & 10 deletions ext/gps_pvt/sdr/Signal/Signal_wrap.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2580,22 +2580,36 @@ struct SignalUtil {
switch(TYPE(src)){
case T_STRING: {
unsigned char *buf((unsigned char *)(RSTRING_PTR(src)));
for(int i(0), len(RSTRING_LEN(src)); i < len; ++i, ++buf){
int len_src(RSTRING_LEN(src));
int len_dst(len_src * (8 / bits));
dst.resize(dst.size() + len_dst);
typename Signal<T>::buf_t::iterator it(dst.end() - len_dst);
for(int i(0); i < len_src; ++i, ++buf){
unsigned char c(*buf);
for(int j(8 / bits); j > 0; --j, c >>= bits){
dst.push_back((int)((c & mask) << 1) - (int)mask);
for(int j(8 / bits); j > 0; --j, c >>= bits, ++it){
*it = ((int)((c & mask) << 1) - (int)mask);
}
}
return true;
}
case T_FILE: {
rb_io_ascii8bit_binmode(src);
while(!RTEST(rb_io_eof(src))){
unsigned char c(NUM2CHR(rb_io_getbyte(src)));
for(int j(8 / bits); j > 0; --j, c >>= bits){
dst.push_back((int)((c & mask) << 1) - (int)mask);
typename Signal<T>::buf_t::iterator it_end(dst.end()), it(it_end);
static const int size_buf_add(1024 * bits);
while(true){
VALUE v(rb_io_getbyte(src));
if(!RTEST(v)){break;} // eof?
if(it == it_end){
dst.resize(dst.size() + size_buf_add);
it_end = dst.end();
it = it_end - size_buf_add;
}
unsigned char c(NUM2CHR(v));
for(int j(8 / bits); j > 0; --j, c >>= bits, ++it){
*it = ((int)((c & mask) << 1) - (int)mask);
}
}
if(it != it_end){dst.resize(it - dst.begin());}
return true;
}
default: return false;
Expand All @@ -2613,12 +2627,13 @@ struct SignalUtil {
bool valid_input(false);
if(value){
if(RB_TYPE_P(*value, T_ARRAY)){
res.reserve(len = RARRAY_LEN(*value));
res.resize(len = RARRAY_LEN(*value));
typename Signal<T>::buf_t::iterator it(res.begin());
T elm;
for(; i < len; i++){
for(; i < len; i++, ++it){
elm_val = RARRAY_AREF(*value, i);
if(!SWIG_IsOK(swig::asval(elm_val, &elm))){break;}
res.push_back(elm);
*it = elm;
}
valid_input = true;
}else if(RB_TYPE_P(*value, T_HASH)){
Expand Down
116 changes: 57 additions & 59 deletions ext/sdr/param/signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,41 @@
template <class T, class BufferT = std::vector<T> >
class Signal;

template <class T_Signal, class T_Tick = double>
template <class T_Generator, class T_Signal, class T_Tick = double>
struct SignalGenerator {
typedef T_Tick tick_t;
tick_t phase_cycle;
SignalGenerator(const tick_t &offset_cycle = 0)
: phase_cycle(offset_cycle){}
virtual ~SignalGenerator(){}
virtual T_Signal current() const = 0;
virtual void advance(const tick_t &shift_cycle){
~SignalGenerator(){}
T_Signal current() const;
void advance(const tick_t &shift_cycle){
phase_cycle += shift_cycle;
}
Signal<T_Signal> generate(
const tick_t &t, const tick_t &dt, const tick_t &freq);
};

template <class T_Signal, class T_Tick = double>
struct TimeBasedSignalGenerator : public SignalGenerator<T_Signal, T_Tick> {
typedef SignalGenerator<T_Signal, T_Tick> super_t;
template <class T_Generator, class T_Signal, class T_Tick = double>
struct TimeBasedSignalGenerator : public SignalGenerator<T_Generator, T_Signal, T_Tick> {
typedef SignalGenerator<T_Generator, T_Signal, T_Tick> super_t;
typename super_t::tick_t frequency;
TimeBasedSignalGenerator(
const typename super_t::tick_t &init_freq,
const typename super_t::tick_t &offset_time = 0)
: super_t(offset_time * init_freq), frequency(init_freq) {}
void advance_time(const typename super_t::tick_t &shift_time){
this->advance(frequency * shift_time);
static_cast<T_Generator *>(this)->advance(frequency * shift_time);
}
using super_t::generate;
Signal<T_Signal> generate(
const typename super_t::tick_t &t, const typename super_t::tick_t &dt);
};

template <class T_Signal>
struct CosineWave : public TimeBasedSignalGenerator<T_Signal> {
typedef TimeBasedSignalGenerator<T_Signal> super_t;
struct CosineWave
: public TimeBasedSignalGenerator<CosineWave<T_Signal>, T_Signal> {
typedef TimeBasedSignalGenerator<CosineWave<T_Signal>, T_Signal> super_t;
CosineWave(const typename super_t::tick_t &init_freq)
: super_t(init_freq) {}
T_Signal current() const {
Expand All @@ -67,8 +68,8 @@ struct CosineWave : public TimeBasedSignalGenerator<T_Signal> {
};
template <class T_Signal>
struct CosineWave<Complex<T_Signal> >
: public TimeBasedSignalGenerator<Complex<T_Signal> > {
typedef TimeBasedSignalGenerator<Complex<T_Signal> > super_t;
: public TimeBasedSignalGenerator<CosineWave<Complex<T_Signal> >, Complex<T_Signal> > {
typedef TimeBasedSignalGenerator<CosineWave<Complex<T_Signal> >, Complex<T_Signal> > super_t;
CosineWave(const typename super_t::tick_t &init_freq)
: super_t(init_freq) {}
Complex<T_Signal> current() const {
Expand Down Expand Up @@ -133,32 +134,28 @@ class Signal {
}
public:
template <class T2, class BufferT2>
Signal(const Signal<T2, BufferT2> &sig) : samples() {
samples.reserve(sig.samples.size());
std::copy(sig.samples.begin(), sig.samples.end(), std::back_inserter(samples));
}
template <class T_Signal, class T_Tick>
Signal(const Signal<T2, BufferT2> &sig)
: samples(sig.samples.begin(), sig.samples.end()) {}
template <class T_Generator, class T_Signal, class T_Tick>
Signal(
SignalGenerator<T_Signal, T_Tick> &gen,
SignalGenerator<T_Generator, T_Signal, T_Tick> &gen,
const T_Tick &t, const T_Tick &dt, const T_Tick &freq)
: samples() {
int len(std::round(t / dt));
samples.reserve(len);
for(; len > 0; --len){
samples.push_back(gen.current());
gen.advance(freq * dt);
: samples(std::round(t / dt)) {
for(typename buf_t::iterator it(samples.begin()), it_end(samples.end());
it != it_end; ++it){
*it = static_cast<T_Generator &>(gen).current();
static_cast<T_Generator &>(gen).advance(freq * dt);
}
}
template <class T_Signal, class T_Tick>
template <class T_Generator, class T_Signal, class T_Tick>
Signal(
TimeBasedSignalGenerator<T_Signal, T_Tick> &gen,
TimeBasedSignalGenerator<T_Generator, T_Signal, T_Tick> &gen,
const T_Tick &t, const T_Tick &dt)
: samples() {
int len(std::round(t / dt));
samples.reserve(len);
for(; len > 0; --len){
samples.push_back(gen.current());
gen.advance_time(dt);
: samples(std::round(t / dt)) {
for(typename buf_t::iterator it(samples.begin()), it_end(samples.end());
it != it_end; ++it){
*it = static_cast<T_Generator &>(gen).current();
static_cast<T_Generator &>(gen).advance_time(dt);
}
}

Expand Down Expand Up @@ -257,29 +254,30 @@ class Signal {
self_t &rotate(int offset){
offset = std::div(offset, samples.size()).rem;
if(offset < 0){offset += samples.size();}
buf_t buf;
buf.reserve(samples.size());
std::copy(samples.begin() + offset, samples.end(), std::back_inserter(buf));
std::copy(samples.begin(), samples.begin() + offset, std::back_inserter(buf));
buf_t buf(samples.size());
std::copy(samples.begin() + offset, samples.end(), buf.begin());
std::copy(samples.begin(), samples.begin() + offset, buf.end() - offset);
samples.swap(buf);
return *this;
}
sig_t circular(int offset, size_t length) const {
offset = std::div(offset, samples.size()).rem;
if(offset < 0){offset += samples.size();}
sig_t res(length);
typename sig_t::buf_t buf(length);
typename sig_t::buf_t::iterator it(buf.begin());
typename buf_t::const_iterator it_first(samples.begin() + offset);
size_t length_latter(samples.size() - offset);
if(length >= length_latter){
std::copy(it_first, samples.end(), std::back_inserter(res.samples));
for(length -= length_latter; length > samples.size(); length -= samples.size()){
std::copy(samples.begin(), samples.end(), std::back_inserter(res.samples));
std::copy(it_first, samples.end(), it);
it += length_latter;
for(length -= length_latter; length > samples.size(); length -= samples.size(), it += samples.size()){
std::copy(samples.begin(), samples.end(), it);
}
std::copy(samples.begin(), samples.begin() + length, std::back_inserter(res.samples));
std::copy(samples.begin(), samples.begin() + length, it);
}else{
std::copy(it_first, it_first + length, std::back_inserter(res.samples));
std::copy(it_first, it_first + length, it);
}
return res;
return sig_t(buf);
}
sig_t circular(const int &offset) const {
return circular(offset, size());
Expand All @@ -299,25 +297,25 @@ class Signal {

protected:
sig_r_t pick(const bool &is_real) const {
sig_r_t res(samples.size());
typename sig_r_t::buf_t buf(samples.size());
if(is_real){
struct op_t {
real_t operator()(const real_t &v) const {return v;}
real_t operator()(const complex_t &v) const {return v.real();}
};
std::transform(
samples.begin(), samples.end(),
std::back_inserter(res.samples), op_t());
buf.begin(), op_t());
}else{
struct op_t {
real_t operator()(const real_t &v) const {return real_t(0);}
real_t operator()(const complex_t &v) const {return v.imaginary();}
};
std::transform(
samples.begin(), samples.end(),
std::back_inserter(res.samples), op_t());
buf.begin(), op_t());
}
return res;
return sig_r_t(buf);
}
public:
sig_r_t real() const {return pick(true);}
Expand All @@ -327,24 +325,24 @@ class Signal {
real_t operator()(const real_t &v) const {return v;}
complex_t operator()(const complex_t &v) const {return v.conjugate();}
};
sig_t res(samples.size());
typename sig_t::buf_t buf(samples.size());
std::transform(
samples.begin(), samples.end(),
std::back_inserter(res.samples),
buf.begin(),
op_t());
return res;
return sig_t(buf);
}
sig_r_t abs() const {
struct op_t {
real_t operator()(const real_t &v) const {return std::abs(v);}
real_t operator()(const complex_t &v) const {return v.abs();}
};
sig_r_t res(samples.size());
typename sig_r_t::buf_t buf(samples.size());
std::transform(
samples.begin(), samples.end(),
std::back_inserter(res.samples),
buf.begin(),
op_t());
return res;
return sig_r_t(buf);
}
value_t sum() const {
return std::accumulate(samples.begin(), samples.end(), value_t(0));
Expand Down Expand Up @@ -383,16 +381,16 @@ class Signal {
}
};

template <class T_Signal, class T_Tick>
Signal<T_Signal> SignalGenerator<T_Signal, T_Tick>::generate(
template <class T_Generator, class T_Signal, class T_Tick>
Signal<T_Signal> SignalGenerator<T_Generator, T_Signal, T_Tick>::generate(
const tick_t &t, const tick_t &dt, const tick_t &freq) {
return Signal<T_Signal>(*this, t, dt, freq);
return Signal<T_Signal>(static_cast<T_Generator &>(*this), t, dt, freq);
}

template <class T_Signal, class T_Tick>
Signal<T_Signal> TimeBasedSignalGenerator<T_Signal, T_Tick>::generate(
template <class T_Generator, class T_Signal, class T_Tick>
Signal<T_Signal> TimeBasedSignalGenerator<T_Generator, T_Signal, T_Tick>::generate(
const typename super_t::tick_t &t, const typename super_t::tick_t &dt) {
return Signal<T_Signal>(*this, t, dt);
return Signal<T_Signal>(static_cast<T_Generator &>(*this), t, dt);
}

template <class SignalT>
Expand Down
4 changes: 2 additions & 2 deletions ext/sdr/swig/Replica.i
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ struct GPS_CA_Code {
};

%{
struct GPS_CA_Code : public TimeBasedSignalGenerator<int, tick_t> {
typedef TimeBasedSignalGenerator<int, tick_t> super_t;
struct GPS_CA_Code : public TimeBasedSignalGenerator<GPS_CA_Code, int, tick_t> {
typedef TimeBasedSignalGenerator<GPS_CA_Code, int, tick_t> super_t;
typedef typename GPS_Signal<tick_t>::CA_Code code_t;
code_t code;
int phase_cycle_int;
Expand Down
35 changes: 25 additions & 10 deletions ext/sdr/swig/Signal.i
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,36 @@ struct SignalUtil {
switch(TYPE(src)){
case T_STRING: {
unsigned char *buf((unsigned char *)(RSTRING_PTR(src)));
for(int i(0), len(RSTRING_LEN(src)); i < len; ++i, ++buf){
int len_src(RSTRING_LEN(src));
int len_dst(len_src * (8 / bits));
dst.resize(dst.size() + len_dst);
typename Signal<T>::buf_t::iterator it(dst.end() - len_dst);
for(int i(0); i < len_src; ++i, ++buf){
unsigned char c(*buf);
for(int j(8 / bits); j > 0; --j, c >>= bits){
dst.push_back((int)((c & mask) << 1) - (int)mask);
for(int j(8 / bits); j > 0; --j, c >>= bits, ++it){
*it = ((int)((c & mask) << 1) - (int)mask);
}
}
return true;
}
case T_FILE: {
rb_io_ascii8bit_binmode(src);
while(!RTEST(rb_io_eof(src))){
unsigned char c(NUM2CHR(rb_io_getbyte(src)));
for(int j(8 / bits); j > 0; --j, c >>= bits){
dst.push_back((int)((c & mask) << 1) - (int)mask);
typename Signal<T>::buf_t::iterator it_end(dst.end()), it(it_end);
static const int size_buf_add(1024 * bits);
while(true){
VALUE v(rb_io_getbyte(src));
if(!RTEST(v)){break;} // eof?
if(it == it_end){
dst.resize(dst.size() + size_buf_add);
it_end = dst.end();
it = it_end - size_buf_add;
}
unsigned char c(NUM2CHR(v));
for(int j(8 / bits); j > 0; --j, c >>= bits, ++it){
*it = ((int)((c & mask) << 1) - (int)mask);
}
}
if(it != it_end){dst.resize(it - dst.begin());}
return true;
}
default: return false;
Expand All @@ -93,12 +107,13 @@ struct SignalUtil {
bool valid_input(false);
if(value){
if(RB_TYPE_P(*value, T_ARRAY)){
res.reserve(len = RARRAY_LEN(*value));
res.resize(len = RARRAY_LEN(*value));
typename Signal<T>::buf_t::iterator it(res.begin());
T elm;
for(; i < len; i++){
for(; i < len; i++, ++it){
elm_val = RARRAY_AREF(*value, i);
if(!SWIG_IsOK(swig::asval(elm_val, &elm))){break;}
res.push_back(elm);
*it = elm;
}
valid_input = true;
}else if(RB_TYPE_P(*value, T_HASH)){
Expand Down
2 changes: 1 addition & 1 deletion ext/sdr/swig/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ $(BUILD_DIR)/%.so : $(BUILD_DIR)/%_wrap.cxx $(RUBY_CONF)
cd $(BUILD_DIR)/$(PACKAGE); \
echo 'create_makefile("$(PACKAGE)")' >> $(RUBY_CONF); \
$(RUBY) $(RUBY_CONF); \
make; \
make V=1; \
cp *.so ../

$(BUILD_DIR) :
Expand Down

0 comments on commit 8bd1fd9

Please sign in to comment.