Commit 09a98fe4 authored by Jonathon Duerig's avatar Jonathon Duerig

An update to route-histogram, adding command line options for fixed-size...

An update to route-histogram, adding command line options for fixed-size buckets. This allows better comparisons between the histograms of different methods.
parent c2b2d9ce
......@@ -23,6 +23,8 @@ using namespace std;
namespace arg
{
bool fixedSizeBuckets = false;
int bucketSize = 10;
int bucketCount = 11;
}
......@@ -38,13 +40,11 @@ public:
void parseArgs(int argc, char * argv[]);
void execute(void);
void setupRanges(int maxCount, map<int, size_t> & ranges);
void setupRanges(int maxCount, map<int, int> & ranges);
int getRange(int bucket, int bucketCount, int maxValue);
void fillBuckets(list<int> const & countList,
map<int, size_t> const & ranges,
vector<int> & buckets);
void printBuckets(map<int, size_t> const & ranges,
vector<int> const & buckets);
map<int, int> & ranges);
void printBuckets(map<int, int> const & ranges);
int main(int argc, char * argv[])
{
......@@ -78,6 +78,23 @@ void parseArgs(int argc, char * argv[])
}
arg::bucketCount = newBucketCount;
}
else if (strncmp(argv[i], "--bucket-size=",
sizeof("--bucket-size=") - 1) == 0)
{
int newBucketSize = 0;
istringstream buffer(argv[i] + sizeof("--bucket-size=") - 1);
buffer >> newBucketSize;
if (!buffer)
{
throw InvalidArgumentException(argv[i]);
}
arg::bucketSize = newBucketSize;
}
else if (strncmp(argv[i], "--fixed-size-buckets",
sizeof("--fixed-size-buckets") - 1) == 0)
{
arg::fixedSizeBuckets = true;
}
else
{
throw InvalidArgumentException(argv[i]);
......@@ -108,25 +125,30 @@ void execute(void)
getline(cin, bufferString);
}
map<int, size_t> ranges;
map<int, int> ranges;
setupRanges(maxCount, ranges);
vector<int> buckets;
buckets.resize(arg::bucketCount);
fill(buckets.begin(), buckets.end(), 0);
fillBuckets(countList, ranges, buckets);
printBuckets(ranges, buckets);
fillBuckets(countList, ranges);
printBuckets(ranges);
}
void setupRanges(int maxCount, map<int, size_t> & ranges)
void setupRanges(int maxCount, map<int, int> & ranges)
{
ranges[0] = 0;
size_t i = 1;
for ( ; i < static_cast<size_t>(arg::bucketCount + 1); ++i)
if (arg::fixedSizeBuckets)
{
ranges[getRange(static_cast<int>(i), arg::bucketCount,
maxCount)] = i-1;
ranges[1] = 0;
ranges[2] = 0;
ranges[3] = 0;
arg::bucketCount = maxCount / arg::bucketSize;
if (maxCount % arg::bucketSize != 0)
{
++(arg::bucketCount);
}
}
int i = 1;
for ( ; i < arg::bucketCount + 1; ++i)
{
ranges[getRange(i, arg::bucketCount, maxCount)] = 0;
}
}
......@@ -139,10 +161,17 @@ int getRange(int bucket, int bucketCount, int maxValue)
}
else if (bucket < bucketCount)
{
int num = bucket * maxValue;
int denom = bucketCount;
// round down on the division.
result = num/denom;
if (arg::fixedSizeBuckets)
{
result = bucket * arg::bucketSize;
}
else
{
int num = bucket * maxValue;
int denom = bucketCount;
// round down on the division.
result = num/denom;
}
}
else if (bucket = bucketCount)
{
......@@ -156,29 +185,27 @@ int getRange(int bucket, int bucketCount, int maxValue)
}
void fillBuckets(list<int> const & countList,
map<int, size_t> const & ranges,
vector<int> & buckets)
map<int, int> & ranges)
{
list<int>::const_iterator pos = countList.begin();
list<int>::const_iterator limit = countList.end();
for ( ; pos != limit; ++pos)
{
map<int, size_t>::const_iterator maxPos = ranges.lower_bound(*pos);
++(buckets[maxPos->second]);
map<int, int>::iterator maxPos = ranges.lower_bound(*pos);
++(maxPos->second);
}
}
void printBuckets(map<int, size_t> const & ranges,
vector<int> const & buckets)
void printBuckets(map<int, int> const & ranges)
{
map<int, size_t>::const_iterator pos = ranges.begin();
map<int, size_t>::const_iterator forward = pos;
map<int, int>::const_iterator pos = ranges.begin();
map<int, int>::const_iterator forward = pos;
++forward;
map<int, size_t>::const_iterator limit = ranges.end();
map<int, int>::const_iterator limit = ranges.end();
while(forward != limit)
{
cout << pos->first << " " << forward->first << " "
<< buckets[forward->second] << endl;
<< forward->second << endl;
++pos;
++forward;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment