@@ -1199,7 +1199,7 @@ public int[] successorArray() {
1199
1199
final int numPairs = this .numPairs ;
1200
1200
// Neither quicksort nor heaps are stable, so we reestablish order here.
1201
1201
IntArrays .quickSort (successor , 0 , numPairs );
1202
- if (numPairs != 0 ) {
1202
+ if (numPairs != 0 ) {
1203
1203
int p = 0 ;
1204
1204
for (int j = 1 ; j < numPairs ; j ++) if (successor [p ] != successor [j ]) successor [++p ] = successor [j ];
1205
1205
outdegree = p + 1 ;
@@ -1311,6 +1311,8 @@ class InternalArcLabelledNodeIterator extends ArcLabelledNodeIterator {
1311
1311
private int last ;
1312
1312
/** The outdegree of the current node (valid if {@link #last} is not -1). */
1313
1313
private int outdegree ;
1314
+ /** The number of pairs associated with the current node (valid if {@link #last} is not -1). */
1315
+ private int numPairs ;
1314
1316
/** The successors of the current node (valid if {@link #last} is not -1);
1315
1317
* only the first {@link #outdegree} entries are meaningful. */
1316
1318
private int [] successor ;
@@ -1319,7 +1321,7 @@ class InternalArcLabelledNodeIterator extends ArcLabelledNodeIterator {
1319
1321
private Label [] label ;
1320
1322
1321
1323
public InternalArcLabelledNodeIterator (final int upperBound ) throws IOException {
1322
- this (upperBound , null , null , null , null , null , -1 , 0 , IntArrays .EMPTY_ARRAY , Label .EMPTY_LABEL_ARRAY );
1324
+ this (upperBound , null , null , null , null , null , -1 , - 1 , IntArrays .EMPTY_ARRAY , Label .EMPTY_LABEL_ARRAY );
1323
1325
}
1324
1326
1325
1327
public InternalArcLabelledNodeIterator (final int upperBound , final InputBitStream [] baseIbs , final InputBitStream [] baseLabelInputBitStream , final int [] refArray , final int [] prevTarget , final int [] inputStreamLength , final int last , final int outdegree , final int successor [], final Label [] label ) throws IOException {
@@ -1377,8 +1379,10 @@ public boolean hasNext() {
1377
1379
1378
1380
@ Override
1379
1381
public int nextInt () {
1382
+ if (! hasNext ()) throw new NoSuchElementException ();
1380
1383
last ++;
1381
1384
int d = 0 ;
1385
+ outdegree = -1 ;
1382
1386
int i ;
1383
1387
1384
1388
try {
@@ -1395,8 +1399,8 @@ public int nextInt() {
1395
1399
if (--inputStreamLength [i ] == 0 ) {
1396
1400
queue .dequeue ();
1397
1401
batchIbs [i ].close ();
1398
- labelInputBitStream [i ].close ();
1399
1402
batchIbs [i ] = null ;
1403
+ labelInputBitStream [i ].close ();
1400
1404
labelInputBitStream [i ] = null ;
1401
1405
}
1402
1406
else {
@@ -1410,8 +1414,19 @@ public int nextInt() {
1410
1414
}
1411
1415
d ++;
1412
1416
}
1417
+
1418
+ numPairs = d ;
1419
+ }
1420
+ catch (final IOException e ) {
1421
+ e .printStackTrace ();
1422
+ throw new RuntimeException (this + " " + e );
1423
+ }
1424
+
1425
+ // Compute outdegree
1426
+ if (outdegree == -1 ) {
1427
+ final int numPairs = this .numPairs ;
1413
1428
// Neither quicksort nor heaps are stable, so we reestablish order here.
1414
- it .unimi .dsi .fastutil .Arrays .quickSort (0 , d , (x , y ) -> Integer .compare (successor [x ], successor [y ]),
1429
+ it .unimi .dsi .fastutil .Arrays .quickSort (0 , numPairs , (x , y ) -> Integer .compare (successor [x ], successor [y ]),
1415
1430
(x , y ) -> {
1416
1431
final int t = successor [x ];
1417
1432
successor [x ] = successor [y ];
@@ -1420,12 +1435,16 @@ public int nextInt() {
1420
1435
label [x ] = label [y ];
1421
1436
label [y ] = l ;
1422
1437
});
1423
- }
1424
- catch (final IOException e ) {
1425
- throw new RuntimeException (e );
1438
+
1439
+ if (numPairs != 0 ) {
1440
+ // Avoid returning the duplicate arcs
1441
+ int p = 0 ;
1442
+ for (int j = 1 ; j < numPairs ; j ++) if (successor [p ] != successor [j ]) successor [++p ] = successor [j ];
1443
+ outdegree = p + 1 ;
1444
+ }
1445
+ else outdegree = 0 ;
1426
1446
}
1427
1447
1428
- outdegree = d ;
1429
1448
return last ;
1430
1449
}
1431
1450
@@ -1604,6 +1623,12 @@ public static int processTransposeBatch(final int n, final int[] source, final i
1604
1623
batchFile .deleteOnExit ();
1605
1624
batches .add (batchFile );
1606
1625
final OutputBitStream batch = new OutputBitStream (batchFile );
1626
+
1627
+ final File labelFile = File .createTempFile ("label-" , ".bits" , tempDir );
1628
+ labelFile .deleteOnExit ();
1629
+ labelBatches .add (labelFile );
1630
+ final OutputBitStream labelObs = new OutputBitStream (labelFile );
1631
+
1607
1632
int u = 0 ;
1608
1633
1609
1634
if (n != 0 ) {
@@ -1616,32 +1641,35 @@ public static int processTransposeBatch(final int n, final int[] source, final i
1616
1641
batch .writeDelta (prevSource );
1617
1642
batch .writeDelta (target [0 ]);
1618
1643
1644
+ labelBitStream .position (start [0 ]);
1645
+ prototype .fromBitStream (labelBitStream , source [0 ]);
1646
+ prototype .toBitStream (labelObs , target [0 ]);
1647
+
1619
1648
for (int i = 1 ; i < n ; i ++) {
1620
1649
if (source [i ] != prevSource ) {
1621
1650
batch .writeDelta (source [i ] - prevSource );
1622
1651
batch .writeDelta (target [i ]);
1623
1652
prevSource = source [i ];
1653
+
1654
+ labelBitStream .position (start [i ]);
1655
+ prototype .fromBitStream (labelBitStream , source [i ]);
1656
+ prototype .toBitStream (labelObs , target [i ]);
1624
1657
}
1625
1658
else if (target [i ] != target [i - 1 ]) {
1626
1659
// We don't write duplicate pairs
1627
1660
batch .writeDelta (0 );
1628
1661
batch .writeDelta (target [i ] - target [i - 1 ] - 1 );
1662
+
1663
+ labelBitStream .position (start [i ]);
1664
+ prototype .fromBitStream (labelBitStream , source [i ]);
1665
+ prototype .toBitStream (labelObs , target [i ]);
1629
1666
}
1630
1667
}
1631
1668
}
1669
+
1632
1670
else batch .writeDelta (0 );
1633
1671
1634
1672
batch .close ();
1635
-
1636
- final File labelFile = File .createTempFile ("label-" , ".bits" , tempDir );
1637
- labelFile .deleteOnExit ();
1638
- labelBatches .add (labelFile );
1639
- final OutputBitStream labelObs = new OutputBitStream (labelFile );
1640
- for (int i = 0 ; i < n ; i ++) {
1641
- labelBitStream .position (start [i ]);
1642
- prototype .fromBitStream (labelBitStream , source [i ]);
1643
- prototype .toBitStream (labelObs , target [i ]);
1644
- }
1645
1673
labelObs .close ();
1646
1674
1647
1675
return u ;
0 commit comments