File: Histogram.cpp

package info (click to toggle)
snap-aligner 1.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,988 kB
  • sloc: cpp: 36,500; ansic: 5,239; python: 227; makefile: 85; sh: 28
file content (103 lines) | stat: -rw-r--r-- 2,045 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*++

Module Name:

    Histogram.cpp

Abstract:

    Cheezy histogram class

Authors:

    Bill Bolosky, September, 2011

Environment:

    User mode service.

    This class is NOT thread safe.  It's the caller's responsibility to ensure that
    at most one thread uses an instance at any time.

Revision History:

--*/

#include "stdafx.h"
#include "Compat.h"
#include "Histogram.h"
#include "exit.h"

Histogram::Histogram(unsigned i_nBuckets, bool i_isExponential) :
    nBuckets(i_nBuckets), isExponential(i_isExponential)
{
    _ASSERT(nBuckets > 0);

    buckets = new Bucket[nBuckets];

    buckets[0].maxValue = 1;
    buckets[0].count = 0;

    for (unsigned i = 1; i < nBuckets; i++) {
        if (isExponential) {
            buckets[i].maxValue = buckets[i-1].maxValue * 2;
        } else {
            buckets[i].maxValue = buckets[i-1].maxValue +1;
        }
        buckets[i].count = 0;
    }
}

    unsigned
Histogram::getBucketCount(unsigned whichBucket) const
{
    _ASSERT(whichBucket < nBuckets);
    return buckets[whichBucket].count;
}

    unsigned
Histogram::getBucketMax(unsigned whichBucket) const
{
    _ASSERT(whichBucket < nBuckets);
    return buckets[whichBucket].maxValue;
}

    unsigned
Histogram::getBucketMin(unsigned whichBucket) const
{
    _ASSERT(whichBucket < nBuckets);
    if (0 == whichBucket) {
        return 0;
    }
    return buckets[whichBucket-1].maxValue + 1;
}

    void
Histogram::addToCount(unsigned value, unsigned amountToAdd)
{
    for (unsigned i = 0 ; i < nBuckets; i++) {  // It is called "cheezy" after all
        if (value <= buckets[i].maxValue) {
            buckets[i].count += amountToAdd;
            return;
        }
    }
    //
    // Overflow.  Just drop it.
    //
}

    void
Histogram::print() const
{
    printf("MaxValue    Count\n");
    printf("-------- --------\n");
    for (unsigned i = 0; i < nBuckets; i++) {
        printf("%8d %8d\n",buckets[i].maxValue, buckets[i].count);
    }
}

Histogram::~Histogram()
{
    delete [] buckets;
    buckets = NULL;
}