OKlibrary  0.2.1.6
ResultProcessing.hpp
Go to the documentation of this file.
00001 // Oliver Kullmann, 18.5.2005 (Swansea)
00002 /* Copyright 2005 - 2007 Oliver Kullmann
00003 This file is part of the OKlibrary. OKlibrary is free software; you can redistribute
00004 it and/or modify it under the terms of the GNU General Public License as published by
00005 the Free Software Foundation and included in this library; either version 3 of the
00006 License, or any later version. */
00007 
00020 #ifndef RESULTPROCESSING_ppOgB535
00021 #define RESULTPROCESSING_ppOgB535
00022 
00023 #include <vector>
00024 #include <set>
00025 #include <map>
00026 #include <utility>
00027 #include <algorithm>
00028 #include <iterator>
00029 #include <ostream>
00030 #include <cassert>
00031 
00032 #include <OKlib/General/FunctionHandling.hpp>
00033 
00034 #include <OKlib/Structures/Sets/SetAlgorithms/BasicSetOperations.hpp>
00035 
00036 #include <OKlib/Experimentation/Competition/SingleResult.hpp>
00037 #include <OKlib/Experimentation/Competition/ParsingSingleResult.hpp>
00038 #include <OKlib/Experimentation/Competition/ParsingResultSequences.hpp>
00039 
00040 namespace OKlib {
00041   namespace SATCompetition {
00042 
00043     class ResultNode;
00044 
00045     typedef std::set<const ResultNode*> SetResultNodesP;
00046 
00047     typedef std::map<SuperSeries, SetResultNodesP*> MapSuperSeries;
00048     typedef std::map<Series, SetResultNodesP*> MapSeries;
00049     typedef std::map<Benchmark, SetResultNodesP*> MapBenchmark;
00050     typedef std::map<Solver, SetResultNodesP*> MapSolver;
00051     typedef std::map<SATStatus, SetResultNodesP*> MapSATStatus;
00052     typedef std::map<TimeOut, SetResultNodesP*> MapTimeOut;
00053 
00054     struct ResultNode {
00055       const ResultBasis* rb;
00056 
00057       MapSuperSeries::const_iterator m_sup_ser_it;
00058       MapSeries::const_iterator m_ser_it;
00059       MapBenchmark::const_iterator m_bench_it;
00060       MapSolver::const_iterator m_solv_it;
00061       MapSATStatus::const_iterator m_sat_stat_it;
00062       MapTimeOut::const_iterator m_tmo_it;
00063 
00064       explicit ResultNode(const ResultBasis* rb) : rb(rb) {}
00065     };
00066 
00067     inline std::ostream& operator <<(std::ostream& out, const ResultNode& r) {
00068       return out << *(r.rb);
00069     }
00070 
00071     typedef std::vector<const ResultNode*> VectorResultNodesP;
00072     
00073     inline std::ostream& operator <<(std::ostream& out, const VectorResultNodesP& vec) {
00074       typedef VectorResultNodesP::const_iterator const_iterator;
00075       const const_iterator end(vec.end());
00076       for (const_iterator i = vec.begin(); i != end; ++i)
00077         out << **i << "\n";
00078       return out;
00079     }
00080 
00081 
00082     // #####################################################
00083 
00095     template <typename ResultIterator>
00096     class ResultDatabase {
00097 
00098       ResultDatabase& operator =(const ResultDatabase&);
00099 
00100       MapSuperSeries map_sup_ser;
00101       MapSeries map_ser;
00102       MapBenchmark map_bench;
00103       MapSolver map_solv;
00104       MapSATStatus map_sat_stat;
00105       MapTimeOut map_tmo;
00106 
00107       typedef std::vector<ResultNode> VectorResultNodes;
00108       VectorResultNodes result_collection;
00109       typedef VectorResultNodes::size_type number_results_type;
00110       number_results_type number_results_;
00111       typedef VectorResultNodes::iterator result_collection_iterator;
00112       typedef VectorResultNodes::const_iterator result_collection_const_iterator;
00113 
00114     public :
00115 
00116       ResultDatabase(ResultIterator begin, const ResultIterator end) {
00117         for (; begin != end; ++begin)
00118           result_collection.push_back(ResultNode(&*begin));
00119         number_results_ = result_collection.size();
00120         query_result.resize(number_results_);
00121         update_all_maps();
00122       }
00123 
00124       ResultDatabase(const ResultDatabase& other) : result_collection(other.result_collection), number_results_(other.number_results_) {
00125         query_result.resize(number_results_);
00126         update_all_maps();
00127       }
00128 
00129       ~ResultDatabase() {
00130         delete_all_map_values();
00131       }
00132 
00133       number_results_type number_results() const { return number_results_; }
00134 
00135       const MapSuperSeries& super_series() const { return map_sup_ser; }
00136       const MapSeries& series() const { return map_ser; }
00137       const MapBenchmark& benchmark() const { return map_bench; }
00138       const MapSolver& solver() const { return map_solv; }
00139       const MapSATStatus& sat_status() const { return map_sat_stat; }
00140       const MapTimeOut& time_out() const { return map_tmo; }
00141 
00142       typedef std::vector<const SetResultNodesP*> VectorOfSetsP;
00143       mutable VectorOfSetsP vector_of_sets;
00144 
00145     private :
00146       mutable VectorResultNodesP query_result;
00147     public :
00148 
00149       const VectorResultNodesP& intersection() const {
00150         query_result.clear();
00151         const VectorOfSetsP::size_type size_vector_of_sets = vector_of_sets.size();
00152         if (size_vector_of_sets == 0) {
00153           const result_collection_const_iterator end = result_collection.end();
00154           for (result_collection_const_iterator i = result_collection.begin(); i != end; ++i)
00155             query_result.push_back(&*i);
00156         }
00157         else {
00158           typedef SetResultNodesP::iterator set_iterator;
00159           typedef std::pair<set_iterator, set_iterator> Range;
00160           typedef std::vector<Range> VectorRanges;
00161           VectorRanges vr;
00162           vr.reserve(size_vector_of_sets);
00163           typedef VectorOfSetsP::const_iterator vector_sets_iterator;
00164           const vector_sets_iterator end = vector_of_sets.end();
00165           for (vector_sets_iterator i = vector_of_sets.begin(); i != end; ++i) {
00166             const SetResultNodesP* const set_pointer = *i;
00167             vr.push_back(Range(set_pointer -> begin(), set_pointer -> end()));
00168           }
00169           assert(vr.size() == size_vector_of_sets);
00170           ::OKlib::SetAlgorithms::intersection_sets(vr.begin(), vr.end(), std::back_inserter(query_result));
00171         }
00172         return query_result;
00173       }
00174 
00175       void restrict() {
00176         result_collection.erase(
00177                                 std::transform(query_result.begin(), query_result.end(), result_collection.begin(), FunctionHandling::Dereferenciation<ResultNode>()),
00178                                 result_collection.end());
00179         number_results_ = result_collection.size();
00180         query_result.resize(number_results_);
00181         delete_all_map_values();
00182         delete_all_maps();
00183         update_all_maps();
00184       }
00185 
00186     private :
00187 
00188       template <class Map>
00189       void delete_values(Map& m) {
00190         std::for_each(m.begin(), m.end(), FunctionHandling::DeleteObjectSecond());
00191       }
00192 
00193       void update_all_maps() {
00194         const MapSuperSeries::iterator end_map_sup_ser(map_sup_ser.end());
00195         const MapSeries::iterator end_map_ser(map_ser.end());
00196         const MapBenchmark::iterator end_map_bench(map_bench.end());
00197         const MapSolver::iterator end_map_solv(map_solv.end());
00198         const MapSATStatus::iterator end_map_sat_stat(map_sat_stat.end());
00199         const MapTimeOut::iterator end_map_tmo(map_tmo.end());
00200 
00201         for (result_collection_iterator i = result_collection.begin(); i != result_collection.end(); ++i) {
00202           ResultNode& rn(*i);
00203           const ResultBasis* r(rn.rb);
00204           
00205           update_map<SuperSeries>(rn, r -> super_series(), map_sup_ser, rn.m_sup_ser_it, end_map_sup_ser);
00206           update_map<Series>(rn, r -> series(), map_ser, rn.m_ser_it, end_map_ser);
00207           update_map<Benchmark>(rn, r -> benchmark(), map_bench, rn.m_bench_it, end_map_bench);
00208           update_map<Solver>(rn, r -> solver(), map_solv, rn.m_solv_it, end_map_solv);
00209           update_map<SATStatus>(rn, r -> sat_status(), map_sat_stat, rn.m_sat_stat_it, end_map_sat_stat);
00210           update_map<TimeOut>(rn, r -> time_out(), map_tmo, rn.m_tmo_it, end_map_tmo);
00211         }
00212 
00213       }
00214 
00215       template <class ResultElement, class Map>
00216       void update_map(ResultNode& rn, const ResultElement e, Map& m, typename Map::const_iterator& m_it, const typename Map::iterator& end) {
00217         typedef typename Map::iterator iterator;
00218         const iterator it(m.find(e));
00219             if (it != end) {
00220               m_it = it;
00221               it -> second -> insert(&rn);
00222             }
00223             else {
00224               SetResultNodesP* set = new SetResultNodesP;
00225               set -> insert(&rn);
00226               m_it = m.insert(std::make_pair(e, set)).first;
00227             }
00228       }
00229 
00230       void delete_all_map_values() {
00231         delete_values(map_sup_ser);
00232         delete_values(map_ser);
00233         delete_values(map_bench);
00234         delete_values(map_solv);
00235         delete_values(map_sat_stat);
00236         delete_values(map_tmo);
00237       }
00238 
00239       void delete_all_maps() {
00240         map_sup_ser.clear();
00241         map_ser.clear();
00242         map_bench.clear();
00243         map_solv.clear();
00244         map_sat_stat.clear();
00245         map_tmo.clear();
00246       }
00247 
00248     };
00249 
00250     // ####################################################
00251 
00252     template <template <typename Result, typename CharT, typename ParseIterator, class ParserExtension> class ParserResult, class Result_ = Result, template <typename CharT, typename Parseiterator> class ParserExtension = ParserEmpty, template <typename Value> class Container = std::vector>
00253     struct Result_database_from_file {
00254 
00255       typedef Result_ result_type;
00256       typedef Copy_results_from_file_to_container<ParserResult, Container, result_type, ParserExtension> copy_type;
00257       typedef typename copy_type::container_type container_type;
00258       typedef typename container_type::const_iterator result_iterator_type;
00259       typedef ResultDatabase<result_iterator_type> database_type;
00260 
00261       container_type result_sequence;
00262       copy_type copy;
00263       database_type db;
00264 
00265       Result_database_from_file(const boost::filesystem::path& filename) : copy(filename, result_sequence), db(result_sequence.begin(), result_sequence.end()) {}
00266 
00267     private :
00268 
00269       Result_database_from_file(const Result_database_from_file&);
00270       Result_database_from_file operator =(const Result_database_from_file&);
00271    };
00272     
00273   }
00274 
00275 }
00276 
00277 #endif