Debug swcsa
[SXSI/TextCollection.git] / lzindex / nodemap.c
1
2 // Implements the Map data structure, which maps block ids to lztrie nodes
3
4 #include "nodemap.h"
5
6         // creates a nodemap structure from a mapping array, not owning it
7         // n is number of blocks
8         // max is the number of trie nodes
9
10 nodemap createNodemap(uint *map, uint n, uint max)
11  { 
12     nodemap M;
13     uint i;
14     M = malloc(sizeof(struct snodemap));
15     M->nbits = bits(2*max-1);
16     M->n = n;
17     M->map = malloc(((n*M->nbits+W-1)/W)*sizeof(uint));
18     for (i=0;i<n;i++)
19        bitput(M->map,i*M->nbits,M->nbits,map[i]);
20     return M;
21  }
22
23         // frees revtrie structure, including the owned data
24
25 void destroyNodemap(nodemap M)
26  { 
27     free(M->map);
28     free(M);
29  }
30
31         // mapping
32
33 trieNode mapto(nodemap M, uint id)
34  { 
35     return bitget(M->map,id*M->nbits,M->nbits);
36  }
37
38 void saveNodemap(FILE *f, nodemap M)
39  {
40     fwrite(&M->nbits, sizeof(uint), 1, f);
41     fwrite(&M->n, sizeof(uint), 1, f);
42     fwrite(M->map, sizeof(uint), (M->n*M->nbits+W-1)/W, f);
43  }
44
45 nodemap loadNodemap(FILE *f)
46  {
47     nodemap M;
48
49     M = malloc(sizeof(struct snodemap));
50     fread(&M->nbits, sizeof(uint), 1, f);
51     fread(&M->n, sizeof(uint), 1, f);
52     M->map = malloc(((M->n*M->nbits+W-1)/W)*sizeof(uint));
53     fread(M->map, sizeof(uint), (M->n*M->nbits+W-1)/W, f);
54     return M;
55  }
56
57 uint sizeofNodemap(nodemap M)
58  {
59     return sizeof(struct snodemap) + ((M->n*M->nbits+W-1)/W)*sizeof(uint); 
60  }
61