STL Help with Store/Retrieve .. Maps

Discussion in 'C++' started by forums_mp, Oct 5, 2003.

  1. forums_mp

    forums_mp Guest

    I've got an STL class (see below) with two functions to store and
    retrieve data - msg structs.

    The "Store" function when called will copy the received message
    (depending on which message) into the appropriate array. For instance
    if I receive a RCVD_MSG1, I'll copy into msg1 [ 0 ]. Upon receipt of
    the next RCVD_MSG1. I'll copy into msg1 [ 1 ].
    The Retrieve function when called should retrieve the latest copy.
    ie. msg1 [ 0 ] or msg1 [ 1 ].

    I've made it this far and am unsure how best to implement this.
    One other thing. I suspect maps (not too familiar with maps but I
    vaguely recall seeing something similiar in a newsgroup recently .. )
    would be provide an more elegant approach but i'm unsure how to modify
    code to do this.

    Thanks

    ------------------

    #include <iostream.h>

    const Max = 20;
    const True = 1;
    const False = 0;

    struct {
    unsigned rcvd_item1 : 16;
    unsigned rcvd_item2 : 13;
    unsigned rcvd_item3 : 10;
    unsigned spare : 9;
    } RCVD_MSG1;

    struct {
    unsigned rcvd_item1 : 11;
    unsigned rcvd_item2 : 10;
    unsigned spare : 11;
    } RCVD_MSG2;

    RCVD_MSG2 msg2 [ 2 ];
    RCVD_MSG1 msg1 [ 2 ];


    template<class T> class Vector
    {
    struct
    {
    T Data; //field Data is type T ..
    // May need to make this T Data [ 2 ];

    int Defined; //field Defined is type int
    } Elements[Max]; //array of Max items

    public:
    Vector::Vector()
    //Creates an empty abstract array.
    {
    int Index;

    for (Index = 0; Index < Max; ++Index)
    Elements[Index].Defined = False;
    }

    void Vector::Init(T X)
    //Initialize all elements to X
    //Pre : Vector created and X defined.
    //Post: All elements set to X and marked as defined.
    {
    int Index;

    for (Index = 0; Index < Max; ++Index)
    {
    Elements[Index].Data = X;
    Elements[Index].Defined = True;
    }
    }

    void Vector::Store (T X, int I)
    //Stores X at position I in abstract array.
    //Pre : Vector created; X and I are defined.
    //Post: Elements[Index].Data is X and position I is defined.
    //Need to implement ping/pong approach of the stored sturcts.
    // i.e. first call to Store a RCVD_MSG1 would store at msg1 [ 0 ]
    // Next call would store at msg1 [ 1 ]
    {
    Elements.Data = X;
    Elements.Defined = True;
    }

    void Vector::Retrieve(T &X, int I, int &Success)
    //Copies the value stored at vector position I to X.
    //
    //Post: Returns value stored at position I through X and
    // set Success to True, if position defined; otherwise
    // Success set to False.
    // Retrieve the LATEST at all times .. msg1 [ 0 ] or msg1 [ 1 ];

    {
    Success = Elements.Defined;
    if (Success)
    X = Elements.Data;
    }

    };
    forums_mp, Oct 5, 2003
    #1
    1. Advertising

  2. forums_mp

    sam Guest

    I have read priority_queue is good for this type of operation.

    "forums_mp" <> wrote in message
    news:...
    > I've got an STL class (see below) with two functions to store and
    > retrieve data - msg structs.
    >
    > The "Store" function when called will copy the received message
    > (depending on which message) into the appropriate array. For instance
    > if I receive a RCVD_MSG1, I'll copy into msg1 [ 0 ]. Upon receipt of
    > the next RCVD_MSG1. I'll copy into msg1 [ 1 ].
    > The Retrieve function when called should retrieve the latest copy.
    > ie. msg1 [ 0 ] or msg1 [ 1 ].
    >
    > I've made it this far and am unsure how best to implement this.
    > One other thing. I suspect maps (not too familiar with maps but I
    > vaguely recall seeing something similiar in a newsgroup recently .. )
    > would be provide an more elegant approach but i'm unsure how to modify
    > code to do this.
    >
    > Thanks
    >
    > ------------------
    >
    > #include <iostream.h>
    >
    > const Max = 20;
    > const True = 1;
    > const False = 0;
    >
    > struct {
    > unsigned rcvd_item1 : 16;
    > unsigned rcvd_item2 : 13;
    > unsigned rcvd_item3 : 10;
    > unsigned spare : 9;
    > } RCVD_MSG1;
    >
    > struct {
    > unsigned rcvd_item1 : 11;
    > unsigned rcvd_item2 : 10;
    > unsigned spare : 11;
    > } RCVD_MSG2;
    >
    > RCVD_MSG2 msg2 [ 2 ];
    > RCVD_MSG1 msg1 [ 2 ];
    >
    >
    > template<class T> class Vector
    > {
    > struct
    > {
    > T Data; //field Data is type T ..
    > // May need to make this T Data [ 2 ];
    >
    > int Defined; //field Defined is type int
    > } Elements[Max]; //array of Max items
    >
    > public:
    > Vector::Vector()
    > //Creates an empty abstract array.
    > {
    > int Index;
    >
    > for (Index = 0; Index < Max; ++Index)
    > Elements[Index].Defined = False;
    > }
    >
    > void Vector::Init(T X)
    > //Initialize all elements to X
    > //Pre : Vector created and X defined.
    > //Post: All elements set to X and marked as defined.
    > {
    > int Index;
    >
    > for (Index = 0; Index < Max; ++Index)
    > {
    > Elements[Index].Data = X;
    > Elements[Index].Defined = True;
    > }
    > }
    >
    > void Vector::Store (T X, int I)
    > //Stores X at position I in abstract array.
    > //Pre : Vector created; X and I are defined.
    > //Post: Elements[Index].Data is X and position I is defined.
    > //Need to implement ping/pong approach of the stored sturcts.
    > // i.e. first call to Store a RCVD_MSG1 would store at msg1 [ 0 ]
    > // Next call would store at msg1 [ 1 ]
    > {
    > Elements.Data = X;
    > Elements.Defined = True;
    > }
    >
    > void Vector::Retrieve(T &X, int I, int &Success)
    > //Copies the value stored at vector position I to X.
    > //
    > //Post: Returns value stored at position I through X and
    > // set Success to True, if position defined; otherwise
    > // Success set to False.
    > // Retrieve the LATEST at all times .. msg1 [ 0 ] or msg1 [ 1 ];
    >
    > {
    > Success = Elements.Defined;
    > if (Success)
    > X = Elements.Data;
    > }
    >
    > };
    sam, Oct 6, 2003
    #2
    1. Advertising

  3. forums_mp

    Cy Edmunds Guest

    "forums_mp" <> wrote in message
    news:...
    > I've got an STL class (see below) with two functions to store and
    > retrieve data - msg structs.
    >
    > The "Store" function when called will copy the received message
    > (depending on which message) into the appropriate array. For instance
    > if I receive a RCVD_MSG1, I'll copy into msg1 [ 0 ]. Upon receipt of
    > the next RCVD_MSG1. I'll copy into msg1 [ 1 ].
    > The Retrieve function when called should retrieve the latest copy.
    > ie. msg1 [ 0 ] or msg1 [ 1 ].
    >
    > I've made it this far and am unsure how best to implement this.
    > One other thing. I suspect maps (not too familiar with maps but I
    > vaguely recall seeing something similiar in a newsgroup recently .. )
    > would be provide an more elegant approach but i'm unsure how to modify
    > code to do this.
    >
    > Thanks
    >
    > ------------------
    >
    > #include <iostream.h>
    >
    > const Max = 20;
    > const True = 1;
    > const False = 0;
    >
    > struct {
    > unsigned rcvd_item1 : 16;
    > unsigned rcvd_item2 : 13;
    > unsigned rcvd_item3 : 10;
    > unsigned spare : 9;
    > } RCVD_MSG1;
    >
    > struct {
    > unsigned rcvd_item1 : 11;
    > unsigned rcvd_item2 : 10;
    > unsigned spare : 11;
    > } RCVD_MSG2;
    >
    > RCVD_MSG2 msg2 [ 2 ];
    > RCVD_MSG1 msg1 [ 2 ];
    >
    >
    > template<class T> class Vector
    > {
    > struct
    > {
    > T Data; //field Data is type T ..
    > // May need to make this T Data [ 2 ];
    >
    > int Defined; //field Defined is type int
    > } Elements[Max]; //array of Max items
    >
    > public:
    > Vector::Vector()
    > //Creates an empty abstract array.
    > {
    > int Index;
    >
    > for (Index = 0; Index < Max; ++Index)
    > Elements[Index].Defined = False;
    > }
    >
    > void Vector::Init(T X)
    > //Initialize all elements to X
    > //Pre : Vector created and X defined.
    > //Post: All elements set to X and marked as defined.
    > {
    > int Index;
    >
    > for (Index = 0; Index < Max; ++Index)
    > {
    > Elements[Index].Data = X;
    > Elements[Index].Defined = True;
    > }
    > }
    >
    > void Vector::Store (T X, int I)
    > //Stores X at position I in abstract array.
    > //Pre : Vector created; X and I are defined.
    > //Post: Elements[Index].Data is X and position I is defined.
    > //Need to implement ping/pong approach of the stored sturcts.
    > // i.e. first call to Store a RCVD_MSG1 would store at msg1 [ 0 ]
    > // Next call would store at msg1 [ 1 ]
    > {
    > Elements.Data = X;
    > Elements.Defined = True;
    > }
    >
    > void Vector::Retrieve(T &X, int I, int &Success)
    > //Copies the value stored at vector position I to X.
    > //
    > //Post: Returns value stored at position I through X and
    > // set Success to True, if position defined; otherwise
    > // Success set to False.
    > // Retrieve the LATEST at all times .. msg1 [ 0 ] or msg1 [ 1 ];
    >
    > {
    > Success = Elements.Defined;
    > if (Success)
    > X = Elements.Data;
    > }
    >
    > };


    I don't see how a map would help. I think you could do everything you need
    with std::vector:

    #include <vector>
    #include <exception>

    template <typename RCVD_MSG>
    class messages
    {
    private:
    std::vector<RCVD_MSG> m_vec;

    public:
    void store(RCVD_MSG msg)
    {
    m_vvec.push_back(msg);
    }

    RCVD_MSG retrieve()
    {
    if (m_vec.size() == 0)
    throw std::exception("oops");
    return m_vec.back();
    }

    int size() const // check this before calling retrieve to avoid
    exceptions
    {
    return m_vec.size();
    }
    };

    messages<RCVD_MSG1> m1;
    messages<RCVD_MSG2> m2;

    <untested code>

    --
    Cy
    http://home.rochester.rr.com/cyhome/
    Cy Edmunds, Oct 6, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael H Lees

    STL maps and const keys

    Michael H Lees, Jun 25, 2003, in forum: C++
    Replies:
    3
    Views:
    5,100
    Thomas Matthews
    Jun 26, 2003
  2. Jorge Schramm
    Replies:
    7
    Views:
    373
    Jorge Schramm
    Oct 8, 2003
  3. Simon Elliott
    Replies:
    4
    Views:
    1,146
    Simon Elliott
    Mar 10, 2005
  4. Marcus
    Replies:
    2
    Views:
    579
    Marcus
    Dec 9, 2005
  5. æœã®æœ¨
    Replies:
    3
    Views:
    1,005
    Juha Nieminen
    Apr 18, 2012
Loading...

Share This Page