Click
Loading...
Searching...
No Matches
statvector.hh
1// -*- c-basic-offset: 4 -*-
2#ifndef CLICK_STATVECTOR_HH
3#define CLICK_STATVECTOR_HH
4#include <click/batchelement.hh>
5#include <click/multithread.hh>
6#include <click/vector.hh>
7#include <click/straccum.hh>
8#include <click/statvector.hh>
9CLICK_DECLS
10
11
12template <typename T>
13class StatVector {
14
15
16
17 enum{H_MEDIAN=736,H_AVERAGE,H_DUMP,H_MAX_OBS,H_N_OBS,H_NZ,H_MAX,H_MAX_OBS_VAL};
18
19 static String read_handler(Element *e, void *thunk)
20 {
21 StatVector *fd = (StatVector*)e->cast("StatVector");
22 switch ((intptr_t)thunk) {
23 case H_MAX:
24 case H_NZ:
25 case H_MAX_OBS: {
26 Vector<T> sums(fd->stats.get_value(0).size(),0);
27 T max_batch_v = -1;
28 int max_batch_index = -1;
29 int max_nz = -1;
30 int nz=0;
31
32 for (unsigned j = 0; j < (unsigned)sums.size(); j++) {
33 for (unsigned i = 0; i < fd->stats.weight(); i++) {
34 sums[j] += fd->stats.get_value(i)[j];
35 }
36 if (sums[j] > max_batch_v) {
37 max_batch_v = sums[j];
38 max_batch_index = j;
39 }
40 if (sums[j] > 0) {
41 max_nz = j;
42 nz++;
43 }
44 }
45 if ((intptr_t)thunk == H_MAX) {
46 return String(max_nz);
47 } else if ((intptr_t)thunk == H_NZ) {
48 return String(nz);
49 } else
50 return String(max_batch_index);
51 }
52 case H_N_OBS:
53 case H_MEDIAN: {
54 Vector<T> sums(fd->stats.get_value(0).size(),0);
55 T total = 0;
56 for (unsigned j = 0; j < (unsigned)sums.size(); j++) {
57 for (unsigned i = 0; i < fd->stats.weight(); i++) {
58 sums[j] += fd->stats.get_value(i)[j];
59 }
60 total += sums[j];
61 }
62 if ((intptr_t)thunk == H_N_OBS)
63 return String(total);
64 T val = 0;
65 for (int i = 0; i < sums.size(); i++) {
66 val += sums[i];
67 if (val > total/2)
68 return String(i);
69 }
70 return "0";
71 }
72 case H_AVERAGE: {
73 int count = 0;
74 int total = 0;
75 for (unsigned i = 0; i < fd->stats.weight(); i++) {
76 for (unsigned j = 0; j < (unsigned)fd->stats.get_value(i).size(); j++) {
77 total += fd->stats.get_value(i)[j] * j;
78 count += fd->stats.get_value(i)[j];
79 }
80 }
81 if (count > 0)
82 return String((double)total/(double)count);
83 else
84 return String(0);
85 }
86 case H_DUMP: {
88 Vector<T> sums(fd->stats.get_value(0).size(),0);
89 for (unsigned i = 0; i < fd->stats.weight(); i++) {
90 for (unsigned j = 0; j < (unsigned)fd->stats.get_value(i).size(); j++) {
91 sums[j] += fd->stats.get_value(i)[j];
92 if (i == fd->stats.weight() - 1 && sums[j] != 0)
93 s << j << ": " << sums[j] << "\n";
94 }
95 }
96 return s.take_string();
97 }
98 default:
99 return "<error>";
100 }
101 }
102
103protected:
105
106 StatVector() {
107
108 }
109
110 StatVector(Vector<T> v) : stats(v) {
111
112 }
113 void add_stat_handler(Element* e) {
114 //Value the most seen (gives the value)
115 e->add_read_handler("most_seen", read_handler, H_MAX_OBS, Handler::f_expensive);
116 //Value the most seen (gives the frequency of the value)
117 e->add_read_handler("most_seen_freq", read_handler, H_MAX_OBS_VAL, Handler::f_expensive);
118 //Maximum value seen
119 e->add_read_handler("max", read_handler, H_MAX, Handler::f_expensive);
120 //Number of observations
121 e->add_read_handler("count", read_handler, H_N_OBS, Handler::f_expensive);
122 //Number of values that had at least one observations
123 e->add_read_handler("nval", read_handler, H_NZ, Handler::f_expensive);
124 //Value for the median number of observations
125 e->add_read_handler("median", read_handler, H_MEDIAN, Handler::f_expensive);
126 //Average of value*frequency
127 e->add_read_handler("average", read_handler, H_AVERAGE, Handler::f_expensive);
128 e->add_read_handler("avg", read_handler, H_AVERAGE, Handler::f_expensive);
129 //Dump all value: frequency
130 e->add_read_handler("dump", read_handler, H_DUMP, Handler::f_expensive);
131 }
132};
133
134CLICK_ENDDECLS
135#endif //CLICK_STATVECTOR_HH
Base class for Click elements.
Definition element.hh:42
void add_read_handler(const String &name, ReadHandlerCallback read_callback, const void *user_data=0, uint32_t flags=0)
Register a read handler named name.
Definition element.cc:2020
virtual void * cast(const char *name)
Attempt to cast the element to a named type.
Definition element.cc:515
@ f_expensive
Read handler is expensive to call.
Definition handler.hh:41
Efficiently build up Strings from pieces.
Definition straccum.hh:28
String take_string()
Return a String object with this StringAccum's contents.
Definition straccum.cc:211
A string of characters.
Definition string.hh:22
Vector template.
Definition vector.hh:111
Definition sync.hh:57
Click's StringAccum class, used to construct Strings efficiently from pieces.
Click's vector container template.