📄 ycpp_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_SET_HEADER_FILE__
#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_SET_HEADER_FILE__
//------------------------------------------------------------------------------
#include <memory>
#include <utility>
#include <iterator>
#include <algorithm>
#include <functional>
#include "../youngc/yc_bbstree.h"
#include "../youngc/yc_memory.h"
#include "ycpp_memory.hpp"
#include "ycpp_function.hpp"
//------------------------------------------------------------------------------
namespace youngcpp {
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
template< typename Key, typename Compare, typename Allocator >
class set;
template< typename Key, typename Compare, typename Allocator >
class multiset;
template< typename Key, typename Compare, typename Allocator >
class set_iterator;
template< typename Key, typename Compare, typename Allocator >
struct move_traits< set<Key, Compare, Allocator> >
{
typedef true_type move_ability;
};
template< typename Key, typename Compare, typename Allocator >
struct move_traits< multiset<Key, Compare, Allocator> >
{
typedef true_type move_ability;
};
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
template< typename Key, typename Compare, typename Allocator >
class set_iterator
{
private:
friend class set<Key, Compare, Allocator>;
friend class multiset<Key, Compare, Allocator>;
youngc::bbstree_iterator itr;
public:
typedef std::bidirectional_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 set_iterator<Key, Compare, Allocator> self;
set_iterator() : itr(0) {}
set_iterator( const youngc::bbstree_iterator& src ) : itr(src) {}
bool operator==( const self& rhs ) const { return itr == rhs.itr; }
bool operator!=( const self& rhs ) const { return itr != rhs.itr; }
reference operator*() const
{ return *( (pointer)bbstree_itr_deref(itr) ); }
pointer operator->() const
{ return (pointer)bbstree_itr_deref(itr); }
self& operator++() { bbstree_itr_inc(&itr); return *this; }
self& operator--() { bbstree_itr_dec(&itr); return *this; }
self operator++(int)
{ self old = *this; bbstree_itr_inc(&itr); return old; }
self operator--(int)
{ self old = *this; bbstree_itr_dec(&itr); return old; }
};
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
template< typename Key, typename Compare = std::less<Key>,
typename Allocator = std::allocator<Key> >
class set
{
public:
typedef set<Key, Compare, Allocator> self;
typedef Key key_type;
typedef Key value_type;
typedef Compare key_compare;
typedef Compare value_compare;
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 set_iterator<Key, Compare, Allocator> iterator;
typedef set_iterator<Key, Compare, Allocator> const_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
set() { init(); }
template< typename InputIterator >
set( InputIterator first, InputIterator last )
{
init();
for( ; first != last; ++first )
{
if( !bbstree_insert_unique_value(&m_tree, &(*first)) )
{
bbstree_destroy( &m_tree );
throw std::bad_alloc();
}
}
}
set( const self& rhs )
{
if( bbstree_init_copy( &m_tree, &(rhs.m_tree) ) == 0 )
throw std::bad_alloc();
}
self& operator=( const self& rhs )
{
if( bbstree_assign_copy( &m_tree, &(rhs.m_tree) ) == 0 )
throw std::bad_alloc();
return *this;
}
~set() { bbstree_destroy( &m_tree ); }
static void init_move( void* uninit_dst, void* src )
{
bbstree_init_move( &( static_cast<self*>(uninit_dst)->m_tree ),
&( static_cast<self*>(src)->m_tree ) );
}
static void assign_move( void* dst, void* src )
{
bbstree_assign_move( &( static_cast<self*>(dst)->m_tree ),
&( static_cast<self*>(src)->m_tree ) );
}
iterator begin() const { return bbstree_begin(&m_tree); }
iterator end() const { return bbstree_end(&m_tree); }
reverse_iterator rbegin() const { return reverse_iterator( end() ); }
reverse_iterator rend() const { return reverse_iterator( begin() ); }
void clear() { bbstree_destroy(&m_tree); }
bool empty() const { return bbstree_size(&m_tree) == 0; }
size_type size() const { return bbstree_size(&m_tree); }
size_type max_size() const { return youngc::bbstree_max_size(); }
void swap( const self& rhs ) { bbstree_swap( &m_tree, &(rhs.m_tree) ); }
void erase( iterator position )
{ bbstree_erase_pos( &m_tree, position.itr ); }
void erase( iterator first, iterator last )
{ bbstree_erase_range( &m_tree, first.itr, last.itr ); }
size_type erase( const key_type& k )
{ return bbstree_erase_key( &m_tree, &k ); }
std::pair<iterator, bool> insert( const value_type& v )
{
size_type old = size();
youngc::bbstree_iterator p = bbstree_insert_unique_value(&m_tree, &v);
if( !p )
throw std::bad_alloc();
return old == size() ? std::pair<iterator, bool>( p, false )
: std::pair<iterator, bool>( p, true );
}
iterator insert( iterator pos, const value_type& v )
{
youngc::bbstree_iterator p = bbstree_insert_unique_pos( &m_tree,
pos.itr, &v );
if( !p )
throw std::bad_alloc();
return p;
}
template< typename InputIterator >
void insert( InputIterator first, InputIterator last )
{
for( ; first != last; ++first )
{
if( !bbstree_insert_unique_value(&m_tree, &(*first)) )
throw std::bad_alloc();
}
}
size_type count( const key_type& k ) const
{ return bbstree_count( &m_tree, &k ); }
iterator find( const key_type& k ) const
{ return bbstree_find( &m_tree, &k ); }
iterator lower_bound( const key_type& k ) const
{ return bbstree_lower_bound( &m_tree, &k ); }
iterator upper_bound( const key_type& k ) const
{ return bbstree_upper_bound( &m_tree, &k ); }
std::pair<iterator, iterator> equal_range( const key_type& k ) const
{
youngc::bbstree_pair_iterator pr = bbstree_equal_range( &m_tree, &k );
return std::pair<iterator, iterator>( pr.first, pr.second );
}
bool replace_key( iterator position, const key_type& new_key )
{
typedef typename property_traits<key_type>::has_trivial_assign assign;
return bbstree_replace_key_unique_pos( &m_tree, position.itr,
&new_key, assign_copy_adapter<key_type> );
}
iterator replace_key( const key_type& old_key, const key_type& new_key )
{
typedef typename property_traits<key_type>::has_trivial_assign assign;
return bbstree_replace_key_unique( &m_tree, &old_key, &new_key,
assign_copy_adapter<key_type> );
}
private:
mutable youngc::bbstree m_tree;
void init()
{
youngc::ylib_fp_copy_t cp = init_copy_adapter<value_type>;
youngc::ylib_fp_oper_t dsty = destroy_adapter<value_type>;
bbstree_init( &m_tree, sizeof(value_type),
property_traits<value_type>::trivial_copy ? NULL : cp,
property_traits<value_type>::trivial_destructor ? NULL : dsty,
NULL, cmp_adapter<Key, Compare>,
alloc_adapter<Allocator>, dealloc_adapter<Allocator> );
}
};
template< typename Key, typename Compare, typename Allocator >
inline void swap( set<Key, Compare, Allocator>& lhs,
set<Key, Compare, Allocator>& rhs )
{
lhs.swap( rhs );
}
template< typename Key, typename Compare, typename Allocator >
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -