atlas  0.6
tally_def.h
Go to the documentation of this file.
1 #include <limits>
2 #include <cassert>
3 #include <stdexcept>
4 
5 #include "basic_io.h"
6 
7 namespace atlas {
8 namespace tally {
9 
10 /* template class constants must be defined outside class definition */
11 
12 template <typename Count>
13  const Count TallyVec<Count>::maxCount = std::numeric_limits<Count>::max();
14 
15 template <typename Count>
16  inline unsigned long long int TallyVec<Count>::multiplicity (Index i) const
17  {
18  if (i<count.size())
19  return count[i]!=maxCount ? count[i] : overflow.find(i)->second;
20  map_type::const_iterator it=overflow.find(i);
21  return it==overflow.end() ? 0 : it->second;
22  }
23 
24 template <typename Count>
26  {
27  ++total;
28  if (i<count.size()) // then |i| already recorded in |count| or |overflow|
29  {
30  if (count[i]!=maxCount)
31  if (++count[i]==maxCount)
32  overflow[i]=maxCount; // create entry upon hitting |maxCount|
33  else return count[i]==1; // just might be the first occurrence of |i|
34  else ++overflow[i];
35  return false;
36  }
37  if (i>max) max=i; // only need to check this if |i>=count.size()|
38  if (i<count.capacity()) // then slot for |i| can be created
39  {
40  while (count.size()<i) count.push_back(0); // clear new counters skipped
41  count.push_back(1); // install first tally for this counter
42  return true;
43  }
44 
45  // now |i>=count.capacity()|, it must be added to overflow
46  std::pair<map_type::iterator,bool> p =overflow.insert(std::make_pair(i,1));
47  if (not p.second) ++p.first->second; // if already recorded, increase tally
48  return p.second;
49  }
50 
51 template <typename Count>
52  bool TallyVec<Count>::tally(Index i,ullong multiplicity)
53  {
54  total+=multiplicity;
55  if (i<count.size()) // then |i| already recorded in |count| or |overflow|
56  {
57  if (count[i]!=maxCount)
58  if (count[i]+multiplicity<maxCount) // then |count[i]| not saturated
59  {
60  bool result= count[i]==0; // this might just be the first occurence
61  count[i]+=multiplicity;
62  return result;
63  }
64  else
65  {
66  overflow[i]=count[i]+multiplicity; // create new entry
67  count[i]=maxCount; // and mark |count[i]| as saturated
68  }
69  else overflow[i]+=multiplicity;
70  return false;
71  }
72  if (i>max) max=i;
73  if (i<count.capacity()) // then slot for |i| can be created
74  {
75  while (count.size()<i) count.push_back(0);
76  if (multiplicity>=maxCount) // then |count[i]| saturated
77  {
78  overflow[i]=multiplicity; // create new entry
79  multiplicity=maxCount; // and mark |count[i]| as saturated
80  }
81  count.push_back(multiplicity);
82  return true;
83  }
84 
85  // now |i>=count.capacity()|, it must be added to overflow
86  std::pair<map_type::iterator,bool> p
87  =overflow.insert(std::make_pair(i,multiplicity));
88  if (not p.second) // then it was already recorded
89  p.first->second+=multiplicity; // so increase tally for |i| instead
90  return p.second; // return whether |i| was previously unrecorded
91  }
92 
93 template <typename Count>
95  {
96  if (i>=max) { i=size(); return; } // avoid fruitless search
97 
98  ++i; // make sure we advance at least by one
99  while (i<count.size())
100  if (count[i]!=0) return;
101  else ++i;
102 
103  // now find the first entry |j| in |overflow| with |j>=i|
104  map_type::const_iterator it=overflow.lower_bound(i);
105  assert(it!=overflow.end()); // there is at least one |j>=i|
106  i=it->first;
107  }
108 
109 template <typename Count>
111  {
112  if (i>count.size()) // then find last entry |j| in overflow with |j<i|
113  {
114  map_type::const_iterator it=overflow.lower_bound(i);
115  if (it!=overflow.begin() and (--it)->first>=count.size())
116  { i=it->first; return true; }
117 
118  else i=count.size(); // and fall through to search in |count|
119  }
120 
121  // now |i<=count.size()|; find last entry |j<i| in count with |count[j]!=0|
122  while (i-->0)
123  if (count[i]!=0) return true;
124 
125  return false; // could not find lower entry
126 }
127 
128 template <typename Count>
129  void TallyVec<Count>::write_to(std::ostream& out) const
130  {
131  basic_io::put_int(count.size(),out);
133  for (size_t i=0; i<count.size(); ++i)
134  basic_io::write_bytes<sizeof(Count)>(count[i],out);
135  for (map_type::const_iterator it=overflow.begin();
136  it!=overflow.end(); ++it)
137  {
138  basic_io::write_bytes<sizeof(Index)>(it->first,out);
139  basic_io::write_bytes<sizeof(ullong)>(it->second,out);
140  }
141 }
142 
143 template <typename Count>
145  : count(0), overflow(), max(0), total(0)
146  {
147  file.seekg(0,std::ios_base::beg);
148  count.resize(basic_io::read_bytes<4>(file));
149  if (not count.empty()) max=count.size()-1;
150  size_t ovf_size=basic_io::read_bytes<4>(file);
151  for (size_t i=0; i<count.size(); ++i)
152  count[i]=basic_io::read_bytes<sizeof(Count)>(file);
153  for (size_t i=0; i<ovf_size; ++i)
154  {
155  Index k=basic_io::read_bytes<sizeof(Index)>(file);
156  if (k>max) max=k;
157  ullong v=basic_io::read_bytes<sizeof(ullong)>(file);
158  overflow.insert(std::make_pair(k,v));
159  }
160  }
161 
162 template <typename Count>
163  TallyVec<Count>::TallyVec (std::istream& file, size_t w_key, size_t w_val)
164  : count(0), overflow(), max(0), total(0)
165  {
166  file.seekg(0,std::ios_base::beg);
167  count.resize(basic_io::read_bytes<4>(file));
168  if (not count.empty()) max=count.size()-1;
169  size_t ovf_size=basic_io::read_bytes<4>(file);
170  for (size_t i=0; i<count.size(); ++i)
171  count[i]=basic_io::read_bytes<sizeof(Count)>(file);
172  for (size_t i=0; i<ovf_size; ++i)
173  {
174  Index k=basic_io::read_var_bytes(w_key,file);
175  if (k>max) max=k;
176  ullong v=basic_io::read_var_bytes(w_val,file);
177  overflow.insert(std::make_pair(k,v));
178  }
179  }
180 
181 template <typename Count>
182  template <typename MuCount>
184  {
185  TallyVec<MuCount> mu (limit); mu.tally(0); // provide sentinel
186  for (Index i=0; i<size(); advance(i))
187  mu.tally(multiplicity(i));
188  return mu;
189  }
190 } // |namespace tally|
191 } // |namespace atlas|
unsigned long long read_bytes(std::istream &in)
Definition: basic_io_def.h:51
unsigned long long read_var_bytes(unsigned int n, std::istream &in)
Definition: basic_io.cpp:168
void write_to(std::ostream &out) const
Definition: tally_def.h:129
unsigned long size
Definition: testprint.cpp:46
ullong total
Definition: tally.h:54
uA p
Definition: lists.cpp:26
Index max
Definition: tally.h:53
char * limit
Definition: common.c:91
Index size() const
Definition: tally.h:66
unsigned long long int Index
Definition: tally.h:44
void put_int(unsigned int val, std::ostream &out)
Definition: basic_io.cpp:182
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
#define overflow(t)
Definition: common.c:53
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
Vertex v
Definition: graph.cpp:116
void write_bytes(unsigned int n, unsigned long long val, std::ostream &out)
Definition: basic_io.cpp:183