NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
infix.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 InfixCore {};
71 
77 template <
78  typename StringType,
79  typename CoordType>
80 struct InfixCore<StringType,CoordType,2u>
81 {
82  typedef StringType string_type;
83  typedef CoordType coord_type;
86 
87  typedef typename std::iterator_traits<string_type>::value_type symbol_type;
88  typedef typename std::iterator_traits<string_type>::value_type value_type;
89  typedef typename std::iterator_traits<string_type>::reference reference;
90 
94 
98  InfixCore() {}
99 
104  const string_type string,
105  const coord_type infix) :
106  m_string( string ),
107  m_coords( infix ) {}
108 
112  index_type size() const { return m_coords.y - m_coords.x; }
113 
117  index_type length() const { return size(); }
118 
122  symbol_type operator[] (const uint32 i) const { return m_string[ m_coords.x + i ]; }
123 
127  reference operator[] (const uint32 i) { return m_string[ m_coords.x + i ]; }
128 
132  range_type range() const { return make_vector( m_coords.x, m_coords.y ); }
133 
137  coord_type coords() const { return m_coords; }
138 
141  iterator begin() { return m_string.begin() + m_coords.x; }
142 
145  iterator end() { return m_string.begin() + m_coords.y; }
146 
149  const_iterator begin() const { return m_string.begin() + m_coords.x; }
150 
153  const_iterator end() const { return m_string.begin() + m_coords.y; }
154 
157 };
158 
164 template <
165  typename StringType,
166  typename CoordType>
167 struct InfixCore<StringType,CoordType,4u>
168 {
169  typedef StringType string_type;
170  typedef CoordType coord_type;
173 
174  typedef typename std::iterator_traits<string_type>::value_type symbol_type;
175  typedef typename std::iterator_traits<string_type>::value_type value_type;
176  typedef typename std::iterator_traits<string_type>::reference reference;
177 
181 
186 
191  const string_type string,
192  const coord_type infix) :
193  m_string( string ),
194  m_coords( infix ) {}
195 
199  index_type size() const { return m_coords.z - m_coords.y; }
200 
204  index_type length() const { return size(); }
205 
209  symbol_type operator[] (const uint32 i) const { return m_string[ m_coords.y + i ]; }
210 
214  reference operator[] (const uint32 i) { return m_string[ m_coords.y + i ]; }
215 
219  range_type range() const { return make_vector( m_coords.y, m_coords.z ); }
220 
224  coord_type coords() const { return m_coords; }
225 
229  index_type string_id() const { return m_coords.x; }
230 
233  iterator begin() { return m_string.begin() + m_coords.y; }
234 
237  iterator end() { return m_string.begin() + m_coords.z; }
238 
241  const_iterator begin() const { return m_string.begin() + m_coords.y; }
242 
245  const_iterator end() const { return m_string.begin() + m_coords.z; }
246 
249 };
250 
252 
259 template <
260  typename StringType,
261  typename CoordType>
262 struct Infix : public InfixCore< StringType, CoordType, vector_traits<CoordType>::DIM >
263 {
265 
267  typedef StringType string_type;
268  typedef CoordType coord_type;
270 
271  typedef typename std::iterator_traits<string_type>::value_type symbol_type;
272  typedef typename std::iterator_traits<string_type>::value_type value_type;
273  typedef typename std::iterator_traits<string_type>::reference reference;
274 
278 
282  Infix() {}
283 
288  const string_type string,
289  const coord_type infix) : core_type( string, infix ) {}
290 };
291 
300 template <typename StringType, typename CoordType>
302 Infix<StringType,CoordType> make_infix(const StringType string, const CoordType coords)
303 {
304  return Infix<StringType,CoordType>( string, coords );
305 }
306 
309 
316 template <
317  typename SequenceType,
318  typename InfixIterator,
319  uint32 CoordDim>
320 struct InfixSetCore {};
321 
327 template <
328  typename SequenceType,
329  typename InfixIterator>
330 struct InfixSetCore<SequenceType,InfixIterator,2u>
331 {
332  typedef SequenceType sequence_type;
333  typedef InfixIterator infix_iterator;
334 
335  typedef typename std::iterator_traits<InfixIterator>::value_type coord_type;
337 
342 
347  const uint32 size,
348  const sequence_type sequence,
349  const infix_iterator infixes) :
350  m_size( size ),
351  m_sequence( sequence ),
352  m_infixes( infixes ) {}
353 
357  uint32 size() const { return m_size; }
358 
362  string_type operator[] (const uint32 i) const
363  {
364  const coord_type coords = m_infixes[i];
365  return string_type( m_sequence, coords );
366  }
367 
371 };
372 
378 template <
379  typename SequenceType,
380  typename InfixIterator>
381 struct InfixSetCore<SequenceType,InfixIterator,4u>
382 {
383  typedef SequenceType sequence_type;
384  typedef InfixIterator infix_iterator;
385 
386  typedef typename sequence_type::string_type base_string_type;
387  typedef typename std::iterator_traits<InfixIterator>::value_type coord_type;
389 
394 
399  const uint32 size,
400  const sequence_type sequence,
401  const infix_iterator infixes) :
402  m_size( size ),
403  m_sequence( sequence ),
404  m_infixes( infixes ) {}
405 
409  uint32 size() const { return m_size; }
410 
414  string_type operator[] (const uint32 i) const
415  {
416  const coord_type coords = m_infixes[i];
417  return string_type( m_sequence[ coords.x ], coords );
418  }
419 
423 };
424 
426 
430 uint32 infix_begin(const uint32_2& infix) { return infix.x; }
431 
435 uint32 infix_end(const uint32_2& infix) { return infix.y; }
436 
440 uint32 length(const uint32_2& infix) { return infix.y - infix.x; }
441 
445 uint64 infix_begin(const uint64_2& infix) { return infix.x; }
446 
450 uint64 infix_end(const uint64_2& infix) { return infix.y; }
451 
455 uint64 length(const uint64_2& infix) { return infix.y - infix.x; }
456 
460 uint32 infix_begin(const uint32_4& infix) { return infix.y; }
461 
465 uint32 infix_end(const uint32_4& infix) { return infix.z; }
466 
470 uint32 length(const uint32_4& infix) { return infix.z - infix.y; }
471 
475 uint64 infix_begin(const uint64_4& infix) { return infix.y; }
476 
480 uint64 infix_end(const uint64_4& infix) { return infix.z; }
481 
485 uint64 length(const uint64_4& infix) { return infix.z - infix.y; }
486 
490 uint32 string_id(const uint32_4& infix) { return infix.x; }
491 
495 uint64 string_id(const uint64_4& infix) { return infix.x; }
496 
499 template <typename StringType, typename CoordType>
501 typename Infix<StringType,CoordType>::index_type infix_begin(const Infix<StringType,CoordType>& infix) { return infix.range().x; }
502 
505 template <typename StringType, typename CoordType>
507 typename Infix<StringType,CoordType>::index_type infix_end(const Infix<StringType,CoordType>& infix) { return infix.range().y; }
508 
511 template <typename StringType, typename CoordType>
514 
517 template <typename StringType, typename CoordType>
519 uint32 length(const Infix<StringType,CoordType>& infix) { return infix.length(); }
520 
538 template <
539  typename SequenceType,
540  typename InfixIterator>
541 struct InfixSet : public InfixSetCore<
542  SequenceType,
543  InfixIterator,
544  vector_traits<typename std::iterator_traits<InfixIterator>::value_type>::DIM>
545 {
546  typedef InfixSetCore<
547  SequenceType,
548  InfixIterator,
550 
551  typedef SequenceType sequence_type;
552  typedef InfixIterator infix_iterator;
554 
555  typedef typename base_type::coord_type coord_type;
556  typedef typename base_type::string_type string_type;
557 
560 
564  InfixSet() {}
565 
570  const uint32 size,
571  const sequence_type sequence,
572  const infix_iterator infixes) :
573  base_type( size, sequence, infixes ) {}
574 
577  const_iterator begin() const { return const_iterator(*this,0u); }
578 
581  const_iterator end() const { return const_iterator(*this,base_type::size()); }
582 
585  iterator begin() { return iterator(*this,0u); }
586 
589  iterator end() { return iterator(*this,base_type::size()); }
590 };
591 
594 
595 } // namespace nvbio
596 
597 //#include <nvbio/basic/infix_inl.h>