-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvec.c
241 lines (214 loc) · 5.08 KB
/
vec.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
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
#include "string.h"
#include "stdlib.h"
#include "stdio.h"
#include "vec.h"
/* output the distance matrix in vec format */
void output_dmat( DMAT* dmat, const char* filename ) {
int i,j;
float w;
FILE* fp;
fp = fopen(filename,"w");
for( i = 0; i < dmat->N; i++ ) {
GHashTable* row = g_array_index(dmat->rows,GHashTable*,i);
GList* indices = g_hash_table_get_keys(row);
while(indices != NULL) {
j = *((int*)(indices->data));
w = *((float*)g_hash_table_lookup(row,indices->data));
if( i!=j )
fprintf(fp, "(%d,%0.4f) ",j+1,w);
indices = indices->next;
}
fprintf(fp,"\n");
}
fclose(fp);
}
/* Load distance matrix data from the .vec sparse matrix format */
DMAT* load_dmat( const char *filename ) {
char line[65536];
char item[256];
int i = 0, j = 0, k = 0;
char line_done = 0;
char paren_done = 0;
FILE *fp = 0;
DMAT* dmat = NULL;
float tempfloat = 0.f;
// open the file
fp = fopen(filename, "r");
// return if there is an error
if( fp == NULL )
return NULL;
// count the number of vals (for every line)
int tempNNZ = 0;
int max_nnz = 0;
int line_num = 0;
while( fgets( line, 65535, fp ) != NULL ) {
tempNNZ = 0;
i = 0;
while( line[i] != '\0' ) {
if( line[i] == '(')
tempNNZ++;
i++;
}
line_num++;
max_nnz = (tempNNZ > max_nnz)?tempNNZ:max_nnz;
}
fclose( fp );
dmat = (DMAT*)malloc(sizeof(DMAT));
dmat->N = line_num;
dmat->vals = (float*)malloc(sizeof(float)*line_num*max_nnz);
dmat->idxs = (int*)malloc(sizeof(int)*line_num*max_nnz);
dmat->rows = g_array_new(FALSE,FALSE,sizeof(GHashTable*));
// read values into data structures
line_num = 0;
fp = fopen(filename, "r");
while( fgets( line, 65535, fp ) != NULL ) {
GHashTable* row = g_hash_table_new(g_int_hash,g_int_equal);
g_array_append_val(dmat->rows,row);
k = line_num * max_nnz;
line_done = 0;
i = 0;
j = 0;
while( !line_done ) {
paren_done = 0;
if( line[i++] == '(' ) {
while( !paren_done ) {
if( line[i] == ',' ) {
item[j] = '\0';
dmat->idxs[k] = atoi(item)-1;
j=0;
}
else if( line[i] == ')' ) {
item[j] = '\0';
tempfloat = atof(item);
dmat->vals[k] = tempfloat < 0.f ? 0.f : tempfloat;
g_hash_table_insert(row,&(dmat->idxs[k]),&(dmat->vals[k]));
j=0;
paren_done = 1;
k++;
}
else if( line[i] != '(' && line[i] != ' ' ) {
item[j++]=line[i];
}
i++;
}
}
if( line[i] == '\0' ) {
line_num++;
line_done = 1;
}
}
}
return dmat;
}
/*
Load VECTYPE data from the .vec sparse matrix format
*/
VECFILE* load_vec( const char *filename ) {
VECFILE* vecfile=NULL;
char* line = NULL;
char item[256];
int i = 0, j = 0, k = 0;
char line_done = 0;
char paren_done = 0;
FILE *fp = 0;
float tempfloat = 0.f;
int maxlinelen = (2<<24)-1;
line = (char*)malloc(sizeof(char)*maxlinelen);
vecfile = (VECFILE*)malloc(sizeof(VECFILE));
vecfile->max_dim=0;
vecfile->points = g_array_new(FALSE,FALSE,sizeof(GArray*));
vecfile->term_count = 0;
// open the file
fp = fopen(filename, "r");
// return if there is an error
if( fp == NULL )
return NULL;
// count the number of points (for every line)
int tempNNZ = 0;
int line_num = 0;
while( fgets( line, maxlinelen, fp ) != NULL ) {
fflush(stdout);
tempNNZ = 0;
i = 0;
while( line[i] != '\0' ) {
if( line[i] == '(')
tempNNZ++;
i++;
}
vecfile->max_nnz = (tempNNZ>vecfile->max_nnz)?tempNNZ:vecfile->max_nnz;
GArray* new_points = g_array_new(FALSE,FALSE,sizeof(TERM*));
g_array_append_val(vecfile->points,new_points);
line_done = 0;
i = 0;
j = 0;
while( !line_done ) {
paren_done = 0;
if( line[i++] == '(' ) {
while( !paren_done ) {
if( line[i] == ',' ) {
item[j] = '\0';
vecfile->max_dim = (vecfile->max_dim<atoi(item))?atoi(item):vecfile->max_dim;
j=0;
}
else if( line[i] == ')' ) {
item[j] = '\0';
j=0;
vecfile->term_count++;
paren_done = 1;
}
else if( line[i] != '(' && line[i] != ' ' ) {
item[j++]=line[i];
}
i++;
}
}
if( line[i] == '\0' ) {
line_num++;
line_done = 1;
}
}
}
fclose( fp );
// allocate data
vecfile->terms = (TERM*)malloc(vecfile->term_count*sizeof(TERM));
// read values into data structures
line_num = 0;
fp = fopen(filename, "r");
while( fgets( line, maxlinelen, fp ) != NULL ) {
line_done = 0;
i = 0;
j = 0;
while( !line_done ) {
paren_done = 0;
if( line[i++] == '(' ) {
while( !paren_done ) {
if( line[i] == ',' ) {
item[j] = '\0';
vecfile->terms[k].num=atoi(item);
j=0;
}
else if( line[i] == ')' ) {
item[j] = '\0';
tempfloat = atof(item);
vecfile->terms[k].val=tempfloat<0.f?0.f:tempfloat;
TERM* termptr = &(vecfile->terms[k]);
g_array_append_val(g_array_index(vecfile->points,GArray*,line_num),termptr);
j=0;
paren_done = 1;
k++;
}
else if( line[i] != '(' && line[i] != ' ' ) {
item[j++]=line[i];
}
i++;
}
}
if( line[i] == '\0' ) {
line_num++;
line_done = 1;
}
}
}
free(line);
return vecfile;
}