Connecting Tech Pros Worldwide Help | Site Map

STL Help with Store/Retrieve .. Maps

  #1  
Old July 19th, 2005, 07:48 PM
forums_mp
Guest
 
Posts: n/a
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::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[i].Defined;
if (Success)
X = Elements[i].Data;
}

};
  #2  
Old July 19th, 2005, 07:48 PM
sam
Guest
 
Posts: n/a

re: STL Help with Store/Retrieve .. Maps


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

"forums_mp" <forums__mp@bellsouth.net> wrote in message
news:c9d2134e.0310051450.15173b15@posting.google.c om...[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::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[i].Defined;
> if (Success)
> X = Elements[i].Data;
> }
>
> };[/color]


  #3  
Old July 19th, 2005, 07:48 PM
Cy Edmunds
Guest
 
Posts: n/a

re: STL Help with Store/Retrieve .. Maps


"forums_mp" <forums__mp@bellsouth.net> wrote in message
news:c9d2134e.0310051450.15173b15@posting.google.c om...[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::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[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<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/


Closed Thread