BasicList.cs 8.0 KB

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