]> git.saurik.com Git - bison.git/blame - lib/bitset_stats.c
Avoid warnings from gcc -Wundef y.tab.c.
[bison.git] / lib / bitset_stats.c
CommitLineData
613f5e1a 1/* Bitset statistics.
401b73af
JD
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2009 Free Software
3 Foundation, Inc.
613f5e1a
AD
4 Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
5
f16b0819 6 This program is free software: you can redistribute it and/or modify
613f5e1a 7 it under the terms of the GNU General Public License as published by
f16b0819 8 the Free Software Foundation, either version 3 of the License, or
613f5e1a
AD
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
f16b0819 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
613f5e1a
AD
18
19/* This file is a wrapper bitset implementation for the other bitset
20 implementations. It provides bitset compatibility checking and
21 statistics gathering without having to instrument the bitset
22 implementations. When statistics gathering is enabled, the bitset
23 operations get vectored through here and we then call the appropriate
02650b7f 24 routines. */
613f5e1a 25
231ed89a
PE
26#include <config.h>
27
28#include "bitset_stats.h"
613f5e1a
AD
29
30#include "bbitset.h"
31#include "abitset.h"
32#include "ebitset.h"
33#include "lbitset.h"
eaef5507 34#include "vbitset.h"
613f5e1a
AD
35#include <stdlib.h>
36#include <string.h>
37#include <stdio.h>
38
c131cbff
PE
39#include "gettext.h"
40#define _(Msgid) gettext (Msgid)
613f5e1a
AD
41
42/* Configuration macros. */
43#define BITSET_STATS_FILE "bitset.dat"
44#define BITSET_LOG_COUNT_BINS 10
45#define BITSET_LOG_SIZE_BINS 16
46#define BITSET_DENSITY_BINS 20
47
48
49/* Accessor macros. */
04098407 50#define BITSET_STATS_ALLOCS_INC(TYPE) \
613f5e1a 51 bitset_stats_info->types[(TYPE)].allocs++
04098407 52#define BITSET_STATS_FREES_INC(BSET) \
613f5e1a 53 bitset_stats_info->types[BITSET_TYPE_ (BSET)].frees++
04098407 54#define BITSET_STATS_SETS_INC(BSET) \
613f5e1a 55 bitset_stats_info->types[BITSET_TYPE_ (BSET)].sets++
04098407 56#define BITSET_STATS_CACHE_SETS_INC(BSET) \
613f5e1a 57 bitset_stats_info->types[BITSET_TYPE_ (BSET)].cache_sets++
04098407 58#define BITSET_STATS_RESETS_INC(BSET) \
613f5e1a 59 bitset_stats_info->types[BITSET_TYPE_ (BSET)].resets++
04098407 60#define BITSET_STATS_CACHE_RESETS_INC(BSET) \
613f5e1a 61 bitset_stats_info->types[BITSET_TYPE_ (BSET)].cache_resets++
04098407 62#define BITSET_STATS_TESTS_INC(BSET) \
613f5e1a 63 bitset_stats_info->types[BITSET_TYPE_ (BSET)].tests++
04098407 64#define BITSET_STATS_CACHE_TESTS_INC(BSET) \
613f5e1a 65 bitset_stats_info->types[BITSET_TYPE_ (BSET)].cache_tests++
04098407 66#define BITSET_STATS_LISTS_INC(BSET) \
613f5e1a 67 bitset_stats_info->types[BITSET_TYPE_ (BSET)].lists++
04098407 68#define BITSET_STATS_LIST_COUNTS_INC(BSET, I) \
613f5e1a 69 bitset_stats_info->types[BITSET_TYPE_ (BSET)].list_counts[(I)]++
04098407 70#define BITSET_STATS_LIST_SIZES_INC(BSET, I) \
613f5e1a 71 bitset_stats_info->types[BITSET_TYPE_ (BSET)].list_sizes[(I)]++
04098407 72#define BITSET_STATS_LIST_DENSITY_INC(BSET, I) \
613f5e1a
AD
73 bitset_stats_info->types[BITSET_TYPE_ (BSET)].list_density[(I)]++
74
75
613f5e1a
AD
76struct bitset_type_info_struct
77{
78 unsigned int allocs;
79 unsigned int frees;
80 unsigned int lists;
81 unsigned int sets;
82 unsigned int cache_sets;
83 unsigned int resets;
84 unsigned int cache_resets;
85 unsigned int tests;
86 unsigned int cache_tests;
87 unsigned int list_counts[BITSET_LOG_COUNT_BINS];
88 unsigned int list_sizes[BITSET_LOG_SIZE_BINS];
89 unsigned int list_density[BITSET_DENSITY_BINS];
90};
91
92struct bitset_stats_info_struct
93{
94 unsigned int runs;
95 struct bitset_type_info_struct types[BITSET_TYPE_NUM];
96};
97
98
99struct bitset_stats_info_struct bitset_stats_info_data;
100struct bitset_stats_info_struct *bitset_stats_info;
d0829076 101bool bitset_stats_enabled = false;
613f5e1a
AD
102
103
613f5e1a
AD
104/* Print a percentage histogram with message MSG to FILE. */
105static void
d65ec44e
PE
106bitset_percent_histogram_print (FILE *file, const char *name, const char *msg,
107 unsigned int n_bins, unsigned int *bins)
613f5e1a
AD
108{
109 unsigned int i;
110 unsigned int total;
111
112 total = 0;
113 for (i = 0; i < n_bins; i++)
114 total += bins[i];
115
116 if (!total)
117 return;
118
119 fprintf (file, "%s %s", name, msg);
120 for (i = 0; i < n_bins; i++)
2d382ea8 121 fprintf (file, "%.0f-%.0f%%\t%8u (%5.1f%%)\n",
613f5e1a
AD
122 i * 100.0 / n_bins,
123 (i + 1) * 100.0 / n_bins, bins[i],
124 (100.0 * bins[i]) / total);
125}
126
127
128/* Print a log histogram with message MSG to FILE. */
129static void
d65ec44e
PE
130bitset_log_histogram_print (FILE *file, const char *name, const char *msg,
131 unsigned int n_bins, unsigned int *bins)
613f5e1a
AD
132{
133 unsigned int i;
134 unsigned int total;
135 unsigned int max_width;
613f5e1a
AD
136
137 total = 0;
138 for (i = 0; i < n_bins; i++)
139 total += bins[i];
140
141 if (!total)
142 return;
143
6aa452a6 144 /* Determine number of useful bins. */
613f5e1a
AD
145 for (i = n_bins; i > 3 && ! bins[i - 1]; i--)
146 continue;
6aa452a6 147 n_bins = i;
613f5e1a
AD
148
149 /* 2 * ceil (log10 (2) * (N - 1)) + 1. */
150 max_width = 2 * (unsigned int) (0.30103 * (n_bins - 1) + 0.9999) + 1;
151
152 fprintf (file, "%s %s", name, msg);
153 for (i = 0; i < 2; i++)
2d382ea8 154 fprintf (file, "%*d\t%8u (%5.1f%%)\n",
613f5e1a
AD
155 max_width, i, bins[i], 100.0 * bins[i] / total);
156
6aa452a6 157 for (; i < n_bins; i++)
2d382ea8 158 fprintf (file, "%*lu-%lu\t%8u (%5.1f%%)\n",
613f5e1a 159 max_width - ((unsigned int) (0.30103 * (i) + 0.9999) + 1),
779e7ceb
PE
160 1UL << (i - 1),
161 (1UL << i) - 1,
2d382ea8 162 bins[i],
613f5e1a
AD
163 (100.0 * bins[i]) / total);
164}
165
166
167/* Print bitset statistics to FILE. */
168static void
d65ec44e
PE
169bitset_stats_print_1 (FILE *file, const char *name,
170 struct bitset_type_info_struct *stats)
613f5e1a
AD
171{
172 if (!stats)
173 return;
d65ec44e 174
613f5e1a 175 fprintf (file, "%s:\n", name);
2d382ea8 176 fprintf (file, _("%u bitset_allocs, %u freed (%.2f%%).\n"),
613f5e1a
AD
177 stats->allocs, stats->frees,
178 stats->allocs ? 100.0 * stats->frees / stats->allocs : 0);
2d382ea8 179 fprintf (file, _("%u bitset_sets, %u cached (%.2f%%)\n"),
613f5e1a
AD
180 stats->sets, stats->cache_sets,
181 stats->sets ? 100.0 * stats->cache_sets / stats->sets : 0);
2d382ea8 182 fprintf (file, _("%u bitset_resets, %u cached (%.2f%%)\n"),
613f5e1a
AD
183 stats->resets, stats->cache_resets,
184 stats->resets ? 100.0 * stats->cache_resets / stats->resets : 0);
2d382ea8 185 fprintf (file, _("%u bitset_tests, %u cached (%.2f%%)\n"),
613f5e1a
AD
186 stats->tests, stats->cache_tests,
187 stats->tests ? 100.0 * stats->cache_tests / stats->tests : 0);
188
2d382ea8 189 fprintf (file, _("%u bitset_lists\n"), stats->lists);
613f5e1a 190
c131cbff 191 bitset_log_histogram_print (file, name, _("count log histogram\n"),
613f5e1a
AD
192 BITSET_LOG_COUNT_BINS, stats->list_counts);
193
c131cbff 194 bitset_log_histogram_print (file, name, _("size log histogram\n"),
613f5e1a
AD
195 BITSET_LOG_SIZE_BINS, stats->list_sizes);
196
c131cbff 197 bitset_percent_histogram_print (file, name, _("density histogram\n"),
613f5e1a
AD
198 BITSET_DENSITY_BINS, stats->list_density);
199}
200
201
202/* Print all bitset statistics to FILE. */
203static void
d0829076 204bitset_stats_print (FILE *file, bool verbose ATTRIBUTE_UNUSED)
613f5e1a
AD
205{
206 int i;
613f5e1a
AD
207
208 if (!bitset_stats_info)
209 return;
210
c131cbff 211 fprintf (file, _("Bitset statistics:\n\n"));
613f5e1a
AD
212
213 if (bitset_stats_info->runs > 1)
2d382ea8 214 fprintf (file, _("Accumulated runs = %u\n"), bitset_stats_info->runs);
613f5e1a
AD
215
216 for (i = 0; i < BITSET_TYPE_NUM; i++)
d65ec44e 217 bitset_stats_print_1 (file, bitset_type_names[i],
6aa452a6 218 &bitset_stats_info->types[i]);
613f5e1a
AD
219}
220
221
222/* Initialise bitset statistics logging. */
223void
d65ec44e 224bitset_stats_enable (void)
613f5e1a
AD
225{
226 if (!bitset_stats_info)
227 bitset_stats_info = &bitset_stats_info_data;
d0829076 228 bitset_stats_enabled = true;
613f5e1a
AD
229}
230
231
232void
d65ec44e 233bitset_stats_disable (void)
613f5e1a 234{
d0829076 235 bitset_stats_enabled = false;
613f5e1a
AD
236}
237
238
239/* Read bitset statistics file. */
240void
48b16bbc 241bitset_stats_read (const char *file_name)
613f5e1a
AD
242{
243 FILE *file;
244
245 if (!bitset_stats_info)
246 return;
d65ec44e 247
48b16bbc
PE
248 if (!file_name)
249 file_name = BITSET_STATS_FILE;
613f5e1a 250
48b16bbc 251 file = fopen (file_name, "r");
613f5e1a
AD
252 if (file)
253 {
254 if (fread (&bitset_stats_info_data, sizeof (bitset_stats_info_data),
255 1, file) != 1)
256 {
257 if (ferror (file))
c131cbff 258 perror (_("Could not read stats file."));
613f5e1a 259 else
c131cbff 260 fprintf (stderr, _("Bad stats file size.\n"));
613f5e1a 261 }
11a71262
PE
262 if (fclose (file) != 0)
263 perror (_("Could not read stats file."));
613f5e1a
AD
264 }
265 bitset_stats_info_data.runs++;
266}
267
268
269/* Write bitset statistics file. */
270void
48b16bbc 271bitset_stats_write (const char *file_name)
613f5e1a
AD
272{
273 FILE *file;
274
275 if (!bitset_stats_info)
276 return;
277
48b16bbc
PE
278 if (!file_name)
279 file_name = BITSET_STATS_FILE;
613f5e1a 280
48b16bbc 281 file = fopen (file_name, "w");
613f5e1a
AD
282 if (file)
283 {
284 if (fwrite (&bitset_stats_info_data, sizeof (bitset_stats_info_data),
285 1, file) != 1)
c131cbff 286 perror (_("Could not write stats file."));
11a71262
PE
287 if (fclose (file) != 0)
288 perror (_("Could not write stats file."));
613f5e1a
AD
289 }
290 else
c131cbff 291 perror (_("Could not open stats file for writing."));
613f5e1a
AD
292}
293
294
295/* Dump bitset statistics to FILE. */
296void
d65ec44e 297bitset_stats_dump (FILE *file)
613f5e1a 298{
d0829076 299 bitset_stats_print (file, false);
613f5e1a
AD
300}
301
302
303/* Function to be called from debugger to print bitset stats. */
304void
305debug_bitset_stats (void)
306{
d0829076 307 bitset_stats_print (stderr, true);
613f5e1a
AD
308}
309
310
311static void
d65ec44e 312bitset_stats_set (bitset dst, bitset_bindex bitno)
613f5e1a
AD
313{
314 bitset bset = dst->s.bset;
c131cbff
PE
315 bitset_windex wordno = bitno / BITSET_WORD_BITS;
316 bitset_windex offset = wordno - bset->b.cindex;
d65ec44e 317
613f5e1a
AD
318 BITSET_STATS_SETS_INC (bset);
319
320 if (offset < bset->b.csize)
321 {
c131cbff 322 bset->b.cdata[offset] |= (bitset_word) 1 << (bitno % BITSET_WORD_BITS);
613f5e1a
AD
323 BITSET_STATS_CACHE_SETS_INC (bset);
324 }
325 else
326 BITSET_SET_ (bset, bitno);
327}
328
329
330static void
d65ec44e 331bitset_stats_reset (bitset dst, bitset_bindex bitno)
613f5e1a
AD
332{
333 bitset bset = dst->s.bset;
c131cbff
PE
334 bitset_windex wordno = bitno / BITSET_WORD_BITS;
335 bitset_windex offset = wordno - bset->b.cindex;
d65ec44e 336
613f5e1a
AD
337 BITSET_STATS_RESETS_INC (bset);
338
339 if (offset < bset->b.csize)
340 {
c131cbff
PE
341 bset->b.cdata[offset] &=
342 ~((bitset_word) 1 << (bitno % BITSET_WORD_BITS));
613f5e1a
AD
343 BITSET_STATS_CACHE_RESETS_INC (bset);
344 }
345 else
346 BITSET_RESET_ (bset, bitno);
347}
348
349
d0829076 350static bool
d65ec44e 351bitset_stats_toggle (bitset src, bitset_bindex bitno)
6aa452a6
AD
352{
353 return BITSET_TOGGLE_ (src->s.bset, bitno);
354}
355
356
d0829076 357static bool
d65ec44e 358bitset_stats_test (bitset src, bitset_bindex bitno)
613f5e1a
AD
359{
360 bitset bset = src->s.bset;
c131cbff
PE
361 bitset_windex wordno = bitno / BITSET_WORD_BITS;
362 bitset_windex offset = wordno - bset->b.cindex;
613f5e1a
AD
363
364 BITSET_STATS_TESTS_INC (bset);
d65ec44e 365
613f5e1a
AD
366 if (offset < bset->b.csize)
367 {
368 BITSET_STATS_CACHE_TESTS_INC (bset);
369 return (bset->b.cdata[offset] >> (bitno % BITSET_WORD_BITS)) & 1;
370 }
371 else
372 return BITSET_TEST_ (bset, bitno);
373}
374
375
eaef5507
PE
376static bitset_bindex
377bitset_stats_resize (bitset src, bitset_bindex size)
378{
379 return BITSET_RESIZE_ (src->s.bset, size);
380}
381
382
2d382ea8 383static bitset_bindex
d65ec44e 384bitset_stats_size (bitset src)
613f5e1a
AD
385{
386 return BITSET_SIZE_ (src->s.bset);
387}
388
389
2d382ea8 390static bitset_bindex
d65ec44e 391bitset_stats_count (bitset src)
6aa452a6
AD
392{
393 return BITSET_COUNT_ (src->s.bset);
394}
395
396
d0829076 397static bool
d65ec44e 398bitset_stats_empty_p (bitset dst)
6aa452a6
AD
399{
400 return BITSET_EMPTY_P_ (dst->s.bset);
401}
402
403
404static void
d65ec44e 405bitset_stats_ones (bitset dst)
6aa452a6
AD
406{
407 BITSET_ONES_ (dst->s.bset);
408}
409
410
411static void
d65ec44e 412bitset_stats_zero (bitset dst)
6aa452a6
AD
413{
414 BITSET_ZERO_ (dst->s.bset);
415}
416
417
418static void
d65ec44e 419bitset_stats_copy (bitset dst, bitset src)
6aa452a6
AD
420{
421 BITSET_CHECK2_ (dst, src);
422 BITSET_COPY_ (dst->s.bset, src->s.bset);
423}
424
425
d0829076 426static bool
d65ec44e 427bitset_stats_disjoint_p (bitset dst, bitset src)
613f5e1a 428{
6aa452a6
AD
429 BITSET_CHECK2_ (dst, src);
430 return BITSET_DISJOINT_P_ (dst->s.bset, src->s.bset);
613f5e1a
AD
431}
432
433
d0829076 434static bool
d65ec44e 435bitset_stats_equal_p (bitset dst, bitset src)
613f5e1a
AD
436{
437 BITSET_CHECK2_ (dst, src);
6aa452a6
AD
438 return BITSET_EQUAL_P_ (dst->s.bset, src->s.bset);
439}
440
441
442static void
d65ec44e 443bitset_stats_not (bitset dst, bitset src)
6aa452a6
AD
444{
445 BITSET_CHECK2_ (dst, src);
446 BITSET_NOT_ (dst->s.bset, src->s.bset);
447}
448
449
d0829076 450static bool
d65ec44e 451bitset_stats_subset_p (bitset dst, bitset src)
6aa452a6
AD
452{
453 BITSET_CHECK2_ (dst, src);
454 return BITSET_SUBSET_P_ (dst->s.bset, src->s.bset);
455}
456
457
458static void
d65ec44e 459bitset_stats_and (bitset dst, bitset src1, bitset src2)
6aa452a6
AD
460{
461 BITSET_CHECK3_ (dst, src1, src2);
462 BITSET_AND_ (dst->s.bset, src1->s.bset, src2->s.bset);
463}
464
465
d0829076 466static bool
d65ec44e 467bitset_stats_and_cmp (bitset dst, bitset src1, bitset src2)
6aa452a6
AD
468{
469 BITSET_CHECK3_ (dst, src1, src2);
470 return BITSET_AND_CMP_ (dst->s.bset, src1->s.bset, src2->s.bset);
471}
472
473
474static void
d65ec44e 475bitset_stats_andn (bitset dst, bitset src1, bitset src2)
6aa452a6
AD
476{
477 BITSET_CHECK3_ (dst, src1, src2);
478 BITSET_ANDN_ (dst->s.bset, src1->s.bset, src2->s.bset);
479}
480
481
d0829076 482static bool
d65ec44e 483bitset_stats_andn_cmp (bitset dst, bitset src1, bitset src2)
6aa452a6
AD
484{
485 BITSET_CHECK3_ (dst, src1, src2);
486 return BITSET_ANDN_CMP_ (dst->s.bset, src1->s.bset, src2->s.bset);
487}
488
489
490static void
d65ec44e 491bitset_stats_or (bitset dst, bitset src1, bitset src2)
6aa452a6
AD
492{
493 BITSET_CHECK3_ (dst, src1, src2);
494 BITSET_OR_ (dst->s.bset, src1->s.bset, src2->s.bset);
495}
496
497
d0829076 498static bool
d65ec44e 499bitset_stats_or_cmp (bitset dst, bitset src1, bitset src2)
6aa452a6
AD
500{
501 BITSET_CHECK3_ (dst, src1, src2);
502 return BITSET_OR_CMP_ (dst->s.bset, src1->s.bset, src2->s.bset);
503}
504
505
506static void
d65ec44e 507bitset_stats_xor (bitset dst, bitset src1, bitset src2)
6aa452a6
AD
508{
509 BITSET_CHECK3_ (dst, src1, src2);
510 BITSET_XOR_ (dst->s.bset, src1->s.bset, src2->s.bset);
613f5e1a
AD
511}
512
513
d0829076 514static bool
d65ec44e 515bitset_stats_xor_cmp (bitset dst, bitset src1, bitset src2)
613f5e1a
AD
516{
517 BITSET_CHECK3_ (dst, src1, src2);
6aa452a6
AD
518 return BITSET_XOR_CMP_ (dst->s.bset, src1->s.bset, src2->s.bset);
519}
520
521
522static void
d65ec44e 523bitset_stats_and_or (bitset dst, bitset src1, bitset src2, bitset src3)
6aa452a6
AD
524{
525 BITSET_CHECK4_ (dst, src1, src2, src3);
6aa452a6 526 BITSET_AND_OR_ (dst->s.bset, src1->s.bset, src2->s.bset, src3->s.bset);
613f5e1a
AD
527}
528
529
d0829076 530static bool
d65ec44e 531bitset_stats_and_or_cmp (bitset dst, bitset src1, bitset src2, bitset src3)
613f5e1a
AD
532{
533 BITSET_CHECK4_ (dst, src1, src2, src3);
6aa452a6
AD
534 return BITSET_AND_OR_CMP_ (dst->s.bset, src1->s.bset, src2->s.bset, src3->s.bset);
535}
536
537
538static void
d65ec44e 539bitset_stats_andn_or (bitset dst, bitset src1, bitset src2, bitset src3)
6aa452a6
AD
540{
541 BITSET_CHECK4_ (dst, src1, src2, src3);
6aa452a6
AD
542 BITSET_ANDN_OR_ (dst->s.bset, src1->s.bset, src2->s.bset, src3->s.bset);
543}
544
545
d0829076 546static bool
d65ec44e 547bitset_stats_andn_or_cmp (bitset dst, bitset src1, bitset src2, bitset src3)
6aa452a6
AD
548{
549 BITSET_CHECK4_ (dst, src1, src2, src3);
6aa452a6
AD
550 return BITSET_ANDN_OR_CMP_ (dst->s.bset, src1->s.bset, src2->s.bset, src3->s.bset);
551}
552
553
554static void
d65ec44e 555bitset_stats_or_and (bitset dst, bitset src1, bitset src2, bitset src3)
6aa452a6
AD
556{
557 BITSET_CHECK4_ (dst, src1, src2, src3);
6aa452a6
AD
558 BITSET_OR_AND_ (dst->s.bset, src1->s.bset, src2->s.bset, src3->s.bset);
559}
560
561
d0829076 562static bool
d65ec44e 563bitset_stats_or_and_cmp (bitset dst, bitset src1, bitset src2, bitset src3)
6aa452a6
AD
564{
565 BITSET_CHECK4_ (dst, src1, src2, src3);
6aa452a6 566 return BITSET_OR_AND_CMP_ (dst->s.bset, src1->s.bset, src2->s.bset, src3->s.bset);
613f5e1a
AD
567}
568
569
2d382ea8 570static bitset_bindex
d65ec44e
PE
571bitset_stats_list (bitset bset, bitset_bindex *list,
572 bitset_bindex num, bitset_bindex *next)
613f5e1a
AD
573{
574 bitset_bindex count;
575 bitset_bindex tmp;
576 bitset_bindex size;
577 bitset_bindex i;
eaef5507 578 enum bitset_type type;
613f5e1a
AD
579
580 count = BITSET_LIST_ (bset->s.bset, list, num, next);
d65ec44e 581
eaef5507 582 type = BITSET_TYPE_ (bset->s.bset);
613f5e1a 583 BITSET_STATS_LISTS_INC (bset->s.bset);
d65ec44e 584
613f5e1a
AD
585 /* Log histogram of number of set bits. */
586 for (i = 0, tmp = count; tmp; tmp >>= 1, i++)
587 continue;
588 if (i >= BITSET_LOG_COUNT_BINS)
589 i = BITSET_LOG_COUNT_BINS - 1;
590 BITSET_STATS_LIST_COUNTS_INC (bset->s.bset, i);
d65ec44e 591
613f5e1a
AD
592 /* Log histogram of number of bits in set. */
593 size = BITSET_SIZE_ (bset->s.bset);
594 for (i = 0, tmp = size; tmp; tmp >>= 1, i++)
595 continue;
596 if (i >= BITSET_LOG_SIZE_BINS)
597 i = BITSET_LOG_SIZE_BINS - 1;
598 BITSET_STATS_LIST_SIZES_INC (bset->s.bset, i);
d65ec44e 599
613f5e1a
AD
600 /* Histogram of fraction of bits set. */
601 i = size ? (count * BITSET_DENSITY_BINS) / size : 0;
602 if (i >= BITSET_DENSITY_BINS)
603 i = BITSET_DENSITY_BINS - 1;
604 BITSET_STATS_LIST_DENSITY_INC (bset->s.bset, i);
605 return count;
606}
607
608
2d382ea8 609static bitset_bindex
d65ec44e
PE
610bitset_stats_list_reverse (bitset bset, bitset_bindex *list,
611 bitset_bindex num, bitset_bindex *next)
613f5e1a 612{
6aa452a6 613 return BITSET_LIST_REVERSE_ (bset->s.bset, list, num, next);
613f5e1a
AD
614}
615
616
617static void
d65ec44e 618bitset_stats_free (bitset bset)
613f5e1a
AD
619{
620 BITSET_STATS_FREES_INC (bset->s.bset);
621 BITSET_FREE_ (bset->s.bset);
622}
623
624
6aa452a6 625struct bitset_vtable bitset_stats_vtable = {
613f5e1a
AD
626 bitset_stats_set,
627 bitset_stats_reset,
6aa452a6 628 bitset_stats_toggle,
613f5e1a 629 bitset_stats_test,
eaef5507 630 bitset_stats_resize,
613f5e1a 631 bitset_stats_size,
6aa452a6
AD
632 bitset_stats_count,
633 bitset_stats_empty_p,
634 bitset_stats_ones,
635 bitset_stats_zero,
636 bitset_stats_copy,
637 bitset_stats_disjoint_p,
638 bitset_stats_equal_p,
639 bitset_stats_not,
640 bitset_stats_subset_p,
641 bitset_stats_and,
642 bitset_stats_and_cmp,
643 bitset_stats_andn,
644 bitset_stats_andn_cmp,
645 bitset_stats_or,
646 bitset_stats_or_cmp,
647 bitset_stats_xor,
648 bitset_stats_xor_cmp,
649 bitset_stats_and_or,
650 bitset_stats_and_or_cmp,
651 bitset_stats_andn_or,
652 bitset_stats_andn_or_cmp,
653 bitset_stats_or_and,
654 bitset_stats_or_and_cmp,
613f5e1a 655 bitset_stats_list,
6aa452a6 656 bitset_stats_list_reverse,
613f5e1a
AD
657 bitset_stats_free,
658 BITSET_STATS
659};
660
661
662/* Return enclosed bitset type. */
663enum bitset_type
d65ec44e 664bitset_stats_type_get (bitset bset)
613f5e1a
AD
665{
666 return BITSET_TYPE_ (bset->s.bset);
667}
668
669
2d382ea8
PE
670size_t
671bitset_stats_bytes (void)
613f5e1a 672{
55e728eb 673 return sizeof (struct bitset_stats_struct);
613f5e1a
AD
674}
675
676
677bitset
d65ec44e 678bitset_stats_init (bitset bset, bitset_bindex n_bits, enum bitset_type type)
613f5e1a 679{
2d382ea8 680 size_t bytes;
613f5e1a
AD
681 bitset sbset;
682
6aa452a6 683 bset->b.vtable = &bitset_stats_vtable;
613f5e1a
AD
684
685 /* Disable cache. */
686 bset->b.cindex = 0;
687 bset->b.csize = 0;
688 bset->b.cdata = 0;
689
eaef5507
PE
690 BITSET_NBITS_ (bset) = n_bits;
691
613f5e1a
AD
692 /* Set up the actual bitset implementation that
693 we are a wrapper over. */
694 switch (type)
695 {
68cae94e
PE
696 default:
697 abort ();
698
613f5e1a
AD
699 case BITSET_ARRAY:
700 bytes = abitset_bytes (n_bits);
89519adf 701 sbset = xcalloc (1, bytes);
613f5e1a
AD
702 abitset_init (sbset, n_bits);
703 break;
704
705 case BITSET_LIST:
706 bytes = lbitset_bytes (n_bits);
89519adf 707 sbset = xcalloc (1, bytes);
613f5e1a
AD
708 lbitset_init (sbset, n_bits);
709 break;
710
711 case BITSET_TABLE:
712 bytes = ebitset_bytes (n_bits);
89519adf 713 sbset = xcalloc (1, bytes);
613f5e1a
AD
714 ebitset_init (sbset, n_bits);
715 break;
716
eaef5507
PE
717 case BITSET_VARRAY:
718 bytes = vbitset_bytes (n_bits);
89519adf 719 sbset = xcalloc (1, bytes);
eaef5507
PE
720 vbitset_init (sbset, n_bits);
721 break;
613f5e1a
AD
722 }
723
724 bset->s.bset = sbset;
725
726 BITSET_STATS_ALLOCS_INC (type);
d65ec44e 727
613f5e1a
AD
728 return bset;
729}