attr_range_coll.hpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:9k
- /*
- * ===========================================================================
- * PRODUCTION $Log: attr_range_coll.hpp,v $
- * PRODUCTION Revision 1000.2 2004/06/01 19:51:46 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.4
- * PRODUCTION
- * ===========================================================================
- */
- #ifndef GUI_WIDGETS_ALN_MULTIPLE___ATTR_RANGE_COLL__HPP
- #define GUI_WIDGETS_ALN_MULTIPLE___ATTR_RANGE_COLL__HPP
- /* $Id: attr_range_coll.hpp,v 1000.2 2004/06/01 19:51:46 gouriano Exp $
- * ===========================================================================
- *
- * PUBLIC DOMAIN NOTICE
- * National Center for Biotechnology Information
- *
- * This software/database is a "United States Government Work" under the
- * terms of the United States Copyright Act. It was written as part of
- * the author's official duties as a United States Government employee and
- * thus cannot be copyrighted. This software/database is freely available
- * to the public for use. The National Library of Medicine and the U.S.
- * Government have not placed any restriction on its use or reproduction.
- *
- * Although all reasonable efforts have been taken to ensure the accuracy
- * and reliability of the software and data, the NLM and the U.S.
- * Government do not and cannot warrant the performance or results that
- * may be obtained by using this software or data. The NLM and the U.S.
- * Government disclaim all warranties, express or implied, including
- * warranties of performance, merchantability or fitness for any particular
- * purpose.
- *
- * Please cite the author in any work or product based on this material.
- *
- * ===========================================================================
- *
- * Authors: Andrey Yazhuk
- *
- */
- #include <corelib/ncbistd.hpp>
- #include <corelib/ncbistl.hpp>
- #include <algorithm>
- BEGIN_NCBI_SCOPE
- ///////////////////////////////////////////////////////////////////////////////
- /// TAttrRangeCollection represents a sequence of continious attributed intervals.
- /// Intervals are neither intersecting nor separated by gaps.
- template<class Attr, class Position>
- class CAttrRangeCollection
- {
- public:
- typedef Attr attr_type;
- typedef Position position_type;
- typedef CAttrRangeCollection<attr_type, position_type> TThisType;
-
- protected:
- struct SElement
- {
- position_type m_Start;
- attr_type m_Attr;
- bool operator<(position_type pos) const
- {
- return m_Start < pos;
- }
- };
-
- typedef vector<SElement> TElementVector;
- TElementVector m_vElems;
- /// contains one extra element that is used to represent open end pos
- public:
- typedef typename TElementVector::size_type size_type;
- typedef typename TElementVector::const_iterator vec_const_iterator;
- class const_iterator;
- class element_proxy;
- CAttrRangeCollection()
- {
- SetFrom(0);
- }
- CAttrRangeCollection(TSeqPos start)
- {
- SetFrom(start);
- }
- void SetFrom(TSeqPos start)
- {
- if(m_vElems.size() == 0) {
- m_vElems.push_back(SElement());
- }
- else if(m_vElems.size() > 1) {
- clear();
- }
- m_vElems.back().m_Start = start;
- }
- void push_back(const attr_type& attr)
- {
- push_back(attr, 1);
- }
- void push_back(const attr_type& attr, TSeqPos len)
- {
- int N = m_vElems.size();
- if((N > 1) && (m_vElems[N-2].m_Attr == attr)) {
- m_vElems[N-1].m_Start += len; //extend
- } else {
- m_vElems[N-1].m_Attr = attr;
- m_vElems.push_back(SElement());
- m_vElems.back().m_Start = m_vElems[N-1].m_Start + len;
- }
- }
- const_iterator begin() const
- {
- return const_iterator(m_vElems.begin());
- }
- const_iterator end() const
- {
- return const_iterator(m_vElems.end() - 1);
- }
- size_type size() const
- {
- return m_vElems.size() - 1;
- }
- bool empty() const
- {
- return m_vElems.size() == 1;
- }
- const element_proxy operator[](size_type pos) const
- {
- return element_proxy(m_vElems.begin() + pos);
- }
- void clear()
- {
- m_vElems.begin()->m_Start = m_vElems.back().m_Start;
- m_vElems.resize(1);
- }
- const_iterator find(position_type pos) const
- {
- vec_const_iterator it = x_Find(m_vElems.begin(), m_vElems.end(), pos);
- return const_iterator(it);
- }
- const_iterator find(const_iterator first, const_iterator last, position_type pos) const
- {
- vec_const_iterator it = x_Find(*first, *last, pos);
- return const_iterator(it);
- }
- position_type GetFrom() const
- {
- return m_vElems.begin()->m_Start;
- }
- position_type GetToOpen() const
- {
- return m_vElems.back().m_Start;
- }
- position_type GetTo() const
- {
- return m_vElems.back().m_Start - 1;
- }
- position_type GetLength (void) const
- {
- return m_vElems.back().m_Start - m_vElems.begin().m_Start;
- }
- protected:
- vec_const_iterator x_Find(vec_const_iterator first, vec_const_iterator last,
- position_type pos) const
- {
- if(first != last) {
- if(pos >= first->m_Start && pos < last->m_Start) {
- // pos lies with in the collection
- vec_const_iterator it = lower_bound(first, last, pos);
- return (it->m_Start > pos) ? --it : it;
- }
- }
- return m_vElems.end() - 1;
- }
- public:
- // represents virtual element
- class element_proxy
- {
- public:
- element_proxy(vec_const_iterator it)
- : m_itEl(it)
- {
- }
- element_proxy(const element_proxy& ep)
- : m_itEl(ep.m_itEl)
- {
- }
- TSeqPos GetStart() const
- {
- return m_itEl->m_Start;
- }
- TSeqPos GetTo() const
- {
- return (m_itEl + 1)->m_Start - 1;
- }
- TSeqPos GetToOpen() const
- {
- return (m_itEl + 1)->m_Start;
- }
- TSeqPos GetLength() const
- {
- return (m_itEl + 1)->m_Start - m_itEl->m_Start;
- }
- const attr_type& GetAttr() const
- {
- return m_itEl->m_Attr;
- }
- bool RefEquals(const element_proxy& ep) const
- {
- return m_itEl == ep.m_itEl;
- }
- operator vec_const_iterator () const
- {
- return m_itEl;
- }
- void Inc()
- {
- ++m_itEl;
- }
- void Dec()
- {
- --m_itEl;
- }
- protected:
- vec_const_iterator m_itEl;
- };
- class const_iterator
- {
- friend class CAttrRangeCollection;
- public:
- const_iterator(vec_const_iterator itEl)
- : m_proxy(itEl)
- {
- }
- const_iterator(const const_iterator& it)
- : m_proxy(it.m_proxy)
- {
- }
- const element_proxy& operator*() const
- {
- return m_proxy;
- }
- const element_proxy* operator->() const
- {
- return &m_proxy;
- }
- const_iterator& operator++()
- {
- Inc();
- return (*this);
- }
- const_iterator operator++(int)
- {
- const_iterator Tmp = *this;
- Inc();
- return Tmp;
- }
- const_iterator& operator--()
- {
- Dec();
- return (*this);
- }
- const_iterator operator--(int)
- {
- const_iterator Tmp = *this;
- Dec();
- return Tmp;
- }
- bool operator==(const const_iterator& it) const
- {
- return m_proxy.RefEquals(it.m_proxy);
- }
- bool operator!=(const const_iterator& it) const
- {
- return ! (*this==it);
- }
- protected:
- void Inc()
- {
- m_proxy.Inc();
- }
- void Dec()
- {
- m_proxy.Dec();
- }
- protected:
- element_proxy m_proxy;
- };
- protected:
-
- };
- END_NCBI_SCOPE
- /*
- * ===========================================================================
- * $Log: attr_range_coll.hpp,v $
- * Revision 1000.2 2004/06/01 19:51:46 gouriano
- * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.4
- *
- * Revision 1.4 2004/04/26 17:31:25 ucko
- * Fix a typo in operator[].
- *
- * Revision 1.3 2003/11/17 21:16:50 yazhuk
- * Cosmetic changes to function argument names
- *
- * Revision 1.2 2003/10/29 23:37:20 yazhuk
- * Changed comments
- *
- * Revision 1.1 2003/10/10 18:56:26 yazhuk
- * Initial revision
- *
- * ===========================================================================
- */
- #endif // GUI_WIDGETS_ALN_MULTIPLE___ATTR_RANGE_COLL__HPP