1 #ifndef _DIMACS__9FD17A0C_743B_4438_B8D7_9CD297CED5D3_
2 #define _DIMACS__9FD17A0C_743B_4438_B8D7_9CD297CED5D3_
11 inline static void OnProblemInformationRead(
int nVertices,
int nEdges) { }
12 inline static void OnFoundEdge(
int nVert1,
int nVert2) { }
13 inline static void OnLoadComplete() { }
18 int get_params(
int& Nr_vert,
int& Nr_edges,
const char* Preamble)
21 const char * pp = Preamble;
23 tmp = (
char *)calloc(100,
sizeof(
char));
25 Nr_vert = Nr_edges = 0;
27 while (!stop && (c = *pp++) !=
'\0'){
31 while ((c = *pp++) !=
'\n' && c !=
'\0');
35 sscanf(pp,
"%s %d %d\n", tmp, &Nr_vert, &Nr_edges);
46 if (Nr_vert == 0 || Nr_edges == 0)
55 #define MAX_NR_VERTICES 10000
56 #define MAX_NR_VERTICESdiv8 (MAX_NR_VERTICES/8)
57 #define MAX_PREAMBLE 10000
61 bool get_edge(
int i,
int j,
char Bitmap[MAX_NR_VERTICES][MAX_NR_VERTICESdiv8])
63 static const unsigned char masks[ 8 ] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
68 bit = 7-(j & 0x00000007);
72 return( (Bitmap[i][byte] & mask)==mask );
76 template<
class _listener>
77 bool read_graph_DIMACS_bin(
const char* lpszFile)
79 char Preamble[MAX_PREAMBLE];
80 char Bitmap[MAX_NR_VERTICES][MAX_NR_VERTICESdiv8];
84 FILE *fp = fopen(lpszFile,
"r");
85 if(fp == NULL) { fprintf(stderr,
"\nERROR: Unable to open %s \n", lpszFile);
return false; }
88 if (!fscanf(fp,
"%d\n", &length))
89 { fprintf(stderr,
"\nERROR: Corrupted preamble in %s \n", lpszFile);
return false; }
91 if(length >= MAX_PREAMBLE)
92 { fprintf(stderr,
"\nERROR: Too long preamble in %s \n", lpszFile);
return false; }
95 fread(Preamble, 1, length, fp);
96 Preamble[length] =
'\0';
99 int Nr_vert, Nr_edges;
100 if (!get_params(Nr_vert, Nr_edges, Preamble))
101 { fprintf(stderr,
"\nERROR: Corrupted preamble in %s \n", lpszFile);
return false; }
104 _listener::OnProblemInformationRead(Nr_vert, Nr_edges);
108 ; i < Nr_vert && fread(Bitmap[i], 1, (
int)((i + 8)/8), fp)
114 for ( i = 0; i<Nr_vert; i++ )
115 for ( j=0; j<=i; j++ )
116 if ( get_edge(i,j,Bitmap) )
117 _listener::OnFoundEdge(i+1, j+1);
119 _listener::OnLoadComplete();
125 template<
class _listener>
126 bool read_graph_DIMACS_ascii(
const char* lpszFile)
128 char Preamble[MAX_PREAMBLE];
131 char * pp = Preamble;
133 int Nr_vert, Nr_edges;
135 FILE *fp = fopen(lpszFile,
"r");
136 if(fp == NULL) { fprintf(stderr,
"\nERROR: Unable to open %s \n", lpszFile);
return false; }
139 for(oc =
'\0' ;(c = fgetc(fp)) != EOF && (oc !=
'\n' || c !=
'e')
144 if(!get_params(Nr_vert, Nr_edges, Preamble))
145 { fprintf(stderr,
"\nERROR: Corrupted preamble in %s \n", lpszFile);
return false; }
148 _listener::OnProblemInformationRead(Nr_vert, Nr_edges);
151 while ((c = fgetc(fp)) != EOF)
157 if (!fscanf(fp,
"%d %d", &i, &j))
158 { fprintf(stderr,
"\nERROR: corrupted inputfile %s \n", lpszFile);
return false; }
160 _listener::OnFoundEdge(i, j);
170 _listener::OnLoadComplete();
176 template<
class _listener>
177 bool load_DIMACS_graphfile(
const char* lpszFile)
179 if(strstr(lpszFile,
".b"))
180 return read_graph_DIMACS_bin<_listener>(lpszFile);
183 return read_graph_DIMACS_ascii<_listener>(lpszFile);
186 #endif // _DIMACS__9FD17A0C_743B_4438_B8D7_9CD297CED5D3_