Connect++ 0.4.0
A fast, readable connection prover for first-order logic.
Loading...
Searching...
No Matches
Reorder.hpp
1/*
2
3Copyright © 2023-24 Sean Holden. All rights reserved.
4
5*/
6/*
7
8This file is part of Connect++.
9
10Connect++ is free software: you can redistribute it and/or modify it
11under the terms of the GNU General Public License as published by the
12Free Software Foundation, either version 3 of the License, or (at your
13option) any later version.
14
15Connect++ is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18more details.
19
20You should have received a copy of the GNU General Public License along
21with Connect++. If not, see <https://www.gnu.org/licenses/>.
22
23*/
24
25#ifndef REORDER_HPP
26#define REORDER_HPP
27
28#include<iostream>
29
30using std::vector;
31using std::cout;
32using std::endl;
33
45template<class T>
46void show_vector(vector<T> v) {
47 cout << "( ";
48 for (T e : v)
49 cout << e << " ";
50 cout << ")" << endl;
51}
56template<class T>
57vector<T> three_way_merge(vector<T> v1,
58 vector<T> v2,
59 vector<T> v3) {
60 if (v1.size() == 0) {
61 return v3;
62 }
63 else {
64 T v1_head = v1.front();
65 v1.erase(v1.begin());
66 T v2_head = v2.front();
67 v2.erase(v2.begin());
68 T v3_head = v3.front();
69 v3.erase(v3.begin());
70 vector<T> prefix {v1_head, v2_head, v3_head};
71 vector<T> tail = three_way_merge(v1, v2, v3);
72 prefix.insert(prefix.end(), tail.begin(), tail.end());
73 return prefix;
74 }
75}
82template<class T>
83vector<T> deterministic_reorder_once(vector<T> v) {
84 size_t s = v.size();
85 size_t s2 = s / 3;
86 vector<T> v1 = v;
87 v1.erase(v1.begin() + s2, v1.end());
88 vector<T> v2 = v;
89 v2.erase(v2.begin(), v2.begin() + s2);
90 v2.erase(v2.end() - s2, v2.end());
91 vector<T> v3 = v;
92 v3.erase(v3.begin(), v3.end() - s2);
93 return three_way_merge<T>(v3, v1, v2);
94}
98template<class T>
99vector<T> deterministic_reorder_n_times(vector<T> v, size_t n) {
100 for (size_t i = 0; i < n; i++)
101 v = deterministic_reorder_once(v);
102 return v;
103}
104
105#endif