From d8efd70a841acc4adb9391b7b022de1181f4e9f6 Mon Sep 17 00:00:00 2001 From: Anairkoen Schno Date: Mon, 9 Dec 2019 01:43:49 -0600 Subject: [PATCH] Made Map keep order --- IPA.Loader/Config/Data/Map.cs | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/IPA.Loader/Config/Data/Map.cs b/IPA.Loader/Config/Data/Map.cs index 4bd7fcb4..21be20b4 100644 --- a/IPA.Loader/Config/Data/Map.cs +++ b/IPA.Loader/Config/Data/Map.cs @@ -7,12 +7,13 @@ namespace IPA.Config.Data { /// - /// A map of to for serialization by an . + /// A ordered map of to for serialization by an . /// Use or to create. /// public sealed class Map : Value, IDictionary { private readonly Dictionary values = new Dictionary(); + private readonly List keyOrder = new List(); internal Map() { } @@ -28,11 +29,15 @@ namespace IPA.Config.Data /// Gets a collection of the keys for the . /// /// - public ICollection Keys => values.Keys; + public ICollection Keys => keyOrder; /// /// Gets a collection of the values in the . /// + /// + /// Unlike all other iterables given by , this does not + /// guarantee that order is maintained. + /// /// public ICollection Values => values.Values; @@ -50,16 +55,24 @@ namespace IPA.Config.Data /// the key to put the value at /// the to add /// - public void Add(string key, Value value) => values.Add(key, value); + public void Add(string key, Value value) + { + values.Add(key, value); + keyOrder.Add(key); + } void ICollection>.Add(KeyValuePair item) - => ((IDictionary)values).Add(item); + => Add(item.Key, item.Value); /// /// Clears the of its key-value pairs. /// /// - public void Clear() => values.Clear(); + public void Clear() + { + values.Clear(); + keyOrder.Clear(); + } bool ICollection>.Contains(KeyValuePair item) => ((IDictionary)values).Contains(item); @@ -80,7 +93,11 @@ namespace IPA.Config.Data /// /// an of key-value pairs in this /// - public IEnumerator> GetEnumerator() => values.GetEnumerator(); + public IEnumerator> GetEnumerator() + { + foreach (var key in keyOrder) + yield return new KeyValuePair(key, this[key]); + } /// /// Removes the object associated with a key in this . @@ -88,10 +105,10 @@ namespace IPA.Config.Data /// the key to remove /// if the key existed, otherwise /// - public bool Remove(string key) => values.Remove(key); + public bool Remove(string key) => values.Remove(key) && keyOrder.Remove(key); bool ICollection>.Remove(KeyValuePair item) - => ((IDictionary)values).Remove(item); + => ((IDictionary)values).Remove(item) && (keyOrder.Remove(item.Key) || true); /// /// Gets the value associated with the specified key. @@ -102,7 +119,7 @@ namespace IPA.Config.Data /// public bool TryGetValue(string key, out Value value) => values.TryGetValue(key, out value); - IEnumerator IEnumerable.GetEnumerator() => ((IDictionary)values).GetEnumerator(); + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); /// /// Converts this into a human-readable format.