You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2728 lines
87 KiB

2 years ago
  1. /*--------------------------------------------------------------------------
  2. * linq.js - LINQ for JavaScript
  3. * ver 2.2.0.2 (Jan. 21th, 2011)
  4. *
  5. * created and maintained by neuecc <ils@neue.cc>
  6. * licensed under Microsoft Public License(Ms-PL)
  7. * http://neue.cc/
  8. * http://linqjs.codeplex.com/
  9. *--------------------------------------------------------------------------*/
  10. Enumerable = (function ()
  11. {
  12. var Enumerable = function (getEnumerator)
  13. {
  14. this.GetEnumerator = getEnumerator;
  15. }
  16. // Generator
  17. Enumerable.Choice = function () // variable argument
  18. {
  19. var args = (arguments[0] instanceof Array) ? arguments[0] : arguments;
  20. return new Enumerable(function ()
  21. {
  22. return new IEnumerator(
  23. Functions.Blank,
  24. function ()
  25. {
  26. return this.Yield(args[Math.floor(Math.random() * args.length)]);
  27. },
  28. Functions.Blank);
  29. });
  30. }
  31. Enumerable.Cycle = function () // variable argument
  32. {
  33. var args = (arguments[0] instanceof Array) ? arguments[0] : arguments;
  34. return new Enumerable(function ()
  35. {
  36. var index = 0;
  37. return new IEnumerator(
  38. Functions.Blank,
  39. function ()
  40. {
  41. if (index >= args.length) index = 0;
  42. return this.Yield(args[index++]);
  43. },
  44. Functions.Blank);
  45. });
  46. }
  47. Enumerable.Empty = function ()
  48. {
  49. return new Enumerable(function ()
  50. {
  51. return new IEnumerator(
  52. Functions.Blank,
  53. function () { return false; },
  54. Functions.Blank);
  55. });
  56. }
  57. Enumerable.From = function (obj)
  58. {
  59. if (obj == null)
  60. {
  61. return Enumerable.Empty();
  62. }
  63. if (obj instanceof Enumerable)
  64. {
  65. return obj;
  66. }
  67. if (typeof obj == Types.Number || typeof obj == Types.Boolean)
  68. {
  69. return Enumerable.Repeat(obj, 1);
  70. }
  71. if (typeof obj == Types.String)
  72. {
  73. return new Enumerable(function ()
  74. {
  75. var index = 0;
  76. return new IEnumerator(
  77. Functions.Blank,
  78. function ()
  79. {
  80. return (index < obj.length) ? this.Yield(obj.charAt(index++)) : false;
  81. },
  82. Functions.Blank);
  83. });
  84. }
  85. if (typeof obj != Types.Function)
  86. {
  87. // array or array like object
  88. if (typeof obj.length == Types.Number)
  89. {
  90. return new ArrayEnumerable(obj);
  91. }
  92. // JScript's IEnumerable
  93. if (!(obj instanceof Object) && Utils.IsIEnumerable(obj))
  94. {
  95. return new Enumerable(function ()
  96. {
  97. var isFirst = true;
  98. var enumerator;
  99. return new IEnumerator(
  100. function () { enumerator = new Enumerator(obj); },
  101. function ()
  102. {
  103. if (isFirst) isFirst = false;
  104. else enumerator.moveNext();
  105. return (enumerator.atEnd()) ? false : this.Yield(enumerator.item());
  106. },
  107. Functions.Blank);
  108. });
  109. }
  110. }
  111. // case function/object : Create KeyValuePair[]
  112. return new Enumerable(function ()
  113. {
  114. var array = [];
  115. var index = 0;
  116. return new IEnumerator(
  117. function ()
  118. {
  119. for (var key in obj)
  120. {
  121. if (!(obj[key] instanceof Function))
  122. {
  123. array.push({ Key: key, Value: obj[key] });
  124. }
  125. }
  126. },
  127. function ()
  128. {
  129. return (index < array.length)
  130. ? this.Yield(array[index++])
  131. : false;
  132. },
  133. Functions.Blank);
  134. });
  135. },
  136. Enumerable.Return = function (element)
  137. {
  138. return Enumerable.Repeat(element, 1);
  139. }
  140. // Overload:function(input, pattern)
  141. // Overload:function(input, pattern, flags)
  142. Enumerable.Matches = function (input, pattern, flags)
  143. {
  144. if (flags == null) flags = "";
  145. if (pattern instanceof RegExp)
  146. {
  147. flags += (pattern.ignoreCase) ? "i" : "";
  148. flags += (pattern.multiline) ? "m" : "";
  149. pattern = pattern.source;
  150. }
  151. if (flags.indexOf("g") === -1) flags += "g";
  152. return new Enumerable(function ()
  153. {
  154. var regex;
  155. return new IEnumerator(
  156. function () { regex = new RegExp(pattern, flags) },
  157. function ()
  158. {
  159. var match = regex.exec(input);
  160. return (match) ? this.Yield(match) : false;
  161. },
  162. Functions.Blank);
  163. });
  164. }
  165. // Overload:function(start, count)
  166. // Overload:function(start, count, step)
  167. Enumerable.Range = function (start, count, step)
  168. {
  169. if (step == null) step = 1;
  170. return Enumerable.ToInfinity(start, step).Take(count);
  171. }
  172. // Overload:function(start, count)
  173. // Overload:function(start, count, step)
  174. Enumerable.RangeDown = function (start, count, step)
  175. {
  176. if (step == null) step = 1;
  177. return Enumerable.ToNegativeInfinity(start, step).Take(count);
  178. }
  179. // Overload:function(start, to)
  180. // Overload:function(start, to, step)
  181. Enumerable.RangeTo = function (start, to, step)
  182. {
  183. if (step == null) step = 1;
  184. return (start < to)
  185. ? Enumerable.ToInfinity(start, step).TakeWhile(function (i) { return i <= to; })
  186. : Enumerable.ToNegativeInfinity(start, step).TakeWhile(function (i) { return i >= to; })
  187. }
  188. // Overload:function(obj)
  189. // Overload:function(obj, num)
  190. Enumerable.Repeat = function (obj, num)
  191. {
  192. if (num != null) return Enumerable.Repeat(obj).Take(num);
  193. return new Enumerable(function ()
  194. {
  195. return new IEnumerator(
  196. Functions.Blank,
  197. function () { return this.Yield(obj); },
  198. Functions.Blank);
  199. });
  200. }
  201. Enumerable.RepeatWithFinalize = function (initializer, finalizer)
  202. {
  203. initializer = Utils.CreateLambda(initializer);
  204. finalizer = Utils.CreateLambda(finalizer);
  205. return new Enumerable(function ()
  206. {
  207. var element;
  208. return new IEnumerator(
  209. function () { element = initializer(); },
  210. function () { return this.Yield(element); },
  211. function ()
  212. {
  213. if (element != null)
  214. {
  215. finalizer(element);
  216. element = null;
  217. }
  218. });
  219. });
  220. }
  221. // Overload:function(func)
  222. // Overload:function(func, count)
  223. Enumerable.Generate = function (func, count)
  224. {
  225. if (count != null) return Enumerable.Generate(func).Take(count);
  226. func = Utils.CreateLambda(func);
  227. return new Enumerable(function ()
  228. {
  229. return new IEnumerator(
  230. Functions.Blank,
  231. function () { return this.Yield(func()); },
  232. Functions.Blank);
  233. });
  234. }
  235. // Overload:function()
  236. // Overload:function(start)
  237. // Overload:function(start, step)
  238. Enumerable.ToInfinity = function (start, step)
  239. {
  240. if (start == null) start = 0;
  241. if (step == null) step = 1;
  242. return new Enumerable(function ()
  243. {
  244. var value;
  245. return new IEnumerator(
  246. function () { value = start - step },
  247. function () { return this.Yield(value += step); },
  248. Functions.Blank);
  249. });
  250. }
  251. // Overload:function()
  252. // Overload:function(start)
  253. // Overload:function(start, step)
  254. Enumerable.ToNegativeInfinity = function (start, step)
  255. {
  256. if (start == null) start = 0;
  257. if (step == null) step = 1;
  258. return new Enumerable(function ()
  259. {
  260. var value;
  261. return new IEnumerator(
  262. function () { value = start + step },
  263. function () { return this.Yield(value -= step); },
  264. Functions.Blank);
  265. });
  266. }
  267. Enumerable.Unfold = function (seed, func)
  268. {
  269. func = Utils.CreateLambda(func);
  270. return new Enumerable(function ()
  271. {
  272. var isFirst = true;
  273. var value;
  274. return new IEnumerator(
  275. Functions.Blank,
  276. function ()
  277. {
  278. if (isFirst)
  279. {
  280. isFirst = false;
  281. value = seed;
  282. return this.Yield(value);
  283. }
  284. value = func(value);
  285. return this.Yield(value);
  286. },
  287. Functions.Blank);
  288. });
  289. }
  290. // Extension Methods
  291. Enumerable.prototype =
  292. {
  293. /* Projection and Filtering Methods */
  294. // Overload:function(func)
  295. // Overload:function(func, resultSelector<element>)
  296. // Overload:function(func, resultSelector<element, nestLevel>)
  297. CascadeBreadthFirst: function (func, resultSelector)
  298. {
  299. var source = this;
  300. func = Utils.CreateLambda(func);
  301. resultSelector = Utils.CreateLambda(resultSelector);
  302. return new Enumerable(function ()
  303. {
  304. var enumerator;
  305. var nestLevel = 0;
  306. var buffer = [];
  307. return new IEnumerator(
  308. function () { enumerator = source.GetEnumerator(); },
  309. function ()
  310. {
  311. while (true)
  312. {
  313. if (enumerator.MoveNext())
  314. {
  315. buffer.push(enumerator.Current());
  316. return this.Yield(resultSelector(enumerator.Current(), nestLevel));
  317. }
  318. var next = Enumerable.From(buffer).SelectMany(function (x) { return func(x); });
  319. if (!next.Any())
  320. {
  321. return false;
  322. }
  323. else
  324. {
  325. nestLevel++;
  326. buffer = [];
  327. Utils.Dispose(enumerator);
  328. enumerator = next.GetEnumerator();
  329. }
  330. }
  331. },
  332. function () { Utils.Dispose(enumerator); });
  333. });
  334. },
  335. // Overload:function(func)
  336. // Overload:function(func, resultSelector<element>)
  337. // Overload:function(func, resultSelector<element, nestLevel>)
  338. CascadeDepthFirst: function (func, resultSelector)
  339. {
  340. var source = this;
  341. func = Utils.CreateLambda(func);
  342. resultSelector = Utils.CreateLambda(resultSelector);
  343. return new Enumerable(function ()
  344. {
  345. var enumeratorStack = [];
  346. var enumerator;
  347. return new IEnumerator(
  348. function () { enumerator = source.GetEnumerator(); },
  349. function ()
  350. {
  351. while (true)
  352. {
  353. if (enumerator.MoveNext())
  354. {
  355. var value = resultSelector(enumerator.Current(), enumeratorStack.length);
  356. enumeratorStack.push(enumerator);
  357. enumerator = Enumerable.From(func(enumerator.Current())).GetEnumerator();
  358. return this.Yield(value);
  359. }
  360. if (enumeratorStack.length <= 0) return false;
  361. Utils.Dispose(enumerator);
  362. enumerator = enumeratorStack.pop();
  363. }
  364. },
  365. function ()
  366. {
  367. try { Utils.Dispose(enumerator); }
  368. finally { Enumerable.From(enumeratorStack).ForEach(function (s) { s.Dispose(); }) }
  369. });
  370. });
  371. },
  372. Flatten: function ()
  373. {
  374. var source = this;
  375. return new Enumerable(function ()
  376. {
  377. var enumerator;
  378. var middleEnumerator = null;
  379. return new IEnumerator(
  380. function () { enumerator = source.GetEnumerator(); },
  381. function ()
  382. {
  383. while (true)
  384. {
  385. if (middleEnumerator != null)
  386. {
  387. if (middleEnumerator.MoveNext())
  388. {
  389. return this.Yield(middleEnumerator.Current());
  390. }
  391. else
  392. {
  393. middleEnumerator = null;
  394. }
  395. }
  396. if (enumerator.MoveNext())
  397. {
  398. if (enumerator.Current() instanceof Array)
  399. {
  400. Utils.Dispose(middleEnumerator);
  401. middleEnumerator = Enumerable.From(enumerator.Current())
  402. .SelectMany(Functions.Identity)
  403. .Flatten()
  404. .GetEnumerator();
  405. continue;
  406. }
  407. else
  408. {
  409. return this.Yield(enumerator.Current());
  410. }
  411. }
  412. return false;
  413. }
  414. },
  415. function ()
  416. {
  417. try { Utils.Dispose(enumerator); }
  418. finally { Utils.Dispose(middleEnumerator); }
  419. });
  420. });
  421. },
  422. Pairwise: function (selector)
  423. {
  424. var source = this;
  425. selector = Utils.CreateLambda(selector);
  426. return new Enumerable(function ()
  427. {
  428. var enumerator;
  429. return new IEnumerator(
  430. function ()
  431. {
  432. enumerator = source.GetEnumerator();
  433. enumerator.MoveNext();
  434. },
  435. function ()
  436. {
  437. var prev = enumerator.Current();
  438. return (enumerator.MoveNext())
  439. ? this.Yield(selector(prev, enumerator.Current()))
  440. : false;
  441. },
  442. function () { Utils.Dispose(enumerator); });
  443. });
  444. },
  445. // Overload:function(func)
  446. // Overload:function(seed,func<value,element>)
  447. // Overload:function(seed,func<value,element>,resultSelector)
  448. Scan: function (seed, func, resultSelector)
  449. {
  450. if (resultSelector != null) return this.Scan(seed, func).Select(resultSelector);
  451. var isUseSeed;
  452. if (func == null)
  453. {
  454. func = Utils.CreateLambda(seed); // arguments[0]
  455. isUseSeed = false;
  456. }
  457. else
  458. {
  459. func = Utils.CreateLambda(func);
  460. isUseSeed = true;
  461. }
  462. var source = this;
  463. return new Enumerable(function ()
  464. {
  465. var enumerator;
  466. var value;
  467. var isFirst = true;
  468. return new IEnumerator(
  469. function () { enumerator = source.GetEnumerator(); },
  470. function ()
  471. {
  472. if (isFirst)
  473. {
  474. isFirst = false;
  475. if (!isUseSeed)
  476. {
  477. if (enumerator.MoveNext())
  478. {
  479. return this.Yield(value = enumerator.Current());
  480. }
  481. }
  482. else
  483. {
  484. return this.Yield(value = seed);
  485. }
  486. }
  487. return (enumerator.MoveNext())
  488. ? this.Yield(value = func(value, enumerator.Current()))
  489. : false;
  490. },
  491. function () { Utils.Dispose(enumerator); });
  492. });
  493. },
  494. // Overload:function(selector<element>)
  495. // Overload:function(selector<element,index>)
  496. Select: function (selector)
  497. {
  498. var source = this;
  499. selector = Utils.CreateLambda(selector);
  500. return new Enumerable(function ()
  501. {
  502. var enumerator;
  503. var index = 0;
  504. return new IEnumerator(
  505. function () { enumerator = source.GetEnumerator(); },
  506. function ()
  507. {
  508. return (enumerator.MoveNext())
  509. ? this.Yield(selector(enumerator.Current(), index++))
  510. : false;
  511. },
  512. function () { Utils.Dispose(enumerator); })
  513. });
  514. },
  515. // Overload:function(collectionSelector<element>)
  516. // Overload:function(collectionSelector<element,index>)
  517. // Overload:function(collectionSelector<element>,resultSelector)
  518. // Overload:function(collectionSelector<element,index>,resultSelector)
  519. SelectMany: function (collectionSelector, resultSelector)
  520. {
  521. var source = this;
  522. collectionSelector = Utils.CreateLambda(collectionSelector);
  523. if (resultSelector == null) resultSelector = function (a, b) { return b; }
  524. resultSelector = Utils.CreateLambda(resultSelector);
  525. return new Enumerable(function ()
  526. {
  527. var enumerator;
  528. var middleEnumerator = undefined;
  529. var index = 0;
  530. return new IEnumerator(
  531. function () { enumerator = source.GetEnumerator(); },
  532. function ()
  533. {
  534. if (middleEnumerator === undefined)
  535. {
  536. if (!enumerator.MoveNext()) return false;
  537. }
  538. do
  539. {
  540. if (middleEnumerator == null)
  541. {
  542. var middleSeq = collectionSelector(enumerator.Current(), index++);
  543. middleEnumerator = Enumerable.From(middleSeq).GetEnumerator();
  544. }
  545. if (middleEnumerator.MoveNext())
  546. {
  547. return this.Yield(resultSelector(enumerator.Current(), middleEnumerator.Current()));
  548. }
  549. Utils.Dispose(middleEnumerator);
  550. middleEnumerator = null;
  551. } while (enumerator.MoveNext())
  552. return false;
  553. },
  554. function ()
  555. {
  556. try { Utils.Dispose(enumerator); }
  557. finally { Utils.Dispose(middleEnumerator); }
  558. })
  559. });
  560. },
  561. // Overload:function(predicate<element>)
  562. // Overload:function(predicate<element,index>)
  563. Where: function (predicate)
  564. {
  565. predicate = Utils.CreateLambda(predicate);
  566. var source = this;
  567. return new Enumerable(function ()
  568. {
  569. var enumerator;
  570. var index = 0;
  571. return new IEnumerator(
  572. function () { enumerator = source.GetEnumerator(); },
  573. function ()
  574. {
  575. while (enumerator.MoveNext())
  576. {
  577. if (predicate(enumerator.Current(), index++))
  578. {
  579. return this.Yield(enumerator.Current());
  580. }
  581. }
  582. return false;
  583. },
  584. function () { Utils.Dispose(enumerator); })
  585. });
  586. },
  587. OfType: function (type)
  588. {
  589. var typeName;
  590. switch (type)
  591. {
  592. case Number: typeName = Types.Number; break;
  593. case String: typeName = Types.String; break;
  594. case Boolean: typeName = Types.Boolean; break;
  595. case Function: typeName = Types.Function; break;
  596. default: typeName = null; break;
  597. }
  598. return (typeName === null)
  599. ? this.Where(function (x) { return x instanceof type })
  600. : this.Where(function (x) { return typeof x === typeName });
  601. },
  602. // Overload:function(second,selector<outer,inner>)
  603. // Overload:function(second,selector<outer,inner,index>)
  604. Zip: function (second, selector)
  605. {
  606. selector = Utils.CreateLambda(selector);
  607. var source = this;
  608. return new Enumerable(function ()
  609. {
  610. var firstEnumerator;
  611. var secondEnumerator;
  612. var index = 0;
  613. return new IEnumerator(
  614. function ()
  615. {
  616. firstEnumerator = source.GetEnumerator();
  617. secondEnumerator = Enumerable.From(second).GetEnumerator();
  618. },
  619. function ()
  620. {
  621. if (firstEnumerator.MoveNext() && secondEnumerator.MoveNext())
  622. {
  623. return this.Yield(selector(firstEnumerator.Current(), secondEnumerator.Current(), index++));
  624. }
  625. return false;
  626. },
  627. function ()
  628. {
  629. try { Utils.Dispose(firstEnumerator); }
  630. finally { Utils.Dispose(secondEnumerator); }
  631. })
  632. });
  633. },
  634. /* Join Methods */
  635. // Overload:function (inner, outerKeySelector, innerKeySelector, resultSelector)
  636. // Overload:function (inner, outerKeySelector, innerKeySelector, resultSelector, compareSelector)
  637. Join: function (inner, outerKeySelector, innerKeySelector, resultSelector, compareSelector)
  638. {
  639. outerKeySelector = Utils.CreateLambda(outerKeySelector);
  640. innerKeySelector = Utils.CreateLambda(innerKeySelector);
  641. resultSelector = Utils.CreateLambda(resultSelector);
  642. compareSelector = Utils.CreateLambda(compareSelector);
  643. var source = this;
  644. return new Enumerable(function ()
  645. {
  646. var outerEnumerator;
  647. var lookup;
  648. var innerElements = null;
  649. var innerCount = 0;
  650. return new IEnumerator(
  651. function ()
  652. {
  653. outerEnumerator = source.GetEnumerator();
  654. lookup = Enumerable.From(inner).ToLookup(innerKeySelector, Functions.Identity, compareSelector);
  655. },
  656. function ()
  657. {
  658. while (true)
  659. {
  660. if (innerElements != null)
  661. {
  662. var innerElement = innerElements[innerCount++];
  663. if (innerElement !== undefined)
  664. {
  665. return this.Yield(resultSelector(outerEnumerator.Current(), innerElement));
  666. }
  667. innerElement = null;
  668. innerCount = 0;
  669. }
  670. if (outerEnumerator.MoveNext())
  671. {
  672. var key = outerKeySelector(outerEnumerator.Current());
  673. innerElements = lookup.Get(key).ToArray();
  674. }
  675. else
  676. {
  677. return false;
  678. }
  679. }
  680. },
  681. function () { Utils.Dispose(outerEnumerator); })
  682. });
  683. },
  684. // Overload:function (inner, outerKeySelector, innerKeySelector, resultSelector)
  685. // Overload:function (inner, outerKeySelector, innerKeySelector, resultSelector, compareSelector)
  686. GroupJoin: function (inner, outerKeySelector, innerKeySelector, resultSelector, compareSelector)
  687. {
  688. outerKeySelector = Utils.CreateLambda(outerKeySelector);
  689. innerKeySelector = Utils.CreateLambda(innerKeySelector);
  690. resultSelector = Utils.CreateLambda(resultSelector);
  691. compareSelector = Utils.CreateLambda(compareSelector);
  692. var source = this;
  693. return new Enumerable(function ()
  694. {
  695. var enumerator = source.GetEnumerator();
  696. var lookup = null;
  697. return new IEnumerator(
  698. function ()
  699. {
  700. enumerator = source.GetEnumerator();
  701. lookup = Enumerable.From(inner).ToLookup(innerKeySelector, Functions.Identity, compareSelector);
  702. },
  703. function ()
  704. {
  705. if (enumerator.MoveNext())
  706. {
  707. var innerElement = lookup.Get(outerKeySelector(enumerator.Current()));
  708. return this.Yield(resultSelector(enumerator.Current(), innerElement));
  709. }
  710. return false;
  711. },
  712. function () { Utils.Dispose(enumerator); })
  713. });
  714. },
  715. /* Set Methods */
  716. All: function (predicate)
  717. {
  718. predicate = Utils.CreateLambda(predicate);
  719. var result = true;
  720. this.ForEach(function (x)
  721. {
  722. if (!predicate(x))
  723. {
  724. result = false;
  725. return false; // break
  726. }
  727. });
  728. return result;
  729. },
  730. // Overload:function()
  731. // Overload:function(predicate)
  732. Any: function (predicate)
  733. {
  734. predicate = Utils.CreateLambda(predicate);
  735. var enumerator = this.GetEnumerator();
  736. try
  737. {
  738. if (arguments.length == 0) return enumerator.MoveNext(); // case:function()
  739. while (enumerator.MoveNext()) // case:function(predicate)
  740. {
  741. if (predicate(enumerator.Current())) return true;
  742. }
  743. return false;
  744. }
  745. finally { Utils.Dispose(enumerator); }
  746. },
  747. Concat: function (second)
  748. {
  749. var source = this;
  750. return new Enumerable(function ()
  751. {
  752. var firstEnumerator;
  753. var secondEnumerator;
  754. return new IEnumerator(
  755. function () { firstEnumerator = source.GetEnumerator(); },
  756. function ()
  757. {
  758. if (secondEnumerator == null)
  759. {
  760. if (firstEnumerator.MoveNext()) return this.Yield(firstEnumerator.Current());
  761. secondEnumerator = Enumerable.From(second).GetEnumerator();
  762. }
  763. if (secondEnumerator.MoveNext()) return this.Yield(secondEnumerator.Current());
  764. return false;
  765. },
  766. function ()
  767. {
  768. try { Utils.Dispose(firstEnumerator); }
  769. finally { Utils.Dispose(secondEnumerator); }
  770. })
  771. });
  772. },
  773. Insert: function (index, second)
  774. {
  775. var source = this;
  776. return new Enumerable(function ()
  777. {
  778. var firstEnumerator;
  779. var secondEnumerator;
  780. var count = 0;
  781. var isEnumerated = false;
  782. return new IEnumerator(
  783. function ()
  784. {
  785. firstEnumerator = source.GetEnumerator();
  786. secondEnumerator = Enumerable.From(second).GetEnumerator();
  787. },
  788. function ()
  789. {
  790. if (count == index && secondEnumerator.MoveNext())
  791. {
  792. isEnumerated = true;
  793. return this.Yield(secondEnumerator.Current());
  794. }
  795. if (firstEnumerator.MoveNext())
  796. {
  797. count++;
  798. return this.Yield(firstEnumerator.Current());
  799. }
  800. if (!isEnumerated && secondEnumerator.MoveNext())
  801. {
  802. return this.Yield(secondEnumerator.Current());
  803. }
  804. return false;
  805. },
  806. function ()
  807. {
  808. try { Utils.Dispose(firstEnumerator); }
  809. finally { Utils.Dispose(secondEnumerator); }
  810. })
  811. });
  812. },
  813. Alternate: function (value)
  814. {
  815. value = Enumerable.Return(value);
  816. return this.SelectMany(function (elem)
  817. {
  818. return Enumerable.Return(elem).Concat(value);
  819. }).TakeExceptLast();
  820. },
  821. // Overload:function(value)
  822. // Overload:function(value, compareSelector)
  823. Contains: function (value, compareSelector)
  824. {
  825. compareSelector = Utils.CreateLambda(compareSelector);
  826. var enumerator = this.GetEnumerator();
  827. try
  828. {
  829. while (enumerator.MoveNext())
  830. {
  831. if (compareSelector(enumerator.Current()) === value) return true;
  832. }
  833. return false;
  834. }
  835. finally { Utils.Dispose(enumerator) }
  836. },
  837. DefaultIfEmpty: function (defaultValue)
  838. {
  839. var source = this;
  840. return new Enumerable(function ()
  841. {
  842. var enumerator;
  843. var isFirst = true;
  844. return new IEnumerator(
  845. function () { enumerator = source.GetEnumerator(); },
  846. function ()
  847. {
  848. if (enumerator.MoveNext())
  849. {
  850. isFirst = false;
  851. return this.Yield(enumerator.Current());
  852. }
  853. else if (isFirst)
  854. {
  855. isFirst = false;
  856. return this.Yield(defaultValue);
  857. }
  858. return false;
  859. },
  860. function () { Utils.Dispose(enumerator); })
  861. });
  862. },
  863. // Overload:function()
  864. // Overload:function(compareSelector)
  865. Distinct: function (compareSelector)
  866. {
  867. return this.Except(Enumerable.Empty(), compareSelector);
  868. },
  869. // Overload:function(second)
  870. // Overload:function(second, compareSelector)
  871. Except: function (second, compareSelector)
  872. {
  873. compareSelector = Utils.CreateLambda(compareSelector);
  874. var source = this;
  875. return new Enumerable(function ()
  876. {
  877. var enumerator;
  878. var keys;
  879. return new IEnumerator(
  880. function ()
  881. {
  882. enumerator = source.GetEnumerator();
  883. keys = new Dictionary(compareSelector);
  884. Enumerable.From(second).ForEach(function (key) { keys.Add(key); });
  885. },
  886. function ()
  887. {
  888. while (enumerator.MoveNext())
  889. {
  890. var current = enumerator.Current();
  891. if (!keys.Contains(current))
  892. {
  893. keys.Add(current);
  894. return this.Yield(current);
  895. }
  896. }
  897. return false;
  898. },
  899. function () { Utils.Dispose(enumerator); })
  900. });
  901. },
  902. // Overload:function(second)
  903. // Overload:function(second, compareSelector)
  904. Intersect: function (second, compareSelector)
  905. {
  906. compareSelector = Utils.CreateLambda(compareSelector);
  907. var source = this;
  908. return new Enumerable(function ()
  909. {
  910. var enumerator;
  911. var keys;
  912. var outs;
  913. return new IEnumerator(
  914. function ()
  915. {
  916. enumerator = source.GetEnumerator();
  917. keys = new Dictionary(compareSelector);
  918. Enumerable.From(second).ForEach(function (key) { keys.Add(key); });
  919. outs = new Dictionary(compareSelector);
  920. },
  921. function ()
  922. {
  923. while (enumerator.MoveNext())
  924. {
  925. var current = enumerator.Current();
  926. if (!outs.Contains(current) && keys.Contains(current))
  927. {
  928. outs.Add(current);
  929. return this.Yield(current);
  930. }
  931. }
  932. return false;
  933. },
  934. function () { Utils.Dispose(enumerator); })
  935. });
  936. },
  937. // Overload:function(second)
  938. // Overload:function(second, compareSelector)
  939. SequenceEqual: function (second, compareSelector)
  940. {
  941. compareSelector = Utils.CreateLambda(compareSelector);
  942. var firstEnumerator = this.GetEnumerator();
  943. try
  944. {
  945. var secondEnumerator = Enumerable.From(second).GetEnumerator();
  946. try
  947. {
  948. while (firstEnumerator.MoveNext())
  949. {
  950. if (!secondEnumerator.MoveNext()
  951. || compareSelector(firstEnumerator.Current()) !== compareSelector(secondEnumerator.Current()))
  952. {
  953. return false;
  954. }
  955. }
  956. if (secondEnumerator.MoveNext()) return false;
  957. return true;
  958. }
  959. finally { Utils.Dispose(secondEnumerator); }
  960. }
  961. finally { Utils.Dispose(firstEnumerator); }
  962. },
  963. Union: function (second, compareSelector)
  964. {
  965. compareSelector = Utils.CreateLambda(compareSelector);
  966. var source = this;
  967. return new Enumerable(function ()
  968. {
  969. var firstEnumerator;
  970. var secondEnumerator;
  971. var keys;
  972. return new IEnumerator(
  973. function ()
  974. {
  975. firstEnumerator = source.GetEnumerator();
  976. keys = new Dictionary(compareSelector);
  977. },
  978. function ()
  979. {
  980. var current;
  981. if (secondEnumerator === undefined)
  982. {
  983. while (firstEnumerator.MoveNext())
  984. {
  985. current = firstEnumerator.Current();
  986. if (!keys.Contains(current))
  987. {
  988. keys.Add(current);
  989. return this.Yield(current);
  990. }
  991. }
  992. secondEnumerator = Enumerable.From(second).GetEnumerator();
  993. }
  994. while (secondEnumerator.MoveNext())
  995. {
  996. current = secondEnumerator.Current();
  997. if (!keys.Contains(current))
  998. {
  999. keys.Add(current);
  1000. return this.Yield(current);
  1001. }
  1002. }
  1003. return false;
  1004. },
  1005. function ()
  1006. {
  1007. try { Utils.Dispose(firstEnumerator); }
  1008. finally { Utils.Dispose(secondEnumerator); }
  1009. })
  1010. });
  1011. },
  1012. /* Ordering Methods */
  1013. OrderBy: function (keySelector)
  1014. {
  1015. return new OrderedEnumerable(this, keySelector, false);
  1016. },
  1017. OrderByDescending: function (keySelector)
  1018. {
  1019. return new OrderedEnumerable(this, keySelector, true);
  1020. },
  1021. Reverse: function ()
  1022. {
  1023. var source = this;
  1024. return new Enumerable(function ()
  1025. {
  1026. var buffer;
  1027. var index;
  1028. return new IEnumerator(
  1029. function ()
  1030. {
  1031. buffer = source.ToArray();
  1032. index = buffer.length;
  1033. },
  1034. function ()
  1035. {
  1036. return (index > 0)
  1037. ? this.Yield(buffer[--index])
  1038. : false;
  1039. },
  1040. Functions.Blank)
  1041. });
  1042. },
  1043. Shuffle: function ()
  1044. {
  1045. var source = this;
  1046. return new Enumerable(function ()
  1047. {
  1048. var buffer;
  1049. return new IEnumerator(
  1050. function () { buffer = source.ToArray(); },
  1051. function ()
  1052. {
  1053. if (buffer.length > 0)
  1054. {
  1055. var i = Math.floor(Math.random() * buffer.length);
  1056. return this.Yield(buffer.splice(i, 1)[0]);
  1057. }
  1058. return false;
  1059. },
  1060. Functions.Blank)
  1061. });
  1062. },
  1063. /* Grouping Methods */
  1064. // Overload:function(keySelector)
  1065. // Overload:function(keySelector,elementSelector)
  1066. // Overload:function(keySelector,elementSelector,resultSelector)
  1067. // Overload:function(keySelector,elementSelector,resultSelector,compareSelector)
  1068. GroupBy: function (keySelector, elementSelector, resultSelector, compareSelector)
  1069. {
  1070. var source = this;
  1071. keySelector = Utils.CreateLambda(keySelector);
  1072. elementSelector = Utils.CreateLambda(elementSelector);
  1073. if (resultSelector != null) resultSelector = Utils.CreateLambda(resultSelector);
  1074. compareSelector = Utils.CreateLambda(compareSelector);
  1075. return new Enumerable(function ()
  1076. {
  1077. var enumerator;
  1078. return new IEnumerator(
  1079. function ()
  1080. {
  1081. enumerator = source.ToLookup(keySelector, elementSelector, compareSelector)
  1082. .ToEnumerable()
  1083. .GetEnumerator();
  1084. },
  1085. function ()
  1086. {
  1087. while (enumerator.MoveNext())
  1088. {
  1089. return (resultSelector == null)
  1090. ? this.Yield(enumerator.Current())
  1091. : this.Yield(resultSelector(enumerator.Current().Key(), enumerator.Current()));
  1092. }
  1093. return false;
  1094. },
  1095. function () { Utils.Dispose(enumerator); })
  1096. });
  1097. },
  1098. // Overload:function(keySelector)
  1099. // Overload:function(keySelector,elementSelector)
  1100. // Overload:function(keySelector,elementSelector,resultSelector)
  1101. // Overload:function(keySelector,elementSelector,resultSelector,compareSelector)
  1102. PartitionBy: function (keySelector, elementSelector, resultSelector, compareSelector)
  1103. {
  1104. var source = this;
  1105. keySelector = Utils.CreateLambda(keySelector);
  1106. elementSelector = Utils.CreateLambda(elementSelector);
  1107. compareSelector = Utils.CreateLambda(compareSelector);
  1108. var hasResultSelector;
  1109. if (resultSelector == null)
  1110. {
  1111. hasResultSelector = false;
  1112. resultSelector = function (key, group) { return new Grouping(key, group) }
  1113. }
  1114. else
  1115. {
  1116. hasResultSelector = true;
  1117. resultSelector = Utils.CreateLambda(resultSelector);
  1118. }
  1119. return new Enumerable(function ()
  1120. {
  1121. var enumerator;
  1122. var key;
  1123. var compareKey;
  1124. var group = [];
  1125. return new IEnumerator(
  1126. function ()
  1127. {
  1128. enumerator = source.GetEnumerator();
  1129. if (enumerator.MoveNext())
  1130. {
  1131. key = keySelector(enumerator.Current());
  1132. compareKey = compareSelector(key);
  1133. group.push(elementSelector(enumerator.Current()));
  1134. }
  1135. },
  1136. function ()
  1137. {
  1138. var hasNext;
  1139. while ((hasNext = enumerator.MoveNext()) == true)
  1140. {
  1141. if (compareKey === compareSelector(keySelector(enumerator.Current())))
  1142. {
  1143. group.push(elementSelector(enumerator.Current()));
  1144. }
  1145. else break;
  1146. }
  1147. if (group.length > 0)
  1148. {
  1149. var result = (hasResultSelector)
  1150. ? resultSelector(key, Enumerable.From(group))
  1151. : resultSelector(key, group);
  1152. if (hasNext)
  1153. {
  1154. key = keySelector(enumerator.Current());
  1155. compareKey = compareSelector(key);
  1156. group = [elementSelector(enumerator.Current())];
  1157. }
  1158. else group = [];
  1159. return this.Yield(result);
  1160. }
  1161. return false;
  1162. },
  1163. function () { Utils.Dispose(enumerator); })
  1164. });
  1165. },
  1166. BufferWithCount: function (count)
  1167. {
  1168. var source = this;
  1169. return new Enumerable(function ()
  1170. {
  1171. var enumerator;
  1172. return new IEnumerator(
  1173. function () { enumerator = source.GetEnumerator(); },
  1174. function ()
  1175. {
  1176. var array = [];
  1177. var index = 0;
  1178. while (enumerator.MoveNext())
  1179. {
  1180. array.push(enumerator.Current());
  1181. if (++index >= count) return this.Yield(array);
  1182. }
  1183. if (array.length > 0) return this.Yield(array);
  1184. return false;
  1185. },
  1186. function () { Utils.Dispose(enumerator); })
  1187. });
  1188. },
  1189. /* Aggregate Methods */
  1190. // Overload:function(func)
  1191. // Overload:function(seed,func)
  1192. // Overload:function(seed,func,resultSelector)
  1193. Aggregate: function (seed, func, resultSelector)
  1194. {
  1195. return this.Scan(seed, func, resultSelector).Last();
  1196. },
  1197. // Overload:function()
  1198. // Overload:function(selector)
  1199. Average: function (selector)
  1200. {
  1201. selector = Utils.CreateLambda(selector);
  1202. var sum = 0;
  1203. var count = 0;
  1204. this.ForEach(function (x)
  1205. {
  1206. sum += selector(x);
  1207. ++count;
  1208. });
  1209. return sum / count;
  1210. },
  1211. // Overload:function()
  1212. // Overload:function(predicate)
  1213. Count: function (predicate)
  1214. {
  1215. predicate = (predicate == null) ? Functions.True : Utils.CreateLambda(predicate);
  1216. var count = 0;
  1217. this.ForEach(function (x, i)
  1218. {
  1219. if (predicate(x, i)) ++count;
  1220. });
  1221. return count;
  1222. },
  1223. // Overload:function()
  1224. // Overload:function(selector)
  1225. Max: function (selector)
  1226. {
  1227. if (selector == null) selector = Functions.Identity;
  1228. return this.Select(selector).Aggregate(function (a, b) { return (a > b) ? a : b; });
  1229. },
  1230. // Overload:function()
  1231. // Overload:function(selector)
  1232. Min: function (selector)
  1233. {
  1234. if (selector == null) selector = Functions.Identity;
  1235. return this.Select(selector).Aggregate(function (a, b) { return (a < b) ? a : b; });
  1236. },
  1237. MaxBy: function (keySelector)
  1238. {
  1239. keySelector = Utils.CreateLambda(keySelector);
  1240. return this.Aggregate(function (a, b) { return (keySelector(a) > keySelector(b)) ? a : b });
  1241. },
  1242. MinBy: function (keySelector)
  1243. {
  1244. keySelector = Utils.CreateLambda(keySelector);
  1245. return this.Aggregate(function (a, b) { return (keySelector(a) < keySelector(b)) ? a : b });
  1246. },
  1247. // Overload:function()
  1248. // Overload:function(selector)
  1249. Sum: function (selector)
  1250. {
  1251. if (selector == null) selector = Functions.Identity;
  1252. return this.Select(selector).Aggregate(0, function (a, b) { return a + b; });
  1253. },
  1254. /* Paging Methods */
  1255. ElementAt: function (index)
  1256. {
  1257. var value;
  1258. var found = false;
  1259. this.ForEach(function (x, i)
  1260. {
  1261. if (i == index)
  1262. {
  1263. value = x;
  1264. found = true;
  1265. return false;
  1266. }
  1267. });
  1268. if (!found) throw new Error("index is less than 0 or greater than or equal to the number of elements in source.");
  1269. return value;
  1270. },
  1271. ElementAtOrDefault: function (index, defaultValue)
  1272. {
  1273. var value;
  1274. var found = false;
  1275. this.ForEach(function (x, i)
  1276. {
  1277. if (i == index)
  1278. {
  1279. value = x;
  1280. found = true;
  1281. return false;
  1282. }
  1283. });
  1284. return (!found) ? defaultValue : value;
  1285. },
  1286. // Overload:function()
  1287. // Overload:function(predicate)
  1288. First: function (predicate)
  1289. {
  1290. if (predicate != null) return this.Where(predicate).First();
  1291. var value;
  1292. var found = false;
  1293. this.ForEach(function (x)
  1294. {
  1295. value = x;
  1296. found = true;
  1297. return false;
  1298. });
  1299. if (!found) throw new Error("First:No element satisfies the condition.");
  1300. return value;
  1301. },
  1302. // Overload:function(defaultValue)
  1303. // Overload:function(defaultValue,predicate)
  1304. FirstOrDefault: function (defaultValue, predicate)
  1305. {
  1306. if (predicate != null) return this.Where(predicate).FirstOrDefault(defaultValue);
  1307. var value;
  1308. var found = false;
  1309. this.ForEach(function (x)
  1310. {
  1311. value = x;
  1312. found = true;
  1313. return false;
  1314. });
  1315. return (!found) ? defaultValue : value;
  1316. },
  1317. // Overload:function()
  1318. // Overload:function(predicate)
  1319. Last: function (predicate)
  1320. {
  1321. if (predicate != null) return this.Where(predicate).Last();
  1322. var value;
  1323. var found = false;
  1324. this.ForEach(function (x)
  1325. {
  1326. found = true;
  1327. value = x;
  1328. });
  1329. if (!found) throw new Error("Last:No element satisfies the condition.");
  1330. return value;
  1331. },
  1332. // Overload:function(defaultValue)
  1333. // Overload:function(defaultValue,predicate)
  1334. LastOrDefault: function (defaultValue, predicate)
  1335. {
  1336. if (predicate != null) return this.Where(predicate).LastOrDefault(defaultValue);
  1337. var value;
  1338. var found = false;
  1339. this.ForEach(function (x)
  1340. {
  1341. found = true;
  1342. value = x;
  1343. });
  1344. return (!found) ? defaultValue : value;
  1345. },
  1346. // Overload:function()
  1347. // Overload:function(predicate)
  1348. Single: function (predicate)
  1349. {
  1350. if (predicate != null) return this.Where(predicate).Single();
  1351. var value;
  1352. var found = false;
  1353. this.ForEach(function (x)
  1354. {
  1355. if (!found)
  1356. {
  1357. found = true;
  1358. value = x;
  1359. }
  1360. else throw new Error("Single:sequence contains more than one element.");
  1361. });
  1362. if (!found) throw new Error("Single:No element satisfies the condition.");
  1363. return value;
  1364. },
  1365. // Overload:function(defaultValue)
  1366. // Overload:function(defaultValue,predicate)
  1367. SingleOrDefault: function (defaultValue, predicate)
  1368. {
  1369. if (predicate != null) return this.Where(predicate).SingleOrDefault(defaultValue);
  1370. var value;
  1371. var found = false;
  1372. this.ForEach(function (x)
  1373. {
  1374. if (!found)
  1375. {
  1376. found = true;
  1377. value = x;
  1378. }
  1379. else throw new Error("Single:sequence contains more than one element.");
  1380. });
  1381. return (!found) ? defaultValue : value;
  1382. },
  1383. Skip: function (count)
  1384. {
  1385. var source = this;
  1386. return new Enumerable(function ()
  1387. {
  1388. var enumerator;
  1389. var index = 0;
  1390. return new IEnumerator(
  1391. function ()
  1392. {
  1393. enumerator = source.GetEnumerator();
  1394. while (index++ < count && enumerator.MoveNext()) { };
  1395. },
  1396. function ()
  1397. {
  1398. return (enumerator.MoveNext())
  1399. ? this.Yield(enumerator.Current())
  1400. : false;
  1401. },
  1402. function () { Utils.Dispose(enumerator); })
  1403. });
  1404. },
  1405. // Overload:function(predicate<element>)
  1406. // Overload:function(predicate<element,index>)
  1407. SkipWhile: function (predicate)
  1408. {
  1409. predicate = Utils.CreateLambda(predicate);
  1410. var source = this;
  1411. return new Enumerable(function ()
  1412. {
  1413. var enumerator;
  1414. var index = 0;
  1415. var isSkipEnd = false;
  1416. return new IEnumerator(
  1417. function () { enumerator = source.GetEnumerator(); },
  1418. function ()
  1419. {
  1420. while (!isSkipEnd)
  1421. {
  1422. if (enumerator.MoveNext())
  1423. {
  1424. if (!predicate(enumerator.Current(), index++))
  1425. {
  1426. isSkipEnd = true;
  1427. return this.Yield(enumerator.Current());
  1428. }
  1429. continue;
  1430. }
  1431. else return false;
  1432. }
  1433. return (enumerator.MoveNext())
  1434. ? this.Yield(enumerator.Current())
  1435. : false;
  1436. },
  1437. function () { Utils.Dispose(enumerator); });
  1438. });
  1439. },
  1440. Take: function (count)
  1441. {
  1442. var source = this;
  1443. return new Enumerable(function ()
  1444. {
  1445. var enumerator;
  1446. var index = 0;
  1447. return new IEnumerator(
  1448. function () { enumerator = source.GetEnumerator(); },
  1449. function ()
  1450. {
  1451. return (index++ < count && enumerator.MoveNext())
  1452. ? this.Yield(enumerator.Current())
  1453. : false;
  1454. },
  1455. function () { Utils.Dispose(enumerator); }
  1456. )
  1457. });
  1458. },
  1459. // Overload:function(predicate<element>)
  1460. // Overload:function(predicate<element,index>)
  1461. TakeWhile: function (predicate)
  1462. {
  1463. predicate = Utils.CreateLambda(predicate);
  1464. var source = this;
  1465. return new Enumerable(function ()
  1466. {
  1467. var enumerator;
  1468. var index = 0;
  1469. return new IEnumerator(
  1470. function () { enumerator = source.GetEnumerator(); },
  1471. function ()
  1472. {
  1473. return (enumerator.MoveNext() && predicate(enumerator.Current(), index++))
  1474. ? this.Yield(enumerator.Current())
  1475. : false;
  1476. },
  1477. function () { Utils.Dispose(enumerator); });
  1478. });
  1479. },
  1480. // Overload:function()
  1481. // Overload:function(count)
  1482. TakeExceptLast: function (count)
  1483. {
  1484. if (count == null) count = 1;
  1485. var source = this;
  1486. return new Enumerable(function ()
  1487. {
  1488. if (count <= 0) return source.GetEnumerator(); // do nothing
  1489. var enumerator;
  1490. var q = [];
  1491. return new IEnumerator(
  1492. function () { enumerator = source.GetEnumerator(); },
  1493. function ()
  1494. {
  1495. while (enumerator.MoveNext())
  1496. {
  1497. if (q.length == count)
  1498. {
  1499. q.push(enumerator.Current());
  1500. return this.Yield(q.shift());
  1501. }
  1502. q.push(enumerator.Current());
  1503. }
  1504. return false;
  1505. },
  1506. function () { Utils.Dispose(enumerator); });
  1507. });
  1508. },
  1509. TakeFromLast: function (count)
  1510. {
  1511. if (count <= 0 || count == null) return Enumerable.Empty();
  1512. var source = this;
  1513. return new Enumerable(function ()
  1514. {
  1515. var sourceEnumerator;
  1516. var enumerator;
  1517. var q = [];
  1518. return new IEnumerator(
  1519. function () { sourceEnumerator = source.GetEnumerator(); },
  1520. function ()
  1521. {
  1522. while (sourceEnumerator.MoveNext())
  1523. {
  1524. if (q.length == count) q.shift()
  1525. q.push(sourceEnumerator.Current());
  1526. }
  1527. if (enumerator == null)
  1528. {
  1529. enumerator = Enumerable.From(q).GetEnumerator();
  1530. }
  1531. return (enumerator.MoveNext())
  1532. ? this.Yield(enumerator.Current())
  1533. : false;
  1534. },
  1535. function () { Utils.Dispose(enumerator); });
  1536. });
  1537. },
  1538. IndexOf: function (item)
  1539. {
  1540. var found = null;
  1541. this.ForEach(function (x, i)
  1542. {
  1543. if (x === item)
  1544. {
  1545. found = i;
  1546. return true;
  1547. }
  1548. });
  1549. return (found !== null) ? found : -1;
  1550. },
  1551. LastIndexOf: function (item)
  1552. {
  1553. var result = -1;
  1554. this.ForEach(function (x, i)
  1555. {
  1556. if (x === item) result = i;
  1557. });
  1558. return result;
  1559. },
  1560. /* Convert Methods */
  1561. ToArray: function ()
  1562. {
  1563. var array = [];
  1564. this.ForEach(function (x) { array.push(x) });
  1565. return array;
  1566. },
  1567. // Overload:function(keySelector)
  1568. // Overload:function(keySelector, elementSelector)
  1569. // Overload:function(keySelector, elementSelector, compareSelector)
  1570. ToLookup: function (keySelector, elementSelector, compareSelector)
  1571. {
  1572. keySelector = Utils.CreateLambda(keySelector);
  1573. elementSelector = Utils.CreateLambda(elementSelector);
  1574. compareSelector = Utils.CreateLambda(compareSelector);
  1575. var dict = new Dictionary(compareSelector);
  1576. this.ForEach(function (x)
  1577. {
  1578. var key = keySelector(x);
  1579. var element = elementSelector(x);
  1580. var array = dict.Get(key);
  1581. if (array !== undefined) array.push(element);
  1582. else dict.Add(key, [element]);
  1583. });
  1584. return new Lookup(dict);
  1585. },
  1586. ToObject: function (keySelector, elementSelector)
  1587. {
  1588. keySelector = Utils.CreateLambda(keySelector);
  1589. elementSelector = Utils.CreateLambda(elementSelector);
  1590. var obj = {};
  1591. this.ForEach(function (x)
  1592. {
  1593. obj[keySelector(x)] = elementSelector(x);
  1594. });
  1595. return obj;
  1596. },
  1597. // Overload:function(keySelector, elementSelector)
  1598. // Overload:function(keySelector, elementSelector, compareSelector)
  1599. ToDictionary: function (keySelector, elementSelector, compareSelector)
  1600. {
  1601. keySelector = Utils.CreateLambda(keySelector);
  1602. elementSelector = Utils.CreateLambda(elementSelector);
  1603. compareSelector = Utils.CreateLambda(compareSelector);
  1604. var dict = new Dictionary(compareSelector);
  1605. this.ForEach(function (x)
  1606. {
  1607. dict.Add(keySelector(x), elementSelector(x));
  1608. });
  1609. return dict;
  1610. },
  1611. // Overload:function()
  1612. // Overload:function(replacer)
  1613. // Overload:function(replacer, space)
  1614. ToJSON: function (replacer, space)
  1615. {
  1616. return JSON.stringify(this.ToArray(), replacer, space);
  1617. },
  1618. // Overload:function()
  1619. // Overload:function(separator)
  1620. // Overload:function(separator,selector)
  1621. ToString: function (separator, selector)
  1622. {
  1623. if (separator == null) separator = "";
  1624. if (selector == null) selector = Functions.Identity;
  1625. return this.Select(selector).ToArray().join(separator);
  1626. },
  1627. /* Action Methods */
  1628. // Overload:function(action<element>)
  1629. // Overload:function(action<element,index>)
  1630. Do: function (action)
  1631. {
  1632. var source = this;
  1633. action = Utils.CreateLambda(action);
  1634. return new Enumerable(function ()
  1635. {
  1636. var enumerator;
  1637. var index = 0;
  1638. return new IEnumerator(
  1639. function () { enumerator = source.GetEnumerator(); },
  1640. function ()
  1641. {
  1642. if (enumerator.MoveNext())
  1643. {
  1644. action(enumerator.Current(), index++);
  1645. return this.Yield(enumerator.Current());
  1646. }
  1647. return false;
  1648. },
  1649. function () { Utils.Dispose(enumerator); });
  1650. });
  1651. },
  1652. // Overload:function(action<element>)
  1653. // Overload:function(action<element,index>)
  1654. // Overload:function(func<element,bool>)
  1655. // Overload:function(func<element,index,bool>)
  1656. ForEach: function (action)
  1657. {
  1658. action = Utils.CreateLambda(action);
  1659. var index = 0;
  1660. var enumerator = this.GetEnumerator();
  1661. try
  1662. {
  1663. while (enumerator.MoveNext())
  1664. {
  1665. if (action(enumerator.Current(), index++) === false) break;
  1666. }
  1667. }
  1668. finally { Utils.Dispose(enumerator); }
  1669. },
  1670. // Overload:function()
  1671. // Overload:function(separator)
  1672. // Overload:function(separator,selector)
  1673. Write: function (separator, selector)
  1674. {
  1675. if (separator == null) separator = "";
  1676. selector = Utils.CreateLambda(selector);
  1677. var isFirst = true;
  1678. this.ForEach(function (item)
  1679. {
  1680. if (isFirst) isFirst = false;
  1681. else document.write(separator);
  1682. document.write(selector(item));
  1683. });
  1684. },
  1685. // Overload:function()
  1686. // Overload:function(selector)
  1687. WriteLine: function (selector)
  1688. {
  1689. selector = Utils.CreateLambda(selector);
  1690. this.ForEach(function (item)
  1691. {
  1692. document.write(selector(item));
  1693. document.write("<br />");
  1694. });
  1695. },
  1696. Force: function ()
  1697. {
  1698. var enumerator = this.GetEnumerator();
  1699. try { while (enumerator.MoveNext()) { } }
  1700. finally { Utils.Dispose(enumerator); }
  1701. },
  1702. /* Functional Methods */
  1703. Let: function (func)
  1704. {
  1705. func = Utils.CreateLambda(func);
  1706. var source = this;
  1707. return new Enumerable(function ()
  1708. {
  1709. var enumerator;
  1710. return new IEnumerator(
  1711. function ()
  1712. {
  1713. enumerator = Enumerable.From(func(source)).GetEnumerator();
  1714. },
  1715. function ()
  1716. {
  1717. return (enumerator.MoveNext())
  1718. ? this.Yield(enumerator.Current())
  1719. : false;
  1720. },
  1721. function () { Utils.Dispose(enumerator); })
  1722. });
  1723. },
  1724. Share: function ()
  1725. {
  1726. var source = this;
  1727. var sharedEnumerator;
  1728. return new Enumerable(function ()
  1729. {
  1730. return new IEnumerator(
  1731. function ()
  1732. {
  1733. if (sharedEnumerator == null)
  1734. {
  1735. sharedEnumerator = source.GetEnumerator();
  1736. }
  1737. },
  1738. function ()
  1739. {
  1740. return (sharedEnumerator.MoveNext())
  1741. ? this.Yield(sharedEnumerator.Current())
  1742. : false;
  1743. },
  1744. Functions.Blank
  1745. )
  1746. });
  1747. },
  1748. MemoizeAll: function ()
  1749. {
  1750. var source = this;
  1751. var cache;
  1752. var enumerator;
  1753. return new Enumerable(function ()
  1754. {
  1755. var index = -1;
  1756. return new IEnumerator(
  1757. function ()
  1758. {
  1759. if (enumerator == null)
  1760. {
  1761. enumerator = source.GetEnumerator();
  1762. cache = [];
  1763. }
  1764. },
  1765. function ()
  1766. {
  1767. index++;
  1768. if (cache.length <= index)
  1769. {
  1770. return (enumerator.MoveNext())
  1771. ? this.Yield(cache[index] = enumerator.Current())
  1772. : false;
  1773. }
  1774. return this.Yield(cache[index]);
  1775. },
  1776. Functions.Blank
  1777. )
  1778. });
  1779. },
  1780. /* Error Handling Methods */
  1781. Catch: function (handler)
  1782. {
  1783. handler = Utils.CreateLambda(handler);
  1784. var source = this;
  1785. return new Enumerable(function ()
  1786. {
  1787. var enumerator;
  1788. return new IEnumerator(
  1789. function () { enumerator = source.GetEnumerator(); },
  1790. function ()
  1791. {
  1792. try
  1793. {
  1794. return (enumerator.MoveNext())
  1795. ? this.Yield(enumerator.Current())
  1796. : false;
  1797. }
  1798. catch (e)
  1799. {
  1800. handler(e);
  1801. return false;
  1802. }
  1803. },
  1804. function () { Utils.Dispose(enumerator); });
  1805. });
  1806. },
  1807. Finally: function (finallyAction)
  1808. {
  1809. finallyAction = Utils.CreateLambda(finallyAction);
  1810. var source = this;
  1811. return new Enumerable(function ()
  1812. {
  1813. var enumerator;
  1814. return new IEnumerator(
  1815. function () { enumerator = source.GetEnumerator(); },
  1816. function ()
  1817. {
  1818. return (enumerator.MoveNext())
  1819. ? this.Yield(enumerator.Current())
  1820. : false;
  1821. },
  1822. function ()
  1823. {
  1824. try { Utils.Dispose(enumerator); }
  1825. finally { finallyAction(); }
  1826. });
  1827. });
  1828. },
  1829. /* For Debug Methods */
  1830. // Overload:function()
  1831. // Overload:function(message)
  1832. // Overload:function(message,selector)
  1833. Trace: function (message, selector)
  1834. {
  1835. if (message == null) message = "Trace";
  1836. selector = Utils.CreateLambda(selector);
  1837. return this.Do(function (item)
  1838. {
  1839. console.log(message, ":", selector(item));
  1840. });
  1841. }
  1842. }
  1843. // private
  1844. // static functions
  1845. var Functions =
  1846. {
  1847. Identity: function (x) { return x; },
  1848. True: function () { return true; },
  1849. Blank: function () { }
  1850. }
  1851. // static const
  1852. var Types =
  1853. {
  1854. Boolean: typeof true,
  1855. Number: typeof 0,
  1856. String: typeof "",
  1857. Object: typeof {},
  1858. Undefined: typeof undefined,
  1859. Function: typeof function () { }
  1860. }
  1861. // static utility methods
  1862. var Utils =
  1863. {
  1864. // Create anonymous function from lambda expression string
  1865. CreateLambda: function (expression)
  1866. {
  1867. if (expression == null) return Functions.Identity;
  1868. if (typeof expression == Types.String)
  1869. {
  1870. if (expression == "")
  1871. {
  1872. return Functions.Identity;
  1873. }
  1874. else if (expression.indexOf("=>") == -1)
  1875. {
  1876. return new Function("$,$$,$$$,$$$$", "return " + expression);
  1877. }
  1878. else
  1879. {
  1880. var expr = expression.match(/^[(\s]*([^()]*?)[)\s]*=>(.*)/);
  1881. return new Function(expr[1], "return " + expr[2]);
  1882. }
  1883. }
  1884. return expression;
  1885. },
  1886. IsIEnumerable: function (obj)
  1887. {
  1888. if (typeof Enumerator != Types.Undefined)
  1889. {
  1890. try
  1891. {
  1892. new Enumerator(obj);
  1893. return true;
  1894. }
  1895. catch (e) { }
  1896. }
  1897. return false;
  1898. },
  1899. Compare: function (a, b)
  1900. {
  1901. return (a === b) ? 0
  1902. : (a > b) ? 1
  1903. : -1;
  1904. },
  1905. Dispose: function (obj)
  1906. {
  1907. if (obj != null) obj.Dispose();
  1908. }
  1909. }
  1910. // IEnumerator State
  1911. var State = { Before: 0, Running: 1, After: 2 }
  1912. // name "Enumerator" is conflict JScript's "Enumerator"
  1913. var IEnumerator = function (initialize, tryGetNext, dispose)
  1914. {
  1915. var yielder = new Yielder();
  1916. var state = State.Before;
  1917. this.Current = yielder.Current;
  1918. this.MoveNext = function ()
  1919. {
  1920. try
  1921. {
  1922. switch (state)
  1923. {
  1924. case State.Before:
  1925. state = State.Running;
  1926. initialize(); // fall through
  1927. case State.Running:
  1928. if (tryGetNext.apply(yielder))
  1929. {
  1930. return true;
  1931. }
  1932. else
  1933. {
  1934. this.Dispose();
  1935. return false;
  1936. }
  1937. case State.After:
  1938. return false;
  1939. }
  1940. }
  1941. catch (e)
  1942. {
  1943. this.Dispose();
  1944. throw e;
  1945. }
  1946. }
  1947. this.Dispose = function ()
  1948. {
  1949. if (state != State.Running) return;
  1950. try { dispose(); }
  1951. finally { state = State.After; }
  1952. }
  1953. }
  1954. // for tryGetNext
  1955. var Yielder = function ()
  1956. {
  1957. var current = null;
  1958. this.Current = function () { return current; }
  1959. this.Yield = function (value)
  1960. {
  1961. current = value;
  1962. return true;
  1963. }
  1964. }
  1965. // for OrderBy/ThenBy
  1966. var OrderedEnumerable = function (source, keySelector, descending, parent)
  1967. {
  1968. this.source = source;
  1969. this.keySelector = Utils.CreateLambda(keySelector);
  1970. this.descending = descending;
  1971. this.parent = parent;
  1972. }
  1973. OrderedEnumerable.prototype = new Enumerable();
  1974. OrderedEnumerable.prototype.CreateOrderedEnumerable = function (keySelector, descending)
  1975. {
  1976. return new OrderedEnumerable(this.source, keySelector, descending, this);
  1977. }
  1978. OrderedEnumerable.prototype.ThenBy = function (keySelector)
  1979. {
  1980. return this.CreateOrderedEnumerable(keySelector, false);
  1981. }
  1982. OrderedEnumerable.prototype.ThenByDescending = function (keySelector)
  1983. {
  1984. return this.CreateOrderedEnumerable(keySelector, true);
  1985. }
  1986. OrderedEnumerable.prototype.GetEnumerator = function ()
  1987. {
  1988. var self = this;
  1989. var buffer;
  1990. var indexes;
  1991. var index = 0;
  1992. return new IEnumerator(
  1993. function ()
  1994. {
  1995. buffer = [];
  1996. indexes = [];
  1997. self.source.ForEach(function (item, index)
  1998. {
  1999. buffer.push(item);
  2000. indexes.push(index);
  2001. });
  2002. var sortContext = SortContext.Create(self, null);
  2003. sortContext.GenerateKeys(buffer);
  2004. indexes.sort(function (a, b) { return sortContext.Compare(a, b); });
  2005. },
  2006. function ()
  2007. {
  2008. return (index < indexes.length)
  2009. ? this.Yield(buffer[indexes[index++]])
  2010. : false;
  2011. },
  2012. Functions.Blank
  2013. )
  2014. }
  2015. var SortContext = function (keySelector, descending, child)
  2016. {
  2017. this.keySelector = keySelector;
  2018. this.descending = descending;
  2019. this.child = child;
  2020. this.keys = null;
  2021. }
  2022. SortContext.Create = function (orderedEnumerable, currentContext)
  2023. {
  2024. var context = new SortContext(orderedEnumerable.keySelector, orderedEnumerable.descending, currentContext);
  2025. if (orderedEnumerable.parent != null) return SortContext.Create(orderedEnumerable.parent, context);
  2026. return context;
  2027. }
  2028. SortContext.prototype.GenerateKeys = function (source)
  2029. {
  2030. var len = source.length;
  2031. var keySelector = this.keySelector;
  2032. var keys = new Array(len);
  2033. for (var i = 0; i < len; i++) keys[i] = keySelector(source[i]);
  2034. this.keys = keys;
  2035. if (this.child != null) this.child.GenerateKeys(source);
  2036. }
  2037. SortContext.prototype.Compare = function (index1, index2)
  2038. {
  2039. var comparison = Utils.Compare(this.keys[index1], this.keys[index2]);
  2040. if (comparison == 0)
  2041. {
  2042. if (this.child != null) return this.child.Compare(index1, index2)
  2043. comparison = Utils.Compare(index1, index2);
  2044. }
  2045. return (this.descending) ? -comparison : comparison;
  2046. }
  2047. // optimize array or arraylike object
  2048. var ArrayEnumerable = function (source)
  2049. {
  2050. this.source = source;
  2051. }
  2052. ArrayEnumerable.prototype = new Enumerable();
  2053. ArrayEnumerable.prototype.Any = function (predicate)
  2054. {
  2055. return (predicate == null)
  2056. ? (this.source.length > 0)
  2057. : Enumerable.prototype.Any.apply(this, arguments);
  2058. }
  2059. ArrayEnumerable.prototype.Count = function (predicate)
  2060. {
  2061. return (predicate == null)
  2062. ? this.source.length
  2063. : Enumerable.prototype.Count.apply(this, arguments);
  2064. }
  2065. ArrayEnumerable.prototype.ElementAt = function (index)
  2066. {
  2067. return (0 <= index && index < this.source.length)
  2068. ? this.source[index]
  2069. : Enumerable.prototype.ElementAt.apply(this, arguments);
  2070. }
  2071. ArrayEnumerable.prototype.ElementAtOrDefault = function (index, defaultValue)
  2072. {
  2073. return (0 <= index && index < this.source.length)
  2074. ? this.source[index]
  2075. : defaultValue;
  2076. }
  2077. ArrayEnumerable.prototype.First = function (predicate)
  2078. {
  2079. return (predicate == null && this.source.length > 0)
  2080. ? this.source[0]
  2081. : Enumerable.prototype.First.apply(this, arguments);
  2082. }
  2083. ArrayEnumerable.prototype.FirstOrDefault = function (defaultValue, predicate)
  2084. {
  2085. if (predicate != null)
  2086. {
  2087. return Enumerable.prototype.FirstOrDefault.apply(this, arguments);
  2088. }
  2089. return this.source.length > 0 ? this.source[0] : defaultValue;
  2090. }
  2091. ArrayEnumerable.prototype.Last = function (predicate)
  2092. {
  2093. return (predicate == null && this.source.length > 0)
  2094. ? this.source[this.source.length - 1]
  2095. : Enumerable.prototype.Last.apply(this, arguments);
  2096. }
  2097. ArrayEnumerable.prototype.LastOrDefault = function (defaultValue, predicate)
  2098. {
  2099. if (predicate != null)
  2100. {
  2101. return Enumerable.prototype.LastOrDefault.apply(this, arguments);
  2102. }
  2103. return this.source.length > 0 ? this.source[this.source.length - 1] : defaultValue;
  2104. }
  2105. ArrayEnumerable.prototype.Skip = function (count)
  2106. {
  2107. var source = this.source;
  2108. return new Enumerable(function ()
  2109. {
  2110. var index;
  2111. return new IEnumerator(
  2112. function () { index = (count < 0) ? 0 : count },
  2113. function ()
  2114. {
  2115. return (index < source.length)
  2116. ? this.Yield(source[index++])
  2117. : false;
  2118. },
  2119. Functions.Blank);
  2120. });
  2121. };
  2122. ArrayEnumerable.prototype.TakeExceptLast = function (count)
  2123. {
  2124. if (count == null) count = 1;
  2125. return this.Take(this.source.length - count);
  2126. }
  2127. ArrayEnumerable.prototype.TakeFromLast = function (count)
  2128. {
  2129. return this.Skip(this.source.length - count);
  2130. }
  2131. ArrayEnumerable.prototype.Reverse = function ()
  2132. {
  2133. var source = this.source;
  2134. return new Enumerable(function ()
  2135. {
  2136. var index;
  2137. return new IEnumerator(
  2138. function ()
  2139. {
  2140. index = source.length;
  2141. },
  2142. function ()
  2143. {
  2144. return (index > 0)
  2145. ? this.Yield(source[--index])
  2146. : false;
  2147. },
  2148. Functions.Blank)
  2149. });
  2150. }
  2151. ArrayEnumerable.prototype.SequenceEqual = function (second, compareSelector)
  2152. {
  2153. if ((second instanceof ArrayEnumerable || second instanceof Array)
  2154. && compareSelector == null
  2155. && Enumerable.From(second).Count() != this.Count())
  2156. {
  2157. return false;
  2158. }
  2159. return Enumerable.prototype.SequenceEqual.apply(this, arguments);
  2160. }
  2161. ArrayEnumerable.prototype.ToString = function (separator, selector)
  2162. {
  2163. if (selector != null || !(this.source instanceof Array))
  2164. {
  2165. return Enumerable.prototype.ToString.apply(this, arguments);
  2166. }
  2167. if (separator == null) separator = "";
  2168. return this.source.join(separator);
  2169. }
  2170. ArrayEnumerable.prototype.GetEnumerator = function ()
  2171. {
  2172. var source = this.source;
  2173. var index = 0;
  2174. return new IEnumerator(
  2175. Functions.Blank,
  2176. function ()
  2177. {
  2178. return (index < source.length)
  2179. ? this.Yield(source[index++])
  2180. : false;
  2181. },
  2182. Functions.Blank);
  2183. }
  2184. // Collections
  2185. var Dictionary = (function ()
  2186. {
  2187. // static utility methods
  2188. var HasOwnProperty = function (target, key)
  2189. {
  2190. return Object.prototype.hasOwnProperty.call(target, key);
  2191. }
  2192. var ComputeHashCode = function (obj)
  2193. {
  2194. if (obj === null) return "null";
  2195. if (obj === undefined) return "undefined";
  2196. return (typeof obj.toString === Types.Function)
  2197. ? obj.toString()
  2198. : Object.prototype.toString.call(obj);
  2199. }
  2200. // LinkedList for Dictionary
  2201. var HashEntry = function (key, value)
  2202. {
  2203. this.Key = key;
  2204. this.Value = value;
  2205. this.Prev = null;
  2206. this.Next = null;
  2207. }
  2208. var EntryList = function ()
  2209. {
  2210. this.First = null;
  2211. this.Last = null;
  2212. }
  2213. EntryList.prototype =
  2214. {
  2215. AddLast: function (entry)
  2216. {
  2217. if (this.Last != null)
  2218. {
  2219. this.Last.Next = entry;
  2220. entry.Prev = this.Last;
  2221. this.Last = entry;
  2222. }
  2223. else this.First = this.Last = entry;
  2224. },
  2225. Replace: function (entry, newEntry)
  2226. {
  2227. if (entry.Prev != null)
  2228. {
  2229. entry.Prev.Next = newEntry;
  2230. newEntry.Prev = entry.Prev;
  2231. }
  2232. else this.First = newEntry;
  2233. if (entry.Next != null)
  2234. {
  2235. entry.Next.Prev = newEntry;
  2236. newEntry.Next = entry.Next;
  2237. }
  2238. else this.Last = newEntry;
  2239. },
  2240. Remove: function (entry)
  2241. {
  2242. if (entry.Prev != null) entry.Prev.Next = entry.Next;
  2243. else this.First = entry.Next;
  2244. if (entry.Next != null) entry.Next.Prev = entry.Prev;
  2245. else this.Last = entry.Prev;
  2246. }
  2247. }
  2248. // Overload:function()
  2249. // Overload:function(compareSelector)
  2250. var Dictionary = function (compareSelector)
  2251. {
  2252. this.count = 0;
  2253. this.entryList = new EntryList();
  2254. this.buckets = {}; // as Dictionary<string,List<object>>
  2255. this.compareSelector = (compareSelector == null) ? Functions.Identity : compareSelector;
  2256. }
  2257. Dictionary.prototype =
  2258. {
  2259. Add: function (key, value)
  2260. {
  2261. var compareKey = this.compareSelector(key);
  2262. var hash = ComputeHashCode(compareKey);
  2263. var entry = new HashEntry(key, value);
  2264. if (HasOwnProperty(this.buckets, hash))
  2265. {
  2266. var array = this.buckets[hash];
  2267. for (var i = 0; i < array.length; i++)
  2268. {
  2269. if (this.compareSelector(array[i].Key) === compareKey)
  2270. {
  2271. this.entryList.Replace(array[i], entry);
  2272. array[i] = entry;
  2273. return;
  2274. }
  2275. }
  2276. array.push(entry);
  2277. }
  2278. else
  2279. {
  2280. this.buckets[hash] = [entry];
  2281. }
  2282. this.count++;
  2283. this.entryList.AddLast(entry);
  2284. },
  2285. Get: function (key)
  2286. {
  2287. var compareKey = this.compareSelector(key);
  2288. var hash = ComputeHashCode(compareKey);
  2289. if (!HasOwnProperty(this.buckets, hash)) return undefined;
  2290. var array = this.buckets[hash];
  2291. for (var i = 0; i < array.length; i++)
  2292. {
  2293. var entry = array[i];
  2294. if (this.compareSelector(entry.Key) === compareKey) return entry.Value;
  2295. }
  2296. return undefined;
  2297. },
  2298. Set: function (key, value)
  2299. {
  2300. var compareKey = this.compareSelector(key);
  2301. var hash = ComputeHashCode(compareKey);
  2302. if (HasOwnProperty(this.buckets, hash))
  2303. {
  2304. var array = this.buckets[hash];
  2305. for (var i = 0; i < array.length; i++)
  2306. {
  2307. if (this.compareSelector(array[i].Key) === compareKey)
  2308. {
  2309. var newEntry = new HashEntry(key, value);
  2310. this.entryList.Replace(array[i], newEntry);
  2311. array[i] = newEntry;
  2312. return true;
  2313. }
  2314. }
  2315. }
  2316. return false;
  2317. },
  2318. Contains: function (key)
  2319. {
  2320. var compareKey = this.compareSelector(key);
  2321. var hash = ComputeHashCode(compareKey);
  2322. if (!HasOwnProperty(this.buckets, hash)) return false;
  2323. var array = this.buckets[hash];
  2324. for (var i = 0; i < array.length; i++)
  2325. {
  2326. if (this.compareSelector(array[i].Key) === compareKey) return true;
  2327. }
  2328. return false;
  2329. },
  2330. Clear: function ()
  2331. {
  2332. this.count = 0;
  2333. this.buckets = {};
  2334. this.entryList = new EntryList();
  2335. },
  2336. Remove: function (key)
  2337. {
  2338. var compareKey = this.compareSelector(key);
  2339. var hash = ComputeHashCode(compareKey);
  2340. if (!HasOwnProperty(this.buckets, hash)) return;
  2341. var array = this.buckets[hash];
  2342. for (var i = 0; i < array.length; i++)
  2343. {
  2344. if (this.compareSelector(array[i].Key) === compareKey)
  2345. {
  2346. this.entryList.Remove(array[i]);
  2347. array.splice(i, 1);
  2348. if (array.length == 0) delete this.buckets[hash];
  2349. this.count--;
  2350. return;
  2351. }
  2352. }
  2353. },
  2354. Count: function ()
  2355. {
  2356. return this.count;
  2357. },
  2358. ToEnumerable: function ()
  2359. {
  2360. var self = this;
  2361. return new Enumerable(function ()
  2362. {
  2363. var currentEntry;
  2364. return new IEnumerator(
  2365. function () { currentEntry = self.entryList.First },
  2366. function ()
  2367. {
  2368. if (currentEntry != null)
  2369. {
  2370. var result = { Key: currentEntry.Key, Value: currentEntry.Value };
  2371. currentEntry = currentEntry.Next;
  2372. return this.Yield(result);
  2373. }
  2374. return false;
  2375. },
  2376. Functions.Blank);
  2377. });
  2378. }
  2379. }
  2380. return Dictionary;
  2381. })();
  2382. // dictionary = Dictionary<TKey, TValue[]>
  2383. var Lookup = function (dictionary)
  2384. {
  2385. this.Count = function ()
  2386. {
  2387. return dictionary.Count();
  2388. }
  2389. this.Get = function (key)
  2390. {
  2391. return Enumerable.From(dictionary.Get(key));
  2392. }
  2393. this.Contains = function (key)
  2394. {
  2395. return dictionary.Contains(key);
  2396. }
  2397. this.ToEnumerable = function ()
  2398. {
  2399. return dictionary.ToEnumerable().Select(function (kvp)
  2400. {
  2401. return new Grouping(kvp.Key, kvp.Value);
  2402. });
  2403. }
  2404. }
  2405. var Grouping = function (key, elements)
  2406. {
  2407. this.Key = function ()
  2408. {
  2409. return key;
  2410. }
  2411. ArrayEnumerable.call(this, elements);
  2412. }
  2413. Grouping.prototype = new ArrayEnumerable();
  2414. // out to global
  2415. return Enumerable;
  2416. })()