LuaTable.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. using System.Runtime.CompilerServices;
  2. using Lua.Internal;
  3. using System.Collections;
  4. namespace Lua;
  5. public sealed class LuaTable : IEnumerable<KeyValuePair<LuaValue, LuaValue>>
  6. {
  7. public LuaTable() : this(8, 8)
  8. {
  9. }
  10. public LuaTable(int arrayCapacity, int dictionaryCapacity)
  11. {
  12. array = arrayCapacity > 1 ? new LuaValue[arrayCapacity] : [];
  13. dictionary = new(dictionaryCapacity);
  14. }
  15. LuaValue[] array;
  16. readonly LuaValueDictionary dictionary;
  17. LuaTable? metatable;
  18. internal LuaValueDictionary Dictionary => dictionary;
  19. const int MaxArraySize = 1 << 24;
  20. const int MaxDistance = 1 << 12;
  21. public LuaValue this[LuaValue key]
  22. {
  23. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  24. get
  25. {
  26. if (key.Type is LuaValueType.Nil)
  27. {
  28. ThrowIndexIsNil();
  29. }
  30. if (TryGetInteger(key, out var index))
  31. {
  32. if (index > 0 && index <= array.Length)
  33. {
  34. // Arrays in Lua are 1-origin...
  35. return array[index - 1];
  36. }
  37. }
  38. if (dictionary.TryGetValue(key, out var value))
  39. {
  40. return value;
  41. }
  42. return LuaValue.Nil;
  43. }
  44. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  45. set
  46. {
  47. if (key.TryReadNumber(out var d))
  48. {
  49. if (double.IsNaN(d))
  50. {
  51. ThrowIndexIsNaN();
  52. }
  53. if (MathEx.IsInteger(d))
  54. {
  55. var index = (int)d;
  56. var distance = index - array.Length;
  57. if (distance > MaxDistance)
  58. {
  59. dictionary[key] = value;
  60. return;
  61. }
  62. if (0 < index && index < MaxArraySize && index <= Math.Max(array.Length * 2, 8))
  63. {
  64. if (array.Length < index)
  65. {
  66. EnsureArrayCapacity(index);
  67. }
  68. array[index - 1] = value;
  69. return;
  70. }
  71. }
  72. }
  73. dictionary[key] = value;
  74. }
  75. }
  76. public int HashMapCount => dictionary.Count - dictionary.NilCount;
  77. public int ArrayLength
  78. {
  79. get
  80. {
  81. for (var i = 0; i < array.Length; i++)
  82. {
  83. if (array[i].Type is LuaValueType.Nil)
  84. {
  85. return i;
  86. }
  87. }
  88. return array.Length;
  89. }
  90. }
  91. public LuaTable? Metatable
  92. {
  93. get => metatable;
  94. set => metatable = value;
  95. }
  96. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  97. public bool TryGetValue(LuaValue key, out LuaValue value)
  98. {
  99. if (key.Type is LuaValueType.Nil)
  100. {
  101. value = default;
  102. return false;
  103. }
  104. if (TryGetInteger(key, out var index))
  105. {
  106. if (index > 0 && index <= array.Length)
  107. {
  108. value = array[index - 1];
  109. return value.Type is not LuaValueType.Nil;
  110. }
  111. }
  112. return dictionary.TryGetValue(key, out value) && value.Type is not LuaValueType.Nil;
  113. }
  114. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  115. internal ref LuaValue FindValue(LuaValue key)
  116. {
  117. if (key.Type is LuaValueType.Nil)
  118. {
  119. ThrowIndexIsNil();
  120. }
  121. if (TryGetInteger(key, out var index))
  122. {
  123. if (index > 0 && index <= array.Length)
  124. {
  125. return ref array[index - 1];
  126. }
  127. }
  128. return ref dictionary.FindValue(key, out _);
  129. }
  130. public bool ContainsKey(LuaValue key)
  131. {
  132. if (key.Type is LuaValueType.Nil)
  133. {
  134. return false;
  135. }
  136. if (TryGetInteger(key, out var index))
  137. {
  138. return index > 0 && index <= array.Length &&
  139. array[index - 1].Type != LuaValueType.Nil;
  140. }
  141. return dictionary.TryGetValue(key, out var value) && value.Type is not LuaValueType.Nil;
  142. }
  143. public LuaValue RemoveAt(int index)
  144. {
  145. var arrayIndex = index - 1;
  146. var value = array[arrayIndex];
  147. if (arrayIndex < array.Length - 1)
  148. {
  149. array.AsSpan(arrayIndex + 1).CopyTo(array.AsSpan(arrayIndex));
  150. }
  151. array[^1] = default;
  152. return value;
  153. }
  154. public void Insert(int index, LuaValue value)
  155. {
  156. if (index <= 0 || index > array.Length + 1)
  157. {
  158. throw new IndexOutOfRangeException();
  159. }
  160. var arrayIndex = index - 1;
  161. var distance = index - array.Length;
  162. if (distance > MaxDistance)
  163. {
  164. dictionary[index] = value;
  165. return;
  166. }
  167. if (index > array.Length || array[^1].Type != LuaValueType.Nil)
  168. {
  169. EnsureArrayCapacity(array.Length + 1);
  170. }
  171. if (arrayIndex != array.Length - 1)
  172. {
  173. array.AsSpan(arrayIndex, array.Length - arrayIndex - 1).CopyTo(array.AsSpan(arrayIndex + 1));
  174. }
  175. array[arrayIndex] = value;
  176. }
  177. public bool TryGetNext(LuaValue key, out KeyValuePair<LuaValue, LuaValue> pair)
  178. {
  179. var index = -1;
  180. if (key.Type is LuaValueType.Nil)
  181. {
  182. index = 0;
  183. }
  184. else if (TryGetInteger(key, out var integer) && integer > 0 && integer <= array.Length)
  185. {
  186. index = integer;
  187. }
  188. if (index != -1)
  189. {
  190. var span = array.AsSpan(index);
  191. for (var i = 0; i < span.Length; i++)
  192. {
  193. if (span[i].Type is not LuaValueType.Nil)
  194. {
  195. pair = new(index + i + 1, span[i]);
  196. return true;
  197. }
  198. }
  199. foreach (var kv in dictionary)
  200. {
  201. if (kv.Value.Type is not LuaValueType.Nil)
  202. {
  203. pair = kv;
  204. return true;
  205. }
  206. }
  207. }
  208. else
  209. {
  210. if (dictionary.TryGetNext(key, out pair))
  211. {
  212. return true;
  213. }
  214. }
  215. pair = default;
  216. return false;
  217. }
  218. public void Clear()
  219. {
  220. array.AsSpan().Clear();
  221. dictionary.Clear();
  222. }
  223. public Memory<LuaValue> GetArrayMemory()
  224. {
  225. return array.AsMemory();
  226. }
  227. public Span<LuaValue> GetArraySpan()
  228. {
  229. return array.AsSpan();
  230. }
  231. internal void EnsureArrayCapacity(int newCapacity)
  232. {
  233. if (array.Length >= newCapacity)
  234. {
  235. return;
  236. }
  237. var prevLength = array.Length;
  238. var newLength = array.Length;
  239. newLength = newCapacity <= 8 ? 8 : MathEx.NextPowerOfTwo(newCapacity);
  240. Array.Resize(ref array, newLength);
  241. using PooledList<(int, LuaValue)> indexList = new(dictionary.Count);
  242. // Move some of the elements of the hash part to a newly allocated array
  243. foreach (var kv in dictionary)
  244. {
  245. if (TryGetInteger(kv.Key, out var index))
  246. {
  247. if (index > prevLength && index <= newLength)
  248. {
  249. indexList.Add((index, kv.Value));
  250. }
  251. }
  252. }
  253. foreach (var (index, value) in indexList.AsSpan())
  254. {
  255. dictionary.Remove(index);
  256. array[index - 1] = value;
  257. }
  258. }
  259. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  260. static bool TryGetInteger(LuaValue value, out int integer)
  261. {
  262. if (value.TryReadNumber(out var num) && MathEx.IsInteger(num))
  263. {
  264. integer = (int)num;
  265. return true;
  266. }
  267. integer = default;
  268. return false;
  269. }
  270. static void ThrowIndexIsNil()
  271. {
  272. throw new ArgumentException("the table index is nil");
  273. }
  274. static void ThrowIndexIsNaN()
  275. {
  276. throw new ArgumentException("the table index is NaN");
  277. }
  278. public LuaTableEnumerator GetEnumerator()
  279. {
  280. return new(this);
  281. }
  282. IEnumerator<KeyValuePair<LuaValue, LuaValue>> IEnumerable<KeyValuePair<LuaValue, LuaValue>>.GetEnumerator()
  283. {
  284. return new LuaTableEnumerator(this);
  285. }
  286. IEnumerator IEnumerable.GetEnumerator()
  287. {
  288. return new LuaTableEnumerator(this);
  289. }
  290. public struct LuaTableEnumerator(LuaTable table) : IEnumerator<KeyValuePair<LuaValue, LuaValue>>
  291. {
  292. public KeyValuePair<LuaValue, LuaValue> Current => current;
  293. int index = -1;
  294. readonly int version = table.dictionary.Version;
  295. KeyValuePair<LuaValue, LuaValue> current = default;
  296. public bool MoveNext()
  297. {
  298. if (index < 0)
  299. {
  300. var arrayIndex = -index - 1;
  301. var span = table.array.AsSpan(arrayIndex);
  302. for (var i = 0; i < span.Length; i++)
  303. {
  304. if (span[i].Type is not LuaValueType.Nil)
  305. {
  306. current = new(arrayIndex + i + 1, span[i]);
  307. index = -arrayIndex - i - 2;
  308. return true;
  309. }
  310. }
  311. index = 0;
  312. }
  313. while (LuaValueDictionary.MoveNext(table.Dictionary, version, ref index, out current) && current.Value.Type is LuaValueType.Nil)
  314. {
  315. }
  316. return current.Value.Type is not LuaValueType.Nil;
  317. }
  318. public void Reset()
  319. {
  320. }
  321. object IEnumerator.Current => Current;
  322. public void Dispose()
  323. {
  324. }
  325. }
  326. }