File: RUtils.cpp

package info (click to toggle)
abyss 2.3.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,284 kB
  • sloc: cpp: 78,182; ansic: 6,512; makefile: 2,252; perl: 672; sh: 509; haskell: 412; python: 4
file content (104 lines) | stat: -rw-r--r-- 2,471 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "RUtils.h"

#include "Common/Options.h"

#include <cassert>
#include <iomanip>
#include <iostream>

static std::string progressName;
static unsigned progressNumber = 0;
static unsigned progressTotal;
static unsigned progressUpdates;
static unsigned progressLastPrinted;
static bool progressRunning = false;

void
progressStart(const std::string& name, unsigned total)
{
	if (opt::verbose) {
		progressName = name;
		std::string tempName = name;
		tempName[0] = std::tolower(tempName[0]);
		std::cerr << '\n' << ++progressNumber << ". Starting " + tempName << "...\n";
		std::cerr << "Progress: 0%" << std::flush;
		progressTotal = total;
		progressUpdates = 0;
		progressLastPrinted = 0;
		assert(!progressRunning);
		progressRunning = true;
	}
}

void
progressUpdate()
{
	if (opt::verbose) {
		assert(progressRunning);
		progressUpdates++;
		assert(progressUpdates <= progressTotal);
		double fraction = double(progressUpdates) / progressTotal;

		if (progressUpdates == progressTotal) {
			std::cerr << "\rProgress: 100%\n" << progressName << " done." << std::endl;
			progressRunning = false;
		} else if (
		    double(progressUpdates - progressLastPrinted) / progressTotal >=
		        PROGRESS_PRINT_FRACTION &&
		    progressUpdates < progressTotal) {
			std::cerr << "\rProgress: " << int(fraction * 100.0) << "%" << std::flush;
			progressLastPrinted = progressUpdates;
		}
	}
}

unsigned
nChoosek(unsigned n, unsigned k)
{
	if (k > n)
		return 0;
	if (k * 2 > n)
		k = n - k;
	if (k == 0)
		return 1;

	long result = n;
	for (unsigned i = 2; i <= k; ++i) {
		result *= (n - i + 1);
		result /= i;
	}
	return result;
}

std::vector<std::vector<unsigned>>
genPermutations(unsigned n)
{
	std::vector<std::vector<unsigned>> permutations;
	std::vector<unsigned> permutation;
	for (unsigned i = 0; i < n; i++) {
		permutation.push_back(i);
	}
	for (unsigned i = 0; i < n; i++) {
		permutations.push_back(permutation);
		std::next_permutation(permutation.begin(), permutation.end());
	}
	return permutations;
}

std::vector<std::vector<unsigned>>
genCombinations(unsigned n, unsigned k)
{
	std::vector<bool> v(n);
	std::fill(v.begin(), v.begin() + k, true);
	std::vector<std::vector<unsigned>> combinations;
	do {
		std::vector<unsigned> combination;
		for (unsigned i = 0; i < n; ++i) {
			if (v[i]) {
				combination.push_back(i);
			}
		}
		combinations.push_back(combination);
	} while (std::prev_permutation(v.begin(), v.end()));
	return combinations;
}