summaryrefslogtreecommitdiff
path: root/include/erebos/set.h
blob: f625cb067d9719835cede62776a997597559c628 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#pragma once

#include <erebos/merge.h>
#include <erebos/storage.h>

namespace erebos
{

class SetViewBase;
template<class T> class SetView;

class SetBase
{
protected:
	struct Priv;

	SetBase();
	SetBase(const vector<Ref> &);
	SetBase(shared_ptr<const Priv>);
	
	shared_ptr<const Priv> add(Storage &, const vector<Ref> &) const;

	vector<vector<Ref>> toList() const;

public:
	vector<Digest> digests() const;

protected:
	shared_ptr<const Priv> p;
};

template<class T>
class Set : public SetBase
{
	Set(shared_ptr<const Priv> p): SetBase(p) {};
public:
	Set() = default;
	Set(const vector<Ref> & refs): SetBase(move(refs)) {}
	Set(const Set<T> &) = default;
	Set(Set<T> &&) = default;
	Set & operator=(const Set<T> &) = default;
	Set & operator=(Set<T> &&) = default;

	static Set<T> load(const vector<Ref> & refs) { return Set<T>(move(refs)); }

	Set<T> add(Storage &, const T &) const;

	template<class F>
	SetView<T> view(F && cmp) const;
};

template<class T>
class SetView
{
public:
	template<class F>
	SetView(F && cmp, const vector<vector<Ref>> & refs);

	typename vector<T>::const_iterator begin() const { return items.begin(); }
	typename vector<T>::const_iterator end() const { return items.end(); }

private:
	vector<T> items;
};

template<class T>
Set<T> Set<T>::add(Storage & st, const T & x) const
{
	return Set<T>(SetBase::add(st, storedRefs(Mergeable<T>::components(x))));
}

template<class T>
template<class F>
SetView<T> Set<T>::view(F && cmp) const
{
	return SetView<T>(std::move(cmp), toList());
}

template<class T>
template<class F>
SetView<T>::SetView(F && cmp, const vector<vector<Ref>> & refs)
{
	items.reserve(refs.size());
	for (const auto & crefs : refs) {
		vector<Stored<typename Mergeable<T>::Component>> comps;
		comps.reserve(crefs.size());
		for (const auto & r : crefs)
			comps.push_back(Stored<typename Mergeable<T>::Component>::load(r));

		filterAncestors(comps);
		items.push_back(Mergeable<T>::merge(comps));
	}
	std::sort(items.begin(), items.end(), cmp);
}

}