JeroMiya
2007.07.05, 03:08 PM
I just finished writing a function that is, I think, one of the most awful pieces of code I've ever written, and I thought I'd share it with you guys.
I particularly like the set<string>::iterator j = i->second.begin()->second.begin();:wacko:
/**
* This function takes a mapping of the dataset which maps
* for each station and each date, a set of times for which
* there is valid data (ie "good times"). It then generates a mapping which maps
* for each station, a set of good times that exist for all dates
* in the dataset for that station.
* \param dmap This is the original dataset mapping, generated as the
* data was processed. it's in the form of:
* map<station, map<date, set<good times> > >
* \param goodNums This is the output mapping of stations to a set
* of good times that are present on all dates in the dataset.
* it is in the form of:
* map<station, set<good times> >
*/
void ProcessDataMap(map<string, map<string, set<string> > >& dmap,
map<string, set<string> >& goodNums)
{
map<string, map<string, set<string> > >::iterator i = dmap.begin();
goodNums.clear();
// for each station
while(i != dmap.end())
{
set<string> good;
if(i->second.size() > 0)
{
// add the first date's set of good numbers to the good pile
set<string>::iterator j = i->second.begin()->second.begin();
while(j != i->second.begin()->second.end())
{
good.insert(*j); // add the number
j++;
}
i++;
// now for the rest of dates' lists of good numbers, apply the following rule
// if a number in the good pile is not in the current list, remove it from the good pile
map<string, set<string> >::iterator k = i->second.begin();
k++; // skip the first one, we got it already
while(k != i->second.end())
{
// apply rule 1)
set<string>::iterator g = good.begin();
while(g != good.end())
{
// if the number isn't in the list, remove it from the good pile
if(k->second.find(*g) == k->second.end())
{
set<string>::iterator next = g; next++; // save the next spot
good.erase(g); // remove it from the good pile
g = next;
}
else
g++; // *g is in both the current date's good times and the good pile
}
}
}
}
}
I particularly like the set<string>::iterator j = i->second.begin()->second.begin();:wacko:
/**
* This function takes a mapping of the dataset which maps
* for each station and each date, a set of times for which
* there is valid data (ie "good times"). It then generates a mapping which maps
* for each station, a set of good times that exist for all dates
* in the dataset for that station.
* \param dmap This is the original dataset mapping, generated as the
* data was processed. it's in the form of:
* map<station, map<date, set<good times> > >
* \param goodNums This is the output mapping of stations to a set
* of good times that are present on all dates in the dataset.
* it is in the form of:
* map<station, set<good times> >
*/
void ProcessDataMap(map<string, map<string, set<string> > >& dmap,
map<string, set<string> >& goodNums)
{
map<string, map<string, set<string> > >::iterator i = dmap.begin();
goodNums.clear();
// for each station
while(i != dmap.end())
{
set<string> good;
if(i->second.size() > 0)
{
// add the first date's set of good numbers to the good pile
set<string>::iterator j = i->second.begin()->second.begin();
while(j != i->second.begin()->second.end())
{
good.insert(*j); // add the number
j++;
}
i++;
// now for the rest of dates' lists of good numbers, apply the following rule
// if a number in the good pile is not in the current list, remove it from the good pile
map<string, set<string> >::iterator k = i->second.begin();
k++; // skip the first one, we got it already
while(k != i->second.end())
{
// apply rule 1)
set<string>::iterator g = good.begin();
while(g != good.end())
{
// if the number isn't in the list, remove it from the good pile
if(k->second.find(*g) == k->second.end())
{
set<string>::iterator next = g; next++; // save the next spot
good.erase(g); // remove it from the good pile
g = next;
}
else
g++; // *g is in both the current date's good times and the good pile
}
}
}
}
}