LuaTable.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. [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 MemoryMarshalEx.UnsafeElementAt(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.Type is LuaValueType.Number && double.IsNaN(key.UnsafeRead<double>()))
  38. {
  39. ThrowIndexIsNaN();
  40. }
  41. if (TryGetInteger(key, out var index))
  42. {
  43. if (0 < index && index <= Math.Max(array.Length * 2, 8))
  44. {
  45. EnsureArrayCapacity(index);
  46. MemoryMarshalEx.UnsafeElementAt(array, index - 1) = value;
  47. return;
  48. }
  49. }
  50. dictionary[key] = value;
  51. }
  52. }
  53. public int HashMapCount
  54. {
  55. get => dictionary.Count(x => x.Value.Type is not LuaValueType.Nil);
  56. }
  57. public int ArrayLength
  58. {
  59. get
  60. {
  61. for (int i = 0; i < array.Length; i++)
  62. {
  63. if (array[i].Type is LuaValueType.Nil) return i;
  64. }
  65. return array.Length;
  66. }
  67. }
  68. public LuaTable? Metatable
  69. {
  70. get => metatable;
  71. set => metatable = value;
  72. }
  73. public bool TryGetValue(LuaValue key, out LuaValue value)
  74. {
  75. if (key.Type is LuaValueType.Nil)
  76. {
  77. value = default;
  78. return false;
  79. }
  80. if (TryGetInteger(key, out var index))
  81. {
  82. if (index > 0 && index <= array.Length)
  83. {
  84. value = MemoryMarshalEx.UnsafeElementAt(array, index - 1);
  85. return value.Type is not LuaValueType.Nil;
  86. }
  87. }
  88. return dictionary.TryGetValue(key, out value) && value.Type is not LuaValueType.Nil;
  89. }
  90. public bool ContainsKey(LuaValue key)
  91. {
  92. if (key.Type is LuaValueType.Nil)
  93. {
  94. return false;
  95. }
  96. if (TryGetInteger(key, out var index))
  97. {
  98. return index > 0 && index <= array.Length &&
  99. MemoryMarshalEx.UnsafeElementAt(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 = MemoryMarshalEx.UnsafeElementAt(array, arrayIndex);
  111. if (arrayIndex < array.Length - 1)
  112. {
  113. array.AsSpan(arrayIndex + 1).CopyTo(array.AsSpan(arrayIndex));
  114. }
  115. MemoryMarshalEx.UnsafeElementAt(array, array.Length - 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 (TryGetInteger(kv.Key, out var index))
  210. {
  211. if (index > prevLength && index <= newLength)
  212. {
  213. indexList.Add((index, kv.Value));
  214. }
  215. }
  216. }
  217. foreach ((var index, var value) in indexList.AsSpan())
  218. {
  219. dictionary.Remove(index);
  220. array[index - 1] = value;
  221. }
  222. }
  223. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  224. static bool TryGetInteger(LuaValue value, out int integer)
  225. {
  226. if (value.TryReadNumber(out var num) && MathEx.IsInteger(num))
  227. {
  228. integer = (int)num;
  229. return true;
  230. }
  231. integer = default;
  232. return false;
  233. }
  234. static void ThrowIndexIsNil()
  235. {
  236. throw new ArgumentException("the table index is nil");
  237. }
  238. static void ThrowIndexIsNaN()
  239. {
  240. throw new ArgumentException("the table index is NaN");
  241. }
  242. }