owlcpp  v0.3.3~
C++ library for working with OWL ontologies
 All Classes Namespaces Files Functions Macros Pages
map_triple.hpp
Go to the documentation of this file.
1 
6 #ifndef MAP_TRIPLE_HPP_
7 #define MAP_TRIPLE_HPP_
8 #include "boost/fusion/include/at.hpp"
9 #include "boost/fusion/include/for_each.hpp"
10 #include "boost/fusion/include/front.hpp"
11 #include "boost/fusion/include/mpl.hpp"
12 #include "boost/mpl/assert.hpp"
13 #include "boost/mpl/at.hpp"
14 #include "boost/mpl/fold.hpp"
15 #include "boost/mpl/if.hpp"
16 #include "boost/mpl/push_back.hpp"
17 #include "boost/mpl/vector.hpp"
18 #include "boost/mpl/vector_c.hpp"
19 
20 #include "owlcpp/rdf/detail/map_triple_config.hpp"
21 #include "owlcpp/rdf/detail/triple_index.hpp"
22 
23 namespace owlcpp{
24 
27 template<bool Index_subj, bool Index_pred, bool Index_obj, bool Index_doc>
28 class Map_triple {
29 
30  typedef typename
31  map_triple_detail::Store_config<Index_subj,Index_pred,Index_obj,Index_doc>
32  config;
33 
34  typedef typename config::store store;
35  typedef typename boost::mpl::front<store>::type main_store;
36  BOOST_MPL_ASSERT((boost::is_same<typename main_store::tag, map_triple_detail::Main_store_tag>));
37 
38  class Insert {
39  public:
40  Insert(Triple const& t) : t_(t) {}
41  template<class Ind> void operator()(Ind& i) const {i.add(t_);}
42  private:
43  Triple const& t_;
44  };
45 
46  struct Clear {
47  template<class Ind> void operator()(Ind& i) const {i.clear();}
48  };
49 
50  class Erase {
51  public:
52  Erase(Triple const& t) : t_(t) {}
53  template<class Ind> void operator()(Ind& i) const {i.erase(t_);}
54  private:
55  const Triple t_;
56  };
57 
58 public:
59  typedef typename main_store::const_iterator const_iterator;
60  typedef typename main_store::const_iterator iterator;
61 
68  template<class Subj, class Pred, class Obj, class Doc> struct result {
69  typedef typename
70  map_triple_detail::Search_config<config,Subj,Pred,Obj,Doc>::range
71  type;
72  };
73 
81  template<bool Subj, bool Pred, bool Obj, bool Doc>
82  class result_b {
83  typedef typename map_triple_detail::Deduce_args<Subj,Pred,Obj,Doc>::type
84  q_args;
85 
86  public:
87  typedef typename result<
88  typename boost::mpl::at_c<q_args,0>::type,
89  typename boost::mpl::at_c<q_args,1>::type,
90  typename boost::mpl::at_c<q_args,2>::type,
91  typename boost::mpl::at_c<q_args,3>::type
92  >::type type;
93  };
94 
95 
97  std::size_t size() const {return boost::fusion::front(store_).get_range().size();}
98  bool empty() const {return boost::fusion::front(store_).get_range().empty();}
99  const_iterator begin() const {return boost::fusion::front(store_).get_range().begin();}
100  const_iterator end() const {return boost::fusion::front(store_).get_range().end();}
101 
102  void clear() {
103  Clear c;
104  boost::fusion::for_each(store_, c);
105  }
106 
111  void erase(Triple const& t) {
112  Erase e(t);
113  boost::fusion::for_each(store_, e);
114  }
115 
118  void insert(
119  const Node_id subj,
120  const Node_id pred,
121  const Node_id obj,
122  const Doc_id doc
123  ) {
124  const Triple t = Triple::make(subj, pred, obj, doc);
125  Insert ins(t);
126  boost::fusion::for_each(store_, ins);
127  }
128 
129 
155  template<class Subj, class Pred, class Obj, class Doc>
156  typename result<Subj,Pred,Obj,Doc>::type
157  find(const Subj subj, const Pred pred, const Obj obj, const Doc doc) const {
158  /*
159  - find search index
160  - form predicate
161  - get index iterator range
162  - make filter iterator range
163  */
164  typedef map_triple_detail::Search_config<config,Subj,Pred,Obj,Doc> search_config;
165  typedef typename search_config::index_num index_num;
166  typedef typename boost::mpl::at<store, index_num>::type index_t;
167  index_t const& index = boost::fusion::at<index_num>(store_);
168  typedef typename index_t::const_range range1_t;
169  const range1_t r1 = map_triple_detail::Search_range1<index_t>::get(index,subj,pred,obj,doc);
170  return search_config::search::get(r1, subj, pred, obj, doc);
171  }
172 
173 private:
174  store store_;
175 
176 };
177 
178 }//namespace owlcpp
179 #endif /* MAP_TRIPLE_HPP_ */