STL Help with Store/Retrieve .. Maps

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

    STL Help with Store/Retrieve .. Maps

    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[I].Data = X;
    Elements[I].Defined = True;
    }

    void Vector::Retriev e(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[I].Defined;
    if (Success)
    X = Elements[I].Data;
    }

    };
  • sam

    #2
    Re: STL Help with Store/Retrieve .. Maps

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

    "forums_mp" <forums__mp@bel lsouth.net> wrote in message
    news:c9d2134e.0 310051450.15173 b15@posting.goo gle.com...[color=blue]
    > 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[I].Data = X;
    > Elements[I].Defined = True;
    > }
    >
    > void Vector::Retriev e(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[I].Defined;
    > if (Success)
    > X = Elements[I].Data;
    > }
    >
    > };[/color]


    Comment

    • Cy Edmunds

      #3
      Re: STL Help with Store/Retrieve .. Maps

      "forums_mp" <forums__mp@bel lsouth.net> wrote in message
      news:c9d2134e.0 310051450.15173 b15@posting.goo gle.com...[color=blue]
      > 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[I].Data = X;
      > Elements[I].Defined = True;
      > }
      >
      > void Vector::Retriev e(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[I].Defined;
      > if (Success)
      > X = Elements[I].Data;
      > }
      >
      > };[/color]

      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<RCV D_MSG> m_vec;

      public:
      void store(RCVD_MSG msg)
      {
      m_vvec.push_bac k(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_M SG1> m1;
      messages<RCVD_M SG2> m2;

      <untested code>

      --
      Cy



      Comment

      Working...