-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
merge.ts
181 lines (171 loc) · 7.44 KB
/
merge.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
179
180
181
//================================================================
/**
* @packageDocumentation
* @module std.ranges
*/
//================================================================
import * as base from "../../algorithm/merge";
import { IBidirectionalContainer } from "../container/IBidirectionalContainer";
import { IForwardContainer } from "../container/IForwardContainer";
import { IForwardIterator } from "../../iterator/IForwardIterator";
import { Writeonly } from "../../internal/functional/Writeonly";
import { begin, end } from "../../iterator/factory";
import { size } from "../../iterator/global";
import { less } from "../../functional/comparators";
import { Comparator } from "../../internal/functional/Comparator";
import { Temporary } from "../../internal/functional/Temporary";
/* ---------------------------------------------------------
MERGE
--------------------------------------------------------- */
/**
* Merge two sorted ranges.
*
* @param range1 The 1st iterable ranged container.
* @param range2 The 2nd iterable ranged container.
* @param output Output iterator of the first position.
* @param comp A binary function predicates *x* element would be placed before *y*. When returns `true`, then *x* precedes *y*. Default is {@link less}.
*
* @return Output Iterator of the last position by advancing.
*/
export function merge<
Range1 extends Array<any> | IForwardContainer<any>,
Range2 extends IForwardContainer.SimilarType<Range1>,
OutputIterator extends Writeonly<IForwardIterator<IForwardContainer.ValueType<Range1>, OutputIterator>>>
(
range1: Range1,
range2: Range2,
output: OutputIterator,
comp: Comparator<IForwardContainer.ValueType<Range1>> = less
): OutputIterator
{
return base.merge(begin(range1), end(range1), <Temporary>begin(range2), end(range2), output, comp);
}
/**
* Merge two sorted & consecutive ranges.
*
* @param range An iterable ranged container.
* @param middle Bidirectional iterator of the initial position of the 2nd range.
* @param comp A binary function predicates *x* element would be placed before *y*. When returns `true`, then *x* precedes *y*. Default is {@link less}.
*/
export function inplace_merge<Range extends Array<any> | IBidirectionalContainer<any, any>>
(
range: Range,
middle: IBidirectionalContainer.IteratorType<Range>,
comp: Comparator<IForwardContainer.ValueType<Range>> = less): void
{
return base.inplace_merge(begin(range), <any>middle, end(range), comp);
}
/* ---------------------------------------------------------
SET OPERATIONS
--------------------------------------------------------- */
/**
* Test whether two sorted ranges are in inclusion relationship.
*
* @param range1 The 1st iterable ranged container.
* @param range2 The 2nd iterable ranged container.
* @param comp A binary function predicates *x* element would be placed before *y*. When returns `true`, then *x* precedes *y*. Default is {@link less}.
*
* @return Whether [first, last1) includes [first2, last2).
*/
export function includes<
Range1 extends Array<any> | IForwardContainer<any>,
Range2 extends IForwardContainer.SimilarType<Range1>>
(range1: Range1, range2: Range2, comp: Comparator<IForwardContainer.ValueType<Range1>> = less): boolean
{
if (size(range1) < size(range2))
return false;
else
return base.includes(begin(range1), end(range1), <Temporary>begin(range2), end(range2), comp);
}
/**
* Combine two sorted ranges to union relationship.
*
* @param range1 The 1st iterable ranged container.
* @param range2 The 2nd iterable ranged container.
* @param output Output iterator of the first position.
* @param comp A binary function predicates *x* element would be placed before *y*. When returns `true`, then *x* precedes *y*. Default is {@link less}.
*
* @return Output Iterator of the last position by advancing.
*/
export function set_union<
Range1 extends Array<any> | IForwardContainer<any>,
Range2 extends IForwardContainer.SimilarType<Range1>,
OutputIterator extends Writeonly<IForwardIterator<IForwardContainer.ValueType<Range1>, OutputIterator>>>
(
range1: Range1,
range2: Range2,
output: OutputIterator,
comp: Comparator<IForwardContainer.ValueType<Range1>> = less
): OutputIterator
{
return base.set_union(begin(range1), end(range1), <Temporary>begin(range2), end(range2), output, comp);
}
/**
* Combine two sorted ranges to intersection relationship.
*
* @param range1 The 1st iterable ranged container.
* @param range2 The 2nd iterable ranged container.
* @param output Output iterator of the first position.
* @param comp A binary function predicates *x* element would be placed before *y*. When returns `true`, then *x* precedes *y*. Default is {@link less}.
*
* @return Output Iterator of the last position by advancing.
*/
export function set_intersection<
Range1 extends Array<any> | IForwardContainer<any>,
Range2 extends IForwardContainer.SimilarType<Range1>,
OutputIterator extends Writeonly<IForwardIterator<IForwardContainer.ValueType<Range1>, OutputIterator>>>
(
range1: Range1,
range2: Range2,
output: OutputIterator,
comp: Comparator<IForwardContainer.ValueType<Range1>> = less
): OutputIterator
{
return base.set_intersection(begin(range1), end(range1), <Temporary>begin(range2), end(range2), output, comp);
}
/**
* Combine two sorted ranges to difference relationship.
*
* @param range1 The 1st iterable ranged container.
* @param range2 The 2nd iterable ranged container.
* @param output Output iterator of the first position.
* @param comp A binary function predicates *x* element would be placed before *y*. When returns `true`, then *x* precedes *y*. Default is {@link less}.
*
* @return Output Iterator of the last position by advancing.
*/
export function set_difference<
Range1 extends Array<any> | IForwardContainer<any>,
Range2 extends IForwardContainer.SimilarType<Range1>,
OutputIterator extends Writeonly<IForwardIterator<IForwardContainer.ValueType<Range1>, OutputIterator>>>
(
range1: Range1,
range2: Range2,
output: OutputIterator,
comp: Comparator<IForwardContainer.ValueType<Range1>> = less
): OutputIterator
{
return base.set_difference(begin(range1), end(range1), <Temporary>begin(range2), end(range2), output, comp);
}
/**
* Combine two sorted ranges to symmetric difference relationship.
*
* @param range1 The 1st iterable ranged container.
* @param range2 The 2nd iterable ranged container.
* @param output Output iterator of the first position.
* @param comp A binary function predicates *x* element would be placed before *y*. When returns `true`, then *x* precedes *y*. Default is {@link less}.
*
* @return Output Iterator of the last position by advancing.
*/
export function set_symmetric_difference<
Range1 extends Array<any> | IForwardContainer<any>,
Range2 extends IForwardContainer.SimilarType<Range1>,
OutputIterator extends Writeonly<IForwardIterator<IForwardContainer.ValueType<Range1>, OutputIterator>>>
(
range1: Range1,
range2: Range2,
output: OutputIterator,
comp: Comparator<IForwardContainer.ValueType<Range1>> = less
): OutputIterator
{
return base.set_symmetric_difference(begin(range1), end(range1), <Temporary>begin(range2), end(range2), output, comp);
}