atlas  0.6
tally.h
Go to the documentation of this file.
1 
5 /*
6  Copyright (C) 2007 Marc van Leeuwen
7  part of the Atlas of Lie Groups and Representations
8 
9  For license information see the LICENSE file
10 */
11 
12 /* The purpose of this module it to provide a simple class template |TallyVec|
13  that can be used to maintain a histogram in an efficient and flexible way.
14  The class behaves as a vector of counters, indexed by unsigned numbers, and
15  the basic operation provided is to increment the counter for a given index.
16  The resulting vector can be written to are read back from a file, and an
17  operation is provided to make a histogram of the counter values themselves.
18 */
19 
20 #ifndef TALLY_H
21 #define TALLY_H
22 
23 #include <vector>
24 #include <map>
25 #include <iostream>
26 
27 namespace atlas {
28 namespace tally {
29 
30 /* The |TallyVec| class template has one type parameters that should be an
31  unsigned integral type: it is the type used for the primary counters,
32  determining the range of frequency that can be maintained most efficiently
33  (once a counter overflows it will be maintained in a less storage- and
34  time-efficient way). Typically it will be a small type to avoid wasting
35  space on the average low counter values. The type indexing the counters and
36  used for the counter values when they overflow are considered to be of less
37  impact, so both will be taken |unsigned long long int| in all cases.
38 */
39 
40 template <typename Count>
41 class TallyVec
42 {
43  public:
44  typedef unsigned long long int Index;
45  typedef unsigned long long int ullong; // when used as counter value
46  private:
47  typedef std::map<Index,ullong> map_type;
48 
49  static const Count maxCount; // limit of |Count| type
50 
51  std::vector<Count> count; // primary histogram
52  map_type overflow; // for multiplicities >=256
53  Index max; // maximal index seen
54  ullong total; // grans total
55 
56  public:
57  TallyVec(size_t limit) : count(0), overflow(), max(0), total(0)
58  { count.reserve(limit); }
59  TallyVec (std::istream& file); // recover table dumped to file
60 
61  // same specifying width of keys and values in overflow table explicitly
62  TallyVec (std::istream& file, size_t w_key, size_t w_val);
63 
64  bool tally (Index i); // increase count for i by 1; tell whether new
65  bool tally (Index i,ullong multiplicity); // same with multiplicity
66  Index size() const { return max+1; } // size of collection now tallied
67  inline ullong multiplicity (Index i) const;
68  ullong sum() const { return total; }
69 
70  template<typename MuCount> TallyVec<MuCount> derived(size_t limit) const;
71 
72  void advance(Index& i) const; // like ++ and -- where |i| iterates
73  bool lower(Index& i) const; // over indices with nonzero multiplicity
74 
75  // use |lower| as |i=t.size(); while (t.lower(i)) use(i);|
76 
77  void write_to(std::ostream& out) const; // dump table to file
78 }; // class TallyVec
79 
80 } // |namespace tally|
81 } // |namespace atlas|
82 
83 #include "tally_def.h"
84 
85 #endif
void write_to(std::ostream &out) const
Definition: tally_def.h:129
ullong total
Definition: tally.h:54
Index max
Definition: tally.h:53
char * limit
Definition: common.c:91
Index size() const
Definition: tally.h:66
ullong sum() const
Definition: tally.h:68
unsigned long long int Index
Definition: tally.h:44
std::map< Index, ullong > map_type
Definition: tally.h:47
static const Count maxCount
Definition: tally.h:49
Definition: tally.h:41
bool tally(Index i)
Definition: tally_def.h:25
void advance(Index &i) const
Definition: tally_def.h:94
#define out(c)
Definition: cweave.c:205
bool lower(Index &i) const
Definition: tally_def.h:110
TallyVec(size_t limit)
Definition: tally.h:57
TallyVec< MuCount > derived(size_t limit) const
Definition: tally_def.h:183
map_type overflow
Definition: tally.h:52
struct f file[max_include_depth]
Definition: common.c:93
Definition: Atlas.h:38
unsigned long long int ullong
Definition: tally.h:45
ullong multiplicity(Index i) const
Definition: tally_def.h:16
std::vector< Count > count
Definition: tally.h:51