using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace RGB.NET.Core;
///
/// Represents a mapping from to a custom identifier.
///
/// The identifier the is mapped to.
public class LedMapping : IEnumerable<(LedId ledId, T mapping)>
where T : notnull
{
#region Properties & Fields
private readonly Dictionary _mapping = new();
private readonly Dictionary _reverseMapping = new();
///
/// Gets the number of entries in this mapping.
///
public int Count => _mapping.Count;
///
/// Gets a collection of all mapped ledids.
///
public ICollection LedIds => _mapping.Keys;
///
/// Gets a collection of all mapped custom identifiers.
///
public ICollection Mappings => _reverseMapping.Keys;
#endregion
#region Indexer
///
/// Gets the custom identifier mapped to the specified .
///
/// The led id to get the mapped identifier.
/// The mapped ifentifier.
public T this[LedId ledId]
{
get => _mapping[ledId];
set
{
_mapping[ledId] = value;
_reverseMapping[value] = ledId;
}
}
///
/// Gets the mapped to the specified custom identifier.
///
/// The custom identifier to get the mapped led id.
/// The led id.
public LedId this[T mapping]
{
get => _reverseMapping[mapping];
set => this[value] = mapping;
}
#endregion
#region Methods
///
/// Adds a new entry to the mapping.
///
/// The to map.
/// The custom identifier to map.
public void Add(LedId ledId, T mapping)
{
_mapping.Add(ledId, mapping);
_reverseMapping.Add(mapping, ledId);
}
///
/// Checks if the specified is mapped.
///
/// The led id to check.
/// true if the led id is mapped; otherwise false.
public bool Contains(LedId ledId) => _mapping.ContainsKey(ledId);
///
/// Checks if the specified custom identifier is mapped.
///
/// The custom identifier to check.
/// true if the led id is mapped; otherwise false.
public bool Contains(T mapping) => _reverseMapping.ContainsKey(mapping);
///
/// Gets the custom identifier mapped to the specified led id.
///
/// The led id to get the custom identifier for.
/// Contains the mapped custom identifier or null if there is no mapping for the specified led id.
/// true if there was a custom identifier for the specified led id; otherwise false.
public bool TryGetValue(LedId ledId, out T? mapping) => _mapping.TryGetValue(ledId, out mapping);
///
/// Gets the led id mapped to the specified custom identifier.
///
/// The custom identifier to get the led id for.
/// Contains the mapped led id or null if there is no mapping for the specified led id.
/// true if there was a led id for the specified custom identifier; otherwise false.
public bool TryGetValue(T mapping, out LedId ledId) => _reverseMapping.TryGetValue(mapping, out ledId);
///
/// Removes the specified led id and the mapped custom identifier.
///
/// The led id to remove.
/// true if there was a mapping for the led id to remove; otherwise false.
public bool Remove(LedId ledId)
{
if (_mapping.TryGetValue(ledId, out T? mapping))
_reverseMapping.Remove(mapping);
return _mapping.Remove(ledId);
}
///
/// Removes the specified custom identifier and the mapped led id.
///
/// The custom identifier to remove.
/// true if there was a mapping for the custom identifier to remove; otherwise false.
public bool Remove(T mapping)
{
if (_reverseMapping.TryGetValue(mapping, out LedId ledId))
_mapping.Remove(ledId);
return _reverseMapping.Remove(mapping);
}
///
/// Removes all registered mappings.
///
public void Clear()
{
_mapping.Clear();
_reverseMapping.Clear();
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
///
public IEnumerator<(LedId ledId, T mapping)> GetEnumerator() => _mapping.Select(x => (x.Key, x.Value)).GetEnumerator();
#endregion
}