NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
prefix.h
Go to the documentation of this file.
1 /*
2  * nvbio
3  * Copyright (c) 2011-2014, NVIDIA CORPORATION. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the NVIDIA CORPORATION nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #pragma once
29 
31 
32 
33 namespace nvbio {
34 
37 
40 
44 
48 
52 
56 
59 
66 template <
67  typename StringType,
68  typename CoordType,
69  uint32 CoordDim>
70 struct PrefixCore {};
71 
77 template <
78  typename StringType,
79  typename CoordType>
80 struct PrefixCore<StringType,CoordType,1u>
81 {
82  typedef StringType string_type;
83  typedef CoordType coord_type;
85 
86  typedef typename std::iterator_traits<string_type>::value_type symbol_type;
87  typedef typename std::iterator_traits<string_type>::value_type value_type;
88  typedef typename std::iterator_traits<string_type>::reference reference;
89 
93 
98 
103  const string_type string,
104  const coord_type prefix) :
105  m_string( string ),
106  m_coords( prefix ) {}
107 
111  uint32 size() const { return m_coords; }
112 
116  uint32 length() const { return size(); }
117 
121  symbol_type operator[] (const uint32 i) const { return m_string[ i ]; }
122 
126  reference operator[] (const uint32 i) { return m_string[ i ]; }
127 
131  coord_type coords() const { return m_coords; }
132 
135  iterator begin() { return m_string.begin(); }
136 
139  iterator end() { return m_string.begin() + m_coords; }
140 
143  const_iterator begin() const { return m_string.begin(); }
144 
147  const_iterator end() const { return m_string.begin() + m_coords; }
148 
151 };
152 
158 template <
159  typename StringType,
160  typename CoordType>
161 struct PrefixCore<StringType,CoordType,2u>
162 {
163  typedef StringType string_type;
164  typedef CoordType coord_type;
166 
167  typedef typename std::iterator_traits<string_type>::value_type symbol_type;
168  typedef typename std::iterator_traits<string_type>::value_type value_type;
169  typedef typename std::iterator_traits<string_type>::reference reference;
170 
174 
179 
184  const string_type string,
185  const coord_type prefix) :
186  m_string( string ),
187  m_coords( prefix ) {}
188 
192  uint32 size() const { return m_coords.y; }
193 
197  uint32 length() const { return size(); }
198 
202  symbol_type operator[] (const uint32 i) const { return m_string[ i ]; }
203 
207  reference operator[] (const uint32 i) { return m_string[ i ]; }
208 
212  coord_type coords() const { return m_coords; }
213 
216  iterator begin() { return m_string.begin(); }
217 
220  iterator end() { return m_string.begin() + m_coords.y; }
221 
224  const_iterator begin() const { return m_string.begin(); }
225 
228  const_iterator end() const { return m_string.begin() + m_coords.y; }
229 
232 };
233 
235 
242 template <
243  typename StringType,
244  typename CoordType>
245 struct Prefix : PrefixCore< StringType, CoordType, vector_traits<CoordType>::DIM >
246 {
248 
250  typedef StringType string_type;
251  typedef CoordType coord_type;
252 
253  typedef typename std::iterator_traits<string_type>::value_type symbol_type;
254  typedef typename std::iterator_traits<string_type>::value_type value_type;
255  typedef typename std::iterator_traits<string_type>::reference reference;
256 
260 
264  Prefix() {}
265 
270  const string_type string,
271  const coord_type infix) : core_type( string, infix ) {}
272 };
273 
282 template <typename StringType, typename CoordType>
284 Prefix<StringType,CoordType> make_prefix(const StringType string, const CoordType coords)
285 {
286  return Prefix<StringType,CoordType>( string, coords );
287 }
288 
291 
298 template <
299  typename SequenceType,
300  typename PrefixIterator,
301  uint32 CoordDim>
302 struct PrefixSetCore {};
303 
309 template <
310  typename SequenceType,
311  typename PrefixIterator>
312 struct PrefixSetCore<SequenceType,PrefixIterator,1u>
313 {
314  typedef SequenceType sequence_type;
315  typedef PrefixIterator prefix_iterator;
316 
317  typedef typename std::iterator_traits<PrefixIterator>::value_type coord_type;
319 
324 
329  const uint32 size,
330  const sequence_type sequence,
331  const prefix_iterator prefixes) :
332  m_size( size ),
333  m_sequence( sequence ),
334  m_prefixes( prefixes ) {}
335 
339  uint32 size() const { return m_size; }
340 
344  string_type operator[] (const uint32 i) const
345  {
346  const coord_type coords = m_prefixes[i];
347  return string_type( m_sequence, coords );
348  }
349 
353 };
354 
360 template <
361  typename SequenceType,
362  typename PrefixIterator>
363 struct PrefixSetCore<SequenceType,PrefixIterator,2u>
364 {
365  typedef SequenceType sequence_type;
366  typedef PrefixIterator prefix_iterator;
367 
368  typedef typename sequence_type::string_type base_string_type;
369  typedef typename std::iterator_traits<PrefixIterator>::value_type coord_type;
371 
376 
381  const uint32 size,
382  const sequence_type sequence,
383  const prefix_iterator prefixes) :
384  m_size( size ),
385  m_sequence( sequence ),
386  m_prefixes( prefixes ) {}
387 
391  uint32 size() const { return m_size; }
392 
396  string_type operator[] (const uint32 i) const
397  {
398  const coord_type coords = m_prefixes[i];
399  return string_type( m_sequence[ coords.x ], coords );
400  }
401 
405 };
406 
408 
411 template <typename StringType, typename CoordType>
414 
417 template <typename StringType, typename CoordType, uint32 CoordDim>
419 uint32 length(const PrefixCore<StringType,CoordType,CoordDim>& prefix) { return prefix.length(); }
420 
436 template <
437  typename SequenceType,
438  typename PrefixIterator>
439 struct PrefixSet : public PrefixSetCore<
440  SequenceType,
441  PrefixIterator,
442  vector_traits<typename std::iterator_traits<PrefixIterator>::value_type>::DIM>
443 {
444  typedef PrefixSetCore<
445  SequenceType,
446  PrefixIterator,
448 
449  typedef SequenceType sequence_type;
450  typedef PrefixIterator prefix_iterator;
452 
453  typedef typename base_type::coord_type coord_type;
454  typedef typename base_type::string_type string_type;
455 
458 
463 
468  const uint32 size,
469  const sequence_type sequence,
470  const prefix_iterator prefixes) :
471  base_type( size, sequence, prefixes ) {}
472 
475  const_iterator begin() const { return const_iterator(*this,0u); }
476 
479  const_iterator end() const { return const_iterator(*this,base_type::size()); }
480 
483  iterator begin() { return iterator(*this,0u); }
484 
487  iterator end() { return iterator(*this,base_type::size()); }
488 };
489 
492 
493 } // namespace nvbio
494 
495 //#include <nvbio/basic/prefix_inl.h>