@@ -34,11 +34,21 @@ export class ObjectComparator<T extends NodeInput> implements BaseComparator<T,
34
34
*/
35
35
private currentValues : T [ ] = [ ] ;
36
36
37
+ /**
38
+ * Stores the current nodes that have been used in a perfect match.
39
+ */
40
+ private readonly usedCurrentValueNodeIds = new Set < number > ( ) ;
41
+
37
42
/**
38
43
* Stores the next nodes to compare.
39
44
*/
40
45
private nextValues : T [ ] = [ ] ;
41
46
47
+ /**
48
+ * Stores the next nodes that have been used in a perfect match.
49
+ */
50
+ private readonly usedNextValueNodeIds = new Set < number > ( ) ;
51
+
42
52
/**
43
53
* Threshold for next best match.
44
54
*/
@@ -91,17 +101,19 @@ export class ObjectComparator<T extends NodeInput> implements BaseComparator<T,
91
101
console . log ( 'Starting next best match search for objects' ) ;
92
102
currentValuesLength = this . currentValues . length ;
93
103
let bestMatches = [ ] ;
104
+ const filteredCurrentValues = this . currentValues . filter ( value => ! this . usedCurrentValueNodeIds . has ( value . nodeId ) ) ;
105
+ const filteredNextValues = this . nextValues . filter ( value => ! this . usedNextValueNodeIds . has ( value . nodeId ) ) ;
94
106
if ( this . threads <= 1 ) {
95
- for ( const [ i , value ] of this . currentValues . entries ( ) ) {
96
- bestMatches . push ( ...this . findNextBestMatches ( value ) ) ;
107
+ for ( const [ i , value ] of filteredCurrentValues . entries ( ) ) {
108
+ bestMatches . push ( ...this . findNextBestMatches ( value , filteredNextValues ) ) ;
97
109
98
110
if ( i % 2500 === 0 ) {
99
111
this . debug ( ) ;
100
112
console . log ( `Progress: ${ ( i / currentValuesLength * 100 ) . toFixed ( 2 ) } %` ) ;
101
113
}
102
114
}
103
115
} else {
104
- const bestMatchesHub = new NextBestFitHub ( this . currentValues , this . nextValues , { threads : this . threads , threshold : this . threshold } ) ;
116
+ const bestMatchesHub = new NextBestFitHub ( filteredCurrentValues , filteredNextValues , { threads : this . threads , threshold : this . threshold } ) ;
105
117
bestMatches = await bestMatchesHub . runComparison ( ) ;
106
118
}
107
119
@@ -122,6 +134,10 @@ export class ObjectComparator<T extends NodeInput> implements BaseComparator<T,
122
134
*/
123
135
private findPerfectMatch ( currentValue : NodeInput ) : boolean {
124
136
const perfectMatch = this . nextValues . find ( nextValue => {
137
+ if ( this . usedNextValueNodeIds . has ( nextValue . nodeId ) ) {
138
+ return false ;
139
+ }
140
+
125
141
try {
126
142
return deepEqual ( currentValue . node . obj , nextValue . node . obj ) ;
127
143
} catch {
@@ -139,8 +155,8 @@ export class ObjectComparator<T extends NodeInput> implements BaseComparator<T,
139
155
aggregatorReference . currentNodeId . add ( currentValue . nodeId ) ;
140
156
aggregatorReference . nextNodeId . add ( perfectMatch . nodeId ) ;
141
157
this . results . perfectMatchNodes . set ( valueHash , aggregatorReference ) ;
142
- this . currentValues = this . currentValues . filter ( _currentValue => _currentValue . nodeId !== currentValue . nodeId ) ;
143
- this . nextValues = this . nextValues . filter ( nextValue => nextValue . nodeId !== perfectMatch . nodeId ) ;
158
+ this . usedCurrentValueNodeIds . add ( currentValue . nodeId ) ;
159
+ this . usedNextValueNodeIds . add ( perfectMatch . nodeId ) ;
144
160
return true ;
145
161
}
146
162
@@ -152,8 +168,12 @@ export class ObjectComparator<T extends NodeInput> implements BaseComparator<T,
152
168
*
153
169
* @param currentValue
154
170
*/
155
- private findNextBestMatches ( currentValue : NodeInput ) : FuzzyEqualSimilarity [ ] {
156
- return this . nextValues . map ( nextValue => {
171
+ private findNextBestMatches ( currentValue : NodeInput , nextValues : NodeInput [ ] ) : FuzzyEqualSimilarity [ ] {
172
+ return nextValues . map ( nextValue => {
173
+ if ( this . usedNextValueNodeIds . has ( nextValue . nodeId ) ) {
174
+ return { similarity : 0 , currentValueNodeId : 0 , nextValueNodeId : 0 } ;
175
+ }
176
+
157
177
try {
158
178
const totalSimilarity : FuzzyEqualComparison = fuzzyEqual ( currentValue . node . obj , nextValue . node . obj ) ;
159
179
if ( totalSimilarity . propertyCount === 0 ) {
@@ -198,10 +218,9 @@ export class ObjectComparator<T extends NodeInput> implements BaseComparator<T,
198
218
aggregatorReference . nextNodeId . add ( nextBestMatch . nextValueNodeId ) ;
199
219
usedCurrentNodes . add ( nextBestMatch . currentValueNodeId ) ;
200
220
usedNextNodes . add ( nextBestMatch . nextValueNodeId ) ;
221
+ this . usedCurrentValueNodeIds . add ( nextBestMatch . currentValueNodeId ) ;
222
+ this . usedNextValueNodeIds . add ( nextBestMatch . nextValueNodeId ) ;
201
223
similarityAggregatorReference . set ( valueHash , aggregatorReference ) ;
202
-
203
- this . currentValues = this . currentValues . filter ( _currentValue => _currentValue . nodeId !== nextBestMatch . currentValueNodeId ) ;
204
- this . nextValues = this . nextValues . filter ( nextValue => nextValue . nodeId !== nextBestMatch . nextValueNodeId ) ;
205
224
}
206
225
}
207
226
@@ -210,10 +229,18 @@ export class ObjectComparator<T extends NodeInput> implements BaseComparator<T,
210
229
*/
211
230
private fillDisjunctNodes ( ) : void {
212
231
for ( const currentValue of this . currentValues ) {
232
+ if ( this . usedCurrentValueNodeIds . has ( currentValue . nodeId ) ) {
233
+ continue ;
234
+ }
235
+
213
236
this . results . disjunctNodes . currentNodeId . add ( currentValue . nodeId ) ;
214
237
}
215
238
216
239
for ( const nextValue of this . nextValues ) {
240
+ if ( this . usedNextValueNodeIds . has ( nextValue . nodeId ) ) {
241
+ continue ;
242
+ }
243
+
217
244
this . results . disjunctNodes . nextNodeId . add ( nextValue . nodeId ) ;
218
245
}
219
246
@@ -243,6 +270,6 @@ export class ObjectComparator<T extends NodeInput> implements BaseComparator<T,
243
270
console . log ( 'Perfect matches:' , perfectMatchCounter ) ;
244
271
console . log ( 'Next best nodes:' , nextBestMatchCounter ) ;
245
272
console . log ( 'Disjunct nodes:' , { current : this . results . disjunctNodes . currentNodeId . size , next : this . results . disjunctNodes . nextNodeId . size } ) ;
246
- console . log ( 'Available nodes:' , { current : this . currentValues . length , next : this . nextValues . length } ) ;
273
+ console . log ( 'Available nodes:' , { current : this . currentValues . length - this . usedCurrentValueNodeIds . size , next : this . nextValues . length - this . usedNextValueNodeIds . size } ) ;
247
274
}
248
275
}
0 commit comments