EnigmaMachineCore 0.1.0
A modular Enigma Machine simulation in C++20
Loading...
Searching...
No Matches
Rotor.cpp
Go to the documentation of this file.
1
6#include "Rotor.hpp"
7
8#include <algorithm>
9#include <iostream>
10#include <stdexcept>
11#include <vector>
12
32
34 notchPosition = config.notchPosition;
35 rotationCount = 0;
37
38 copyTransformRow(0, config.wiring);
39
42}
43
51 const auto& forwardRow = getTransformRow(0);
52 std::vector<bool> seen(TRANSFORMER_SIZE, false);
53
54 int newVal{0};
55 std::for_each(forwardRow.begin(), forwardRow.end(), [&](int value) {
56 if (value < 0 || value >= TRANSFORMER_SIZE) {
57 throw std::runtime_error("Invalid Rotor mapping: Value out of range: " + std::to_string(value));
58 }
59 if (seen[value]) {
60 throw std::runtime_error("Invalid Rotor mapping: Duplicate output value: " + std::to_string(value));
61 }
62 seen[value] = true;
63 auto newCol = value;
64 setTransformValue(1, /* col = */ newCol, /* value = */ newVal++); // Initialize reverse mapping
65 });
66
67 auto missing_it = std::find(seen.begin(), seen.end(), false);
68 if (missing_it != seen.end()) {
69 throw std::runtime_error("Invalid Rotor mapping: Not a bijection. Missing output: " +
70 std::to_string(std::distance(seen.begin(), missing_it)));
71 }
72}
73
77int Rotor::initRotorPosition(int offset) {
78 rotationCount = offset;
79 return 0;
80}
81
82bool Rotor::isNotchPosition(int position) const { return (position == notchPosition); }
83
93AlphabetIndex Rotor::transform(AlphabetIndex position, bool reverse) const {
94 return reverse ? transformReverse(position) : transformForward(position);
95}
96
103AlphabetIndex Rotor::transformForward(AlphabetIndex position) const {
104 position = (position + rotationCount);
105 if (position >= TRANSFORMER_SIZE) {
106 position -= TRANSFORMER_SIZE;
107 }
108 position = getTransformValue(0, position);
109 position = (position - rotationCount + TRANSFORMER_SIZE);
110 if (position >= TRANSFORMER_SIZE) {
111 position -= TRANSFORMER_SIZE;
112 }
113
114 return position;
115}
116
123AlphabetIndex Rotor::transformReverse(AlphabetIndex position) const {
124 position = (rotationCount + position);
125 if (position >= TRANSFORMER_SIZE) {
126 position -= TRANSFORMER_SIZE;
127 }
128 position = getTransformValue(1, position);
129 position = (position - rotationCount + TRANSFORMER_SIZE);
130 if (position >= TRANSFORMER_SIZE) {
131 position -= TRANSFORMER_SIZE;
132 }
133
134 return position;
135}
136
150
155void Rotor::setPosition(int position) { rotationCount = position; }
156
157int Rotor::getPosition() const { return rotationCount; }
Defines the Rotor class, a concrete implementation of the Transformer interface.
Rotor(const RotorConfig &config)
Constructor for the Rotor class. Initializes the rotor with a configuration.
Definition Rotor.cpp:22
void initReverseLookupTable()
Generates the reverse transformation lookup table.
Definition Rotor.cpp:50
AlphabetIndex transformReverse(AlphabetIndex position) const override
Transforms the given position in reverse using the reverse LUT.
Definition Rotor.cpp:123
void setPosition(int position) override
Sets the rotor to a specific position.
Definition Rotor.cpp:155
AlphabetIndex transform(AlphabetIndex position, bool reverse=false) const override
Transforms the given position based on the transformation lookup table (LUT).
Definition Rotor.cpp:93
int getPosition() const override
Gets the current position of the rotor.
Definition Rotor.cpp:157
int rotate() override
Rotates the rotor by one position. If the rotor reaches the notch position, it returns 1,...
Definition Rotor.cpp:143
bool isNotchPosition(int position) const
Checks if the given position is the notch position.
Definition Rotor.cpp:82
AlphabetIndex transformForward(AlphabetIndex position) const override
Transforms the given position forward using the forward LUT.
Definition Rotor.cpp:103
int initRotorPosition(int offset=0)
Initializes the rotor position. This function sets the initial position of the rotor based on the pro...
Definition Rotor.cpp:77
int rotationCount
Definition Rotor.hpp:20
int notchPosition
Definition Rotor.hpp:19
void setTransformValue(int row, int col, AlphabetIndex value)
Sets a value in the transformation lookup table.
TransformerType type
const std::array< AlphabetIndex, TRANSFORMER_SIZE > & getTransformRow(int row) const
Gets a read-only reference to a row in the transformation lookup table. Useful for using standard alg...
AlphabetIndex getTransformValue(int row, int col) const
Gets a value from the transformation lookup table.
void copyTransformRow(int row, const std::array< AlphabetIndex, TRANSFORMER_SIZE > &values)
Copies a whole array into a row of the transformation lookup table.
constexpr int TRANSFORMER_SIZE
Size of the alphabet/transformer (Standard Enigma is 26).
Definition config.hpp:10
Configuration structure for an individual Rotor.
AlphabetIndex notchPosition
The position (0 - (TRANSFORMER_SIZE - 1)) at which the rotor triggers the turnover of the next rotor.
std::array< AlphabetIndex, TRANSFORMER_SIZE > wiring
The internal wiring permutation array (forward direction). Must be of size TRANSFORMER_SIZE.