forked from radfordneal/LDPC-codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpchk-to-alist.c
166 lines (135 loc) · 3.33 KB
/
pchk-to-alist.c
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
/* ALIST-TO-PCHK.C - Convert a parity check matrix to alist format. */
/* Copyright (c) 1995-2012 by Radford M. Neal.
*
* Permission is granted for anyone to copy, use, modify, and distribute
* these programs and accompanying documents for any purpose, provided
* this copyright notice is retained and prominently displayed, and note
* is made of any changes made to these programs. These programs and
* documents are distributed without any warranty, express or implied.
* As the programs were written for research purposes only, they have not
* been tested to the degree that would be advisable in any important
* application. All use of these programs is entirely at the user's own
* risk.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "alloc.h"
#include "open.h"
#include "mod2sparse.h"
#include "mod2dense.h"
#include "mod2convert.h"
#include "rcode.h"
void usage(void);
/* MAIN PROGRAM. */
int main
( int argc,
char **argv
)
{
char *alist_file, *pchk_file;
FILE *af, *pf;
int mxrw, mxcw;
int *rw, *cw;
int i, j, k;
mod2entry *e;
int trans;
int nozeros;
int last;
trans = 0;
nozeros = 0;
for (;;)
{
if (argc>1 && strcmp(argv[1],"-t")==0)
{ trans = 1;
argc -= 1;
argv += 1;
}
else if (argc>1 && strcmp(argv[1],"-z")==0)
{ nozeros = 1;
argc -= 1;
argv += 1;
}
else
{ break;
}
}
if (argc!=3)
{ usage();
}
pchk_file = argv[1];
alist_file = argv[2];
read_pchk(pchk_file);
if (trans)
{ mod2sparse *HT;
HT = H;
H = mod2sparse_allocate(N,M);
mod2sparse_transpose(HT,H);
M = mod2sparse_rows(H);
N = mod2sparse_cols(H);
}
af = open_file_std(alist_file,"wb");
if (af==NULL)
{ fprintf(stderr,"Can't create alist file: %s\n",alist_file);
exit(1);
}
fprintf(af,"%d %d\n",M,N);
rw = (int *) chk_alloc (M, sizeof *rw);
mxrw = 0;
for (i = 0; i<M; i++)
{ rw[i] = mod2sparse_count_row(H,i);
if (rw[i]>mxrw)
{ mxrw = rw[i];
}
}
cw = (int *) chk_alloc (N, sizeof *cw);
mxcw = 0;
for (j = 0; j<N; j++)
{ cw[j] = mod2sparse_count_col(H,j);
if (cw[j]>mxcw)
{ mxcw = cw[j];
}
}
fprintf(af,"%d %d\n",mxrw,mxcw);
for (i = 0; i<M; i++)
{ fprintf(af,"%d%c",rw[i],i==M-1?'\n':' ');
}
for (j = 0; j<N; j++)
{ fprintf(af,"%d%c",cw[j],j==N-1?'\n':' ');
}
for (i = 0; i<M; i++)
{ e = mod2sparse_first_in_row(H,i);
last = 0;
for (k = 0; !last; k++)
{ last = nozeros ? k==rw[i]-1 : k==mxrw-1;
fprintf (af, "%d%c", mod2sparse_at_end(e)?0:mod2sparse_col(e)+1,
last?'\n':' ');
if (!mod2sparse_at_end(e))
{ e = mod2sparse_next_in_row(e);
}
}
}
for (j = 0; j<N; j++)
{ e = mod2sparse_first_in_col(H,j);
last = 0;
for (k = 0; !last; k++)
{ last = nozeros ? k==cw[j]-1 : k==mxcw-1;
fprintf (af, "%d%c", mod2sparse_at_end(e)?0:mod2sparse_row(e)+1,
last?'\n':' ');
if (!mod2sparse_at_end(e))
{ e = mod2sparse_next_in_col(e);
}
}
}
if (ferror(af) || fclose(af)!=0)
{ fprintf(stderr,"Error writing to alist file %s\n",alist_file);
exit(1);
}
return 0;
}
/* PRINT USAGE MESSAGE AND EXIT. */
void usage(void)
{ fprintf(stderr,"Usage: pchk-to-alist [ -t ] [ -z ] pchk-file alist-file\n");
exit(1);
}