5 #ifndef CVMFS_UTIL_ALGORITHM_H_
6 #define CVMFS_UTIL_ALGORITHM_H_
22 #ifdef CVMFS_NAMESPACE_GUARD
23 namespace CVMFS_NAMESPACE_GUARD {
31 inline void SetBit(
unsigned int bit, T *field) {
32 *field |=
static_cast<T
>(1) << bit;
36 inline void ClearBit(
unsigned int bit, T *field) {
37 *field &= ~(
static_cast<T
>(1) << bit);
41 inline bool TestBit(
unsigned int bit,
const T field) {
42 return field & (
static_cast<T
>(1) << bit);
50 std::vector<T>
Shuffle(
const std::vector<T> &input,
Prng *prng) {
51 std::vector<T> shuffled(input);
52 unsigned N = shuffled.size();
54 for (
unsigned i = 0; i < N; ++i) {
55 const unsigned swap_idx = i + prng->
Next(N - i);
56 std::swap(shuffled[i], shuffled[swap_idx]);
67 template <
typename T,
typename U>
68 void SortTeam(std::vector<T> *tractor, std::vector<U> *towed) {
71 assert(tractor->size() == towed->size());
72 int N = tractor->size();
75 for (
int i = 1; i < N; ++i) {
76 T val_tractor = (*tractor)[i];
77 U val_towed = (*towed)[i];
79 for (pos = i-1; (pos >= 0) && ((*tractor)[pos] > val_tractor); --pos) {
80 (*tractor)[pos+1] = (*tractor)[pos];
81 (*towed)[pos+1] = (*towed)[pos];
83 (*tractor)[pos+1] = val_tractor;
84 (*towed)[pos+1] = val_towed;
89 template <
typename hashed_type>
91 size_t operator() (
const hashed_type key)
const {
93 return MurmurHash64A(&key,
sizeof(key), 0x9ce603115bba659bLLU);
120 double GetTime()
const;
147 void Add(uint64_t value) {
149 const unsigned int n = this->bins_.size() - 1;
151 for (i = 1; i <= n; i++) {
152 if (value < this->boundary_values_[i]) {
153 atomic_inc32(&(this->bins_[i]));
158 atomic_inc32(&(this->bins_[0]));
164 inline uint64_t
N() {
167 for (i = 0; i <= this->bins_.size() - 1; i++) {
168 n +=
static_cast<unsigned int>(atomic_read32(&(this->bins_[i])));
176 unsigned int GetQuantile(
float n);
178 std::string ToString();
180 void PrintLog2Histogram();
205 , recorder_(recorder)
218 #ifdef CVMFS_NAMESPACE_GUARD
222 #endif // CVMFS_UTIL_ALGORITHM_H_
std::vector< T > Shuffle(const std::vector< T > &input, Prng *prng)
uint64_t timestamp_start_
std::vector< unsigned int > boundary_values_
std::vector< atomic_int32 > bins_
double DiffTimeSeconds(struct timeval start, struct timeval end)
Log2Histogram * recorder_
assert((mem||(size==0))&&"Out Of Memory")
void SetBit(unsigned int bit, T *field)
void ClearBit(unsigned int bit, T *field)
HighPrecisionTimer(Log2Histogram *recorder)
bool TestBit(unsigned int bit, const T field)
uint64_t MurmurHash64A(const void *key, int len, uint64_t seed)
void SortTeam(std::vector< T > *tractor, std::vector< U > *towed)
uint32_t MurmurHash2(const void *key, int len, uint32_t seed)
uint32_t Next(const uint64_t boundary)