LuaTable.cs 9.5 KB

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