NCBI C++ ToolKit
seq_vector_cvt_gen.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef SEQ_VECTOR_CVT_GEN__HPP
2 #define SEQ_VECTOR_CVT_GEN__HPP
3 /* $Id: seq_vector_cvt_gen.hpp 74559 2016-09-13 11:58:16Z ivanov $
4 * ===========================================================================
5 *
6 * PUBLIC DOMAIN NOTICE
7 * National Center for Biotechnology Information
8 *
9 * This software/database is a "United States Government Work" under the
10 * terms of the United States Copyright Act. It was written as part of
11 * the author's official duties as a United States Government employee and
12 * thus cannot be copyrighted. This software/database is freely available
13 * to the public for use. The National Library of Medicine and the U.S.
14 * Government have not placed any restriction on its use or reproduction.
15 *
16 * Although all reasonable efforts have been taken to ensure the accuracy
17 * and reliability of the software and data, the NLM and the U.S.
18 * Government do not and cannot warrant the performance or results that
19 * may be obtained by using this software or data. The NLM and the U.S.
20 * Government disclaim all warranties, express or implied, including
21 * warranties of performance, merchantability or fitness for any particular
22 * purpose.
23 *
24 * Please cite the author in any work or product based on this material.
25 *
26 * ===========================================================================
27 *
28 * Author: Eugene Vasilchenko
29 *
30 * File Description:
31 * Generic Seq-vector conversion functions
32 *
33 */
34 
36 
37 template<class DstIter, class SrcCont>
38 inline
39 void copy_8bit(DstIter dst, size_t count,
40  const SrcCont& srcCont, size_t srcPos)
41 {
42  typename SrcCont::const_iterator src = srcCont.begin() + srcPos;
43  for ( DstIter end(dst + count); dst != end; ++dst ) {
44  _ASSERT(src >= srcCont.begin() && src < srcCont.end());
45  char c = *src;
46  ++src;
47  *dst = c;
48  }
49 }
50 
51 
52 template<class DstIter, class SrcCont>
53 inline
54 void copy_8bit_table(DstIter dst, size_t count,
55  const SrcCont& srcCont, size_t srcPos,
56  const char* table)
57 {
58  typename SrcCont::const_iterator src = srcCont.begin() + srcPos;
59  for ( DstIter end(dst + count); dst != end; ++dst ) {
60  _ASSERT(src >= srcCont.begin() && src < srcCont.end());
61  char c = *src;
62  ++src;
63  *dst = table[c & 0xff];
64  }
65 }
66 
67 
68 template<class DstIter, class SrcCont>
69 inline
70 void copy_8bit_reverse(DstIter dst, size_t count,
71  const SrcCont& srcCont, size_t srcPos)
72 {
73  srcPos += count;
74  typename SrcCont::const_iterator src = srcCont.begin() + srcPos;
75  for ( DstIter end(dst + count); dst != end; ++dst ) {
76  _ASSERT(src > srcCont.begin() && src <= srcCont.end());
77  char c = *--src;
78  *dst = c;
79  }
80 }
81 
82 
83 template<class DstIter, class SrcCont>
84 inline
85 void copy_8bit_table_reverse(DstIter dst, size_t count,
86  const SrcCont& srcCont, size_t srcPos,
87  const char* table)
88 {
89  srcPos += count;
90  typename SrcCont::const_iterator src = srcCont.begin() + srcPos;
91  for ( DstIter end(dst + count); dst != end; ++dst ) {
92  _ASSERT(src > srcCont.begin() && src <= srcCont.end());
93  char c = *--src;
94  *dst = table[c & 0xff];
95  }
96 }
97 
98 
99 template<class DstIter, class SrcCont>
100 void copy_4bit(DstIter dst, size_t count,
101  const SrcCont& srcCont, size_t srcPos)
102 {
103  typename SrcCont::const_iterator src = srcCont.begin() + srcPos / 2;
104  if ( srcPos % 2 ) {
105  // odd char first
106  _ASSERT(src >= srcCont.begin() && src < srcCont.end());
107  char c = *src;
108  ++src;
109  *dst = (c ) & 0x0f;
110  ++dst;
111  --count;
112  }
113  for ( DstIter end(dst + (count & ~1)); dst != end; dst += 2 ) {
114  _ASSERT(src >= srcCont.begin() && src < srcCont.end());
115  char c = *src;
116  ++src;
117  *dst = (c >> 4) & 0x0f;
118  dst[1] = (c ) & 0x0f;
119  }
120  if ( count % 2 ) {
121  // remaining odd char
122  _ASSERT(src >= srcCont.begin() && src < srcCont.end());
123  char c = *src;
124  *dst = (c >> 4) & 0x0f;
125  }
126 }
127 
128 
129 template<class DstIter, class SrcCont>
130 void copy_4bit_table(DstIter dst, size_t count,
131  const SrcCont& srcCont, size_t srcPos,
132  const char* table)
133 {
134  typename SrcCont::const_iterator src = srcCont.begin() + srcPos / 2;
135  if ( srcPos % 2 ) {
136  // odd char first
137  _ASSERT(src >= srcCont.begin() && src < srcCont.end());
138  char c = *src;
139  ++src;
140  *dst = table[(c ) & 0x0f];
141  ++dst;
142  --count;
143  }
144  for ( DstIter end(dst + (count & ~1)); dst != end; dst += 2 ) {
145  _ASSERT(src >= srcCont.begin() && src < srcCont.end());
146  char c = *src;
147  ++src;
148  *dst = table[(c >> 4) & 0x0f];
149  dst[1] = table[(c ) & 0x0f];
150  }
151  if ( count % 2 ) {
152  // remaining odd char
153  _ASSERT(src >= srcCont.begin() && src < srcCont.end());
154  char c = *src;
155  *dst = table[(c >> 4) & 0x0f];
156  }
157 }
158 
159 
160 template<class DstIter, class SrcCont>
161 void copy_4bit_reverse(DstIter dst, size_t count,
162  const SrcCont& srcCont, size_t srcPos)
163 {
164  srcPos += count;
165  typename SrcCont::const_iterator src = srcCont.begin() + srcPos / 2;
166  if ( srcPos % 2 ) {
167  // odd char first
168  _ASSERT(src >= srcCont.begin() && src < srcCont.end());
169  char c = *src;
170  *dst = (c >> 4) & 0x0f;
171  ++dst;
172  --count;
173  }
174  for ( DstIter end(dst + (count & ~1)); dst != end; dst += 2 ) {
175  _ASSERT(src > srcCont.begin() && src <= srcCont.end());
176  char c = *--src;
177  *dst = (c ) & 0x0f;
178  dst[1] = (c >> 4) & 0x0f;
179  }
180  if ( count % 2 ) {
181  // remaining odd char
182  _ASSERT(src > srcCont.begin() && src <= srcCont.end());
183  char c = *--src;
184  *dst = (c ) & 0x0f;
185  }
186 }
187 
188 
189 template<class DstIter, class SrcCont>
190 void copy_4bit_table_reverse(DstIter dst, size_t count,
191  const SrcCont& srcCont, size_t srcPos,
192  const char* table)
193 {
194  srcPos += count;
195  typename SrcCont::const_iterator src = srcCont.begin() + srcPos / 2;
196  if ( srcPos % 2 ) {
197  // odd char first
198  _ASSERT(src >= srcCont.begin() && src < srcCont.end());
199  char c = *src;
200  *dst = table[(c >> 4) & 0x0f];
201  ++dst;
202  --count;
203  }
204  for ( DstIter end(dst + (count & ~1)); dst != end; dst += 2 ) {
205  _ASSERT(src > srcCont.begin() && src <= srcCont.end());
206  char c = *--src;
207  *dst = table[(c ) & 0x0f];
208  dst[1] = table[(c >> 4) & 0x0f];
209  }
210  if ( count % 2 ) {
211  // remaining odd char
212  _ASSERT(src > srcCont.begin() && src <= srcCont.end());
213  char c = *--src;
214  *dst = table[(c ) & 0x0f];
215  }
216 }
217 
218 
219 template<class DstIter, class SrcCont>
220 void copy_2bit(DstIter dst, size_t count,
221  const SrcCont& srcCont, size_t srcPos)
222 {
223  typename SrcCont::const_iterator src = srcCont.begin() + srcPos / 4;
224  size_t first_byte_pos = srcPos % 4;
225  if ( first_byte_pos ) {
226  // odd chars first
227  _ASSERT(src >= srcCont.begin() && src < srcCont.end());
228  char c = *src;
229  ++src;
230  if ( first_byte_pos <= 1 ) {
231  *dst = (c >> 4) & 0x03;
232  if ( --count == 0 ) return;
233  ++dst;
234  }
235  if ( first_byte_pos <= 2 ) {
236  *dst = (c >> 2) & 0x03;
237  if ( --count == 0 ) return;
238  ++dst;
239  }
240  *dst = (c ) & 0x03;
241  --count;
242  ++dst;
243  }
244  for ( DstIter end = dst + (count & ~3); dst != end; dst += 4 ) {
245  _ASSERT(src >= srcCont.begin() && src < srcCont.end());
246  char c3 = *src;
247  ++src;
248  char c0 = char(c3 >> 6);
249  char c1 = char(c3 >> 4);
250  char c2 = char(c3 >> 2);
251  c0 &= 0x03;
252  c1 &= 0x03;
253  c2 &= 0x03;
254  c3 &= 0x03;
255  *dst = c0;
256  dst[1] = c1;
257  dst[2] = c2;
258  dst[3] = c3;
259  }
260  // remaining odd chars
261  size_t last_byte_count = count % 4;
262  if ( last_byte_count ) {
263  _ASSERT(src >= srcCont.begin() && src < srcCont.end());
264  char c = *src;
265  *dst = (c >> 6) & 0x03;
266  if ( last_byte_count >= 2 ) {
267  dst[1] = (c >> 4) & 0x03;
268  if ( last_byte_count >= 3 ) {
269  dst[2] = (c >> 2) & 0x03;
270  }
271  }
272  }
273 }
274 
275 
276 template<class DstIter, class SrcCont>
277 void copy_2bit_table(DstIter dst, size_t count,
278  const SrcCont& srcCont, size_t srcPos,
279  const char* table)
280 {
281  typename SrcCont::const_iterator src = srcCont.begin() + srcPos / 4;
282  size_t first_byte_pos = srcPos % 4;
283  if ( first_byte_pos ) {
284  // odd chars first
285  _ASSERT(src >= srcCont.begin() && src < srcCont.end());
286  char c = *src;
287  ++src;
288  if ( first_byte_pos <= 1 ) {
289  *dst = table[(c >> 4) & 0x03];
290  if ( --count == 0 ) return;
291  ++dst;
292  }
293  if ( first_byte_pos <= 2 ) {
294  *dst = table[(c >> 2) & 0x03];
295  if ( --count == 0 ) return;
296  ++dst;
297  }
298  *dst = table[(c ) & 0x03];
299  --count;
300  ++dst;
301  }
302  for ( DstIter end = dst + (count & ~3); dst != end; dst += 4 ) {
303  _ASSERT(src >= srcCont.begin() && src < srcCont.end());
304  char c3 = *src;
305  ++src;
306  char c0 = char(c3 >> 6);
307  char c1 = char(c3 >> 4);
308  char c2 = char(c3 >> 2);
309  c0 = table[c0 & 0x03];
310  c1 = table[c1 & 0x03];
311  *dst = c0;
312  c2 = table[c2 & 0x03];
313  dst[1] = c1;
314  c3 = table[c3 & 0x03];
315  dst[2] = c2;
316  dst[3] = c3;
317  }
318  // remaining odd chars
319  size_t last_byte_count = count % 4;
320  if ( last_byte_count ) {
321  _ASSERT(src >= srcCont.begin() && src < srcCont.end());
322  char c = *src;
323  *dst = table[(c >> 6) & 0x03];
324  if ( last_byte_count >= 2 ) {
325  dst[1] = table[(c >> 4) & 0x03];
326  if ( last_byte_count >= 3 ) {
327  dst[2] = table[(c >> 2) & 0x03];
328  }
329  }
330  }
331 }
332 
333 
334 template<class DstIter, class SrcCont>
335 void copy_2bit_reverse(DstIter dst, size_t count,
336  const SrcCont& srcCont, size_t srcPos)
337 {
338  srcPos += count;
339  typename SrcCont::const_iterator src = srcCont.begin() + srcPos / 4;
340  size_t first_byte_pos = srcPos % 4;
341  if ( first_byte_pos ) {
342  // odd chars first
343  _ASSERT(src >= srcCont.begin() && src < srcCont.end());
344  char c = *src;
345  if ( first_byte_pos >= 3 ) {
346  *dst = (c >> 2) & 0x03;
347  if ( --count == 0 ) return;
348  ++dst;
349  }
350  if ( first_byte_pos >= 2 ) {
351  *dst = (c >> 3) & 0x03;
352  if ( --count == 0 ) return;
353  ++dst;
354  }
355  *dst = (c >> 6) & 0x03;
356  --count;
357  ++dst;
358  }
359  for ( DstIter end = dst + (count & ~3); dst != end; dst += 4 ) {
360  _ASSERT(src > srcCont.begin() && src <= srcCont.end());
361  char c0 = *--src;
362  char c1 = char(c0 >> 2);
363  char c2 = char(c0 >> 4);
364  char c3 = char(c0 >> 6);
365  c0 &= 0x03;
366  c1 &= 0x03;
367  c2 &= 0x03;
368  c3 &= 0x03;
369  *dst = c0;
370  dst[1] = c1;
371  dst[2] = c2;
372  dst[3] = c3;
373  }
374  // remaining odd chars
375  size_t last_byte_count = count % 4;
376  if ( last_byte_count ) {
377  _ASSERT(src > srcCont.begin() && src <= srcCont.end());
378  char c = *--src;
379  *dst = (c ) & 0x03;
380  if ( last_byte_count >= 2 ) {
381  dst[1] = (c >> 2) & 0x03;
382  if ( last_byte_count >= 3 ) {
383  dst[2] = (c >> 4) & 0x03;
384  }
385  }
386  }
387 }
388 
389 
390 template<class DstIter, class SrcCont>
391 void copy_2bit_table_reverse(DstIter dst, size_t count,
392  const SrcCont& srcCont, size_t srcPos,
393  const char* table)
394 {
395  srcPos += count;
396  typename SrcCont::const_iterator src = srcCont.begin() + srcPos / 4;
397  size_t first_byte_pos = srcPos % 4;
398  if ( first_byte_pos ) {
399  // odd chars first
400  _ASSERT(src >= srcCont.begin() && src < srcCont.end());
401  char c = *src;
402  if ( first_byte_pos >= 3 ) {
403  *dst = table[(c >> 2) & 0x03];
404  if ( --count == 0 ) return;
405  ++dst;
406  }
407  if ( first_byte_pos >= 2 ) {
408  *dst = table[(c >> 4) & 0x03];
409  if ( --count == 0 ) return;
410  ++dst;
411  }
412  *dst = table[(c >> 6) & 0x03];
413  --count;
414  ++dst;
415  }
416  for ( DstIter end = dst + (count & ~3); dst != end; dst += 4 ) {
417  _ASSERT(src > srcCont.begin() && src <= srcCont.end());
418  char c0 = *--src;
419  char c1 = char(c0 >> 2);
420  char c2 = char(c0 >> 4);
421  char c3 = char(c0 >> 6);
422  c0 = table[c0 & 0x03];
423  c1 = table[c1 & 0x03];
424  *dst = c0;
425  c2 = table[c2 & 0x03];
426  dst[1] = c1;
427  c3 = table[c3 & 0x03];
428  dst[2] = c2;
429  dst[3] = c3;
430  }
431  // remaining odd chars
432  size_t last_byte_count = count % 4;
433  if ( last_byte_count ) {
434  _ASSERT(src > srcCont.begin() && src <= srcCont.end());
435  char c = *--src;
436  *dst = table[(c ) & 0x03];
437  if ( last_byte_count >= 2 ) {
438  dst[1] = table[(c >> 2) & 0x03];
439  if ( last_byte_count >= 3 ) {
440  dst[2] = table[(c >> 4) & 0x03];
441  }
442  }
443  }
444 }
445 
447 
448 #endif//SEQ_VECTOR_CVT_GEN__HPP
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
<!DOCTYPE HTML >< html > n< header > n< title > PubSeq Gateway Help Page</title > n< style > n table
void copy_4bit(DstIter dst, size_t count, const SrcCont &srcCont, size_t srcPos)
void copy_2bit_reverse(DstIter dst, size_t count, const SrcCont &srcCont, size_t srcPos)
void copy_4bit_table(DstIter dst, size_t count, const SrcCont &srcCont, size_t srcPos, const char *table)
void copy_2bit_table(DstIter dst, size_t count, const SrcCont &srcCont, size_t srcPos, const char *table)
void copy_8bit_reverse(DstIter dst, size_t count, const SrcCont &srcCont, size_t srcPos)
void copy_8bit_table(DstIter dst, size_t count, const SrcCont &srcCont, size_t srcPos, const char *table)
void copy_2bit_table_reverse(DstIter dst, size_t count, const SrcCont &srcCont, size_t srcPos, const char *table)
void copy_4bit_reverse(DstIter dst, size_t count, const SrcCont &srcCont, size_t srcPos)
void copy_4bit_table_reverse(DstIter dst, size_t count, const SrcCont &srcCont, size_t srcPos, const char *table)
void copy_8bit_table_reverse(DstIter dst, size_t count, const SrcCont &srcCont, size_t srcPos, const char *table)
void copy_2bit(DstIter dst, size_t count, const SrcCont &srcCont, size_t srcPos)
void copy_8bit(DstIter dst, size_t count, const SrcCont &srcCont, size_t srcPos)
#define _ASSERT
Modified on Fri Apr 12 17:16:09 2024 by modify_doxy.py rev. 669887