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

王朝c/c++·作者佚名  2006-01-10
宽屏版  字体: |||超大  

文件位置: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

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

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有