LuaTable.cs 7.4 KB

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