-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpermutation.cpp
69 lines (53 loc) · 1.76 KB
/
permutation.cpp
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
#include "permutation.h"
#include "io_helper.h"
#include "tiny_id_func.h"
#include <stdexcept>
#include <vector>
static
ArrayIDIDFunc load_permutation_impl(std::istream&in){
std::vector<int>buffer;
int x;
while(in >> x)
buffer.push_back(x);
const int n = buffer.size();
BitIDFunc seen(n);
seen.fill(false);
for(auto x:buffer){
if(x < 0)
throw std::runtime_error("an order position can not be negative");
else if(x > n)
throw std::runtime_error("an order position can not be larger than the number of elements");
else if(seen(x))
throw std::runtime_error("an order position can only appear once");
seen.set(x, true);
}
ArrayIDIDFunc order(n, n);
for(int i=0; i<n; ++i){
order[buffer[i]] = i;
}
return order;
}
ArrayIDIDFunc load_binary_permutation_impl(std::istream&in, long long size){
if(size % 4 != 0)
throw std::runtime_error("file size of binary cache does not match for order file");
ArrayIDIDFunc order((int)size/4, (int)size/4);
in.read((char*)order.begin(), size);
return order;
}
void save_binary_permutation_impl(std::ostream&out, const ArrayIDIDFunc&order){
out.write((const char*)order.begin(), order.preimage_count()*4);
}
ArrayIDIDFunc load_permutation(const std::string&file_name){
return load_cached_text_file(file_name, "dimacs", load_permutation_impl, load_binary_permutation_impl, save_binary_permutation_impl);
}
ArrayIDIDFunc uncached_load_permutation(const std::string&file_name){
return load_uncached_text_file(file_name, load_permutation_impl);
}
static
void save_permutation_impl(std::ostream&out, const ArrayIDIDFunc&perm){
for(auto x:inverse_permutation(perm))
out << x << '\n';
}
void save_permutation(const std::string&file_name, const ArrayIDIDFunc&perm){
save_text_file(file_name, save_permutation_impl, perm);
}