1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2006, 2009, 2010 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "language/stats/freq.h"
23 #include "data/variable.h"
24 #include "data/value.h"
25 #include "libpspp/array.h"
26 #include "libpspp/compiler.h"
29 freq_hmap_destroy (struct hmap *hmap, int width)
31 struct freq *f, *next;
33 HMAP_FOR_EACH_SAFE (f, next, struct freq, hmap_node, hmap)
35 value_destroy (&f->value, width);
36 hmap_delete (hmap, &f->hmap_node);
43 freq_hmap_search (struct hmap *hmap,
44 const union value *value, int width, size_t hash)
48 HMAP_FOR_EACH_WITH_HASH (f, struct freq, hmap_node, hash, hmap)
49 if (value_equal (value, &f->value, width))
56 freq_hmap_insert (struct hmap *hmap,
57 const union value *value, int width, size_t hash)
59 struct freq *f = xmalloc (sizeof *f);
60 value_clone (&f->value, value, width);
62 hmap_insert (hmap, &f->hmap_node, hash);
67 compare_freq_ptr_3way (const void *a_, const void *b_, const void *width_)
69 const struct freq *const *ap = a_;
70 const struct freq *const *bp = b_;
71 const int *widthp = width_;
73 return value_compare_3way (&(*ap)->value, &(*bp)->value, *widthp);
77 freq_hmap_sort (struct hmap *hmap, int width)
79 size_t n_entries = hmap_count (hmap);
80 struct freq **entries;
84 entries = xnmalloc (n_entries, sizeof *entries);
86 HMAP_FOR_EACH (f, struct freq, hmap_node, hmap)
88 assert (i == n_entries);
90 sort (entries, n_entries, sizeof *entries, compare_freq_ptr_3way, &width);
96 freq_hmap_extract (struct hmap *hmap)
98 struct freq *freqs, *f;
102 n_freqs = hmap_count (hmap);
103 freqs = xnmalloc (n_freqs, sizeof *freqs);
105 HMAP_FOR_EACH (f, struct freq, hmap_node, hmap)
107 assert (i == n_freqs);