30 template <
class contents>
34 averager(
int entries = 100,
bool compacting =
true);
45 void add(contents value,
int count = 1);
54 int length()
const {
return _averages.length(); }
57 contents
average(
int start,
int end)
const;
92 :
averager<int>(entries, compacting) {}
101 template <
class contents>
103 : _do_compaction(compacting), _averages(), _entries(entries)
110 template <
class contents>
113 if (length() < 8)
return;
114 int end_whacking = _averages.length() / 4;
115 if (_do_compaction) {
116 contents whacked_average = average(0, end_whacking);
117 _averages.zap(1, end_whacking);
118 _averages[0].value = whacked_average;
119 _averages[0].count = end_whacking + 1;
120 }
else _averages.zap(0, end_whacking);
123 template <
class contents>
129 if (
int(_averages.length() + 2) * unit_size >= limit) compact();
132 template <
class contents>
136 to_add.
value = value;
137 to_add.
count = count;
138 check_for_compaction();
142 template <
class contents>
151 for (
int i = start; i <= end; i++) {
152 accum += get(i).value * get(i).count;
153 count += get(i).count;
155 if (!count) count = 1;
156 return accum / count;
159 template <
class contents>
163 for (
int i = 0; i < length(); i++) to_return += get(i).count;
Represents a sequential, ordered, contiguous collection of objects.
Maintains a list of numbers and provides the average for them.
void check_for_compaction()
checks whether the averager needs to be compacted yet or not.
weighted_entry get(int index) const
accesses the entry stored at the "index" specified.
int samples() const
returns the total number of samples recorded in the average.
void add(contents value, int count=1)
adds a new "value" to the averager, with an optional "count".
contents average() const
reports the overall average of the whole list.
int length() const
returns the current length of the averages list.
void compact()
chops off the oldest portion of the averager.
averager(int entries=100, bool compacting=true)
creates an averager whose list length is restricted to "entries".
contents average(int start, int end) const
reports the average over the range from "start" to "end" inclusive.
keeps an average on a stream of integers.
int_averager(int entries=100, bool compacting=true)
#define bounds_return(value, low, high, to_return)
Verifies that "value" is between "low" and "high", inclusive.
type minimum(type a, type b)
maximum returns the greater of two values.
bool negative(const type &a)
negative returns true if "a" is less than zero.
An extension to floating point primitives providing approximate equality.
const int AVERAGER_SIZE_LIMIT
structure holding a weighted chunk of the average.