-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSym_input_processing.cpp
More file actions
149 lines (124 loc) · 3.19 KB
/
Copy pathSym_input_processing.cpp
File metadata and controls
149 lines (124 loc) · 3.19 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
/*
Author: Ryan Patrick
Timestamp: Sep-29-2024
Purpose: Implementations of routines for processing user input
*/
// Normal includes
#include "Sym_cycle_notation_parser.hpp"
#include "Sym_data_types.hpp"
#include "Sym_input_processing.hpp"
// Generated by Bison
#include "build/cycle_notation_parser.hpp"
// Generated by Flex
#include "build/cycle_notation_scanner.hpp"
// C++ Standard Library includes
#include <cassert>
#include <iostream>
#include <vector>
#include <stdexcept>
using namespace Sym;
void Sym::ProcessPermutationInput(int i, Permutation& inputBuffer, Permutation& permutation)
{
if (inputBuffer.size() != permutation.size())
{
throw std::invalid_argument("Expected inputBuffer and permutation to have the same size.");
}
// Only allow values between 1-n inclusive
if (inputBuffer[i] > 0 && inputBuffer[i] <= inputBuffer.size())
{
// A permutation is a shuffling of symbols, so repetitions can't be allowed.
// So, swap the existing occurrence of this symbol with this one. Example:
/*
**********Stage 1**********
inputBuffer
1 2 3 4
1 3 2 3
permutation
1 2 3 4
1 4 2 3
**********Stage 2**********
inputBuffer
1 2 3 4
1 3 2 *4*
permutation
1 2 3 4
1 4 2 *4*
**********Stage 3**********
inputBuffer
1 2 3 4
1 3 2 4
permutation
1 2 3 4
1 *3* 2 4
*/
bool foundValue = false;
for (int j = 0; j < inputBuffer.size(); j++)
{
if (inputBuffer[j] == inputBuffer[i] && i != j)
{
inputBuffer[j] = permutation[i];
permutation[j] = permutation[i];
foundValue = true;
break;
}
}
assert(foundValue);
permutation[i] = inputBuffer[i];
}
else
{
inputBuffer[i] = permutation[i];
}
}
void Sym::ShrinkPermutationByOne(Permutation& permutation)
{
int n = static_cast<int>(permutation.size());
for (int i = 0; i < n; i++)
{
if (permutation[i] == n)
{
permutation[i] = permutation[n - 1];
}
}
permutation.resize(n - 1);
}
void Sym::CopyPermutation(Permutation& destination, Permutation& source)
{
for (int i = 0; i < destination.size(); i++)
{
destination[i] = source[i];
}
}
Permutation Sym::InitializePermutation(int size)
{
Permutation permutation;
for (int i = 0; i < size; i++)
{
permutation.push_back(i + 1);
}
return permutation;
}
Permutation Sym::ProcessCycleNotationInput(const char* cycleInput)
{
Permutation result;
YY_BUFFER_STATE buf = yy_scan_string(cycleInput);
yyparse(result);
yy_delete_buffer(buf);
return result;
}
void Sym::ResizeAllToMax(PermutationVector& permVector)
{
size_t maxSize = 1;
for (Permutation* perm : permVector)
{
if (perm->size() > maxSize)
maxSize = perm->size();
}
for (Permutation* perm : permVector)
{
for (int i = perm->size(); i < maxSize; i++)
{
perm->push_back(i + 1);
}
}
}