type_utils.hpp (numeric_traits): Add, const expression interface to std::numeric_limits::min and max functions.

2006-06-15  Benjamin Kosnik  <bkoz@redhat.com>

	* include/ext/pb_ds/detail/type_utils.hpp (numeric_traits): Add,
	const expression interface to std::numeric_limits::min and max
	functions.
	* include/ext/pb_ds/trie_policy.hpp (string_trie_e_access_traits):
	Use it.

	* include/ext/pb_ds/detail/resize_policy/
	hash_load_check_resize_trigger_imp.hpp: Format.
	* include/ext/pb_ds/detail/pat_trie_/internal_node.hpp: Same.

From-SVN: r114706
This commit is contained in:
Benjamin Kosnik 2006-06-16 06:32:22 +00:00 committed by Benjamin Kosnik
parent 4e95268d59
commit 81ee09de66
5 changed files with 248 additions and 400 deletions

View File

@ -1,3 +1,15 @@
2006-06-15 Benjamin Kosnik <bkoz@redhat.com>
* include/ext/pb_ds/detail/type_utils.hpp (numeric_traits): Add,
const expression interface to std::numeric_limits::min and max
functions.
* include/ext/pb_ds/trie_policy.hpp (string_trie_e_access_traits):
Use it.
* include/ext/pb_ds/detail/resize_policy/
hash_load_check_resize_trigger_imp.hpp: Format.
* include/ext/pb_ds/detail/pat_trie_/internal_node.hpp: Same.
2006-06-15 Paolo Carlini <pcarlini@suse.de>
* include/tr1/random.tcc (mersenne_twister<>::operator()()):

View File

@ -49,158 +49,97 @@
#ifdef PB_DS_PAT_TRIE_DEBUG_
#include <cassert>
#endif // #ifdef PB_DS_PAT_TRIE_DEBUG_
#endif
namespace pb_ds
{
namespace detail
{
#define PB_DS_CLASS_T_DEC \
template<typename Type_Traits, typename E_Access_Traits, \
typename Metadata, typename Allocator>
#define PB_DS_CLASS_T_DEC \
template< \
class Type_Traits, \
class E_Access_Traits, \
class Metadata, \
class Allocator>
#define PB_DS_CLASS_C_DEC \
pat_trie_internal_node<Type_Traits, E_Access_Traits, Metadata, Allocator>
#define PB_DS_CLASS_C_DEC \
pat_trie_internal_node< \
Type_Traits, \
E_Access_Traits, \
Metadata, \
Allocator>
#define PB_DS_BASE_C_DEC \
pat_trie_node_base<Type_Traits, E_Access_Traits, Metadata, Allocator>
#define PB_DS_BASE_C_DEC \
pat_trie_node_base< \
Type_Traits, \
E_Access_Traits, \
Metadata, \
Allocator>
#define PB_DS_LEAF_C_DEC \
pat_trie_leaf<Type_Traits, E_Access_Traits, Metadata, Allocator>
#define PB_DS_LEAF_C_DEC \
pat_trie_leaf< \
Type_Traits, \
E_Access_Traits, \
Metadata, \
Allocator>
#define PB_DS_STATIC_ASSERT(UNIQUE, E) \
typedef \
static_assert_dumclass< \
sizeof(static_assert<(bool)(E)>)> \
UNIQUE##static_assert_type
#define PB_DS_STATIC_ASSERT(UNIQUE, E) \
typedef static_assert_dumclass<sizeof(static_assert<(bool)(E)>)> UNIQUE##static_assert_type
#ifdef PB_DS_PAT_TRIE_DEBUG_
#define PB_DS_DBG_ASSERT(X) assert(X)
#define PB_DS_DBG_VERIFY(X) assert(X)
#define PB_DS_DBG_ONLY(X) X
#else // #ifdef PB_DS_PAT_TRIE_DEBUG_
#else
#define PB_DS_DBG_ASSERT(X)
#define PB_DS_DBG_VERIFY(X) {if((X)==0);}
#define PB_DS_DBG_VERIFY(X) { if((X)==0); }
#define PB_DS_DBG_ONLY(X) ;
#endif // #ifdef PB_DS_PAT_TRIE_DEBUG_
#endif
template<typename Type_Traits,
class E_Access_Traits,
class Metadata,
class Allocator>
typename E_Access_Traits,
typename Metadata,
typename Allocator>
struct pat_trie_internal_node : public PB_DS_BASE_C_DEC
{
public:
enum
{
arr_size =
E_Access_Traits::max_size + 1
};
private:
typedef typename Allocator::size_type size_type;
typedef PB_DS_BASE_C_DEC base_type;
typedef Type_Traits type_traits;
typedef typename type_traits::value_type value_type;
typedef typename Allocator::size_type size_type;
typedef E_Access_Traits e_access_traits;
typedef
typename Allocator::template rebind<
e_access_traits>::other::const_pointer
const_e_access_traits_pointer;
typedef typename e_access_traits::const_iterator const_e_iterator;
typedef typename Allocator::template rebind<e_access_traits>::other access_rebind;
typedef typename access_rebind::const_pointer const_e_access_traits_pointer;
typedef
typename Allocator::template rebind<
PB_DS_BASE_C_DEC >::other::pointer
node_pointer;
typedef
typename Allocator::template rebind<
PB_DS_BASE_C_DEC >::other::const_pointer
const_node_pointer;
typedef typename Allocator::template rebind<base_type>::other base_rebind;
typedef typename base_rebind::pointer node_pointer;
typedef typename base_rebind::const_pointer const_node_pointer;
typedef PB_DS_LEAF_C_DEC leaf;
typedef typename Allocator::template rebind<leaf>::other leaf_rebind;
typedef typename leaf_rebind::pointer leaf_pointer;
typedef typename leaf_rebind::const_pointer const_leaf_pointer;
typedef
typename Allocator::template rebind<
leaf>::other
leaf_allocator;
typedef typename leaf_allocator::pointer leaf_pointer;
typedef typename leaf_allocator::const_pointer const_leaf_pointer;
typedef
typename Allocator::template rebind<
PB_DS_CLASS_C_DEC>::other
internal_node_allocator;
typedef
typename Allocator::template rebind<
PB_DS_CLASS_C_DEC>::other::const_pointer
const_internal_node_pointer;
typedef
typename Allocator::template rebind<
PB_DS_CLASS_C_DEC>::other::pointer
internal_node_pointer;
PB_DS_STATIC_ASSERT(min_arr_size, arr_size >= 2);
typedef typename Type_Traits::value_type value_type;
typedef typename Allocator::template rebind<pat_trie_internal_node>::other internal_node_rebind;
typedef typename internal_node_rebind::pointer internal_node_pointer;
typedef typename internal_node_rebind::const_pointer const_internal_node_pointer;
#ifdef PB_DS_PAT_TRIE_DEBUG_
typedef
typename PB_DS_BASE_C_DEC::subtree_debug_info
subtree_debug_info;
#endif // #ifdef PB_DS_PAT_TRIE_DEBUG_
typedef typename base_type::subtree_debug_info subtree_debug_info;
typedef PB_DS_BASE_C_DEC base_type;
private:
inline size_type
get_pref_pos(const_e_iterator b_it, const_e_iterator e_it, const_e_access_traits_pointer p_traits) const;
#ifdef PB_DS_PAT_TRIE_DEBUG_
virtual subtree_debug_info
assert_valid_imp(const_e_access_traits_pointer p_traits) const;
#endif // #ifdef PB_DS_PAT_TRIE_DEBUG_
assert_valid_imp(const_e_access_traits_pointer) const;
#endif
inline size_type
get_pref_pos(const_e_iterator, const_e_iterator,
const_e_access_traits_pointer) const;
public:
typedef
typename Allocator::template rebind<
node_pointer>::other::pointer
node_pointer_pointer;
typedef typename Allocator::template rebind<node_pointer>::other node_pointer_rebind;
typedef typename node_pointer_rebind::pointer node_pointer_pointer;
typedef typename node_pointer_rebind::reference node_pointer_reference;
typedef
typename Allocator::template rebind<
node_pointer>::other::reference
node_pointer_reference;
enum
{
arr_size = E_Access_Traits::max_size + 1
};
PB_DS_STATIC_ASSERT(min_arr_size, arr_size >= 2);
#include <ext/pb_ds/detail/pat_trie_/const_child_iterator.hpp>
#include <ext/pb_ds/detail/pat_trie_/child_iterator.hpp>
public:
pat_trie_internal_node(size_type e_ind, const const_e_iterator pref_b_it);
pat_trie_internal_node(size_type, const const_e_iterator);
void
update_prefixes(const_e_access_traits_pointer p_traits);
update_prefixes(const_e_access_traits_pointer);
const_iterator
begin() const;
@ -215,25 +154,30 @@ namespace pb_ds
end();
inline node_pointer
get_child_node(const_e_iterator b_it, const_e_iterator e_it, const_e_access_traits_pointer p_traits);
get_child_node(const_e_iterator, const_e_iterator,
const_e_access_traits_pointer);
inline const_node_pointer
get_child_node(const_e_iterator b_it, const_e_iterator e_it, const_e_access_traits_pointer p_traits) const;
get_child_node(const_e_iterator, const_e_iterator,
const_e_access_traits_pointer) const;
inline iterator
get_child_it(const_e_iterator b_it, const_e_iterator e_it, const_e_access_traits_pointer p_traits);
get_child_it(const_e_iterator, const_e_iterator,
const_e_access_traits_pointer);
inline node_pointer
get_lower_bound_child_node(const_e_iterator b_it, const_e_iterator e_it, size_type checked_ind, const_e_access_traits_pointer p_traits);
get_lower_bound_child_node(const_e_iterator, const_e_iterator,
size_type, const_e_access_traits_pointer);
inline node_pointer
add_child(node_pointer p_nd, const_e_iterator b_it, const_e_iterator e_it, const_e_access_traits_pointer p_traits);
add_child(node_pointer, const_e_iterator, const_e_iterator,
const_e_access_traits_pointer);
inline const_node_pointer
get_join_child(const_node_pointer p_nd, const_e_access_traits_pointer p_traits) const;
get_join_child(const_node_pointer, const_e_access_traits_pointer) const;
inline node_pointer
get_join_child(node_pointer p_nd, const_e_access_traits_pointer p_traits);
get_join_child(node_pointer, const_e_access_traits_pointer);
void
remove_child(node_pointer p_nd);
@ -242,7 +186,8 @@ namespace pb_ds
remove_child(iterator it);
void
replace_child(node_pointer p_nd, const_e_iterator b_it, const_e_iterator e_it, const_e_access_traits_pointer p_traits);
replace_child(node_pointer, const_e_iterator, const_e_iterator,
const_e_access_traits_pointer);
inline const_e_iterator
pref_b_it() const;
@ -254,7 +199,8 @@ namespace pb_ds
get_e_ind() const;
bool
should_be_mine(const_e_iterator b_it, const_e_iterator e_it, size_type checked_ind, const_e_access_traits_pointer p_traits) const;
should_be_mine(const_e_iterator, const_e_iterator, size_type,
const_e_access_traits_pointer) const;
leaf_pointer
leftmost_descendant();
@ -271,58 +217,49 @@ namespace pb_ds
#ifdef PB_DS_PAT_TRIE_DEBUG_
size_type
e_ind() const;
#endif // #ifdef PB_DS_PAT_TRIE_DEBUG_
#endif
private:
pat_trie_internal_node(const PB_DS_CLASS_C_DEC& other);
pat_trie_internal_node(const pat_trie_internal_node&);
size_type
get_begin_pos() const;
private:
const size_type m_e_ind;
const_e_iterator m_pref_b_it;
const_e_iterator m_pref_e_it;
node_pointer m_a_p_children[arr_size];
static leaf_allocator s_leaf_alloc;
static internal_node_allocator s_internal_node_alloc;
static leaf_rebind s_leaf_alloc;
static internal_node_rebind s_internal_node_alloc;
};
PB_DS_CLASS_T_DEC
typename PB_DS_CLASS_C_DEC::leaf_allocator
typename PB_DS_CLASS_C_DEC::leaf_rebind
PB_DS_CLASS_C_DEC::s_leaf_alloc;
PB_DS_CLASS_T_DEC
typename PB_DS_CLASS_C_DEC::internal_node_allocator
typename PB_DS_CLASS_C_DEC::internal_node_rebind
PB_DS_CLASS_C_DEC::s_internal_node_alloc;
PB_DS_CLASS_T_DEC
inline typename PB_DS_CLASS_C_DEC::size_type
PB_DS_CLASS_C_DEC::
get_pref_pos(const_e_iterator b_it, const_e_iterator e_it, const_e_access_traits_pointer p_traits) const
get_pref_pos(const_e_iterator b_it, const_e_iterator e_it,
const_e_access_traits_pointer p_traits) const
{
if (static_cast<size_t>(std::distance(b_it, e_it)) <= m_e_ind)
return (0);
return 0;
std::advance(b_it, m_e_ind);
return (1 + p_traits->e_pos(*b_it));
return 1 + p_traits->e_pos(*b_it);
}
PB_DS_CLASS_T_DEC
PB_DS_CLASS_C_DEC::
pat_trie_internal_node(size_type e_ind, const const_e_iterator pref_b_it) :
PB_DS_BASE_C_DEC(pat_trie_internal_node_type),
m_e_ind(e_ind),
m_pref_b_it(pref_b_it),
m_pref_e_it(pref_b_it)
m_e_ind(e_ind), m_pref_b_it(pref_b_it), m_pref_e_it(pref_b_it)
{
std::advance(m_pref_e_it, m_e_ind);
std::fill(m_a_p_children, m_a_p_children + arr_size,
static_cast<node_pointer>(NULL));
}
@ -332,22 +269,18 @@ namespace pb_ds
PB_DS_CLASS_C_DEC::
update_prefixes(const_e_access_traits_pointer p_traits)
{
node_pointer p_first =* begin();
node_pointer p_first = *begin();
if (p_first->m_type == pat_trie_leaf_node_type)
m_pref_b_it =
p_traits->begin(
E_Access_Traits::extract_key(
static_cast<const_leaf_pointer>(p_first)->value()));
{
const_leaf_pointer p = static_cast<const_leaf_pointer>(p_first);
m_pref_b_it = p_traits->begin(e_access_traits::extract_key(p->value()));
}
else
{
PB_DS_DBG_ASSERT(p_first->m_type == pat_trie_internal_node_type);
m_pref_b_it = static_cast<internal_node_pointer>(p_first)->pref_b_it();
}
m_pref_e_it = m_pref_b_it;
std::advance(m_pref_e_it, m_e_ind);
}
@ -356,9 +289,9 @@ namespace pb_ds
PB_DS_CLASS_C_DEC::
begin() const
{
return (const_iterator(
const_cast<node_pointer_pointer>(m_a_p_children) + get_begin_pos(),
const_cast<node_pointer_pointer>(m_a_p_children) + arr_size));
typedef node_pointer_pointer pointer_type;
pointer_type p = const_cast<pointer_type>(m_a_p_children);
return const_iterator(p + get_begin_pos(), p + arr_size);
}
PB_DS_CLASS_T_DEC
@ -366,9 +299,8 @@ namespace pb_ds
PB_DS_CLASS_C_DEC::
begin()
{
return (iterator(
m_a_p_children + get_begin_pos(),
m_a_p_children + arr_size));
return iterator(m_a_p_children + get_begin_pos(),
m_a_p_children + arr_size);
}
PB_DS_CLASS_T_DEC
@ -376,108 +308,96 @@ namespace pb_ds
PB_DS_CLASS_C_DEC::
end() const
{
return (const_iterator(
const_cast<node_pointer_pointer>(m_a_p_children) + arr_size,
const_cast<node_pointer_pointer>(m_a_p_children) + arr_size));
typedef node_pointer_pointer pointer_type;
pointer_type p = const_cast<pointer_type>(m_a_p_children) + arr_size;
return const_iterator(p, p);
}
PB_DS_CLASS_T_DEC
typename PB_DS_CLASS_C_DEC::iterator
PB_DS_CLASS_C_DEC::
end()
{
return (iterator( m_a_p_children + arr_size, m_a_p_children + arr_size));
}
{ return iterator(m_a_p_children + arr_size, m_a_p_children + arr_size); }
PB_DS_CLASS_T_DEC
inline typename PB_DS_CLASS_C_DEC::node_pointer
PB_DS_CLASS_C_DEC::
get_child_node(const_e_iterator b_it, const_e_iterator e_it, const_e_access_traits_pointer p_traits)
get_child_node(const_e_iterator b_it, const_e_iterator e_it,
const_e_access_traits_pointer p_traits)
{
const size_type i = get_pref_pos( b_it, e_it, p_traits);
const size_type i = get_pref_pos(b_it, e_it, p_traits);
PB_DS_DBG_ASSERT(i < arr_size);
return (m_a_p_children[i]);
return m_a_p_children[i];
}
PB_DS_CLASS_T_DEC
inline typename PB_DS_CLASS_C_DEC::iterator
PB_DS_CLASS_C_DEC::
get_child_it(const_e_iterator b_it, const_e_iterator e_it, const_e_access_traits_pointer p_traits)
get_child_it(const_e_iterator b_it, const_e_iterator e_it,
const_e_access_traits_pointer p_traits)
{
const size_type i = get_pref_pos( b_it, e_it, p_traits);
const size_type i = get_pref_pos(b_it, e_it, p_traits);
PB_DS_DBG_ASSERT(i < arr_size);
PB_DS_DBG_ASSERT(m_a_p_children[i] != NULL);
return (iterator(m_a_p_children + i, m_a_p_children + i));
return iterator(m_a_p_children + i, m_a_p_children + i);
}
PB_DS_CLASS_T_DEC
inline typename PB_DS_CLASS_C_DEC::const_node_pointer
PB_DS_CLASS_C_DEC::
get_child_node(const_e_iterator b_it, const_e_iterator e_it, const_e_access_traits_pointer p_traits) const
{
return (const_cast<node_pointer>(get_child_node(b_it, e_it, p_traits)));
}
get_child_node(const_e_iterator b_it, const_e_iterator e_it,
const_e_access_traits_pointer p_traits) const
{ return const_cast<node_pointer>(get_child_node(b_it, e_it, p_traits)); }
PB_DS_CLASS_T_DEC
typename PB_DS_CLASS_C_DEC::node_pointer
PB_DS_CLASS_C_DEC::
get_lower_bound_child_node(const_e_iterator b_it, const_e_iterator e_it, size_type checked_ind, const_e_access_traits_pointer p_traits)
get_lower_bound_child_node(const_e_iterator b_it, const_e_iterator e_it,
size_type checked_ind,
const_e_access_traits_pointer p_traits)
{
if (!should_be_mine(b_it, e_it, checked_ind, p_traits))
{
if (p_traits->cmp_prefixes(b_it, e_it, m_pref_b_it, m_pref_e_it, true))
return (leftmost_descendant());
return (rightmost_descendant());
return leftmost_descendant();
return rightmost_descendant();
}
size_type i = get_pref_pos( b_it, e_it, p_traits);
size_type i = get_pref_pos(b_it, e_it, p_traits);
PB_DS_DBG_ASSERT(i < arr_size);
if (m_a_p_children[i] != NULL)
return (m_a_p_children[i]);
return m_a_p_children[i];
while (++i < arr_size)
if (m_a_p_children[i] != NULL)
{
if (m_a_p_children[i]->m_type == pat_trie_leaf_node_type)
return (m_a_p_children[i]);
return m_a_p_children[i];
PB_DS_DBG_ASSERT(m_a_p_children[i]->m_type ==
pat_trie_internal_node_type);
PB_DS_DBG_ASSERT(m_a_p_children[i]->m_type == pat_trie_internal_node_type);
return (static_cast<internal_node_pointer>(
m_a_p_children[i])->leftmost_descendant());
return static_cast<internal_node_pointer>(m_a_p_children[i])->leftmost_descendant();
}
return (rightmost_descendant());
return rightmost_descendant();
}
PB_DS_CLASS_T_DEC
inline typename PB_DS_CLASS_C_DEC::node_pointer
PB_DS_CLASS_C_DEC::
add_child(node_pointer p_nd, const_e_iterator b_it, const_e_iterator e_it, const_e_access_traits_pointer p_traits)
add_child(node_pointer p_nd, const_e_iterator b_it, const_e_iterator e_it,
const_e_access_traits_pointer p_traits)
{
const size_type i = get_pref_pos( b_it, e_it, p_traits);
const size_type i = get_pref_pos(b_it, e_it, p_traits);
PB_DS_DBG_ASSERT(i < arr_size);
if (m_a_p_children[i] == NULL)
{
m_a_p_children[i] = p_nd;
p_nd->m_p_parent = this;
return (p_nd);
return p_nd;
}
return (m_a_p_children[i]);
return m_a_p_children[i];
}
PB_DS_CLASS_T_DEC
@ -485,9 +405,8 @@ namespace pb_ds
PB_DS_CLASS_C_DEC::
get_join_child(const_node_pointer p_nd, const_e_access_traits_pointer p_traits) const
{
return (const_cast<internal_node_pointer>(this)->get_join_child(
const_cast<node_pointer>(p_nd),
p_traits));
node_pointer p = const_cast<node_pointer>(p_nd);
return const_cast<internal_node_pointer>(this)->get_join_child(p, p_traits);
}
PB_DS_CLASS_T_DEC
@ -496,15 +415,12 @@ namespace pb_ds
get_join_child(node_pointer p_nd, const_e_access_traits_pointer p_traits)
{
size_type i;
const_e_iterator b_it;
const_e_iterator e_it;
if (p_nd->m_type == pat_trie_leaf_node_type)
{
typename Type_Traits::const_key_reference r_key =
e_access_traits::extract_key(
static_cast<const_leaf_pointer>(p_nd)->value());
e_access_traits::extract_key(static_cast<const_leaf_pointer>(p_nd)->value());
b_it = p_traits->begin(r_key);
e_it = p_traits->end(r_key);
@ -514,12 +430,9 @@ namespace pb_ds
b_it = static_cast<internal_node_pointer>(p_nd)->pref_b_it();
e_it = static_cast<internal_node_pointer>(p_nd)->pref_e_it();
}
i = get_pref_pos(b_it, e_it, p_traits);
PB_DS_DBG_ASSERT(i < arr_size);
return (m_a_p_children[i]);
return m_a_p_children[i];
}
PB_DS_CLASS_T_DEC
@ -528,15 +441,12 @@ namespace pb_ds
remove_child(node_pointer p_nd)
{
size_type i = 0;
for (; i < arr_size; ++i)
if (m_a_p_children[i] == p_nd)
{
m_a_p_children[i] = NULL;
return;
}
PB_DS_DBG_ASSERT(i != arr_size);
}
@ -546,25 +456,21 @@ namespace pb_ds
remove_child(iterator it)
{
iterator ret = it;
++ret;
* it.m_p_p_cur = NULL;
return (ret);
return ret;
}
PB_DS_CLASS_T_DEC
void
PB_DS_CLASS_C_DEC::
replace_child(node_pointer p_nd, const_e_iterator b_it, const_e_iterator e_it, const_e_access_traits_pointer p_traits)
replace_child(node_pointer p_nd, const_e_iterator b_it,
const_e_iterator e_it,
const_e_access_traits_pointer p_traits)
{
const size_type i = get_pref_pos( b_it, e_it, p_traits);
const size_type i = get_pref_pos(b_it, e_it, p_traits);
PB_DS_DBG_ASSERT(i < arr_size);
m_a_p_children[i] = p_nd;
p_nd->m_p_parent = this;
}
@ -572,38 +478,33 @@ namespace pb_ds
inline typename PB_DS_CLASS_C_DEC::const_e_iterator
PB_DS_CLASS_C_DEC::
pref_b_it() const
{
return (m_pref_b_it);
}
{ return m_pref_b_it; }
PB_DS_CLASS_T_DEC
inline typename PB_DS_CLASS_C_DEC::const_e_iterator
PB_DS_CLASS_C_DEC::
pref_e_it() const
{
return (m_pref_e_it);
}
{ return m_pref_e_it; }
PB_DS_CLASS_T_DEC
inline typename PB_DS_CLASS_C_DEC::size_type
PB_DS_CLASS_C_DEC::
get_e_ind() const
{
return (m_e_ind);
}
{ return m_e_ind; }
PB_DS_CLASS_T_DEC
bool
PB_DS_CLASS_C_DEC::
should_be_mine(const_e_iterator b_it, const_e_iterator e_it, size_type checked_ind, const_e_access_traits_pointer p_traits) const
should_be_mine(const_e_iterator b_it, const_e_iterator e_it,
size_type checked_ind,
const_e_access_traits_pointer p_traits) const
{
if (m_e_ind == 0)
return (true);
return true;
const size_type num_es = std::distance(b_it, e_it);
if (num_es < m_e_ind)
return (false);
return false;
const_e_iterator key_b_it = b_it;
std::advance(key_b_it, checked_ind);
@ -615,7 +516,8 @@ namespace pb_ds
const_e_iterator value_e_it = m_pref_b_it;
std::advance(value_e_it, m_e_ind);
return (p_traits->equal_prefixes( key_b_it, key_e_it, value_b_it, value_e_it));
return p_traits->equal_prefixes(key_b_it, key_e_it, value_b_it,
value_e_it);
}
PB_DS_CLASS_T_DEC
@ -624,13 +526,10 @@ namespace pb_ds
leftmost_descendant()
{
node_pointer p_pot =* begin();
if (p_pot->m_type == pat_trie_leaf_node_type)
return (static_cast<leaf_pointer>(p_pot));
PB_DS_DBG_ASSERT(p_pot->m_type == pat_trie_internal_node_type);
return (static_cast<internal_node_pointer>(p_pot)->leftmost_descendant());
return static_cast<internal_node_pointer>(p_pot)->leftmost_descendant();
}
PB_DS_CLASS_T_DEC
@ -638,7 +537,7 @@ namespace pb_ds
PB_DS_CLASS_C_DEC::
leftmost_descendant() const
{
return (const_cast<internal_node_pointer>(this)->leftmost_descendant());
return const_cast<internal_node_pointer>(this)->leftmost_descendant();
}
PB_DS_CLASS_T_DEC
@ -647,20 +546,15 @@ namespace pb_ds
rightmost_descendant()
{
const size_type num_children = std::distance(begin(), end());
PB_DS_DBG_ASSERT(num_children >= 2);
iterator it = begin();
std::advance(it, num_children - 1);
node_pointer p_pot =* it;
if (p_pot->m_type == pat_trie_leaf_node_type)
return (static_cast<leaf_pointer>(p_pot));
return static_cast<leaf_pointer>(p_pot);
PB_DS_DBG_ASSERT(p_pot->m_type == pat_trie_internal_node_type);
return (static_cast<internal_node_pointer>(p_pot)->rightmost_descendant());
return static_cast<internal_node_pointer>(p_pot)->rightmost_descendant();
}
PB_DS_CLASS_T_DEC
@ -668,7 +562,7 @@ namespace pb_ds
PB_DS_CLASS_C_DEC::
rightmost_descendant() const
{
return (const_cast<internal_node_pointer>(this)->rightmost_descendant());
return const_cast<internal_node_pointer>(this)->rightmost_descendant();
}
#ifdef PB_DS_PAT_TRIE_DEBUG_
@ -676,9 +570,7 @@ namespace pb_ds
typename PB_DS_CLASS_C_DEC::size_type
PB_DS_CLASS_C_DEC::
e_ind() const
{
return (m_e_ind);
}
{ return m_e_ind; }
#endif // #ifdef PB_DS_PAT_TRIE_DEBUG_
PB_DS_CLASS_T_DEC
@ -687,11 +579,9 @@ namespace pb_ds
get_begin_pos() const
{
size_type i;
for (i = 0; i < arr_size&& m_a_p_children[i] == NULL; ++i)
for (i = 0; i < arr_size && m_a_p_children[i] == NULL; ++i)
;
return (i);
return i;
}
#ifdef PB_DS_PAT_TRIE_DEBUG_
@ -701,46 +591,28 @@ namespace pb_ds
assert_valid_imp(const_e_access_traits_pointer p_traits) const
{
PB_DS_DBG_ASSERT(base_type::m_type == pat_trie_internal_node_type);
PB_DS_DBG_ASSERT(
static_cast<size_type>(std::distance(pref_b_it(), pref_e_it())) ==
m_e_ind);
PB_DS_DBG_ASSERT(
std::distance(begin(), end()) >= 2);
PB_DS_DBG_ASSERT(static_cast<size_type>(std::distance(pref_b_it(), pref_e_it())) == m_e_ind);
PB_DS_DBG_ASSERT(std::distance(begin(), end()) >= 2);
for (typename pat_trie_internal_node::const_iterator it = begin();
it != end(); ++it)
{
const_node_pointer p_nd =* it;
PB_DS_DBG_ASSERT(p_nd->m_p_parent == this);
subtree_debug_info child_ret = p_nd->assert_valid_imp(p_traits);
PB_DS_DBG_ASSERT(
static_cast<size_type>(std::distance(child_ret.first, child_ret.second)) >=
m_e_ind);
PB_DS_DBG_ASSERT(static_cast<size_type>(std::distance(child_ret.first, child_ret.second)) >= m_e_ind);
PB_DS_DBG_ASSERT(should_be_mine(child_ret.first, child_ret.second, 0, p_traits));
PB_DS_DBG_ASSERT(
get_pref_pos(child_ret.first, child_ret.second, p_traits) ==
static_cast<size_type>(it.m_p_p_cur - m_a_p_children));
PB_DS_DBG_ASSERT(get_pref_pos(child_ret.first, child_ret.second, p_traits) == static_cast<size_type>(it.m_p_p_cur - m_a_p_children));
}
return (std::make_pair(pref_b_it(), pref_e_it()));
return std::make_pair(pref_b_it(), pref_e_it());
}
#endif // #ifdef PB_DS_PAT_TRIE_DEBUG_
#undef PB_DS_CLASS_T_DEC
#undef PB_DS_CLASS_C_DEC
#undef PB_DS_BASE_C_DEC
#undef PB_DS_LEAF_C_DEC
#undef PB_DS_STATIC_ASSERT
#undef PB_DS_DBG_ASSERT
@ -750,5 +622,4 @@ namespace pb_ds
} // namespace detail
} // namespace pb_ds
#endif // #ifndef PB_DS_PAT_TRIE_NODE_BASE_HPP
#endif

View File

@ -44,93 +44,69 @@
* Contains a resize trigger implementation.
*/
#define PB_DS_STATIC_ASSERT(UNIQUE, E) \
typedef detail::static_assert_dumclass<sizeof(detail::static_assert<(bool)(E)>)> \
UNIQUE##static_assert_type
#define PB_DS_STATIC_ASSERT(UNIQUE, E) \
typedef detail::static_assert_dumclass<sizeof(detail::static_assert<(bool)(E)>)> UNIQUE##static_assert_type
PB_DS_CLASS_T_DEC
PB_DS_CLASS_C_DEC::
hash_load_check_resize_trigger(float load_min, float load_max) :
m_load_min(load_min),
m_load_max(load_max),
m_next_shrink_size(0),
m_next_grow_size(0),
m_resize_needed(false)
{
PB_DS_DBG_ONLY(assert_valid();)
}
hash_load_check_resize_trigger(float load_min, float load_max)
: m_load_min(load_min), m_load_max(load_max), m_next_shrink_size(0),
m_next_grow_size(0), m_resize_needed(false)
{ PB_DS_DBG_ONLY(assert_valid();) }
PB_DS_CLASS_T_DEC
inline void
PB_DS_CLASS_C_DEC::
notify_find_search_start()
{
PB_DS_DBG_ONLY(assert_valid();)
}
{ PB_DS_DBG_ONLY(assert_valid();) }
PB_DS_CLASS_T_DEC
inline void
PB_DS_CLASS_C_DEC::
notify_find_search_collision()
{
PB_DS_DBG_ONLY(assert_valid();)
}
{ PB_DS_DBG_ONLY(assert_valid();) }
PB_DS_CLASS_T_DEC
inline void
PB_DS_CLASS_C_DEC::
notify_find_search_end()
{
PB_DS_DBG_ONLY(assert_valid();)
}
{ PB_DS_DBG_ONLY(assert_valid();) }
PB_DS_CLASS_T_DEC
inline void
PB_DS_CLASS_C_DEC::
notify_insert_search_start()
{
PB_DS_DBG_ONLY(assert_valid();)
}
{ PB_DS_DBG_ONLY(assert_valid();) }
PB_DS_CLASS_T_DEC
inline void
PB_DS_CLASS_C_DEC::
notify_insert_search_collision()
{
PB_DS_DBG_ONLY(assert_valid();)
}
{ PB_DS_DBG_ONLY(assert_valid();) }
PB_DS_CLASS_T_DEC
inline void
PB_DS_CLASS_C_DEC::
notify_insert_search_end()
{
PB_DS_DBG_ONLY(assert_valid();)
}
{ PB_DS_DBG_ONLY(assert_valid();) }
PB_DS_CLASS_T_DEC
inline void
PB_DS_CLASS_C_DEC::
notify_erase_search_start()
{
PB_DS_DBG_ONLY(assert_valid();)
}
{ PB_DS_DBG_ONLY(assert_valid();) }
PB_DS_CLASS_T_DEC
inline void
PB_DS_CLASS_C_DEC::
notify_erase_search_collision()
{
PB_DS_DBG_ONLY(assert_valid();)
}
{ PB_DS_DBG_ONLY(assert_valid();) }
PB_DS_CLASS_T_DEC
inline void
PB_DS_CLASS_C_DEC::
notify_erase_search_end()
{
PB_DS_DBG_ONLY(assert_valid();)
}
{ PB_DS_DBG_ONLY(assert_valid();) }
PB_DS_CLASS_T_DEC
inline void
@ -138,11 +114,9 @@ PB_DS_CLASS_C_DEC::
notify_inserted(size_type num_entries)
{
m_resize_needed = (num_entries >= m_next_grow_size);
size_base::set_size(num_entries);
PB_DS_DBG_ONLY(assert_valid();)
}
}
PB_DS_CLASS_T_DEC
inline void
@ -150,12 +124,9 @@ PB_DS_CLASS_C_DEC::
notify_erased(size_type num_entries)
{
size_base::set_size(num_entries);
m_resize_needed =
num_entries <= m_next_shrink_size;
m_resize_needed = num_entries <= m_next_shrink_size;
PB_DS_DBG_ONLY(assert_valid();)
}
}
PB_DS_CLASS_T_DEC
inline bool
@ -163,8 +134,7 @@ PB_DS_CLASS_C_DEC::
is_resize_needed() const
{
PB_DS_DBG_ONLY(assert_valid();)
return (m_resize_needed);
return m_resize_needed;
}
PB_DS_CLASS_T_DEC
@ -173,14 +143,12 @@ PB_DS_CLASS_C_DEC::
is_grow_needed(size_type /*size*/, size_type num_entries) const
{
PB_DS_DBG_ASSERT(m_resize_needed);
return (num_entries >= m_next_grow_size);
return num_entries >= m_next_grow_size;
}
PB_DS_CLASS_T_DEC
PB_DS_CLASS_C_DEC::
~hash_load_check_resize_trigger()
{ }
~hash_load_check_resize_trigger() { }
PB_DS_CLASS_T_DEC
void
@ -188,24 +156,20 @@ PB_DS_CLASS_C_DEC::
notify_resized(size_type new_size)
{
m_resize_needed = false;
m_next_grow_size =
size_type(m_load_max* new_size - 1);
m_next_shrink_size =
size_type(m_load_min* new_size);
m_next_grow_size = size_type(m_load_max * new_size - 1);
m_next_shrink_size = size_type(m_load_min * new_size);
#ifdef PB_DS_HT_MAP_RESIZE_TRACE_
std::cerr << "hlcrt::notify_resized " <<
static_cast<unsigned long>(new_size) << " " <<
static_cast<unsigned long>(m_load_min) << " " <<
static_cast<unsigned long>(m_load_max) << " " <<
static_cast<unsigned long>(new_size) << " " <<
static_cast<unsigned long>(m_load_min) << " " <<
static_cast<unsigned long>(m_load_max) << " " <<
static_cast<unsigned long>(m_next_shrink_size) << " " <<
static_cast<unsigned long>(m_next_grow_size) << " " << std::endl;
#endif // #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
static_cast<unsigned long>(m_next_grow_size) << " " << std::endl;
#endif
PB_DS_DBG_ONLY(assert_valid();)
}
}
PB_DS_CLASS_T_DEC
void
@ -213,48 +177,39 @@ PB_DS_CLASS_C_DEC::
notify_externally_resized(size_type new_size)
{
m_resize_needed = false;
size_type new_grow_size =
size_type(m_load_max* new_size - 1);
size_type new_shrink_size =
size_type(m_load_min* new_size );
size_type new_grow_size = size_type(m_load_max * new_size - 1);
size_type new_shrink_size = size_type(m_load_min * new_size );
if (new_grow_size >= m_next_grow_size)
{
PB_DS_DBG_ASSERT(new_shrink_size > m_next_shrink_size);
m_next_grow_size = new_grow_size;
PB_DS_DBG_ONLY(assert_valid();)
#ifdef PB_DS_HT_MAP_RESIZE_TRACE_
std::cerr << "hlcrt::notify_externally_resized1 " <<
static_cast<unsigned long>(new_size) << " " <<
static_cast<unsigned long>(m_load_min) << " " <<
static_cast<unsigned long>(m_load_max) << " " <<
static_cast<unsigned long>(new_size) << " " <<
static_cast<unsigned long>(m_load_min) << " " <<
static_cast<unsigned long>(m_load_max) << " " <<
static_cast<unsigned long>(m_next_shrink_size) << " " <<
static_cast<unsigned long>(m_next_grow_size) << " " << std::endl;
#endif // #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
static_cast<unsigned long>(m_next_grow_size) << " " << std::endl;
#endif
return;
}
PB_DS_DBG_ASSERT(new_shrink_size <= m_next_shrink_size);
m_next_shrink_size = new_shrink_size;
#ifdef PB_DS_HT_MAP_RESIZE_TRACE_
std::cerr << "hlcrt::notify_externally_resized2 " <<
static_cast<unsigned long>(new_size) << " " <<
static_cast<unsigned long>(m_load_min) << " " <<
static_cast<unsigned long>(m_load_max) << " " <<
static_cast<unsigned long>(new_size) << " " <<
static_cast<unsigned long>(m_load_min) << " " <<
static_cast<unsigned long>(m_load_max) << " " <<
static_cast<unsigned long>(m_next_shrink_size) << " " <<
static_cast<unsigned long>(m_next_grow_size) << " " << std::endl;
#endif // #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
static_cast<unsigned long>(m_next_grow_size) << " " << std::endl;
#endif
PB_DS_DBG_ONLY(assert_valid();)
}
}
PB_DS_CLASS_T_DEC
void
@ -262,13 +217,10 @@ PB_DS_CLASS_C_DEC::
notify_cleared()
{
PB_DS_DBG_ONLY(assert_valid();)
size_base::set_size(0);
size_base::set_size(0);
m_resize_needed = (0 < m_next_shrink_size);
PB_DS_DBG_ONLY(assert_valid();)
}
}
PB_DS_CLASS_T_DEC
void
@ -276,21 +228,18 @@ PB_DS_CLASS_C_DEC::
swap(PB_DS_CLASS_C_DEC& other)
{
PB_DS_DBG_ONLY(assert_valid();)
PB_DS_DBG_ONLY(other.assert_valid();)
size_base::swap(other);
PB_DS_DBG_ONLY(other.assert_valid();)
size_base::swap(other);
std::swap(m_load_min, other.m_load_min);
std::swap(m_load_max, other.m_load_max);
std::swap(m_resize_needed, other.m_resize_needed);
std::swap(m_next_grow_size, other.m_next_grow_size);
std::swap(m_next_shrink_size, other.m_next_shrink_size);
PB_DS_DBG_ONLY(assert_valid();)
PB_DS_DBG_ONLY(other.assert_valid();)
}
PB_DS_DBG_ONLY(other.assert_valid();)
}
PB_DS_CLASS_T_DEC
inline std::pair<float, float>
@ -298,8 +247,7 @@ PB_DS_CLASS_C_DEC::
get_loads() const
{
PB_DS_STATIC_ASSERT(access, external_load_access);
return (std::make_pair(m_load_min, m_load_max));
return std::make_pair(m_load_min, m_load_max);
}
PB_DS_CLASS_T_DEC
@ -308,7 +256,6 @@ PB_DS_CLASS_C_DEC::
set_loads(std::pair<float, float> load_pair)
{
PB_DS_STATIC_ASSERT(access, external_load_access);
const float old_load_min = m_load_min;
const float old_load_max = m_load_max;
const size_type old_next_shrink_size = m_next_shrink_size;
@ -319,18 +266,15 @@ set_loads(std::pair<float, float> load_pair)
{
m_load_min = load_pair.first;
m_load_max = load_pair.second;
do_resize(static_cast<size_type>(
size_base::get_size() / ((m_load_min + m_load_max) / 2)));
do_resize(static_cast<size_type>(size_base::get_size() / ((m_load_min + m_load_max) / 2)));
}
catch(...)
catch (...)
{
m_load_min = old_load_min;
m_load_max = old_load_max;
m_next_shrink_size = old_next_shrink_size;
m_next_grow_size = old_next_grow_size;
m_resize_needed = old_resize_needed;
throw;
}
}
@ -338,10 +282,8 @@ set_loads(std::pair<float, float> load_pair)
PB_DS_CLASS_T_DEC
void
PB_DS_CLASS_C_DEC::
do_resize(size_type /*new_size*/)
{
abort();
}
do_resize(size_type)
{ abort(); }
#ifdef PB_DS_HASH_POLICY_DEBUG
PB_DS_CLASS_T_DEC
@ -350,10 +292,9 @@ PB_DS_CLASS_C_DEC::
assert_valid() const
{
PB_DS_DBG_ASSERT(m_load_max > m_load_min);
PB_DS_DBG_ASSERT(m_next_grow_size >= m_next_shrink_size);
}
#endif // #ifdef PB_DS_HASH_POLICY_DEBUG
#endif
#undef PB_DS_STATIC_ASSERT

View File

@ -171,6 +171,31 @@ namespace pb_ds
{
typedef B type;
};
#define __glibcxx_signed(T) ((T)(-1) < 0)
#define __glibcxx_min(T) \
(__glibcxx_signed (T) ? (T)1 << __glibcxx_digits (T) : (T)0)
#define __glibcxx_max(T) \
(__glibcxx_signed (T) ? ((T)1 << __glibcxx_digits (T)) - 1 : ~(T)0)
#define __glibcxx_digits(T) \
(sizeof(T) * __CHAR_BIT__ - __glibcxx_signed (T))
template<typename Value>
struct numeric_traits
{
typedef Value value_type;
static const value_type min = __glibcxx_min(value_type);
static const value_type max = __glibcxx_max(value_type);
};
template<typename Value>
const Value numeric_traits<Value>::min;
template<typename Value>
const Value numeric_traits<Value>::max;
} // namespace detail
} // namespace pb_ds

View File

@ -48,7 +48,6 @@
#define PB_DS_TRIE_POLICY_HPP
#include <string>
#include <climits>
#include <ext/pb_ds/detail/type_utils.hpp>
#include <ext/pb_ds/detail/trie_policy/trie_policy_base.hpp>
@ -63,7 +62,7 @@ namespace pb_ds
{ };
#define PB_DS_STATIC_ASSERT(UNIQUE, E) \
typedef detail::static_assert_dumclass<sizeof(detail::static_assert<(bool)(E)>)> UNIQUE##static_assert_type
typedef detail::static_assert_dumclass<sizeof(detail::static_assert<bool(E)>)> UNIQUE##_static_assert_type
#define PB_DS_CLASS_T_DEC \
template<typename String, typename String::value_type Min_E_Val, typename String::value_type Max_E_Val, bool Reverse, typename Allocator>
@ -73,8 +72,8 @@ namespace pb_ds
// Element access traits for string types.
template<typename String = std::string,
typename String::value_type Min_E_Val = SCHAR_MIN,
typename String::value_type Max_E_Val = SCHAR_MAX,
typename String::value_type Min_E_Val = detail::numeric_traits<typename String::value_type>::min,
typename String::value_type Max_E_Val = detail::numeric_traits<typename String::value_type>::max,
bool Reverse = false,
typename Allocator = std::allocator<char> >
struct string_trie_e_access_traits
@ -102,6 +101,7 @@ namespace pb_ds
max_e_val = Max_E_Val,
max_size = max_e_val - min_e_val + 1
};
PB_DS_STATIC_ASSERT(min_max_size, max_size >= 2);
// Returns a const_iterator to the first element of
// const_key_reference agumnet.
@ -118,7 +118,6 @@ namespace pb_ds
e_pos(e_type e);
private:
PB_DS_STATIC_ASSERT(min_max_size, max_size >= 2);
inline static const_iterator
begin_imp(const_key_reference, detail::false_type);
@ -132,7 +131,7 @@ namespace pb_ds
inline static const_iterator
end_imp(const_key_reference, detail::true_type);
static detail::integral_constant<int,Reverse> s_rev_ind;
static detail::integral_constant<int, Reverse> s_rev_ind;
};
#include <ext/pb_ds/detail/trie_policy/string_trie_e_access_traits_imp.hpp>