Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

histogram.hpp 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // histogram.hpp
  2. // Copyright (C) 2006 - 2009 MicroNeil Research Corporation
  3. // Class to capture a histogram of events using a <set>
  4. #pragma once
  5. #include <set>
  6. namespace codedweller {
  7. /** The Histogram class is managed set of HistogramRecords.
  8. *** We play some naughty tricks with pointers to break the rules and
  9. *** directly manipulate the counts of HistogramRecords stored in the
  10. *** set - thus saving space, complexity, and cycles. The set allows us
  11. *** to add new records as needed and locate existing records quickly.
  12. *** At any point in time, the set contains all of the event (hit) counts
  13. *** ordered by key.
  14. **/
  15. class HistogramRecord { // A record to assocate a key and count.
  16. public:
  17. int Key; // Here is the key.
  18. mutable int Count; // Here is the count.
  19. HistogramRecord(const int NewKey) : // We must have a key to make one.
  20. Key(NewKey), Count(0) {} // and a new one starts at count 0.
  21. bool operator<(const HistogramRecord& Right) const { // To live in a set we need to <
  22. return (Key < Right.Key); // properly based on the key.
  23. }
  24. };
  25. class Histogram : public std::set<HistogramRecord> { // A Histogram is a set of HistogramRecords
  26. private: // and a private hit counter...
  27. int HitCount;
  28. public:
  29. Histogram() : HitCount(0) {}
  30. // with a few extra public functions. The
  31. int hit(const int EventKey, const int Adjustment = 1) { // hit() method increments a specific count.
  32. HistogramRecord E(EventKey); // First, make a record for the event key.
  33. insert(E); // Insert the new record (if it's not there).
  34. set<HistogramRecord>::iterator iE = // Find either the pre-existing or the new
  35. find(E); // record for this key.
  36. int* C; // Play naughty pointer games to access
  37. C = const_cast<int*>(&((*iE).Count)); // the Count for this record inside the
  38. (*C) += Adjustment; // set and add our Adjustment to it.
  39. HitCount += Adjustment; // Accumulate the adjustments overall.
  40. return(*C); // Return the count for this key.
  41. }
  42. int Hits() { return HitCount; } // Return the sum of hits so far.
  43. void reset() { // Reset the histogram to zero.
  44. HitCount = 0; // That means no counts, and
  45. clear(); // an empty set of records.
  46. }
  47. };
  48. } // End namespace codedweller