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