summaryrefslogtreecommitdiff
path: root/starts/meaning-vm/level-0/concept.hpp
blob: e885ccf2f313329509a2f9ff3a03e94207eefb2a (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
#pragma once

#include "common.hpp"

#include <any>
#include <map>
#include <unordered_set>
#include <vector>

namespace intellect {
namespace level0 {

struct concept
{
	// a concept is made of concept-typed links to other concepts
	std::multimap<concept*,concept*> links;
	// and optional associated arbitrary data
	std::any data;

	using array = std::vector<concept*>;

	concept* id();

	void link(concept* type, concept* target);
	void unlink(concept* type, concept* target);
	void unlink(concept* type);
	void unlink(decltype(links)::iterator & it);

	bool crucial() { return iscrucial || crucialparts.size(); }
	bool crucial(concept* type, concept* target);
	bool crucial(decltype(links)::iterator it) { return crucialparts.count(it); }
	void setcrucial() { iscrucial = true; }
	void setcrucial(concept* type, concept* target);
	void setcrucial(decltype(links)::iterator it) { crucialparts.insert(it); }

	bool linked(concept* type) const;
	bool linked(concept* type, concept* target) const;

	array getAll(concept* type) const;

	// get and set enforce that only 1 link of a given type is present
	concept* get(concept* type) const;
	void set(concept* type, concept* target);

	template <typename T>
	T & vget(concept* type) const { return get(type)->val<T>(); }

	template <typename T>
	T & val() { return std::any_cast<T&>(data); }

	template <typename T>
	void val(T const & v) { data = v; }

	bool hasval() { return data.has_value(); }

	template <typename T>
	bool hasvalof() { return hasval() && data.type() == typeid(T); }

private:
	// for permanence
	bool iscrucial;
	struct linksit_hash
	{
		size_t operator()(decltype(links)::iterator const &it) const
		{
			return std::hash<decltype(&*it)>()(&*it);
		}
	};
	std::unordered_set<decltype(links)::iterator, linksit_hash> crucialparts;
};

}
}