swift
Loading...
Searching...
No Matches
samplesalgorithm.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) 2014 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
6
7#include "samplesalgorithm.h"
8
9#include <algorithm>
10#include <cmath>
11#include <ctime>
12#include <numeric>
13
14#include <QChar>
15#include <QDebug>
16#include <QList>
17#include <QString>
18#include <QStringList>
19#include <QTypeInfo>
20#include <QtDebug>
21
22#include "misc/algorithm.h"
23#include "misc/iterator.h"
24#include "misc/sequence.h"
25#include "misc/stringutils.h"
26
27namespace swift::sample
28{
30 {
32 for (int i = 1; i <= 100; ++i)
33 {
34 seq.push_back(i);
35 }
36 const int samples = 200;
38 {
39 for (int i = 0; i < samples; ++i)
40 {
41 auto randoms = seq.randomElements(10);
42 means.push_back(std::accumulate(randoms.cbegin(), randoms.cend(), 0) / 10);
43 }
44 int mean = std::accumulate(means.cbegin(), means.cend(), 0) / samples;
45 int stdDev = std::sqrt(std::accumulate(means.cbegin(), means.cend(), 0,
46 [&](int a, int n) { return a + (n - mean) * (n - mean); }) /
47 samples);
48 qDebug() << "randomElements";
49 qDebug() << "means:" << means;
50 qDebug() << "mean of the means:" << mean;
51 qDebug() << "std deviation of the means:" << stdDev;
52 }
53 means.clear();
54 {
55 for (int i = 0; i < samples; ++i)
56 {
57 auto randoms = seq.sampleElements(10);
58 means.push_back(std::accumulate(randoms.cbegin(), randoms.cend(), 0) / 10);
59 }
60 int mean = std::accumulate(means.cbegin(), means.cend(), 0) / samples;
61 int stdDev = std::sqrt(std::accumulate(means.cbegin(), means.cend(), 0,
62 [&](int a, int n) { return a + (n - mean) * (n - mean); }) /
63 samples);
64 qDebug() << "sampleElements";
65 qDebug() << "means:" << means;
66 qDebug() << "mean of the means:" << mean;
67 qDebug() << "std deviation of the means:" << stdDev;
68 }
69
70 QStringList src { "a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3" };
71 std::shuffle(src.begin(), src.end(), std::mt19937(static_cast<unsigned>(std::time(nullptr))));
72 qDebug() << src;
73 qDebug() << "topologicallySortedInsert";
74 QStringList dst;
75 int count = 0;
76 auto cmp = [&](const QString &a, const QString &b) {
77 count++;
78 return a[0] == b[0] && a[1] < b[1];
79 };
80 for (const auto &s : src)
81 {
83 }
84 qDebug() << count << "comparisons";
85 qDebug() << dst;
86 qDebug() << "topologicalSort";
87 count = 0;
88 swift::misc::topologicalSort(dst.begin(), dst.end(), cmp);
89 qDebug() << count << "comparisons";
90 qDebug() << dst;
91
92 return 0;
93 }
94
95} // namespace swift::sample
Derived sampleElements(int n) const
Copy n elements from the container, randomly selected but evenly distributed.
Definition range.h:159
Derived randomElements(int n) const
Copy n elements from the container at random.
Definition range.h:151
Generic sequential container with value semantics.
Definition sequence.h:86
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition sequence.h:308
const_iterator cbegin() const
Returns const iterator at the beginning of the sequence.
Definition sequence.h:169
void clear()
Removes all elements in the sequence.
Definition sequence.h:288
const_iterator cend() const
Returns const iterator one past the end of the sequence.
Definition sequence.h:178
static int samples()
Run the samples.
void topologicalSort(I begin, I end, F comparator)
Topological sorting algorithm.
Definition algorithm.h:123
void topologicallySortedInsert(C &container, T &&value, F comparator)
Insert an element into a sequential container while preserving the topological ordering of the contai...
Definition algorithm.h:151
QList< T >::iterator begin()
QList< T >::iterator end()