f6a9cb5a433fe4fa0ec3dd4003937d8e1da4a9fe
[SXSI/XMLTree.git] / libcds / src / utils / alphabet_mapper_cont.cpp
1 /* alphabet_mapper_cont.cpp
2  * Copyright (C) 2008, Francisco Claude, all rights reserved.
3  *
4  * alphabet_mapper_cont definition
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21  
22 #include <alphabet_mapper_cont.h>
23
24 alphabet_mapper_cont::alphabet_mapper_cont(uint * seq, uint n, static_bitsequence_builder *bmb) { 
25   uint max_v = 0;
26   for(uint i=0;i<n;i++)
27     max_v = max(max_v,seq[i]);
28   max_v++;
29   uint blen = uint_len(max_v,1);
30   uint * bmap = new uint[blen];
31   for(uint i=0;i<blen;i++)
32     bmap[i] = 0;
33   for(uint i=0;i<n;i++)
34     bitset(bmap,seq[i]);
35   m = bmb->build(bmap,max_v);
36   delete [] bmap;
37 }
38
39 alphabet_mapper_cont::alphabet_mapper_cont() {
40 }
41
42 alphabet_mapper_cont::~alphabet_mapper_cont() {
43   delete m;
44 }
45
46 uint alphabet_mapper_cont::map(uint s) {
47   return m->rank1(s);
48 }
49
50 uint alphabet_mapper_cont::unmap(uint s) {
51   return m->select1(s);
52 }
53
54 uint alphabet_mapper_cont::size() { 
55   return sizeof(alphabet_mapper_cont)+m->size(); 
56 }
57
58 uint alphabet_mapper_cont::save(FILE *fp) {
59   uint wr = ALPHABET_MAPPER_CONT_HDR;
60   wr = fwrite(&wr,sizeof(uint),1,fp);
61   if(wr!=1) return 1;
62   if(m->save(fp)) return 1;
63   return 0;
64 }
65
66 alphabet_mapper_cont * alphabet_mapper_cont::load(FILE * fp) {
67   uint rd;
68   if(fread(&rd,sizeof(uint),1,fp)!=1) return NULL;
69   if(rd!=ALPHABET_MAPPER_CONT_HDR) return NULL;
70   alphabet_mapper_cont * ret = new alphabet_mapper_cont();
71   ret->m = static_bitsequence::load(fp);
72   if(ret->m==NULL) {
73     delete ret;
74     return NULL;
75   }
76   return ret;
77 }