EnigmaMachineCore 0.1.0
A modular Enigma Machine simulation in C++20
Loading...
Searching...
No Matches
PlugBoard.cpp
Go to the documentation of this file.
1
6#include "PlugBoard.hpp"
7#include <iostream>
8#include <numeric>
9#include <stdexcept>
10#include <string>
11#include "config.hpp"
12
13PlugBoard::PlugBoard() { std::iota(mapping.begin(), mapping.end(), 0); }
14
23PlugBoard::PlugBoard(const std::array<PlugBoardPair, PLUGBOARD_MAX_PAIRS>& pairs) : PlugBoard() {
24 for (const auto& [sourcePortIndex, destinationPortIndex] : pairs) {
25 // Skip uninitialized/empty pairs
26 if (sourcePortIndex == -1 || destinationPortIndex == -1) {
27 continue;
28 }
29
30 if (sourcePortIndex < 0 || sourcePortIndex >= TRANSFORMER_SIZE || destinationPortIndex < 0 ||
31 destinationPortIndex >= TRANSFORMER_SIZE) {
32 throw std::invalid_argument("PlugBoard error: Port index out of range (" + std::to_string(sourcePortIndex) +
33 ", " + std::to_string(destinationPortIndex) + ").");
34 }
35
36 if (sourcePortIndex == destinationPortIndex) {
37 continue;
38 }
39
40 if (mapping.at(sourcePortIndex) != sourcePortIndex ||
41 mapping.at(destinationPortIndex) != destinationPortIndex) {
42 throw std::invalid_argument("PlugBoard error: Conflict for pair (" + std::to_string(sourcePortIndex) +
43 ", " + std::to_string(destinationPortIndex) + "). Port already in use.");
44 }
45
46 mapping.at(sourcePortIndex) = destinationPortIndex;
47 mapping.at(destinationPortIndex) = sourcePortIndex;
48 }
49}
50
55AlphabetIndex PlugBoard::swap(AlphabetIndex key) const {
56 if (key < 0 || key >= TRANSFORMER_SIZE) {
57 return key;
58 }
59 return mapping.at(key);
60}
Header file for the PlugBoard class.
Class representing the PlugBoard (Steckerbrett) of the Enigma machine.
Definition PlugBoard.hpp:22
AlphabetIndex swap(AlphabetIndex key) const
Swaps the input key based on the plugboard pairs.
Definition PlugBoard.cpp:55
PlugBoard()
Constructor for the PlugBoard class. Initializes an empty plugboard with no pairs (identity mapping).
Definition PlugBoard.cpp:13
std::array< AlphabetIndex, TRANSFORMER_SIZE > mapping
Definition PlugBoard.hpp:24
Global configuration constants for the Enigma machine.
constexpr int TRANSFORMER_SIZE
Size of the alphabet/transformer (Standard Enigma is 26).
Definition config.hpp:10