blu 0.2.1

LU factorization with dynamic Markowitz search and columnwise threshold pivoting
Documentation
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
// Copyright (C) 2016-2019 ERGO-Code
// Copyright (C) 2022-2023 Richard Lincoln

use crate::lu::def::*;
use crate::lu::file::*;
use crate::lu::list::*;
use crate::lu::LU;
use crate::LUInt;
use crate::Status;
use std::time::Instant;

// The maximum number of off-diagonal elements in the pivot column handled
// by `pivot_small()`. `pivot_small()` uses `i64` integers
// for bit masking. Since each row to be updated requires one bit, the routine
// can handle pivot operations for up to 64 rows (excluding pivot row).
//
// Since int64_t is optional in the C99 standard, using it limits portability
// of the code. However, using a fixed threshold to switch between
// pivot_small() and pivot_any() guarantees identical pivot operations
// on all architectures. If int64_t does not exist, then the user can adapt it
// by hand and is aware of it.
const MAXROW_SMALL: usize = 64;

// Pivot elimination from active submatrix
//
// pivot() is the only routine callable from extern. It branches out the
// implementation of the pivot operation. The pivot operation removes row
// this.pivot_row and column this.pivot_col from the active submatrix and
// applies a rank-1 update to the remaining active submatrix. It updates the
// row and column counts and the column maxima.
//
// Each pivot elimination adds one column to L and one row to U. On entry
// Lbegin_p[rank] and Ubegin[rank] must point to the next position in Lindex,
// Lvalue respectively Uindex, Uvalue. On return the column in L is terminated
// by index -1 and Lbegin_p[rank+1] and Ubegin[rank+1] point to the next free
// position.
//
// The routines check if memory is sufficient and request reallocation before
// manipulating data structures otherwise.
//
// Updating the columns of the active submatrix is implemented like in the
// Coin-OR code Clp (J. Forrest) by compressing unmodified entries and then
// appending the entries updated or filled-in by the pivot column. Compared
// to the technique from the Suhl/Suhl paper, this method often produces
// sparser factors. I assume the reason is tie breaking in the Markowitz
// search and that in Forrest's method updated elements are moved to the end
// of the column (and likewise for rows).
pub(crate) fn pivot(lu: &mut LU) -> Status {
    let m = lu.m;
    let rank = lu.rank;
    let l_mem = lu.l_mem;
    let u_mem = lu.u_mem;
    let pivot_col = lu.pivot_col.unwrap();
    let pivot_row = lu.pivot_row.unwrap();
    // let colmax = &lu.col_pivot;
    let l_begin_p = &lu.l_begin_p;
    // let Ubegin = &lu.Ubegin;
    let w_begin = &lu.w_begin;
    let w_end = &lu.w_end;
    // let Uindex = lu.Uindex.as_ref().unwrap();
    let nz_col = (w_end[pivot_col] - w_begin[pivot_col]) as usize;
    let nz_row = (w_end[m + pivot_row] - w_begin[m + pivot_row]) as usize;

    let mut status = Status::OK;
    let tic = Instant::now();

    assert!(nz_row >= 1);
    assert!(nz_col >= 1);

    // Check if room is available in L and U.
    let room = l_mem - l_begin_p[rank] as usize;
    let need = nz_col; // # off-diagonals in pivot col + end marker (-1)
    if room < need {
        lu.addmem_l = need - room;
        status = Status::Reallocate;
    }
    let room = u_mem - lu.u_begin[rank] as usize;
    let need = nz_row - 1; // # off-diagonals in pivot row
    if room < need {
        lu.addmem_u = need - room;
        status = Status::Reallocate;
    }
    if status != Status::OK {
        return status;
    }

    // Branch out implementation of pivot operation.
    if nz_row == 1 {
        status = pivot_singleton_row(lu);
    } else if nz_col == 1 {
        status = pivot_singleton_col(lu);
    } else if nz_col == 2 {
        status = pivot_doubleton_col(lu);
    } else if nz_col - 1 <= MAXROW_SMALL {
        status = pivot_small(lu);
    } else {
        status = pivot_any(lu);
    }

    // Remove all entries in columns whose maximum entry has dropped below
    // absolute pivot tolerance.
    if status == Status::OK {
        for pos in lu.u_begin[rank]..lu.u_begin[rank + 1] {
            let j = lu.u_index[pos as usize] as usize;
            assert_ne!(j, pivot_col);
            if lu.col_pivot[j] == 0.0 || lu.col_pivot[j] < lu.abstol {
                remove_col(lu, j);
            }
        }
    }

    lu.factor_flops += (nz_col - 1) * (nz_row - 1);
    lu.time_elim_pivot += tic.elapsed().as_secs_f64();
    status
}

fn pivot_any(lu: &mut LU) -> Status {
    let m = lu.m;
    let rank = lu.rank;
    let droptol = lu.droptol;
    let pad = lu.pad;
    let stretch = lu.stretch;
    let pivot_col = lu.pivot_col.unwrap();
    let pivot_row = lu.pivot_row.unwrap();
    let colcount_flink = &mut lu.colcount_flink;
    let colcount_blink = &mut lu.colcount_blink;
    let rowcount_flink = &mut lu.rowcount_flink;
    let rowcount_blink = &mut lu.rowcount_blink;
    let colmax = &mut lu.col_pivot;
    let l_begin_p = &mut lu.l_begin_p;
    let u_begin = &mut lu.u_begin;
    let w_begin = &mut lu.w_begin;
    let w_end = &mut lu.w_end;
    let w_flink = &mut lu.w_flink;
    let w_blink = &mut lu.w_blink;
    let l_index = &mut lu.l_index;
    let l_value = &mut lu.l_value;
    let u_index = &mut lu.u_index;
    let u_value = &mut lu.u_value;
    let w_index = &mut lu.w_index;
    let w_value = &mut lu.w_value;
    let marked = &mut lu.iwork0;
    let work = &mut lu.work0;

    let mut cbeg = w_begin[pivot_col] as usize; // changed by file compression
    let mut cend = w_end[pivot_col] as usize;
    let mut rbeg = w_begin[m + pivot_row] as usize;
    let mut rend = w_end[m + pivot_row] as usize;
    let cnz1 = cend - cbeg - 1; // nz in pivot column except pivot
    let rnz1 = rend - rbeg - 1; // nz in pivot row except pivot

    // lu_int i, j, pos, pos1, rpos, put, Uput, where, nz, *wi;
    // lu_int grow, room, found, position;
    // double pivot, a, x, cmx, xrj, *wx;

    // Check if room is available in W. At most each updated row and each
    // updated column will be reappended and filled-in with rnz1 respectively
    // cnz1 elements. Move pivot to the front of pivot row and pivot column.
    let mut grow = 0;
    let mut where_: Option<usize> = None;
    for pos in cbeg..cend {
        // if ((i = Windex[pos]) == pivot_row) TODO: check
        let i = w_index[pos] as usize;
        if i == pivot_row {
            where_ = Some(pos);
        } else {
            let nz = (w_end[m + i] - w_begin[m + i]) as usize;
            grow += nz + rnz1 + (stretch * (nz + rnz1) as f64) as usize + pad;
        }
    }
    assert!(where_.is_some());
    iswap(w_index, cbeg, where_.unwrap());
    fswap(w_value, cbeg, where_.unwrap());
    let pivot = w_value[cbeg as usize];
    assert_ne!(pivot, 0.0);
    let mut where_: Option<usize> = None;
    for rpos in rbeg..rend {
        // if ((j = Windex[rpos]) == pivot_col) TODO: check
        let j = w_index[rpos as usize] as usize;
        if j == pivot_col {
            where_ = Some(rpos);
        } else {
            let nz = (w_end[j] - w_begin[j]) as usize;
            grow += nz + cnz1 + (stretch * (nz + cnz1) as f64) as usize + pad;
        }
    }
    assert!(where_.is_some());
    iswap(w_index, rbeg, where_.unwrap());
    let mut room = (w_end[2 * m] - w_begin[2 * m]) as usize;
    if grow > room {
        file_compress(
            2 * m,
            w_begin,
            w_end,
            w_flink,
            w_index,
            w_value,
            stretch,
            pad,
        );
        cbeg = w_begin[pivot_col] as usize;
        cend = w_end[pivot_col] as usize;
        rbeg = w_begin[m + pivot_row] as usize;
        rend = w_end[m + pivot_row] as usize;
        room = (w_end[2 * m] - w_begin[2 * m]) as usize;
        lu.ngarbage += 1;
    }
    if grow > room {
        lu.addmem_w = grow - room;
        return Status::Reallocate;
    }

    // get pointer to U
    let mut u_put = u_begin[rank] as usize;
    // assert!(u_put >= 0);
    assert!(u_put < lu.u_mem);

    // Column file update //

    // For each row i to be updated set marked[i] > 0 to its position
    // in the (packed) pivot column.
    let mut position = 1;
    for pos in (cbeg + 1)..cend {
        let i = w_index[pos as usize] as usize;
        marked[i] = position;
        position += 1;
    }

    // let wi = Windex + cbeg;
    // let wx = Wvalue + cbeg;
    for rpos in (rbeg + 1)..rend {
        let j = w_index[rpos as usize] as usize;
        assert_ne!(j, pivot_col);
        let mut cmx = 0.0; // column maximum

        // Compress unmodified column entries. Store entries to be updated
        // in workspace. Move pivot row entry to the front of column.
        let mut where_: Option<usize> = None;
        let mut put = w_begin[j] as usize;
        let pos1 = w_begin[j] as usize;
        for pos in pos1..w_end[j] as usize {
            let i = w_index[pos] as usize;
            // if ((position = marked[i]) > 0) {
            position = marked[i as usize];
            if position > 0 {
                assert_ne!(i, pivot_row);
                work[position as usize] = w_value[pos];
            } else {
                assert_eq!(position, 0);
                let x = w_value[pos].abs();
                if i == pivot_row {
                    where_ = Some(put);
                // } else if ((x = fabs(Wvalue[pos])) > cmx) {
                } else if x > cmx {
                    cmx = x;
                }
                w_index[put] = w_index[pos];
                w_value[put] = w_value[pos];
                put += 1;
            }
        }
        assert!(where_.is_some());
        w_end[j as usize] = put as LUInt;
        iswap(w_index, pos1, where_.unwrap());
        fswap(w_value, pos1, where_.unwrap());
        let xrj = w_value[pos1 as usize]; // pivot row entry

        // Reappend column if no room for update.
        let mut room = w_begin[w_flink[j] as usize] as usize - put;
        if room < cnz1 {
            let nz = (w_end[j] - w_begin[j]) as usize;
            room = cnz1 + (stretch * (nz + cnz1) as f64) as usize + pad;
            file_reappend(
                j,
                2 * m,
                w_begin,
                w_end,
                w_flink,
                w_blink,
                w_index,
                w_value,
                room,
            );
            put = w_end[j] as usize;
            assert_eq!(w_begin[w_flink[j] as usize] as usize - put, room);
            lu.nexpand += 1;
        }

        // Compute update in workspace and append to column.
        let a = xrj / pivot;
        for pos in 1..=cnz1 {
            // work[pos as usize] -= a * wx[pos as usize];
            work[pos as usize] -= a * w_value[cbeg as usize..][pos as usize];
        }
        for pos in 1..=cnz1 {
            // Windex[put as usize] = wi[pos as usize];
            w_index[put] = w_index[cbeg as usize..][pos as usize];
            w_value[put] = work[pos as usize];
            put += 1;
            // if ((x = fabs(work[pos])) > cmx) {
            let x = work[pos as usize].abs();
            if x > cmx {
                cmx = x;
            }
            work[pos as usize] = 0.0;
        }
        w_end[j] = put as LUInt;

        // Write pivot row entry to U and remove from file.
        if xrj.abs() > droptol {
            assert!(u_put < lu.u_mem);
            u_index[u_put] = j as LUInt;
            u_value[u_put] = xrj;
            u_put += 1;
        }
        assert_eq!(w_index[w_begin[j] as usize] as usize, pivot_row);
        w_begin[j] += 1;

        // Move column to new list and update min_colnz.
        let nz = (w_end[j] - w_begin[j]) as usize;
        list_move(
            j,
            nz,
            colcount_flink,
            colcount_blink,
            m,
            Some(&mut lu.min_colnz),
        );

        colmax[j as usize] = cmx;
    }
    for pos in (cbeg + 1)..cend {
        marked[w_index[pos as usize] as usize] = 0;
    }

    // Row file update //

    for rpos in rbeg..rend {
        marked[w_index[rpos as usize] as usize] = 1;
    }
    assert_eq!(marked[pivot_col], 1);

    for pos in (cbeg + 1)..cend {
        let i = w_index[pos as usize] as usize;
        assert_ne!(i, pivot_row);

        // Compress unmodified row entries (not marked). Remove
        // overlap with pivot row, including pivot column entry.
        let mut found = 0;
        let mut put = w_begin[m + i] as usize;
        for rpos in w_begin[m + i]..w_end[m + i] {
            let j = w_index[rpos as usize] as usize;
            if j == pivot_col {
                found = 1;
            }
            if marked[j] == 0 {
                w_index[put] = j as LUInt;
                put += 1;
            }
        }
        assert_ne!(found, 0);
        w_end[m + i] = put as LUInt;

        // Reappend row if no room for update. Append pattern of pivot row.
        room = w_begin[w_flink[m + i] as usize] as usize - put;
        if room < rnz1 {
            let nz = (w_end[m + i] - w_begin[m + i]) as usize;
            room = rnz1 + (stretch * (nz + rnz1) as f64) as usize + pad;
            file_reappend(
                m + i,
                2 * m,
                w_begin,
                w_end,
                w_flink,
                w_blink,
                w_index,
                w_value,
                room,
            );
            put = w_end[m + i] as usize;
            assert_eq!(w_begin[w_flink[m + i] as usize] as usize - put, room);
            lu.nexpand += 1;
        }
        for rpos in (rbeg + 1)..rend {
            w_index[put] = w_index[rpos as usize];
            put += 1;
        }
        w_end[m + i] = put as LUInt;

        // Move to new list. The row must be reinserted even if nz are
        // unchanged since it might have been taken out in Markowitz search.
        let nz = (w_end[m + i] - w_begin[m + i]) as usize;
        list_move(
            i,
            nz,
            rowcount_flink,
            rowcount_blink,
            m,
            Some(&mut lu.min_rownz),
        );
    }
    for rpos in rbeg..rend {
        marked[w_index[rpos as usize] as usize] = 0;
    }

    // Store column in L.
    let mut put = l_begin_p[rank] as usize;
    for pos in (cbeg + 1)..cend {
        let x = w_value[pos as usize] / pivot;
        if x.abs() > droptol {
            l_index[put] = w_index[pos as usize];
            l_value[put] = x;
            put += 1;
        }
    }
    l_index[put] = -1; // terminate column
    put += 1;
    l_begin_p[rank + 1] = put as LUInt;
    u_begin[rank + 1] = u_put as LUInt;

    // Cleanup:
    // store pivot element;
    // remove pivot colum from column file, pivot row from row file;
    // remove pivot column/row from column/row counts
    colmax[pivot_col] = pivot;
    w_end[pivot_col] = cbeg as LUInt;
    w_end[m + pivot_row] = rbeg as LUInt;
    list_remove(colcount_flink, colcount_blink, pivot_col);
    list_remove(rowcount_flink, rowcount_blink, pivot_row);

    // Check that row file and column file are consistent. Only use when
    // DEBUG_EXTRA since this check is really expensive.
    if cfg!(feature = "debug_extra") {
        assert_eq!(
            file_diff(
                m,
                &w_begin[m as usize..],
                &w_end[m as usize..],
                w_begin,
                w_end,
                w_index,
                None
            ),
            0
        );
        assert_eq!(
            file_diff(
                m,
                w_begin,
                w_end,
                &w_begin[m as usize..],
                &w_end[m as usize..],
                w_index,
                None
            ),
            0
        );
    }

    Status::OK
}

fn pivot_small(lu: &mut LU) -> Status {
    let m = lu.m;
    let rank = lu.rank;
    let droptol = lu.droptol;
    let pad = lu.pad;
    let stretch = lu.stretch;
    let pivot_col = lu.pivot_col.unwrap();
    let pivot_row = lu.pivot_row.unwrap();
    let colcount_flink = &mut lu.colcount_flink;
    let colcount_blink = &mut lu.colcount_blink;
    let rowcount_flink = &mut lu.rowcount_flink;
    let rowcount_blink = &mut lu.rowcount_blink;
    let colmax = &mut lu.col_pivot;
    let l_begin_p = &mut lu.l_begin_p;
    let u_begin = &mut lu.u_begin;
    let w_begin = &mut lu.w_begin;
    let w_end = &mut lu.w_end;
    let w_flink = &mut lu.w_flink;
    let w_blink = &mut lu.w_blink;
    let l_index = &mut lu.l_index;
    let l_value = &mut lu.l_value;
    let u_index = &mut lu.u_index;
    let u_value = &mut lu.u_value;
    let w_index = &mut lu.w_index;
    let w_value = &mut lu.w_value;
    let marked = &mut lu.iwork0;
    let work = &mut lu.work0;
    // int64_t *cancelled      = (void *) lu.row_pivot;
    let cancelled = &mut lu.row_pivot;

    let mut cbeg = w_begin[pivot_col] as usize; // changed by file compression
    let mut cend = w_end[pivot_col] as usize;
    let mut rbeg = w_begin[m + pivot_row] as usize;
    let mut rend = w_end[m + pivot_row] as usize;
    let cnz1 = cend - cbeg - 1; // nz in pivot column except pivot
    let rnz1 = rend - rbeg - 1; // nz in pivot row except pivot

    // lu_int i, j, pos, pos1, rpos, put, Uput, where_, nz, *wi;
    // lu_int grow, room, found, position, col_number;
    // double pivot, a, x, cmx, xrj, *wx;
    // int64_t mask;

    assert!(cnz1 <= MAXROW_SMALL);

    // Check if room is available in W. At most each updated row and each
    // updated column will be reappended and filled-in with rnz1 respectively
    // cnz1 elements. Move pivot to the front of pivot row and pivot column.
    let mut grow = 0;
    let mut where_: Option<usize> = None;
    for pos in cbeg..cend {
        let i = w_index[pos as usize] as usize;
        if i == pivot_row {
            where_ = Some(pos);
        } else {
            let nz = (w_end[m + i] - w_begin[m + i]) as usize;
            grow += nz + rnz1 + (stretch * (nz + rnz1) as f64) as usize + pad;
        }
    }
    assert!(where_.is_some());
    iswap(w_index, cbeg, where_.unwrap());
    fswap(w_value, cbeg, where_.unwrap());
    let pivot = w_value[cbeg as usize];
    assert_ne!(pivot, 0.0);
    let mut where_ = None;
    for rpos in rbeg..rend {
        let j = w_index[rpos as usize] as usize;
        // if ((j = Windex[rpos]) == pivot_col)
        if j == pivot_col {
            where_ = Some(rpos);
        } else {
            let nz = (w_end[j] - w_begin[j]) as usize;
            grow += nz + cnz1 + (stretch * (nz + cnz1) as f64) as usize + pad;
        }
    }
    assert!(where_.is_some());
    iswap(w_index, rbeg, where_.unwrap());
    let mut room = (w_end[2 * m] - w_begin[2 * m]) as usize;
    if grow > room {
        file_compress(
            2 * m,
            w_begin,
            w_end,
            w_flink,
            w_index,
            w_value,
            stretch,
            pad,
        );
        cbeg = w_begin[pivot_col] as usize;
        cend = w_end[pivot_col] as usize;
        rbeg = w_begin[m + pivot_row] as usize;
        rend = w_end[m + pivot_row] as usize;
        room = (w_end[2 * m] - w_begin[2 * m]) as usize;
        lu.ngarbage += 1;
    }
    if grow > room {
        lu.addmem_w = grow - room;
        return Status::Reallocate;
    }

    // get pointer to U
    let mut u_put = u_begin[rank] as usize;
    // assert!(u_put >= 0);
    assert!(u_put < lu.u_mem);

    // Column file update //

    // For each row i to be updated set marked[i] > 0 to its position
    // in the (packed) pivot column.
    let mut position = 1;
    for pos in (cbeg + 1)..cend {
        let i = w_index[pos as usize] as usize;
        marked[i] = position;
        position += 1;
    }

    // let wi = Windex + cbeg;
    // let wx = Wvalue + cbeg;
    let mut col_number = 0; // mask cancelled[col_number]

    // for (rpos = rbeg+1; rpos < rend; rpos++, col_number++) {
    for rpos in (rbeg + 1)..rend {
        let j = w_index[rpos as usize] as usize;
        assert_ne!(j, pivot_col);
        let mut cmx = 0.0; // column maximum

        // Compress unmodified column entries. Store entries to be updated
        // in workspace. Move pivot row entry to the front of column.
        let mut where_ = None;
        let mut put = w_begin[j] as usize;
        let pos1 = w_begin[j] as usize;
        for pos in pos1..w_end[j] as usize {
            let i = w_index[pos] as usize;
            // if ((position = marked[i]) > 0)
            position = marked[i];
            if position > 0 {
                assert_ne!(i, pivot_row);
                work[position as usize] = w_value[pos];
            } else {
                assert_eq!(position, 0);
                let x = w_value[pos].abs();
                if i == pivot_row {
                    where_ = Some(put);
                // } else if ((x = fabs(Wvalue[pos])) > cmx) {
                } else if x > cmx {
                    cmx = x;
                }
                w_index[put] = w_index[pos];
                w_value[put] = w_value[pos];
                put += 1;
            }
        }
        assert!(where_.is_some());
        w_end[j] = put as LUInt;
        iswap(w_index, pos1, where_.unwrap());
        fswap(w_value, pos1, where_.unwrap());
        let xrj = w_value[pos1 as usize]; // pivot row entry

        // Reappend column if no room for update.
        room = w_begin[w_flink[j] as usize] as usize - put;
        if room < cnz1 {
            let nz = (w_end[j] - w_begin[j]) as usize;
            room = cnz1 + (stretch * (nz + cnz1) as f64) as usize + pad;
            file_reappend(
                j,
                2 * m,
                w_begin,
                w_end,
                w_flink,
                w_blink,
                w_index,
                w_value,
                room,
            );
            put = w_end[j] as usize;
            assert_eq!(w_begin[w_flink[j] as usize] as usize - put, room);
            lu.nexpand += 1;
        }

        // Compute update in workspace and append to column.
        let a = xrj / pivot;
        for pos in 1..=cnz1 {
            // work[pos as usize] -= a * wx[pos as usize];
            work[pos] -= a * w_value[cbeg..][pos];
        }
        let mut mask = 0;
        for pos in 1..=cnz1 {
            let x = work[pos].abs();
            if x > droptol {
                // Windex[put as usize] = wi[pos as usize];
                w_index[put] = w_index[cbeg..][pos];
                w_value[put] = work[pos];
                put += 1;
                if x > cmx {
                    cmx = x;
                }
            } else {
                // cancellation in row wi[pos]
                // mask |= (int64_t) 1 << (pos-1);
                mask |= 1 << (pos - 1) as i64;
            }
            work[pos] = 0.0;
        }
        w_end[j] = put as LUInt;
        cancelled[col_number] = mask as f64;

        // Write pivot row entry to U and remove from file.
        if xrj.abs() > droptol {
            assert!(u_put < lu.u_mem);
            u_index[u_put] = j as LUInt;
            u_value[u_put] = xrj;
            u_put += 1;
        }
        assert_eq!(w_index[w_begin[j] as usize] as usize, pivot_row);
        w_begin[j] += 1;

        // Move column to new list and update min_colnz.
        let nz = (w_end[j] - w_begin[j]) as usize;
        list_move(
            j,
            nz,
            colcount_flink,
            colcount_blink,
            m,
            Some(&mut lu.min_colnz),
        );

        colmax[j] = cmx;

        col_number += 1;
    }
    for pos in (cbeg + 1)..cend {
        marked[w_index[pos as usize] as usize] = 0;
    }

    // Row file update //

    for rpos in rbeg..rend {
        marked[w_index[rpos as usize] as usize] = 1;
    }
    assert_eq!(marked[pivot_col], 1);

    let mut mask = 1;
    // for (pos = cbeg+1; pos < cend; pos++, mask <<= 1)
    for pos in (cbeg + 1)..cend {
        assert_ne!(mask, 0);
        let i = w_index[pos as usize] as usize;
        assert_ne!(i, pivot_row);

        // Compress unmodified row entries (not marked). Remove
        // overlap with pivot row, including pivot column entry.
        let mut found = 0;
        let mut put = w_begin[m + i] as usize;
        for rpos in w_begin[m + i] as usize..w_end[m + i] as usize {
            let j = w_index[rpos] as usize;
            // if ((j = Windex[rpos]) == pivot_col)
            if j == pivot_col {
                found = 1;
            }
            if marked[j] == 0 {
                w_index[put] = j as LUInt;
                put += 1;
            }
        }
        assert_ne!(found, 0);
        w_end[m + i] = put as LUInt;

        // Reappend row if no room for update. Append pattern of pivot row.
        room = w_begin[w_flink[m + i] as usize] as usize - put;
        if room < rnz1 {
            let nz = (w_end[m + i] - w_begin[m + i]) as usize;
            room = rnz1 + (stretch * (nz + rnz1) as f64) as usize + pad;
            file_reappend(
                m + i,
                2 * m,
                w_begin,
                w_end,
                w_flink,
                w_blink,
                w_index,
                w_value,
                room,
            );
            put = w_end[m + i] as usize;
            assert_eq!(w_begin[w_flink[m + i] as usize] as usize - put, room);
            lu.nexpand += 1;
        }

        col_number = 0;
        for rpos in (rbeg + 1)..rend {
            if (cancelled[col_number] as i64 & mask) == 0 {
                w_index[put as usize] = w_index[rpos];
                put += 1;
            }
            col_number += 1;
        }
        w_end[m + i] = put as LUInt;

        // Move to new list. The row must be reinserted even if nz are
        // unchanged since it might have been taken out in Markowitz search.
        let nz = (w_end[m + i] - w_begin[m + i]) as usize;
        list_move(
            i,
            nz,
            rowcount_flink,
            rowcount_blink,
            m,
            Some(&mut lu.min_rownz),
        );

        mask <<= 1;
    }
    for rpos in rbeg..rend {
        marked[w_index[rpos] as usize] = 0;
    }

    // Store column in L.
    let mut put = l_begin_p[rank] as usize;
    for pos in (cbeg + 1)..cend {
        let x = w_value[pos] / pivot;
        if x.abs() > droptol {
            l_index[put] = w_index[pos];
            l_value[put] = x;
            put += 1;
        }
    }
    l_index[put as usize] = -1; // terminate column
    put += 1;
    l_begin_p[rank + 1] = put as LUInt;
    u_begin[rank + 1] = u_put as LUInt;

    // Cleanup:
    // store pivot elemnt;
    // remove pivot colum from column file, pivot row from row file;
    // remove pivot column/row from column/row counts
    colmax[pivot_col] = pivot;
    w_end[pivot_col] = cbeg as LUInt;
    w_end[m + pivot_row] = rbeg as LUInt;
    list_remove(colcount_flink, colcount_blink, pivot_col);
    list_remove(rowcount_flink, rowcount_blink, pivot_row);

    // Check that row file and column file are consistent. Only use when
    // DEBUG_EXTRA since this check is really expensive.
    if cfg!(feature = "debug_extra") {
        // let (_, Wbegin_m) = Wbegin.split_at(m as usize);
        // let (_, Wend_m) = Wend.split_at(m as usize);
        assert_eq!(
            file_diff(
                m,
                &w_begin[m as usize..],
                &w_end[m as usize..],
                w_begin,
                w_end,
                w_index,
                None
            ),
            0
        );
        assert_eq!(
            file_diff(
                m,
                w_begin,
                w_end,
                &w_begin[m as usize..],
                &w_end[m as usize..],
                w_index,
                None
            ),
            0
        );
    }

    Status::OK
}

fn pivot_singleton_row(lu: &mut LU) -> Status {
    let m = lu.m;
    let rank = lu.rank;
    let droptol = lu.droptol;
    let pivot_col = lu.pivot_col.unwrap();
    let pivot_row = lu.pivot_row.unwrap();
    let colcount_flink = &mut lu.colcount_flink;
    let colcount_blink = &mut lu.colcount_blink;
    let rowcount_flink = &mut lu.rowcount_flink;
    let rowcount_blink = &mut lu.rowcount_blink;
    let colmax = &mut lu.col_pivot;
    let l_begin_p = &mut lu.l_begin_p;
    let u_begin = &mut lu.u_begin;
    let w_begin = &mut lu.w_begin;
    let w_end = &mut lu.w_end;
    let l_index = &mut lu.l_index;
    let l_value = &mut lu.l_value;
    let w_index = &mut lu.w_index;
    let w_value = &mut lu.w_value;

    let cbeg = w_begin[pivot_col];
    let cend = w_end[pivot_col];
    let rbeg = w_begin[m + pivot_row];
    let rend = w_end[m + pivot_row];
    let rnz1 = rend - rbeg - 1; /* nz in pivot row except pivot */

    // lu_int i, pos, put, nz, where_;
    // double pivot, x;

    assert_eq!(rnz1, 0);

    // Find pivot.
    let mut where_ = cbeg;
    while w_index[where_ as usize] != pivot_row as LUInt {
        assert!(where_ < cend - 1);
        where_ += 1;
    }
    let pivot = w_value[where_ as usize];
    assert_ne!(pivot, 0.0);

    // Store column in L.
    let mut put = l_begin_p[rank];
    for pos in cbeg..cend {
        let x = w_value[pos as usize] / pivot;
        if pos != where_ && x.abs() > droptol {
            l_index[put as usize] = w_index[pos as usize];
            l_value[put as usize] = x;
            put += 1;
        }
    }
    l_index[put as usize] = -1; // terminate column
    put += 1;
    l_begin_p[rank + 1] = put;
    u_begin[rank + 1] = u_begin[rank];

    // Remove pivot column from row file. Update row lists.
    for pos in cbeg..cend {
        let i = w_index[pos as usize] as usize;
        if i == pivot_row {
            continue;
        }
        where_ = w_begin[m + i];
        while w_index[where_ as usize] != pivot_col as LUInt {
            assert!(where_ < w_end[m + i] - 1);
            where_ += 1;
        }
        // Windex[where_] = Windex[--Wend[m+i]];
        w_end[m + i] -= 1;
        w_index[where_ as usize] = w_index[w_end[m + i] as usize];
        let nz = (w_end[m + i] - w_begin[m + i]) as usize;
        list_move(
            i,
            nz,
            rowcount_flink,
            rowcount_blink,
            m,
            Some(&mut lu.min_rownz),
        );
    }

    // Cleanup:
    // store pivot elemnt;
    // remove pivot colum from column file, pivot row from row file;
    // remove pivot column/row from column/row counts
    colmax[pivot_col] = pivot;
    w_end[pivot_col] = cbeg;
    w_end[(m + pivot_row) as usize] = rbeg;
    list_remove(colcount_flink, colcount_blink, pivot_col);
    list_remove(rowcount_flink, rowcount_blink, pivot_row);

    Status::OK
}

fn pivot_singleton_col(lu: &mut LU) -> Status {
    let m = lu.m;
    let rank = lu.rank;
    let droptol = lu.droptol;
    let pivot_col = lu.pivot_col.unwrap();
    let pivot_row = lu.pivot_row.unwrap();
    let colcount_flink = &mut lu.colcount_flink;
    let colcount_blink = &mut lu.colcount_blink;
    let rowcount_flink = &mut lu.rowcount_flink;
    let rowcount_blink = &mut lu.rowcount_blink;
    let colmax = &mut lu.col_pivot;
    let l_begin_p = &mut lu.l_begin_p;
    let u_begin = &mut lu.u_begin;
    let w_begin = &mut lu.w_begin;
    let w_end = &mut lu.w_end;
    let l_index = &mut lu.l_index;
    let u_index = &mut lu.u_index;
    let u_value = &mut lu.u_value;
    let w_index = &mut lu.w_index;
    let w_value = &mut lu.w_value;

    let cbeg = w_begin[pivot_col];
    let cend = w_end[pivot_col];
    let rbeg = w_begin[m + pivot_row];
    let rend = w_end[m + pivot_row];
    let cnz1 = cend - cbeg - 1; /* nz in pivot column except pivot */

    // lu_int j, pos, rpos, put, nz, where_, found;
    // double pivot, cmx, x, xrj;

    assert_eq!(cnz1, 0);

    // Remove pivot row from column file and store in U. Update column lists.
    let mut put = u_begin[rank];
    let pivot = w_value[cbeg as usize];
    assert_ne!(pivot, 0.0);
    let mut found = 0;
    let mut xrj = 0.0; // initialize to make gcc happy
    for rpos in rbeg..rend {
        let j = w_index[rpos as usize] as usize;
        if j == pivot_col {
            found = 1;
            continue;
        }
        let mut where_: Option<usize> = None;
        let mut cmx = 0.0; // column maximum
        for pos in w_begin[j] as usize..w_end[j] as usize {
            let x = w_value[pos].abs();
            if w_index[pos] == pivot_row as LUInt {
                where_ = Some(pos);
                xrj = w_value[pos];
            // } else if ((x = fabs(Wvalue[pos])) > cmx) {
            } else if x > cmx {
                cmx = x;
            }
        }
        assert!(where_.is_some());
        if xrj.abs() > droptol {
            u_index[put as usize] = j as LUInt;
            u_value[put as usize] = xrj;
            put += 1;
        }
        // Windex[where_] = Windex[--Wend [j]];
        w_end[j] -= 1;
        w_index[where_.unwrap()] = w_index[w_end[j] as usize];
        w_value[where_.unwrap()] = w_value[w_end[j] as usize];
        let nz = (w_end[j] - w_begin[j]) as usize;
        list_move(
            j,
            nz,
            colcount_flink,
            colcount_blink,
            m,
            Some(&mut lu.min_colnz),
        );
        colmax[j] = cmx;
    }
    assert_ne!(found, 0);
    u_begin[rank + 1] = put;

    // Store empty column in L.
    put = l_begin_p[rank];
    l_index[put as usize] = -1; // terminate column
    put += 1;
    l_begin_p[rank + 1] = put;

    // Cleanup:
    // store pivot element;
    // remove pivot colum from column file, pivot row from row file;
    // remove pivot column/row from column/row counts
    colmax[pivot_col] = pivot;
    w_end[pivot_col] = cbeg;
    w_end[m + pivot_row] = rbeg;
    list_remove(colcount_flink, colcount_blink, pivot_col);
    list_remove(rowcount_flink, rowcount_blink, pivot_row);

    Status::OK
}

fn pivot_doubleton_col(lu: &mut LU) -> Status {
    let m = lu.m;
    let rank = lu.rank;
    let droptol = lu.droptol;
    let pad = lu.pad;
    let stretch = lu.stretch;
    let pivot_col = lu.pivot_col.unwrap();
    let pivot_row = lu.pivot_row.unwrap();
    let colcount_flink = &mut lu.colcount_flink;
    let colcount_blink = &mut lu.colcount_blink;
    let rowcount_flink = &mut lu.rowcount_flink;
    let rowcount_blink = &mut lu.rowcount_blink;
    let colmax = &mut lu.col_pivot;
    let l_begin_p = &mut lu.l_begin_p;
    let u_begin = &mut lu.u_begin;
    let w_begin = &mut lu.w_begin;
    let w_end = &mut lu.w_end;
    let w_flink = &mut lu.w_flink;
    let w_blink = &mut lu.w_blink;
    let l_index = &mut lu.l_index;
    let l_value = &mut lu.l_value;
    let u_index = &mut lu.u_index;
    let u_value = &mut lu.u_value;
    let w_index = &mut lu.w_index;
    let w_value = &mut lu.w_value;
    let marked = &mut lu.iwork0;

    let mut cbeg = w_begin[pivot_col] as usize; // changed by file compression
    let cend = w_end[pivot_col] as usize;
    let mut rbeg = w_begin[m + pivot_row] as usize;
    let mut rend = w_end[m + pivot_row] as usize;
    let cnz1 = cend - cbeg - 1; // nz in pivot column except pivot
    let rnz1 = rend - rbeg - 1; // nz in pivot row except pivot

    // lu_int j, pos, rpos, put, Uput, nz, nfill, where_, where_pivot, where_other;
    // lu_int other_row, grow, room, space, end, ncancelled;
    // double pivot, other_value, xrj, cmx, x, xabs;

    assert_eq!(cnz1, 1);

    /* Move pivot element to front of pivot column and pivot row. */
    if w_index[cbeg] != pivot_row as LUInt {
        iswap(w_index, cbeg, cbeg + 1);
        fswap(w_value, cbeg, cbeg + 1);
    }
    assert_eq!(w_index[cbeg] as usize, pivot_row);
    let pivot = w_value[cbeg];
    assert_ne!(pivot, 0.0);
    let other_row = w_index[cbeg + 1] as usize;
    let other_value = w_value[cbeg + 1];
    let mut where_ = rbeg;
    while w_index[where_ as usize] != pivot_col as LUInt {
        assert!(where_ < rend - 1);
        where_ += 1;
    }
    iswap(w_index, rbeg, where_);

    // Check if room is available in W.
    // Columns can be updated in place but the updated row may need to be
    // expanded.
    let mut nz = (w_end[m + other_row] - w_begin[m + other_row]) as usize;
    let grow = nz + rnz1 + (stretch * (nz + rnz1) as f64) as usize + pad;
    let mut room = (w_end[2 * m] - w_begin[2 * m]) as usize;
    if grow > room {
        file_compress(
            2 * m,
            w_begin,
            w_end,
            w_flink,
            w_index,
            w_value,
            stretch,
            pad,
        );
        cbeg = w_begin[pivot_col] as usize;
        // cend = w_end[pivot_col] as usize;
        rbeg = w_begin[m + pivot_row] as usize;
        rend = w_end[m + pivot_row] as usize;
        room = (w_end[2 * m] - w_begin[2 * m]) as usize;
        lu.ngarbage += 1;
    }
    if grow > room {
        lu.addmem_w = grow - room;
        return Status::Reallocate;
    }

    // Column file update //

    let mut u_put = u_begin[rank];
    let mut put = rbeg + 1;
    let mut ncancelled = 0;
    for rpos in (rbeg + 1)..rend {
        let j = w_index[rpos as usize] as usize;
        assert_ne!(j, pivot_col);
        let mut cmx = 0.0; // column maximum

        // Find position of pivot row entry and possibly other row entry in
        // column j.
        let mut where_pivot = None;
        let mut where_other = None;
        let mut end = w_end[j as usize] as usize;
        for pos in w_begin[j as usize] as usize..end {
            let x = w_value[pos].abs();
            if w_index[pos] == pivot_row as LUInt {
                where_pivot = Some(pos);
            } else if w_index[pos] as usize == other_row {
                where_other = Some(pos);
            // } else if ((x = fabs(Wvalue[pos])) > cmx) {
            } else if x > cmx {
                cmx = x;
            }
        }
        assert!(where_pivot.is_some());
        let xrj = w_value[where_pivot.unwrap()];

        // Store pivot row entry in U.
        if w_value[where_pivot.unwrap()].abs() > droptol {
            u_index[u_put as usize] = j as LUInt;
            u_value[u_put as usize] = w_value[where_pivot.unwrap()];
            u_put += 1;
        }

        if where_other.is_none() {
            // Compute fill-in element.
            let x = -xrj * (other_value / pivot);
            let xabs = x.abs();
            if xabs > droptol {
                // Store fill-in where pivot row entry was.
                w_index[where_pivot.unwrap()] = other_row as LUInt;
                w_value[where_pivot.unwrap()] = x;
                w_index[put as usize] = j as LUInt;
                put += 1;
                if xabs > cmx {
                    cmx = xabs;
                }
            } else {
                // Remove pivot row entry.
                // end = --Wend[j]; TODO: check
                w_end[j] -= 1;
                end = w_end[j] as usize;
                w_index[where_pivot.unwrap()] = w_index[end];
                w_value[where_pivot.unwrap()] = w_value[end];

                // Decrease column count.
                nz = end - w_begin[j] as usize;
                list_move(
                    j,
                    nz,
                    colcount_flink,
                    colcount_blink,
                    m,
                    Some(&mut lu.min_colnz),
                );
            }
        } else {
            // Remove pivot row entry and update other row entry.
            // end = --Wend[j]; TODO: check
            w_end[j] -= 1;
            end = w_end[j] as usize;
            w_index[where_pivot.unwrap()] = w_index[end];
            w_value[where_pivot.unwrap()] = w_value[end];
            if where_other.unwrap() == end {
                where_other = where_pivot;
            }
            w_value[where_other.unwrap()] -= xrj * (other_value / pivot);

            // If we have numerical cancellation, then remove the entry and mark
            // the column.
            let x = w_value[where_other.unwrap()].abs();
            if x <= droptol {
                // end = --Wend[j]; TODO: check
                w_end[j] -= 1;
                end = w_end[j] as usize;
                w_index[where_other.unwrap()] = w_index[end as usize];
                w_value[where_other.unwrap()] = w_value[end as usize];
                marked[j] = 1;
                ncancelled += 1;
            } else if x > cmx {
                cmx = x;
            }

            // Decrease column count.
            nz = (w_end[j] - w_begin[j]) as usize;
            list_move(
                j,
                nz,
                colcount_flink,
                colcount_blink,
                m,
                Some(&mut lu.min_colnz),
            );
        }
        colmax[j] = cmx;
    }
    rend = put;
    u_begin[rank + 1] = u_put;

    // Row file update //

    // If we have numerical cancellation, then we have to remove these entries
    // (marked) from the row pattern. In any case remove pivot column entry.
    if ncancelled != 0 {
        assert_eq!(marked[pivot_col], 0);
        marked[pivot_col] = 1; // treat as cancelled
        let mut put = w_begin[m + other_row]; // compress remaining entries
        let end = w_end[m + other_row];
        for pos in put..end {
            let j = w_index[pos as usize] as usize;
            if marked[j] != 0 {
                marked[j] = 0;
            } else {
                w_index[put as usize] = j as LUInt;
                put += 1;
            }
        }
        assert_eq!(end - put, ncancelled + 1);
        w_end[m + other_row] = put;
    } else {
        where_ = w_begin[m + other_row] as usize;
        while w_index[where_] as usize != pivot_col {
            assert!(where_ < w_end[m + other_row] as usize - 1);
            where_ += 1;
        }
        // end = --Wend[m+other_row]; TODO: check
        w_end[m + other_row] -= 1;
        let end = w_end[m + other_row];
        w_index[where_] = w_index[end as usize];
    }

    // Reappend row if no room for update.
    let nfill = rend - (rbeg + 1);
    let room = (w_begin[w_flink[m + other_row] as usize] - w_end[m + other_row]) as usize;
    if nfill > room {
        let nz = (w_end[m + other_row] - w_begin[m + other_row]) as usize;
        let space = nfill + (stretch * (nz + nfill) as f64) as usize + pad;
        file_reappend(
            m + other_row,
            2 * m,
            w_begin,
            w_end,
            w_flink,
            w_blink,
            w_index,
            w_value,
            space,
        );
        lu.nexpand += 1;
    }

    // Append fill-in to row pattern.
    let mut put = w_end[m + other_row];
    for pos in (rbeg + 1)..rend {
        w_index[put as usize] = w_index[pos as usize];
        put += 1;
    }
    w_end[m + other_row] = put;

    // Reinsert other row into row counts.
    let nz = (w_end[m + other_row] - w_begin[m + other_row]) as usize;
    list_move(
        other_row,
        nz,
        rowcount_flink,
        rowcount_blink,
        m,
        Some(&mut lu.min_rownz),
    );

    // Store column in L.
    let mut put = l_begin_p[rank];
    let x = other_value / pivot;
    if x.abs() > droptol {
        l_index[put as usize] = other_row as LUInt;
        l_value[put as usize] = x;
        put += 1;
    }
    l_index[put as usize] = -1; // terminate column
    put += 1;
    l_begin_p[rank + 1] = put;

    // Cleanup:
    // store pivot elemnt;
    // remove pivot colum from column file, pivot row from row file;
    // remove pivot column/row from column/row counts
    colmax[pivot_col] = pivot;
    w_end[pivot_col] = cbeg as LUInt;
    w_end[m + pivot_row] = rbeg as LUInt;
    list_remove(colcount_flink, colcount_blink, pivot_col);
    list_remove(rowcount_flink, rowcount_blink, pivot_row);

    // Check that row file and column file are consistent. Only use when
    // DEBUG_EXTRA since this check is really expensive.
    if cfg!(feature = "debug_extra") {
        assert_eq!(
            file_diff(m, &w_begin[m..], &w_end[m..], w_begin, w_end, w_index, None),
            0
        );
        assert_eq!(
            file_diff(m, w_begin, w_end, &w_begin[m..], &w_end[m..], w_index, None),
            0
        );
    }

    Status::OK
}

fn remove_col(lu: &mut LU, j: usize) {
    let m = lu.m;
    let colcount_flink = &mut lu.colcount_flink;
    let colcount_blink = &mut lu.colcount_blink;
    let rowcount_flink = &mut lu.rowcount_flink;
    let rowcount_blink = &mut lu.rowcount_blink;
    let colmax = &mut lu.col_pivot;
    let w_begin = &mut lu.w_begin;
    let w_end = &mut lu.w_end;
    let w_index = &mut lu.w_index;
    let cbeg = w_begin[j as usize];
    let cend = w_end[j as usize];

    // lu_int i, pos, nz, where_;

    // Remove column j from row file.
    for pos in cbeg..cend {
        let i = w_index[pos as usize] as usize;
        let mut where_ = w_begin[m + i];
        while w_index[where_ as usize] as usize != j {
            assert!(where_ < w_end[m + i] - 1);
            where_ += 1;
        }
        // Windex[where_] = Windex[--Wend[m+i]];
        w_end[m + i] -= 1;
        w_index[where_ as usize] = w_index[w_end[m + i] as usize];
        let nz = (w_end[m + i] - w_begin[m + i]) as usize;
        list_move(
            i,
            nz,
            rowcount_flink,
            rowcount_blink,
            m,
            Some(&mut lu.min_rownz),
        );
    }

    // Remove column j from column file.
    colmax[j] = 0.0;
    w_end[j] = cbeg;
    list_move(
        j,
        0,
        colcount_flink,
        colcount_blink,
        m,
        Some(&mut lu.min_colnz),
    );
}