Create branch library-split
[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 using std::min;
24 using std::max;
25 wt_coder_binary::wt_coder_binary(uint * seq, uint n, alphabet_mapper * am) {
26         uint max_v = 0;
27   for(uint i=0;i<n;i++)
28     max_v = max(am->map(seq[i]),max_v);
29   h=bits(max_v);
30 }
31
32 wt_coder_binary::wt_coder_binary(uchar * seq, uint n, alphabet_mapper * am) {
33         uint max_v = 0;
34   for(uint i=0;i<n;i++)
35       max_v = max(am->map((uint)seq[i]),max_v);
36   h=bits(max_v);
37 }
38
39 wt_coder_binary::wt_coder_binary() {}
40
41 wt_coder_binary::~wt_coder_binary() {}
42
43 bool wt_coder_binary::is_set(uint symbol, uint l) {
44         if((1<<(h-l-1))&symbol) return true;
45         return false;
46 }
47
48 bool wt_coder_binary::done(uint symbol, uint l) {
49         if(l==h) return true;
50         return false;
51 }
52
53 uint wt_coder_binary::size() {
54   return sizeof(wt_coder_binary);
55 }
56
57 uint wt_coder_binary::save(FILE *fp) {
58   uint wr = WT_CODER_BINARY_HDR;
59   wr = fwrite(&wr,sizeof(uint),1,fp);
60   wr += fwrite(&h,sizeof(uint),1,fp);
61   return wr-2;
62 }
63
64 wt_coder_binary * wt_coder_binary::load(FILE *fp) {
65   uint rd;
66   if(fread(&rd,sizeof(uint),1,fp)!=1) return NULL;
67   if(rd!=WT_CODER_BINARY_HDR) return NULL;
68   wt_coder_binary * ret = new wt_coder_binary();
69   if(fread(&ret->h,sizeof(uint),1,fp)!=1) {
70     delete ret;
71     return NULL;
72   }
73   return ret;
74 }