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_clone (const struct freq *in, int values, int *widths)
32 struct freq *f = xmalloc (sizeof (struct freq) +
33 (sizeof (union value) * (values - 1)));
37 for (i = 0; i < values; ++i)
39 value_init (&f->values[i], widths[i]);
40 value_copy (&f->values[i], &in->values[i], widths[i]);
47 freq_destroy (struct freq *f, int values, int *widths)
50 for (i = 0; i < values; ++i)
52 value_destroy (&f->values[i], widths[i]);
61 freq_hmap_destroy (struct hmap *hmap, int width)
63 struct freq *f, *next;
65 HMAP_FOR_EACH_SAFE (f, next, struct freq, node, hmap)
67 value_destroy (&f->values[0], width);
68 hmap_delete (hmap, &f->node);
75 freq_hmap_search (struct hmap *hmap,
76 const union value *value, int width, size_t hash)
80 HMAP_FOR_EACH_WITH_HASH (f, struct freq, node, hash, hmap)
81 if (value_equal (value, &f->values[0], width))
88 freq_hmap_insert (struct hmap *hmap,
89 const union value *value, int width, size_t hash)
91 struct freq *f = xmalloc (sizeof *f);
92 value_clone (&f->values[0], value, width);
94 hmap_insert (hmap, &f->node, hash);
99 compare_freq_ptr_3way (const void *a_, const void *b_, const void *width_)
101 const struct freq *const *ap = a_;
102 const struct freq *const *bp = b_;
103 const int *widthp = width_;
105 return value_compare_3way (&(*ap)->values[0], &(*bp)->values[0], *widthp);
109 freq_hmap_sort (struct hmap *hmap, int width)
111 size_t n_entries = hmap_count (hmap);
112 struct freq **entries;
116 entries = xnmalloc (n_entries, sizeof *entries);
118 HMAP_FOR_EACH (f, struct freq, node, hmap)
120 assert (i == n_entries);
122 sort (entries, n_entries, sizeof *entries, compare_freq_ptr_3way, &width);
128 freq_hmap_extract (struct hmap *hmap)
130 struct freq *freqs, *f;
134 n_freqs = hmap_count (hmap);
135 freqs = xnmalloc (n_freqs, sizeof *freqs);
137 HMAP_FOR_EACH (f, struct freq, node, hmap)
139 assert (i == n_freqs);