suggestions on improving source

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ma740988

    #1

    suggestions on improving source

    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::ostream;

    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(tp In)
    , rp_data_size(rp In)
    , tp_offset(tp_of fset_glob)
    , rp_offset(rp_of fset_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_of fset_glob=0xC00 0000; // known initial value
    unsigned int Storable::rp_of fset_glob=0xF00 0000; // 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_proce ssors;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[i][j].tp_data_size !=0 || ov.ipd[i][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[i][j].sid.segment_id )
    {
    // Gotcha
    v.push_back(Sto rable(ov.mpt[k].seg_arr[l].segment_id,
    ov.mpt[k].pt,
    ov.ipd[i][j].tp_data_size,
    ov.ipd[i][j].rp_data_size)) ;
    }
    }
    }
    }
    }
    }
    }
    }

    void dump()
    {
    // iterate and print stuff here.
    for(vector<Stor able>::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();
    }

  • Victor Bazarov

    #2
    Re: suggestions on improving source

    ma740988 wrote:[color=blue]
    > 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.
    >
    > [..]
    > friend ostream &operator<< (ostream &os, const Storable s)[/color]

    Didn't you mean to pass by reference?

    friend ostream &operator<< (ostream &os, const Storable & s)
    [color=blue]
    > {
    > 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_of fset_glob=0xC00 0000; // known initial value
    > unsigned int Storable::rp_of fset_glob=0xF00 0000; // known initial value
    >
    > // requirements are here in foo_w
    > class foo_w
    > {
    > private:
    > vector<Storable > v;
    > public:
    > foo_w(const overall ov)[/color]

    Top-level 'const' is usually meaningless. Did you mean to pass by
    reference again?

    foo_w(const overall & ov)
    [color=blue]
    > {
    > [..][/color]

    V

    Comment

    • ma740988

      #3
      Re: suggestions on improving source


      Greetings Vic[color=blue]
      >
      > Top-level 'const' is usually meaningless. Did you mean to pass by
      > reference again?[/color]

      I tend to miss out on const correctness at times. Thanks
      [color=blue]
      > foo_w(const overall & ov)
      >[color=green]
      > > {
      > > [..][/color]
      >
      > V[/color]

      Comment

      Working...