statistics.hh (8296:be7f03723412) statistics.hh (8514:57c96df312a1)
1/*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;

--- 42 unchanged lines hidden (view full) ---

51#include <cassert>
52#ifdef __SUNPRO_CC
53#include <math.h>
54#endif
55#include <cmath>
56#include <functional>
57#include <iosfwd>
58#include <list>
1/*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;

--- 42 unchanged lines hidden (view full) ---

51#include <cassert>
52#ifdef __SUNPRO_CC
53#include <math.h>
54#endif
55#include <cmath>
56#include <functional>
57#include <iosfwd>
58#include <list>
59#include <map>
59#include <string>
60#include <vector>
61
62#include "base/stats/info.hh"
63#include "base/stats/output.hh"
64#include "base/stats/types.hh"
65#include "base/cast.hh"
66#include "base/cprintf.hh"

--- 2536 unchanged lines hidden (view full) ---

2603 return vec;
2604 }
2605 Result total() const { return this->s.total(); }
2606 VCounter &value() const { return cvec; }
2607
2608 std::string str() const { return this->s.str(); }
2609};
2610
60#include <string>
61#include <vector>
62
63#include "base/stats/info.hh"
64#include "base/stats/output.hh"
65#include "base/stats/types.hh"
66#include "base/cast.hh"
67#include "base/cprintf.hh"

--- 2536 unchanged lines hidden (view full) ---

2604 return vec;
2605 }
2606 Result total() const { return this->s.total(); }
2607 VCounter &value() const { return cvec; }
2608
2609 std::string str() const { return this->s.str(); }
2610};
2611
2612template <class Stat>
2613class SparseHistInfoProxy : public InfoProxy<Stat, SparseHistInfo>
2614{
2615 public:
2616 SparseHistInfoProxy(Stat &stat) : InfoProxy<Stat, SparseHistInfo>(stat) {}
2617};
2618
2619/**
2620 * Implementation of a sparse histogram stat. The storage class is
2621 * determined by the Storage template.
2622 */
2623template <class Derived, class Stor>
2624class SparseHistBase : public DataWrap<Derived, SparseHistInfoProxy>
2625{
2626 public:
2627 typedef SparseHistInfoProxy<Derived> Info;
2628 typedef Stor Storage;
2629 typedef typename Stor::Params Params;
2630
2631 protected:
2632 /** The storage for this stat. */
2633 char storage[sizeof(Storage)];
2634
2635 protected:
2636 /**
2637 * Retrieve the storage.
2638 * @return The storage object for this stat.
2639 */
2640 Storage *
2641 data()
2642 {
2643 return reinterpret_cast<Storage *>(storage);
2644 }
2645
2646 /**
2647 * Retrieve a const pointer to the storage.
2648 * @return A const pointer to the storage object for this stat.
2649 */
2650 const Storage *
2651 data() const
2652 {
2653 return reinterpret_cast<const Storage *>(storage);
2654 }
2655
2656 void
2657 doInit()
2658 {
2659 new (storage) Storage(this->info());
2660 this->setInit();
2661 }
2662
2663 public:
2664 SparseHistBase() { }
2665
2666 /**
2667 * Add a value to the distribtion n times. Calls sample on the storage
2668 * class.
2669 * @param v The value to add.
2670 * @param n The number of times to add it, defaults to 1.
2671 */
2672 template <typename U>
2673 void sample(const U &v, int n = 1) { data()->sample(v, n); }
2674
2675 /**
2676 * Return the number of entries in this stat.
2677 * @return The number of entries.
2678 */
2679 size_type size() const { return data()->size(); }
2680 /**
2681 * Return true if no samples have been added.
2682 * @return True if there haven't been any samples.
2683 */
2684 bool zero() const { return data()->zero(); }
2685
2686 void
2687 prepare()
2688 {
2689 Info *info = this->info();
2690 data()->prepare(info, info->data);
2691 }
2692
2693 /**
2694 * Reset stat value to default
2695 */
2696 void
2697 reset()
2698 {
2699 data()->reset(this->info());
2700 }
2701};
2702
2703/**
2704 * Templatized storage and interface for a sparse histogram stat.
2705 */
2706class SparseHistStor
2707{
2708 public:
2709 /** The parameters for a sparse histogram stat. */
2710 struct Params : public DistParams
2711 {
2712 Params() : DistParams(Hist) {}
2713 };
2714
2715 private:
2716 /** Counter for number of samples */
2717 Counter samples;
2718 /** Counter for each bucket. */
2719 MCounter cmap;
2720
2721 public:
2722 SparseHistStor(Info *info)
2723 {
2724 reset(info);
2725 }
2726
2727 /**
2728 * Add a value to the distribution for the given number of times.
2729 * @param val The value to add.
2730 * @param number The number of times to add the value.
2731 */
2732 void
2733 sample(Counter val, int number)
2734 {
2735 cmap[val] += number;
2736 samples += number;
2737 }
2738
2739 /**
2740 * Return the number of buckets in this distribution.
2741 * @return the number of buckets.
2742 */
2743 size_type size() const { return cmap.size(); }
2744
2745 /**
2746 * Returns true if any calls to sample have been made.
2747 * @return True if any values have been sampled.
2748 */
2749 bool
2750 zero() const
2751 {
2752 return samples == Counter();
2753 }
2754
2755 void
2756 prepare(Info *info, SparseHistData &data)
2757 {
2758 MCounter::iterator it;
2759 data.cmap.clear();
2760 for (it = cmap.begin(); it != cmap.end(); it++) {
2761 data.cmap[(*it).first] = (*it).second;
2762 }
2763
2764 data.samples = samples;
2765 }
2766
2767 /**
2768 * Reset stat value to default
2769 */
2770 void
2771 reset(Info *info)
2772 {
2773 cmap.clear();
2774 samples = 0;
2775 }
2776};
2777
2778class SparseHistogram : public SparseHistBase<SparseHistogram, SparseHistStor>
2779{
2780 public:
2781 /**
2782 * Set the parameters of this histogram. @sa HistStor::Params
2783 * @param size The number of buckets in the histogram
2784 * @return A reference to this histogram.
2785 */
2786 SparseHistogram &
2787 init(size_type size)
2788 {
2789 SparseHistStor::Params *params = new SparseHistStor::Params;
2790 this->setParams(params);
2791 this->doInit();
2792 return this->self();
2793 }
2794};
2795
2611class Temp;
2612/**
2613 * A formula for statistics that is calculated when printed. A formula is
2614 * stored as a tree of Nodes that represent the equation to calculate.
2615 * @sa Stat, ScalarStat, VectorStat, Node, Temp
2616 */
2617class Formula : public DataWrapVec<Formula, FormulaInfoProxy>
2618{

--- 332 unchanged lines hidden ---
2796class Temp;
2797/**
2798 * A formula for statistics that is calculated when printed. A formula is
2799 * stored as a tree of Nodes that represent the equation to calculate.
2800 * @sa Stat, ScalarStat, VectorStat, Node, Temp
2801 */
2802class Formula : public DataWrapVec<Formula, FormulaInfoProxy>
2803{

--- 332 unchanged lines hidden ---