LuaTable.cs 7.2 KB

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