00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00030 #ifndef DB_PLANNER_CACHING_NEIGHBOR_FINDER_H
00031 #define DB_PLANNER_CACHING_NEIGHBOR_FINDER_H
00032
00033 #include <string>
00034 #include <vector>
00035 #include "db_manager.h"
00036 #include "model.h"
00037 #include "neighbor_finder.h"
00038 using std::string;
00039 using std::vector;
00040
00041 namespace db_planner {
00042
00044
00049 class CachingNeighborFinder : public NeighborFinder<Model>{
00050 private:
00052 const DatabaseManager& db_manager_;
00054 const bool cache_results_;
00057 const string distance_function_name_;
00059 const NeighborFinder<Model>& neighbor_finder_;
00060 public:
00061 CachingNeighborFinder(
00062 const DatabaseManager& db_manager,
00063 const bool cache_results,
00064 const string& distance_function_name,
00065 const NeighborFinder<Model>& neighbor_finder = NeighborFinder<Model>())
00066 : db_manager_(db_manager),
00067 cache_results_(cache_results),
00068 distance_function_name_(distance_function_name),
00069 neighbor_finder_(neighbor_finder) { }
00071
00079 virtual bool Find(const Model& query,
00080 const int num_neighbors,
00081 vector<pair<Model*, double> >* neighbors) const {
00082
00083 if (db_manager_.GetNeighbors(query,
00084 distance_function_name_,
00085 num_neighbors,
00086 neighbors)) {
00087 return true;
00088 }
00089
00090 if (neighbor_finder_.Find(query, num_neighbors, neighbors)) {
00091
00092 if (cache_results_) {
00093 db_manager_.SaveNeighbors(query,
00094 distance_function_name_,
00095 *neighbors);
00096 }
00097 return true;
00098 }
00099 return false;
00100 }
00101 };
00102
00103
00104
00105 }
00106
00107 #endif // DB_PLANNER_CACHING_NEIGHBOR_FINDER_H_