Commit 81ee09de by Benjamin Kosnik Committed by Benjamin Kosnik

type_utils.hpp (numeric_traits): Add, const expression interface to…

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
parent 4e95268d
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> 2006-06-15 Paolo Carlini <pcarlini@suse.de>
* include/tr1/random.tcc (mersenne_twister<>::operator()()): * include/tr1/random.tcc (mersenne_twister<>::operator()()):
......
...@@ -44,93 +44,69 @@ ...@@ -44,93 +44,69 @@
* Contains a resize trigger implementation. * Contains a resize trigger implementation.
*/ */
#define PB_DS_STATIC_ASSERT(UNIQUE, E) \ #define PB_DS_STATIC_ASSERT(UNIQUE, E) \
typedef detail::static_assert_dumclass<sizeof(detail::static_assert<(bool)(E)>)> \ typedef detail::static_assert_dumclass<sizeof(detail::static_assert<(bool)(E)>)> UNIQUE##static_assert_type
UNIQUE##static_assert_type
PB_DS_CLASS_T_DEC PB_DS_CLASS_T_DEC
PB_DS_CLASS_C_DEC:: PB_DS_CLASS_C_DEC::
hash_load_check_resize_trigger(float load_min, float load_max) : hash_load_check_resize_trigger(float load_min, float load_max)
m_load_min(load_min), : m_load_min(load_min), m_load_max(load_max), m_next_shrink_size(0),
m_load_max(load_max), m_next_grow_size(0), m_resize_needed(false)
m_next_shrink_size(0), { PB_DS_DBG_ONLY(assert_valid();) }
m_next_grow_size(0),
m_resize_needed(false)
{
PB_DS_DBG_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::
notify_find_search_start() notify_find_search_start()
{ { PB_DS_DBG_ONLY(assert_valid();) }
PB_DS_DBG_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::
notify_find_search_collision() notify_find_search_collision()
{ { PB_DS_DBG_ONLY(assert_valid();) }
PB_DS_DBG_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::
notify_find_search_end() notify_find_search_end()
{ { PB_DS_DBG_ONLY(assert_valid();) }
PB_DS_DBG_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::
notify_insert_search_start() notify_insert_search_start()
{ { PB_DS_DBG_ONLY(assert_valid();) }
PB_DS_DBG_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::
notify_insert_search_collision() notify_insert_search_collision()
{ { PB_DS_DBG_ONLY(assert_valid();) }
PB_DS_DBG_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::
notify_insert_search_end() notify_insert_search_end()
{ { PB_DS_DBG_ONLY(assert_valid();) }
PB_DS_DBG_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::
notify_erase_search_start() notify_erase_search_start()
{ { PB_DS_DBG_ONLY(assert_valid();) }
PB_DS_DBG_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::
notify_erase_search_collision() notify_erase_search_collision()
{ { PB_DS_DBG_ONLY(assert_valid();) }
PB_DS_DBG_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::
notify_erase_search_end() notify_erase_search_end()
{ { PB_DS_DBG_ONLY(assert_valid();) }
PB_DS_DBG_ONLY(assert_valid();)
}
PB_DS_CLASS_T_DEC PB_DS_CLASS_T_DEC
inline void inline void
...@@ -138,11 +114,9 @@ PB_DS_CLASS_C_DEC:: ...@@ -138,11 +114,9 @@ PB_DS_CLASS_C_DEC::
notify_inserted(size_type num_entries) notify_inserted(size_type num_entries)
{ {
m_resize_needed = (num_entries >= m_next_grow_size); m_resize_needed = (num_entries >= m_next_grow_size);
size_base::set_size(num_entries); size_base::set_size(num_entries);
PB_DS_DBG_ONLY(assert_valid();) PB_DS_DBG_ONLY(assert_valid();)
} }
PB_DS_CLASS_T_DEC PB_DS_CLASS_T_DEC
inline void inline void
...@@ -150,12 +124,9 @@ PB_DS_CLASS_C_DEC:: ...@@ -150,12 +124,9 @@ PB_DS_CLASS_C_DEC::
notify_erased(size_type num_entries) notify_erased(size_type num_entries)
{ {
size_base::set_size(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_DBG_ONLY(assert_valid();)
} }
PB_DS_CLASS_T_DEC PB_DS_CLASS_T_DEC
inline bool inline bool
...@@ -163,8 +134,7 @@ PB_DS_CLASS_C_DEC:: ...@@ -163,8 +134,7 @@ PB_DS_CLASS_C_DEC::
is_resize_needed() const is_resize_needed() const
{ {
PB_DS_DBG_ONLY(assert_valid();) PB_DS_DBG_ONLY(assert_valid();)
return m_resize_needed;
return (m_resize_needed);
} }
PB_DS_CLASS_T_DEC PB_DS_CLASS_T_DEC
...@@ -173,14 +143,12 @@ PB_DS_CLASS_C_DEC:: ...@@ -173,14 +143,12 @@ PB_DS_CLASS_C_DEC::
is_grow_needed(size_type /*size*/, size_type num_entries) const is_grow_needed(size_type /*size*/, size_type num_entries) const
{ {
PB_DS_DBG_ASSERT(m_resize_needed); 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_T_DEC
PB_DS_CLASS_C_DEC:: PB_DS_CLASS_C_DEC::
~hash_load_check_resize_trigger() ~hash_load_check_resize_trigger() { }
{ }
PB_DS_CLASS_T_DEC PB_DS_CLASS_T_DEC
void void
...@@ -188,24 +156,20 @@ PB_DS_CLASS_C_DEC:: ...@@ -188,24 +156,20 @@ PB_DS_CLASS_C_DEC::
notify_resized(size_type new_size) notify_resized(size_type new_size)
{ {
m_resize_needed = false; m_resize_needed = false;
m_next_grow_size = size_type(m_load_max * new_size - 1);
m_next_grow_size = m_next_shrink_size = size_type(m_load_min * new_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_ #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
std::cerr << "hlcrt::notify_resized " << std::cerr << "hlcrt::notify_resized " <<
static_cast<unsigned long>(new_size) << " " << static_cast<unsigned long>(new_size) << " " <<
static_cast<unsigned long>(m_load_min) << " " << static_cast<unsigned long>(m_load_min) << " " <<
static_cast<unsigned long>(m_load_max) << " " << static_cast<unsigned long>(m_load_max) << " " <<
static_cast<unsigned long>(m_next_shrink_size) << " " << static_cast<unsigned long>(m_next_shrink_size) << " " <<
static_cast<unsigned long>(m_next_grow_size) << " " << std::endl; static_cast<unsigned long>(m_next_grow_size) << " " << std::endl;
#endif // #ifdef PB_DS_HT_MAP_RESIZE_TRACE_ #endif
PB_DS_DBG_ONLY(assert_valid();) PB_DS_DBG_ONLY(assert_valid();)
} }
PB_DS_CLASS_T_DEC PB_DS_CLASS_T_DEC
void void
...@@ -213,48 +177,39 @@ PB_DS_CLASS_C_DEC:: ...@@ -213,48 +177,39 @@ PB_DS_CLASS_C_DEC::
notify_externally_resized(size_type new_size) notify_externally_resized(size_type new_size)
{ {
m_resize_needed = false; m_resize_needed = false;
size_type new_grow_size = size_type(m_load_max * new_size - 1);
size_type new_grow_size = size_type new_shrink_size = size_type(m_load_min * new_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) if (new_grow_size >= m_next_grow_size)
{ {
PB_DS_DBG_ASSERT(new_shrink_size > m_next_shrink_size); PB_DS_DBG_ASSERT(new_shrink_size > m_next_shrink_size);
m_next_grow_size = new_grow_size; m_next_grow_size = new_grow_size;
PB_DS_DBG_ONLY(assert_valid();) PB_DS_DBG_ONLY(assert_valid();)
#ifdef PB_DS_HT_MAP_RESIZE_TRACE_ #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
std::cerr << "hlcrt::notify_externally_resized1 " << std::cerr << "hlcrt::notify_externally_resized1 " <<
static_cast<unsigned long>(new_size) << " " << static_cast<unsigned long>(new_size) << " " <<
static_cast<unsigned long>(m_load_min) << " " << static_cast<unsigned long>(m_load_min) << " " <<
static_cast<unsigned long>(m_load_max) << " " << static_cast<unsigned long>(m_load_max) << " " <<
static_cast<unsigned long>(m_next_shrink_size) << " " << static_cast<unsigned long>(m_next_shrink_size) << " " <<
static_cast<unsigned long>(m_next_grow_size) << " " << std::endl; static_cast<unsigned long>(m_next_grow_size) << " " << std::endl;
#endif // #ifdef PB_DS_HT_MAP_RESIZE_TRACE_ #endif
return; return;
} }
PB_DS_DBG_ASSERT(new_shrink_size <= m_next_shrink_size); PB_DS_DBG_ASSERT(new_shrink_size <= m_next_shrink_size);
m_next_shrink_size = new_shrink_size; m_next_shrink_size = new_shrink_size;
#ifdef PB_DS_HT_MAP_RESIZE_TRACE_ #ifdef PB_DS_HT_MAP_RESIZE_TRACE_
std::cerr << "hlcrt::notify_externally_resized2 " << std::cerr << "hlcrt::notify_externally_resized2 " <<
static_cast<unsigned long>(new_size) << " " << static_cast<unsigned long>(new_size) << " " <<
static_cast<unsigned long>(m_load_min) << " " << static_cast<unsigned long>(m_load_min) << " " <<
static_cast<unsigned long>(m_load_max) << " " << static_cast<unsigned long>(m_load_max) << " " <<
static_cast<unsigned long>(m_next_shrink_size) << " " << static_cast<unsigned long>(m_next_shrink_size) << " " <<
static_cast<unsigned long>(m_next_grow_size) << " " << std::endl; static_cast<unsigned long>(m_next_grow_size) << " " << std::endl;
#endif // #ifdef PB_DS_HT_MAP_RESIZE_TRACE_ #endif
PB_DS_DBG_ONLY(assert_valid();) PB_DS_DBG_ONLY(assert_valid();)
} }
PB_DS_CLASS_T_DEC PB_DS_CLASS_T_DEC
void void
...@@ -262,13 +217,10 @@ PB_DS_CLASS_C_DEC:: ...@@ -262,13 +217,10 @@ PB_DS_CLASS_C_DEC::
notify_cleared() notify_cleared()
{ {
PB_DS_DBG_ONLY(assert_valid();) PB_DS_DBG_ONLY(assert_valid();)
size_base::set_size(0);
size_base::set_size(0);
m_resize_needed = (0 < m_next_shrink_size); m_resize_needed = (0 < m_next_shrink_size);
PB_DS_DBG_ONLY(assert_valid();) PB_DS_DBG_ONLY(assert_valid();)
} }
PB_DS_CLASS_T_DEC PB_DS_CLASS_T_DEC
void void
...@@ -276,21 +228,18 @@ PB_DS_CLASS_C_DEC:: ...@@ -276,21 +228,18 @@ PB_DS_CLASS_C_DEC::
swap(PB_DS_CLASS_C_DEC& other) swap(PB_DS_CLASS_C_DEC& other)
{ {
PB_DS_DBG_ONLY(assert_valid();) PB_DS_DBG_ONLY(assert_valid();)
PB_DS_DBG_ONLY(other.assert_valid();) PB_DS_DBG_ONLY(other.assert_valid();)
size_base::swap(other); size_base::swap(other);
std::swap(m_load_min, other.m_load_min); std::swap(m_load_min, other.m_load_min);
std::swap(m_load_max, other.m_load_max); std::swap(m_load_max, other.m_load_max);
std::swap(m_resize_needed, other.m_resize_needed); std::swap(m_resize_needed, other.m_resize_needed);
std::swap(m_next_grow_size, other.m_next_grow_size); std::swap(m_next_grow_size, other.m_next_grow_size);
std::swap(m_next_shrink_size, other.m_next_shrink_size); std::swap(m_next_shrink_size, other.m_next_shrink_size);
PB_DS_DBG_ONLY(assert_valid();) 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 PB_DS_CLASS_T_DEC
inline std::pair<float, float> inline std::pair<float, float>
...@@ -298,8 +247,7 @@ PB_DS_CLASS_C_DEC:: ...@@ -298,8 +247,7 @@ PB_DS_CLASS_C_DEC::
get_loads() const get_loads() const
{ {
PB_DS_STATIC_ASSERT(access, external_load_access); 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 PB_DS_CLASS_T_DEC
...@@ -308,7 +256,6 @@ PB_DS_CLASS_C_DEC:: ...@@ -308,7 +256,6 @@ PB_DS_CLASS_C_DEC::
set_loads(std::pair<float, float> load_pair) set_loads(std::pair<float, float> load_pair)
{ {
PB_DS_STATIC_ASSERT(access, external_load_access); PB_DS_STATIC_ASSERT(access, external_load_access);
const float old_load_min = m_load_min; const float old_load_min = m_load_min;
const float old_load_max = m_load_max; const float old_load_max = m_load_max;
const size_type old_next_shrink_size = m_next_shrink_size; const size_type old_next_shrink_size = m_next_shrink_size;
...@@ -319,18 +266,15 @@ set_loads(std::pair<float, float> load_pair) ...@@ -319,18 +266,15 @@ set_loads(std::pair<float, float> load_pair)
{ {
m_load_min = load_pair.first; m_load_min = load_pair.first;
m_load_max = load_pair.second; 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_min = old_load_min;
m_load_max = old_load_max; m_load_max = old_load_max;
m_next_shrink_size = old_next_shrink_size; m_next_shrink_size = old_next_shrink_size;
m_next_grow_size = old_next_grow_size; m_next_grow_size = old_next_grow_size;
m_resize_needed = old_resize_needed; m_resize_needed = old_resize_needed;
throw; throw;
} }
} }
...@@ -338,10 +282,8 @@ set_loads(std::pair<float, float> load_pair) ...@@ -338,10 +282,8 @@ set_loads(std::pair<float, float> load_pair)
PB_DS_CLASS_T_DEC PB_DS_CLASS_T_DEC
void void
PB_DS_CLASS_C_DEC:: PB_DS_CLASS_C_DEC::
do_resize(size_type /*new_size*/) do_resize(size_type)
{ { abort(); }
abort();
}
#ifdef PB_DS_HASH_POLICY_DEBUG #ifdef PB_DS_HASH_POLICY_DEBUG
PB_DS_CLASS_T_DEC PB_DS_CLASS_T_DEC
...@@ -350,10 +292,9 @@ PB_DS_CLASS_C_DEC:: ...@@ -350,10 +292,9 @@ PB_DS_CLASS_C_DEC::
assert_valid() const assert_valid() const
{ {
PB_DS_DBG_ASSERT(m_load_max > m_load_min); PB_DS_DBG_ASSERT(m_load_max > m_load_min);
PB_DS_DBG_ASSERT(m_next_grow_size >= m_next_shrink_size); 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 #undef PB_DS_STATIC_ASSERT
...@@ -171,6 +171,31 @@ namespace pb_ds ...@@ -171,6 +171,31 @@ namespace pb_ds
{ {
typedef B type; 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 detail
} // namespace pb_ds } // namespace pb_ds
......
...@@ -48,7 +48,6 @@ ...@@ -48,7 +48,6 @@
#define PB_DS_TRIE_POLICY_HPP #define PB_DS_TRIE_POLICY_HPP
#include <string> #include <string>
#include <climits>
#include <ext/pb_ds/detail/type_utils.hpp> #include <ext/pb_ds/detail/type_utils.hpp>
#include <ext/pb_ds/detail/trie_policy/trie_policy_base.hpp> #include <ext/pb_ds/detail/trie_policy/trie_policy_base.hpp>
...@@ -63,7 +62,7 @@ namespace pb_ds ...@@ -63,7 +62,7 @@ namespace pb_ds
{ }; { };
#define PB_DS_STATIC_ASSERT(UNIQUE, E) \ #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 \ #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> 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 ...@@ -73,8 +72,8 @@ namespace pb_ds
// Element access traits for string types. // Element access traits for string types.
template<typename String = std::string, template<typename String = std::string,
typename String::value_type Min_E_Val = SCHAR_MIN, typename String::value_type Min_E_Val = detail::numeric_traits<typename String::value_type>::min,
typename String::value_type Max_E_Val = SCHAR_MAX, typename String::value_type Max_E_Val = detail::numeric_traits<typename String::value_type>::max,
bool Reverse = false, bool Reverse = false,
typename Allocator = std::allocator<char> > typename Allocator = std::allocator<char> >
struct string_trie_e_access_traits struct string_trie_e_access_traits
...@@ -102,6 +101,7 @@ namespace pb_ds ...@@ -102,6 +101,7 @@ namespace pb_ds
max_e_val = Max_E_Val, max_e_val = Max_E_Val,
max_size = max_e_val - min_e_val + 1 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 // Returns a const_iterator to the first element of
// const_key_reference agumnet. // const_key_reference agumnet.
...@@ -118,7 +118,6 @@ namespace pb_ds ...@@ -118,7 +118,6 @@ namespace pb_ds
e_pos(e_type e); e_pos(e_type e);
private: private:
PB_DS_STATIC_ASSERT(min_max_size, max_size >= 2);
inline static const_iterator inline static const_iterator
begin_imp(const_key_reference, detail::false_type); begin_imp(const_key_reference, detail::false_type);
...@@ -132,7 +131,7 @@ namespace pb_ds ...@@ -132,7 +131,7 @@ namespace pb_ds
inline static const_iterator inline static const_iterator
end_imp(const_key_reference, detail::true_type); 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> #include <ext/pb_ds/detail/trie_policy/string_trie_e_access_traits_imp.hpp>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment