LuaTable.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using System.Runtime.CompilerServices;
  2. using Lua.Internal;
  3. namespace Lua;
  4. public sealed class LuaTable
  5. {
  6. public LuaTable() : this(8, 8)
  7. {
  8. }
  9. public LuaTable(IEnumerable<LuaValue> values)
  10. {
  11. array = values.ToArray();
  12. dictionary = [];
  13. }
  14. public LuaTable(IEnumerable<KeyValuePair<LuaValue, LuaValue>> values)
  15. {
  16. array = [];
  17. dictionary = new Dictionary<LuaValue, LuaValue>(values);
  18. }
  19. public LuaTable(int arrayCapacity, int dictionaryCapacity)
  20. {
  21. array = new LuaValue[arrayCapacity];
  22. dictionary = new(dictionaryCapacity);
  23. }
  24. LuaValue[] array;
  25. Dictionary<LuaValue, LuaValue> dictionary;
  26. LuaTable? metatable;
  27. public LuaValue this[LuaValue key]
  28. {
  29. get
  30. {
  31. if (key.Type is LuaValueType.Nil)
  32. {
  33. throw new ArgumentException("table index is nil");
  34. }
  35. if (TryGetInteger(key, out var index))
  36. {
  37. if (index > 0 && index <= array.Length)
  38. {
  39. // Arrays in Lua are 1-origin...
  40. return array[index - 1];
  41. }
  42. }
  43. if (dictionary.TryGetValue(key, out var value)) return value;
  44. return LuaValue.Nil;
  45. }
  46. set
  47. {
  48. if (TryGetInteger(key, out var index))
  49. {
  50. if (0 < index && index <= array.Length * 2)
  51. {
  52. EnsureArrayCapacity(index);
  53. array[index - 1] = value;
  54. return;
  55. }
  56. }
  57. if (value.Type is LuaValueType.Nil)
  58. {
  59. dictionary.Remove(key);
  60. }
  61. else
  62. {
  63. dictionary[key] = value;
  64. }
  65. }
  66. }
  67. public int HashMapCount
  68. {
  69. get => dictionary.Count;
  70. }
  71. public int ArrayLength
  72. {
  73. get
  74. {
  75. for (int i = 0; i < array.Length; i++)
  76. {
  77. if (array[i].Type is LuaValueType.Nil) return i;
  78. }
  79. return array.Length;
  80. }
  81. }
  82. public LuaTable? Metatable
  83. {
  84. get => metatable;
  85. set => metatable = value;
  86. }
  87. public bool TryGetValue(LuaValue key, out LuaValue value)
  88. {
  89. if (key.Type is LuaValueType.Nil)
  90. {
  91. value = default;
  92. return false;
  93. }
  94. if (TryGetInteger(key, out var index))
  95. {
  96. if (index > 0 && index <= array.Length)
  97. {
  98. value = array[index - 1];
  99. return value.Type is not LuaValueType.Nil;
  100. }
  101. }
  102. return dictionary.TryGetValue(key, out value);
  103. }
  104. public bool ContainsKey(LuaValue key)
  105. {
  106. if (key.Type is LuaValueType.Nil)
  107. {
  108. return false;
  109. }
  110. if (TryGetInteger(key, out var index))
  111. {
  112. return index > 0 && index <= array.Length && array[index].Type != LuaValueType.Nil;
  113. }
  114. return dictionary.ContainsKey(key);
  115. }
  116. public LuaValue RemoveAt(int index)
  117. {
  118. if (index <= 0 || index > array.Length)
  119. {
  120. throw new IndexOutOfRangeException();
  121. }
  122. var arrayIndex = index - 1;
  123. var value = array[arrayIndex];
  124. if (arrayIndex < array.Length - 1)
  125. {
  126. array.AsSpan(arrayIndex + 1).CopyTo(array.AsSpan(arrayIndex));
  127. }
  128. array[^1] = default;
  129. return value;
  130. }
  131. public void Insert(int index, LuaValue value)
  132. {
  133. if (index <= 0 || index > array.Length + 1)
  134. {
  135. throw new IndexOutOfRangeException();
  136. }
  137. var arrayIndex = index - 1;
  138. EnsureArrayCapacity(array.Length + 1);
  139. if (arrayIndex != array.Length - 1)
  140. {
  141. array.AsSpan(arrayIndex, array.Length - arrayIndex - 1).CopyTo(array.AsSpan(arrayIndex + 1));
  142. }
  143. array[arrayIndex] = value;
  144. }
  145. public KeyValuePair<LuaValue, LuaValue> GetNext(LuaValue key)
  146. {
  147. var index = -1;
  148. if (key.Type is LuaValueType.Nil)
  149. {
  150. index = 0;
  151. }
  152. else if (TryGetInteger(key, out var integer) && integer > 0 && integer <= array.Length)
  153. {
  154. index = integer;
  155. }
  156. if (index != -1)
  157. {
  158. var span = array.AsSpan(index);
  159. for (int i = 0; i < span.Length; i++)
  160. {
  161. if (span[i].Type is not LuaValueType.Nil)
  162. {
  163. return new(index + i + 1, span[i]);
  164. }
  165. }
  166. foreach (var pair in dictionary)
  167. {
  168. return pair;
  169. }
  170. }
  171. else
  172. {
  173. var foundKey = false;
  174. foreach (var pair in dictionary)
  175. {
  176. if (foundKey) return pair;
  177. if (pair.Key.Equals(key))
  178. {
  179. foundKey = true;
  180. }
  181. }
  182. }
  183. return default;
  184. }
  185. public void Clear()
  186. {
  187. dictionary.Clear();
  188. }
  189. public Span<LuaValue> GetArraySpan()
  190. {
  191. return array.AsSpan();
  192. }
  193. internal void EnsureArrayCapacity(int newCapacity)
  194. {
  195. if (array.Length >= newCapacity) return;
  196. var prevLength = array.Length;
  197. var newLength = array.Length;
  198. if (newLength == 0) newLength = 8;
  199. while (newLength < newCapacity)
  200. {
  201. newLength *= 2;
  202. }
  203. Array.Resize(ref array, newLength);
  204. using var indexList = new PooledList<(int, LuaValue)>(dictionary.Count);
  205. // Move some of the elements of the hash part to a newly allocated array
  206. foreach (var kv in dictionary)
  207. {
  208. if (kv.Key.TryRead<double>(out var d) && MathEx.IsInteger(d))
  209. {
  210. var index = (int)d;
  211. if (index > prevLength && index <= newLength)
  212. {
  213. indexList.Add((index, kv.Value));
  214. }
  215. }
  216. }
  217. foreach ((var index, var value) in indexList.AsSpan())
  218. {
  219. dictionary.Remove(index);
  220. array[index - 1] = value;
  221. }
  222. }
  223. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  224. static bool TryGetInteger(LuaValue value, out int integer)
  225. {
  226. if (value.TryRead<double>(out var num) && MathEx.IsInteger(num))
  227. {
  228. integer = (int)num;
  229. return true;
  230. }
  231. integer = default;
  232. return false;
  233. }
  234. }