You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
5.8 KiB

  1. using IPA.Utilities;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace IPA.Config.Data
  6. {
  7. /// <summary>
  8. /// A ordered map of <see cref="string"/> to <see cref="Value"/> for serialization by an <see cref="IConfigProvider"/>.
  9. /// Use <see cref="Value.Map"/> or <see cref="Value.From(IDictionary{string, Value})"/> to create.
  10. /// </summary>
  11. public sealed class Map : Value, IDictionary<string, Value>
  12. {
  13. private readonly Dictionary<string, Value> values = new Dictionary<string, Value>();
  14. private readonly List<string> keyOrder = new List<string>();
  15. internal Map() { }
  16. /// <summary>
  17. /// Accesses the <see cref="Value"/> at <paramref name="key"/> in the map.
  18. /// </summary>
  19. /// <param name="key">the key to get the value associated with</param>
  20. /// <returns>the value associated with the <paramref name="key"/></returns>
  21. /// <seealso cref="IDictionary{TKey, TValue}.this[TKey]"/>
  22. public Value this[string key] { get => values[key]; set => values[key] = value; }
  23. /// <summary>
  24. /// Gets a collection of the keys for the <see cref="Map"/>.
  25. /// </summary>
  26. /// <seealso cref="IDictionary{TKey, TValue}.Keys"/>
  27. public ICollection<string> Keys => keyOrder;
  28. /// <summary>
  29. /// Gets a collection of the values in the <see cref="Map"/>.
  30. /// </summary>
  31. /// <remarks>
  32. /// Unlike all other iterables given by <see cref="Map"/>, this does <i>not</i>
  33. /// guarantee that order is maintained.
  34. /// </remarks>
  35. /// <seealso cref="IDictionary{TKey, TValue}.Values"/>
  36. public ICollection<Value> Values => values.Values;
  37. /// <summary>
  38. /// Gets the number of key-value pairs in this <see cref="Map"/>.
  39. /// </summary>
  40. /// <seealso cref="ICollection{T}.Count"/>
  41. public int Count => values.Count;
  42. bool ICollection<KeyValuePair<string, Value>>.IsReadOnly => ((IDictionary<string, Value>)values).IsReadOnly;
  43. /// <summary>
  44. /// Adds a new <see cref="Value"/> with a given key.
  45. /// </summary>
  46. /// <param name="key">the key to put the value at</param>
  47. /// <param name="value">the <see cref="Value"/> to add</param>
  48. /// <seealso cref="IDictionary{TKey, TValue}.Add(TKey, TValue)"/>
  49. public void Add(string key, Value value)
  50. {
  51. values.Add(key, value);
  52. keyOrder.Add(key);
  53. }
  54. void ICollection<KeyValuePair<string, Value>>.Add(KeyValuePair<string, Value> item)
  55. => Add(item.Key, item.Value);
  56. /// <summary>
  57. /// Clears the <see cref="Map"/> of its key-value pairs.
  58. /// </summary>
  59. /// <seealso cref="ICollection{T}.Clear"/>
  60. public void Clear()
  61. {
  62. values.Clear();
  63. keyOrder.Clear();
  64. }
  65. bool ICollection<KeyValuePair<string, Value>>.Contains(KeyValuePair<string, Value> item)
  66. => ((IDictionary<string, Value>)values).Contains(item);
  67. /// <summary>
  68. /// Checks if the <see cref="Map"/> contains a given <paramref name="key"/>.
  69. /// </summary>
  70. /// <param name="key">the key to check for</param>
  71. /// <returns><see langword="true"/> if the key exists, otherwise <see langword="false"/></returns>
  72. /// <seealso cref="IDictionary{TKey, TValue}.ContainsKey(TKey)"/>
  73. public bool ContainsKey(string key) => values.ContainsKey(key);
  74. void ICollection<KeyValuePair<string, Value>>.CopyTo(KeyValuePair<string, Value>[] array, int arrayIndex)
  75. => ((IDictionary<string, Value>)values).CopyTo(array, arrayIndex);
  76. /// <summary>
  77. /// Enumerates the <see cref="Map"/>'s key-value pairs.
  78. /// </summary>
  79. /// <returns>an <see cref="IEnumerator{T}"/> of key-value pairs in this <see cref="Map"/></returns>
  80. /// <seealso cref="IEnumerable{T}.GetEnumerator()"/>
  81. public IEnumerator<KeyValuePair<string, Value>> GetEnumerator()
  82. {
  83. foreach (var key in keyOrder)
  84. yield return new KeyValuePair<string, Value>(key, this[key]);
  85. }
  86. /// <summary>
  87. /// Removes the object associated with a key in this <see cref="Map"/>.
  88. /// </summary>
  89. /// <param name="key">the key to remove</param>
  90. /// <returns><see langword="true"/> if the key existed, <see langword="false"/> otherwise</returns>
  91. /// <seealso cref="IDictionary{TKey, TValue}.Remove(TKey)"/>
  92. public bool Remove(string key) => values.Remove(key) && keyOrder.Remove(key);
  93. bool ICollection<KeyValuePair<string, Value>>.Remove(KeyValuePair<string, Value> item)
  94. => ((IDictionary<string, Value>)values).Remove(item) && (keyOrder.Remove(item.Key) || true);
  95. /// <summary>
  96. /// Gets the value associated with the specified key.
  97. /// </summary>
  98. /// <param name="key">the key of the value to get</param>
  99. /// <param name="value">the target location of the retrieved object</param>
  100. /// <returns><see langword="true"/> if the key was found and <paramref name="value"/> set, <see langword="false"/> otherwise</returns>
  101. /// <seealso cref="IDictionary{TKey, TValue}.TryGetValue(TKey, out TValue)"/>
  102. public bool TryGetValue(string key, out Value value) => values.TryGetValue(key, out value);
  103. IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
  104. /// <summary>
  105. /// Converts this <see cref="Value"/> into a human-readable format.
  106. /// </summary>
  107. /// <returns>a JSON-like set of key-value pairs</returns>
  108. public override string ToString()
  109. => $"{{{string.Join(",", this.Select(p => $"\"{p.Key}\":{p.Value?.ToString() ?? "null"}").StrJP())}}}";
  110. }
  111. }