/**
* Parallel Code Count
* atomic.h
* Multi-threaded (OpenMP) code count implementation using atomic increase.
*/
#pragma once
#include "reference.h"
struct AtomicCountCodes
{
// requires zero-filled amount array
void operator()(const std::string &text, int amount[COUNTERS]) const
{
// OpenMP 2.0 doesn't parallelize if the counter is unsigned
const std::ptrdiff_t size = text.size();
#pragma omp parallel for
for (std::ptrdiff_t i = 0; i < size; ++i)
{
int * const p = amount + static_cast<unsigned char>(text[i]);
#pragma omp atomic
(*p)++;
}
}
};