Loading...
Searching...
No Matches
commonUtils.cpp
1/*---------------------------------------------------------------------------*\
2 *
3 * bitpit
4 *
5 * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6 *
7 * -------------------------------------------------------------------------
8 * License
9 * This file is part of bitpit.
10 *
11 * bitpit is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License v3 (LGPL)
13 * as published by the Free Software Foundation.
14 *
15 * bitpit is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with bitpit. If not, see <http://www.gnu.org/licenses/>.
22 *
23\*---------------------------------------------------------------------------*/
24
25#define __BITPIT_COMMON_UTILS_SRC__
26
27#include <iostream>
28#include <cmath>
29
30#include "commonUtils.hpp"
31
32namespace bitpit {
33
34namespace utils {
35
36template bool addToOrderedVector<>(const long&, std::vector<long>&, std::less<long>);
37template bool addToOrderedVector<>(const unsigned long&, std::vector<unsigned long>&, std::less<unsigned long>);
38
39template std::vector<long>::const_iterator findInOrderedVector<>(const long&, const std::vector<long>&, std::less<long>);
40template std::vector<unsigned long>::const_iterator findInOrderedVector<>(const unsigned long&, const std::vector<unsigned long>&, std::less<unsigned long>);
41
51template<>
52void swapValue(std::vector<bool> &v, std::size_t i, std::size_t j)
53{
54 v.swap(v[i], v[j]);
55}
56
67void extractWithoutReplacement(int n, int m, std::vector<int> &list)
68{
69 // Initialize variables
70 if (n > m+1) {
71 std::cout << "error" << std::endl;
72 return;
73 }
74
75 // Resize input variables
76 list.resize(n);
77
78 // Initialize extraction set
79 int N = m;
80 std::vector<int> set(m+1, -1);
81 for (int i = 0; i < m+1; i++) {
82 set[i] = i;
83 }
84
85 // Extract integers without replacement
86 for (int i = 0; i < n; i++) {
87 int index = (int) round(((double) N) * ((double) rand())/((double) RAND_MAX));
88 list[i] = set[index];
89 set[index] = set[N];
90 N--;
91 }
92}
93
94
103std::size_t countDigits(int n)
104{
105 if (n== 0) {
106 return 1;
107 }
108
109 std::size_t nDigits = 0;
110 while (n != 0) {
111 n /= 10;
112 ++nDigits;
113 }
114
115 return nDigits;
116}
117
126unsigned long factorial(unsigned long n)
127{
128 unsigned long factorial = 1;
129 for (unsigned long i = 1; i <= n; ++i) {
130 factorial *= i;
131 }
132
133 return factorial;
134}
135
136}
137
138}
void swapValue(std::vector< bool > &v, std::size_t i, std::size_t j)
unsigned long factorial(unsigned long n)
void extractWithoutReplacement(int n, int m, std::vector< int > &list)
std::size_t countDigits(int n)
--- layout: doxygen_footer ---