using System.Collections.Generic; namespace Cryville.Common { /// /// A manager that assigns each given identifiers a unique integer ID. /// public class IdentifierManager { /// /// A shared instance of the class. /// public static IdentifierManager SharedInstance = new IdentifierManager(); Dictionary _idents = new Dictionary(); List _ids = new List(); object _syncRoot = new object(); /// /// Creates an instance of the class. /// public IdentifierManager() { Request(this); } /// /// Requests an integer ID for an identifier. /// /// The identifier. /// The integer ID. public int Request(object ident) { lock (_syncRoot) { int id; if (!_idents.TryGetValue(ident, out id)) { _idents.Add(ident, id = _idents.Count); _ids.Add(ident); } return id; } } /// /// Retrieves the identifier assigned with an integer ID. /// /// The integer ID. /// The identifier. public object Retrieve(int id) { lock (_syncRoot) { return _ids[id]; } } } }