-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset.hpp
More file actions
542 lines (468 loc) · 12.1 KB
/
set.hpp
File metadata and controls
542 lines (468 loc) · 12.1 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/**
@file set.hpp
@brief File header della classe set templata
File di dichiarazioni/definizioni della classe set templata
*/
#ifndef SET_H
#define SET_H
#include <algorithm> // std::swap
#include <ostream> // std::ostream
#include <cstddef> // std::ptrdiff_t
#include <iterator> // std::forward_iterator_tag
#include <fstream> // std::ofstream
/**
* @brief classe set
*
* @tparam T tipo di dato
* @tparam Equal funtore di uguaglianza
* @tparam Allocator tipo di allocazione
*/
template <typename T, typename Equal, typename Allocator>
class Set
{
public:
typedef unsigned int size_type; ///< Tipo di dato per la dimensione dell'array
typedef T value_type;
private:
value_type *_elements; ///< Array of elements
size_type _size; ///< Number of elements
Equal _eq; ///< funtore
Allocator _allocator; ///< allocazione
public:
// Costruttore di default
Set() : _elements(nullptr), _size(0) {}
// Distruttore
~Set()
{
clear();
}
// Costruttore Copia
Set(const Set &other) : _elements(nullptr), _size(0)
{
if (other._size > 0)
{
_elements = _allocator.allocate(other._size);
_size = other._size;
for (size_type i = 0; i < _size; i++)
{
_allocator.construct(_elements + i, other._elements[i]);
}
}
}
/**
* @brief Costruttore che crea un set riempito con dati presi
* da una sequenza identificata da un iteratore di inizio e
* uno di fine.
*
*
* @tparam Iter tipo di iteratore
* @param begin iteratore di inizio sequenza
* @param end iteratore di fine sequenza
* @throw std::bad_alloc possibile eccezione di allocazione
*/
template <typename Iter>
Set(Iter begin, Iter end) : _elements(nullptr),
_size(0)
{
Iter curr = begin;
try
{
for (; curr != end; ++curr)
{
add(static_cast<T>(*curr));
}
}
catch (...)
{
clear();
throw;
}
}
/**
* @brief Aggiungere un elemento alla lista
*
*
* @param value valore da inserire nella lista
* @return true se l'elemento è stato inserito, false altrimenti
* @throw std::bad_alloc possibile eccezione di allocazione
*
*/
bool add(const value_type &value)
{
if (contains(value))
{
return false;
}
value_type *tmp = _allocator.allocate(_size + 1);
for (size_type i = 0; i < _size; i++)
{
_allocator.construct(tmp + i, _elements[i]);
}
_allocator.construct(tmp + _size, value);
for (size_type i = 0; i < _size; i++)
{
_allocator.destroy(_elements + i);
}
_allocator.deallocate(_elements, _size);
_elements = tmp;
_size++;
return true;
}
/**
* @brief Rimuovere un elemento dal set
*
* @param value valore da rimuovere dal set
* @return true se l'elemento è stato rimosso, false altrimenti
* @throw std::bad_alloc possibile eccezione di allocazione
*/
bool remove(const value_type &value)
{
if (!contains(value))
{
return false;
}
if (_size == 1)
{
clear();
return true;
}
value_type *tmp = _allocator.allocate(_size - 1);
size_type j = 0;
for (size_type i = 0; i < _size; i++)
{
if (!_eq(_elements[i], value))
{
_allocator.construct(tmp + j, _elements[i]);
j++;
}
}
for (size_type i = 0; i < _size; i++)
{
_allocator.destroy(_elements + i);
}
_allocator.deallocate(_elements, _size);
_elements = tmp;
_size--;
return true;
}
/**
* @brief Controlla se un elemento è presente nel set
*
* @param value valore da cercare nel set
* @return true se l'elemento è presente nel set, false altrimenti
*/
bool contains(const value_type &value) const
{
for (size_type i = 0; i < _size; i++)
{
if (_eq(_elements[i], value))
{
return true;
}
}
return false;
}
/**
* @brief Scambia il contenuto di due set
*
* @param other set con cui scambiare il contenuto
*/
void swap(Set &other)
{
std::swap(_elements, other._elements);
std::swap(_size, other._size);
std::swap(_eq, other._eq);
}
/**
* @brief operatore di assegnamento
*
* @param other set da copiare
* @return reference a this
*/
Set &operator=(const Set &other)
{
if (&other != this)
{
Set tmp(other);
this->swap(tmp);
}
return *this;
}
/**
@brief operatore di uguaglianza
@param other set da confrontare
@return true se i due set sono uguali, tutti gli elementi coincidono, false altrimenti
*/
bool operator==(const Set &other) const
{
if (_size != other._size)
{
return false;
}
for (size_type i = 0; i < _size; i++)
{
if (!contains(other._elements[i]))
return false;
}
return true;
}
/**
@brief Accesso in sola lettura ad un elemento tramite operatore []
@param i indice dell'elemento da leggere
@return reference all'elemento di indice i
@throw std::out_of_range se i >= _size
*/
const value_type &operator[](size_type i) const
{
if (i < _size)
{
return _elements[i];
}
else
{
throw std::out_of_range("Index out of range");
}
}
/**
* @brief Dimensione del set
*
*
* @return Il numero di elementi nel set
*
*/
size_type size(void) const
{
return _size;
}
/**
* @brief Controlla se il set è vuoto
*
* @return true se il set è vuoto, false altrimenti
*/
bool empty(void) const
{
return (_size == 0);
}
/**
* @brief Svuota il set
*
*/
void clear(void)
{
if (_size > 0)
{
for (size_type i = 0; i < _size; i++)
{
_allocator.destroy(_elements + i);
}
_allocator.deallocate(_elements, _size);
}
_elements = nullptr;
_size = 0;
}
/**
* @brief Iteratore costante
*
*/
class const_iterator
{
public:
typedef std::forward_iterator_tag iterator_category;
typedef T value_type;
typedef std::ptrdiff_t difference_type;
typedef const T *pointer;
typedef const T &reference;
// Costruttore di default
const_iterator() : ptr(nullptr) {}
// Costruttore Copia
const_iterator(const const_iterator &other) : ptr(other.ptr) {}
// Distruttore
~const_iterator() {}
// Operatore di assegnamento
const_iterator &operator=(const const_iterator &other)
{
ptr = other.ptr;
return *this;
}
// Operatore di iterazione post-incremento
const_iterator &operator++()
{
++ptr;
return *this;
}
// Operatore di iterazione pre-incremento
const_iterator operator++(int)
{
const_iterator temp(*this);
++ptr;
return temp;
}
// Operatore di dereferenziazione
reference operator*() const
{
return *ptr;
}
// Operatore di accesso a membro
pointer operator->() const
{
return ptr;
}
// Operatore di uguaglianza
bool operator==(const const_iterator &other) const
{
return ptr == other.ptr;
}
// Operatore di disuguaglianza
bool operator!=(const const_iterator &other) const
{
return !(*this == other);
}
private:
const T *ptr;
friend class Set;
// Costruttore privato di inizializzazione usato dalla classe container
const_iterator(const T *p) : ptr(p) {}
};
/**
* @brief Iteratore costante di inizio sequenza
*
* @return const_iterator di inizio sequenza
*/
const_iterator begin() const
{
return const_iterator(_elements);
}
/**
* @brief Iteratore costante di fine sequenza
*
* @return const_iterator di fine sequenza
*/
const_iterator end() const
{
return const_iterator(_elements + _size);
}
};
/**
* @brief Operatore di stream
*
* @tparam T tipo di dato
* @tparam Equal funtore di uguaglianza
* @tparam Allocator tipo di allocazione
* @param os stream di output
* @param s set da stampare
* @return reference allo stream di output
*/
template <typename T, typename Equal, typename Allocator>
std::ostream &operator<<(std::ostream &os, const Set<T, Equal, Allocator> &s)
{
os << s.size();
for (typename Set<T, Equal, Allocator>::const_iterator i = s.begin(); i != s.end(); ++i)
{
os << " (" << *i << ")";
}
return os;
}
/**
* @brief Operatore di stream
*
* @tparam T tipo di dato
* @tparam Equal funtore di uguaglianza
* @tparam Allocator tipo di allocazione
* @tparam P tipo di predicato
* @param s set da filtrare
* @param predicate predicato
*
* @return nuovo set filtrato
*/
template <typename T, typename Equal, typename Allocator, typename P>
Set<T, Equal, Allocator> filter_out(const Set<T, Equal, Allocator> &s, P predicate)
{
typename Set<T, Equal, Allocator>::const_iterator i, ie;
Set<T, Equal, Allocator> result;
for (i = s.begin(), ie = s.end(); i != ie; ++i)
{
if (predicate(*i))
{
result.add(*i);
}
}
return result;
}
/**
* @brief Operatore +
*
* @tparam T tipo di dato
* @tparam Equal funtore di uguaglianza
* @tparam Allocator tipo di allocazione
* @param s1 set da sommare
* @param s2 set da sommare
* @return nuovo set unione
*/
template <typename T, typename Equal, typename Allocator>
Set<T, Equal, Allocator> operator+(const Set<T, Equal, Allocator> &s1, const Set<T, Equal, Allocator> &s2)
{
typename Set<T, Equal, Allocator>::const_iterator i, ie;
Set<T, Equal, Allocator> result;
for (i = s1.begin(), ie = s1.end(); i != ie; ++i)
{
result.add(*i);
}
for (i = s2.begin(), ie = s2.end(); i != ie; ++i)
{
result.add(*i);
}
return result;
}
/**
* @brief Operatore -
*
* @tparam T tipo di dato
* @tparam Equal funtore di uguaglianza
* @tparam Allocator tipo di allocazione
* @param s1 set da intersecare
* @param s2 set da intersecare
* @return nuovo set intersezione
*/
template <typename T, typename Equal, typename Allocator>
Set<T, Equal, Allocator> operator-(const Set<T, Equal, Allocator> &s1, const Set<T, Equal, Allocator> &s2)
{
typename Set<T, Equal, Allocator>::const_iterator i, ie;
Set<T, Equal, Allocator> result;
for (i = s1.begin(), ie = s1.end(); i != ie; ++i)
{
if (s2.contains(*i))
{
result.add(*i);
}
}
return result;
}
/**
* @brief salva il contenuto di un set su file
* @details Salva escusivamente il contenuto di Set<std::string> su file
* @tparam Equal funtore di uguaglianza
* @tparam Allocator
* @param s set da salvare
* @param filename nome del file su cui salvare il set
*/
template <typename Equal, typename Allocator>
void save(const Set<std::string, Equal, Allocator> &s, const std::string &filename)
{
std::ostream *os = nullptr;
std::ofstream ofs;
ofs.open(filename.c_str());
if (ofs.fail())
{
throw std::runtime_error("Cannot open file");
}
os = &ofs;
*os << s;
ofs.close();
if (ofs.fail())
{
throw std::runtime_error("Cannot close file");
}
os = nullptr;
return;
}
#endif // SET_H