2020-01-23 23:37:15 +00:00
using System ;
2013-12-14 05:55:25 +00:00
using System.Collections ;
using System.Collections.Generic ;
2020-01-23 23:37:15 +00:00
using System.Diagnostics.CodeAnalysis ;
2013-12-14 05:55:25 +00:00
using System.Linq ;
using System.Runtime.Serialization ;
namespace BizHawk.Common
{
2020-01-23 23:37:15 +00:00
/// <summary>Wrapper over <see cref="WorkingDictionary{TKey, TValue}">WorkingDictionary</see><<paramref name="TKey"/>, <see cref="List{T}">List</see><<paramref name="TValue"/>>>.</summary>
2013-12-14 05:55:25 +00:00
[Serializable]
2020-01-23 23:37:15 +00:00
public class Bag < TKey , TValue > : IEnumerable < TValue > where TKey : notnull
2013-12-14 05:55:25 +00:00
{
2020-01-23 23:37:15 +00:00
private readonly WorkingDictionary < TKey , List < TValue > > dictionary = new WorkingDictionary < TKey , List < TValue > > ( ) ;
2013-12-14 05:55:25 +00:00
2020-01-23 23:37:15 +00:00
public IList < TKey > Keys = > dictionary . Keys . ToList ( ) ;
2013-12-14 05:55:25 +00:00
2020-01-23 23:37:15 +00:00
public List < TValue > this [ TKey key ]
{
#pragma warning disable CS8603 // the only call to the index setter of `dictionary` is this index setter, which only takes non-null `List<TValue>`s
get = > dictionary [ key ] ;
#pragma warning restore CS8603
set = > dictionary [ key ] = value ;
2013-12-14 05:55:25 +00:00
}
2020-01-23 23:37:15 +00:00
public void Add ( TKey key , IEnumerable < TValue > val ) = > this [ key ] . AddRange ( val ) ;
2013-12-14 05:55:25 +00:00
2020-01-23 23:37:15 +00:00
public void Add ( TKey key , TValue val ) = > this [ key ] . Add ( val ) ;
2013-12-14 05:55:25 +00:00
2020-01-23 23:37:15 +00:00
public bool ContainsKey ( TKey key ) = > dictionary . ContainsKey ( key ) ;
2013-12-14 05:55:25 +00:00
2020-01-23 23:37:15 +00:00
IEnumerator IEnumerable . GetEnumerator ( ) = > GetEnumerator ( ) ;
2013-12-14 05:55:25 +00:00
2020-01-23 23:37:15 +00:00
public IEnumerator < TValue > GetEnumerator ( ) = > dictionary . Values . SelectMany ( lv = > lv ) . GetEnumerator ( ) ;
2013-12-14 05:55:25 +00:00
2020-01-23 23:37:15 +00:00
public IEnumerator < KeyValuePair < TKey , List < TValue > > > GetKVPEnumerator ( ) = > dictionary . GetEnumerator ( ) ;
}
2013-12-14 05:55:25 +00:00
2020-01-23 23:37:15 +00:00
/// <summary>A dictionary whose index getter creates an entry if the requested key isn't part of the collection, making it always safe to use the returned value. The new entry's value will be the result of the default constructor of <typeparamref name="TValue"/>.</summary>
[Serializable]
public class WorkingDictionary < TKey , TValue > : Dictionary < TKey , TValue >
where TKey : notnull
where TValue : new ( )
{
public WorkingDictionary ( ) { }
2013-12-14 05:55:25 +00:00
2020-01-23 23:37:15 +00:00
protected WorkingDictionary ( SerializationInfo info , StreamingContext context ) : base ( info , context ) { }
2013-12-14 05:55:25 +00:00
2020-01-23 23:37:15 +00:00
[property: MaybeNull]
public new TValue this [ TKey key ]
2013-12-14 05:55:25 +00:00
{
2020-01-23 23:37:15 +00:00
get = > TryGetValue ( key , out var temp )
? temp
: ( base [ key ] = new TValue ( ) ) ;
set = > base [ key ] = value ;
2013-12-14 05:55:25 +00:00
}
}
}