Browse Source

mol-data: fixed SortedArray.union

David Sehnal 5 years ago
parent
commit
22a8758852
2 changed files with 18 additions and 2 deletions
  1. 14 0
      src/mol-data/int/_spec/sorted-array.spec.ts
  2. 4 2
      src/mol-data/int/impl/sorted-array.ts

+ 14 - 0
src/mol-data/int/_spec/sorted-array.spec.ts

@@ -65,6 +65,20 @@ describe('sortedArray', () => {
 
     test('intersectionSize', SortedArray.intersectionSize(a1234, a2468), 2);
 
+    it('union1', () => {
+        compareArrays(
+            SortedArray.union(SortedArray.ofSortedArray([830, 831, 832, 833, 834, 836, 837, 838, 839, 840, 841, 842, 843]), SortedArray.ofSortedArray([835])),
+            SortedArray.ofSortedArray([830, 831, 832, 833, 834, 835, 836, 837, 838, 839, 840, 841, 842, 843])
+        )
+    })
+
+    it('union2', () => {
+        compareArrays(
+            SortedArray.union(SortedArray.ofSortedArray([830, 832, 833]), SortedArray.ofSortedArray([831])),
+            SortedArray.ofSortedArray([830, 831, 832, 833])
+        )
+    })
+
     // console.log(Interval.findPredecessorIndexInInterval(Interval.ofBounds(0, 3), 2, Interval.ofBounds(0, 3)))
     // console.log(SortedArray.findPredecessorIndexInInterval(SortedArray.ofSortedArray([0, 1, 2]), 2, Interval.ofBounds(0, 3)))
 });

+ 4 - 2
src/mol-data/int/impl/sorted-array.ts

@@ -187,8 +187,10 @@ export function union(a: Nums, b: Nums) {
     let offset = 0;
 
     // insert the "prefixes"
-    for (let k = 0; k < startI; k++) indices[offset++] = a[k];
-    for (let k = 0; k < startJ; k++) indices[offset++] = b[k];
+    let k = 0;
+    for (k = 0; k < startI; k++) indices[offset++] = a[k];
+    k = 0;
+    while (k <= startJ && a[startI] > b[k]) indices[offset++] = b[k++];
 
     // insert the common part
     let i = startI;