1 // Stats.h -- classes for generic statistics gathering
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
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.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "string_table.h"
35 typedef std::map
<string_table::key
, unsigned long int> Stat
;
39 /// @param label The label to print for dumps of this stat
40 /// @param st The string table to use for resolving stats values
41 /// @param dumpTrigger The number of calls to check() that should be
43 /// @param restrict If non-zero dumpTrigger refers to this key lookups
44 /// @param dumpCount Number of items to print in the dump (makes sense if not restricted)
46 KeyLookup(const std::string
& label
, const string_table
& st
, int dumpTrigger
=0,
47 string_table::key restrict
=0, int dumpCount
=5)
50 _dumpCount(dumpCount
),
51 _dumpTrigger(dumpTrigger
),
61 void check(string_table::key k
) {
62 int gotTo
= ++stat
[k
];
63 if ( _restrict
&& k
!= _restrict
) return;
64 if ( ! _dumpTrigger
) return;
65 if ( ! ( gotTo
% _dumpTrigger
) ) dump(_dumpCount
);
68 void dump(int count
) {
69 typedef std::map
<unsigned long int, string_table::key
> Sorted
;
71 for (Stat::iterator i
=stat
.begin(), e
=stat
.end(); i
!=e
; ++i
)
72 sorted
[i
->second
] = i
->first
;
73 std::cerr
<< _label
<< " lookups: " << std::endl
;
74 for (Sorted::reverse_iterator i
=sorted
.rbegin(), e
=sorted
.rend();
80 << _st
.value(i
->second
) << "("
83 if ( ! --count
) break;
90 const string_table
& _st
;
94 string_table::key _restrict
;
99 } // namespace gnash.stats