⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ycpp_map.hpp

📁 一个类STL的多平台可移植的算法容器库,主要用于嵌入式系统编程时的内存管理等方面
💻 HPP
📖 第 1 页 / 共 2 页
字号:
                  map<Key, Value, Compare, Allocator>& rhs )
{
    lhs.swap( rhs );
}

template< typename Key, typename Value, typename Compare, typename Allocator >
inline bool operator==( const map<Key, Value, Compare, Allocator>& lhs,
                        const map<Key, Value, Compare, Allocator>& rhs )
{
    return lhs.size() == rhs.size()
           && std::equal( lhs.begin(), lhs.end(), rhs.begin() );
}

template< typename Key, typename Value, typename Compare, typename Allocator >
inline bool operator!=( const map<Key, Value, Compare, Allocator>& lhs,
                        const map<Key, Value, Compare, Allocator>& rhs )
{
    return !( lhs == rhs );
}

template< typename Key, typename Value, typename Compare, typename Allocator >
inline bool operator<( const map<Key, Value, Compare, Allocator>& lhs,
                       const map<Key, Value, Compare, Allocator>& rhs )
{
    if( lhs.end() == rhs.end() || lhs.size() > rhs.size() )
        return false;
    return std::lexicographical_compare( lhs.begin(), lhs.end(),
                                         rhs.begin(), rhs.end(), Compare() );
}

template< typename Key, typename Value, typename Compare, typename Allocator >
inline bool operator>( const map<Key, Value, Compare, Allocator>& lhs,
                       const map<Key, Value, Compare, Allocator>& rhs )
{
    return rhs < lhs;
}

template< typename Key, typename Value, typename Compare, typename Allocator >
inline bool operator<=( const map<Key, Value, Compare, Allocator>& lhs,
                        const map<Key, Value, Compare, Allocator>& rhs )
{
    return !( rhs < lhs );
}

template< typename Key, typename Value, typename Compare, typename Allocator >
inline bool operator>=( const map<Key, Value, Compare, Allocator>& lhs,
                        const map<Key, Value, Compare, Allocator>& rhs )
{
    return !( lhs < rhs );
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

template< typename Key, typename Value, typename Compare = std::less<Key>,
          typename Allocator = std::allocator< std::pair<const Key, Value> > >
class multimap
{
public:
    typedef  multimap<Key, Value, Compare, Allocator>  self;

    typedef  Key                            key_type;
    typedef  Value                          mapped_type;
    typedef  std::pair<const Key, Value>    value_type;
    typedef  Compare                        key_compare;
    typedef  Allocator                      allocator_type;

    typedef  size_t                         size_type;
    typedef  ptrdiff_t                      difference_type;
    typedef  value_type&                    reference;
    typedef  const value_type&              const_reference;
    typedef  value_type*                    pointer;
    typedef  const value_type*              cons_pointer;

    typedef  map_iterator<value_type, reference, pointer,
                          Key, mapped_type, Compare, Allocator>  iterator;
    typedef  map_iterator<value_type, const_reference, cons_pointer,
                          Key, mapped_type, Compare, Allocator>  const_iterator;
    typedef  std::reverse_iterator<iterator>        reverse_iterator;
    typedef  std::reverse_iterator<const_iterator>  const_reverse_iterator;

    multimap()
    {
        typedef  typename property_traits<key_type>::is_POD     kPOD;
        typedef  typename property_traits<mapped_type>::is_POD  vPOD;
        init( kPOD(), vPOD() );
    }

    template< typename InputIterator >
    multimap( InputIterator first, InputIterator last )
    {
        typedef  typename property_traits<key_type>::is_POD     kPOD;
        typedef  typename property_traits<mapped_type>::is_POD  vPOD;
        init( kPOD(), vPOD() );
        for( ; first != last; ++first )
        {
            if( !bbstree_insert_unique_value(&m_tree, &(*first)) )
            {
                bbstree_destroy( &m_tree );
                throw std::bad_alloc();
            }
        }
    }

    multimap( 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;
    }

    ~multimap()  {  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()
        {  return iterator( bbstree_begin(&m_tree) );  }
    iterator end()
        {  return iterator( bbstree_end(&m_tree) );  }
    const_iterator begin() const
        {  return const_iterator( bbstree_begin(&m_tree) );  }
    const_iterator end() const
        {  return const_iterator( bbstree_end(&m_tree) );  }

    reverse_iterator rbegin()
        {  return reverse_iterator( end() );  }
    reverse_iterator rend()
        {  return reverse_iterator( begin() );  }
    const_reverse_iterator rbegin() const
        {  return const_reverse_iterator( end() );  }
    const_reverse_iterator rend() const
        {  return const_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 );  }

    iterator insert( const value_type& v )
    {
        youngc::bbstree_iterator p = bbstree_insert_equal_value(&m_tree, &v);
        if( !p )
            throw std::bad_alloc();
        return p;
    }
    iterator insert( iterator pos, const value_type& v )
    {
        youngc::bbstree_iterator p = bbstree_insert_equal_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_equal_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 )
        {  return bbstree_find( &m_tree, &k );  }
    const_iterator find( const key_type& k ) const
        {  return bbstree_find( &m_tree, &k );  }

    iterator lower_bound( const key_type& k )
        {  return bbstree_lower_bound( &m_tree, &k );  }
    const_iterator lower_bound( const key_type& k ) const
        {  return bbstree_lower_bound( &m_tree, &k );  }

    iterator upper_bound( const key_type& k )
        {  return bbstree_upper_bound( &m_tree, &k );  }
    const_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 )
    {
        youngc::bbstree_pair_iterator pr = bbstree_equal_range( &m_tree, &k );
        return std::pair<iterator, iterator>( pr.first, pr.second );
    }
    std::pair<const_iterator, const_iterator> equal_range( const key_type& k ) const
    {
        youngc::bbstree_pair_iterator pr = bbstree_equal_range( &m_tree, &k );
        return std::pair<const_iterator, const_iterator>( pr.first, pr.second );
    }

    void replace_key( iterator position, const key_type& new_key )
    {
        typedef  typename property_traits<key_type>::has_trivial_assign  assign;

        bbstree_replace_key_equal_pos( &m_tree, position.itr,
                                       &new_key, assign_copy_adapter<key_type> );
    }
    void replace_key( const key_type& old_key, const key_type& new_key )
    {
        typedef  typename property_traits<key_type>::has_trivial_assign  assign;

        bbstree_replace_key_equal( &m_tree, &old_key, &new_key,
                                   assign_copy_adapter<key_type> );
    }

private:
    mutable youngc::bbstree m_tree;

    void init( true_type, true_type )
    {
        bbstree_init( &m_tree, sizeof(value_type),
                      NULL, NULL, get_key, cmp_adapter<Key, Compare>,
                      alloc_adapter<Allocator>, dealloc_adapter<Allocator> );
    }

    template< typename T1, typename T2 >
    void init( T1, T2 )
    {
        bbstree_init( &m_tree, sizeof(value_type),
                      init_copy_adapter<value_type>, destroy_adapter<value_type>,
                      get_key, cmp_adapter<Key, Compare>,
                      alloc_adapter<Allocator>, dealloc_adapter<Allocator> );
    }

    static const void* get_key( const void* value )
    {
        return &( ((value_type*)value)->first );
    }
};



template< typename Key, typename Value, typename Compare, typename Allocator >
inline void swap( multimap<Key, Value, Compare, Allocator>& lhs,
                  multimap<Key, Value, Compare, Allocator>& rhs )
{
    lhs.swap( rhs );
}

template< typename Key, typename Value, typename Compare, typename Allocator >
inline bool operator==( const multimap<Key, Value, Compare, Allocator>& lhs,
                        const multimap<Key, Value, Compare, Allocator>& rhs )
{
    return lhs.size() == rhs.size()
           && std::equal( lhs.begin(), lhs.end(), rhs.begin() );
}

template< typename Key, typename Value, typename Compare, typename Allocator >
inline bool operator!=( const multimap<Key, Value, Compare, Allocator>& lhs,
                        const multimap<Key, Value, Compare, Allocator>& rhs )
{
    return !( lhs == rhs );
}

template< typename Key, typename Value, typename Compare, typename Allocator >
inline bool operator<( const multimap<Key, Value, Compare, Allocator>& lhs,
                       const multimap<Key, Value, Compare, Allocator>& rhs )
{
    return std::lexicographical_compare( lhs.begin(), lhs.end(),
                                         rhs.begin(), rhs.end(), Compare() );
}

template< typename Key, typename Value, typename Compare, typename Allocator >
inline bool operator>( const multimap<Key, Value, Compare, Allocator>& lhs,
                       const multimap<Key, Value, Compare, Allocator>& rhs )
{
    return rhs < lhs;
}

template< typename Key, typename Value, typename Compare, typename Allocator >
inline bool operator<=( const multimap<Key, Value, Compare, Allocator>& lhs,
                        const multimap<Key, Value, Compare, Allocator>& rhs )
{
    return !( rhs < lhs );
}

template< typename Key, typename Value, typename Compare, typename Allocator >
inline bool operator>=( const multimap<Key, Value, Compare, Allocator>& lhs,
                        const multimap<Key, Value, Compare, Allocator>& rhs )
{
    return !( lhs < rhs );
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
}  //end namespace
#endif
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -