BasicList.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using System;
  2. using System.Collections;
  3. namespace ProtoBuf.Meta
  4. {
  5. internal sealed class MutableList : BasicList
  6. {
  7. /* Like BasicList, but allows existing values to be changed
  8. */
  9. public new object this[int index]
  10. {
  11. get { return head[index]; }
  12. set { head[index] = value; }
  13. }
  14. public void RemoveLast()
  15. {
  16. head.RemoveLastWithMutate();
  17. }
  18. public void Clear()
  19. {
  20. head.Clear();
  21. }
  22. }
  23. internal class BasicList : IEnumerable
  24. {
  25. /* Requirements:
  26. * - Fast access by index
  27. * - Immutable in the tail, so a node can be read (iterated) without locking
  28. * - Lock-free tail handling must match the memory mode; struct for Node
  29. * wouldn't work as "read" would not be atomic
  30. * - Only operation required is append, but this shouldn't go out of its
  31. * way to be inefficient
  32. * - Assume that the caller is handling thread-safety (to co-ordinate with
  33. * other code); no attempt to be thread-safe
  34. * - Assume that the data is private; internal data structure is allowed to
  35. * be mutable (i.e. array is fine as long as we don't screw it up)
  36. */
  37. private static readonly Node nil = new Node(null, 0);
  38. public void CopyTo(Array array, int offset)
  39. {
  40. head.CopyTo(array, offset);
  41. }
  42. protected Node head = nil;
  43. public int Add(object value)
  44. {
  45. return (head = head.Append(value)).Length - 1;
  46. }
  47. public object this[int index] => head[index];
  48. //public object TryGet(int index)
  49. //{
  50. // return head.TryGet(index);
  51. //}
  52. public void Trim() { head = head.Trim(); }
  53. public int Count => head.Length;
  54. IEnumerator IEnumerable.GetEnumerator() => new NodeEnumerator(head);
  55. public NodeEnumerator GetEnumerator() => new NodeEnumerator(head);
  56. public struct NodeEnumerator : IEnumerator
  57. {
  58. private int position;
  59. private readonly Node node;
  60. internal NodeEnumerator(Node node)
  61. {
  62. this.position = -1;
  63. this.node = node;
  64. }
  65. void IEnumerator.Reset() { position = -1; }
  66. public object Current { get { return node[position]; } }
  67. public bool MoveNext()
  68. {
  69. int len = node.Length;
  70. return (position <= len) && (++position < len);
  71. }
  72. }
  73. internal sealed class Node
  74. {
  75. public object this[int index]
  76. {
  77. get
  78. {
  79. if (index >= 0 && index < length)
  80. {
  81. return data[index];
  82. }
  83. throw new ArgumentOutOfRangeException(nameof(index));
  84. }
  85. set
  86. {
  87. if (index >= 0 && index < length)
  88. {
  89. data[index] = value;
  90. }
  91. else
  92. {
  93. throw new ArgumentOutOfRangeException(nameof(index));
  94. }
  95. }
  96. }
  97. //public object TryGet(int index)
  98. //{
  99. // return (index >= 0 && index < length) ? data[index] : null;
  100. //}
  101. private readonly object[] data;
  102. private int length;
  103. public int Length => length;
  104. internal Node(object[] data, int length)
  105. {
  106. Helpers.DebugAssert((data == null && length == 0) ||
  107. (data != null && length > 0 && length <= data.Length));
  108. this.data = data;
  109. this.length = length;
  110. }
  111. public void RemoveLastWithMutate()
  112. {
  113. if (length == 0) throw new InvalidOperationException();
  114. length -= 1;
  115. }
  116. public Node Append(object value)
  117. {
  118. object[] newData;
  119. int newLength = length + 1;
  120. if (data == null)
  121. {
  122. newData = new object[10];
  123. }
  124. else if (length == data.Length)
  125. {
  126. newData = new object[data.Length * 2];
  127. Array.Copy(data, newData, length);
  128. }
  129. else
  130. {
  131. newData = data;
  132. }
  133. newData[length] = value;
  134. return new Node(newData, newLength);
  135. }
  136. public Node Trim()
  137. {
  138. if (length == 0 || length == data.Length) return this;
  139. object[] newData = new object[length];
  140. Array.Copy(data, newData, length);
  141. return new Node(newData, length);
  142. }
  143. internal int IndexOfString(string value)
  144. {
  145. for (int i = 0; i < length; i++)
  146. {
  147. if ((string)value == (string)data[i]) return i;
  148. }
  149. return -1;
  150. }
  151. internal int IndexOfReference(object instance)
  152. {
  153. for (int i = 0; i < length; i++)
  154. {
  155. if ((object)instance == (object)data[i]) return i;
  156. } // ^^^ (object) above should be preserved, even if this was typed; needs
  157. // to be a reference check
  158. return -1;
  159. }
  160. internal int IndexOf(MatchPredicate predicate, object ctx)
  161. {
  162. for (int i = 0; i < length; i++)
  163. {
  164. if (predicate(data[i], ctx)) return i;
  165. }
  166. return -1;
  167. }
  168. internal void CopyTo(Array array, int offset)
  169. {
  170. if (length > 0)
  171. {
  172. Array.Copy(data, 0, array, offset, length);
  173. }
  174. }
  175. internal void Clear()
  176. {
  177. if (data != null)
  178. {
  179. Array.Clear(data, 0, data.Length);
  180. }
  181. length = 0;
  182. }
  183. }
  184. internal int IndexOf(MatchPredicate predicate, object ctx)
  185. {
  186. return head.IndexOf(predicate, ctx);
  187. }
  188. internal int IndexOfString(string value)
  189. {
  190. return head.IndexOfString(value);
  191. }
  192. internal int IndexOfReference(object instance)
  193. {
  194. return head.IndexOfReference(instance);
  195. }
  196. internal delegate bool MatchPredicate(object value, object ctx);
  197. internal bool Contains(object value)
  198. {
  199. foreach (object obj in this)
  200. {
  201. if (object.Equals(obj, value)) return true;
  202. }
  203. return false;
  204. }
  205. internal sealed class Group
  206. {
  207. public readonly int First;
  208. public readonly BasicList Items;
  209. public Group(int first)
  210. {
  211. this.First = first;
  212. this.Items = new BasicList();
  213. }
  214. }
  215. internal static BasicList GetContiguousGroups(int[] keys, object[] values)
  216. {
  217. if (keys == null) throw new ArgumentNullException(nameof(keys));
  218. if (values == null) throw new ArgumentNullException(nameof(values));
  219. if (values.Length < keys.Length) throw new ArgumentException("Not all keys are covered by values", nameof(values));
  220. BasicList outer = new BasicList();
  221. Group group = null;
  222. for (int i = 0; i < keys.Length; i++)
  223. {
  224. if (i == 0 || keys[i] != keys[i - 1]) { group = null; }
  225. if (group == null)
  226. {
  227. group = new Group(keys[i]);
  228. outer.Add(group);
  229. }
  230. group.Items.Add(values[i]);
  231. }
  232. return outer;
  233. }
  234. }
  235. }