DIY  3.0
data-parallel out-of-core C++ library
 All Classes Namespaces Functions Typedefs Groups Pages
collection.hpp
1 #ifndef DIY_COLLECTION_HPP
2 #define DIY_COLLECTION_HPP
3 
4 #include <vector>
5 
6 #include "serialization.hpp"
7 #include "storage.hpp"
8 #include "thread.hpp"
9 
10 
11 namespace diy
12 {
13  class Collection
14  {
15  public:
16  typedef void* Element;
17  typedef std::vector<Element> Elements;
19 
20  typedef void* (*Create)();
21  typedef void (*Destroy)(void*);
22  typedef detail::Save Save;
23  typedef detail::Load Load;
24 
25  public:
26  Collection(Create create__,
27  Destroy destroy__,
28  ExternalStorage* storage__,
29  Save save__,
30  Load load__):
31  create_(create__),
32  destroy_(destroy__),
33  storage_(storage__),
34  save_(save__),
35  load_(load__),
36  in_memory_(0) {}
37 
38  size_t size() const { return elements_.size(); }
39  const CInt& in_memory() const { return in_memory_; }
40  inline void clear();
41 
42  int add(Element e) { elements_.push_back(e); external_.push_back(-1); ++(*in_memory_.access()); return static_cast<int>(elements_.size()) - 1; }
43  void* release(int i) { void* e = get(i); elements_[static_cast<size_t>(i)] = 0; return e; }
44 
45  void* find(int i) const { return elements_[static_cast<size_t>(i)]; } // possibly returns 0, if the element is unloaded
46  void* get(int i) { if (!find(i)) load(i); return find(i); } // loads the element first, and then returns its address
47 
48  int available() const { int i = 0; for (; i < (int)size(); ++i) if (find(i) != 0) break; return i; }
49 
50  inline void load(int i);
51  inline void unload(int i);
52 
53  Create creator() const { return create_; }
54  Destroy destroyer() const { return destroy_; }
55  Load loader() const { return load_; }
56  Save saver() const { return save_; }
57 
58  void* create() const { return create_(); }
59  void destroy(int i) { if (find(i)) { destroy_(find(i)); elements_[static_cast<size_t>(i)] = 0; } else if (external_[static_cast<size_t>(i)] != -1) storage_->destroy(external_[static_cast<size_t>(i)]); }
60 
61  bool own() const { return destroy_ != 0; }
62 
63  ExternalStorage* storage() const { return storage_; }
64 
65  private:
66  Create create_;
67  Destroy destroy_;
68  ExternalStorage* storage_;
69  Save save_;
70  Load load_;
71 
72  Elements elements_;
73  std::vector<int> external_;
74  CInt in_memory_;
75  };
76 }
77 
78 void
79 diy::Collection::
80 clear()
81 {
82  if (own())
83  for (size_t i = 0; i < size(); ++i)
84  destroy(static_cast<int>(i));
85  elements_.clear();
86  external_.clear();
87  *in_memory_.access() = 0;
88 }
89 
90 void
91 diy::Collection::
92 unload(int i)
93 {
94  //BinaryBuffer bb;
95  void* e = find(i);
96  //save_(e, bb);
97  //external_[i] = storage_->put(bb);
98  external_[static_cast<size_t>(i)] = storage_->put(e, save_);
99 
100  destroy_(e);
101  elements_[static_cast<size_t>(i)] = 0;
102 
103  --(*in_memory_.access());
104 }
105 
106 void
107 diy::Collection::
108 load(int i)
109 {
110  //BinaryBuffer bb;
111  //storage_->get(external_[i], bb);
112  void* e = create_();
113  //load_(e, bb);
114  storage_->get(external_[static_cast<size_t>(i)], e, load_);
115  elements_[static_cast<size_t>(i)] = e;
116  external_[static_cast<size_t>(i)] = -1;
117 
118  ++(*in_memory_.access());
119 }
120 
121 #endif
Definition: storage.hpp:38
Definition: collection.hpp:13