PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/PowerLib/PowerLib.System/Collections/Extensions/BitsListExtension.cs

#
C# | 2260 lines | 2080 code | 94 blank | 86 comment | 939 complexity | 5000a3e8c3bee9b63a4a518e2c58e875 MD5 | raw file
Possible License(s): CC-BY-SA-3.0

Large files files are truncated, but you can click here to view the full file

  1. using System;
  2. using System.Collections.Generic;
  3. namespace PowerLib.System.Collections.Extensions
  4. {
  5. /// <summary>
  6. /// Extension methods to operate with bits packed in interger type collections.
  7. /// </summary>
  8. public static class BitsListExtension
  9. {
  10. #region Factor constants
  11. private const int byteFactor = sizeof(byte) * 8;
  12. private const int ushortFactor = sizeof(ushort) * 8;
  13. private const int uintFactor = sizeof(uint) * 8;
  14. private const int ulongFactor = sizeof(ulong) * 8;
  15. private const int sbyteFactor = sizeof(sbyte) * 8;
  16. private const int shortFactor = sizeof(short) * 8;
  17. private const int intFactor = sizeof(int) * 8;
  18. private const int longFactor = sizeof(long) * 8;
  19. #endregion
  20. #region Get operations
  21. public static bool GetBit(this IList<byte> list, int index)
  22. {
  23. if (list == null)
  24. throw new ArgumentNullException("list");
  25. if (index < 0 || index >= list.Count * byteFactor)
  26. throw new ArgumentOutOfRangeException("index");
  27. //
  28. return (list[index / byteFactor] & (1 << (index % byteFactor))) != 0;
  29. }
  30. public static bool GetBit(this IList<ushort> list, int index)
  31. {
  32. if (list == null)
  33. throw new ArgumentNullException("list");
  34. if (index < 0 || index >= list.Count * ushortFactor)
  35. throw new ArgumentOutOfRangeException("index");
  36. //
  37. return (list[index / ushortFactor] & (1 << (index % ushortFactor))) != 0;
  38. }
  39. public static bool GetBit(this IList<uint> list, int index)
  40. {
  41. if (list == null)
  42. throw new ArgumentNullException("list");
  43. if (index < 0 || index >= list.Count * uintFactor)
  44. throw new ArgumentOutOfRangeException("index");
  45. //
  46. return (list[index / uintFactor] & (1 << (index % uintFactor))) != 0;
  47. }
  48. public static bool GetBit(this IList<ulong> list, int index)
  49. {
  50. if (list == null)
  51. throw new ArgumentNullException("list");
  52. if (index < 0 || index >= list.Count * ulongFactor)
  53. throw new ArgumentOutOfRangeException("index");
  54. //
  55. return (list[index / ulongFactor] & (1UL << (index % ulongFactor))) != 0;
  56. }
  57. public static bool GetBit(this IList<sbyte> list, int index)
  58. {
  59. if (list == null)
  60. throw new ArgumentNullException("list");
  61. if (index < 0 || index >= list.Count * sbyteFactor)
  62. throw new ArgumentOutOfRangeException("index");
  63. //
  64. return (list[index / sbyteFactor] & (1 << (index % sbyteFactor))) != 0;
  65. }
  66. public static bool GetBit(this IList<short> list, int index)
  67. {
  68. if (list == null)
  69. throw new ArgumentNullException("list");
  70. if (index < 0 || index >= list.Count * shortFactor)
  71. throw new ArgumentOutOfRangeException("index");
  72. //
  73. return (list[index / shortFactor] & (1 << (index % shortFactor))) != 0;
  74. }
  75. public static bool GetBit(this IList<int> list, int index)
  76. {
  77. if (list == null)
  78. throw new ArgumentNullException("list");
  79. if (index < 0 || index >= list.Count * intFactor)
  80. throw new ArgumentOutOfRangeException("index");
  81. //
  82. return (list[index / intFactor] & (1 << (index % intFactor))) != 0;
  83. }
  84. public static bool GetBit(this IList<long> list, int index)
  85. {
  86. if (list == null)
  87. throw new ArgumentNullException("list");
  88. if (index < 0 || index >= list.Count * longFactor)
  89. throw new ArgumentOutOfRangeException("index");
  90. //
  91. return (list[index / longFactor] & (1L << (index % longFactor))) != 0;
  92. }
  93. public static IEnumerable<bool> GetBits(this IList<byte> list, int index, int count)
  94. {
  95. if (list == null)
  96. throw new ArgumentNullException("list");
  97. if (index < 0 || index > list.Count * byteFactor)
  98. throw new ArgumentOutOfRangeException("index");
  99. if (count < 0 || count > list.Count * byteFactor - index)
  100. throw new ArgumentOutOfRangeException("count");
  101. //
  102. int i = index / byteFactor;
  103. int j = index % byteFactor;
  104. while (count-- > 0)
  105. {
  106. yield return (list[i] & (byte)(1 << j)) != 0;
  107. if (++j >= byteFactor)
  108. {
  109. j = 0;
  110. i++;
  111. }
  112. }
  113. }
  114. public static IEnumerable<bool> GetBits(this IList<ushort> list, int index, int count)
  115. {
  116. if (list == null)
  117. throw new ArgumentNullException("list");
  118. if (index < 0 || index > list.Count * ushortFactor)
  119. throw new ArgumentOutOfRangeException("index");
  120. if (count < 0 || count > list.Count * ushortFactor - index)
  121. throw new ArgumentOutOfRangeException("count");
  122. //
  123. int i = index / ushortFactor;
  124. int j = index % ushortFactor;
  125. while (count-- > 0)
  126. {
  127. yield return (list[i] & (ushort)(1 << j)) != 0;
  128. if (++j >= ushortFactor)
  129. {
  130. j = 0;
  131. i++;
  132. }
  133. }
  134. }
  135. public static IEnumerable<bool> GetBits(this IList<uint> list, int index, int count)
  136. {
  137. if (list == null)
  138. throw new ArgumentNullException("list");
  139. if (index < 0 || index > list.Count * uintFactor)
  140. throw new ArgumentOutOfRangeException("index");
  141. if (count < 0 || count > list.Count * uintFactor - index)
  142. throw new ArgumentOutOfRangeException("count");
  143. //
  144. int i = index / uintFactor;
  145. int j = index % uintFactor;
  146. while (count-- > 0)
  147. {
  148. yield return (list[i] & (1U << j)) != 0;
  149. if (++j >= uintFactor)
  150. {
  151. j = 0;
  152. i++;
  153. }
  154. }
  155. }
  156. public static IEnumerable<bool> GetBits(this IList<ulong> list, int index, int count)
  157. {
  158. if (list == null)
  159. throw new ArgumentNullException("list");
  160. if (index < 0 || index > list.Count * ulongFactor)
  161. throw new ArgumentOutOfRangeException("index");
  162. if (count < 0 || count > list.Count * ulongFactor - index)
  163. throw new ArgumentOutOfRangeException("count");
  164. //
  165. int i = index / ulongFactor;
  166. int j = index % ulongFactor;
  167. while (count-- > 0)
  168. {
  169. yield return (list[i] & (1UL << j)) != 0;
  170. if (++j >= ulongFactor)
  171. {
  172. j = 0;
  173. i++;
  174. }
  175. }
  176. }
  177. public static IEnumerable<bool> GetBits(this IList<sbyte> list, int index, int count)
  178. {
  179. if (list == null)
  180. throw new ArgumentNullException("list");
  181. if (index < 0 || index > list.Count * sbyteFactor)
  182. throw new ArgumentOutOfRangeException("index");
  183. if (count < 0 || count > list.Count * sbyteFactor - index)
  184. throw new ArgumentOutOfRangeException("count");
  185. //
  186. int i = index / sbyteFactor;
  187. int j = index % sbyteFactor;
  188. while (count-- > 0)
  189. {
  190. yield return (list[i] & (sbyte)(1 << j)) != 0;
  191. if (++j >= sbyteFactor)
  192. {
  193. j = 0;
  194. i++;
  195. }
  196. }
  197. }
  198. public static IEnumerable<bool> GetBits(this IList<short> list, int index, int count)
  199. {
  200. if (list == null)
  201. throw new ArgumentNullException("list");
  202. if (index < 0 || index > list.Count * shortFactor)
  203. throw new ArgumentOutOfRangeException("index");
  204. if (count < 0 || count > list.Count * shortFactor - index)
  205. throw new ArgumentOutOfRangeException("count");
  206. //
  207. int i = index / shortFactor;
  208. int j = index % shortFactor;
  209. while (count-- > 0)
  210. {
  211. yield return (list[i] & (short)(1 << j)) != 0;
  212. if (++j >= shortFactor)
  213. {
  214. j = 0;
  215. i++;
  216. }
  217. }
  218. }
  219. public static IEnumerable<bool> GetBits(this IList<int> list, int index, int count)
  220. {
  221. if (list == null)
  222. throw new ArgumentNullException("list");
  223. if (index < 0 || index > list.Count * intFactor)
  224. throw new ArgumentOutOfRangeException("index");
  225. if (count < 0 || count > list.Count * intFactor - index)
  226. throw new ArgumentOutOfRangeException("count");
  227. //
  228. int i = index / intFactor;
  229. int j = index % intFactor;
  230. while (count-- > 0)
  231. {
  232. yield return (list[i] & (1 << j)) != 0;
  233. if (++j >= intFactor)
  234. {
  235. j = 0;
  236. i++;
  237. }
  238. }
  239. }
  240. public static IEnumerable<bool> GetBits(this IList<long> list, int index, int count)
  241. {
  242. if (list == null)
  243. throw new ArgumentNullException("list");
  244. if (index < 0 || index > list.Count * longFactor)
  245. throw new ArgumentOutOfRangeException("index");
  246. if (count < 0 || count > list.Count * longFactor - index)
  247. throw new ArgumentOutOfRangeException("count");
  248. //
  249. int i = index / longFactor;
  250. int j = index % longFactor;
  251. while (count-- > 0)
  252. {
  253. yield return (list[i] & (1L << j)) != 0;
  254. if (++j >= longFactor)
  255. {
  256. j = 0;
  257. i++;
  258. }
  259. }
  260. }
  261. #endregion
  262. #region Set operations
  263. public static void SetBit(this IList<byte> list, int index, bool value)
  264. {
  265. if (list == null)
  266. throw new ArgumentNullException("list");
  267. if (index < 0 || index >= list.Count * byteFactor)
  268. throw new ArgumentOutOfRangeException("index");
  269. //
  270. if (value)
  271. list[index / byteFactor] |= (byte)(1 << (index % byteFactor));
  272. else
  273. list[index / byteFactor] &= (byte)~(1 << (index % byteFactor));
  274. }
  275. public static void SetBit(this IList<ushort> list, int index, bool value)
  276. {
  277. if (list == null)
  278. throw new ArgumentNullException("list");
  279. if (index < 0 || index >= list.Count * ushortFactor)
  280. throw new ArgumentOutOfRangeException("index");
  281. //
  282. if (value)
  283. list[index / ushortFactor] |= (ushort)(1 << (index % ushortFactor));
  284. else
  285. list[index / ushortFactor] &= (ushort)~(1 << (index % ushortFactor));
  286. }
  287. public static void SetBit(this IList<uint> list, int index, bool value)
  288. {
  289. if (list == null)
  290. throw new ArgumentNullException("list");
  291. if (index < 0 || index >= list.Count * uintFactor)
  292. throw new ArgumentOutOfRangeException("index");
  293. //
  294. if (value)
  295. list[index / uintFactor] |= (1U << (index % uintFactor));
  296. else
  297. list[index / uintFactor] &= ~(1U << (index % uintFactor));
  298. }
  299. public static void SetBit(this IList<ulong> list, int index, bool value)
  300. {
  301. if (list == null)
  302. throw new ArgumentNullException("list");
  303. if (index < 0 || index >= list.Count * ulongFactor)
  304. throw new ArgumentOutOfRangeException("index");
  305. //
  306. if (value)
  307. list[index / ulongFactor] |= (1UL << (index % ulongFactor));
  308. else
  309. list[index / ulongFactor] &= ~(1UL << (index % ulongFactor));
  310. }
  311. public static void SetBit(this IList<sbyte> list, int index, bool value)
  312. {
  313. if (list == null)
  314. throw new ArgumentNullException("list");
  315. if (index < 0 || index >= list.Count * sbyteFactor)
  316. throw new ArgumentOutOfRangeException("index");
  317. //
  318. if (value)
  319. list[index / sbyteFactor] |= (sbyte)(1 << (index % sbyteFactor));
  320. else
  321. list[index / sbyteFactor] &= (sbyte)~(1 << (index % sbyteFactor));
  322. }
  323. public static void SetBit(this IList<short> list, int index, bool value)
  324. {
  325. if (list == null)
  326. throw new ArgumentNullException("list");
  327. if (index < 0 || index >= list.Count * shortFactor)
  328. throw new ArgumentOutOfRangeException("index");
  329. //
  330. if (value)
  331. list[index / shortFactor] |= (short)(1 << (index % shortFactor));
  332. else
  333. list[index / shortFactor] &= (short)~(1 << (index % shortFactor));
  334. }
  335. public static void SetBit(this IList<int> list, int index, bool value)
  336. {
  337. if (list == null)
  338. throw new ArgumentNullException("list");
  339. if (index < 0 || index >= list.Count * intFactor)
  340. throw new ArgumentOutOfRangeException("index");
  341. //
  342. if (value)
  343. list[index / intFactor] |= (1 << (index % intFactor));
  344. else
  345. list[index / intFactor] &= ~(1 << (index % intFactor));
  346. }
  347. public static void SetBit(this IList<long> list, int index, bool value)
  348. {
  349. if (list == null)
  350. throw new ArgumentNullException("list");
  351. if (index < 0 || index >= list.Count * longFactor)
  352. throw new ArgumentOutOfRangeException("index");
  353. //
  354. if (value)
  355. list[index / longFactor] |= (1L << (index % longFactor));
  356. else
  357. list[index / longFactor] &= ~(1L << (index % longFactor));
  358. }
  359. public static void SetBits(this IList<byte> list, int index, int count, bool value)
  360. {
  361. if (list == null)
  362. throw new ArgumentNullException("list");
  363. if (index < 0 || index > list.Count * byteFactor)
  364. throw new ArgumentOutOfRangeException("index");
  365. if (count < 0 || count > list.Count * byteFactor - index)
  366. throw new ArgumentOutOfRangeException("count");
  367. //
  368. int i = index / byteFactor;
  369. int j = index % byteFactor;
  370. while (count-- > 0)
  371. {
  372. if (value)
  373. list[i] |= (byte)(1 << j);
  374. else
  375. list[i] &= (byte)~(1 << j);
  376. if (++j >= byteFactor)
  377. {
  378. j = 0;
  379. i++;
  380. }
  381. }
  382. }
  383. public static void SetBits(this IList<ushort> list, int index, int count, bool value)
  384. {
  385. if (list == null)
  386. throw new ArgumentNullException("list");
  387. if (index < 0 || index > list.Count * ushortFactor)
  388. throw new ArgumentOutOfRangeException("index");
  389. if (count < 0 || count > list.Count * ushortFactor - index)
  390. throw new ArgumentOutOfRangeException("count");
  391. //
  392. int i = index / ushortFactor;
  393. int j = index % ushortFactor;
  394. while (count-- > 0)
  395. {
  396. if (value)
  397. list[i] |= (ushort)(1 << j);
  398. else
  399. list[i] &= (ushort)~(1 << j);
  400. if (++j >= ushortFactor)
  401. {
  402. j = 0;
  403. i++;
  404. }
  405. }
  406. }
  407. public static void SetBits(this IList<uint> list, int index, int count, bool value)
  408. {
  409. if (list == null)
  410. throw new ArgumentNullException("list");
  411. if (index < 0 || index > list.Count * uintFactor)
  412. throw new ArgumentOutOfRangeException("index");
  413. if (count < 0 || count > list.Count * uintFactor - index)
  414. throw new ArgumentOutOfRangeException("count");
  415. //
  416. int i = index / uintFactor;
  417. int j = index % uintFactor;
  418. while (count-- > 0)
  419. {
  420. if (value)
  421. list[i] |= (1U << j);
  422. else
  423. list[i] &= ~(1U << j);
  424. if (++j >= uintFactor)
  425. {
  426. j = 0;
  427. i++;
  428. }
  429. }
  430. }
  431. public static void SetBits(this IList<ulong> list, int index, int count, bool value)
  432. {
  433. if (list == null)
  434. throw new ArgumentNullException("list");
  435. if (index < 0 || index > list.Count * ulongFactor)
  436. throw new ArgumentOutOfRangeException("index");
  437. if (count < 0 || count > list.Count * ulongFactor - index)
  438. throw new ArgumentOutOfRangeException("count");
  439. //
  440. int i = index / ulongFactor;
  441. int j = index % ulongFactor;
  442. while (count-- > 0)
  443. {
  444. if (value)
  445. list[i] |= (1UL << j);
  446. else
  447. list[i] &= ~(1UL << j);
  448. if (++j >= ulongFactor)
  449. {
  450. j = 0;
  451. i++;
  452. }
  453. }
  454. }
  455. public static void SetBits(this IList<sbyte> list, int index, int count, bool value)
  456. {
  457. if (list == null)
  458. throw new ArgumentNullException("list");
  459. if (index < 0 || index > list.Count * sbyteFactor)
  460. throw new ArgumentOutOfRangeException("index");
  461. if (count < 0 || count > list.Count * sbyteFactor - index)
  462. throw new ArgumentOutOfRangeException("count");
  463. //
  464. int i = index / sbyteFactor;
  465. int j = index % sbyteFactor;
  466. while (count-- > 0)
  467. {
  468. if (value)
  469. list[i] |= (sbyte)(1 << j);
  470. else
  471. list[i] &= (sbyte)~(1 << j);
  472. if (++j >= sbyteFactor)
  473. {
  474. j = 0;
  475. i++;
  476. }
  477. }
  478. }
  479. public static void SetBits(this IList<short> list, int index, int count, bool value)
  480. {
  481. if (list == null)
  482. throw new ArgumentNullException("list");
  483. if (index < 0 || index > list.Count * shortFactor)
  484. throw new ArgumentOutOfRangeException("index");
  485. if (count < 0 || count > list.Count * shortFactor - index)
  486. throw new ArgumentOutOfRangeException("count");
  487. //
  488. int i = index / shortFactor;
  489. int j = index % shortFactor;
  490. while (count-- > 0)
  491. {
  492. if (value)
  493. list[i] |= (short)(1 << j);
  494. else
  495. list[i] &= (short)~(1 << j);
  496. if (++j >= shortFactor)
  497. {
  498. j = 0;
  499. i++;
  500. }
  501. }
  502. }
  503. public static void SetBits(this IList<int> list, int index, int count, bool value)
  504. {
  505. if (list == null)
  506. throw new ArgumentNullException("list");
  507. if (index < 0 || index > list.Count * intFactor)
  508. throw new ArgumentOutOfRangeException("index");
  509. if (count < 0 || count > list.Count * intFactor - index)
  510. throw new ArgumentOutOfRangeException("count");
  511. //
  512. int i = index / intFactor;
  513. int j = index % intFactor;
  514. while (count-- > 0)
  515. {
  516. if (value)
  517. list[i] |= (1 << j);
  518. else
  519. list[i] &= ~(1 << j);
  520. if (++j >= intFactor)
  521. {
  522. j = 0;
  523. i++;
  524. }
  525. }
  526. }
  527. public static void SetBits(this IList<long> list, int index, int count, bool value)
  528. {
  529. if (list == null)
  530. throw new ArgumentNullException("list");
  531. if (index < 0 || index > list.Count * longFactor)
  532. throw new ArgumentOutOfRangeException("index");
  533. if (count < 0 || count > list.Count * longFactor - index)
  534. throw new ArgumentOutOfRangeException("count");
  535. //
  536. int i = index / longFactor;
  537. int j = index % longFactor;
  538. while (count-- > 0)
  539. {
  540. if (value)
  541. list[i] |= (1L << j);
  542. else
  543. list[i] &= ~(1L << j);
  544. if (++j >= longFactor)
  545. {
  546. j = 0;
  547. i++;
  548. }
  549. }
  550. }
  551. public static void SetBits(this IList<byte> list, int index, IEnumerable<bool> coll)
  552. {
  553. if (list == null)
  554. throw new ArgumentNullException("list");
  555. if (coll == null)
  556. throw new ArgumentNullException("coll");
  557. if (index < 0 || index >= list.Count * byteFactor)
  558. throw new ArgumentOutOfRangeException("index");
  559. //
  560. using (IEnumerator<bool> e = coll.GetEnumerator())
  561. {
  562. int i = index / byteFactor;
  563. int j = index % byteFactor;
  564. while (i < list.Count && e.MoveNext())
  565. {
  566. if (e.Current)
  567. list[i] |= (byte)(1 << j);
  568. else
  569. list[i] &= (byte)~(1 << j);
  570. if (++j >= byteFactor)
  571. {
  572. j = 0;
  573. i++;
  574. }
  575. }
  576. }
  577. }
  578. public static void SetBits(this IList<ushort> list, int index, IEnumerable<bool> coll)
  579. {
  580. if (list == null)
  581. throw new ArgumentNullException("list");
  582. if (coll == null)
  583. throw new ArgumentNullException("coll");
  584. if (index < 0 || index >= list.Count * ushortFactor)
  585. throw new ArgumentOutOfRangeException("index");
  586. //
  587. using (IEnumerator<bool> e = coll.GetEnumerator())
  588. {
  589. int i = index / ushortFactor;
  590. int j = index % ushortFactor;
  591. while (i < list.Count && e.MoveNext())
  592. {
  593. if (e.Current)
  594. list[i] |= (ushort)(1 << j);
  595. else
  596. list[i] &= (ushort)~(1 << j);
  597. if (++j >= ushortFactor)
  598. {
  599. j = 0;
  600. i++;
  601. }
  602. }
  603. }
  604. }
  605. public static void SetBits(this IList<uint> list, int index, IEnumerable<bool> coll)
  606. {
  607. if (list == null)
  608. throw new ArgumentNullException("list");
  609. if (coll == null)
  610. throw new ArgumentNullException("coll");
  611. if (index < 0 || index >= list.Count * uintFactor)
  612. throw new ArgumentOutOfRangeException("index");
  613. //
  614. using (IEnumerator<bool> e = coll.GetEnumerator())
  615. {
  616. int i = index / uintFactor;
  617. int j = index % uintFactor;
  618. while (i < list.Count && e.MoveNext())
  619. {
  620. if (e.Current)
  621. list[i] |= (1U << j);
  622. else
  623. list[i] &= ~(1U << j);
  624. if (++j >= uintFactor)
  625. {
  626. j = 0;
  627. i++;
  628. }
  629. }
  630. }
  631. }
  632. public static void SetBits(this IList<ulong> list, int index, IEnumerable<bool> coll)
  633. {
  634. if (list == null)
  635. throw new ArgumentNullException("list");
  636. if (coll == null)
  637. throw new ArgumentNullException("coll");
  638. if (index < 0 || index >= list.Count * ulongFactor)
  639. throw new ArgumentOutOfRangeException("index");
  640. //
  641. using (IEnumerator<bool> e = coll.GetEnumerator())
  642. {
  643. int i = index / ulongFactor;
  644. int j = index % ulongFactor;
  645. while (i < list.Count && e.MoveNext())
  646. {
  647. if (e.Current)
  648. list[i] |= (1UL << j);
  649. else
  650. list[i] &= ~(1UL << j);
  651. if (++j >= ulongFactor)
  652. {
  653. j = 0;
  654. i++;
  655. }
  656. }
  657. }
  658. }
  659. public static void SetBits(this IList<sbyte> list, int index, IEnumerable<bool> coll)
  660. {
  661. if (list == null)
  662. throw new ArgumentNullException("list");
  663. if (coll == null)
  664. throw new ArgumentNullException("coll");
  665. if (index < 0 || index >= list.Count * sbyteFactor)
  666. throw new ArgumentOutOfRangeException("index");
  667. //
  668. using (IEnumerator<bool> e = coll.GetEnumerator())
  669. {
  670. int i = index / sbyteFactor;
  671. int j = index % sbyteFactor;
  672. while (i < list.Count && e.MoveNext())
  673. {
  674. if (e.Current)
  675. list[i] |= (sbyte)(1 << j);
  676. else
  677. list[i] &= (sbyte)~(1 << j);
  678. if (++j >= sbyteFactor)
  679. {
  680. j = 0;
  681. i++;
  682. }
  683. }
  684. }
  685. }
  686. public static void SetBits(this IList<short> list, int index, IEnumerable<bool> coll)
  687. {
  688. if (list == null)
  689. throw new ArgumentNullException("list");
  690. if (coll == null)
  691. throw new ArgumentNullException("coll");
  692. if (index < 0 || index >= list.Count * shortFactor)
  693. throw new ArgumentOutOfRangeException("index");
  694. //
  695. using (IEnumerator<bool> e = coll.GetEnumerator())
  696. {
  697. int i = index / shortFactor;
  698. int j = index % shortFactor;
  699. while (i < list.Count && e.MoveNext())
  700. {
  701. if (e.Current)
  702. list[i] |= (short)(1 << j);
  703. else
  704. list[i] &= (short)~(1 << j);
  705. if (++j >= shortFactor)
  706. {
  707. j = 0;
  708. i++;
  709. }
  710. }
  711. }
  712. }
  713. public static void SetBits(this IList<int> list, int index, IEnumerable<bool> coll)
  714. {
  715. if (list == null)
  716. throw new ArgumentNullException("list");
  717. if (coll == null)
  718. throw new ArgumentNullException("coll");
  719. if (index < 0 || index >= list.Count * intFactor)
  720. throw new ArgumentOutOfRangeException("index");
  721. //
  722. using (IEnumerator<bool> e = coll.GetEnumerator())
  723. {
  724. int i = index / intFactor;
  725. int j = index % intFactor;
  726. while (i < list.Count && e.MoveNext())
  727. {
  728. if (e.Current)
  729. list[i] |= (1 << j);
  730. else
  731. list[i] &= ~(1 << j);
  732. if (++j >= intFactor)
  733. {
  734. j = 0;
  735. i++;
  736. }
  737. }
  738. }
  739. }
  740. public static void SetBits(this IList<long> list, int index, IEnumerable<bool> coll)
  741. {
  742. if (list == null)
  743. throw new ArgumentNullException("list");
  744. if (coll == null)
  745. throw new ArgumentNullException("coll");
  746. if (index < 0 || index >= list.Count * longFactor)
  747. throw new ArgumentOutOfRangeException("index");
  748. //
  749. using (IEnumerator<bool> e = coll.GetEnumerator())
  750. {
  751. int i = index / longFactor;
  752. int j = index % longFactor;
  753. while (i < list.Count && e.MoveNext())
  754. {
  755. if (e.Current)
  756. list[i] |= (1L << j);
  757. else
  758. list[i] &= ~(1L << j);
  759. if (++j >= longFactor)
  760. {
  761. j = 0;
  762. i++;
  763. }
  764. }
  765. }
  766. }
  767. #endregion
  768. #region Retrieval operations
  769. public static int IndexOf(this IList<byte> list, int index, int count, bool value)
  770. {
  771. if (list == null)
  772. throw new ArgumentNullException("list");
  773. if (index < 0 || index > list.Count * byteFactor)
  774. throw new ArgumentOutOfRangeException("index");
  775. if (count < 0 || count > list.Count * byteFactor - index)
  776. throw new ArgumentOutOfRangeException("count");
  777. //
  778. int i = index / byteFactor;
  779. int j = index % byteFactor;
  780. while (count-- > 0)
  781. {
  782. if (((list[i] & (byte)(1 << j)) != 0) == value)
  783. return i * byteFactor + j;
  784. if (++j >= byteFactor)
  785. {
  786. j = 0;
  787. i++;
  788. }
  789. }
  790. return -1;
  791. }
  792. public static int IndexOf(this IList<ushort> list, int index, int count, bool value)
  793. {
  794. if (list == null)
  795. throw new ArgumentNullException("list");
  796. if (index < 0 || index > list.Count * ushortFactor)
  797. throw new ArgumentOutOfRangeException("index");
  798. if (count < 0 || count > list.Count * ushortFactor - index)
  799. throw new ArgumentOutOfRangeException("count");
  800. //
  801. int i = index / ushortFactor;
  802. int j = index % ushortFactor;
  803. while (count-- > 0)
  804. {
  805. if (((list[i] & (ushort)(1 << j)) != 0) == value)
  806. return i * ushortFactor + j;
  807. if (++j >= ushortFactor)
  808. {
  809. j = 0;
  810. i++;
  811. }
  812. }
  813. return -1;
  814. }
  815. public static int IndexOf(this IList<uint> list, int index, int count, bool value)
  816. {
  817. if (list == null)
  818. throw new ArgumentNullException("list");
  819. if (index < 0 || index > list.Count * uintFactor)
  820. throw new ArgumentOutOfRangeException("index");
  821. if (count < 0 || count > list.Count * uintFactor - index)
  822. throw new ArgumentOutOfRangeException("count");
  823. //
  824. int i = index / uintFactor;
  825. int j = index % uintFactor;
  826. while (count-- > 0)
  827. {
  828. if (((list[i] & (1U << j)) != 0) == value)
  829. return i * uintFactor + j;
  830. if (++j >= uintFactor)
  831. {
  832. j = 0;
  833. i++;
  834. }
  835. }
  836. return -1;
  837. }
  838. public static int IndexOf(this IList<ulong> list, int index, int count, bool value)
  839. {
  840. if (list == null)
  841. throw new ArgumentNullException("list");
  842. if (index < 0 || index > list.Count * ulongFactor)
  843. throw new ArgumentOutOfRangeException("index");
  844. if (count < 0 || count > list.Count * ulongFactor - index)
  845. throw new ArgumentOutOfRangeException("count");
  846. //
  847. int i = index / ulongFactor;
  848. int j = index % ulongFactor;
  849. while (count-- > 0)
  850. {
  851. if (((list[i] & (1UL << j)) != 0) == value)
  852. return i * ulongFactor + j;
  853. if (++j >= ulongFactor)
  854. {
  855. j = 0;
  856. i++;
  857. }
  858. }
  859. return -1;
  860. }
  861. public static int IndexOf(this IList<sbyte> list, int index, int count, bool value)
  862. {
  863. if (list == null)
  864. throw new ArgumentNullException("list");
  865. if (index < 0 || index > list.Count * sbyteFactor)
  866. throw new ArgumentOutOfRangeException("index");
  867. if (count < 0 || count > list.Count * sbyteFactor - index)
  868. throw new ArgumentOutOfRangeException("count");
  869. //
  870. int i = index / sbyteFactor;
  871. int j = index % sbyteFactor;
  872. while (count-- > 0)
  873. {
  874. if (((list[i] & (sbyte)(1 << j)) != 0) == value)
  875. return i * sbyteFactor + j;
  876. if (++j >= sbyteFactor)
  877. {
  878. j = 0;
  879. i++;
  880. }
  881. }
  882. return -1;
  883. }
  884. public static int IndexOf(this IList<short> list, int index, int count, bool value)
  885. {
  886. if (list == null)
  887. throw new ArgumentNullException("list");
  888. if (index < 0 || index > list.Count * shortFactor)
  889. throw new ArgumentOutOfRangeException("index");
  890. if (count < 0 || count > list.Count * shortFactor - index)
  891. throw new ArgumentOutOfRangeException("count");
  892. //
  893. int i = index / shortFactor;
  894. int j = index % shortFactor;
  895. while (count-- > 0)
  896. {
  897. if (((list[i] & (short)(1 << j)) != 0) == value)
  898. return i * shortFactor + j;
  899. if (++j >= shortFactor)
  900. {
  901. j = 0;
  902. i++;
  903. }
  904. }
  905. return -1;
  906. }
  907. public static int IndexOf(this IList<int> list, int index, int count, bool value)
  908. {
  909. if (list == null)
  910. throw new ArgumentNullException("list");
  911. if (index < 0 || index > list.Count * intFactor)
  912. throw new ArgumentOutOfRangeException("index");
  913. if (count < 0 || count > list.Count * intFactor - index)
  914. throw new ArgumentOutOfRangeException("count");
  915. //
  916. int i = index / intFactor;
  917. int j = index % intFactor;
  918. while (count-- > 0)
  919. {
  920. if (((list[i] & (1 << j)) != 0) == value)
  921. return i * intFactor + j;
  922. if (++j >= intFactor)
  923. {
  924. j = 0;
  925. i++;
  926. }
  927. }
  928. return -1;
  929. }
  930. public static int IndexOf(this IList<long> list, int index, int count, bool value)
  931. {
  932. if (list == null)
  933. throw new ArgumentNullException("list");
  934. if (index < 0 || index > list.Count * longFactor)
  935. throw new ArgumentOutOfRangeException("index");
  936. if (count < 0 || count > list.Count * longFactor - index)
  937. throw new ArgumentOutOfRangeException("count");
  938. //
  939. int i = index / longFactor;
  940. int j = index % longFactor;
  941. while (count-- > 0)
  942. {
  943. if (((list[i] & (1L << j)) != 0) == value)
  944. return i * longFactor + j;
  945. if (++j >= longFactor)
  946. {
  947. j = 0;
  948. i++;
  949. }
  950. }
  951. return -1;
  952. }
  953. public static int LastIndexOf(this IList<byte> list, int index, int count, bool value)
  954. {
  955. if (list == null)
  956. throw new ArgumentNullException("list");
  957. if (index < -1 || index >= list.Count * byteFactor + (list.Count == 0 ? 1 : 0))
  958. throw new ArgumentOutOfRangeException("index");
  959. if (count < 0 || count > index + 1)
  960. throw new ArgumentOutOfRangeException("count");
  961. //
  962. int total = list.Count * byteFactor;
  963. int i = (total - 1) / byteFactor;
  964. int j = (total - 1) % byteFactor;
  965. while (count-- > 0)
  966. {
  967. if (((list[i] & (byte)(1 << j)) != 0) == value)
  968. return i * byteFactor + j;
  969. if (--j < 0)
  970. {
  971. j = byteFactor - 1;
  972. i--;
  973. }
  974. }
  975. return -1;
  976. }
  977. public static int LastIndexOf(this IList<ushort> list, int index, int count, bool value)
  978. {
  979. if (list == null)
  980. throw new ArgumentNullException("list");
  981. if (index < -1 || index >= list.Count * ushortFactor + (list.Count == 0 ? 1 : 0))
  982. throw new ArgumentOutOfRangeException("index");
  983. if (count < 0 || count > index + 1)
  984. throw new ArgumentOutOfRangeException("count");
  985. //
  986. int total = list.Count * ushortFactor;
  987. int i = (total - 1) / ushortFactor;
  988. int j = (total - 1) % ushortFactor;
  989. while (count-- > 0)
  990. {
  991. if (((list[i] & (ushort)(1 << j)) != 0) == value)
  992. return i * ushortFactor + j;
  993. if (--j < 0)
  994. {
  995. j = ushortFactor - 1;
  996. i--;
  997. }
  998. }
  999. return -1;
  1000. }
  1001. public static int LastIndexOf(this IList<uint> list, int index, int count, bool value)
  1002. {
  1003. if (list == null)
  1004. throw new ArgumentNullException("list");
  1005. if (index < -1 || index >= list.Count * uintFactor + (list.Count == 0 ? 1 : 0))
  1006. throw new ArgumentOutOfRangeException("index");
  1007. if (count < 0 || count > index + 1)
  1008. throw new ArgumentOutOfRangeException("count");
  1009. //
  1010. int total = list.Count * uintFactor;
  1011. int i = (total - 1) / uintFactor;
  1012. int j = (total - 1) % uintFactor;
  1013. while (count-- > 0)
  1014. {
  1015. if (((list[i] & (1U << j)) != 0) == value)
  1016. return i * uintFactor + j;
  1017. if (--j < 0)
  1018. {
  1019. j = uintFactor - 1;
  1020. i--;
  1021. }
  1022. }
  1023. return -1;
  1024. }
  1025. public static int LastIndexOf(this IList<ulong> list, int index, int count, bool value)
  1026. {
  1027. if (list == null)
  1028. throw new ArgumentNullException("list");
  1029. if (index < -1 || index >= list.Count * ulongFactor + (list.Count == 0 ? 1 : 0))
  1030. throw new ArgumentOutOfRangeException("index");
  1031. if (count < 0 || count > index + 1)
  1032. throw new ArgumentOutOfRangeException("count");
  1033. //
  1034. int total = list.Count * ulongFactor;
  1035. int i = (total - 1) / ulongFactor;
  1036. int j = (total - 1) % ulongFactor;
  1037. while (count-- > 0)
  1038. {
  1039. if (((list[i] & (1UL << j)) != 0) == value)
  1040. return i * ulongFactor + j;
  1041. if (--j < 0)
  1042. {
  1043. j = ulongFactor - 1;
  1044. i--;
  1045. }
  1046. }
  1047. return -1;
  1048. }
  1049. public static int LastIndexOf(this IList<sbyte> list, int index, int count, bool value)
  1050. {
  1051. if (list == null)
  1052. throw new ArgumentNullException("list");
  1053. if (index < -1 || index >= list.Count * sbyteFactor + (list.Count == 0 ? 1 : 0))
  1054. throw new ArgumentOutOfRangeException("index");
  1055. if (count < 0 || count > index + 1)
  1056. throw new ArgumentOutOfRangeException("count");
  1057. //
  1058. int total = list.Count * sbyteFactor;
  1059. int i = (total - 1) / sbyteFactor;
  1060. int j = (total - 1) % sbyteFactor;
  1061. while (count-- > 0)
  1062. {
  1063. if (((list[i] & (sbyte)(1 << j)) != 0) == value)
  1064. return i * sbyteFactor + j;
  1065. if (--j < 0)
  1066. {
  1067. j = sbyteFactor - 1;
  1068. i--;
  1069. }
  1070. }
  1071. return -1;
  1072. }
  1073. public static int LastIndexOf(this IList<short> list, int index, int count, bool value)
  1074. {
  1075. if (list == null)
  1076. throw new ArgumentNullException("list");
  1077. if (index < -1 || index >= list.Count * shortFactor + (list.Count == 0 ? 1 : 0))
  1078. throw new ArgumentOutOfRangeException("index");
  1079. if (count < 0 || count > index + 1)
  1080. throw new ArgumentOutOfRangeException("count");
  1081. //
  1082. int total = list.Count * shortFactor;
  1083. int i = (total - 1) / shortFactor;
  1084. int j = (total - 1) % shortFactor;
  1085. while (count-- > 0)
  1086. {
  1087. if (((list[i] & (short)(1 << j)) != 0) == value)
  1088. return i * shortFactor + j;
  1089. if (--j < 0)
  1090. {
  1091. j = shortFactor - 1;
  1092. i--;
  1093. }
  1094. }
  1095. return -1;
  1096. }
  1097. public static int LastIndexOf(this IList<int> list, int index, int count, bool value)
  1098. {
  1099. if (list == null)
  1100. throw new ArgumentNullException("list");
  1101. if (index < -1 || index >= list.Count * intFactor + (list.Count == 0 ? 1 : 0))
  1102. throw new ArgumentOutOfRangeException("index");
  1103. if (count < 0 || count > index + 1)
  1104. throw new ArgumentOutOfRangeException("count");
  1105. //
  1106. int total = list.Count * intFactor;
  1107. int i = (total - 1) / intFactor;
  1108. int j = (total - 1) % intFactor;
  1109. while (count-- > 0)
  1110. {
  1111. if (((list[i] & (1 << j)) != 0) == value)
  1112. return i * intFactor + j;
  1113. if (--j < 0)
  1114. {
  1115. j = intFactor - 1;
  1116. i--;
  1117. }
  1118. }
  1119. return -1;
  1120. }
  1121. public static int LastIndexOf(this IList<long> list, int index, int count, bool value)
  1122. {
  1123. if (list == null)
  1124. throw new ArgumentNullException("list");
  1125. if (index < -1 || index >= list.Count * longFactor + (list.Count == 0 ? 1 : 0))
  1126. throw new ArgumentOutOfRangeException("index");
  1127. if (count < 0 || count > index + 1)
  1128. throw new ArgumentOutOfRangeException("count");
  1129. //
  1130. int total = list.Count * longFactor;
  1131. int i = (total - 1) / longFactor;
  1132. int j = (total - 1) % longFactor;
  1133. while (count-- > 0)
  1134. {
  1135. if (((list[i] & (1L << j)) != 0) == value)
  1136. return i * longFactor + j;
  1137. if (--j < 0)
  1138. {
  1139. j = longFactor - 1;
  1140. i--;
  1141. }
  1142. }
  1143. return -1;
  1144. }
  1145. #endregion
  1146. #region Copy operation
  1147. public static void CopyBitsTo(this IList<byte> sourceList, int sourceIndex, IList<byte> targetList, int targetIndex, int count)
  1148. {
  1149. if (sourceList == null)
  1150. throw new ArgumentNullException("sourceList");
  1151. if (targetList == null)
  1152. throw new ArgumentNullException("targetList");
  1153. int sourceTotal = sourceList.Count * byteFactor;
  1154. int targetTotal = targetList.Count * byteFactor;
  1155. if (sourceIndex < 0 || sourceIndex > sourceTotal)
  1156. throw new ArgumentOutOfRangeException("sourceIndex");
  1157. if (targetIndex < 0 || targetIndex > targetTotal)
  1158. throw new ArgumentOutOfRangeException("targetIndex");
  1159. if (count < 0 || count > sourceTotal - sourceIndex || count > targetTotal - targetIndex)
  1160. throw new ArgumentOutOfRangeException("count");
  1161. //
  1162. bool reverse = sourceList == targetList && targetIndex > sourceIndex;
  1163. int sw = ((reverse) ? sourceIndex + count - 1 : sourceIndex) / byteFactor;
  1164. int sb = ((reverse) ? sourceIndex + count - 1 : sourceIndex) % byteFactor;
  1165. int tw = ((reverse) ? targetIndex + count - 1 : targetIndex) / byteFactor;
  1166. int tb = ((reverse) ? targetIndex + count - 1 : targetIndex) % byteFactor;
  1167. while (count-- > 0)
  1168. {
  1169. if ((sourceList[sw] & (byte)(1 << sb)) != 0)
  1170. targetList[tw] |= (byte)(1 << tb);
  1171. else
  1172. targetList[tw] &= (byte)~(1 << tb);
  1173. if (reverse)
  1174. {
  1175. if (--sb < 0)
  1176. {
  1177. sb = byteFactor - 1;
  1178. sw--;
  1179. }
  1180. if (--tb < 0)
  1181. {
  1182. tb = byteFactor - 1;
  1183. tw-- ;
  1184. }
  1185. }
  1186. else
  1187. {
  1188. if (++sb >= byteFactor)
  1189. {
  1190. sb = 0;
  1191. sw++;
  1192. }
  1193. if (++tb >= byteFactor)
  1194. {
  1195. tb = 0;
  1196. tw++;
  1197. }
  1198. }
  1199. }
  1200. }
  1201. public static void CopyBitsTo(this IList<ushort> sourceList, int sourceIndex, IList<ushort> targetList, int targetIndex, int count)
  1202. {
  1203. if (sourceList == null)
  1204. throw new ArgumentNullException("sourceList");
  1205. if (targetList == null)
  1206. throw new ArgumentNullException("targetList");
  1207. int sourceTotal = sourceList.Count * ushortFactor;
  1208. int targetTotal = targetList.Count * ushortFactor;
  1209. if (sourceIndex < 0 || sourceIndex > sourceTotal)
  1210. throw new ArgumentOutOfRangeException("sourceIndex");
  1211. if (targetIndex < 0 || targetIndex > targetTotal)
  1212. throw new ArgumentOutOfRangeException("targetIndex");
  1213. if (count < 0 || count > sourceTotal - sourceIndex || count > targetTotal - targetIndex)
  1214. throw new ArgumentOutOfRangeException("count");
  1215. //
  1216. bool reverse = sourceList == targetList && targetIndex > sourceIndex;
  1217. int sw = ((reverse) ? sourceIndex + count - 1 : sourceIndex) / ushortFactor;
  1218. int sb = ((reverse) ? sourceIndex + count - 1 : sourceIndex) % ushortFactor;
  1219. int tw = ((reverse) ? targetIndex + count - 1 : targetIndex) / ushortFactor;
  1220. int tb = ((reverse) ? targetIndex + count - 1 : targetIndex) % ushortFactor;
  1221. while (count-- > 0)
  1222. {
  1223. if ((sourceList[sw] & (ushort)(1 << sb)) != 0)
  1224. targetList[tw] |= (ushort)(1 << tb);
  1225. else
  1226. targetList[tw] &= (ushort)~(1 << tb);
  1227. if (reverse)
  1228. {
  1229. if (--sb < 0)
  1230. {
  1231. sb = ushortFactor - 1;
  1232. sw--;
  1233. }
  1234. if (--tb < 0)
  1235. {
  1236. tb = ushortFactor - 1;
  1237. tw--;
  1238. }
  1239. }
  1240. else
  1241. {
  1242. if (++sb >= ushortFactor)
  1243. {
  1244. sb = 0;
  1245. sw++;
  1246. }
  1247. if (++tb >= ushortFactor)
  1248. {
  1249. tb = 0;
  1250. tw++;
  1251. }
  1252. }
  1253. }
  1254. }
  1255. public static void CopyBitsTo(this IList<uint> sourceList, int sourceIndex, IList<uint> targetList, int targetIndex, int count)
  1256. {
  1257. if (sourceList == null)
  1258. throw new ArgumentNullException("sourceList");
  1259. if (targetList == null)
  1260. throw new ArgumentNullException("targetList");
  1261. int sourceTotal = sourceList.Count * uintFactor;
  1262. int targetTotal = targetList.Count * uintFactor;
  1263. if (sourceIndex < 0 || sourceIndex > sourceTotal)
  1264. throw new ArgumentOutOfRangeException("sourceIndex");
  1265. if (targetIndex < 0 || targetIndex > targetTotal)
  1266. throw new ArgumentOutOfRangeException("targetIndex");
  1267. if (count < 0 || count > sourceTotal - sourceIndex || count > targetTotal - targetIndex)
  1268. throw new ArgumentOutOfRangeException("count");
  1269. //
  1270. bool reverse = sourceList == targetList && targetIndex > sourceIndex;
  1271. int sw = ((reverse) ? sourceIndex + count - 1 : sourceIndex) / uintFactor;
  1272. int sb = ((reverse) ? sourceIndex + count - 1 : sourceIndex) % uintFactor;
  1273. int tw = ((reverse) ? targetIndex + count - 1 : targetIndex) / uintFactor;
  1274. int tb = ((reverse) ? targetIndex + count - 1 : targetIndex) % uintFactor;
  1275. while (count-- > 0)
  1276. {
  1277. if ((sourceList[sw] & (1U << sb)) != 0)
  1278. targetList[tw] |= (1U << tb);
  1279. else
  1280. targetList[tw] &= ~(1U << tb);
  1281. if (reverse)
  1282. {
  1283. if (--sb < 0)
  1284. {
  1285. sb = uintFactor - 1;
  1286. sw--;
  1287. }
  1288. if (--tb < 0)
  1289. {
  1290. tb = uintFactor - 1;
  1291. tw--;
  1292. }
  1293. }
  1294. else
  1295. {
  1296. if (++sb >= uintFactor)
  1297. {
  1298. sb = 0;
  1299. sw++;
  1300. }
  1301. if (++tb >= uintFactor)
  1302. {
  1303. tb = 0;
  1304. tw++;
  1305. }
  1306. }
  1307. }
  1308. }
  1309. public static void CopyBitsTo(this IList<ulong> sourceList, int sourceIndex, IList<ulong> targetList, int targetIndex, int count)
  1310. {
  1311. if (sourceList == null)
  1312. throw new ArgumentNullException("sourceList");
  1313. if (targetList == null)
  1314. throw new ArgumentNullException("targetList");
  1315. int sourceTotal = sourceList.Count * ulongFactor;
  1316. int targetTotal = targetList.Count * ulongFactor;
  1317. if (sourceIndex < 0 || sourceIndex > sourceTotal)
  1318. throw new ArgumentOutOfRangeException("sourceIndex");
  1319. if (targetIndex < 0 || targetIndex > targetTotal)
  1320. throw new ArgumentOutOfRangeException("targetIndex");
  1321. if (count < 0 || count > sourceTotal - sourceIndex || count > targetTotal - targetIndex)
  1322. throw new ArgumentOutOfRangeException("count");
  1323. //
  1324. bool reverse = sourceList == targetList && targetIndex > sourceIndex;
  1325. int sw = ((reverse) ? sourceIndex + count - 1 : sourceIndex) / ulongFactor;
  1326. int sb = ((reverse) ? sourceIndex + count - 1 : sourceIndex) % ulongFactor;
  1327. int tw = ((reverse) ? targetIndex + count - 1 : targetIndex) / ulongFactor;
  1328. int tb = ((reverse) ? targetIndex + count - 1 : targetIndex) % ulongFactor;
  1329. while (count-- > 0)
  1330. {
  1331. if ((sourceList[sw] & (1UL << sb)) != 0)
  1332. targetList[tw] |= (1UL << tb);
  1333. else
  1334. targetList[tw] &= ~(1UL << tb);
  1335. if (reverse)
  1336. {
  1337. if (--sb < 0)
  1338. {
  1339. sb = ulongFactor - 1;
  1340. sw--;
  1341. }
  1342. if (--tb < 0)
  1343. {
  1344. tb = ulongFactor - 1;
  1345. tw--;
  1346. }
  1347. }
  1348. else
  1349. {
  1350. if (++sb >= ulongFactor)
  1351. {
  1352. sb = 0;
  1353. sw++;
  1354. }
  1355. if (++tb >= ulongFactor)
  1356. {
  1357. tb = 0;
  1358. tw++;
  1359. }
  1360. }
  1361. }
  1362. }
  1363. public static void CopyBitsTo(this IList<sbyte> sourceList, int sourceIndex, IList<sbyte> targetList, int targetIndex, int count)
  1364. {
  1365. if (sourceList == null)
  1366. throw new ArgumentNullException("sourceList");
  1367. if (targetList == null)
  1368. throw new ArgumentNullException("targetList");
  1369. int sourceTotal = sourceList.Count * sbyteFactor;
  1370. int targetTotal = targetList.Count * sbyteFactor;
  1371. if (sourceIndex < 0 || sourceIndex > sourceTotal)
  1372. throw new ArgumentOutOfRangeException("sourceIndex");
  1373. if (targetIndex < 0 || targetIndex > targetTotal)
  1374. throw new ArgumentOutOfRangeException("targetIndex");
  1375. if (count < 0 || count > sourceTotal - sourceIndex || count > targetTotal - targetIndex)
  1376. throw new ArgumentOutOfRangeException("count");
  1377. //
  1378. bool reverse = sourceList == targetList && targetIndex > sourceIndex;
  1379. int sw = ((reverse) ? sourceIndex + count - 1 : sourceIndex) / sbyteFactor;
  1380. int sb = ((reverse) ? sourceIndex + count - 1 : sourceIndex) % sbyteFactor;
  1381. int tw = ((reverse) ? targetIndex + count - 1 : targetIndex) / sbyteFactor;
  1382. int tb = ((reverse) ? targetIndex + count - 1 : targetIndex) % sbyteFactor;
  1383. while (count-- > 0)
  1384. {
  1385. if ((sourceList[sw] & (sbyte)(1 << sb)) != 0)
  1386. targetList[tw] |= (sbyte)(1 << tb);
  1387. else
  1388. targetList[tw] &= (sbyte)~(1 << tb);
  1389. if (reverse)
  1390. {
  1391. if (--sb < 0)
  1392. {
  1393. sb = sbyteFactor - 1;
  1394. sw--;
  1395. }
  1396. if (--tb < 0)
  1397. {
  1398. tb = sbyteFactor - 1;
  1399. tw--;
  1400. }
  1401. }
  1402. else
  1403. {
  1404. if (++sb >= sbyteFactor)
  1405. {
  1406. sb = 0;
  1407. sw++;
  1408. }
  1409. if (++tb >= sbyteFactor)
  1410. {
  1411. tb = 0;
  1412. tw++;
  1413. }
  1414. }
  1415. }
  1416. }
  1417. public static void CopyBitsTo(this IList<short> sourceList, int sourceIndex, IList<short> targetList, int targetIndex, int count)
  1418. {
  1419. if (sourceList == null)
  1420. throw new ArgumentNullException("sourceList");
  1421. if (targetList == null)
  1422. throw new ArgumentNullException("targetList");
  1423. int sourceTotal = sourceList.Count * shortFactor;
  1424. int targetTotal = targetList.Count * shortFactor;
  1425. if (sourceIndex < 0 || sourceIndex > sourceTotal)
  1426. throw new ArgumentOutOfRangeException("sourceIndex");
  1427. if (targetIndex < 0 || targetIndex > targetTotal)
  1428. throw new ArgumentOutOfRangeException("targetIndex");
  1429. if (count < 0 || count > sourceTotal - sourceIndex || count > targetTotal - targetIndex)
  1430. throw new ArgumentOutOfRangeException("count");
  1431. //
  1432. bool reverse = sourceList == targetList && targetIndex > sourceIndex;
  1433. int sw = ((reverse) ? sourceIndex + count - 1 : sourceIndex) / shortFactor;
  1434. int sb = ((reverse) ? sourceIndex + count - 1 : sourceIndex) % shortFactor;
  1435. int tw = ((reverse) ? targetIndex + count - 1 : targetIndex) / shortFactor;
  1436. int tb = ((reverse) ? targetIndex + count - 1 : targetIndex) % shortFactor;
  1437. while (count-- > 0)
  1438. {
  1439. if ((sourceList[sw] & (short)(1 << sb)) != 0)
  1440. targetList[tw] |= (short)(1 << tb);
  1441. else
  1442. targetList[tw] &= (short)~(1 << tb);
  1443. if (reverse)
  1444. {
  1445. if (--sb < 0)
  1446. {
  1447. sb = shortFactor - 1;
  1448. sw--;
  1449. }
  1450. if (--tb < 0)
  1451. {
  1452. tb = shortFactor - 1;
  1453. tw--;
  1454. }
  1455. }
  1456. else
  1457. {
  1458. if (++sb >= shortFactor)
  1459. {
  1460. sb = 0;
  1461. sw++;
  1462. }
  1463. if (++tb >= shortFactor)
  1464. {
  1465. tb = 0;
  1466. tw++;
  1467. }
  1468. }
  1469. }
  1470. }
  1471. public static void CopyBitsTo(this IList<int> sourceList, int sourceIndex, IList<int> targetList, int targetIndex, int count)
  1472. {
  1473. if (sourceList == null)
  1474. throw new ArgumentNullException("sourceList");
  1475. if (targetList == null)
  1476. throw new ArgumentNullException("targetList");
  1477. int sourceTotal = sourceList.Count * intFactor;
  1478. int targetTotal = targetList.Count * intFactor;
  1479. if (sourceIndex < 0 || sourceIndex > sourceTotal)
  1480. throw new ArgumentOutOfRangeException("sourceIndex");
  1481. if (targetIndex < 0 || targetIndex > targetTotal)
  1482. throw new ArgumentOutOfRangeException("targetIndex");
  1483. if (count < 0 || count > sourceTotal - sourceIndex || count > targetTotal - targetIndex)
  1484. throw new ArgumentOutOfRangeException("count");
  1485. //
  1486. bool reverse = sourceList == targetList && targetIndex > sourceIndex;
  1487. int sw = ((reverse) ? sourceIndex + count - 1 : sourceIndex) / intFactor;
  1488. int sb = ((reverse) ? sourceIndex + count - 1 : sourceIndex) % intFactor;
  1489. int tw = ((reverse) ? targetIndex + count - 1 : targetIndex) / intFactor;
  1490. int tb = ((reverse) ? targetIndex + count - 1 : targetIndex) % intFactor;
  1491. while (count-- > 0)
  1492. {
  1493. if ((sourceList[sw] & (1 << sb)) != 0)
  1494. targetList[tw] |= (1 << tb);
  1495. else
  1496. targetList[tw] &= ~(1 << tb);
  1497. if (reverse)
  1498. {
  1499. if (--sb < 0)
  1500. {
  1501. sb = intFactor - 1;
  1502. sw--;
  1503. }
  1504. if (--tb < 0)
  1505. {
  1506. tb = intFactor - 1;
  1507. tw--;
  1508. }
  1509. }
  1510. else
  1511. {
  1512. if (++sb >= intFactor)
  1513. {
  1514. sb = 0;
  1515. sw++;
  1516. }
  1517. if (++tb >= intFactor)
  1518. {
  1519. tb = 0;
  1520. tw++;
  1521. }
  1522. }
  1523. }
  1524. }
  1525. public static void CopyBitsTo(this IList<long> sourceList, int sourceIndex, IList<long> targetList, int targetIndex, int count)
  1526. {
  1527. if (sourceList == null)
  1528. throw new ArgumentNullException("sourceList");
  1529. if (targetList == null)
  1530. throw new ArgumentNullException("targetList");
  1531. int sourceTotal = sourceList.Count * longFactor;
  1532. int targetTotal = targetList.Count * longFactor;
  1533. if (sourceIndex < 0 || sourceIndex > sourceTotal)
  1534. throw new ArgumentOutOfRangeException("sourceIndex");
  1535. if (targetIndex < 0 || targetIndex > targetTotal)
  1536. throw new ArgumentOutOfRangeException("targetIndex");
  1537. if (count < 0 || count > sourceTotal - sourceIndex || count > targetTotal - targetIndex)
  1538. throw new ArgumentOutOfRangeException("count");
  1539. //
  1540. bool reverse = sourceList == targetList && targetIndex > sourceIndex;
  1541. int sw = ((reverse) ? sourceIndex + count - 1 : sourceIndex) / longFactor;
  1542. int sb = ((reverse) ? sourceIndex + count - 1 : sourceIndex) % longFactor;
  1543. int tw = ((reverse) ? targetIndex + count - 1 : targetIndex) / longFactor;
  1544. int tb = ((reverse) ? targetIndex + count - 1 : targetIndex) % longFactor;
  1545. while (count-- > 0)
  1546. {
  1547. if ((sourceList[sw] & (1L << sb)) != 0)
  1548. targetList[tw] |= (1L << tb);
  1549. else
  1550. targetList[tw] &= ~(1L << tb);
  1551. if (reverse)
  1552. {
  1553. if (--sb < 0)
  1554. {
  1555. sb = longFactor - 1;
  1556. sw--;
  1557. }
  1558. if (--tb < 0)
  1559. {
  1560. tb = longFactor - 1;
  1561. tw--;
  1562. }
  1563. }
  1564. else
  1565. {
  1566. if (++sb >= longFactor)
  1567. {
  1568. sb = 0;
  1569. sw++;
  1570. }
  1571. if (++tb >= longFactor)
  1572. {
  1573. tb = 0;
  1574. tw++;
  1575. }
  1576. }
  1577. }
  1578. }
  1579. #endregion
  1580. #region Equals operation
  1581. public static bool EqualsBits(this IList<byte> xList, int xIndex, IList<byte> yList, int yIndex, int count)
  1582. {
  1583. if (xList == null)
  1584. throw new ArgumentNullException("xList");
  1585. if (yList == null)
  1586. throw new ArgumentNullException("yList");
  1587. int xTotal = xList.Count * byteFactor;
  1588. int yTotal = yList.Count * byteFactor;
  1589. if (xIndex < 0 || xIndex > xTotal)
  1590. throw new ArgumentOutOfRangeException("xIndex");
  1591. if (yIndex < 0 || yIndex > yTotal)
  1592. throw new ArgumentOutOfRangeException("yIndex");
  1593. if (count < 0 || count > xTotal - xIndex || count > yTotal - yIndex)
  1594. throw new ArgumentOutOfRangeException("count");
  1595. if (xList == yList && count > global::System.Math.Abs(yIndex - xIndex))
  1596. throw new ArgumentException("Ranges intersected");
  1597. //
  1598. int xw = xIndex / byteFactor;
  1599. int xb = xIndex % byteFactor;
  1600. int yw = yIndex / byteFactor;
  1601. int yb = yIndex % byteFactor;
  1602. while (count-- > 0)
  1603. {
  1604. if (((xList[xw] & (byte)(1 << xb)) != 0) != ((yList[yw] & (byte)(1 << yb)) != 0))
  1605. return false;
  1606. if (++xb >= byteFactor)
  1607. {
  1608. xb = 0;
  1609. xw++;
  1610. }
  1611. if (++yb >= byteFactor)
  1612. {
  1613. yb = 0;
  1614. yw++;
  1615. }
  1616. }
  1617. return true;
  1618. }
  1619. public static bool EqualsBits(this IList<ushort> xList, int xIndex, IList<ushort> yList, int yIndex, int count)
  1620. {
  1621. if (xList == null)
  1622. throw new ArgumentNullException("xList");
  1623. if (yList == null)
  1624. throw new ArgumentNullException("yList");
  1625. int xTotal = xList.Count * ushortFactor;
  1626. int yTotal = yList.Count * ushortFactor;
  1627. if (xIndex < 0 || xIndex > xTotal)
  1628. throw new ArgumentOutOfRangeException("xIndex");
  1629. if (yIndex < 0 || yIndex > yTotal)
  1630. throw new ArgumentOutOfRangeException("yIndex");
  1631. if (count < 0 || count > xTotal - xIndex || count > yTotal - yIndex)
  1632. throw new ArgumentOutOfRangeException("count");
  1633. if (xList == yList && count > global::System.Math.Abs(yIndex - xIndex))
  1634. throw new ArgumentException("Ranges intersected");
  1635. //
  1636. int xw = xIndex / ushortFactor;
  1637. int xb = xIndex % ushortFactor;
  1638. int yw = yIndex / ushortFactor;
  1639. int yb = yIndex % ushortFactor;
  1640. while (count-- > 0)
  1641. {

Large files files are truncated, but you can click here to view the full file