Browse Source

Made Map keep order

pull/46/head
Anairkoen Schno 5 years ago
parent
commit
27baa97f17
1 changed files with 26 additions and 9 deletions
  1. +26
    -9
      IPA.Loader/Config/Data/Map.cs

+ 26
- 9
IPA.Loader/Config/Data/Map.cs View File

@ -7,12 +7,13 @@ namespace IPA.Config.Data
{ {
/// <summary> /// <summary>
/// A map of <see cref="string"/> to <see cref="Value"/> for serialization by an <see cref="IConfigProvider"/>.
/// A ordered map of <see cref="string"/> to <see cref="Value"/> for serialization by an <see cref="IConfigProvider"/>.
/// Use <see cref="Value.Map"/> or <see cref="Value.From(IDictionary{string, Value})"/> to create. /// Use <see cref="Value.Map"/> or <see cref="Value.From(IDictionary{string, Value})"/> to create.
/// </summary> /// </summary>
public sealed class Map : Value, IDictionary<string, Value> public sealed class Map : Value, IDictionary<string, Value>
{ {
private readonly Dictionary<string, Value> values = new Dictionary<string, Value>(); private readonly Dictionary<string, Value> values = new Dictionary<string, Value>();
private readonly List<string> keyOrder = new List<string>();
internal Map() { } internal Map() { }
@ -28,11 +29,15 @@ namespace IPA.Config.Data
/// Gets a collection of the keys for the <see cref="Map"/>. /// Gets a collection of the keys for the <see cref="Map"/>.
/// </summary> /// </summary>
/// <seealso cref="IDictionary{TKey, TValue}.Keys"/> /// <seealso cref="IDictionary{TKey, TValue}.Keys"/>
public ICollection<string> Keys => values.Keys;
public ICollection<string> Keys => keyOrder;
/// <summary> /// <summary>
/// Gets a collection of the values in the <see cref="Map"/>. /// Gets a collection of the values in the <see cref="Map"/>.
/// </summary> /// </summary>
/// <remarks>
/// Unlike all other iterables given by <see cref="Map"/>, this does <i>not</i>
/// guarantee that order is maintained.
/// </remarks>
/// <seealso cref="IDictionary{TKey, TValue}.Values"/> /// <seealso cref="IDictionary{TKey, TValue}.Values"/>
public ICollection<Value> Values => values.Values; public ICollection<Value> Values => values.Values;
@ -50,16 +55,24 @@ namespace IPA.Config.Data
/// <param name="key">the key to put the value at</param> /// <param name="key">the key to put the value at</param>
/// <param name="value">the <see cref="Value"/> to add</param> /// <param name="value">the <see cref="Value"/> to add</param>
/// <seealso cref="IDictionary{TKey, TValue}.Add(TKey, TValue)"/> /// <seealso cref="IDictionary{TKey, TValue}.Add(TKey, TValue)"/>
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<KeyValuePair<string, Value>>.Add(KeyValuePair<string, Value> item) void ICollection<KeyValuePair<string, Value>>.Add(KeyValuePair<string, Value> item)
=> ((IDictionary<string, Value>)values).Add(item);
=> Add(item.Key, item.Value);
/// <summary> /// <summary>
/// Clears the <see cref="Map"/> of its key-value pairs. /// Clears the <see cref="Map"/> of its key-value pairs.
/// </summary> /// </summary>
/// <seealso cref="ICollection{T}.Clear"/> /// <seealso cref="ICollection{T}.Clear"/>
public void Clear() => values.Clear();
public void Clear()
{
values.Clear();
keyOrder.Clear();
}
bool ICollection<KeyValuePair<string, Value>>.Contains(KeyValuePair<string, Value> item) bool ICollection<KeyValuePair<string, Value>>.Contains(KeyValuePair<string, Value> item)
=> ((IDictionary<string, Value>)values).Contains(item); => ((IDictionary<string, Value>)values).Contains(item);
@ -80,7 +93,11 @@ namespace IPA.Config.Data
/// </summary> /// </summary>
/// <returns>an <see cref="IEnumerator{T}"/> of key-value pairs in this <see cref="Map"/></returns> /// <returns>an <see cref="IEnumerator{T}"/> of key-value pairs in this <see cref="Map"/></returns>
/// <seealso cref="IEnumerable{T}.GetEnumerator()"/> /// <seealso cref="IEnumerable{T}.GetEnumerator()"/>
public IEnumerator<KeyValuePair<string, Value>> GetEnumerator() => values.GetEnumerator();
public IEnumerator<KeyValuePair<string, Value>> GetEnumerator()
{
foreach (var key in keyOrder)
yield return new KeyValuePair<string, Value>(key, this[key]);
}
/// <summary> /// <summary>
/// Removes the object associated with a key in this <see cref="Map"/>. /// Removes the object associated with a key in this <see cref="Map"/>.
@ -88,10 +105,10 @@ namespace IPA.Config.Data
/// <param name="key">the key to remove</param> /// <param name="key">the key to remove</param>
/// <returns><see langword="true"/> if the key existed, <see langword="false"/> otherwise</returns> /// <returns><see langword="true"/> if the key existed, <see langword="false"/> otherwise</returns>
/// <seealso cref="IDictionary{TKey, TValue}.Remove(TKey)"/> /// <seealso cref="IDictionary{TKey, TValue}.Remove(TKey)"/>
public bool Remove(string key) => values.Remove(key);
public bool Remove(string key) => values.Remove(key) && keyOrder.Remove(key);
bool ICollection<KeyValuePair<string, Value>>.Remove(KeyValuePair<string, Value> item) bool ICollection<KeyValuePair<string, Value>>.Remove(KeyValuePair<string, Value> item)
=> ((IDictionary<string, Value>)values).Remove(item);
=> ((IDictionary<string, Value>)values).Remove(item) && (keyOrder.Remove(item.Key) || true);
/// <summary> /// <summary>
/// Gets the value associated with the specified key. /// Gets the value associated with the specified key.
@ -102,7 +119,7 @@ namespace IPA.Config.Data
/// <seealso cref="IDictionary{TKey, TValue}.TryGetValue(TKey, out TValue)"/> /// <seealso cref="IDictionary{TKey, TValue}.TryGetValue(TKey, out TValue)"/>
public bool TryGetValue(string key, out Value value) => values.TryGetValue(key, out value); public bool TryGetValue(string key, out Value value) => values.TryGetValue(key, out value);
IEnumerator IEnumerable.GetEnumerator() => ((IDictionary<string, Value>)values).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
/// <summary> /// <summary>
/// Converts this <see cref="Value"/> into a human-readable format. /// Converts this <see cref="Value"/> into a human-readable format.


Loading…
Cancel
Save