e9f37dc4b54361ad7d1f2c336f103e7a8b1266f5
[SXSI/XMLTree.git] / libcds / src / static_sequence / wt_coder_binary.cpp
1 /* wt_coder_binary.cpp
2  * Copyright (C) 2008, Francisco Claude, all rights reserved.
3  *
4  * wt_coder_binary 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 <wt_coder_binary.h>
23
24 wt_coder_binary::wt_coder_binary(uint * seq, uint n, alphabet_mapper * am) {
25         uint max_v = 0;
26   for(uint i=0;i<n;i++)
27     max_v = max(am->map(seq[i]),max_v);
28   h=bits(max_v);
29 }
30
31 wt_coder_binary::wt_coder_binary(uchar * seq, uint n, alphabet_mapper * am) {
32         uint max_v = 0;
33   for(uint i=0;i<n;i++)
34       max_v = max(am->map((uint)seq[i]),max_v);
35   h=bits(max_v);
36 }
37
38 wt_coder_binary::wt_coder_binary() {}
39
40 wt_coder_binary::~wt_coder_binary() {}
41
42 bool wt_coder_binary::is_set(uint symbol, uint l) {
43         if((1<<(h-l-1))&symbol) return true;
44         return false;
45 }
46
47 bool wt_coder_binary::done(uint symbol, uint l) {
48         if(l==h) return true;
49         return false;
50 }
51
52 uint wt_coder_binary::size() {
53   return sizeof(wt_coder_binary);
54 }
55
56 uint wt_coder_binary::save(FILE *fp) {
57   uint wr = WT_CODER_BINARY_HDR;
58   wr = fwrite(&wr,sizeof(uint),1,fp);
59   wr += fwrite(&h,sizeof(uint),1,fp);
60   return wr-2;
61 }
62
63 wt_coder_binary * wt_coder_binary::load(FILE *fp) {
64   uint rd;
65   if(fread(&rd,sizeof(uint),1,fp)!=1) return NULL;
66   if(rd!=WT_CODER_BINARY_HDR) return NULL;
67   wt_coder_binary * ret = new wt_coder_binary();
68   if(fread(&ret->h,sizeof(uint),1,fp)!=1) {
69     delete ret;
70     return NULL;
71   }
72   return ret;
73 }