-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-next-permutation.cpp
More file actions
150 lines (120 loc) · 2.9 KB
/
test-next-permutation.cpp
File metadata and controls
150 lines (120 loc) · 2.9 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (c) 2025-2026 Stefan Teleman.
//
// Licensed under the MIT License.
// See https://opensource.org/license/mit
// SPDX-License-Identifier: MIT
//
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <atomic>
#include <cstdint>
namespace qap {
template<typename _Ty>
inline
void swap(_Ty* A, _Ty* B) {
_Ty T = *B;
*B = *A;
*A = T;
}
template<typename _Ty>
inline
_Ty min(_Ty A, _Ty B) {
return A < B ? A : B;
}
template<typename _Ty>
void reverse(_Ty* AX, int32_t B, int32_t E) {
while (B < E) {
swap(&AX[B], &AX[E]);
++B;
--E;
}
}
template<typename _Ty>
inline
_Ty array_index(const _Ty* AX, uint32_t IXW, uint32_t IXH, uint32_t W) {
const _Ty* AXI = static_cast<const _Ty*>(AX + IXW * W);
const _Ty* AXP = AXI + IXH;
return *AXP;
}
template<typename _Ty>
bool next_permutation(_Ty* AX, uint32_t N) {
if (N < 2U)
return false;
int64_t K = static_cast<int64_t>(N - 2U);
int64_t J = static_cast<int64_t>(N - 1U);
while (K >= 0 && AX[K] >= AX[K + 1])
--K;
if (K < 0)
return false;
while (J >= 0 && AX[J] <= AX[K])
--J;
swap(&AX[K], &AX[J]);
reverse(AX, K + 1, N - 1);
return true;
}
} // namespace qap
template<typename _Ty>
void print_vector(const std::vector<_Ty>& V, std::ostream& ofs = std::cerr) {
ofs << "{ ";
if (!V.empty()) {
typename std::vector<_Ty>::const_iterator I = V.begin();
typename std::vector<_Ty>::const_iterator E = V.end();
ofs << *I;
while (++I != E)
ofs << ", " << *I;
}
ofs << " }" << std::endl;
}
template<typename _Ty>
void print_array(const _Ty* A, size_t N, std::ostream& ofs = std::cerr) {
ofs << "{ ";
if (N > 0) {
const _Ty* I = A;
const _Ty* E = A + N;
ofs << *I;
while (++I != E)
ofs << ", " << *I;
}
ofs << " }" << std::endl;
}
int main()
{
std::ofstream VFS;
std::ofstream AFS;
const char* VO = "vector-tnp-output.txt";
const char* AO = "array-tnp-output.txt";
// The bigger the vectors the bigger the permutation set.
// You have been warned. :-)
std::vector<uint32_t> V = { 0, 1, 2, 3, 4, 5 };
uint32_t A[] = { 0, 1, 2, 3, 4, 5 };
uint32_t IX = 0U;
VFS.open(VO);
if (!VFS.good() || VFS.eof()) {
std::cerr << "Could not open vector output file " << VO
<< " for writing." << std::endl;
return 1;
}
AFS.open(AO);
if (!AFS.good() || AFS.eof()) {
std::cerr << "Could not open array output file " << AO
<< " for writing." << std::endl;
return 1;
}
do {
VFS << ++IX << ": ";
print_vector(V, VFS);
} while (std::next_permutation(V.begin(), V.end()));
IX = 0U;
uint32_t AX = sizeof(A) / sizeof(A[0]);
do {
AFS << ++IX << ": ";
print_array(A, AX, AFS);
} while (qap::next_permutation(A, AX));
AFS.close();
VFS.close();
std::cerr << "std::vector output is in " << VO << std::endl;
std::cerr << "array output is in " << AO << std::endl;
return 0;
}