6c201021abf921a16aa2b4817613c7cd62f636df
[pspp] / src / language / stats / freq.c
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 void
29 freq_hmap_destroy (struct hmap *hmap, int width)
30 {
31   struct freq *f, *next;
32
33   HMAP_FOR_EACH_SAFE (f, next, struct freq, hmap_node, hmap)
34     {
35       value_destroy (&f->value, width);
36       hmap_delete (hmap, &f->hmap_node);
37       free (f);
38     }
39   hmap_destroy (hmap);
40 }
41
42 struct freq *
43 freq_hmap_search (struct hmap *hmap,
44                   const union value *value, int width, size_t hash)
45 {
46   struct freq *f;
47
48   HMAP_FOR_EACH_WITH_HASH (f, struct freq, hmap_node, hash, hmap)
49     if (value_equal (value, &f->value, width))
50       return f;
51
52   return NULL;
53 }
54
55 struct freq *
56 freq_hmap_insert (struct hmap *hmap,
57                   const union value *value, int width, size_t hash)
58 {
59   struct freq *f = xmalloc (sizeof *f);
60   value_clone (&f->value, value, width);
61   f->count = 0;
62   hmap_insert (hmap, &f->hmap_node, hash);
63   return f;
64 }
65
66 static int
67 compare_freq_ptr_3way (const void *a_, const void *b_, const void *width_)
68 {
69   const struct freq *const *ap = a_;
70   const struct freq *const *bp = b_;
71   const int *widthp = width_;
72
73   return value_compare_3way (&(*ap)->value, &(*bp)->value, *widthp);
74 }
75
76 struct freq **
77 freq_hmap_sort (struct hmap *hmap, int width)
78 {
79   size_t n_entries = hmap_count (hmap);
80   struct freq **entries;
81   struct freq *f;
82   size_t i;
83
84   entries = xnmalloc (n_entries, sizeof *entries);
85   i = 0;
86   HMAP_FOR_EACH (f, struct freq, hmap_node, hmap)
87     entries[i++] = f;
88   assert (i == n_entries);
89
90   sort (entries, n_entries, sizeof *entries, compare_freq_ptr_3way, &width);
91
92   return entries;
93 }
94
95 struct freq *
96 freq_hmap_extract (struct hmap *hmap)
97 {
98   struct freq *freqs, *f;
99   size_t n_freqs;
100   size_t i;
101
102   n_freqs = hmap_count (hmap);
103   freqs = xnmalloc (n_freqs, sizeof *freqs);
104   i = 0;
105   HMAP_FOR_EACH (f, struct freq, hmap_node, hmap)
106     freqs[i++] = *f;
107   assert (i == n_freqs);
108
109   return freqs;
110 }
111