Skip to content

Instantly share code, notes, and snippets.

@bosmacs
Created February 25, 2015 14:05
Show Gist options
  • Save bosmacs/aad31a62cdbf08732856 to your computer and use it in GitHub Desktop.
Save bosmacs/aad31a62cdbf08732856 to your computer and use it in GitHub Desktop.
Instrumented type
#include "instrumented.hpp"
// necessary static initialization -- this needs to be done for each type we want to instrument
template<> size_t instrumented<short>::comparison_count = 0;
template<> size_t instrumented<int>::comparison_count = 0;
template<> size_t instrumented<float>::comparison_count = 0;
template<> size_t instrumented<double>::comparison_count = 0;
#ifndef INSTRUMENTED_VALUE
#define INSTRUMENTED_VALUE
#include <cstddef>
template<typename T>
struct instrumented {
typedef T value_type;
// copy assignment from value_type
instrumented<T>& operator=(const T& other) {
this->value = other;
return *this;
}
// implicit conversion to value type
operator T() const { return value; }
bool operator <=(const instrumented<T>& other) const {
comparison_count++;
return value <= other.value;
}
bool operator >=(const instrumented<T>& other) const {
comparison_count++;
return value >= other.value;
}
/*
bool operator ==(const instrumented<T>& other) const {
return value == other.value;
}
bool operator !=(const instrumented<T>& other) const {
return value != other.value;
}
*/
static size_t count() { return comparison_count; }
static void reset() { comparison_count = 0; }
private:
value_type value;
static size_t comparison_count;
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment