C++ young 程序库——y_queue.hpp 和 y_stack.hpp

来源:互联网 发布:秒赞网源码免授权 编辑:程序博客网 时间:2024/06/05 23:44

文件位置:young/y_queue.hpp

/*
The young Library
Copyright (c) 2005 by 杨桓

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_QUEUE_HEADER_FILE__
#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_QUEUE_HEADER_FILE__
//-----------------------------------------------------------------------------
#include "y_functional.hpp"
#include "y_deque.hpp"
#include "y_vector.hpp"
#include "algorithm/y_algorithm_heap.hpp"
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_BEGIN_NAMESPACE__
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template< typename T, typename Container = deque<T> >
class queue
{
public:
    typedef  queue<T, Container>  self;
    typedef  Container  container_type;

    typedef  typename Container::value_type       value_type;
    typedef  typename Container::reference        reference;
    typedef  typename Container::const_reference  const_reference;
    typedef  typename Container::pointer          pointer;
    typedef  typename Container::const_pointer    const_pointer;
    typedef  typename Container::size_type        size_type;
    typedef  typename Container::difference_type  difference_type;

private:
    Container con;

public:
    explicit queue( const Container& other = Container() ) : con(other)  {}

    bool empty() const              {  return con.empty();  }
    size_type size() const          {  return con.size();  }
    reference front()               {  return con.front();  }
    const_reference front() const   {  return con.front();  }
    reference back()                {  return con.back();  }
    const_reference back() const    {  return con.back();  }
    void push( const_reference x )  {  con.push_back(x);  }
    void pop()                      {  con.pop_front();  }
    void swap( self& rhs )          {  con.swap( rhs.con );  }

    bool operator==( const self& rhs ) const  {  return ( con == rhs.con );  }
    bool operator!=( const self& rhs ) const  {  return ( con != rhs.con );  }
    bool operator<( const self& rhs ) const   {  return ( con < rhs.con );  }
    bool operator>( const self& rhs ) const   {  return ( con > rhs.con );  }
    bool operator<=( const self& rhs ) const  {  return ( con <= rhs.con );  }
    bool operator>=( const self& rhs ) const  {  return ( con >= rhs.con );  }
};

template< typename T, typename Container >
inline void swap( queue<T, Container>& lhs, queue<T, Container>& rhs )
{
    lhs.swap( rhs );
}

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

template< typename T, typename Container = vector<T>,
          typename Compare = less<typename Container::value_type> >
class priority_queue
{
public:
    typedef  priority_queue<T, Container, Compare>  self;
    typedef  Container  container_type;

    typedef  typename Container::value_type       value_type;
    typedef  typename Container::reference        reference;
    typedef  typename Container::const_reference  const_reference;
    typedef  typename Container::pointer          pointer;
    typedef  typename Container::const_pointer    const_pointer;
    typedef  typename Container::size_type        size_type;
    typedef  typename Container::difference_type  difference_type;

private:
    Container con;
    Compare  comp;

public:
    explicit priority_queue( const Compare& cmp = Compare() )
             : con(), comp(cmp)  {}

    template< typename InputIterator >
    priority_queue( InputIterator first, InputIterator last )
    : con(first, last), comp()
    {
        make_heap( con.begin(), con.end(), comp );
    }

    template< typename InputIterator >
    priority_queue( InputIterator first, InputIterator last, const Compare& cmp )
    : con(first, last), comp(cmp)
    {
        make_heap( con.begin(), con.end(), comp );
    }

    bool empty() const           {  return con.empty();  }
    size_type size() const       {  return con.size();  }
    const_reference top() const  {  return con.front();  }
    void swap( self& rhs )       {  con.swap( rhs.con );  }

    void push( const_reference value )
    {
        con.push_back( value );
        push_heap( con.begin(), con.end(), comp );
    }

    void pop()
    {
        pop_heap( con.begin(), con.end(), comp );
        con.pop_back();
    }
};

template< typename T, typename Container, typename Compare >
inline void swap( priority_queue<T, Container, Compare>& lhs,
                  priority_queue<T, Container, Compare>& rhs )
{
    lhs.swap( rhs );
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_END_NAMESPACE__
#endif
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

文件位置:young/y_stack.hpp

/*
The young Library
Copyright (c) 2005 by 杨桓

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_STACK_HEADER_FILE__
#define __MACRO_CPLUSPLUS_YOUNG_LIBRARY_STACK_HEADER_FILE__
//-----------------------------------------------------------------------------
#include "y_deque.hpp"
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_BEGIN_NAMESPACE__
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template< typename T, typename Container = deque<T> >
class stack
{
public:
    typedef  stack<T, Container>  self;
    typedef  Container            container_type;

    typedef  typename Container::value_type       value_type;
    typedef  typename Container::reference        reference;
    typedef  typename Container::const_reference  const_reference;
    typedef  typename Container::pointer          pointer;
    typedef  typename Container::const_pointer    const_pointer;
    typedef  typename Container::size_type        size_type;
    typedef  typename Container::difference_type  difference_type;

protected:
    Container con;

public:
    explicit stack( const Container& other = Container() ) : con(other)  {}

    bool empty() const              {  return con.empty();  }
    size_type size() const          {  return con.size();  }
    reference top()                 {  return con.back();  }
    const_reference top() const     {  return con.back();  }
    void push( const_reference x )  {  con.push_back( x );  }
    void pop()                      {  con.pop_back();  }
    void swap( self& rhs )          {  con.swap( rhs.con );  }

    bool operator==( const self& rhs ) const  {  return ( con == rhs.con );  }
    bool operator!=( const self& rhs ) const  {  return ( con != rhs.con );  }
    bool operator<( const self& rhs ) const   {  return ( con < rhs.con );  }
    bool operator>( const self& rhs ) const   {  return ( con > rhs.con );  }
    bool operator<=( const self& rhs ) const  {  return ( con <= rhs.con );  }
    bool operator>=( const self& rhs ) const  {  return ( con >= rhs.con );  }
};

template< typename T, typename Container >
inline void swap( stack<T, Container>& lhs, stack<T, Container>& rhs )
{
    lhs.swap( rhs );
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_YOUNG_LIBRARY_END_NAMESPACE__
#endif
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------