M
ma740988
As part of a struct called 'overall', I have a two dimensional array
(ipd) and one dimensional array (mpt) that I'll merge - to produce an
end result that akin to the chart shown. The end result is stored in a
vector of 'storables'.
The code compiles and excutes. The question. Interested in
improvements or perhaps a more efficient way than what I've shown.
Thanks in advance.
# include <vector>
# include <iostream>
# include <algorithm>
#include <fstream>
using std::cout;
using std::endl;
using std::vector;
using std:stream;
enum proc_type { FFT, IFFT, INVALID = -1 };
struct data_segment_id_t {
int segment_id;
data_segment_id_t()
: segment_id(-1) {}
};
unsigned int const max_seg = 5;
unsigned int const max_proc = 6;
unsigned int const max_recipients = 7;
unsigned int const max_processors = 5;
struct im_def {
data_segment_id_t sid;
unsigned int tp_data_size;
unsigned int rp_data_size;
im_def()
: tp_data_size(0)
, rp_data_size(0) {}
};
struct my_proc_t
{
data_segment_id_t seg_arr[max_seg];
proc_type pt ;
my_proc_t()
: pt (INVALID)
{}
};
struct overall { // this is filled out in main - below.
my_proc_t mpt[ max_proc ];
im_def ipd[ max_seg][ max_processors ];
};
/*
In main (below) I've filled out the structs in overall as follows:
im_def
sid | tp_data_size | rp_data_size
--------------------------------------------------
[0][0] 0 | 0x100 | 0x200
[1][0] 3 | 0x200 | 0x200
mpt
sid | pt
------------------------------------
0, 1, 2 | FFT
3 | IFFT
I will combine the mpt and im_def such that the end result will be:
end_result
sid | pt | tp_data_size | rp_data_size |
tp_offset | rp_offset
--------------------------------------------------------------------------------------
(*) 0 | FFT | 0x100 | 0x200 |
(**)0xC000000 | (**) 0xF000000
1 | IFFT | 0x200 | 0x200 |
(***)0xC000100 |(***) 0xF000200
Couple things:
(*) ipd is a two dimensional array. For simplicity I'm only
doing the [0][0]
and [1][0]. You will compare mpt-sid with im_def-sid. if
there's a match.
I will store sid, pt, tp_data_size, rp_data_size, tp_offset and
rp_offset
(see end result above)
(**) is the initial tp and rp offsets.
(***) is the result of adding - for tp 0xC000000 + 0x100
- for rp 0xF000000 + 0x200
*/
class Storable
{
private:
int sid;
proc_type pt;
unsigned int tp_data_size;
unsigned int rp_data_size;
unsigned int tp_offset;
unsigned int rp_offset;
static unsigned int tp_offset_glob;
static unsigned int rp_offset_glob;
public:
Storable(
int sidIn,
proc_type ptIn,
unsigned int tpIn,
unsigned int rpIn
)
: sid(sidIn)
, pt(ptIn)
, tp_data_size(tpIn)
, rp_data_size(rpIn)
, tp_offset(tp_offset_glob)
, rp_offset(rp_offset_glob)
{
tp_offset_glob+=tp_data_size;
rp_offset_glob+=rp_data_size;
}
friend ostream &operator<< (ostream &os, const Storable s)
{
os << std::dec << s.sid << " " << s.pt << " "
<< std::hex << s.tp_data_size << " " << s.rp_data_size
<< " " << s.tp_offset << " " << s.rp_offset << endl;
return os;
}
};
unsigned int Storable::tp_offset_glob=0xC000000; // known initial value
unsigned int Storable::rp_offset_glob=0xF000000; // known initial value
// requirements are here in foo_w
class foo_w
{
private:
vector<Storable> v;
public:
foo_w(const overall ov)
{
/*
1.
I'll get an overall from the constructor - I'll create
end_results -
using a storable class with a suitable container.
This way, I'll have everything when I iterate through the container
*/
for(int i=0;i<max_seg;i++)
{
for(int j=0;j<max_processors;j++)
{
// I consider im_defs with zero rp and zero tp as unused. It
would be better
// to default the segment_id to -1 in order do distinguish
between unused and
// empty im_def's - perhaps
if(ov.ipd[j].tp_data_size !=0 || ov.ipd[j].rp_data_size != 0
)
{
// Found a used im_def now search a matching mpt.
for(int k=0;k<max_proc;k++)
{
// Search only valid my_proc_t's
if(ov.mpt[k].pt != INVALID)
{
for(int l=0;l<max_seg;l++)
{
// Notice that I'm doing full walks of ther array here, if the
array is
// only used in a sequential order (e.g. only the first n
elements breaks
// should be inserted in order to optimize the code - I think
if(ov.mpt[k].seg_arr[l].segment_id != -1 &&
ov.mpt[k].seg_arr[l].segment_id ==
ov.ipd[j].sid.segment_id)
{
// Gotcha
v.push_back(Storable(ov.mpt[k].seg_arr[l].segment_id,
ov.mpt[k].pt,
ov.ipd[j].tp_data_size,
ov.ipd[j].rp_data_size));
}
}
}
}
}
}
}
}
void dump()
{
// iterate and print stuff here.
for(vector<Storable>::iterator it=v.begin();it!=v.end();it++)
{
cout << *it;
}
}
~foo_w() {}
};
int main()
{
overall ov;
ov.ipd[0][0].sid.segment_id = 0;
ov.ipd[0][0].tp_data_size = 0x100;
ov.ipd[0][0].rp_data_size = 0x200;
ov.ipd[1][0].sid.segment_id = 3;
ov.ipd[1][0].tp_data_size = 0x200;
ov.ipd[1][0].rp_data_size = 0x200;
ov.mpt[0].seg_arr[0].segment_id = 0;
ov.mpt[0].seg_arr[1].segment_id = 1;
ov.mpt[0].seg_arr[2].segment_id = 2;
ov.mpt[0].pt = FFT;
ov.mpt[1].seg_arr[0].segment_id = 3;
ov.mpt[1].pt = IFFT;
foo_w f(ov); // now transform the c style struct into something
meaningful
f.dump();
}
(ipd) and one dimensional array (mpt) that I'll merge - to produce an
end result that akin to the chart shown. The end result is stored in a
vector of 'storables'.
The code compiles and excutes. The question. Interested in
improvements or perhaps a more efficient way than what I've shown.
Thanks in advance.
# include <vector>
# include <iostream>
# include <algorithm>
#include <fstream>
using std::cout;
using std::endl;
using std::vector;
using std:stream;
enum proc_type { FFT, IFFT, INVALID = -1 };
struct data_segment_id_t {
int segment_id;
data_segment_id_t()
: segment_id(-1) {}
};
unsigned int const max_seg = 5;
unsigned int const max_proc = 6;
unsigned int const max_recipients = 7;
unsigned int const max_processors = 5;
struct im_def {
data_segment_id_t sid;
unsigned int tp_data_size;
unsigned int rp_data_size;
im_def()
: tp_data_size(0)
, rp_data_size(0) {}
};
struct my_proc_t
{
data_segment_id_t seg_arr[max_seg];
proc_type pt ;
my_proc_t()
: pt (INVALID)
{}
};
struct overall { // this is filled out in main - below.
my_proc_t mpt[ max_proc ];
im_def ipd[ max_seg][ max_processors ];
};
/*
In main (below) I've filled out the structs in overall as follows:
im_def
sid | tp_data_size | rp_data_size
--------------------------------------------------
[0][0] 0 | 0x100 | 0x200
[1][0] 3 | 0x200 | 0x200
mpt
sid | pt
------------------------------------
0, 1, 2 | FFT
3 | IFFT
I will combine the mpt and im_def such that the end result will be:
end_result
sid | pt | tp_data_size | rp_data_size |
tp_offset | rp_offset
--------------------------------------------------------------------------------------
(*) 0 | FFT | 0x100 | 0x200 |
(**)0xC000000 | (**) 0xF000000
1 | IFFT | 0x200 | 0x200 |
(***)0xC000100 |(***) 0xF000200
Couple things:
(*) ipd is a two dimensional array. For simplicity I'm only
doing the [0][0]
and [1][0]. You will compare mpt-sid with im_def-sid. if
there's a match.
I will store sid, pt, tp_data_size, rp_data_size, tp_offset and
rp_offset
(see end result above)
(**) is the initial tp and rp offsets.
(***) is the result of adding - for tp 0xC000000 + 0x100
- for rp 0xF000000 + 0x200
*/
class Storable
{
private:
int sid;
proc_type pt;
unsigned int tp_data_size;
unsigned int rp_data_size;
unsigned int tp_offset;
unsigned int rp_offset;
static unsigned int tp_offset_glob;
static unsigned int rp_offset_glob;
public:
Storable(
int sidIn,
proc_type ptIn,
unsigned int tpIn,
unsigned int rpIn
)
: sid(sidIn)
, pt(ptIn)
, tp_data_size(tpIn)
, rp_data_size(rpIn)
, tp_offset(tp_offset_glob)
, rp_offset(rp_offset_glob)
{
tp_offset_glob+=tp_data_size;
rp_offset_glob+=rp_data_size;
}
friend ostream &operator<< (ostream &os, const Storable s)
{
os << std::dec << s.sid << " " << s.pt << " "
<< std::hex << s.tp_data_size << " " << s.rp_data_size
<< " " << s.tp_offset << " " << s.rp_offset << endl;
return os;
}
};
unsigned int Storable::tp_offset_glob=0xC000000; // known initial value
unsigned int Storable::rp_offset_glob=0xF000000; // known initial value
// requirements are here in foo_w
class foo_w
{
private:
vector<Storable> v;
public:
foo_w(const overall ov)
{
/*
1.
I'll get an overall from the constructor - I'll create
end_results -
using a storable class with a suitable container.
This way, I'll have everything when I iterate through the container
*/
for(int i=0;i<max_seg;i++)
{
for(int j=0;j<max_processors;j++)
{
// I consider im_defs with zero rp and zero tp as unused. It
would be better
// to default the segment_id to -1 in order do distinguish
between unused and
// empty im_def's - perhaps
if(ov.ipd[j].tp_data_size !=0 || ov.ipd[j].rp_data_size != 0
)
{
// Found a used im_def now search a matching mpt.
for(int k=0;k<max_proc;k++)
{
// Search only valid my_proc_t's
if(ov.mpt[k].pt != INVALID)
{
for(int l=0;l<max_seg;l++)
{
// Notice that I'm doing full walks of ther array here, if the
array is
// only used in a sequential order (e.g. only the first n
elements breaks
// should be inserted in order to optimize the code - I think
if(ov.mpt[k].seg_arr[l].segment_id != -1 &&
ov.mpt[k].seg_arr[l].segment_id ==
ov.ipd[j].sid.segment_id)
{
// Gotcha
v.push_back(Storable(ov.mpt[k].seg_arr[l].segment_id,
ov.mpt[k].pt,
ov.ipd[j].tp_data_size,
ov.ipd[j].rp_data_size));
}
}
}
}
}
}
}
}
void dump()
{
// iterate and print stuff here.
for(vector<Storable>::iterator it=v.begin();it!=v.end();it++)
{
cout << *it;
}
}
~foo_w() {}
};
int main()
{
overall ov;
ov.ipd[0][0].sid.segment_id = 0;
ov.ipd[0][0].tp_data_size = 0x100;
ov.ipd[0][0].rp_data_size = 0x200;
ov.ipd[1][0].sid.segment_id = 3;
ov.ipd[1][0].tp_data_size = 0x200;
ov.ipd[1][0].rp_data_size = 0x200;
ov.mpt[0].seg_arr[0].segment_id = 0;
ov.mpt[0].seg_arr[1].segment_id = 1;
ov.mpt[0].seg_arr[2].segment_id = 2;
ov.mpt[0].pt = FFT;
ov.mpt[1].seg_arr[0].segment_id = 3;
ov.mpt[1].pt = IFFT;
foo_w f(ov); // now transform the c style struct into something
meaningful
f.dump();
}