📄 sparse_disk.c
字号:
/* * This file is part of tomo3d * * Copyright (C) 2002, 2003, Sebastien Judenherc <sebastien.judenherc@na.infn.it> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA * */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h>#include <math.h>#include "sparse_disk.h"#define EPS 1.0e-6#define OK fprintf(stderr,"FILE %s LINE %d\n",__FILE__,__LINE__);int save_cnt=0;int writematrix(f,tmp)FILE *f;struct matrix_t * tmp;{ float *line; int i,j; fprintf(stderr,"Writing matrix ..."); fwrite(&(tmp->nl),sizeof(int),1,f); fwrite (&(tmp->nc),sizeof(int),1,f); fprintf(stderr,"%d:%d...",tmp->nl,tmp->nc); line=calloc(tmp->nc,sizeof(float)); if (!line) { free(tmp); return(2); } for (i=0; i<tmp->nl; i++) { for (j=0; j<tmp->nc; j++) sparsegetval(tmp, i, j, &(line[j])); fwrite(line,sizeof(float),tmp->nc,f); } free(line); fprintf(stderr,"OK\n"); return(0);}struct matrix_t * readmatrix(f)FILE *f;{ struct matrix_t * tmp; int L,C; fprintf(stderr,"Reading matrix ..."); fread(&L,sizeof(int),1,f); fread(&C,sizeof(int),1,f); fprintf(stderr,"%d:%d...",L,C); tmp=newmatrix(L,C); if (!tmp) { return(NULL); } tmp->f=f; fseek(tmp->f,L*C*sizeof(float),SEEK_CUR); fprintf(stderr,"OK\n"); return(tmp);}struct matrix_t * newmatrix(nl,nc)int nl, nc;{ struct matrix_t * tmp; tmp=(struct matrix_t *)calloc(1,sizeof(struct matrix_t)); if (!tmp) return(NULL); tmp->nl=nl; tmp->nc=nc; tmp->lcache=-1; tmp->cache=(float*)calloc(nl,sizeof(float)); if (!tmp->cache) { free(tmp); return(NULL); } return(tmp);}void sparsefreematrix(mat)struct matrix_t * mat;{ free(mat->cache); fclose(mat->f); free(mat);}int sparsegetval( mat, i, j, val )struct matrix_t * mat;int i, j;float *val;{ long dest; float v; assert(i<mat->nl); assert(j<mat->nc); if (i!=mat->lcache) { /* TESTER OPT */ if (mat->lcache<0) { mat->offset=ftell(mat->f); } dest=2*sizeof(int)+i*sizeof(float)*mat->nc; assert( fseek(mat->f,dest,SEEK_SET)==0 ); mat->offset=dest-mat->offset; assert(fread(mat->cache,sizeof(float)*mat->nc,1,mat->f)); mat->lcache=i; } v=*val=mat->cache[j]; if (v>EPS) { return(1); } if (v<-EPS) { return(1); } return(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -