-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiset.h
739 lines (678 loc) · 23.1 KB
/
multiset.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
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
/**
* \file multiset.h
*
* This file contains implementations for array_multiset and hash_multiset,
* which are structures that count the number of occurrences of each distinct
* element in a set. array_multiset implements this using a core::array_map,
* where the elements are the keys, and the frequencies are the values with
* type `unsigned int`. hash_multiset implements this using a core::hash_map.
*
* In the following example, both an array_multiset and a hash_multiset are
* constructed. The same set of elements are added to both, and the two are
* printed to stdout. The expected output is
* `{e:1, i:2, r:0, c:3, q:1} {c:3, e:1, i:2, q:1, r:0}`.
*
* ```{.cpp}
* #include <math/multiset.h>
* using namespace core;
*
* template<typename Multiset>
* void add_elements(Multiset& m) {
* m.add('c'); m.add('c');
* m.add('e'); m.add('q');
* m.add('r'); m.add('i');
* m.add('i'); m.add('c');
* m.remove('r');
* }
*
* int main() {
* hash_multiset<char> first(16);
* add_elements(first);
* print(first, stdout); print(" ", stdout);
*
* array_multiset<char> second(8);
* add_elements(second);
* print(second, stdout);
* }
* ```
*
* <!-- Created on: Dec 14, 2015
* Author: asaparov -->
*/
#ifndef HISTOGRAM_H_
#define HISTOGRAM_H_
#include <core/map.h>
#include <core/io.h>
using namespace core;
template<bool AutomaticallyFree>
struct cleaner { };
template<>
struct cleaner<true> {
template<typename T>
static inline void free(T& element) {
core::free(element);
}
template<typename MapType>
static inline void free_keys(MapType& map) {
for (auto entry : map)
core::free(entry.key);
}
};
template<>
struct cleaner<false> {
template<typename T>
static inline void free(T& element) { }
template<typename MapType>
static inline void free_keys(MapType& map) { }
};
/**
* A multiset structure that keeps track of the number of occurrences of
* distinct element in a set, implemented using a core::array_map, where the
* elements are the keys, and the values are their frequencies. The entries in
* the underlying core::array_map are sorted according to the keys.
*
* hash_multiset implements the same abstract data type using a core::hash_map
* and should be used if the number of distinct elements is expected to be
* large.
*
* \tparam T satisfies [LessThanComparable](https://en.cppreference.com/w/cpp/named_req/LessThanComparable)
* and [CopyAssignable](https://en.cppreference.com/w/cpp/named_req/CopyAssignable).
* \tparam AutomaticallyFree whether the keys of the underlying core::hash_map
* are freed when this object is freed.
*/
template<typename T, bool AutomaticallyFree = true>
struct array_multiset {
/**
* The underlying array_map.
*/
array_map<T, unsigned int> counts;
/**
* The sum of the values in array_multiset::counts (i.e. the total number
* of occurrences of all elements).
*/
unsigned int sum;
/**
* Constructs an empty array_multiset with the given `initial_capacity` for
* the underlying array_map array_multiset::counts.
*/
explicit array_multiset(unsigned int initial_capacity) : counts(initial_capacity), sum(0) { }
~array_multiset() { free(); }
/**
* Returns the total number of occurrences of all elements (array_multiset::sum).
*/
inline unsigned int total() const {
return sum;
}
/**
* Adds the given `item` to the multiset with frequency `count`. The given
* `start_index` may be provided to improve performance. However, this
* function assumes that if `item` already exists in this multiset, its
* underlying index in array_multiset::counts is not smaller than
* `start_index`.
*
* This function sorts array_multiset::counts. If the user wishes to add
* multiple elements, they should consider using add_unsorted or the
* overload of add that takes an array_multiset as its parameter.
*/
inline bool add(const T& item, unsigned int count = 1, unsigned int start_index = 0) {
if (!add_unsorted(item, count, start_index))
return false;
if (counts.size > 1)
sort(counts.keys, counts.values, (unsigned int) counts.size, default_sorter()); /* TODO: test performance of different sorts */
return true;
}
/**
* Adds the given `item` to the multiset with frequency `count`. The given
* `start_index` may be provided to improve performance. However, this
* function assumes that if `item` already exists in this multiset, its
* underlying index in array_multiset::counts is not smaller than
* `start_index`.
*
* This function leaves array_multiset::counts unsorted, and so the user
* must sort array_multiset::counts after all elements are added.
*/
bool add_unsorted(const T& item, unsigned int count = 1, unsigned int start_index = 0) {
unsigned int index = counts.index_of(item, start_index);
if (index < counts.size) {
counts.values[index] += count;
sum += count;
return true;
}
if (!counts.ensure_capacity((unsigned int) counts.size + 1)) {
fprintf(stderr, "array_multiset.add WARNING: Unable to expand array_map.\n");
return false;
}
counts.keys[counts.size] = item;
counts.values[counts.size] = count;
counts.size++;
sum += count;
return true;
}
/**
* Adds the given multiset `items` to this multiset.
*/
template<bool OtherAutomaticallyFree>
bool add(const array_multiset<T, OtherAutomaticallyFree>& items) {
unsigned int i = 0, j = 0;
unsigned int new_length = (unsigned int) counts.size;
while (i < counts.size && j < items.counts.size) {
if (counts.keys[i] == items.counts.keys[j]) {
counts.values[i] += items.counts.values[j];
i++; j++;
} else if (counts.keys[i] < items.counts.keys[j]) {
i++;
} else {
if (!counts.ensure_capacity(new_length + 1)) {
fprintf(stderr, "array_multiset.add ERROR: Unable to expand underlying map.\n");
return false;
}
counts.keys[new_length] = items.counts.keys[j];
counts.values[new_length] = items.counts.values[j];
new_length++; j++;
}
}
/* add the leftover elements from 'items' */
if (!counts.ensure_capacity(new_length + (unsigned int) items.counts.size - j)) {
fprintf(stderr, "array_multiset.add ERROR: Unable to expand array_map.\n");
return false;
}
while (j < items.counts.size) {
counts.keys[new_length] = items.counts.keys[j];
counts.values[new_length] = items.counts.values[j];
new_length++; j++;
}
counts.size = new_length;
if (counts.size > 1)
sort(counts.keys, counts.values, (unsigned int) counts.size, default_sorter());
sum += items.sum;
return true;
}
/**
* Removes the given `item` from the multiset. The given `start_index` may
* be provided to improve performance. However, this function assumes that
* `item` exists in the multiset with non-zero frequency, and the
* underlying index of `item` in array_multiset::counts is not smaller than
* `start_index`.
*/
unsigned int subtract(const T& item, unsigned int start_index = 0)
{
unsigned int index = counts.index_of(item, start_index);
if (index < counts.size) {
#if !defined(NDEBUG)
if (counts.values[index] == 0) {
fprintf(stderr, "array_multiset.subtract WARNING: Attempted "
"to remove more items from a bin than it contains.\n");
} else {
counts.values[index]--;
sum--;
}
#else
counts.values[index]--;
sum--;
#endif
return index;
}
fprintf(stderr, "array_multiset.subtract WARNING: No such item.\n");
return (unsigned int) counts.size;
}
/**
* Removes the given multiset `items` from this multiset. This function
* assumes that the given multiset `items` is a subset of this multiset
* (i.e. this multiset contains all the keys in `items` with frequencies
* at least as large).
*/
template<bool OtherAutomaticallyFree>
void subtract(const array_multiset<T, OtherAutomaticallyFree>& items)
{
unsigned int i = 0, j = 0;
while (i < counts.size && j < items.counts.size) {
if (counts.keys[i] == items.counts.keys[j]) {
#if !defined(NDEBUG)
if (counts.values[i] < items.counts.values[j]) {
fprintf(stderr, "array_multiset.subtract WARNING: Attempted "
"to remove more items from a bin than it contains.\n");
counts.values[i] = 0;
} else counts.values[i] -= items.counts.values[j];
#else
counts.values[i] -= items.counts.values[j];
#endif
i++; j++;
} else i++;
}
#if !defined(NDEBUG)
if (j != items.counts.size)
fprintf(stderr, "array_multiset.subtract WARNING: Missing bin in multiset.\n");
#endif
sum -= items.sum;
}
/**
* Removes all elements from this multiset. If `AutomaticallyFree == true`,
* this function also frees the keys in the underlying array_map by
* calling core::free on each element.
*/
inline void clear() {
cleaner<AutomaticallyFree>::free_keys(counts);
counts.clear();
sum = 0;
}
/**
* This function removes keys from array_multiset::counts whose recorded
* frequencies are `0`. If the user is frequently removing items from this
* multiset, this function should be called periodically to clean up the
* empty entries.
*/
void remove_zeros(unsigned int start_index = 0) {
unsigned int new_length = start_index;
for (unsigned int i = start_index; i < counts.size; i++) {
if (counts.values[i] != 0) {
core::move(counts.keys[i], counts.keys[new_length]);
counts.values[new_length] = counts.values[i];
new_length++;
} else {
cleaner<AutomaticallyFree>::free(counts.keys[i]);
}
}
counts.size = new_length;
}
/**
* Returns true if and only if the underlying array_map
* array_multiset::counts is sorted.
*/
inline bool is_sorted() const {
for (unsigned int i = 1; i < counts.size; i++)
if (counts.keys[i] < counts.keys[i - 1])
return false;
return true;
}
/**
* Returns the hash function evaluated on the given array_multiset `key`.
*/
static inline unsigned int hash(const array_multiset<T, AutomaticallyFree>& key) {
return default_hash(key.counts.keys, key.counts.size)
^ default_hash(key.counts.values, key.counts.size);
}
/**
* Moves the array_multiset `src` into `dst`. Note that this function
* merely copies pointers, and not contents.
*/
template<bool OtherAutomaticallyFree>
static inline void move(const array_multiset<T, AutomaticallyFree>& src, array_multiset<T, OtherAutomaticallyFree>& dst) {
array_map<T, unsigned int>::move(src.counts, dst.counts);
dst.sum = src.sum;
}
/**
* Swaps the contents of the given array_multisets `first` and `second`.
*/
template<bool OtherAutomaticallyFree>
static inline void swap(array_multiset<T, AutomaticallyFree>& first, array_multiset<T, OtherAutomaticallyFree>& second) {
core::swap(first.counts, second.counts);
core::swap(first.sum, second.sum);
}
/**
* Copies the contents of the array_multiset `src` into `dst`.
*/
template<bool OtherAutomaticallyFree>
static inline bool copy(const array_multiset<T, AutomaticallyFree>& src, array_multiset<T, OtherAutomaticallyFree>& dst) {
if (!init(dst, (unsigned int) src.counts.size)) {
fprintf(stderr, "array_multiset.copy ERROR: Unable to initialize destination multiset.\n");
return false;
}
for (unsigned int i = 0; i < src.counts.size; i++) {
dst.counts.keys[i] = src.counts.keys[i];
dst.counts.values[i] = src.counts.values[i];
}
dst.counts.size = src.counts.size;
dst.sum = src.sum;
return true;
}
template<typename Metric = default_metric>
static inline long unsigned int size_of(const array_multiset<T, AutomaticallyFree>& s, const Metric& metric) {
return core::size_of(s.counts, metric) + core::size_of(s.sum);
}
/**
* Frees the given array_multiset `s`. If `AutomaticallyFree == true`,
* this function also frees the keys in the underlying array_map by
* calling core::free on each element.
*/
static inline void free(array_multiset<T, AutomaticallyFree>& s) {
s.free();
core::free(s.counts);
}
private:
inline void free() {
cleaner<AutomaticallyFree>::free_keys(counts);
}
};
/**
* Initializes an empty array_multiset `s` with the given `initial_capacity`
* for the underlying array_map array_multiset::counts.
*/
template<typename T, bool AutomaticallyFree>
inline bool init(array_multiset<T, AutomaticallyFree>& s, unsigned int initial_capacity) {
s.sum = 0;
return array_map_init(s.counts, initial_capacity);
}
/**
* Initializes the given array_multiset `s` by copying the contents from the
* given array_multiset `src`.
*/
template<typename T, bool AutomaticallyFree, bool OtherAutomaticallyFree>
inline bool init(array_multiset<T, AutomaticallyFree>& s, const array_multiset<T, OtherAutomaticallyFree>& src) {
if (!array_map_init(s.counts, (unsigned int) src.counts.size))
return false;
for (unsigned int i = 0; i < src.counts.size; i++) {
s.counts.keys[i] = src.counts.keys[i];
s.counts.values[i] = src.counts.values[i];
}
s.counts.size = src.counts.size;
s.sum = src.sum;
return true;
}
/**
* Returns true if and only if the array_multiset `first` is equivalent to `second`.
*/
template<typename T, bool AutomaticallyFree, bool OtherAutomaticallyFree>
inline bool operator == (const array_multiset<T, AutomaticallyFree>& first, const array_multiset<T, OtherAutomaticallyFree>& second) {
if (first.sum != second.sum)
return false;
unsigned int i = 0, j = 0;
while (i < first.counts.size && j < second.counts.size) {
if (first.counts.keys[i] == second.counts.keys[j]) {
if (first.counts.values[i] != second.counts.values[j])
return false;
i++; j++;
} else if (first.counts.keys[i] < second.counts.keys[j]) {
if (first.counts.values[i] > 0)
return false;
i++;
} else {
if (second.counts.values[j] > 0)
return false;
j++;
}
}
while (i < first.counts.size) {
if (first.counts.values[i] > 0)
return false;
i++;
} while (j < second.counts.size) {
if (second.counts.values[j] > 0)
return false;
j++;
}
return true;
}
/**
* Returns false if and only if the array_multiset `first` is equivalent to `second`.
*/
template<typename T, bool AutomaticallyFree, bool OtherAutomaticallyFree>
inline bool operator != (const array_multiset<T, AutomaticallyFree>& first, const array_multiset<T, OtherAutomaticallyFree>& second) {
return !(first == second);
}
/**
* Reads an array_multiset structure `s` from `in`.
* \param reader a scribe that is passed to `read` for core::array_map.
*/
template<typename T, bool AutomaticallyFree, typename... Reader>
inline bool read(array_multiset<T, AutomaticallyFree>& s, FILE* in, Reader&&... reader) {
if (!read(s.counts, in, std::forward<Reader>(reader)...))
return false;
s.sum = 0;
for (unsigned int i = 0; i < s.counts.size; i++)
s.sum += s.counts.values[i];
if (s.counts.size > 1)
sort(s.counts.keys, s.counts.values, (unsigned int) s.counts.size, default_sorter());
return true;
}
/**
* Writes the given array_multiset structure `s` to `out`.
* \param writer a scribe that is passed to `write` for core::array_map.
*/
template<typename T, bool AutomaticallyFree, typename... Writer>
inline bool write(const array_multiset<T, AutomaticallyFree>& s, FILE* out, Writer&&... writer) {
return write(s.counts, out, std::forward<Writer>(writer)...);
}
/**
* Prints the given array_multiset structure `s` to `out`.
* \param printer a scribe that is passed to `print` for core::array_map.
*/
template<typename T, bool AutomaticallyFree, typename... Printer>
inline void print(const array_multiset<T, AutomaticallyFree>& s, FILE* out, Printer&&... printer) {
fputc('{', out);
if (s.counts.size == 0) {
fputc('}', out);
return;
}
print(s.counts.keys[0], out, std::forward<Printer>(printer)...);
fprintf(out, ":%u", s.counts.values[0]);
for (unsigned int i = 1; i < s.counts.size; i++) {
fprintf(out, ", ");
print(s.counts.keys[i], out, std::forward<Printer>(printer)...);
fprintf(out, ":%u", s.counts.values[i]);
}
fputc('}', out);
}
/**
* A multiset structure that keeps track of the number of occurrences of
* distinct elements in a set, implemented using a core::hash_map, where the
* elements are the keys, and the values are their frequencies.
*
* array_multiset implements the same abstract data type using a
* core::array_map and should be used if the number of distinct elements is
* expected to be small.
*
* \tparam T the generic type of the elements. `T` must satisfy either:
* 1. [is_fundamental](http://en.cppreference.com/w/cpp/types/is_fundamental),
* 2. [is_enum](http://en.cppreference.com/w/cpp/types/is_enum),
* 3. [is_pointer](http://en.cppreference.com/w/cpp/types/is_pointer),
* 4. implements the public static method `unsigned int hash(const T&)`,
* the public static method `void is_empty(const T&)`, implements the
* operators `==`, satisfies [CopyAssignable](https://en.cppreference.com/w/cpp/named_req/CopyAssignable),
* and core::is_moveable. **NOTE:** The first argument to the `==`
* operator may be empty.
* \tparam AutomaticallyFree whether the keys of the underlying core::hash_map
* are freed when this object is freed.
*/
template<typename T, bool AutomaticallyFree = true>
struct hash_multiset {
/**
* The underlying hash_map.
*/
hash_map<T, unsigned int> counts;
/**
* The sum of the values in hash_multiset::counts (i.e. the total number of
* occurrences of all elements).
*/
unsigned int sum;
/**
* Constructs an empty hash_multiset with the given `initial_capacity` for
* the underlying hash_map hash_multiset::counts.
*/
explicit hash_multiset(unsigned int initial_capacity) : counts(initial_capacity), sum(0.0) { }
~hash_multiset() { free(); }
/**
* Returns the total number of occurrences of all elements (hash_multiset::sum).
*/
inline unsigned int total() const {
return sum;
}
/**
* Adds the given item to the multiset.
*/
bool add(const T& item) {
if (!counts.check_size()) {
fprintf(stderr, "hash_multiset.add WARNING: Unable to expand hash_map.\n");
return false;
}
bool contains;
unsigned int index;
unsigned int& count = counts.get(item, contains, index);
if (!contains) {
counts.table.keys[index] = item;
counts.table.size++;
count = 1;
} else {
count++;
}
sum++;
return true;
}
/**
* Adds the given multiset of items to this multiset.
*/
template<bool OtherAutomaticallyFree>
bool add(const array_multiset<T, OtherAutomaticallyFree>& items) {
if (!counts.check_size(counts.table.size + items.counts.size)) {
fprintf(stderr, "hash_multiset.add WARNING: Unable to expand hash_map.\n");
return false;
}
bool contains;
unsigned int index;
for (unsigned int i = 0; i < items.counts.size; i++) {
unsigned int& count = counts.get(items.counts.keys[i], contains, index);
if (!contains) {
counts.table.keys[index] = items.counts.keys[i];
counts.table.size++;
count = items.counts.values[i];
} else {
count += items.counts.values[i];
}
}
sum += items.sum;
return true;
}
/**
* Removes the given item from the multiset. This function assumes that
* item exists in the multiset with non-zero frequency.
*/
void subtract(const T& item)
{
#if !defined(NDEBUG)
bool contains;
unsigned int& count = counts.get(item, contains);
if (!contains) {
fprintf(stderr, "hash_multiset.subtract WARNING: Attempted "
"to remove more items from a bin than it contains.\n");
return;
}
#else
unsigned int& count = counts.get(item);
#endif
count--;
sum--;
}
/**
* Removes the given multiset `items` from this multiset. This function
* assumes that the given multiset `items` is a subset of this multiset
* (i.e. this multiset contains all the keys in items with frequencies at
* least as large).
*/
template<bool FreeEmptyBin = false, bool OtherAutomaticallyFree>
void subtract(const array_multiset<T, OtherAutomaticallyFree>& items)
{
for (unsigned int i = 0; i < items.counts.size; i++) {
bool contains; unsigned int bucket;
unsigned int& count = counts.get(items.counts.keys[i], contains, bucket);
#if !defined(NDEBUG)
if (!contains || count < items.counts.values[i]) {
fprintf(stderr, "hash_multiset.subtract WARNING: Attempted "
"to remove more items from a bin than it contains.\n");
count = 0;
} else count -= items.counts.values[i];
#else
count -= items.counts.values[i];
#endif
if (FreeEmptyBin && count == 0) {
if (AutomaticallyFree)
core::free(counts.table.keys[bucket]);
counts.remove_at(bucket);
}
}
sum -= items.sum;
}
/**
* Moves the hash_multiset `src` into `dst`. Note that this function merely
* copies pointers, and not contents.
*/
template<bool OtherAutomaticallyFree>
static inline void move(const hash_multiset<T, AutomaticallyFree>& src, hash_multiset<T, OtherAutomaticallyFree>& dst) {
hash_map<T, unsigned int>::move(src.counts, dst.counts);
dst.sum = src.sum;
}
/**
* Copies the contents of the array_multiset `src` into `dst`.
*/
template<bool OtherAutomaticallyFree>
static inline bool copy(const hash_multiset<T, AutomaticallyFree>& src, hash_multiset<T, OtherAutomaticallyFree>& dst) {
dst.sum = src.sum;
return hash_map<T, unsigned int>::copy(src.counts, dst.counts);
}
template<typename Metric>
static inline long unsigned int size_of(const hash_multiset<T, AutomaticallyFree>& s, const Metric& metric) {
return size_of(s.counts, metric) + size_of(s.sum);
}
/**
* Frees the given hash_multiset `s`. If `AutomaticallyFree == true`,
* this function also frees the keys in the underlying hash_map by
* calling core::free on each element.
*/
static inline void free(hash_multiset<T, AutomaticallyFree>& s) {
s.free();
core::free(s.counts);
}
private:
inline void free() {
cleaner<AutomaticallyFree>::free_keys(counts);
}
};
/**
* Initializes an empty hash_multiset `s` with the given `initial_capacity` for
* the underlying hash_map hash_multiset::counts.
*/
template<typename T, bool AutomaticallyFree>
inline bool init(hash_multiset<T, AutomaticallyFree>& s, unsigned int initial_capacity) {
s.sum = 0;
return hash_map_init(s.counts, initial_capacity);
}
/**
* Reads a hash_multiset `s` from `in`.
* \param reader a scribe that is passed to `read` for core::hash_map.
*/
template<typename T, bool AutomaticallyFree, typename... Reader>
inline bool read(hash_multiset<T, AutomaticallyFree>& s, FILE* in, Reader&&... reader) {
if (!read(s.counts, in, std::forward<Reader>(reader)...))
return false;
s.sum = 0;
for (unsigned int i = 0; i < s.counts.table.capacity; i++)
if (!is_empty(s.counts.table.keys[i]))
s.sum += s.counts.values[i];
return true;
}
/**
* Writes the given hash_multiset `s` to `out`.
* \param writer a scribe that is passed to `write` for core::hash_map.
*/
template<typename T, bool AutomaticallyFree, typename... Writer>
inline bool write(const hash_multiset<T, AutomaticallyFree>& s, FILE* out, Writer&&... writer) {
return write(s.counts, out, std::forward<Writer>(writer)...);
}
/**
* Prints the given hash_multiset `s` to `out`.
* \param printer a scribe that is passed to `print` for core::hash_map.
*/
template<typename T, bool AutomaticallyFree, typename... Printer>
inline void print(const hash_multiset<T, AutomaticallyFree>& s, FILE* out, Printer&&... printer) {
fputc('{', out);
bool first = true;
for (unsigned int i = 0; i < s.counts.table.capacity; i++) {
if (is_empty(s.counts.table.keys[i]))
continue;
if (!first)
fprintf(out, ", ");
first = false;
print(s.counts.table.keys[i], out, std::forward(printer)...);
fprintf(out, ":%u", s.counts.values[i]);
}
fputc('}', out);
}
#endif /* HISTOGRAM_H_ */