class MapClass holds copies of class DataStruct so that instances of class Action only have to store the key value associated with the DataStruct:
struct DataStruct
{
friend bool operator< (DataStruct const &lhs, DataStruct const &rhs)
{
if (lhs.xData < rhs.xData)
return true;
else if (lhs.xData > rhs.xData)
return false;
else if (lhs.yData < rhs.yData)
return true;
else if (lhs.yData > rhs.yData)
return false;
else
return false;
}
double xData,yData;
int keyVal;
};
class MapClass
{
std::map<int,DataStruct> dsMap;
std::set<DataStruct> dsSet;
public:
int AddDataStruct(DataStruct &ds)
{
// Check to see if ds already entered
if (dsSet.count(ds) > 0)
{
return ds.keyVal;
}
else
{
ds.keyVal = (int)dsMap.size()+1;
dsMap.insert(pair<int,DataStruct>(ds.keyVal,ds));
return ds.keyVal;
}
}
};
class Action
{
int dataKey;
// member functions do stuff
};
This way I can have multiple instances of Action using the same copy of DataStruct while using the memory of only one DataStruct. And when I print to file all instances of Action are easy to compare visually as I only have one keyVal to look at in a csv file.
My question is this, is there a more elegant way to accomplish the same thing? A better data container?