-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactoryReader.hpp
More file actions
240 lines (184 loc) · 6.72 KB
/
FactoryReader.hpp
File metadata and controls
240 lines (184 loc) · 6.72 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#pragma once
#include "Set.hpp"
#include <fstream>
#include <vector>
#include "NormalSet.hpp"
#include "SubstringCriteria.hpp"
#include "ConcatCriteria.h"
template <typename T>
class FactoryReader
{
public:
Set<T>* readSet(std::ifstream& binaryFileReader, short stringsCount, short setType);
static FactoryReader<T>* getInstance();
static void freeInstance();
private:
FactoryReader() = default;
FactoryReader(const FactoryReader<T>& other) = delete;
FactoryReader<T>& operator= (const FactoryReader<T>& other) = delete;
static FactoryReader<T>* instance;
private:
Set<T>* readNormalSet(std::ifstream& binaryFileReader, short stringsCount) const;
Set<T>* readIntervalSet(std::ifstream& binaryFileReader, short stringsCount) const;
Set<T>* readSubstringCriteriaSet(std::ifstream& binaryFileReader, short stringsCount) const;
Set<T>* readConcatCriteriaSet(std::ifstream& binaryFileReader, short stringsCount) const;
Set<T>* readIntersectOfIntervalSets(std::ifstream& binaryFileReader, short stringsCount) const;
Set<T>* readUnionOfIntervalSets(std::ifstream& binaryFileReader, short stringsCount) const;
};
template<typename T>
FactoryReader<T>* FactoryReader<T>::instance = nullptr;
template<typename T>
inline FactoryReader<T>* FactoryReader<T>::getInstance()
{
if (!instance)
{
instance = new FactoryReader<T>();
}
return instance;
}
template<typename T>
inline void FactoryReader<T>::freeInstance()
{
delete instance;
instance = nullptr;
}
template<typename T>
inline Set<T>* FactoryReader<T>::readSet(std::ifstream& binaryFileReader, short stringsCount, short setType)
{
switch (setType)
{
case 0: return readNormalSet(binaryFileReader, stringsCount);
case 1: return readIntervalSet(binaryFileReader, stringsCount);
case 2: return readSubstringCriteriaSet(binaryFileReader, stringsCount);
case 3: return readConcatCriteriaSet(binaryFileReader, stringsCount);
case 4: return readIntersectOfIntervalSets(binaryFileReader, stringsCount);
case 5: return readUnionOfIntervalSets(binaryFileReader, stringsCount);
}
}
template<typename T>
inline Set<T>* FactoryReader<T>::readNormalSet(std::ifstream& binaryFileReader, short stringsCount) const
{
std::vector<T> elements;
for (size_t i = 0; i < stringsCount; i++)
{
short stringLength;
binaryFileReader.read((char*)&stringLength, sizeof(short));
char* string = new char[stringLength + 1];
binaryFileReader.read(string, stringLength);
string[stringLength] = '\0';
T element(string);
elements.push_back(element);
}
return new NormalSet<T>(elements);
}
template<typename T>
inline Set<T>* FactoryReader<T>::readIntervalSet(std::ifstream& binaryFileReader, short stringsCount) const
{
if (stringsCount != 2)
{
return nullptr;
}
short leftIntervalLength;
binaryFileReader.read((char*)&leftIntervalLength, sizeof(short));
char* leftIntervalPtr = new char[leftIntervalLength + 1];
binaryFileReader.read(leftIntervalPtr, leftIntervalLength);
leftIntervalPtr[leftIntervalLength] = '\0';
short rightIntervalLength;
binaryFileReader.read((char*)&rightIntervalLength, sizeof(short));
char* rightIntervalPtr = new char[rightIntervalLength + 1];
binaryFileReader.read(rightIntervalPtr, rightIntervalLength);
rightIntervalPtr[rightIntervalLength] = '\0';
T leftInterval(leftIntervalPtr);
T rightInterval(rightIntervalPtr);
return new IntervalSet<T>(leftInterval, rightInterval);
}
template<typename T>
inline Set<T>* FactoryReader<T>::readSubstringCriteriaSet(std::ifstream& binaryFileReader, short stringsCount) const
{
SubstringCriteria<T> substringCriteria;
for (size_t i = 0; i < stringsCount; i++)
{
short stringLength;
binaryFileReader.read((char*)&stringLength, sizeof(short));
char* string = new char[stringLength + 1];
binaryFileReader.read(string, stringLength);
string[stringLength] = '\0';
T element(string);
substringCriteria.addElement(element);
}
// Set<T>* crit = new CriteriaSet<T, SubstringCriteria<T>>(substringCriteria);
// std::cout << crit->operator[]("blablacfmioop.datskrr");
// std::cout << crit->operator[]("petarrrr.........ivan.union .datskrr");
// std::cout << crit->operator[]("llllllllllllleftInterval.dattttttttttttt");
return new CriteriaSet<T, SubstringCriteria<T>>(substringCriteria);
}
template<typename T>
inline Set<T>* FactoryReader<T>::readConcatCriteriaSet(std::ifstream& binaryFileReader, short stringsCount) const
{
ConcatCriteria<T> concatCriteria;
for (size_t i = 0; i < stringsCount; i++)
{
short stringLength;
binaryFileReader.read((char*)&stringLength, sizeof(short));
char* string = new char[stringLength + 1];
binaryFileReader.read(string, stringLength);
string[stringLength] = '\0';
T element(string);
concatCriteria.addElement(element);
}
/*Set<T>* crit = new CriteriaSet<T, ConcatCriteria<T>>(concatCriteria);
std::cout << crit->operator[]("fmioop.datpetar.dat");
std::cout << crit->operator[]("fmioop.dativan.union");
std::cout << crit->operator[]("fmioop.dativan.unionn");
std::cout << crit->operator[]("rightInterval.datleftInterval.dat");
std::cout << crit->operator[]("rrightInterval.datleftInterval.dat");*/
return new CriteriaSet<T, ConcatCriteria<T>>(concatCriteria);
}
template<typename T>
inline Set<T>* FactoryReader<T>::readIntersectOfIntervalSets(std::ifstream& binaryFileReader, short stringsCount) const
{
if (stringsCount % 2 != 0)
{
return nullptr;
}
std::vector<Set<T>*> sets;
for (size_t i = 0; i < stringsCount / 2; i++)
{
Set<T>* intervalSet = readIntervalSet(binaryFileReader, 2);
sets.push_back(intervalSet);
}
//Set<T>* set = new IntersectSet<T>(sets);
//std::cout << set->operator[]("Grapes"); // 1
//std::cout << set->operator[]("Igloo"); // 1
//std::cout << set->operator[]("Jigsaw"); // 1
//std::cout << set->operator[]("Kite"); // 1
//std::cout << set->operator[]("Lemon"); // 0
//std::cout << set->operator[]("Dance"); // 0
//std::cout << set->operator[]("Apple"); // 0
//std::cout << set->operator[]("Parrot"); // 0
return new IntersectSet<T>(sets);
}
template<typename T>
inline Set<T>* FactoryReader<T>::readUnionOfIntervalSets(std::ifstream& binaryFileReader, short stringsCount) const
{
if (stringsCount % 2 != 0)
{
return nullptr;
}
std::vector<Set<T>*> sets;
for (size_t i = 0; i < stringsCount / 2; i++)
{
Set<T>* intervalSet = readIntervalSet(binaryFileReader, 2);
sets.push_back(intervalSet);
}
// Set<T>* set = new UnionSet<T>(sets);
//
// std::cout << set->operator[]("Apple"); // 0
// std::cout << set->operator[]("Igloo"); // 1
// std::cout << set->operator[]("Jigsaw"); // 1
// std::cout << set->operator[]("Kite"); // 1
// std::cout << set->operator[]("Lemon"); // 1
// std::cout << set->operator[]("Dance"); // 1
// std::cout << set->operator[]("Quilt"); // 0
return new UnionSet<T>(sets);
}