summaryrefslogtreecommitdiffstats
path: root/src/misc/espresso/matrix.c
blob: 08fa1f958ab709677df15377f9d0fb89f25eab9b (plain)
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
/*
 * Revision Control Information
 *
 * $Source$
 * $Author$
 * $Revision$
 * $Date$
 *
 */
//#include "port.h"
#include "sparse_int.h"

ABC_NAMESPACE_IMPL_START


/*
 *  free-lists are only used if 'FAST_AND_LOOSE' is set; this is because
 *  we lose the debugging capability of libmm_t which trashes objects when
 *  they are free'd.  However, FAST_AND_LOOSE is much faster if matrices
 *  are created and freed frequently.
 */

#ifdef FAST_AND_LOOSE
sm_element *sm_element_freelist;
sm_row *sm_row_freelist;
sm_col *sm_col_freelist;
#endif


sm_matrix *
sm_alloc()
{
    register sm_matrix *A;

    A = ALLOC(sm_matrix, 1);
    A->rows = NIL(sm_row *);
    A->cols = NIL(sm_col *);
    A->nrows = A->ncols = 0;
    A->rows_size = A->cols_size = 0;
    A->first_row = A->last_row = NIL(sm_row);
    A->first_col = A->last_col = NIL(sm_col);
    A->user_word = NIL(char);        /* for our user ... */
    return A;
}


sm_matrix *
sm_alloc_size(row, col)
int row, col;
{
    register sm_matrix *A;

    A = sm_alloc();
    sm_resize(A, row, col);
    return A;
}


void
sm_free(A)
sm_matrix *A;
{
#ifdef FAST_AND_LOOSE
    register sm_row *prow;

    if (A->first_row != 0) {
    for(prow = A->first_row; prow != 0; prow = prow->next_row) {
        /* add the elements to the free list of elements */
        prow->last_col->next_col = sm_element_freelist;
        sm_element_freelist = prow->first_col;
    }

    /* Add the linked list of rows to the row-free-list */
    A->last_row->next_row = sm_row_freelist;
    sm_row_freelist = A->first_row;

    /* Add the linked list of cols to the col-free-list */
    A->last_col->next_col = sm_col_freelist;
    sm_col_freelist = A->first_col;
    }
#else
    register sm_row *prow, *pnext_row;
    register sm_col *pcol, *pnext_col;

    for(prow = A->first_row; prow != 0; prow = pnext_row) {
    pnext_row = prow->next_row;
    sm_row_free(prow);
    }
    for(pcol = A->first_col; pcol != 0; pcol = pnext_col) {
    pnext_col = pcol->next_col;
    pcol->first_row = pcol->last_row = NIL(sm_element);
    sm_col_free(pcol);
    }
#endif

    /* Free the arrays to map row/col numbers into pointers */
    FREE(A->rows);
    FREE(A->cols);
    FREE(A);
}


sm_matrix *
sm_dup(A)
sm_matrix *A; 
{
    register sm_row *prow;
    register sm_element *p;
    register sm_matrix *B;

    B = sm_alloc();
    if (A->last_row != 0) {
    sm_resize(B, A->last_row->row_num, A->last_col->col_num);
    for(prow = A->first_row; prow != 0; prow = prow->next_row) {
        for(p = prow->first_col; p != 0; p = p->next_col) {
        (void) sm_insert(B, p->row_num, p->col_num);
        }
    }
    }
    return B;
}


void 
sm_resize(A, row, col)
register sm_matrix *A;
int row, col;
{
    register int i, new_size;

    if (row >= A->rows_size) {
    new_size = MAX(A->rows_size*2, row+1);
    A->rows = REALLOC(sm_row *, A->rows, new_size);
    for(i = A->rows_size; i < new_size; i++) {
        A->rows[i] = NIL(sm_row);
    }
    A->rows_size = new_size;
    }

    if (col >= A->cols_size) {
    new_size = MAX(A->cols_size*2, col+1);
    A->cols = REALLOC(sm_col *, A->cols, new_size);
    for(i = A->cols_size; i < new_size; i++) {
        A->cols[i] = NIL(sm_col);
    }
    A->cols_size = new_size;
    }
}


/*  
 *  insert -- insert a value into the matrix
 */
sm_element *
sm_insert(A, row, col)
register sm_matrix *A;
register int row, col;
{
    register sm_row *prow;
    register sm_col *pcol;
    register sm_element *element;
    sm_element *save_element;

    if (row >= A->rows_size || col >= A->cols_size) {
    sm_resize(A, row, col);
    }

    prow = A->rows[row];
    if (prow == NIL(sm_row)) {
    prow = A->rows[row] = sm_row_alloc();
    prow->row_num = row;
    sorted_insert(sm_row, A->first_row, A->last_row, A->nrows, 
            next_row, prev_row, row_num, row, prow);
    }

    pcol = A->cols[col];
    if (pcol == NIL(sm_col)) {
    pcol = A->cols[col] = sm_col_alloc();
    pcol->col_num = col;
    sorted_insert(sm_col, A->first_col, A->last_col, A->ncols, 
            next_col, prev_col, col_num, col, pcol);
    }

    /* get a new item, save its address */
    sm_element_alloc(element);
    save_element = element;

    /* insert it into the row list */
    sorted_insert(sm_element, prow->first_col, prow->last_col, 
        prow->length, next_col, prev_col, col_num, col, element);

    /* if it was used, also insert it into the column list */
    if (element == save_element) {
    sorted_insert(sm_element, pcol->first_row, pcol->last_row, 
        pcol->length, next_row, prev_row, row_num, row, element);
    } else {
    /* otherwise, it was already in matrix -- free element we allocated */
    sm_element_free(save_element);
    }
    return element;
}


sm_element *
sm_find(A, rownum, colnum)
sm_matrix *A;
int rownum, colnum;
{
    sm_row *prow;
    sm_col *pcol;

    prow = sm_get_row(A, rownum);
    if (prow == NIL(sm_row)) {
    return NIL(sm_element);
    } else {
    pcol = sm_get_col(A, colnum);
    if (pcol == NIL(sm_col)) {
        return NIL(sm_element);
    }
    if (prow->length < pcol->length) {
        return sm_row_find(prow, colnum);
    } else {
        return sm_col_find(pcol, rownum);
    }
    }
}


void
sm_remove(A, rownum, colnum)
sm_matrix *A;
int rownum, colnum;
{
    sm_remove_element(A, sm_find(A, rownum, colnum));
}



void
sm_remove_element(A, p)
register sm_matrix *A; 
register sm_element *p;
{
    register sm_row *prow;
    register sm_col *pcol;

    if (p == 0) return;

    /* Unlink the element from its row */
    prow = sm_get_row(A, p->row_num);
    dll_unlink(p, prow->first_col, prow->last_col, 
            next_col, prev_col, prow->length);

    /* if no more elements in the row, discard the row header */
    if (prow->first_col == NIL(sm_element)) {
    sm_delrow(A, p->row_num);
    }

    /* Unlink the element from its column */
    pcol = sm_get_col(A, p->col_num);
    dll_unlink(p, pcol->first_row, pcol->last_row, 
            next_row, prev_row, pcol->length);

    /* if no more elements in the column, discard the column header */
    if (pcol->first_row == NIL(sm_element)) {
    sm_delcol(A, p->col_num);
    }

    sm_element_free(p);
}

void 
sm_delrow(A, i)
sm_matrix *A;
int i;
{
    register sm_element *p, *pnext;
    sm_col *pcol;
    sm_row *prow;

    prow = sm_get_row(A, i);
    if (prow != NIL(sm_row)) {
    /* walk across the row */
    for(p = prow->first_col; p != 0; p = pnext) {
        pnext = p->next_col;

        /* unlink the item from the column (and delete it) */
        pcol = sm_get_col(A, p->col_num);
        sm_col_remove_element(pcol, p);

        /* discard the column if it is now empty */
        if (pcol->first_row == NIL(sm_element)) {
        sm_delcol(A, pcol->col_num);
        }
    }

    /* discard the row -- we already threw away the elements */ 
    A->rows[i] = NIL(sm_row);
    dll_unlink(prow, A->first_row, A->last_row, 
                next_row, prev_row, A->nrows);
    prow->first_col = prow->last_col = NIL(sm_element);
    sm_row_free(prow);
    }
}

void 
sm_delcol(A, i)
sm_matrix *A;
int i;
{
    register sm_element *p, *pnext;
    sm_row *prow;
    sm_col *pcol;

    pcol = sm_get_col(A, i);
    if (pcol != NIL(sm_col)) {
    /* walk down the column */
    for(p = pcol->first_row; p != 0; p = pnext) {
        pnext = p->next_row;

        /* unlink the element from the row (and delete it) */
        prow = sm_get_row(A, p->row_num);
        sm_row_remove_element(prow, p);

        /* discard the row if it is now empty */
        if (prow->first_col == NIL(sm_element)) {
        sm_delrow(A, prow->row_num);
        }
    }

    /* discard the column -- we already threw away the elements */ 
    A->cols[i] = NIL(sm_col);
    dll_unlink(pcol, A->first_col, A->last_col, 
                next_col, prev_col, A->ncols);
    pcol->first_row = pcol->last_row = NIL(sm_element);
    sm_col_free(pcol);
    }
}

void
sm_copy_row(dest, dest_row, prow)
register sm_matrix *dest;
int dest_row;
sm_row *prow;
{
    register sm_element *p;

    for(p = prow->first_col; p != 0; p = p->next_col) {
    (void) sm_insert(dest, dest_row, p->col_num);
    }
}


void
sm_copy_col(dest, dest_col, pcol)
register sm_matrix *dest;
int dest_col;
sm_col *pcol;
{
    register sm_element *p;

    for(p = pcol->first_row; p != 0; p = p->next_row) {
    (void) sm_insert(dest, dest_col, p->row_num);
    }
}


sm_row *
sm_longest_row(A)
sm_matrix *A;
{
    register sm_row *large_row, *prow;
    register int max_length;

    max_length = 0;
    large_row = NIL(sm_row);
    for(prow = A->first_row; prow != 0; prow = prow->next_row) {
    if (prow->length > max_length) {
        max_length = prow->length;
        large_row = prow;
    }
    }
    return large_row;
}


sm_col *
sm_longest_col(A)
sm_matrix *A;
{
    register sm_col *large_col, *pcol;
    register int max_length;

    max_length = 0;
    large_col = NIL(sm_col);
    for(pcol = A->first_col; pcol != 0; pcol = pcol->next_col) {
    if (pcol->length > max_length) {
        max_length = pcol->length;
        large_col = pcol;
    }
    }
    return large_col;
}

int
sm_num_elements(A)
sm_matrix *A;
{
    register sm_row *prow;
    register int num;

    num = 0;
    sm_foreach_row(A, prow) {
    num += prow->length;
    }
    return num;
}

int 
sm_read(fp, A)
FILE *fp;
sm_matrix **A;
{
    int i, j, err;

    *A = sm_alloc();
    while (! feof(fp)) {
    err = fscanf(fp, "%d %d", &i, &j);
    if (err == EOF) {
        return 1;
    } else if (err != 2) {
        return 0;
    }
    (void) sm_insert(*A, i, j);
    }
    return 1;
}


int 
sm_read_compressed(fp, A)
FILE *fp;
sm_matrix **A;
{
    int i, j, k, nrows, ncols;
    unsigned long x;

    *A = sm_alloc();
    if (fscanf(fp, "%d %d", &nrows, &ncols) != 2) {
    return 0;
    }
    sm_resize(*A, nrows, ncols);

    for(i = 0; i < nrows; i++) {
    if (fscanf(fp, "%lx", &x) != 1) {
        return 0;
    }
    for(j = 0; j < ncols; j += 32) {
        if (fscanf(fp, "%lx", &x) != 1) {
        return 0;
        }
        for(k = j; x != 0; x >>= 1, k++) {
        if (x & 1) {
            (void) sm_insert(*A, i, k);
        }
        }
    }
    }
    return 1;
}


void 
sm_write(fp, A)
FILE *fp;
sm_matrix *A;
{
    register sm_row *prow;
    register sm_element *p;

    for(prow = A->first_row; prow != 0; prow = prow->next_row) {
    for(p = prow->first_col; p != 0; p = p->next_col) {
        (void) fprintf(fp, "%d %d\n", p->row_num, p->col_num);
    }
    }
}

void 
sm_print(fp, A)
FILE *fp;
sm_matrix *A;
{
    register sm_row *prow;
    register sm_col *pcol;
    int c;

    if (A->last_col->col_num >= 100) {
    (void) fprintf(fp, "    ");
    for(pcol = A->first_col; pcol != 0; pcol = pcol->next_col) {
        (void) fprintf(fp, "%d", (pcol->col_num / 100)%10);
    }
    putc('\n', fp);
    }

    if (A->last_col->col_num >= 10) {
    (void) fprintf(fp, "    ");
    for(pcol = A->first_col; pcol != 0; pcol = pcol->next_col) {
        (void) fprintf(fp, "%d", (pcol->col_num / 10)%10);
    }
    putc('\n', fp);
    }

    (void) fprintf(fp, "    ");
    for(pcol = A->first_col; pcol != 0; pcol = pcol->next_col) {
    (void) fprintf(fp, "%d", pcol->col_num % 10);
    }
    putc('\n', fp);

    (void) fprintf(fp, "    ");
    for(pcol = A->first_col; pcol != 0; pcol = pcol->next_col) {
    (void) fprintf(fp, "-");
    }
    putc('\n', fp);

    for(prow = A->first_row; prow != 0; prow = prow->next_row) {
    (void) fprintf(fp, "%3d:", prow->row_num);

    for(pcol = A->first_col; pcol != 0; pcol = pcol->next_col) {
        c = sm_row_find(prow, pcol->col_num) ? '1' : '.';
        putc(c, fp);
    }
    putc('\n', fp);
    }
}


void 
sm_dump(A, s, max)
sm_matrix *A;
char *s;
int max;
{
    FILE *fp = stdout;

    (void) fprintf(fp, "%s %d rows by %d cols\n", s, A->nrows, A->ncols);
    if (A->nrows < max) {
    sm_print(fp, A);
    }
}

void
sm_cleanup()
{
#ifdef FAST_AND_LOOSE
    register sm_element *p, *pnext;
    register sm_row *prow, *pnextrow;
    register sm_col *pcol, *pnextcol;

    for(p = sm_element_freelist; p != 0; p = pnext) {
    pnext = p->next_col;
    FREE(p);
    }
    sm_element_freelist = 0;

    for(prow = sm_row_freelist; prow != 0; prow = pnextrow) {
    pnextrow = prow->next_row;
    FREE(prow);
    }
    sm_row_freelist = 0;

    for(pcol = sm_col_freelist; pcol != 0; pcol = pnextcol) {
    pnextcol = pcol->next_col;
    FREE(pcol);
    }
    sm_col_freelist = 0;
#endif
}
ABC_NAMESPACE_IMPL_END