src/work/akos/loader.h
author marci
Tue, 31 Aug 2004 11:26:59 +0000
changeset 775 e46a1f0623a0
permissions -rw-r--r--
ResGraphWrapper<Graph> is done, so does dimacs.h.
ladanyi@63
     1
#ifndef LOADER_H
ladanyi@63
     2
#define LOADER_H
ladanyi@63
     3
ladanyi@63
     4
#include <map>
ladanyi@63
     5
ladanyi@63
     6
#define LINE_LEN 1024
ladanyi@63
     7
ladanyi@63
     8
template<typename Graph>
ladanyi@63
     9
void LoadGraph(Graph &G, char *filename) {
ladanyi@63
    10
  FILE *file;
ladanyi@63
    11
  if ((file = fopen(filename, "r")) == NULL) {
ladanyi@63
    12
    printf("Could not open %s\n", filename);
ladanyi@63
    13
    return;
ladanyi@63
    14
  }
ladanyi@63
    15
  int i, n1, n2;
ladanyi@63
    16
  std::map<int, typename Graph::NodeIt> nmap;
ladanyi@63
    17
  char line[LINE_LEN];
ladanyi@63
    18
  while (fgets(line, LINE_LEN, file)) {
ladanyi@63
    19
    switch (line[0]) {
ladanyi@63
    20
      case 'n':
ladanyi@63
    21
        nmap[atoi(line + 1)] = G.addNode();
ladanyi@63
    22
        break;
ladanyi@63
    23
      case 'a':
ladanyi@63
    24
        n1 = atoi(line + 1);
ladanyi@63
    25
        i = 1;
ladanyi@63
    26
        while (isspace(line[i])) i++;
ladanyi@63
    27
        while (isdigit(line[i])) i++;
ladanyi@63
    28
        n2 = atoi(line + i);
ladanyi@63
    29
        if (nmap.find(n1) == nmap.end()) {
ladanyi@63
    30
          nmap[n1] = G.addNode();
ladanyi@63
    31
        }
ladanyi@63
    32
        if (nmap.find(n2) == nmap.end()) {
ladanyi@63
    33
          nmap[n2] = G.addNode();
ladanyi@63
    34
        }
ladanyi@63
    35
        G.addEdge(nmap[n1], nmap[n2]);
ladanyi@63
    36
        break;
ladanyi@63
    37
    }
ladanyi@63
    38
  }
ladanyi@63
    39
}
ladanyi@63
    40
ladanyi@63
    41
#endif