PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/main/contrib/NGit/NGit.Util/BlockList.cs

https://github.com/jfcantin/monodevelop
C# | 407 lines | 276 code | 35 blank | 96 comment | 40 complexity | 6a885e66308f340d24cd548bac208bc9 MD5 | raw file
  1. /*
  2. This code is derived from jgit (http://eclipse.org/jgit).
  3. Copyright owners are documented in jgit's IP log.
  4. This program and the accompanying materials are made available
  5. under the terms of the Eclipse Distribution License v1.0 which
  6. accompanies this distribution, is reproduced below, and is
  7. available at http://www.eclipse.org/org/documents/edl-v10.php
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or
  10. without modification, are permitted provided that the following
  11. conditions are met:
  12. - Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. - Redistributions in binary form must reproduce the above
  15. copyright notice, this list of conditions and the following
  16. disclaimer in the documentation and/or other materials provided
  17. with the distribution.
  18. - Neither the name of the Eclipse Foundation, Inc. nor the
  19. names of its contributors may be used to endorse or promote
  20. products derived from this software without specific prior
  21. written permission.
  22. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  23. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  24. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  25. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  27. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  29. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  30. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  32. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  34. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. using System;
  37. using NGit.Util;
  38. using Sharpen;
  39. namespace NGit.Util
  40. {
  41. /// <summary>Random access list that allocates entries in blocks.</summary>
  42. /// <remarks>
  43. /// Random access list that allocates entries in blocks.
  44. /// <p>
  45. /// Unlike
  46. /// <see cref="System.Collections.ArrayList{E}">System.Collections.ArrayList&lt;E&gt;
  47. /// </see>
  48. /// , this type does not need to reallocate the
  49. /// internal array in order to expand the capacity of the list. Access to any
  50. /// element is constant time, but requires two array lookups instead of one.
  51. /// <p>
  52. /// To handle common usages,
  53. /// <see cref="BlockList{T}.AddItem(object)">BlockList&lt;T&gt;.AddItem(object)</see>
  54. /// and
  55. /// <see cref="BlockList{T}.Iterator()">BlockList&lt;T&gt;.Iterator()</see>
  56. /// use
  57. /// internal code paths to amortize out the second array lookup, making addition
  58. /// and simple iteration closer to one array operation per element processed.
  59. /// <p>
  60. /// Similar to
  61. /// <code>ArrayList</code>
  62. /// , adding or removing from any position except the
  63. /// end of the list requires O(N) time to copy all elements between the
  64. /// modification point and the end of the list. Applications are strongly
  65. /// encouraged to not use this access pattern with this list implementation.
  66. /// </remarks>
  67. /// <?></?>
  68. public class BlockList<T> : AbstractList<T>
  69. {
  70. private const int BLOCK_BITS = 10;
  71. internal const int BLOCK_SIZE = 1 << BLOCK_BITS;
  72. private const int BLOCK_MASK = BLOCK_SIZE - 1;
  73. private T[][] directory;
  74. private int size;
  75. private int tailDirIdx;
  76. private int tailBlkIdx;
  77. private T[] tailBlock;
  78. /// <summary>Initialize an empty list.</summary>
  79. /// <remarks>Initialize an empty list.</remarks>
  80. public BlockList()
  81. {
  82. directory = NGit.Util.BlockList<T>.NewDirectory(256);
  83. directory[0] = NGit.Util.BlockList<T>.NewBlock();
  84. tailBlock = directory[0];
  85. }
  86. /// <summary>Initialize an empty list with an expected capacity.</summary>
  87. /// <remarks>Initialize an empty list with an expected capacity.</remarks>
  88. /// <param name="capacity">number of elements expected to be in the list.</param>
  89. public BlockList(int capacity)
  90. {
  91. int dirSize = ToDirectoryIndex(capacity);
  92. if ((capacity & BLOCK_MASK) != 0 || dirSize == 0)
  93. {
  94. dirSize++;
  95. }
  96. directory = NGit.Util.BlockList<T>.NewDirectory(dirSize);
  97. directory[0] = NGit.Util.BlockList<T>.NewBlock();
  98. tailBlock = directory[0];
  99. }
  100. public override int Count
  101. {
  102. get
  103. {
  104. return size;
  105. }
  106. }
  107. public override void Clear()
  108. {
  109. foreach (T[] block in directory)
  110. {
  111. if (block != null)
  112. {
  113. Arrays.Fill(block, default(T));
  114. }
  115. }
  116. size = 0;
  117. tailDirIdx = 0;
  118. tailBlkIdx = 0;
  119. tailBlock = directory[0];
  120. }
  121. public override T Get(int index)
  122. {
  123. if (index < 0 || size <= index)
  124. {
  125. throw new IndexOutOfRangeException(index.ToString());
  126. }
  127. return directory[ToDirectoryIndex(index)][ToBlockIndex(index)];
  128. }
  129. public override T Set(int index, T element)
  130. {
  131. if (index < 0 || size <= index)
  132. {
  133. throw new IndexOutOfRangeException(index.ToString());
  134. }
  135. T[] blockRef = directory[ToDirectoryIndex(index)];
  136. int blockIdx = ToBlockIndex(index);
  137. T old = blockRef[blockIdx];
  138. blockRef[blockIdx] = element;
  139. return old;
  140. }
  141. /// <summary>Quickly append all elements of another BlockList.</summary>
  142. /// <remarks>Quickly append all elements of another BlockList.</remarks>
  143. /// <param name="src">the list to copy elements from.</param>
  144. public virtual void AddAll(NGit.Util.BlockList<T> src)
  145. {
  146. if (src.size == 0)
  147. {
  148. return;
  149. }
  150. int srcDirIdx = 0;
  151. for (; srcDirIdx < src.tailDirIdx; srcDirIdx++)
  152. {
  153. AddAll(src.directory[srcDirIdx], 0, BLOCK_SIZE);
  154. }
  155. if (src.tailBlkIdx != 0)
  156. {
  157. AddAll(src.tailBlock, 0, src.tailBlkIdx);
  158. }
  159. }
  160. /// <summary>Quickly append all elements from an array.</summary>
  161. /// <remarks>Quickly append all elements from an array.</remarks>
  162. /// <param name="src">the source array.</param>
  163. /// <param name="srcIdx">first index to copy.</param>
  164. /// <param name="srcCnt">number of elements to copy.</param>
  165. public virtual void AddAll(T[] src, int srcIdx, int srcCnt)
  166. {
  167. while (0 < srcCnt)
  168. {
  169. int i = tailBlkIdx;
  170. int n = Math.Min(srcCnt, BLOCK_SIZE - i);
  171. if (n == 0)
  172. {
  173. // Our tail is full, expand by one.
  174. AddItem(src[srcIdx++]);
  175. srcCnt--;
  176. continue;
  177. }
  178. System.Array.Copy(src, srcIdx, tailBlock, i, n);
  179. tailBlkIdx += n;
  180. size += n;
  181. srcIdx += n;
  182. srcCnt -= n;
  183. }
  184. }
  185. public override bool AddItem(T element)
  186. {
  187. int i = tailBlkIdx;
  188. if (i < BLOCK_SIZE)
  189. {
  190. // Fast-path: Append to current tail block.
  191. tailBlock[i] = element;
  192. tailBlkIdx = i + 1;
  193. size++;
  194. return true;
  195. }
  196. // Slow path: Move to the next block, expanding if necessary.
  197. if (++tailDirIdx == directory.Length)
  198. {
  199. T[][] newDir = NGit.Util.BlockList<T>.NewDirectory(directory.Length << 1);
  200. System.Array.Copy(directory, 0, newDir, 0, directory.Length);
  201. directory = newDir;
  202. }
  203. T[] blockRef = directory[tailDirIdx];
  204. if (blockRef == null)
  205. {
  206. blockRef = NGit.Util.BlockList<T>.NewBlock();
  207. directory[tailDirIdx] = blockRef;
  208. }
  209. blockRef[0] = element;
  210. tailBlock = blockRef;
  211. tailBlkIdx = 1;
  212. size++;
  213. return true;
  214. }
  215. public override void Add(int index, T element)
  216. {
  217. if (index == size)
  218. {
  219. // Fast-path: append onto the end of the list.
  220. AddItem(element);
  221. }
  222. else
  223. {
  224. if (index < 0 || size < index)
  225. {
  226. throw new IndexOutOfRangeException(index.ToString());
  227. }
  228. else
  229. {
  230. // Slow-path: the list needs to expand and insert.
  231. // Do this the naive way, callers shouldn't abuse
  232. // this class by entering this code path.
  233. //
  234. AddItem(default(T));
  235. // expand the list by one
  236. for (int oldIdx = size - 2; index <= oldIdx; oldIdx--)
  237. {
  238. Set(oldIdx + 1, this[oldIdx]);
  239. }
  240. Set(index, element);
  241. }
  242. }
  243. }
  244. public override T Remove(int index)
  245. {
  246. if (index == size - 1)
  247. {
  248. // Fast-path: remove the last element.
  249. T[] blockRef = directory[ToDirectoryIndex(index)];
  250. int blockIdx = ToBlockIndex(index);
  251. T old = blockRef[blockIdx];
  252. blockRef[blockIdx] = default(T);
  253. size--;
  254. if (0 < tailBlkIdx)
  255. {
  256. tailBlkIdx--;
  257. }
  258. else
  259. {
  260. ResetTailBlock();
  261. }
  262. return old;
  263. }
  264. else
  265. {
  266. if (index < 0 || size <= index)
  267. {
  268. throw new IndexOutOfRangeException(index.ToString());
  269. }
  270. else
  271. {
  272. // Slow-path: the list needs to contract and remove.
  273. // Do this the naive way, callers shouldn't abuse
  274. // this class by entering this code path.
  275. //
  276. T old = this[index];
  277. for (; index < size - 1; index++)
  278. {
  279. Set(index, this[index + 1]);
  280. }
  281. Set(size - 1, default(T));
  282. size--;
  283. ResetTailBlock();
  284. return old;
  285. }
  286. }
  287. }
  288. private void ResetTailBlock()
  289. {
  290. tailDirIdx = ToDirectoryIndex(size);
  291. tailBlkIdx = ToBlockIndex(size);
  292. tailBlock = directory[tailDirIdx];
  293. }
  294. public override Sharpen.Iterator<T> Iterator()
  295. {
  296. return new BlockList<T>.MyIterator(this);
  297. }
  298. private static int ToDirectoryIndex(int index)
  299. {
  300. return (int)(((uint)index) >> BLOCK_BITS);
  301. }
  302. private static int ToBlockIndex(int index)
  303. {
  304. return index & BLOCK_MASK;
  305. }
  306. private static T[][] NewDirectory(int size)
  307. {
  308. return new T[size][];
  309. }
  310. private static T[] NewBlock()
  311. {
  312. return new T[BLOCK_SIZE];
  313. }
  314. private class MyIterator : Iterator<T>
  315. {
  316. private int index;
  317. private int dirIdx;
  318. private int blkIdx;
  319. private T[] block;
  320. public override bool HasNext()
  321. {
  322. return this.index < this._enclosing.size;
  323. }
  324. public override T Next()
  325. {
  326. if (this._enclosing.size <= this.index)
  327. {
  328. throw new NoSuchElementException();
  329. }
  330. T res = this.block[this.blkIdx];
  331. if (++this.blkIdx == BlockList<T>.BLOCK_SIZE)
  332. {
  333. if (++this.dirIdx < this._enclosing.directory.Length)
  334. {
  335. this.block = this._enclosing.directory[this.dirIdx];
  336. }
  337. else
  338. {
  339. this.block = null;
  340. }
  341. this.blkIdx = 0;
  342. }
  343. this.index++;
  344. return res;
  345. }
  346. public override void Remove()
  347. {
  348. if (this.index == 0)
  349. {
  350. throw new InvalidOperationException();
  351. }
  352. this._enclosing.Remove(--this.index);
  353. this.dirIdx = BlockList<T>.ToDirectoryIndex(this.index);
  354. this.blkIdx = BlockList<T>.ToBlockIndex(this.index);
  355. this.block = this._enclosing.directory[this.dirIdx];
  356. }
  357. internal MyIterator(BlockList<T> _enclosing)
  358. {
  359. this._enclosing = _enclosing;
  360. block = this._enclosing.directory[0];
  361. }
  362. private readonly BlockList<T> _enclosing;
  363. }
  364. }
  365. }