TPIE

2362a60
store.h
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2014, The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 
20 #ifndef __TPIE_PIPELINING_STORE_H__
21 #define __TPIE_PIPELINING_STORE_H__
22 #include <tpie/memory.h>
23 namespace tpie {
24 
25 namespace bits {
26 template <typename T> struct remove_pointer {typedef T type;};
27 template <typename T> struct remove_pointer<T*> {typedef T type;};
28 
29 template <typename pred_t, typename specific_store_t>
30 class store_pred {
31 private:
32  typedef typename specific_store_t::store_type store_type;
33  pred_t pred;
34 public:
35  typedef store_type first_argument_type;
36  typedef store_type second_argument_type;
37  typedef bool result_type;
38 
39  store_pred(pred_t pred): pred(pred) {}
40 
41  bool operator()(const store_type & lhs, const store_type & rhs) const {
42  return pred(
43  specific_store_t::store_as_element(lhs),
44  specific_store_t::store_as_element(rhs));
45  }
46 
47  bool operator()(const store_type & lhs, const store_type & rhs) {
48  return pred(
49  specific_store_t::store_as_element(lhs),
50  specific_store_t::store_as_element(rhs));
51  }
52 };
53 } //namespace bits
54 
60 struct plain_store {
61  template <typename outer_t>
62  struct element_type {
63  typedef outer_t type;
64  };
65 
66  template <typename element_t>
67  struct specific {
68  // Type of element we compare and store in file_stream
69  typedef element_t element_type;
70  // Type we store in internal sort buffer
71  typedef element_t store_type;
72  // Type we accept in push and return in pop
73  typedef element_t outer_type;
74  // Internal memory usage per element in internal sort
75  static const size_t item_size = sizeof(element_t);
76 
77  element_type store_to_element(const store_type & e) {return e;}
78  store_type element_to_store(const element_type & e) {return e;}
79  static const element_type & store_as_element(const store_type & e) {return e;}
80  store_type outer_to_store(const outer_type & e) {return e;}
81  outer_type store_to_outer(const store_type & e) {return e;}
82  };
83 
84  template <typename element_t>
85  specific<element_t> get_specific() {return specific<element_t>();}
86 };
87 
98  template <typename outer_t>
99  struct element_type {
100  typedef typename bits::remove_pointer<outer_t>::type type;
101  };
102 
103  template <typename element_t>
104  struct specific {
105  typedef element_t element_type;
106  typedef element_t * store_type;
107  typedef element_t * outer_type;
108 
109  static const size_t item_size = sizeof(element_t) + sizeof(store_type);
110 
111  element_type store_to_element(const store_type e) {
112  element_type ans=*e;
113  tpie_delete(e);
114  return ans;
115  }
116  store_type element_to_store(const element_type & e) {return tpie_new<element_type>(e);}
117  static const element_type & store_as_element(const store_type e) {return *e;}
118  store_type outer_to_store(const outer_type & e) {return e;}
119  outer_type store_to_outer(const store_type & e) {return e;}
120  };
121 
122  template <typename element_t>
123  specific<element_t> get_specific() {return specific<element_t>();}
124 };
125 
132  template <typename outer_t>
133  struct element_type {
134  typedef typename outer_t::element_type type;
135  };
136 
137  template <typename element_t>
138  struct specific {
139  typedef element_t element_type;
140  typedef tpie::unique_ptr<element_t> store_type;
141  typedef tpie::unique_ptr<element_t> outer_type;
142 
143  static const size_t item_size = sizeof(element_t) + sizeof(store_type);
144 
145  element_type store_to_element(store_type e) {
146  return *e;
147  }
148  store_type element_to_store(const element_type & e) {
149  return outer_type(tpie_new<element_type>(e));
150  }
151  static const element_type & store_as_element(const store_type & e) {return *e;}
152  store_type outer_to_store(outer_type e) {return std::move(e);}
153  outer_type store_to_outer(store_type e) {return std::move(e);}
154  };
155 
156  template <typename element_t>
157  specific<element_t> get_specific() {return specific<element_t>();}
158 };
159 
167  template <typename outer_t>
168  struct element_type {
169  typedef outer_t type;
170  };
171 
172  template <typename element_t>
173  struct specific {
174  typedef element_t element_type;
175  typedef element_t * store_type;
176  typedef element_t outer_type;
177 
178  static const size_t item_size = sizeof(element_t) + sizeof(store_type);
179 
180  element_type store_to_element(const store_type e) {
181  element_type ans=*e;
182  tpie_delete(e);
183  return ans;
184  }
185  store_type element_to_store(const element_type & e) {return tpie_new<element_type>(e);}
186  static const element_type & store_as_element(const store_type e) {return *e;}
187  store_type outer_to_store(const outer_type & e) {return tpie_new<element_type>(e);}
188  outer_type store_to_outer(const store_type & e) {
189  outer_type ans=*e;
190  tpie_delete(e);
191  return ans;
192  }
193  };
194 
195  template <typename element_t>
196  specific<element_t> get_specific() {return specific<element_t>();}
197 };
198 
199 namespace bits {
200 
201 template <typename element_t, bool = (sizeof(element_t) > 256)>
203 
204 template <typename element_t>
205 struct dynamic_specific_selector<element_t, true>: public pointer_store::specific<element_t> {};
206 
207 } //namespace bits
208 
219  template <typename outer_t>
220  struct element_type {
221  typedef outer_t type;
222  };
223 
224  template <typename element_t>
225  struct specific: public bits::dynamic_specific_selector<element_t> {};
226 
227  template <typename element_t>
228  specific<element_t> get_specific() {return specific<element_t>();}
229 };
230 
231 
232 typedef dynamic_store default_store;
233 
234 } //namespace tpie
235 
236 #endif //__TPIE_PIPELINING_SPECIFIC_POINTER_H__
Memory management subsystem.
Sort elements in tpie pointers.
Definition: store.h:97
Plain old store.
Definition: store.h:60
std::unique_ptr< T, tpie_deleter > unique_ptr
like std::unique_ptr, but delete the object with tpie_delete.
Definition: memory.h:338
void tpie_delete(T *p)
Delete an object allocated with tpie_new.
Definition: memory.h:301
Fantastic store strategy.
Definition: store.h:218
Sort elements in tpie unique pointers.
Definition: store.h:131
Sort elements using pointer indirection.
Definition: store.h:166