DIY 3.0 data-parallel out-of-core C++ library

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Examples

- File List

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;
18 typedef critical_resource<int, recursive_mutex> CInt;
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 elements_.size() - 1; }
43 void* release(int i) { void* e = get(i); elements_[i] = 0; return e; }
44
45 void* find(int i) const { return elements_[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_[i] = 0; } else if (external_[i] != -1) storage_->destroy(external_[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(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_[i] = storage_->put(e, save_);
99
100 destroy_(e);
101 elements_[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_[i], e, load_);
115 elements_[i] = e;
116 external_[i] = -1;
117
118 ++(*in_memory_.access());
119 }
120
121 #endif
diy::ExternalStorage
Definition: storage.hpp:41
diy::save
void save(BinaryBuffer &bb, const T &x)
Saves x to bb by calling diy::Serialization<T>::save(bb,x).
Definition: serialization.hpp:102
diy::Collection
Definition: collection.hpp:13
diy::critical_resource< int, recursive_mutex >