/* * Copyright (c) 2008 Sean C. Rhea (srhea@srhea.net) * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., 51 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "RideMetric.h" #include "Zones.h" RideMetricFactory *RideMetricFactory::_instance; QVector RideMetricFactory::noDeps; QHash RideMetric::computeMetrics(const RideFile *ride, const Zones *zones, const QStringList &metrics) { int zoneRange = zones->whichRange(ride->startTime().date()); const RideMetricFactory &factory = RideMetricFactory::instance(); QStringList todo = metrics; QHash done; while (!todo.isEmpty()) { QString symbol = todo.takeFirst(); if (!factory.haveMetric(symbol)) continue; const QVector &deps = factory.dependencies(symbol); bool ready = true; foreach (QString dep, deps) { if (!done.contains(dep)) { ready = false; if (!todo.contains(dep)) todo.append(dep); } } if (ready) { RideMetric *m = factory.newMetric(symbol); if (ride->metricOverrides.contains(symbol)) m->override(ride->metricOverrides.value(symbol)); else m->compute(ride, zones, zoneRange, done); done.insert(symbol, m); } else { if (!todo.contains(symbol)) todo.append(symbol); } } QHash result; foreach (QString symbol, metrics) { result.insert(symbol, QSharedPointer(done.value(symbol))); done.remove(symbol); } foreach (QString symbol, done.keys()) delete done.value(symbol); return result; }