namespace FoodFactory.Core; using System.Collections.Generic; public class Registry where TEntry : notnull { private readonly Dictionary _data = []; private readonly List _list = []; private readonly object _lock = new(); public IReadOnlyList Entries => _list; public void Clear() { lock (_lock) { _data.Clear(); _list.Clear(); } } public int GetOrCreate(TEntry name) { lock (_lock) { if (_data.TryGetValue(name, out var tag)) { return tag; } return CreateNewKey(name); } } public TEntry GetEntry(int index) { lock (_lock) { if ((uint)index >= (uint)_list.Count) { throw new KeyNotFoundException($"The index {index} you are trying to access does not exist in the registry. The registry only has {_list.Count} of type {typeof(TEntry).Name}s"); } return _list[index]; } } private int CreateNewKey(TEntry name) { int index = _list.Count; _list.Add(name); _data[name] = index; return index; } }