LuaTable.cs 6.8 KB

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