about summary refs log tree commit diff
path: root/sourcecodes/k-best/src/get_best_net.c
blob: 5ed6ebfbfad693aeef80b6688ce289bc22275786 (plain)
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
#include "cfg.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "files.h"
#include "varpar.h"

void get_best_net(int nof_vars, char* ordfile, FILE** bpsfiles, varset_t* net)
{
  FILE* ordf = fopen(ordfile,"r");
  varset_t parcands = 0;
  int i;
  for(i=0;i<nof_vars; ++i){
    varset_t parset;
    int v;
//    printf("Parcands = %u",parcands);

    fscanf(ordf, "%d", &v);
  //  printf("  v= %d",v);
    fseek(bpsfiles[v], varset2parset(v,parcands)*sizeof(varset_t), SEEK_SET);
    fread(&parset, sizeof(varset_t), 1, bpsfiles[v]);
   // printf("Parset =%u ",parset);
    parcands |= 1U<<v;
   // printf("Parcands = %u",parcands);
    net[v] = parset2varset(v, parset);
   // printf("net %d = %u",v,net[v]);
  }
  fclose(ordf);
}


int main(int argc, char* argv[])
{

  if (argc!=5) {
    fprintf(stderr, "Usage: get_best_net nof_vars dirname ordfile netfile\n");
    return 1;
  }
  
  {
    int nof_vars = atoi(argv[1]);

    varset_t* net    = malloc(nof_vars*sizeof(varset_t));
    FILE** files = open_files(nof_vars, argv[2],".bps", "rb");
    get_best_net(nof_vars, argv[3], files, net);
    free_files(nof_vars, files);

    {
      FILE* netf = fopen(argv[4],"w");
      int i;
      for (i=0;i<nof_vars;++i){
	fprintf(netf, "%u\n", net[i]);
      }
      fclose(netf);
    }

    free(net);
  }
    
  return 0;
}