From 6840c2a12242368150159020df1032aea7dab664 Mon Sep 17 00:00:00 2001 From: adelikat Date: Sat, 14 Dec 2013 05:55:25 +0000 Subject: [PATCH] Make a NamedDictionary object in BizHawk.Common, and move some collection objects to their own file. new Mnemonic system - use NamedDictionary --- .../movie/MnemonicsLookupTable.cs | 11 +- BizHawk.Common/BizHawk.Common.csproj | 1 + BizHawk.Common/CustomCollections.cs | 118 ++++++++++++++++++ BizHawk.Common/Util.cs | 101 --------------- 4 files changed, 124 insertions(+), 107 deletions(-) create mode 100644 BizHawk.Common/CustomCollections.cs diff --git a/BizHawk.Client.Common/movie/MnemonicsLookupTable.cs b/BizHawk.Client.Common/movie/MnemonicsLookupTable.cs index dab363908f..17ce789285 100644 --- a/BizHawk.Client.Common/movie/MnemonicsLookupTable.cs +++ b/BizHawk.Client.Common/movie/MnemonicsLookupTable.cs @@ -3,18 +3,17 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using BizHawk.Common; + namespace BizHawk.Client.Common { - public class MnemonicCollection : Dictionary + public class MnemonicCollection : NamedDictionary { - private readonly string _name = String.Empty; - public MnemonicCollection(string name) + : base(name) { - _name = name; + } - - public string Name { get { return _name; } } } public class CoreMnemonicCollection : List diff --git a/BizHawk.Common/BizHawk.Common.csproj b/BizHawk.Common/BizHawk.Common.csproj index 99045e8e65..7d5bcf50a0 100644 --- a/BizHawk.Common/BizHawk.Common.csproj +++ b/BizHawk.Common/BizHawk.Common.csproj @@ -50,6 +50,7 @@ + diff --git a/BizHawk.Common/CustomCollections.cs b/BizHawk.Common/CustomCollections.cs new file mode 100644 index 0000000000..8af10dc2d4 --- /dev/null +++ b/BizHawk.Common/CustomCollections.cs @@ -0,0 +1,118 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; + +namespace BizHawk.Common +{ + /// + /// A dictionary that creates new values on the fly as necessary so that any key you need will be defined. + /// + /// dictionary keys + /// dictionary values + [Serializable] + public class WorkingDictionary : Dictionary where V : new() + { + public new V this[K key] + { + get + { + V temp; + if (!TryGetValue(key, out temp)) + { + temp = this[key] = new V(); + } + + return temp; + } + + set + { + base[key] = value; + } + } + + public WorkingDictionary() { } + + protected WorkingDictionary(SerializationInfo info, StreamingContext context) : base(info, context) { } + } + + /// + /// A dictionary that contains a name parameter, which is required + /// + public class NamedDictionary : Dictionary + { + public NamedDictionary(string name) + { + Name = name; + } + + public string Name { get; private set; } + } + + /// + /// a Dictionary-of-lists with key K and values List<V> + /// + [Serializable] + public class Bag : BagBase>, List> { } + + /// + /// a Dictionary-of-lists with key K and values List<V> + /// + [Serializable] + public class SortedBag : BagBase>, List> { } + + /// + /// base class for Bag and SortedBag + /// + /// dictionary keys + /// list values + /// dictionary type + /// list type + [Serializable] + public class BagBase : IEnumerable + where D : IDictionary, new() + where L : IList, IEnumerable, new() + { + readonly D dictionary = new D(); + public void Add(K key, V val) + { + this[key].Add(val); + } + + public bool ContainsKey(K key) { return dictionary.ContainsKey(key); } + + IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } + public IEnumerator GetEnumerator() + { + return dictionary.Values.SelectMany(lv => lv).GetEnumerator(); + } + + public IEnumerable KeyValuePairEnumerator { get { return dictionary; } } + + /// + /// Gets the list of keys contained herein + /// + public IList Keys { get { return new List(dictionary.Keys); } } + + public L this[K key] + { + get + { + L slot; + if (!dictionary.TryGetValue(key, out slot)) + { + dictionary[key] = slot = new L(); + } + + return slot; + } + + set + { + dictionary[key] = value; + } + } + } +} diff --git a/BizHawk.Common/Util.cs b/BizHawk.Common/Util.cs index d570c23fbd..19b2f567de 100644 --- a/BizHawk.Common/Util.cs +++ b/BizHawk.Common/Util.cs @@ -1,9 +1,5 @@ using System; -using System.Collections; -using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Runtime.Serialization; using System.Text; namespace BizHawk.Common @@ -380,103 +376,6 @@ namespace BizHawk.Common } } - /// - /// a Dictionary-of-lists with key K and values List<V> - /// - [Serializable] - public class Bag : BagBase>, List> { } - - /// - /// a Dictionary-of-lists with key K and values List<V> - /// - [Serializable] - public class SortedBag : BagBase>, List> { } - - /// - /// A dictionary that creates new values on the fly as necessary so that any key you need will be defined. - /// - /// dictionary keys - /// dictionary values - [Serializable] - public class WorkingDictionary : Dictionary where V : new() - { - public new V this[K key] - { - get - { - V temp; - if (!TryGetValue(key, out temp)) - { - temp = this[key] = new V(); - } - - return temp; - } - - set - { - base[key] = value; - } - } - - public WorkingDictionary() { } - - protected WorkingDictionary(SerializationInfo info, StreamingContext context) : base(info, context) { } - } - - /// - /// base class for Bag and SortedBag - /// - /// dictionary keys - /// list values - /// dictionary type - /// list type - [Serializable] - public class BagBase : IEnumerable - where D : IDictionary, new() - where L : IList, IEnumerable, new() - { - readonly D dictionary = new D(); - public void Add(K key, V val) - { - this[key].Add(val); - } - - public bool ContainsKey(K key) { return dictionary.ContainsKey(key); } - - IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } - public IEnumerator GetEnumerator() - { - return dictionary.Values.SelectMany(lv => lv).GetEnumerator(); - } - - public IEnumerable KeyValuePairEnumerator { get { return dictionary; } } - - /// - /// Gets the list of keys contained herein - /// - public IList Keys { get { return new List(dictionary.Keys); } } - - public L this[K key] - { - get - { - L slot; - if (!dictionary.TryGetValue(key, out slot)) - { - dictionary[key] = slot = new L(); - } - - return slot; - } - - set - { - dictionary[key] = value; - } - } - } - [Serializable] public class NotTestedException : Exception {