In search of the most zigified way to implement the rotor settings of an Enigma machine...?

As an exercise for me I want to write an Enigma machine which can be used as an emulator for the many Enigma devices known.
The Enigma machine used to have “Rotors”, which normally are represented in code as a list
of 26 uppercase letters in an array/slice. There are a lot of different rotor setting with different sequences of characters.
I want to implement a function, which accepts the name of a certain rotor and returns its configuration as a pointer to a slice.
My goals is:

  • not to use global variables
  • not to initialize all rotors each time the above mentioned function get called
  • to minimize re-initialize of data
  • make it easy to modify

My idea was to use static const []u8 slices - one for each rotor and then initialize a hashmap with
the names of the rotors as keys and slice.ptr as its corresponding values.
But how can I initialize a static hashmap with all those key/value-pairs?
And all these static stuff looks not very “beautiful” to me.

As an example:
This is how such an rotor setting looks like (data source: Wikipedia):
static const rotor_ic :[]u8 = “DMTWSILRUYQNKFEJCAZBPGXOHV”;

…but I am sure, there are more elegant ways to implement such a functionality in a far more ziglike fashion…
Any ideas for improvements?


Do you normally initialize a single rotor, or an entire machine? It seems to me that the way to do this is the plain old boring “constructor” approach:

var r1 = Rotor.init("K N Q T W Z C F I L O R U X A D G J M P S V Y B E H");
var r2 = Rotor.init("L O R U X A D G J M P S V Y B E H K N Q T W Z C F I");
var r3 = Rotor.init("A D G J M P S V Y B E H K N Q T W Z C F I L O R U X");
var r4 = Rotor.init("U X A D G J M P S V Y B E H K N Q T W Z C F I L O R");
var r5 = Rotor.init("S V Y B E H K N Q T W Z C F I L O R U X A D G J M P");

This approach encapsulates the implementation nicely, allowing you to add other member fields if needed, and also allows you to change the format to something a little more readable, if you like. :wink:

Hi aghast,

I want to initialize all rotors of one machine independently. The type of machine can be chosen by the user.

…in what type of variable (r) this would result? Slices?
And: Each time the function containing these init-calls get called all rotors will be initialized while
only three or four will be used.
To prevent that, I first thought of static variables…which then looks quite weird to me.