mapgd  0.4
A program for the Maximum-likelihood analysis of population genomic data.
 All Data Structures Functions Variables Friends Groups Pages
state.h
1 #ifndef _STATE_H_
2 #define _STATE_H_
3 
4 #include <string.h>
5 #include <iostream>
6 #include <sstream>
7 #include <climits>
8 #include <libintl.h>
9 
10 #include "lz4.h" //Fast compression/decompression.
11 
12 #include "data.h"
13 #include "typedef.h"
14 #include "gzstream.h" //Good compression.
15 #include "stream_tools.h"
16 
17 // 964799
18 
19 #define LZ4_BUFFER_SIZE 2000000000
20 #define CACHE_SIZE 4000000000
21 #define MASKED true
22 
24 {
25 public:
26  char *lz4_ptr;
27  size_t size_, sites_, cached_sites_;
28 };
29 
30 
31 class State : public virtual Data
32 {
33 private:
34 //DATA
35  size_t size_, sites_, cached_sites_, block_size_, lz4_buffer_size_;
36  bool streaming_, cached_, transposed_, masked_;
37  uint8_t k_;
38  char *lz4_ptr_, *lz4_start_, *lz4_end_, *lz4_last_, *temp_lz4_ptr_;
39 
40 //FUNCTIONS
41  static const Registration registered;
42  static Data * create(const std::vector <std::string> & Columns){
43  return new State(Columns);
44  }
45 
46  void increase_buffer_(void);
47 
48  void uncompress_ (char *&, uint32_t *&, uint32_t *&) const;
49  void compress_ (char *&, char *&, const uint32_t *a, const uint32_t *b) const;
50 
51  void uncompress_ (char *&) const;
52  void compress_ (char *&, char *&) const;
53 
54  inline void set_private_(const State &rhs)
55  {
56  size_=rhs.size_;
57  sites_=rhs.sites_;
58  cached_sites_=rhs.cached_sites_;
59  block_size_=rhs.block_size_;
60  lz4_buffer_size_=rhs.lz4_buffer_size_;
61  streaming_=rhs.streaming_;
62  cached_=rhs.cached_;
63  transposed_=rhs.transposed_;
64  masked_=rhs.masked_;
65  k_=rhs.k_;
66  }
67 
68 public:
69  State();
70  State(const std::vector <std::string> &);
71 
72  State(const State &rhs)
73  {
74  std::cerr << "Copy constructor called from:" << size_t(&rhs) << " to " << size_t (this) << " lz4_buffer_size: " << lz4_buffer_size_ << std::endl;
75  std::cerr << "Some addressed: " << size_t (rhs.lz4_start_) << " and " << size_t (lz4_start_) << std::endl;
76  lz4_buffer_size_=rhs.lz4_buffer_size_;
77 
78  lz4_start_ = new char [lz4_buffer_size_];
79  std::cerr << "Allocating " << size_t(lz4_start_) << ", " << lz4_buffer_size_ << std::endl;
80  lz4_end_ = lz4_start_ + lz4_buffer_size_;
81  lz4_last_ = lz4_start_+(rhs.lz4_last_-rhs.lz4_start_);
82  memcpy(lz4_start_, rhs.lz4_start_, rhs.lz4_end_-rhs.lz4_start_);
83 
84  set_private_(rhs);
85  };
86 
87  State(State&& rhs)
88  {
89  std::cerr << "Move constructor called from " << size_t(lz4_start_) << " to " << size_t(rhs.lz4_start_) << std::endl;
90  if (this != &rhs)
91  {
92  lz4_buffer_size_=rhs.lz4_buffer_size_;
93  lz4_start_ = rhs.lz4_start_;
94  lz4_end_ = rhs.lz4_end_;
95  lz4_last_ = rhs.lz4_last_;
96 
97  set_private_(rhs);
98 
99  rhs.lz4_start_ = nullptr;
100  rhs.lz4_end_ = nullptr;
101  rhs.lz4_last_ = nullptr;
102  }
103  }
104 
105  State& operator=(const State& rhs )
106  {
107  std::cerr << "Assignment constructor called " << std::endl;
108  if (this != &rhs)
109  {
110  if (lz4_start_) delete [] lz4_start_;
111  std::cerr << "Some addressed: " << size_t (rhs.lz4_start_) << " and " << size_t (lz4_start_) << std::endl;
112  std::cerr << "Assign from:" << size_t(&rhs) << " to " << size_t (this) << " lz4_buffer_size: " << lz4_buffer_size_ << std::endl;
113  lz4_buffer_size_=rhs.lz4_buffer_size_;
114 
115  lz4_start_ = new char [lz4_buffer_size_];
116  std::cerr << "Allocating " << size_t(lz4_start_) << ", " << lz4_buffer_size_ << ", " << size_t(rhs.lz4_last_-rhs.lz4_start_) << ", " << size_t(rhs.lz4_end_-rhs.lz4_start_) << std::endl;
117  lz4_end_ = lz4_start_ + lz4_buffer_size_;
118  lz4_last_ = lz4_start_+(rhs.lz4_last_-rhs.lz4_start_);
119  memcpy(lz4_start_, rhs.lz4_start_, rhs.lz4_end_-rhs.lz4_start_);
120  lz4_ptr_=lz4_start_;
121 
122  set_private_(rhs);
123  }
124  return *this;
125  }
126 
127  State(const uint32_t &);
128  State(const uint32_t &, const uint32_t &, const uint32_t &);
129  ~State();
130 
131  void set_k(const uint8_t &);
132  uint8_t get_k (void) const;
133 
134  /*without mask*/
135 
136  void uncompress (uint32_t *a, uint32_t *b);
137  void uncompress_inplace (uint32_t *a, uint32_t *b);
138  void uncompress (uint32_t *a, uint32_t *b, const uint32_t &k);
139  void uncompress (uint32_t *a, uint32_t *b, State_stream &) const;
140 
141  void compress (const uint32_t *a, const uint32_t *b);
142  void compress_inplace (const uint32_t *a, const uint32_t *b);
143 
144  void uncompress (uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d);
145  void uncompress_inplace (uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d);
146  void uncompress (uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, const uint32_t &k);
147  void uncompress (uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, State_stream &) const;
148 
149  void compress (const uint32_t *a, const uint32_t *b, const uint32_t *c, const uint32_t *d);
150  void compress_inplace (const uint32_t *a, const uint32_t *b, const uint32_t *c, const uint32_t *d);
151 
152  void transpose (void);
153 
154  void cache (void);
155  void rewind (void);
156  void advance (void);
157  void finalize (void);
158 
159  void clear(void);
160 
161  static const std::string file_name;
162  static const std::string table_name;
163 
164  const std::string get_file_name() const;
165  const std::string get_table_name() const;
166 
167  static const bool binary;
168 
169  inline const size_t & sample_size(void) const {return size_;};
170  inline const size_t & genome_size(void) const {return sites_;};
171  inline void set_streaming(const bool &b) {streaming_=b;};
172 
173  void read(std::istream& str);
174  void write(std::ostream& str) const;
175 
176  void read_binary(std::istream& str);
177  void write_binary(std::ostream& str) const;
178 
179  std::string header() const;
180 
181  void set_stream(State_stream &) const;
182 
183  inline size_t get_free(void) const {return (size_t)(lz4_end_-lz4_last_); };
184 
185  double compression_ratio(void) const;
186  size_t buffer_size(void) const;
187 
188  bool empty(void) const;
189  bool cached(void) const;
190 
191  inline void swap(State &rhs)
192  {
193  std::cerr << "swapping " << size_t (rhs.lz4_start_) << " and " << size_t (lz4_start_) << std::endl;
194 
195  std::swap(size_, rhs.size_);
196  std::swap(sites_, rhs.sites_);
197  std::swap(cached_sites_, rhs.cached_sites_);
198  std::swap(block_size_, rhs.block_size_);
199  std::swap(lz4_buffer_size_, rhs.lz4_buffer_size_);
200  std::swap(k_, rhs.k_);
201 
202  std::swap(cached_, rhs.cached_);
203  std::swap(masked_, rhs.masked_);
204  std::swap(streaming_, rhs.streaming_);
205  std::swap(transposed_, rhs.transposed_);
206 
207  std::swap(lz4_ptr_, rhs.lz4_ptr_);
208  std::swap(lz4_start_, rhs.lz4_start_);
209  std::swap(lz4_end_, rhs.lz4_end_);
210  std::swap(lz4_last_, rhs.lz4_last_);
211 
212  }
213 
214 };
215 
216 State sub_sample(const State &, const size_t &, const uint32_t *mask);
217 
218 /*
219 inline void
220 swap(State &lhs, State &rhs)
221 {
222  std::swap(lhs.size_, rhs.size_);
223  std::swap(lhs.sites_, rhs.sites_);
224  std::swap(lhs.cached_sites_, rhs.cached_sites_);
225  std::swap(lhs.block_size_, rhs.block_size_);
226  std::swap(lhs.cached_, rhs.cached_);
227  std::swap(lhs.lz4_ptr_, rhs.lz4_ptr_);
228  std::swap(lhs.lz4_start_, rhs.lz4_start_);
229  std::swap(lhs.lz4_end_, rhs.lz4_end_);
230  std::swap(lhs.lz4_last_, rhs.lz4_last_);
231 }*/
232 #endif
Definition: state.h:23
static const bool binary
Destination table in Db.
Definition: state.h:167
void read(std::istream &str)
The read function must be defined in the child class.
Definition: state.cc:779
void write(std::ostream &str) const
The write function must be defined in the child class.
Definition: state.cc:672
Definition: state.h:31
static const std::string file_name
The dafualt extention for files.
Definition: state.h:161
State()
constructor needed by map_file. String should be column names.
Definition: state.cc:9
A class which registers a child of Data in Data::new_data_.
Definition: data.h:222
A class which can be written as flat text file or into an SQL database.
Definition: data.h:34
static const std::string table_name
Destination table in Db.
Definition: state.h:162