libstdc++
rc.hpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2005, 2006, 2009, 2011 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the terms
00007 // of the GNU General Public License as published by the Free Software
00008 // Foundation; either version 3, or (at your option) any later
00009 // version.
00010 
00011 // This library is distributed in the hope that it will be useful, but
00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
00026 
00027 // Permission to use, copy, modify, sell, and distribute this software
00028 // is hereby granted without fee, provided that the above copyright
00029 // notice appears in all copies, and that both that copyright notice
00030 // and this permission notice appear in supporting documentation. None
00031 // of the above authors, nor IBM Haifa Research Laboratories, make any
00032 // representation about the suitability of this software for any
00033 // purpose. It is provided "as is" without express or implied
00034 // warranty.
00035 
00036 /**
00037  * @file rc_binomial_heap_/rc.hpp
00038  * Contains a redundant (binary counter).
00039  */
00040 
00041 #ifndef PB_DS_RC_HPP
00042 #define PB_DS_RC_HPP
00043 
00044 namespace __gnu_pbds
00045 {
00046   namespace detail
00047   {
00048     /// Redundant binary counter.
00049     template<typename _Node, typename _Alloc>
00050     class rc
00051     {
00052     private:
00053       typedef _Alloc                     allocator_type;
00054       typedef typename allocator_type::size_type     size_type;
00055       typedef _Node                      node;
00056 
00057       typedef typename _Alloc::template rebind<node>     __rebind_n;
00058       typedef typename __rebind_n::other::pointer        node_pointer;
00059 
00060       typedef typename _Alloc::template rebind<node_pointer>  __rebind_np;
00061 
00062       typedef typename __rebind_np::other::pointer   entry_pointer;
00063       typedef typename __rebind_np::other::const_pointer entry_const_pointer;
00064 
00065       enum
00066     {
00067       max_entries = sizeof(size_type) << 3
00068     };
00069 
00070     public:
00071       typedef node_pointer               entry;
00072       typedef entry_const_pointer            const_iterator;
00073 
00074       rc();
00075 
00076       rc(const rc&);
00077 
00078       inline void
00079       swap(rc&);
00080 
00081       inline void
00082       push(entry);
00083 
00084       inline node_pointer
00085       top() const;
00086 
00087       inline void
00088       pop();
00089 
00090       inline bool
00091       empty() const;
00092 
00093       inline size_type
00094       size() const;
00095 
00096       void
00097       clear();
00098 
00099       const const_iterator
00100       begin() const;
00101 
00102       const const_iterator
00103       end() const;
00104 
00105 #ifdef _GLIBCXX_DEBUG
00106       void
00107       assert_valid(const char*, int) const;
00108 #endif
00109 
00110 #ifdef PB_DS_RC_BINOMIAL_HEAP_TRACE_
00111       void
00112       trace() const;
00113 #endif
00114 
00115     private:
00116       node_pointer  m_a_entries[max_entries];
00117       size_type     m_over_top;
00118     };
00119 
00120     template<typename _Node, typename _Alloc>
00121     rc<_Node, _Alloc>::
00122     rc() : m_over_top(0)
00123     { PB_DS_ASSERT_VALID((*this)) }
00124 
00125     template<typename _Node, typename _Alloc>
00126     rc<_Node, _Alloc>::
00127     rc(const rc<_Node, _Alloc>& other) : m_over_top(0)
00128     { PB_DS_ASSERT_VALID((*this)) }
00129 
00130     template<typename _Node, typename _Alloc>
00131     inline void
00132     rc<_Node, _Alloc>::
00133     swap(rc<_Node, _Alloc>& other)
00134     {
00135       PB_DS_ASSERT_VALID((*this))
00136       PB_DS_ASSERT_VALID(other)
00137 
00138       const size_type over_top = std::max(m_over_top, other.m_over_top);
00139 
00140       for (size_type i = 0; i < over_top; ++i)
00141     std::swap(m_a_entries[i], other.m_a_entries[i]);
00142 
00143       std::swap(m_over_top, other.m_over_top);
00144       PB_DS_ASSERT_VALID((*this))
00145       PB_DS_ASSERT_VALID(other)
00146      }
00147 
00148     template<typename _Node, typename _Alloc>
00149     inline void
00150     rc<_Node, _Alloc>::
00151     push(entry p_nd)
00152     {
00153       PB_DS_ASSERT_VALID((*this))
00154       _GLIBCXX_DEBUG_ASSERT(m_over_top < max_entries);
00155       m_a_entries[m_over_top++] = p_nd;
00156       PB_DS_ASSERT_VALID((*this))
00157     }
00158 
00159     template<typename _Node, typename _Alloc>
00160     inline void
00161     rc<_Node, _Alloc>::
00162     pop()
00163     {
00164       PB_DS_ASSERT_VALID((*this))
00165       _GLIBCXX_DEBUG_ASSERT(!empty());
00166       --m_over_top;
00167       PB_DS_ASSERT_VALID((*this))
00168     }
00169 
00170     template<typename _Node, typename _Alloc>
00171     inline typename rc<_Node, _Alloc>::node_pointer
00172     rc<_Node, _Alloc>::
00173     top() const
00174     {
00175       PB_DS_ASSERT_VALID((*this))
00176       _GLIBCXX_DEBUG_ASSERT(!empty());
00177       return *(m_a_entries + m_over_top - 1);
00178     }
00179 
00180     template<typename _Node, typename _Alloc>
00181     inline bool
00182     rc<_Node, _Alloc>::
00183     empty() const
00184     {
00185       PB_DS_ASSERT_VALID((*this))
00186       return m_over_top == 0;
00187     }
00188 
00189     template<typename _Node, typename _Alloc>
00190     inline typename rc<_Node, _Alloc>::size_type
00191     rc<_Node, _Alloc>::
00192     size() const
00193     { return m_over_top; }
00194 
00195     template<typename _Node, typename _Alloc>
00196     void
00197     rc<_Node, _Alloc>::
00198     clear()
00199     {
00200       PB_DS_ASSERT_VALID((*this))
00201       m_over_top = 0;
00202       PB_DS_ASSERT_VALID((*this))
00203     }
00204 
00205     template<typename _Node, typename _Alloc>
00206     const typename rc<_Node, _Alloc>::const_iterator
00207     rc<_Node, _Alloc>::
00208     begin() const
00209     { return& m_a_entries[0]; }
00210 
00211     template<typename _Node, typename _Alloc>
00212     const typename rc<_Node, _Alloc>::const_iterator
00213     rc<_Node, _Alloc>::
00214     end() const
00215     { return& m_a_entries[m_over_top]; }
00216 
00217 #ifdef _GLIBCXX_DEBUG
00218     template<typename _Node, typename _Alloc>
00219     void
00220     rc<_Node, _Alloc>::
00221     assert_valid(const char* __file, int __line) const
00222     { PB_DS_DEBUG_VERIFY(m_over_top < max_entries); }
00223 #endif
00224 
00225 #ifdef PB_DS_RC_BINOMIAL_HEAP_TRACE_
00226     template<typename _Node, typename _Alloc>
00227     void
00228     rc<_Node, _Alloc>::
00229     trace() const
00230     {
00231       std::cout << "rc" << std::endl;
00232       for (size_type i = 0; i < m_over_top; ++i)
00233     std::cerr << m_a_entries[i] << std::endl;
00234       std::cout << std::endl;
00235     }
00236 #endif
00237 } // namespace detail
00238 } // namespace __gnu_pbds
00239 
00240 #endif