Adds several convenience features
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System.Collections;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using PkmnLib.Static.Utils;
|
||||
|
||||
@@ -6,10 +7,13 @@ namespace PkmnLib.Static.Libraries;
|
||||
/// <summary>
|
||||
/// A basic library for data types. Stores data both by name and by index.
|
||||
/// </summary>
|
||||
public abstract class DataLibrary<T>
|
||||
public abstract class DataLibrary<T> : IEnumerable<T>
|
||||
where T : INamedValue
|
||||
{
|
||||
private readonly Dictionary<StringKey, T> _data = new();
|
||||
/// <summary>
|
||||
/// The underlying data storage.
|
||||
/// </summary>
|
||||
protected readonly Dictionary<StringKey, T> Data = new();
|
||||
private readonly List<T> _values = [];
|
||||
|
||||
/// <summary>
|
||||
@@ -17,7 +21,7 @@ public abstract class DataLibrary<T>
|
||||
/// </summary>
|
||||
public void Add(T value)
|
||||
{
|
||||
_data.Add(value.Name, value);
|
||||
Data.Add(value.Name, value);
|
||||
_values.Add(value);
|
||||
}
|
||||
|
||||
@@ -26,14 +30,14 @@ public abstract class DataLibrary<T>
|
||||
/// </summary>
|
||||
public void Remove(T value)
|
||||
{
|
||||
_data.Remove(value.Name);
|
||||
Data.Remove(value.Name);
|
||||
_values.Remove(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Try to get a value from the library. Returns false if the value is not found.
|
||||
/// </summary>
|
||||
public bool TryGet(StringKey key, [MaybeNullWhen(false)] out T value) => _data.TryGetValue(key, out value);
|
||||
public bool TryGet(StringKey key, [MaybeNullWhen(false)] out T value) => Data.TryGetValue(key, out value);
|
||||
|
||||
/// <summary>
|
||||
/// Get a random value from the library.
|
||||
@@ -49,4 +53,10 @@ public abstract class DataLibrary<T>
|
||||
/// Whether the library is empty.
|
||||
/// </summary>
|
||||
public bool IsEmpty => _values.Count == 0;
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerator<T> GetEnumerator() => _values.GetEnumerator();
|
||||
|
||||
/// <inheritdoc />
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
}
|
||||
Reference in New Issue
Block a user