-
Notifications
You must be signed in to change notification settings - Fork 12
/
zonesNormalize.ts
130 lines (117 loc) · 3.43 KB
/
zonesNormalize.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
/* eslint-disable max-lines-per-function */
import { FromTo } from 'cheminfo-types';
export interface ZonesNormalizeOptions {
/** specify min value of zones
* @default Number.NEGATIVE_INFINITY
*/
from?: number;
/** specify max value of zones
* @default Number.POSITIVE_INFINITY
*/
to?: number;
/**
* List of exclusion zones
*/
exclusions?: FromTo[];
}
/**
* Normalize an array of zones:
* - ensure than from < to
* - merge overlapping zones
* - deal with exclusions zones
* - if no zones is specified add one between -Infinity and +Infinity
* @param zones - array of zones
* @param options - options
* @returns array of zones
*/
export function zonesNormalize(
zones: FromTo[] = [],
options: ZonesNormalizeOptions = {},
): FromTo[] {
const { exclusions = [] } = options;
let { from = Number.NEGATIVE_INFINITY, to = Number.POSITIVE_INFINITY } =
options;
if (from > to) [from, to] = [to, from];
zones = zones
.map((zone: FromTo) =>
zone.from > zone.to ? { from: zone.to, to: zone.from } : { ...zone },
)
.sort((a, b) => {
if (a.from !== b.from) return a.from - b.from;
return a.to - b.to;
});
if (zones.length === 0) {
zones.push({ from, to });
}
for (const zone of zones) {
if (from > zone.from) zone.from = from;
if (to < zone.to) zone.to = to;
}
zones = zones.filter((zone) => zone.from <= zone.to);
if (zones.length === 0) return [];
let currentZone = zones[0];
const beforeExclusionsZones = [currentZone];
for (let i = 1; i < zones.length; i++) {
const zone = zones[i];
if (zone.from <= currentZone.to) {
if (currentZone.to < zone.to) {
currentZone.to = zone.to;
}
} else {
currentZone = zone;
beforeExclusionsZones.push(currentZone);
}
}
if (exclusions.length === 0) return beforeExclusionsZones;
const normalizedExclusions = zonesNormalize(exclusions);
let currentExclusionIndex = 0;
const results: FromTo[] = [];
for (
let zoneIndex = 0;
zoneIndex < beforeExclusionsZones.length;
zoneIndex++
) {
const zone = beforeExclusionsZones[zoneIndex];
if (currentExclusionIndex === normalizedExclusions.length) {
// we analysed all the exclusion zones
results.push(zone);
continue;
}
while (
currentExclusionIndex < normalizedExclusions.length &&
normalizedExclusions[currentExclusionIndex].to <= zone.from
) {
currentExclusionIndex++;
}
if (currentExclusionIndex === normalizedExclusions.length) {
// we analysed all the exclusion zones
results.push(zone);
continue;
}
if (zone.to < normalizedExclusions[currentExclusionIndex].from) {
// no problems, not yet in exclusion
results.push(zone);
continue;
}
if (normalizedExclusions[currentExclusionIndex].to >= zone.to) {
// could be totally excluded
if (normalizedExclusions[currentExclusionIndex].from <= zone.from) {
continue;
}
results.push({
from: normalizedExclusions[currentExclusionIndex].to,
to: zone.to,
});
}
// we cut in the middle, we need to create more zones, annoying !
if (normalizedExclusions[currentExclusionIndex].from > zone.from) {
results.push({
from: zone.from,
to: normalizedExclusions[currentExclusionIndex].from,
});
}
zone.from = normalizedExclusions[currentExclusionIndex].to;
zoneIndex--;
}
return results;
}