f8cc20992ad3dc2360a9ea8ac738caa976f3cae6
[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() {}
32
33 wt_coder_binary::~wt_coder_binary() {}
34
35 bool wt_coder_binary::is_set(uint symbol, uint l) {
36         if((1<<(h-l-1))&symbol) return true;
37         return false;
38 }
39
40 bool wt_coder_binary::done(uint symbol, uint l) {
41         if(l==h) return true;
42         return false;
43 }
44
45 uint wt_coder_binary::size() {
46   return sizeof(wt_coder_binary);
47 }
48
49 uint wt_coder_binary::save(FILE *fp) {
50   uint wr = WT_CODER_BINARY_HDR;
51   wr = fwrite(&wr,sizeof(uint),1,fp);
52   wr += fwrite(&h,sizeof(uint),1,fp);
53   return wr-2;
54 }
55
56 wt_coder_binary * wt_coder_binary::load(FILE *fp) {
57   uint rd;
58   if(fread(&rd,sizeof(uint),1,fp)!=1) return NULL;
59   if(rd!=WT_CODER_BINARY_HDR) return NULL;
60   wt_coder_binary * ret = new wt_coder_binary();
61   if(fread(&ret->h,sizeof(uint),1,fp)!=1) {
62     delete ret;
63     return NULL;
64   }
65   return ret;
66 }