Skip to content

Instantly share code, notes, and snippets.

@joboccara
joboccara / sp_abstraction.cpp
Last active January 27, 2017 08:24
Code for article on SimpleProgrammer.com: Respecting Levels of Abstraction
....
static void formatValue(V&);
....
void saveIndex(Index const& index)
{
if (index.hasID() && index.isQuoted() && index.isLiquid())
{
...
void saveIndex(const Index& index)
{
if (isValid(index))
{
...
class Location
{
public:
double distanceTo(const Location& other) const;
...
};
class GeographicalAttributes
{
public:
int computeNumberOfBreaks(const std::vector<City>& route)
{
static const double MaxDistance = 100;
int nbBreaks = 0;
for (std::vector<City>::const_iterator it1 = route.begin(), it2 = route.end();
it1 != route.end();
it2 = it1, ++it1)
{
if (it2 != route.end())
for (std::vector<City>::const_iterator it1 = route.begin(), it2 = route.end();
it1 != route.end();
it2 = it1, ++it1)
{
if (it2 != route.end())
{
it1->getGeographicalAttributes().getLocation().distanceTo(
it2->getGeographicalAttributes().getLocation()) > MaxDistance
int nbBreaks = 0;
for (...)
{
if(...)
{
++nbBreaks;
}
}
return nbBreaks;
class FartherThan
{
public:
explicit FartherThan(double distance) : m_distance(distance) {}
bool operator()(const std::pair<City, City>& cities)
{
return cities.first.getGeographicalAttributes().getLocation().distanceTo(
cities.second.getGeographicalAttributes().getLocation()) > m_distance;
}
private:
int computeNumberOfBreaks(const std::vector<City>& route)
{
static const double MaxDistance = 100;
return count_if(consecutive(route), FartherThan(MaxDistance));
}