swift
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 
27 namespace swift::sample
28 {
30  {
32  for (int i = 1; i <= 100; ++i) { seq.push_back(i); }
33  const int samples = 200;
35  {
36  for (int i = 0; i < samples; ++i)
37  {
38  auto randoms = seq.randomElements(10);
39  means.push_back(std::accumulate(randoms.cbegin(), randoms.cend(), 0) / 10);
40  }
41  int mean = std::accumulate(means.cbegin(), means.cend(), 0) / samples;
42  int stdDev = std::sqrt(std::accumulate(means.cbegin(), means.cend(), 0,
43  [&](int a, int n) { return a + (n - mean) * (n - mean); }) /
44  samples);
45  qDebug() << "randomElements";
46  qDebug() << "means:" << means;
47  qDebug() << "mean of the means:" << mean;
48  qDebug() << "std deviation of the means:" << stdDev;
49  }
50  means.clear();
51  {
52  for (int i = 0; i < samples; ++i)
53  {
54  auto randoms = seq.sampleElements(10);
55  means.push_back(std::accumulate(randoms.cbegin(), randoms.cend(), 0) / 10);
56  }
57  int mean = std::accumulate(means.cbegin(), means.cend(), 0) / samples;
58  int stdDev = std::sqrt(std::accumulate(means.cbegin(), means.cend(), 0,
59  [&](int a, int n) { return a + (n - mean) * (n - mean); }) /
60  samples);
61  qDebug() << "sampleElements";
62  qDebug() << "means:" << means;
63  qDebug() << "mean of the means:" << mean;
64  qDebug() << "std deviation of the means:" << stdDev;
65  }
66 
67  QStringList src { "a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3" };
68  std::shuffle(src.begin(), src.end(), std::mt19937(static_cast<unsigned>(std::time(nullptr))));
69  qDebug() << src;
70  qDebug() << "topologicallySortedInsert";
71  QStringList dst;
72  int count = 0;
73  auto cmp = [&](const QString &a, const QString &b) {
74  count++;
75  return a[0] == b[0] && a[1] < b[1];
76  };
77  for (const auto &s : src) { swift::misc::topologicallySortedInsert(dst, s, cmp); }
78  qDebug() << count << "comparisons";
79  qDebug() << dst;
80  qDebug() << "topologicalSort";
81  count = 0;
82  swift::misc::topologicalSort(dst.begin(), dst.end(), cmp);
83  qDebug() << count << "comparisons";
84  qDebug() << dst;
85 
86  return 0;
87  }
88 
89 } // namespace swift::sample
Derived sampleElements(int n) const
Copy n elements from the container, randomly selected but evenly distributed.
Definition: range.h:156
Derived randomElements(int n) const
Copy n elements from the container at random.
Definition: range.h:148
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:305
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:114
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:142