Esempio n. 1
0
void iterate() {
    printf("size = %d\n", numbers.size());
    NumbersIter iter;
    for (iter = numbers.begin(); iter != numbers.end(); ++iter) {
        printf("  %d\n", *iter);
    }
}
Esempio n. 2
0
void find(int num) {
    NumbersIter iter = numbers.find(num);
    if (iter == numbers.end()) {
        printf("%d not found\n", num);
    } else {
        printf("%d found\n", num);
    }
}
Esempio n. 3
0
bool ParallelComputeAverages(Numbers const &numbers, Averages &averages)
{
    if (numbers.empty())
    {
        return false;
    }


    // sort numbers for mode and median
    Numbers sorted = numbers;
    Averages avgResult;
    int modeResult;
    double medianResult;

    // sort numbers for median / mode
    tbb::parallel_sort(sorted);

    auto medianFunc = [&medianResult, &sorted]()
    {
        medianResult = ComputeMedian(sorted);
    };

    auto avgFunc = [&avgResult, &numbers]()
    {
        auto grainSize = numbers.size() / thread::hardware_concurrency();
        Averages avg{0, 0, 0, 0, 0, 0};
        avgResult = tbb::parallel_reduce(
                Range(numbers.begin(), numbers.end(), grainSize), avg,
                CalcIntermediateAverages,
                AddIntermediateAverages);
    };

    auto modeFunc = [&modeResult, &sorted]()
    {
        modeResult = ComputeMode(sorted);
    };

    // invoke parallel execution of calculations
    tbb::parallel_invoke(avgFunc, modeFunc, medianFunc);

    // set result
    double size = (double) numbers.size();
    averages.arithmeticMean = avgResult.arithmeticMean / size;
    averages.geometricMean = exp(avgResult.geometricMean / size);
    averages.harmonicMean = size / avgResult.harmonicMean;
    averages.quadraticMean = sqrt(avgResult.quadraticMean / size);
    averages.mode = modeResult;
    averages.median = medianResult;

    return true;
}
Esempio n. 4
0
bool ComputeAverages(Numbers const &numbers, Averages &averages)
{
    if (numbers.empty())
    {
        return false;
    }
    Numbers sortedNumbers = numbers;
    sort(sortedNumbers.begin(),
         sortedNumbers.end());  // sorting required for mode and median
    averages.mode = ComputeMode(sortedNumbers);
    averages.median = ComputeMedian(sortedNumbers);
    ComputeMeans(numbers, averages);
    return true;
}
Esempio n. 5
0
void remove(int num) {
    NumbersIter iter = numbers.find(num);
    if (iter != numbers.end()) numbers.erase(iter);
}
Esempio n. 6
0
void PrintNumbers(Numbers const &numbers)
{
    cout << "Numbers: ";
    copy(numbers.begin(), numbers.end(), ostream_iterator<int>(cout, " "));
    cout << endl;
}