// -*- C++ -*-
|
// -*- C++ -*-
|
|
|
// Copyright (C) 2005, 2006, 2009 Free Software Foundation, Inc.
|
// Copyright (C) 2005, 2006, 2009 Free Software Foundation, Inc.
|
//
|
//
|
// This file is part of the GNU ISO C++ Library. This library is free
|
// This file is part of the GNU ISO C++ Library. This library is free
|
// software; you can redistribute it and/or modify it under the terms
|
// software; you can redistribute it and/or modify it under the terms
|
// of the GNU General Public License as published by the Free Software
|
// of the GNU General Public License as published by the Free Software
|
// Foundation; either version 3, or (at your option) any later
|
// Foundation; either version 3, or (at your option) any later
|
// version.
|
// version.
|
|
|
// This library is distributed in the hope that it will be useful, but
|
// This library is distributed in the hope that it will be useful, but
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
// General Public License for more details.
|
// General Public License for more details.
|
|
|
// Under Section 7 of GPL version 3, you are granted additional
|
// Under Section 7 of GPL version 3, you are granted additional
|
// permissions described in the GCC Runtime Library Exception, version
|
// permissions described in the GCC Runtime Library Exception, version
|
// 3.1, as published by the Free Software Foundation.
|
// 3.1, as published by the Free Software Foundation.
|
|
|
// You should have received a copy of the GNU General Public License and
|
// You should have received a copy of the GNU General Public License and
|
// a copy of the GCC Runtime Library Exception along with this program;
|
// a copy of the GCC Runtime Library Exception along with this program;
|
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
// <http://www.gnu.org/licenses/>.
|
// <http://www.gnu.org/licenses/>.
|
|
|
// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
|
// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
|
|
|
// Permission to use, copy, modify, sell, and distribute this software
|
// Permission to use, copy, modify, sell, and distribute this software
|
// is hereby granted without fee, provided that the above copyright
|
// is hereby granted without fee, provided that the above copyright
|
// notice appears in all copies, and that both that copyright notice
|
// notice appears in all copies, and that both that copyright notice
|
// and this permission notice appear in supporting documentation. None
|
// and this permission notice appear in supporting documentation. None
|
// of the above authors, nor IBM Haifa Research Laboratories, make any
|
// of the above authors, nor IBM Haifa Research Laboratories, make any
|
// representation about the suitability of this software for any
|
// representation about the suitability of this software for any
|
// purpose. It is provided "as is" without express or implied
|
// purpose. It is provided "as is" without express or implied
|
// warranty.
|
// warranty.
|
|
|
/**
|
/**
|
* @file rc.hpp
|
* @file rc.hpp
|
* Contains a redundant (binary counter).
|
* Contains a redundant (binary counter).
|
*/
|
*/
|
|
|
#ifndef PB_DS_RC_HPP
|
#ifndef PB_DS_RC_HPP
|
#define PB_DS_RC_HPP
|
#define PB_DS_RC_HPP
|
|
|
namespace __gnu_pbds
|
namespace __gnu_pbds
|
{
|
{
|
namespace detail
|
namespace detail
|
{
|
{
|
|
|
#define PB_DS_CLASS_T_DEC \
|
#define PB_DS_CLASS_T_DEC \
|
template<typename Node, class Allocator>
|
template<typename Node, class Allocator>
|
|
|
#define PB_DS_CLASS_C_DEC \
|
#define PB_DS_CLASS_C_DEC \
|
rc<Node, Allocator>
|
rc<Node, Allocator>
|
|
|
template<typename Node, class Allocator>
|
template<typename Node, class Allocator>
|
class rc
|
class rc
|
{
|
{
|
private:
|
private:
|
typedef Allocator allocator_type;
|
typedef Allocator allocator_type;
|
|
|
typedef typename allocator_type::size_type size_type;
|
typedef typename allocator_type::size_type size_type;
|
|
|
typedef Node node;
|
typedef Node node;
|
|
|
typedef
|
typedef
|
typename allocator_type::template rebind<
|
typename allocator_type::template rebind<
|
node>::other::pointer
|
node>::other::pointer
|
node_pointer;
|
node_pointer;
|
|
|
typedef
|
typedef
|
typename allocator_type::template rebind<
|
typename allocator_type::template rebind<
|
node_pointer>::other::pointer
|
node_pointer>::other::pointer
|
entry_pointer;
|
entry_pointer;
|
|
|
typedef
|
typedef
|
typename allocator_type::template rebind<
|
typename allocator_type::template rebind<
|
node_pointer>::other::const_pointer
|
node_pointer>::other::const_pointer
|
const_entry_pointer;
|
const_entry_pointer;
|
|
|
enum
|
enum
|
{
|
{
|
max_entries = sizeof(size_type) << 3
|
max_entries = sizeof(size_type) << 3
|
};
|
};
|
|
|
public:
|
public:
|
typedef node_pointer entry;
|
typedef node_pointer entry;
|
|
|
typedef const_entry_pointer const_iterator;
|
typedef const_entry_pointer const_iterator;
|
|
|
public:
|
public:
|
rc();
|
rc();
|
|
|
rc(const PB_DS_CLASS_C_DEC& other);
|
rc(const PB_DS_CLASS_C_DEC& other);
|
|
|
inline void
|
inline void
|
swap(PB_DS_CLASS_C_DEC& other);
|
swap(PB_DS_CLASS_C_DEC& other);
|
|
|
inline void
|
inline void
|
push(entry p_nd);
|
push(entry p_nd);
|
|
|
inline node_pointer
|
inline node_pointer
|
top() const;
|
top() const;
|
|
|
inline void
|
inline void
|
pop();
|
pop();
|
|
|
inline bool
|
inline bool
|
empty() const;
|
empty() const;
|
|
|
inline size_type
|
inline size_type
|
size() const;
|
size() const;
|
|
|
void
|
void
|
clear();
|
clear();
|
|
|
const const_iterator
|
const const_iterator
|
begin() const;
|
begin() const;
|
|
|
const const_iterator
|
const const_iterator
|
end() const;
|
end() const;
|
|
|
#ifdef _GLIBCXX_DEBUG
|
#ifdef _GLIBCXX_DEBUG
|
void
|
void
|
assert_valid() const;
|
assert_valid() const;
|
#endif
|
#endif
|
|
|
#ifdef PB_DS_RC_BINOMIAL_HEAP_TRACE_
|
#ifdef PB_DS_RC_BINOMIAL_HEAP_TRACE_
|
void
|
void
|
trace() const;
|
trace() const;
|
#endif
|
#endif
|
|
|
private:
|
private:
|
node_pointer m_a_entries[max_entries];
|
node_pointer m_a_entries[max_entries];
|
|
|
size_type m_over_top;
|
size_type m_over_top;
|
};
|
};
|
|
|
PB_DS_CLASS_T_DEC
|
PB_DS_CLASS_T_DEC
|
PB_DS_CLASS_C_DEC::
|
PB_DS_CLASS_C_DEC::
|
rc() : m_over_top(0)
|
rc() : m_over_top(0)
|
{ _GLIBCXX_DEBUG_ONLY(assert_valid();) }
|
{ _GLIBCXX_DEBUG_ONLY(assert_valid();) }
|
|
|
PB_DS_CLASS_T_DEC
|
PB_DS_CLASS_T_DEC
|
PB_DS_CLASS_C_DEC::
|
PB_DS_CLASS_C_DEC::
|
rc(const PB_DS_CLASS_C_DEC& other) : m_over_top(0)
|
rc(const PB_DS_CLASS_C_DEC& other) : m_over_top(0)
|
{ _GLIBCXX_DEBUG_ONLY(assert_valid();) }
|
{ _GLIBCXX_DEBUG_ONLY(assert_valid();) }
|
|
|
PB_DS_CLASS_T_DEC
|
PB_DS_CLASS_T_DEC
|
inline void
|
inline void
|
PB_DS_CLASS_C_DEC::
|
PB_DS_CLASS_C_DEC::
|
swap(PB_DS_CLASS_C_DEC& other)
|
swap(PB_DS_CLASS_C_DEC& other)
|
{
|
{
|
_GLIBCXX_DEBUG_ONLY(assert_valid();)
|
_GLIBCXX_DEBUG_ONLY(assert_valid();)
|
_GLIBCXX_DEBUG_ONLY(other.assert_valid();)
|
_GLIBCXX_DEBUG_ONLY(other.assert_valid();)
|
|
|
const size_type over_top = std::max(m_over_top, other.m_over_top);
|
const size_type over_top = std::max(m_over_top, other.m_over_top);
|
|
|
for (size_type i = 0; i < over_top; ++i)
|
for (size_type i = 0; i < over_top; ++i)
|
std::swap(m_a_entries[i], other.m_a_entries[i]);
|
std::swap(m_a_entries[i], other.m_a_entries[i]);
|
|
|
std::swap(m_over_top, other.m_over_top);
|
std::swap(m_over_top, other.m_over_top);
|
_GLIBCXX_DEBUG_ONLY(assert_valid();)
|
_GLIBCXX_DEBUG_ONLY(assert_valid();)
|
_GLIBCXX_DEBUG_ONLY(other.assert_valid();)
|
_GLIBCXX_DEBUG_ONLY(other.assert_valid();)
|
}
|
}
|
|
|
PB_DS_CLASS_T_DEC
|
PB_DS_CLASS_T_DEC
|
inline void
|
inline void
|
PB_DS_CLASS_C_DEC::
|
PB_DS_CLASS_C_DEC::
|
push(entry p_nd)
|
push(entry p_nd)
|
{
|
{
|
_GLIBCXX_DEBUG_ONLY(assert_valid();)
|
_GLIBCXX_DEBUG_ONLY(assert_valid();)
|
_GLIBCXX_DEBUG_ASSERT(m_over_top < max_entries);
|
_GLIBCXX_DEBUG_ASSERT(m_over_top < max_entries);
|
m_a_entries[m_over_top++] = p_nd;
|
m_a_entries[m_over_top++] = p_nd;
|
_GLIBCXX_DEBUG_ONLY(assert_valid();)
|
_GLIBCXX_DEBUG_ONLY(assert_valid();)
|
}
|
}
|
|
|
PB_DS_CLASS_T_DEC
|
PB_DS_CLASS_T_DEC
|
inline void
|
inline void
|
PB_DS_CLASS_C_DEC::
|
PB_DS_CLASS_C_DEC::
|
pop()
|
pop()
|
{
|
{
|
_GLIBCXX_DEBUG_ONLY(assert_valid();)
|
_GLIBCXX_DEBUG_ONLY(assert_valid();)
|
_GLIBCXX_DEBUG_ASSERT(!empty());
|
_GLIBCXX_DEBUG_ASSERT(!empty());
|
--m_over_top;
|
--m_over_top;
|
_GLIBCXX_DEBUG_ONLY(assert_valid();)
|
_GLIBCXX_DEBUG_ONLY(assert_valid();)
|
}
|
}
|
|
|
PB_DS_CLASS_T_DEC
|
PB_DS_CLASS_T_DEC
|
inline typename PB_DS_CLASS_C_DEC::node_pointer
|
inline typename PB_DS_CLASS_C_DEC::node_pointer
|
PB_DS_CLASS_C_DEC::
|
PB_DS_CLASS_C_DEC::
|
top() const
|
top() const
|
{
|
{
|
_GLIBCXX_DEBUG_ONLY(assert_valid();)
|
_GLIBCXX_DEBUG_ONLY(assert_valid();)
|
_GLIBCXX_DEBUG_ASSERT(!empty());
|
_GLIBCXX_DEBUG_ASSERT(!empty());
|
return *(m_a_entries + m_over_top - 1);
|
return *(m_a_entries + m_over_top - 1);
|
}
|
}
|
|
|
PB_DS_CLASS_T_DEC
|
PB_DS_CLASS_T_DEC
|
inline bool
|
inline bool
|
PB_DS_CLASS_C_DEC::
|
PB_DS_CLASS_C_DEC::
|
empty() const
|
empty() const
|
{
|
{
|
_GLIBCXX_DEBUG_ONLY(assert_valid();)
|
_GLIBCXX_DEBUG_ONLY(assert_valid();)
|
return m_over_top == 0;
|
return m_over_top == 0;
|
}
|
}
|
|
|
PB_DS_CLASS_T_DEC
|
PB_DS_CLASS_T_DEC
|
inline typename PB_DS_CLASS_C_DEC::size_type
|
inline typename PB_DS_CLASS_C_DEC::size_type
|
PB_DS_CLASS_C_DEC::
|
PB_DS_CLASS_C_DEC::
|
size() const
|
size() const
|
{ return m_over_top; }
|
{ return m_over_top; }
|
|
|
PB_DS_CLASS_T_DEC
|
PB_DS_CLASS_T_DEC
|
void
|
void
|
PB_DS_CLASS_C_DEC::
|
PB_DS_CLASS_C_DEC::
|
clear()
|
clear()
|
{
|
{
|
_GLIBCXX_DEBUG_ONLY(assert_valid();)
|
_GLIBCXX_DEBUG_ONLY(assert_valid();)
|
m_over_top = 0;
|
m_over_top = 0;
|
_GLIBCXX_DEBUG_ONLY(assert_valid();)
|
_GLIBCXX_DEBUG_ONLY(assert_valid();)
|
}
|
}
|
|
|
PB_DS_CLASS_T_DEC
|
PB_DS_CLASS_T_DEC
|
const typename PB_DS_CLASS_C_DEC::const_iterator
|
const typename PB_DS_CLASS_C_DEC::const_iterator
|
PB_DS_CLASS_C_DEC::
|
PB_DS_CLASS_C_DEC::
|
begin() const
|
begin() const
|
{ return& m_a_entries[0]; }
|
{ return& m_a_entries[0]; }
|
|
|
PB_DS_CLASS_T_DEC
|
PB_DS_CLASS_T_DEC
|
const typename PB_DS_CLASS_C_DEC::const_iterator
|
const typename PB_DS_CLASS_C_DEC::const_iterator
|
PB_DS_CLASS_C_DEC::
|
PB_DS_CLASS_C_DEC::
|
end() const
|
end() const
|
{ return& m_a_entries[m_over_top]; }
|
{ return& m_a_entries[m_over_top]; }
|
|
|
#ifdef _GLIBCXX_DEBUG
|
#ifdef _GLIBCXX_DEBUG
|
PB_DS_CLASS_T_DEC
|
PB_DS_CLASS_T_DEC
|
void
|
void
|
PB_DS_CLASS_C_DEC::
|
PB_DS_CLASS_C_DEC::
|
assert_valid() const
|
assert_valid() const
|
{ _GLIBCXX_DEBUG_ASSERT(m_over_top < max_entries); }
|
{ _GLIBCXX_DEBUG_ASSERT(m_over_top < max_entries); }
|
#endif
|
#endif
|
|
|
#ifdef PB_DS_RC_BINOMIAL_HEAP_TRACE_
|
#ifdef PB_DS_RC_BINOMIAL_HEAP_TRACE_
|
PB_DS_CLASS_T_DEC
|
PB_DS_CLASS_T_DEC
|
void
|
void
|
PB_DS_CLASS_C_DEC::
|
PB_DS_CLASS_C_DEC::
|
trace() const
|
trace() const
|
{
|
{
|
std::cout << "rc" << std::endl;
|
std::cout << "rc" << std::endl;
|
for (size_type i = 0; i < m_over_top; ++i)
|
for (size_type i = 0; i < m_over_top; ++i)
|
std::cerr << m_a_entries[i] << std::endl;
|
std::cerr << m_a_entries[i] << std::endl;
|
std::cout << std::endl;
|
std::cout << std::endl;
|
}
|
}
|
#endif
|
#endif
|
|
|
#undef PB_DS_CLASS_T_DEC
|
#undef PB_DS_CLASS_T_DEC
|
#undef PB_DS_CLASS_C_DEC
|
#undef PB_DS_CLASS_C_DEC
|
|
|
} // namespace detail
|
} // namespace detail
|
} // namespace __gnu_pbds
|
} // namespace __gnu_pbds
|
|
|
#endif
|
#endif
|
|
|