ArraySegmentList.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace SuperSocket.Common
  6. {
  7. /// <summary>
  8. /// ArraySegmentList
  9. /// </summary>
  10. /// <typeparam name="T"></typeparam>
  11. public class ArraySegmentList<T> : IList<T>
  12. where T : IEquatable<T>
  13. {
  14. private IList<ArraySegmentEx<T>> m_Segments;
  15. internal IList<ArraySegmentEx<T>> Segments
  16. {
  17. get { return m_Segments; }
  18. }
  19. private ArraySegmentEx<T> m_PrevSegment;
  20. private int m_PrevSegmentIndex;
  21. private int m_Count;
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="ArraySegmentList&lt;T&gt;"/> class.
  24. /// </summary>
  25. public ArraySegmentList()
  26. {
  27. m_Segments = new List<ArraySegmentEx<T>>();
  28. }
  29. #region IList<T> Members
  30. /// <summary>
  31. /// Determines the index of a specific item in the <see cref="T:System.Collections.Generic.IList`1"/>.
  32. /// </summary>
  33. /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.IList`1"/>.</param>
  34. /// <returns>
  35. /// The index of <paramref name="item"/> if found in the list; otherwise, -1.
  36. /// </returns>
  37. public int IndexOf(T item)
  38. {
  39. int index = 0;
  40. for (int i = 0; i < m_Segments.Count; i++)
  41. {
  42. var currentSegment = m_Segments[i];
  43. int offset = currentSegment.Offset;
  44. for (int j = 0; j < currentSegment.Count; j++)
  45. {
  46. if (currentSegment.Array[j + offset].Equals(item))
  47. return index;
  48. index++;
  49. }
  50. }
  51. return -1;
  52. }
  53. /// <summary>
  54. /// NotSupported
  55. /// </summary>
  56. public void Insert(int index, T item)
  57. {
  58. throw new NotSupportedException();
  59. }
  60. /// <summary>
  61. /// NotSupported
  62. /// </summary>
  63. public void RemoveAt(int index)
  64. {
  65. throw new NotSupportedException();
  66. }
  67. /// <summary>
  68. /// Gets or sets the element at the specified index.
  69. /// </summary>
  70. /// <returns>
  71. /// The element at the specified index.
  72. /// </returns>
  73. ///
  74. /// <exception cref="T:System.ArgumentOutOfRangeException"><paramref name="index"/> is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"/>.
  75. /// </exception>
  76. ///
  77. /// <exception cref="T:System.NotSupportedException">
  78. /// The property is set and the <see cref="T:System.Collections.Generic.IList`1"/> is read-only.
  79. /// </exception>
  80. public T this[int index]
  81. {
  82. get
  83. {
  84. ArraySegmentEx<T> segment;
  85. var internalIndex = GetElementInternalIndex(index, out segment);
  86. if (internalIndex < 0)
  87. throw new IndexOutOfRangeException();
  88. return segment.Array[internalIndex];
  89. }
  90. set
  91. {
  92. ArraySegmentEx<T> segment;
  93. var internalIndex = GetElementInternalIndex(index, out segment);
  94. if (internalIndex < 0)
  95. throw new IndexOutOfRangeException();
  96. segment.Array[internalIndex] = value;
  97. }
  98. }
  99. private int GetElementInternalIndex(int index, out ArraySegmentEx<T> segment)
  100. {
  101. segment = null;
  102. if (index < 0 || index > Count - 1)
  103. return -1;
  104. if (index == 0)
  105. {
  106. m_PrevSegment = m_Segments[0];
  107. m_PrevSegmentIndex = 0;
  108. segment = m_PrevSegment;
  109. return m_PrevSegment.Offset;
  110. }
  111. int compareValue = 0;
  112. if (m_PrevSegment != null)
  113. {
  114. if (index >= m_PrevSegment.From)
  115. {
  116. if (index <= m_PrevSegment.To)
  117. {
  118. segment = m_PrevSegment;
  119. return m_PrevSegment.Offset + index - m_PrevSegment.From;
  120. }
  121. else
  122. {
  123. compareValue = 1;
  124. }
  125. }
  126. else
  127. {
  128. compareValue = -1;
  129. }
  130. }
  131. int from, to;
  132. if (compareValue != 0)
  133. {
  134. from = m_PrevSegmentIndex + compareValue;
  135. var trySegment = m_Segments[from];
  136. if (index >= trySegment.From && index <= trySegment.To)
  137. {
  138. segment = trySegment;
  139. return trySegment.Offset + index - trySegment.From;
  140. }
  141. from += compareValue;
  142. var currentSegment = m_Segments[from];
  143. if (index >= currentSegment.From && index <= currentSegment.To)
  144. {
  145. m_PrevSegment = currentSegment;
  146. m_PrevSegmentIndex = from;
  147. segment = currentSegment;
  148. return currentSegment.Offset + index - currentSegment.From;
  149. }
  150. if (compareValue > 0)
  151. {
  152. from++;
  153. to = m_Segments.Count - 1;
  154. }
  155. else
  156. {
  157. var tmp = from - 1;
  158. from = 0;
  159. to = tmp;
  160. }
  161. }
  162. else
  163. {
  164. from = 0;
  165. to = m_Segments.Count - 1;
  166. }
  167. int segmentIndex = -1;
  168. var result = QuickSearchSegment(from, to, index, out segmentIndex);
  169. if (result != null)
  170. {
  171. m_PrevSegment = result;
  172. m_PrevSegmentIndex = segmentIndex;
  173. segment = m_PrevSegment;
  174. return result.Offset + index - result.From;
  175. }
  176. m_PrevSegment = null;
  177. return -1;
  178. }
  179. internal ArraySegmentEx<T> QuickSearchSegment(int from, int to, int index, out int segmentIndex)
  180. {
  181. ArraySegmentEx<T> segment;
  182. segmentIndex = -1;
  183. int diff = to - from;
  184. if (diff == 0)
  185. {
  186. segment = m_Segments[from];
  187. if (index >= segment.From && index <= segment.To)
  188. {
  189. segmentIndex = from;
  190. return segment;
  191. }
  192. return null;
  193. }
  194. else if (diff == 1)
  195. {
  196. segment = m_Segments[from];
  197. if (index >= segment.From && index <= segment.To)
  198. {
  199. segmentIndex = from;
  200. return segment;
  201. }
  202. segment = m_Segments[to];
  203. if (index >= segment.From && index <= segment.To)
  204. {
  205. segmentIndex = to;
  206. return segment;
  207. }
  208. return null;
  209. }
  210. int middle = from + diff / 2;
  211. segment = m_Segments[middle];
  212. if (index >= segment.From)
  213. {
  214. if (index <= segment.To)
  215. {
  216. segmentIndex = middle;
  217. return segment;
  218. }
  219. else
  220. {
  221. return QuickSearchSegment(middle + 1, to, index, out segmentIndex);
  222. }
  223. }
  224. else
  225. {
  226. return QuickSearchSegment(from, middle - 1, index, out segmentIndex);
  227. }
  228. }
  229. #endregion
  230. #region ICollection<T> Members
  231. /// <summary>
  232. /// NotSupported
  233. /// </summary>
  234. public void Add(T item)
  235. {
  236. throw new NotSupportedException();
  237. }
  238. /// <summary>
  239. /// NotSupported
  240. /// </summary>
  241. public void Clear()
  242. {
  243. throw new NotSupportedException();
  244. }
  245. /// <summary>
  246. /// NotSupported
  247. /// </summary>
  248. public bool Contains(T item)
  249. {
  250. throw new NotSupportedException();
  251. }
  252. /// <summary>
  253. /// Copies to.
  254. /// </summary>
  255. /// <param name="array">The array.</param>
  256. /// <param name="arrayIndex">Index of the array.</param>
  257. public void CopyTo(T[] array, int arrayIndex)
  258. {
  259. CopyTo(array, 0, arrayIndex, Math.Min(array.Length, this.Count - arrayIndex));
  260. }
  261. /// <summary>
  262. /// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
  263. /// </summary>
  264. /// <returns>
  265. /// The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
  266. /// </returns>
  267. public int Count
  268. {
  269. get { return m_Count; }
  270. }
  271. /// <summary>
  272. /// Gets a value indicating whether the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
  273. /// </summary>
  274. /// <returns>true if the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only; otherwise, false.
  275. /// </returns>
  276. public bool IsReadOnly
  277. {
  278. get { return true; }
  279. }
  280. /// <summary>
  281. /// NotSupported
  282. /// </summary>
  283. public bool Remove(T item)
  284. {
  285. throw new NotSupportedException();
  286. }
  287. #endregion
  288. #region IEnumerable<T> Members
  289. /// <summary>
  290. /// NotSupported
  291. /// </summary>
  292. public IEnumerator<T> GetEnumerator()
  293. {
  294. throw new NotSupportedException();
  295. }
  296. #endregion
  297. #region IEnumerable Members
  298. /// <summary>
  299. /// NotSupported
  300. /// </summary>
  301. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  302. {
  303. throw new NotSupportedException();
  304. }
  305. #endregion
  306. /// <summary>
  307. /// Removes the segment at.
  308. /// </summary>
  309. /// <param name="index">The index.</param>
  310. public void RemoveSegmentAt(int index)
  311. {
  312. var removedSegment = m_Segments[index];
  313. int removedLen = removedSegment.To - removedSegment.From + 1;
  314. m_Segments.RemoveAt(index);
  315. m_PrevSegment = null;
  316. //the removed item is not the the last item
  317. if (index != m_Segments.Count)
  318. {
  319. for (int i = index; i < m_Segments.Count; i++)
  320. {
  321. m_Segments[i].From -= removedLen;
  322. m_Segments[i].To -= removedLen;
  323. }
  324. }
  325. m_Count -= removedLen;
  326. }
  327. /// <summary>
  328. /// Adds the segment to the list.
  329. /// </summary>
  330. /// <param name="array">The array.</param>
  331. /// <param name="offset">The offset.</param>
  332. /// <param name="length">The length.</param>
  333. public void AddSegment(T[] array, int offset, int length)
  334. {
  335. AddSegment(array, offset, length, false);
  336. }
  337. /// <summary>
  338. /// Adds the segment to the list.
  339. /// </summary>
  340. /// <param name="array">The array.</param>
  341. /// <param name="offset">The offset.</param>
  342. /// <param name="length">The length.</param>
  343. /// <param name="toBeCopied">if set to <c>true</c> [to be copied].</param>
  344. public void AddSegment(T[] array, int offset, int length, bool toBeCopied)
  345. {
  346. if (length <= 0)
  347. return;
  348. var currentTotal = m_Count;
  349. ArraySegmentEx<T> segment = null;
  350. if (!toBeCopied)
  351. segment = new ArraySegmentEx<T>(array, offset, length);
  352. else
  353. segment = new ArraySegmentEx<T>(array.CloneRange(offset, length), 0, length);
  354. segment.From = currentTotal;
  355. m_Count = currentTotal + segment.Count;
  356. segment.To = m_Count - 1;
  357. m_Segments.Add(segment);
  358. }
  359. /// <summary>
  360. /// Gets the segment count.
  361. /// </summary>
  362. public int SegmentCount
  363. {
  364. get { return m_Segments.Count; }
  365. }
  366. /// <summary>
  367. /// Clears all the segements.
  368. /// </summary>
  369. public void ClearSegements()
  370. {
  371. m_Segments.Clear();
  372. m_PrevSegment = null;
  373. m_Count = 0;
  374. }
  375. /// <summary>
  376. /// Read all data in this list to the array data.
  377. /// </summary>
  378. /// <returns></returns>
  379. public T[] ToArrayData()
  380. {
  381. return ToArrayData(0, m_Count);
  382. }
  383. /// <summary>
  384. /// Read the data in specific range to the array data.
  385. /// </summary>
  386. /// <param name="startIndex">The start index.</param>
  387. /// <param name="length">The length.</param>
  388. /// <returns></returns>
  389. public T[] ToArrayData(int startIndex, int length)
  390. {
  391. var result = new T[length];
  392. int from = 0, len = 0, total = 0;
  393. var startSegmentIndex = 0;
  394. if (startIndex != 0)
  395. {
  396. var startSegment = QuickSearchSegment(0, m_Segments.Count - 1, startIndex, out startSegmentIndex);
  397. if (startSegment == null)
  398. throw new IndexOutOfRangeException();
  399. from = startIndex - startSegment.From;
  400. }
  401. for (var i = startSegmentIndex; i < m_Segments.Count; i++)
  402. {
  403. var currentSegment = m_Segments[i];
  404. len = Math.Min(currentSegment.Count - from, length - total);
  405. Array.Copy(currentSegment.Array, currentSegment.Offset + from, result, total, len);
  406. total += len;
  407. if (total >= length)
  408. break;
  409. from = 0;
  410. }
  411. return result;
  412. }
  413. /// <summary>
  414. /// Trims the end.
  415. /// </summary>
  416. /// <param name="trimSize">Size of the trim.</param>
  417. public void TrimEnd(int trimSize)
  418. {
  419. if (trimSize <= 0)
  420. return;
  421. int expectedTo = this.Count - trimSize - 1;
  422. for (int i = m_Segments.Count - 1; i >= 0; i--)
  423. {
  424. var s = m_Segments[i];
  425. if (s.From <= expectedTo && expectedTo < s.To)
  426. {
  427. s.To = expectedTo;
  428. m_Count -= trimSize;
  429. return;
  430. }
  431. RemoveSegmentAt(i);
  432. }
  433. }
  434. /// <summary>
  435. /// Searches the last segment.
  436. /// </summary>
  437. /// <param name="state">The state.</param>
  438. /// <returns></returns>
  439. public int SearchLastSegment(SearchMarkState<T> state)
  440. {
  441. if (m_Segments.Count <= 0)
  442. return -1;
  443. var lastSegment = m_Segments[m_Segments.Count - 1];
  444. if (lastSegment == null)
  445. return -1;
  446. var result = lastSegment.Array.SearchMark(lastSegment.Offset, lastSegment.Count, state.Mark);
  447. if (!result.HasValue)
  448. return -1;
  449. if (result.Value > 0)
  450. {
  451. state.Matched = 0;
  452. return result.Value - lastSegment.Offset + lastSegment.From;
  453. }
  454. state.Matched = 0 - result.Value;
  455. return -1;
  456. }
  457. /// <summary>
  458. /// Copies to.
  459. /// </summary>
  460. /// <param name="to">To.</param>
  461. /// <returns></returns>
  462. public int CopyTo(T[] to)
  463. {
  464. return CopyTo(to, 0, 0, Math.Min(m_Count, to.Length));
  465. }
  466. /// <summary>
  467. /// Copies to.
  468. /// </summary>
  469. /// <param name="to">To.</param>
  470. /// <param name="srcIndex">Index of the SRC.</param>
  471. /// <param name="toIndex">To index.</param>
  472. /// <param name="length">The length.</param>
  473. /// <returns></returns>
  474. public int CopyTo(T[] to, int srcIndex, int toIndex, int length)
  475. {
  476. int copied = 0;
  477. int thisCopied = 0;
  478. int offsetSegmentIndex;
  479. ArraySegmentEx<T> offsetSegment;
  480. if (srcIndex > 0)
  481. offsetSegment = QuickSearchSegment(0, m_Segments.Count - 1, srcIndex, out offsetSegmentIndex);
  482. else
  483. {
  484. offsetSegment = m_Segments[0];
  485. offsetSegmentIndex = 0;
  486. }
  487. int thisOffset = srcIndex - offsetSegment.From + offsetSegment.Offset;
  488. thisCopied = Math.Min(offsetSegment.Count - thisOffset + offsetSegment.Offset, length - copied);
  489. Array.Copy(offsetSegment.Array, thisOffset, to, copied + toIndex, thisCopied);
  490. copied += thisCopied;
  491. if (copied >= length)
  492. return copied;
  493. for (var i = offsetSegmentIndex + 1; i < this.m_Segments.Count; i++)
  494. {
  495. var segment = m_Segments[i];
  496. thisCopied = Math.Min(segment.Count, length - copied);
  497. Array.Copy(segment.Array, segment.Offset, to, copied + toIndex, thisCopied);
  498. copied += thisCopied;
  499. if (copied >= length)
  500. break;
  501. }
  502. return copied;
  503. }
  504. }
  505. /// <summary>
  506. /// ArraySegmentList
  507. /// </summary>
  508. public class ArraySegmentList : ArraySegmentList<byte>
  509. {
  510. /// <summary>
  511. /// Decodes bytes to string by the specified encoding.
  512. /// </summary>
  513. /// <param name="encoding">The encoding.</param>
  514. /// <returns></returns>
  515. public string Decode(Encoding encoding)
  516. {
  517. return Decode(encoding, 0, Count);
  518. }
  519. /// <summary>
  520. /// Decodes bytes to string by the specified encoding.
  521. /// </summary>
  522. /// <param name="encoding">The encoding.</param>
  523. /// <param name="offset">The offset.</param>
  524. /// <param name="length">The length.</param>
  525. /// <returns></returns>
  526. public string Decode(Encoding encoding, int offset, int length)
  527. {
  528. var arraySegments = Segments;
  529. if (arraySegments == null || arraySegments.Count <= 0)
  530. return string.Empty;
  531. var charsBuffer = new char[encoding.GetMaxCharCount(this.Count)];
  532. int bytesUsed, charsUsed;
  533. bool completed;
  534. int totalBytes = 0;
  535. int totalChars = 0;
  536. int lastSegIndex = arraySegments.Count - 1;
  537. var flush = false;
  538. var decoder = encoding.GetDecoder();
  539. int startSegmentIndex = 0;
  540. if (offset > 0)
  541. {
  542. QuickSearchSegment(0, arraySegments.Count - 1, offset, out startSegmentIndex);
  543. }
  544. for (var i = startSegmentIndex; i < arraySegments.Count; i++)
  545. {
  546. var segment = arraySegments[i];
  547. if (i == lastSegIndex)
  548. flush = true;
  549. int decodeOffset = segment.Offset;
  550. int toBeDecoded = Math.Min(length - totalBytes, segment.Count);
  551. if (i == startSegmentIndex && offset > 0)
  552. {
  553. decodeOffset = offset - segment.From + segment.Offset;
  554. toBeDecoded = Math.Min(segment.Count - offset + segment.From, toBeDecoded);
  555. }
  556. decoder.Convert(segment.Array, decodeOffset, toBeDecoded, charsBuffer, totalChars, charsBuffer.Length - totalChars, flush, out bytesUsed, out charsUsed, out completed);
  557. totalChars += charsUsed;
  558. totalBytes += bytesUsed;
  559. if (totalBytes >= length)
  560. break;
  561. }
  562. return new string(charsBuffer, 0, totalChars);
  563. }
  564. /// <summary>
  565. /// Decodes data by the mask.
  566. /// </summary>
  567. /// <param name="mask">The mask.</param>
  568. /// <param name="offset">The offset.</param>
  569. /// <param name="length">The length.</param>
  570. public void DecodeMask(byte[] mask, int offset, int length)
  571. {
  572. int maskLen = mask.Length;
  573. var startSegmentIndex = 0;
  574. var startSegment = QuickSearchSegment(0, Segments.Count - 1, offset, out startSegmentIndex);
  575. var shouldDecode = Math.Min(length, startSegment.Count - offset + startSegment.From);
  576. var from = offset - startSegment.From + startSegment.Offset;
  577. var index = 0;
  578. for (var i = from; i < from + shouldDecode; i++)
  579. {
  580. startSegment.Array[i] = (byte)(startSegment.Array[i] ^ mask[index++ % maskLen]);
  581. }
  582. if (index >= length)
  583. return;
  584. for (var i = startSegmentIndex + 1; i < SegmentCount; i++)
  585. {
  586. var segment = Segments[i];
  587. shouldDecode = Math.Min(length - index, segment.Count);
  588. for (var j = segment.Offset; j < segment.Offset + shouldDecode; j++)
  589. {
  590. segment.Array[j] = (byte)(segment.Array[j] ^ mask[index++ % maskLen]);
  591. }
  592. if (index >= length)
  593. return;
  594. }
  595. }
  596. }
  597. }