📄 ycpp_unordered_set.hpp
字号:
/*
* The young Library
* Copyright (c) 2005 by Yang Huan(杨桓)
* Permission to use, copy, modify, distribute and sell this software for any
* purpose is hereby granted without fee, provided that the above copyright
* notice appear in all copies and that both that copyright notice and this
* permission notice appear in supporting documentation.
* The author make no representations about the suitability of this software
* for any purpose. It is provided "as is" without express or implied warranty.
*/
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
#ifndef __MACRO_CPLUSPLUS_YOUNG_LIBRARY_UNORDERED_SET_HEADER_FILE__
#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_UNORDERED_SET_HEADER_FILE__
//------------------------------------------------------------------------------
#include <memory>
#include <utility>
#include <iterator>
#include <functional>
#include "../youngc/yc_hashtable.h"
#include "../youngc/yc_memory.h"
#include "ycpp_memory.hpp"
#include "ycpp_function.hpp"
#include "ycpp_functional.hpp"
//------------------------------------------------------------------------------
namespace youngcpp {
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
template< typename K, typename HK, typename EK, typename A >
class unordered_set;
template< typename K, typename HK, typename EK, typename A >
class unordered_multiset;
template< typename K, typename HK, typename EK, typename A >
class uoset_iterator;
template< typename K, typename HK, typename EK, typename A >
class uoset_local_iterator;
template< typename K, typename HK, typename EK, typename A >
struct move_traits< unordered_set<K, HK, EK, A> >
{
typedef true_type move_ability;
};
template< typename K, typename HK, typename EK, typename A >
struct move_traits< unordered_multiset<K, HK, EK, A> >
{
typedef true_type move_ability;
};
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
template< typename Key, typename HashKey, typename EqualKey, typename Allocator >
class uoset_iterator
{
private:
friend class unordered_set<Key, HashKey, EqualKey, Allocator>;
friend class unordered_multiset<Key, HashKey, EqualKey, Allocator>;
friend class uoset_local_iterator<Key, HashKey, EqualKey, Allocator>;
youngc::hstb_iterator itr;
public:
typedef std::forward_iterator_tag iterator_category;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef Key value_type;
typedef const Key& reference;
typedef const Key* pointer;
typedef uoset_iterator<Key, HashKey, EqualKey, Allocator> self;
uoset_iterator() { itr.node = 0; itr.table = 0; }
uoset_iterator( const youngc::hstb_iterator& src ) : itr(src) {}
bool operator==( const self& rhs ) const
{ return hstb_itr_equal( itr, rhs.itr ); }
bool operator!=( const self& rhs ) const
{ return !hstb_itr_equal( itr, rhs.itr ); }
reference operator*() const
{ return *( (pointer)hstb_itr_deref(itr) ); }
pointer operator->() const
{ return (pointer)hstb_itr_deref(itr); }
self& operator++()
{ hstb_itr_inc(&itr); return *this; }
self operator++(int)
{ self old = *this; hstb_itr_inc(&itr); return old; }
};
template< typename Key, typename HashKey, typename EqualKey, typename Allocator >
class uoset_local_iterator
{
private:
friend class unordered_set<Key, HashKey, EqualKey, Allocator>;
friend class unordered_multiset<Key, HashKey, EqualKey, Allocator>;
youngc::hstb_local_iterator lcitr;
public:
typedef std::forward_iterator_tag iterator_category;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef Key value_type;
typedef const Key& reference;
typedef const Key* pointer;
typedef uoset_iterator<Key, HashKey, EqualKey, Allocator> iterator;
typedef uoset_local_iterator<Key, HashKey, EqualKey, Allocator> self;
uoset_local_iterator() : lcitr(0) {}
uoset_local_iterator( const iterator& src ) : lcitr(src.itr.node) {}
uoset_local_iterator( const youngc::hstb_iterator src ) : lcitr(src.node) {}
uoset_local_iterator( const youngc::hstb_local_iterator src ) : lcitr(src) {}
bool operator==( const self& rhs ) const { return lcitr == rhs.lcitr; }
bool operator!=( const self& rhs ) const { return lcitr != rhs.lcitr; }
reference operator*() const
{ return *( (pointer)hstb_lc_itr_deref(lcitr) ); }
pointer operator->() const
{ return (pointer)hstb_lc_itr_deref(lcitr); }
self& operator++()
{ hstb_lc_itr_inc(&lcitr); return *this; }
self operator++(int)
{ self old = *this; hstb_lc_itr_inc(&lcitr); return old; }
};
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
template< typename Key,
#ifdef __MACRO_CPLUSPLUS_YOUNG_LIBRARY_COMPILER_SUPPORT_STANDARD_HASH_FUNCTION_OBJECT__
typename HashKey = std::tr1::hash<T>,
#else
typename HashKey = hash<Key>,
#endif
typename EqualKey = std::equal_to<Key>,
typename Allocator = std::allocator<Key> >
class unordered_set
{
public:
typedef unordered_set<Key, HashKey, EqualKey, Allocator> self;
typedef Key key_type;
typedef Key value_type;
typedef HashKey hasher;
typedef EqualKey key_equal;
typedef Allocator allocator_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef const Key& reference;
typedef const Key& const_reference;
typedef const Key* pointer;
typedef const Key* const_pointer;
typedef uoset_iterator<Key, HashKey, EqualKey, Allocator> iterator;
typedef uoset_iterator<Key, HashKey, EqualKey, Allocator> const_iterator;
typedef uoset_local_iterator<Key, HashKey, EqualKey, Allocator>
local_iterator;
typedef uoset_local_iterator<Key, HashKey, EqualKey, Allocator>
const_local_iterator;
explicit unordered_set( size_type n = 100 )
{
init();
if( !hstb_rehash( &m_ht, n ) )
throw std::bad_alloc();
}
template< typename InputIterator >
unordered_set( InputIterator first, InputIterator last, size_type n = 100 )
{
init();
if( !hstb_rehash( &m_ht, n ) )
throw std::bad_alloc();
try { insert( first, last ); }
catch(...) { hstb_destroy( &m_ht ); throw; }
}
unordered_set( const self& rhs )
{
if( hstb_init_copy( &m_ht, &(rhs.m_ht) ) == 0 )
throw std::bad_alloc();
}
self& operator=( const self& rhs )
{
if( hstb_assign_copy( &m_ht, &(rhs.m_ht) ) == 0 )
throw std::bad_alloc();
return *this;
}
~unordered_set() { hstb_destroy( &m_ht ); }
static void init_move( void* uninit_dst, void* src )
{
hstb_init_move( &( static_cast<self*>(uninit_dst)->m_ht ),
&( static_cast<self*>(src)->m_ht ) );
}
static void assign_move( void* dst, void* src )
{
hstb_assign_move( &( static_cast<self*>(dst)->m_ht ),
&( static_cast<self*>(src)->m_ht ) );
}
iterator begin() const { return hstb_begin( &m_ht ); }
iterator end() const { return hstb_end( &m_ht ); }
local_iterator begin( size_type index ) const
{ return hstb_local_begin( &m_ht, index ); }
local_iterator end( size_type index ) const
{ return youngc::hstb_local_end(); }
size_type size() const { return hstb_size( &m_ht ); }
size_type max_size() const { return youngc::hstb_max_size(); }
bool empty() const { return size() == 0; }
void swap( self& rhs )
{
youngc::hashtable tmp = m_ht;
m_ht = rhs.m_ht;
rhs.m_ht = tmp;
}
size_type bucket_count() const
{ return hstb_chain_count( &m_ht ); }
size_type max_bucket_count() const
{ return youngc::hstb_max_chain_count(); }
size_type bucket_size( size_type index ) const
{ return hstb_chain_size( &m_ht, index ); }
size_type bucket( const key_type& k ) const
{ return hstb_key_chain( &m_ht, &k ); }
float load_factor() const { return hstb_load_factor( &m_ht ); }
float max_load_factor() const { return hstb_get_max_load_factor( &m_ht ); }
void max_load_factor( float f ) { hstb_set_max_load_factor( &m_ht, f ); }
void clear()
{ hstb_erase_range( &m_ht, hstb_begin(&m_ht), hstb_end(&m_ht) ); }
void rehash( size_type n )
{
if( !hstb_rehash( &m_ht, n ) )
throw std::bad_alloc();
}
size_type count( const key_type& k ) const
{ return hstb_count( &m_ht, &k ); }
iterator find( const key_type& k ) const
{ return hstb_find( &m_ht, &k ); }
std::pair<iterator, iterator> equal_range( const key_type& k ) const
{
youngc::hstb_pair_iterator pr = hstb_equal_range( &m_ht, &k );
return std::pair<iterator, iterator>( pr.first, pr.second );
}
size_type erase( const key_type& k )
{ return hstb_erase_key( &m_ht, &k ); }
void erase( iterator pos )
{ hstb_erase_pos( &m_ht, pos.itr ); }
void erase( iterator first, iterator last )
{ hstb_erase_range( &m_ht, first.itr, last.itr ); }
iterator insert( iterator pos, const value_type& v )
{
iterator itr = hstb_insert_unique_pos( &m_ht, pos.itr, &v );
if( end() == itr )
throw std::bad_alloc();
return itr;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -