📄 sparse.cpp
字号:
#include "main.h"
#include "sparse.h"
/* the details of the functions below can be refered to
Software for Low Density Parity Check Codes*/
static mod2entry *alloc_entry( mod2sparse *m)
{
mod2block *b;
mod2entry *e;
int k;
if ( m->next_free == 0)
{
b = (mod2block *) calloc(1,sizeof *b);
b->next = m->blocks;
m->blocks = b;
for ( k = 0; k < Mod2sparse_block; k++ )
{
b->entry[k].left = m->next_free;
m->next_free = &b->entry[k];
}
}
e = m->next_free;
m->next_free = e->left;
e->pr = 0;
e->lr = 0;
return e;
}
mod2sparse *mod2sparse_allocate (int n_rows, int n_cols)
{
mod2sparse *m;
mod2entry *e;
int i,j;
if( ( n_rows <= 0 ) || ( n_cols <= 0 ) )
{
printf("mod2sparse_allocate:invalid number of rows and columns\n");
exit(1);
}
m = (mod2sparse *) calloc(1,sizeof *m);
m->n_cols = n_cols;
m->n_rows = n_rows;
m->cols = (mod2entry *) calloc(n_cols,sizeof (*(m->cols)));
m->rows = (mod2entry *) calloc(n_rows,sizeof (*(m->rows)));
m->blocks = 0;
m->next_free = 0;
for( i = 0; i < n_rows; i++ )
{
e = &(m->rows[i]);
e->left = e->right = e->up = e->down = e;
e->col = e->row = -1;
}
for( j = 0; j < n_cols; j++ )
{
e = &(m->cols[j]);
e->left = e->right = e->up = e->down = e;
e->col = e->row = -1;
}
return m;
}
mod2entry *mod2sparse_insert ( mod2sparse *m, int row, int col )
{
mod2entry *re, *ce, *ne;
if ( row<0 || row>=mod2sparse_rows(m) || col<0 || col>=mod2sparse_cols(m))
{
printf("mod2sparse_insert: row or column index out of bounds\n");
exit(1);
}
re = mod2sparse_last_in_row(m,row);
if (!mod2sparse_at_end(re) && mod2sparse_col(re)==col)
return re;
if (mod2sparse_at_end(re) || mod2sparse_col(re)<col)
re = re->right;
else
{
re = mod2sparse_first_in_row( m, row );
for (;;)
{
if (!mod2sparse_at_end(re) && mod2sparse_col(re)==col)
return re;
if ( mod2sparse_at_end(re) || mod2sparse_col(re)>col )
break;
re = mod2sparse_next_in_row(re);
}
}
ne = alloc_entry(m);
ne->row = row;
ne->col = ne->TempCol = col;
ne->left = re->left;
ne->right = re;
ne->left->right = ne;
ne->right->left = ne;
ce = mod2sparse_last_in_col(m,col);
if (!mod2sparse_at_end(ce) && mod2sparse_row(ce)==row)
{
printf("mod2sparse_insert: Garbled matrix\n");
exit(1);
}
if (mod2sparse_at_end(ce) || mod2sparse_row(ce)<row)
ce = ce->down;
else
{
ce = mod2sparse_first_in_col(m,col);
for (;;)
{
if (!mod2sparse_at_end(ce) && mod2sparse_row(ce)==row)
{
printf("mod2sparse_insert: Garbled matrix\n");
exit(1);
}
if (mod2sparse_at_end(ce) || mod2sparse_row(ce)>row){ break;}
ce = mod2sparse_next_in_col(ce);
}
}
ne->up = ce->up;
ne->down = ce;
ne->up->down = ne;
ne->down->up = ne;
return ne;
}
mod2sparse* Make_LDPC(int Matrix[][H_ColNum], int StartRow, int RowNumber, int ColNumber)
{
int i,j;
int row,col;
int EndRow=StartRow+RowNumber;
mod2sparse *H;
H=mod2sparse_allocate(RowNumber,ColNumber);
for(i = StartRow; i < EndRow; i++ )
{ for(j = 0;j < H_ColNum; j++ )
{ if( Matrix[i][j] >= 0 )
{ row =i-StartRow;
col = Matrix[i][j];
mod2sparse_insert( H, row, col );
}
}
}
return H;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -