-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspdfp1.cpp
207 lines (168 loc) · 4.75 KB
/
spdfp1.cpp
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <iostream>
#include <string>
#include <sstream>
#include <map>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/map.hpp>
class UniqueKeyGen
{
static int key;
public:
static int getUniqueID()
{
return UniqueKeyGen::key++;
}
};
int UniqueKeyGen::key = 0;
class serializable
{
public:
typedef boost::archive::text_oarchive OutArchive;
typedef boost::archive::text_iarchive InArchive;
virtual void serialize(OutArchive& archive, const unsigned int version = 0) = 0;
virtual void serialize(InArchive& archive, const unsigned int version = 0) = 0;
};
class Lattice : public serializable
{
public:
virtual std::string str(std::string indent=" ") = 0;
virtual Lattice* copy() = 0;
};
class MemoryObject : public serializable
{
public:
int key;
MemoryObject() { key = -1; }
MemoryObject(int _key) : key(_key) { }
MemoryObject(const MemoryObject& that) { this->key = that.key; }
bool operator< (const MemoryObject& that) { return this->key < that.key; }
void serialize(OutArchive& archive, const unsigned int version = 0)
{
archive << key;
}
void serialize(InArchive& archive, const unsigned int version = 0)
{
archive >> key;
}
};
class ProductLattice : public Lattice
{
int size;
public:
std::map<MemoryObject*, Lattice*> productlattice;
ProductLattice() { }
ProductLattice(const ProductLattice& that)
{
this->productlattice = that.productlattice;
}
Lattice* copy()
{
return new ProductLattice(*(dynamic_cast<ProductLattice*>(this)));
}
void init(Lattice* perVarLattice, int size)
{
this->size = size;
for(int i = 0; i < size; i++) {
MemoryObject* _mobj = new MemoryObject(UniqueKeyGen::getUniqueID());
Lattice* varLattice = perVarLattice->copy();
productlattice.insert(std::pair<MemoryObject*, Lattice*> (_mobj, varLattice));
}
}
// NOTE: map should be serialized
void serialize(OutArchive& archive, const unsigned int version = 0)
{
}
void serialize(InArchive& archive, const unsigned int version = 0)
{
}
std::string str(std::string indent=", ")
{
std::stringstream ss;
std::map<MemoryObject*, Lattice*>::iterator it;
for(it = productlattice.begin(); it != productlattice.end(); it++) {
ss << ((*it).first)->key << indent << ((*it).second)->str();
}
return ss.str();
}
};
class IntLattice: public Lattice
{
public:
int val;
IntLattice() { }
IntLattice(int _that) : Lattice(), val(_that) { }
IntLattice(const IntLattice& that)
{
this->val = that.val;
}
Lattice* copy()
{
return new IntLattice(*dynamic_cast<IntLattice*>(this));
}
void serialize(OutArchive& archive, const unsigned int version = 0)
{
archive << val;
}
void serialize(InArchive& archive, const unsigned int version = 0)
{
archive >> val;
}
std::string str(std::string indent=" ")
{
std::stringstream ss;
ss << "val:" << indent << val << std::endl;
return ss.str();
}
};
class FloatLattice: public Lattice
{
public:
float fval;
FloatLattice() { }
FloatLattice(float _that) : fval(_that) { }
FloatLattice(const FloatLattice& that)
{
this->fval = that.fval;
}
Lattice* copy()
{
return new FloatLattice(*dynamic_cast<FloatLattice*>(this));
}
void serialize(OutArchive& archive, const unsigned int version = 0)
{
archive << fval;
}
void serialize(InArchive& archive, const unsigned int version = 0)
{
archive >> fval;
}
std::string str(std::string indent=" ")
{
std::stringstream ss;
ss << "fval:" << indent << fval << std::endl;
return ss.str();
}
};
int main()
{
std::stringstream rw_stream;
// Lattice* lattice = new IntLattice(10);
// Lattice* flattice = new FloatLattice(2.1);
// Lattice::OutArchive out_archive(rw_stream);
// lattice->serialize(out_archive);
// flattice->serialize(out_archive);
// std::cout << rw_stream.str() << std::endl;
// Lattice::InArchive in_archive(rw_stream);
// Lattice* rilattice = new IntLattice();
// Lattice* rflattice = new FloatLattice();
// rilattice->deserialize(in_archive);
// rflattice->deserialize(in_archive);
// dynamic_cast<FloatLattice*>(rflattice)->str();
ProductLattice *pLattice = new ProductLattice();
pLattice->init(new IntLattice(10), 10);
serializable::OutArchive out_archive(rw_stream);
pLattice->serialize(out_archive);
std::cout << rw_stream.str() << std::endl;
return 0;
}