-
Notifications
You must be signed in to change notification settings - Fork 11
/
LInQer.GroupEnumerable.ts
178 lines (168 loc) · 5.36 KB
/
LInQer.GroupEnumerable.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/// <reference path="./LInQer.Slim.ts" />
namespace Linqer {
export interface Enumerable extends Iterable<any> {
/**
* Groups the elements of a sequence.
*
* @param {ISelector} keySelector
* @returns {Enumerable}
* @memberof Enumerable
*/
groupBy(keySelector: ISelector): Enumerable;
/**
* Correlates the elements of two sequences based on key equality and groups the results. A specified equalityComparer is used to compare keys.
* WARNING: using the equality comparer will be slower
*
* @param {IterableType} iterable
* @param {ISelector} innerKeySelector
* @param {ISelector} outerKeySelector
* @param {(item1: any, item2: any) => any} resultSelector
* @param {IEqualityComparer} equalityComparer
* @returns {Enumerable}
* @memberof Enumerable
*/
groupJoin(iterable: IterableType,
innerKeySelector: ISelector,
outerKeySelector: ISelector,
resultSelector: (item1: any, item2: any) => any,
equalityComparer: IEqualityComparer): Enumerable;
/**
* Correlates the elements of two sequences based on matching keys.
* WARNING: using the equality comparer will be slower
*
* @param {IterableType} iterable
* @param {ISelector} innerKeySelector
* @param {ISelector} outerKeySelector
* @param {(item1: any, item2: any) => any} resultSelector
* @param {IEqualityComparer} equalityComparer
* @returns {Enumerable}
* @memberof Enumerable
*/
join(iterable: IterableType,
innerKeySelector: ISelector,
outerKeySelector: ISelector,
resultSelector: (item1: any, item2: any) => any,
equalityComparer: IEqualityComparer): Enumerable;
toLookup(): never;
}
/// Groups the elements of a sequence.
Enumerable.prototype.groupBy = function (keySelector: ISelector): Enumerable {
_ensureFunction(keySelector);
const self: Enumerable = this;
const gen = function* () {
const groupMap = new Map<any, any>();
let index = 0;
// iterate all items and group them in a Map
for (const item of self) {
const key = keySelector(item, index);
const group = groupMap.get(key);
if (group) {
group.push(item);
} else {
groupMap.set(key, [item]);
}
index++;
}
// then yield a GroupEnumerable for each group
for (const [key, items] of groupMap) {
const group = new GroupEnumerable(items, key);
yield group;
}
};
const result = new Enumerable(gen);
return result;
}
/// Correlates the elements of two sequences based on key equality and groups the results. A specified equalityComparer is used to compare keys.
/// WARNING: using the equality comparer will be slower
Enumerable.prototype.groupJoin = function (iterable: IterableType,
innerKeySelector: ISelector,
outerKeySelector: ISelector,
resultSelector: (item1: any, item2: any) => any,
equalityComparer: IEqualityComparer = EqualityComparer.default): Enumerable {
const self: Enumerable = this;
const gen = equalityComparer === EqualityComparer.default
? function* () {
const lookup = new Enumerable(iterable)
.groupBy(outerKeySelector)
.toMap(g => g.key, g => g);
let index = 0;
for (const innerItem of self) {
const arr = _toArray(lookup.get(innerKeySelector(innerItem, index)));
yield resultSelector(innerItem, arr);
index++;
}
}
: function* () {
let innerIndex = 0;
for (const innerItem of self) {
const arr = [];
let outerIndex = 0;
for (const outerItem of Enumerable.from(iterable)) {
if (equalityComparer(innerKeySelector(innerItem, innerIndex), outerKeySelector(outerItem, outerIndex))) {
arr.push(outerItem);
}
outerIndex++;
}
yield resultSelector(innerItem, arr);
innerIndex++;
}
};
return new Enumerable(gen);
}
/// Correlates the elements of two sequences based on matching keys.
/// WARNING: using the equality comparer will be slower
Enumerable.prototype.join = function (iterable: IterableType,
innerKeySelector: ISelector,
outerKeySelector: ISelector,
resultSelector: (item1: any, item2: any) => any,
equalityComparer: IEqualityComparer = EqualityComparer.default): Enumerable {
const self: Enumerable = this;
const gen = equalityComparer === EqualityComparer.default
? function* () {
const lookup = new Enumerable(iterable)
.groupBy(outerKeySelector)
.toMap(g => g.key, g => g);
let index = 0;
for (const innerItem of self) {
const group = lookup.get(innerKeySelector(innerItem, index));
if (group) {
for (const outerItem of group) {
yield resultSelector(innerItem, outerItem);
}
}
index++;
}
}
: function* () {
let innerIndex = 0;
for (const innerItem of self) {
let outerIndex = 0;
for (const outerItem of Enumerable.from(iterable)) {
if (equalityComparer(innerKeySelector(innerItem, innerIndex), outerKeySelector(outerItem, outerIndex))) {
yield resultSelector(innerItem, outerItem);
}
outerIndex++;
}
innerIndex++;
}
};
return new Enumerable(gen);
}
Enumerable.prototype.toLookup = function (): never {
throw new Error('use groupBy instead of toLookup');
}
/**
* An Enumerable that also exposes a group key
*
* @export
* @class GroupEnumerable
* @extends {Enumerable}
*/
export class GroupEnumerable extends Enumerable {
key: string;
constructor(iterable: IterableType, key: string) {
super(iterable);
this.key = key;
}
}
}