1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2006, 2009, 2010 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "language/stats/freq.h"
20
21 #include <stdlib.h>
22
23 #include "data/variable.h"
24 #include "data/value.h"
25 #include "libpspp/array.h"
26 #include "libpspp/compiler.h"
27
28 struct freq *
freq_clone(const struct freq * in,int values,int * widths)29 freq_clone (const struct freq *in, int values, int *widths)
30 {
31 int i;
32 struct freq *f = xmalloc (sizeof (struct freq) +
33 (sizeof (union value) * (values - 1)));
34
35 f->node = in->node;
36 f->count = in->count;
37 for (i = 0; i < values; ++i)
38 {
39 value_init (&f->values[i], widths[i]);
40 value_copy (&f->values[i], &in->values[i], widths[i]);
41 }
42
43 return f;
44 }
45
46 void
freq_destroy(struct freq * f,int values,int * widths)47 freq_destroy (struct freq *f, int values, int *widths)
48 {
49 int i;
50 for (i = 0; i < values; ++i)
51 {
52 value_destroy (&f->values[i], widths[i]);
53 }
54
55 free (f);
56 }
57
58
59
60 void
freq_hmap_destroy(struct hmap * hmap,int width)61 freq_hmap_destroy (struct hmap *hmap, int width)
62 {
63 struct freq *f, *next;
64
65 HMAP_FOR_EACH_SAFE (f, next, struct freq, node, hmap)
66 {
67 value_destroy (&f->values[0], width);
68 hmap_delete (hmap, &f->node);
69 free (f);
70 }
71 hmap_destroy (hmap);
72 }
73
74 struct freq *
freq_hmap_search(struct hmap * hmap,const union value * value,int width,size_t hash)75 freq_hmap_search (struct hmap *hmap,
76 const union value *value, int width, size_t hash)
77 {
78 struct freq *f;
79
80 HMAP_FOR_EACH_WITH_HASH (f, struct freq, node, hash, hmap)
81 if (value_equal (value, &f->values[0], width))
82 return f;
83
84 return NULL;
85 }
86
87 struct freq *
freq_hmap_insert(struct hmap * hmap,const union value * value,int width,size_t hash)88 freq_hmap_insert (struct hmap *hmap,
89 const union value *value, int width, size_t hash)
90 {
91 struct freq *f = xmalloc (sizeof *f);
92 value_clone (&f->values[0], value, width);
93 f->count = 0;
94 hmap_insert (hmap, &f->node, hash);
95 return f;
96 }
97
98 int
compare_freq_ptr_3way(const void * a_,const void * b_,const void * width_)99 compare_freq_ptr_3way (const void *a_, const void *b_, const void *width_)
100 {
101 const struct freq *const *ap = a_;
102 const struct freq *const *bp = b_;
103 const int *widthp = width_;
104
105 return value_compare_3way (&(*ap)->values[0], &(*bp)->values[0], *widthp);
106 }
107
108 struct freq **
freq_hmap_sort(struct hmap * hmap,int width)109 freq_hmap_sort (struct hmap *hmap, int width)
110 {
111 size_t n_entries = hmap_count (hmap);
112 struct freq **entries;
113 struct freq *f;
114 size_t i;
115
116 entries = xnmalloc (n_entries, sizeof *entries);
117 i = 0;
118 HMAP_FOR_EACH (f, struct freq, node, hmap)
119 entries[i++] = f;
120 assert (i == n_entries);
121
122 sort (entries, n_entries, sizeof *entries, compare_freq_ptr_3way, &width);
123
124 return entries;
125 }
126
127 struct freq *
freq_hmap_extract(struct hmap * hmap)128 freq_hmap_extract (struct hmap *hmap)
129 {
130 struct freq *freqs, *f;
131 size_t n_freqs;
132 size_t i;
133
134 n_freqs = hmap_count (hmap);
135 freqs = xnmalloc (n_freqs, sizeof *freqs);
136 i = 0;
137 HMAP_FOR_EACH (f, struct freq, node, hmap)
138 freqs[i++] = *f;
139 assert (i == n_freqs);
140
141 return freqs;
142 }
143
144