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

Go to the SVN repository for this file.

1 #ifndef GUI_WIDGETS_DATA___ATTR_RANGE_COLL__HPP
2 #define GUI_WIDGETS_DATA___ATTR_RANGE_COLL__HPP
3 
4 /* $Id: attr_range_coll.hpp 23970 2011-06-27 18:15:09Z kuznets $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Andrey Yazhuk
30  *
31  */
32 
33 /** @addtogroup GUI_UTILS
34 *
35 * @{
36 */
37 
38 #include <corelib/ncbistd.hpp>
39 #include <corelib/ncbistl.hpp>
40 
41 #include <algorithm>
42 #include <iterator>
43 
44 
46 
47 ///////////////////////////////////////////////////////////////////////////////
48 /// TAttrRangeCollection represents a sequence of continious attributed intervals.
49 /// Intervals are neither intersecting nor separated by gaps.
50 
51 template<class Attr, class Position>
53 {
54  // This structure is moved out of owner template CAttrRangeCollection
55  // because of MSVS fails to resolve necessary reverse operator< by
56  // template's inner class.
57  Position m_Start;
58  Attr m_Attr;
59 
60  bool operator<(const Position& pos) const
61  {
62  return m_Start < pos;
63  }
64 # ifdef NCBI_COMPILER_MSVC
65 # if _MSC_VER >= 1400
66  // Additional compare operator for MSVC 2005 only
67  bool operator<(const SAttrRangeCollectionElement& elem) const
68  {
69  return m_Start < elem.m_Start;
70  }
71 # endif
72 # endif
73 };
74 
75 #ifdef NCBI_COMPILER_MSVC
76 # if _MSC_VER >= 1400
77  // Additional compare operator for MSVC 2005 only
78  template<class Attr, class Position>
79  inline
80  bool operator<(const Position& pos,
82  {
83  return pos < elem.m_Start;
84  }
85 # endif
86 #endif
87 
88 
89 template<class Attr, class Position>
91 {
92 public:
93  typedef Attr attr_type;
94  typedef Position position_type;
96 
97 protected:
99  typedef vector<SElement> TElementVector;
101  /// contains one extra element that is used to represent open end pos
102 public:
103  typedef typename TElementVector::size_type size_type;
104  typedef typename TElementVector::const_iterator vec_const_iterator;
105 
106  class const_iterator;
107  class element_proxy;
108  class const_pos_iterator;
109 
111  {
112  SetFrom(0);
113  }
115  {
116  SetFrom(start);
117  }
119  {
120  m_vElems.swap(c.m_vElems);
121  }
122  void SetFrom(TSeqPos start)
123  {
124  if(m_vElems.size() == 0) {
125  m_vElems.push_back(SElement());
126  }
127  else if(m_vElems.size() > 1) {
128  clear();
129  }
130  m_vElems.back().m_Start = start;
131  }
132  void push_back(const attr_type& attr)
133  {
134  push_back(attr, 1);
135  }
136  void push_back(const attr_type& attr, TSeqPos len)
137  {
138  int N = (int)m_vElems.size();
139  if((N > 1) && (m_vElems[N-2].m_Attr == attr)) {
140  m_vElems[N-1].m_Start += len; //extend
141  } else {
142  m_vElems[N-1].m_Attr = attr;
143  m_vElems.push_back(SElement());
144  m_vElems.back().m_Start = m_vElems[N-1].m_Start + len;
145  }
146  }
147 
148  const_iterator begin() const
149  {
150  return const_iterator(m_vElems.begin());
151  }
152 
153  const_iterator end() const
154  {
155  // return the iterator to the last element (open end position)
156  return const_iterator(m_vElems.end() - 1);
157  }
158  size_type size() const
159  {
160  return m_vElems.size() - 1;
161  }
162  bool empty() const
163  {
164  return m_vElems.size() == 1;
165  }
166  const element_proxy operator[](size_type pos) const
167  {
168  return element_proxy(m_vElems.begin() + pos);
169  }
170  void clear()
171  {
172  m_vElems.begin()->m_Start = m_vElems.back().m_Start;
173  m_vElems.resize(1);
174  }
175  const_iterator find(position_type pos) const
176  {
177  // use "m_vElems.end() - 1" because it represents the collection end()
178  vec_const_iterator it = x_Find(m_vElems.begin(), m_vElems.end() - 1, pos);
179  return const_iterator(it);
180  }
181  const_iterator find(const_iterator first, const_iterator last, position_type pos) const
182  {
183  vec_const_iterator it = x_Find(*first, *last, pos);
184  return const_iterator(it);
185  }
186 
188  {
189  return m_vElems.begin()->m_Start;
190  }
192  {
193  return m_vElems.back().m_Start;
194  }
196  {
197  return m_vElems.back().m_Start - 1;
198  }
200  {
201  return m_vElems.back().m_Start - m_vElems.begin()->m_Start;
202  }
203 
204  const_pos_iterator begin_pos() const
205  {
206  return const_pos_iterator(begin());
207  }
208  const_pos_iterator end_pos() const
209  {
210  return const_pos_iterator(end());
211  }
212 
213 protected:
215  position_type pos) const
216  {
217  if(first != last) {
218  // pos is inside the collection
219  vec_const_iterator it = lower_bound(first, last, pos);
220  if(it != m_vElems.end()) {
221  if(it->m_Start > pos) {
222  if(it > m_vElems.begin()) {
223  return --it;
224  }
225  } else return it;
226  }
227  }
228  return last; // return iterator to the last element
229  }
230 
231 public:
232  // represents virtual element
234  {
235  public:
237  : m_itEl(it)
238  {
239  }
241  : m_itEl(ep.m_itEl)
242  {
243  }
244  TSeqPos GetFrom() const
245  {
246  return m_itEl->m_Start;
247  }
248  TSeqPos GetTo() const
249  {
250  return (m_itEl + 1)->m_Start - 1;
251  }
253  {
254  return (m_itEl + 1)->m_Start;
255  }
257  {
258  return (m_itEl + 1)->m_Start - m_itEl->m_Start;
259  }
260  const attr_type& GetAttr() const
261  {
262  return m_itEl->m_Attr;
263  }
264  bool RefEquals(const element_proxy& ep) const
265  {
266  return m_itEl == ep.m_itEl;
267  }
268  operator vec_const_iterator () const
269  {
270  return m_itEl;
271  }
272  void Inc()
273  {
274  ++m_itEl;
275  }
276  void Dec()
277  {
278  --m_itEl;
279  }
280  protected:
282  };
283 
285  {
286  friend class CAttrRangeCollection;
287  public:
289  : m_proxy(itEl)
290  {
291  }
293  : m_proxy(it.m_proxy)
294  {
295  }
296  const element_proxy& operator*() const
297  {
298  return m_proxy;
299  }
300  const element_proxy* operator->() const
301  {
302  return &m_proxy;
303  }
305  {
306  Inc();
307  return (*this);
308  }
310  {
311  const_iterator Tmp = *this;
312  Inc();
313  return Tmp;
314  }
316  {
317  Dec();
318  return (*this);
319  }
321  {
322  const_iterator Tmp = *this;
323  Dec();
324  return Tmp;
325  }
326  bool operator==(const const_iterator& it) const
327  {
328  return m_proxy.RefEquals(it.m_proxy);
329  }
330  bool operator!=(const const_iterator& it) const
331  {
332  return ! (*this==it);
333  }
334  protected:
335  void Inc()
336  {
337  m_proxy.Inc();
338  }
339  void Dec()
340  {
341  m_proxy.Dec();
342  }
343  protected:
344  element_proxy m_proxy;
345  };
346 
347  // iterate as though this was a vector of attribute values,
348  // not a vector of ranges of equal attribute values.
350  {
351  public:
352  typedef random_access_iterator_tag iterator_category;
353  typedef const attr_type value_type;
354  typedef const attr_type& reference;
355  typedef const attr_type* pointer;
357 
358  const_pos_iterator(const_iterator itR)
359  : m_position(itR->GetFrom()), m_range_it(itR)
360  {
361  }
364  {
365  }
366  const attr_type& operator*() const
367  {
368  return m_range_it->GetAttr();
369  }
370  const attr_type* operator->() const
371  {
372  return &(m_range_it->GetAttr());
373  }
375  {
376  Inc();
377  return (*this);
378  }
380  {
381  const_pos_iterator Tmp = *this;
382  Inc();
383  return Tmp;
384  }
386  {
387  Dec();
388  return (*this);
389  }
391  {
392  const_pos_iterator Tmp = *this;
393  Dec();
394  return Tmp;
395  }
397  m_position += n;
398  if (n > 0) {
399  while (m_range_it->GetTo() < m_position) {
400  ++m_range_it;
401  }
402  } else if (n < 0) {
403  while (m_position < m_range_it->GetFrom()) {
404  --m_range_it;
405  }
406  }
407  return (*this);
408  }
410  {
411  const_pos_iterator Tmp = *this;
412  return Tmp += n;
413  }
415  {
416  return (*this += -n);
417  }
419  {
420  return this + -n;
421  }
423  {
424  return m_position - it.m_position;
425  }
426  bool operator==(const const_pos_iterator& it) const
427  {
428  return m_position == it.m_position;
429  }
430  bool operator!=(const const_pos_iterator& it) const
431  {
432  return ! (*this==it);
433  }
434  bool operator<(const const_pos_iterator& it) const
435  {
436  return m_position < it.m_position;
437  }
438  bool operator>(const const_pos_iterator& it) const
439  {
440  return m_position > it.m_position;
441  }
442  bool operator<=(const const_pos_iterator& it) const
443  {
444  return m_position <= it.m_position;
445  }
446  bool operator>=(const const_pos_iterator& it) const
447  {
448  return m_position >= it.m_position;
449  }
450  protected:
451  void Inc()
452  {
453  ++m_position;
454  if (m_range_it->GetTo() < m_position) {
455  ++m_range_it;
456  }
457  }
458  void Dec()
459  {
460  --m_position;
461  if (m_position < m_range_it->GetFrom()) {
462  --m_range_it;
463  }
464  }
465  protected:
467  const_iterator m_range_it;
468  };
469 
470 protected:
471 
472 };
473 
475 
476 /* @} */
477 
478 #endif // GUI_WIDGETS_DATA___ATTR_RANGE_COLL__HPP
Include a standard set of the NCBI C++ Toolkit most basic headers.
bool operator<(const CEquivRange &A, const CEquivRange &B)
static DLIST_TYPE *DLIST_NAME() first(DLIST_LIST_TYPE *list)
Definition: dlist.tmpl.h:46
static DLIST_TYPE *DLIST_NAME() last(DLIST_LIST_TYPE *list)
Definition: dlist.tmpl.h:51
unsigned int TSeqPos
Type for sequence locations and lengths.
Definition: ncbimisc.hpp:875
int TSignedSeqPos
Type for signed sequence position.
Definition: ncbimisc.hpp:887
const attr_type & GetAttr() const
CAttrRangeCollection(TSeqPos start)
void SetFrom(TSeqPos start)
const element_proxy & operator*() const
position_type GetFrom() const
size_type size() const
bool operator!=(const const_pos_iterator &it) const
void push_back(const attr_type &attr, TSeqPos len)
const_iterator(vec_const_iterator itEl)
bool operator<(const Position &pos) const
const_pos_iterator begin_pos() const
bool operator!=(const const_iterator &it) const
bool operator>=(const const_pos_iterator &it) const
random_access_iterator_tag iterator_category
bool operator<(const const_pos_iterator &it) const
const_pos_iterator operator-(int n) const
const_iterator end() const
const_pos_iterator end_pos() const
TSignedSeqPos operator-(const const_pos_iterator &it) const
position_type GetLength(void) const
const element_proxy operator[](size_type pos) const
const_pos_iterator & operator+=(int n)
void swap(CAttrRangeCollection &c)
const_iterator find(const_iterator first, const_iterator last, position_type pos) const
const_iterator(const const_iterator &it)
const_pos_iterator(const const_pos_iterator &it)
bool operator==(const const_iterator &it) const
TElementVector::size_type size_type
contains one extra element that is used to represent open end pos
position_type GetTo() const
const_pos_iterator operator+(int n) const
element_proxy(const element_proxy &ep)
void push_back(const attr_type &attr)
TElementVector::const_iterator vec_const_iterator
bool operator<=(const const_pos_iterator &it) const
const_iterator find(position_type pos) const
bool RefEquals(const element_proxy &ep) const
bool operator==(const const_pos_iterator &it) const
const_pos_iterator & operator-=(int n)
const_iterator begin() const
const element_proxy * operator->() const
vector< SElement > TElementVector
SAttrRangeCollectionElement< Attr, Position > SElement
bool operator>(const const_pos_iterator &it) const
vec_const_iterator x_Find(vec_const_iterator first, vec_const_iterator last, position_type pos) const
CAttrRangeCollection< attr_type, position_type > TThisType
position_type GetToOpen() const
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
yy_size_t n
int len
The NCBI C++/STL use hints.
TAttrRangeCollection represents a sequence of continious attributed intervals.
#define N
Definition: crc32.c:57
Modified on Fri Apr 12 17:17:20 2024 by modify_doxy.py rev. 669887