-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialization.h
148 lines (117 loc) · 3.42 KB
/
Serialization.h
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* A simple e-graph implementation for educational purposes
*
* Copyright waived by Peter Rudenko <peter.rudenko@gmail.com>, 2023
*
* The contents of this file is free and unencumbered software released into the
* public domain. For more information, please refer to <http://unlicense.org/>
*/
#pragma once
#include "EGraph.h"
// This e-graph serialization example uses Cereal,
// but plugging any other lib that works with plain data objects should be similar
#include "cereal/cereal.hpp"
#include "cereal/archives/portable_binary.hpp"
#include "cereal/types/vector.hpp"
#include "cereal/types/string.hpp"
#include <sstream>
namespace e
{
struct GraphDTO final
{
using Id = ClassId;
struct Term final
{
Id leafId;
Symbol name;
Vector<Id> childrenIds;
template <typename Archive>
void serialize(Archive &archive)
{
archive(this->leafId, this->name, this->childrenIds);
}
};
struct Class final
{
Id classId;
Vector<Id> termIds;
Vector<Id> parentIds;
template <typename Archive>
void serialize(Archive &archive)
{
archive(this->classId, this->termIds, this->parentIds);
}
};
Vector<Id> unionFind;
Vector<Term> terms;
Vector<Class> classes;
template <typename Archive>
void serialize(Archive &archive)
{
archive(this->unionFind, this->terms, this->classes);
}
};
static std::string serialize(const Graph &eGraph)
{
GraphDTO dto;
dto.unionFind = eGraph.unionFind.parents;
for (const auto &[termPtr, leafId] : eGraph.termsLookup)
{
dto.terms.push_back({leafId, termPtr->name, termPtr->childrenIds});
}
for (const auto &[classId, classPtr] : eGraph.classes)
{
GraphDTO::Class c;
c.classId = classId;
for (const auto &term : classPtr->terms)
{
c.termIds.push_back(eGraph.termsLookup.at(term));
}
for (const auto &parent : classPtr->parents)
{
c.parentIds.push_back(eGraph.termsLookup.at(parent.term));
}
dto.classes.push_back(std::move(c));
}
std::stringstream stream;
{
cereal::PortableBinaryOutputArchive serializer(stream);
serializer(dto);
}
return stream.str();
}
static Graph deserialize(const std::string &data)
{
GraphDTO dto;
{
std::stringstream stream(data);
cereal::PortableBinaryInputArchive deserializer(stream);
deserializer(dto);
}
Graph eGraph;
eGraph.unionFind.parents = move(dto.unionFind);
HashMap<ClassId, Term::Ptr> termsLookup;
for (const auto &term : dto.terms)
{
Term::Ptr termPtr = make<Term>(term.name, term.childrenIds);
eGraph.termsLookup[termPtr] = term.leafId;
termsLookup[term.leafId] = termPtr;
}
for (const auto &cls : dto.classes)
{
auto eClass = make<Class>(cls.classId);
for (const auto &leafId : cls.termIds)
{
assert(termsLookup.find(leafId) != termsLookup.end());
eClass->terms.push_back(termsLookup[leafId]);
}
for (const auto &leafId : cls.parentIds)
{
assert(termsLookup.find(leafId) != termsLookup.end());
eClass->parents.push_back({termsLookup[leafId], leafId});
}
eGraph.classes[cls.classId] = move(eClass);
}
return eGraph;
}
} // namespace e