473,853 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

is there lazy copy for STL containers?

Hi,

I'm wondering if the following assignment is lazy copy or not?

Thanks,
Peng

std::vector<int v.
v.push_back(1);
v.push_back(2);
std::vector<int w;
w = v;// is this lazy copy or not?
Sep 12 '08 #1
6 3939
Peng Yu wrote:
Hi,

I'm wondering if the following assignment is lazy copy or not?

Thanks,
Peng

std::vector<int v.
v.push_back(1);
v.push_back(2);
std::vector<int w;
w = v;// is this lazy copy or not?
It isn't lazy in the implementations that I have seen.

Also: because memory for a vector is contiguous and operator[] returns a
reference and not some smart proxy it is at least very hard to make the
copy lazy. Consider:

w = v; // lazy copy
int & w1 = w[1];
w[0] = 0; // triggers physical copy
Best

Kai-Uwe Bux
Sep 12 '08 #2
On Sep 11, 8:49 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
Peng Yu wrote:
Hi,
I'm wondering if the following assignment is lazy copy or not?
Thanks,
Peng
std::vector<int v.
v.push_back(1);
v.push_back(2);
std::vector<int w;
w = v;// is this lazy copy or not?

It isn't lazy in the implementations that I have seen.

Also: because memory for a vector is contiguous and operator[] returns a
reference and not some smart proxy it is at least very hard to make the
copy lazy. Consider:

w = v; // lazy copy
int & w1 = w[1];
w[0] = 0; // triggers physical copy
Is there a contiguous container in STL that is based on lazy-copy?

Thanks,
Peng
Sep 12 '08 #3
On 12 Sep, 06:08, Peng Yu <PengYu...@gmai l.comwrote:
>
Is there a contiguous container in STL that is based on lazy-copy?
Historically, there have been some implementations of std::string
based on "copy-on-write". Nowadays I believe that the common consensus
is that "always-copy" is simpler, safer, and often faster than other
implementations .

DP
Sep 12 '08 #4
Peng Yu wrote:
Is there a contiguous container in STL that is based on lazy-copy?
No. The C++ standard library provides nothing in terms of modern immutable
and/or lazy data structures.

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?u
Sep 13 '08 #5
Peng Yu <Pe*******@gmai l.comkirjutas:
>
Is there a contiguous container in STL that is based on lazy-copy?
No, if you want one you have to roll one yourself or use some third-party
library.

The issue with STL containers is that they can return references to
internal data items (operator[](), back(), front(), etc.) which may or
may not be used for modification of the container content. OTOH, you need
copy-on-write only when you are really going to write. To make a lazy
container fool-proof, easy-to-use and efficient in the same time seems
to be non-trivial so STL has chosen the efficiency and letting the user
to build custom data structures (lazy, thread-safe, whatever) on top of
those primitives.

The (proprietary) software package I am working on uses some sort of
copy-on-write. The objects effectively have only two member functions to
get pointer to the inner data array:

const unsigned char* GetByteArray() const; // does not make any copy
unsigned char* GetByteArray(); // makes a copy if array in shared use.

This means if the object is accessed through a const reference, no copy
occurs, otherwise the lazy copy-on-write kicks in. This system basically
works, but it requires some client code cooperation: it should always
access the object as either const or non-const, using pointers obtained
by both const and non-const references together would yield havoc.

In retrospect I think that relying on const overloads is actually too
brittle, the copying function should have been named something like
GetMutableByteA rray() instead.

Regards
Paavo

Sep 13 '08 #6
Peng Yu wrote:
On Sep 11, 8:49 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
>Peng Yu wrote:
Hi,
I'm wondering if the following assignment is lazy copy or not?
Thanks,
Peng
std::vector<int v.
v.push_back(1);
v.push_back(2);
std::vector<int w;
w = v;// is this lazy copy or not?

It isn't lazy in the implementations that I have seen.

Also: because memory for a vector is contiguous and operator[] returns a
reference and not some smart proxy it is at least very hard to make the
copy lazy. Consider:

w = v; // lazy copy
int & w1 = w[1];
w[0] = 0; // triggers physical copy

Is there a contiguous container in STL that is based on lazy-copy?
No. But you can roll your own. The following might provide a start. It
contains a lot boiler plate code, which can probably be folded somehow. The
code is based upon copy-on-write and has some conservative assumptions on
when a copy is needed.
#include <tr1/memory>
#include <iterator>
#include <memory>
#include <vector>
#include <deque>
#include <cassert>
#include <stdexcept>
#include <algorithm>

namespace kubux {

template < typename Container >
class cow_container {

typedef std::tr1::share d_ptr< Container container_ptr;

container_ptr the_ptr;

void ensure_unique ( void ) {
if ( ! the_ptr.unique( ) ) {
the_ptr = container_ptr( new Container (*the_ptr));
}
}

typedef typename Container::iter ator Iterator;
typedef typename Container::cons t_iterator ConstIterator;

public:

cow_container ( void )
: the_ptr ( new Container () )
{}

template < typename A >
cow_container ( A const & a )
: the_ptr ( new Container (a) )
{}

template < typename A, typename B >
cow_container ( A const & a, B const & b )
: the_ptr ( new Container (a, b ) )
{}
typedef typename Container::valu e_type value_type;
typedef typename Container::size _type size_type;
typedef typename Container::diff erence_type difference_type ;
typedef typename Container::allo cator_type allocator_type;

allocator_type get_allocator ( void ) const {
return ( the_ptr->get_allocator( ) );
}
class reference;
class const_reference ;

class pointer;
class const_pointer;

class iterator;
class const_iterator;
class reference {

friend class cow_container;
friend class pointer;

cow_container * ptr;
size_type pos;
value_type value;
bool written;

reference ( cow_container * adr, size_type where )
: ptr ( adr )
, pos ( where )
, value ( ptr->the_ptr->operator[]( pos ) )
, written ( false )
{}

public:

operator value_type & ( void ) {
written = true;
return ( value );
}

operator value_type const & ( void ) const {
return ( value );
}

reference & operator= ( value_type const & rhs ) {
written = true;
value = rhs;
return ( *this );
}

reference & operator= ( reference const & ref ) {
ptr = ref.ptr;
pos = ref.pos;
value = ref.value;
written = true;
return ( *this );
}

~reference ( void ) {
if ( written ) {
ptr->ensure_unique( );
ptr->the_ptr->operator[]( pos ) = value;
}
}
friend
bool operator== ( reference const & lhs,
const_reference const & rhs ) {
return ( static_cast< value_type const & >( lhs )
==
static_cast< value_type const & >( rhs ) );
}

friend
bool operator== ( const_reference const & lhs,
reference const & rhs ) {
return ( static_cast< value_type const & >( lhs )
==
static_cast< value_type const & >( rhs ) );
}

friend
bool operator!= ( reference const & lhs,
const_reference const & rhs ) {
return ( static_cast< value_type const & >( lhs )
!=
static_cast< value_type const & >( rhs ) );
}

friend
bool operator!= ( const_reference const & lhs,
reference const & rhs ) {
return ( static_cast< value_type const & >( lhs )
!=
static_cast< value_type const & >( rhs ) );
}

friend
bool operator< ( reference const & lhs,
const_reference const & rhs ) {
return ( static_cast< value_type const & >( lhs )
<
static_cast< value_type const & >( rhs ) );
}

friend
bool operator< ( const_reference const & lhs,
reference const & rhs ) {
return ( static_cast< value_type const & >( lhs )
<
static_cast< value_type const & >( rhs ) );
}

friend
bool operator<= ( reference const & lhs,
const_reference const & rhs ) {
return ( static_cast< value_type const & >( lhs )
<=
static_cast< value_type const & >( rhs ) );
}

friend
bool operator<= ( const_reference const & lhs,
reference const & rhs ) {
return ( static_cast< value_type const & >( lhs )
<=
static_cast< value_type const & >( rhs ) );
}

friend
bool operator( reference const & lhs,
const_reference const & rhs ) {
return ( static_cast< value_type const & >( lhs )
>
static_cast< value_type const & >( rhs ) );
}

friend
bool operator( const_reference const & lhs,
reference const & rhs ) {
return ( static_cast< value_type const & >( lhs )
>
static_cast< value_type const & >( rhs ) );
}

friend
bool operator>= ( reference const & lhs,
const_reference const & rhs ) {
return ( static_cast< value_type const & >( lhs )
>=
static_cast< value_type const & >( rhs ) );
}

friend
bool operator>= ( const_reference const & lhs,
reference const & rhs ) {
return ( static_cast< value_type const & >( lhs )
>=
static_cast< value_type const & >( rhs ) );
}
friend
bool operator== ( reference const & lhs,
reference const & rhs ) {
return ( static_cast< value_type const & >( lhs )
==
static_cast< value_type const & >( rhs ) );
}

friend
bool operator== ( value_type const & lhs,
reference const & rhs ) {
return ( lhs
==
static_cast< value_type const & >( rhs ) );
}

friend
bool operator== ( reference const & lhs,
value_type const & rhs ) {
return ( static_cast< value_type const & >( lhs )
==
rhs );
}

friend
bool operator!= ( reference const & lhs,
reference const & rhs ) {
return ( static_cast< value_type const & >( lhs )
!=
static_cast< value_type const & >( rhs ) );
}

friend
bool operator!= ( value_type const & lhs,
reference const & rhs ) {
return ( lhs
!=
static_cast< value_type const & >( rhs ) );
}

friend
bool operator!= ( reference const & lhs,
value_type const & rhs ) {
return ( static_cast< value_type const & >( lhs )
!=
rhs );
}

friend
bool operator< ( reference const & lhs,
reference const & rhs ) {
return ( static_cast< value_type const & >( lhs )
<
static_cast< value_type const & >( rhs ) );
}

friend
bool operator< ( value_type const & lhs,
reference const & rhs ) {
return ( lhs
<
static_cast< value_type const & >( rhs ) );
}

friend
bool operator< ( reference const & lhs,
value_type const & rhs ) {
return ( static_cast< value_type const & >( lhs )
<
rhs );
}

friend
bool operator<= ( reference const & lhs,
reference const & rhs ) {
return ( static_cast< value_type const & >( lhs )
<=
static_cast< value_type const & >( rhs ) );
}

friend
bool operator<= ( value_type const & lhs,
reference const & rhs ) {
return ( lhs
<=
static_cast< value_type const & >( rhs ) );
}

friend
bool operator<= ( reference const & lhs,
value_type const & rhs ) {
return ( static_cast< value_type const & >( lhs )
<=
rhs );
}

friend
bool operator( reference const & lhs,
reference const & rhs ) {
return ( static_cast< value_type const & >( lhs )
>
static_cast< value_type const & >( rhs ) );
}

friend
bool operator( value_type const & lhs,
reference const & rhs ) {
return ( lhs
>
static_cast< value_type const & >( rhs ) );
}

friend
bool operator( reference const & lhs,
value_type const & rhs ) {
return ( static_cast< value_type const & >( lhs )
>
rhs );
}

friend
bool operator>= ( reference const & lhs,
reference const & rhs ) {
return ( static_cast< value_type const & >( lhs )
>=
static_cast< value_type const & >( rhs ) );
}

friend
bool operator>= ( value_type const & lhs,
reference const & rhs ) {
return ( lhs
>=
static_cast< value_type const & >( rhs ) );
}

friend
bool operator>= ( reference const & lhs,
value_type const & rhs ) {
return ( static_cast< value_type const & >( lhs )
>=
rhs );
}
pointer operator& ( void ) const {
return ( pointer( ptr, pos ) );
}

}; // reference
class pointer {

friend class reference;
friend class iterator;

reference ref;

pointer ( cow_container * adr, size_type where )
: ref ( adr, where )
{}

public:

reference operator* ( void ) const {
return ( ref );
}

value_type * operator-( void ) {
return ( & ref.value );
}

}; // pointer
class const_reference {

friend class cow_container;

cow_container const * ptr;
size_type pos;

const_reference ( cow_container const * adr, size_type where )
: ptr ( adr )
, pos ( where )
{}

public:

operator value_type const & ( void ) const {
return ( ptr->the_ptr->operator[]( pos ) );
}
friend
bool operator== ( const_reference const & lhs,
const_reference const & rhs ) {
return ( static_cast< value_type const & >( lhs )
==
static_cast< value_type const & >( rhs ) );
}

friend
bool operator== ( value_type const & lhs,
const_reference const & rhs ) {
return ( lhs
==
static_cast< value_type const & >( rhs ) );
}

friend
bool operator== ( const_reference const & lhs,
value_type const & rhs ) {
return ( static_cast< value_type const & >( lhs )
==
rhs );
}

friend
bool operator!= ( const_reference const & lhs,
const_reference const & rhs ) {
return ( static_cast< value_type const & >( lhs )
!=
static_cast< value_type const & >( rhs ) );
}

friend
bool operator!= ( value_type const & lhs,
const_reference const & rhs ) {
return ( lhs
!=
static_cast< value_type const & >( rhs ) );
}

friend
bool operator!= ( const_reference const & lhs,
value_type const & rhs ) {
return ( static_cast< value_type const & >( lhs )
!=
rhs );
}

friend
bool operator< ( const_reference const & lhs,
const_reference const & rhs ) {
return ( static_cast< value_type const & >( lhs )
<
static_cast< value_type const & >( rhs ) );
}

friend
bool operator< ( value_type const & lhs,
const_reference const & rhs ) {
return ( lhs
<
static_cast< value_type const & >( rhs ) );
}

friend
bool operator< ( const_reference const & lhs,
value_type const & rhs ) {
return ( static_cast< value_type const & >( lhs )
<
rhs );
}

friend
bool operator<= ( const_reference const & lhs,
const_reference const & rhs ) {
return ( static_cast< value_type const & >( lhs )
<=
static_cast< value_type const & >( rhs ) );
}

friend
bool operator<= ( value_type const & lhs,
const_reference const & rhs ) {
return ( lhs
<=
static_cast< value_type const & >( rhs ) );
}

friend
bool operator<= ( const_reference const & lhs,
value_type const & rhs ) {
return ( static_cast< value_type const & >( lhs )
<=
rhs );
}

friend
bool operator( const_reference const & lhs,
const_reference const & rhs ) {
return ( static_cast< value_type const & >( lhs )
>
static_cast< value_type const & >( rhs ) );
}

friend
bool operator( value_type const & lhs,
const_reference const & rhs ) {
return ( lhs
>
static_cast< value_type const & >( rhs ) );
}

friend
bool operator( const_reference const & lhs,
value_type const & rhs ) {
return ( static_cast< value_type const & >( lhs )
>
rhs );
}

friend
bool operator>= ( const_reference const & lhs,
const_reference const & rhs ) {
return ( static_cast< value_type const & >( lhs )
>=
static_cast< value_type const & >( rhs ) );
}

friend
bool operator>= ( value_type const & lhs,
const_reference const & rhs ) {
return ( lhs
>=
static_cast< value_type const & >( rhs ) );
}

friend
bool operator>= ( const_reference const & lhs,
value_type const & rhs ) {
return ( static_cast< value_type const & >( lhs )
>=
rhs );
}
const_pointer operator& ( void ) const {
return ( const_pointer( ptr, pos ) );
}

}; // const_reference
class const_pointer {

friend class const_reference ;

const_reference ref;

const_pointer ( cow_container const * adr, size_type where )
: ref ( adr, where )
{}

public:

const_reference operator* ( void ) const {
return ( ref );
}

value_type const * operator-( void ) const {
return ( &( static_cast< value_type const & >( ref ) ) );
}

}; // const_pointer
reference operator[] ( size_type where ) {
return ( reference( this, where ) );
}

const_reference operator[] ( size_type where ) const {
return ( const_reference ( this, where ) );
}

reference at ( size_type where ) {
if ( size() <= where ) {
throw ( std::out_of_ran ge("cow_contain er") );
}
return ( reference( this, where ) );
}

const_reference at ( size_type where ) const {
if ( size() <= where ) {
throw ( std::out_of_ran ge("cow_contain er") );
}
return ( const_reference ( this, where ) );
}
size_type size ( void ) const {
return ( the_ptr->size() );
}

size_type max_size ( void ) const {
return ( the_ptr->max_size() );
}

void resize ( size_type new_size,
value_type const & t = value_type() ) {
ensure_unique() ;
the_ptr->resize( new_size, t );
}

bool empty ( void ) const {
return ( the_ptr->empty() );
}

class iterator
: public std::iterator<
typename std::iterator_t raits<Iterator> ::iterator_cate gory,
typename std::iterator_t raits<Iterator> ::value_type,
typename std::iterator_t raits<Iterator> ::difference_ty pe,
pointer,
reference
>
{

cow_container * ptr;
size_type pos;

friend class cow_container;
friend class const_iterator;

iterator ( cow_container * adr, size_type where )
: ptr ( adr )
, pos ( where )
{}

public:

reference operator* ( void ) const {
return ( reference( ptr, pos ) );
}

pointer operator-( void ) const {
return ( pointer( ptr, pos ) );
}

reference operator[] ( difference_type displ ) const {
return ( reference( ptr, pos+displ ) );
}

iterator ( void )
: ptr ( 0 )
, pos ( 0 )
{}

friend
iterator operator+ ( difference_type displ, iterator where ) {
return ( iterator( where.ptr, where.pos+displ ) );
}

friend
iterator operator+ ( iterator where, difference_type displ ) {
return ( iterator( where.ptr, where.pos+displ ) );
}

friend
iterator operator- ( iterator where, difference_type displ ) {
return ( iterator( where.ptr, where.pos - displ ) );
}

iterator & operator+= ( difference_type displ ) {
pos += displ;
return ( *this );
}

iterator & operator-= ( difference_type displ ) {
pos -= displ;
return ( *this );
}

difference_type operator- ( iterator other ) const {
assert( ptr == other.ptr );
return ( pos - other.pos );
}

iterator & operator++ ( void ) {
++ pos;
return ( *this );
}

iterator & operator-- ( void ) {
-- pos;
return ( *this );
}

iterator operator++ ( int ) {
iterator dummy ( *this );
++ pos;
return ( dummy );
}

iterator operator-- ( int ) {
iterator dummy ( *this );
-- pos;
return ( dummy );
}

bool operator< ( iterator other ) const {
assert( ptr == other.ptr );
return ( pos < other.pos );
}

bool operator<= ( iterator other ) const {
assert( ptr == other.ptr );
return ( pos <= other.pos );
}

bool operator( iterator other ) const {
assert( ptr == other.ptr );
return ( pos other.pos );
}

bool operator>= ( iterator other ) const {
assert( ptr == other.ptr );
return ( pos >= other.pos );
}

bool operator== ( iterator other ) const {
assert( ptr == other.ptr );
return ( pos == other.pos );
}

bool operator!= ( iterator other ) const {
assert( ptr == other.ptr );
return ( pos != other.pos );
}

}; // iterator
iterator begin ( void ) {
return ( iterator( this, 0u ) );
}

iterator end ( void ) {
return ( iterator( this, size() ) );
}
class const_iterator
: public std::iterator<
typename std::iterator_t raits<ConstIter ator>::iterator _category,
typename std::iterator_t raits<ConstIter ator>::value_ty pe,
typename std::iterator_t raits<ConstIter ator>::differen ce_type,
const_pointer,
const_reference
>
{

cow_container const * ptr;
size_type pos;

friend class cow_container;

const_iterator ( cow_container const * adr, size_type where )
: ptr ( adr )
, pos ( where )
{}

public:

const_iterator ( void )
: ptr ( 0 )
, pos ( 0 )
{}

const_iterator ( iterator iter )
: ptr ( iter.ptr )
, pos ( iter.pos )
{}

const_reference operator* ( void ) const {
return ( const_reference ( ptr, pos ) );
}

const_pointer operator-( void ) const {
return ( const_pointer( ptr, pos ) );
}

const_reference operator[] ( difference_type displ ) const {
return ( const_reference ( ptr, pos+displ ) );
}
friend
const_iterator operator+ ( difference_type displ,
const_iterator where ) {
return ( const_iterator( where.ptr, where.pos+displ ) );
}

friend
const_iterator operator+ ( const_iterator where,
difference_type displ ) {
return ( const_iterator( where.ptr, where.pos+displ ) );
}

friend
const_iterator operator- ( const_iterator where,
difference_type displ ) {
return ( const_iterator( where.ptr, where.pos - displ ) );
}

const_iterator & operator+= ( difference_type displ ) {
pos += displ;
return ( *this );
}

const_iterator & operator-= ( difference_type displ ) {
pos -= displ;
return ( *this );
}

difference_type operator- ( const_iterator other ) const {
assert( ptr == other.ptr );
return ( pos - other.pos );
}

const_iterator & operator++ ( void ) {
++ pos;
return ( *this );
}

const_iterator & operator-- ( void ) {
-- pos;
return ( *this );
}

const_iterator operator++ ( int ) {
const_iterator dummy ( *this );
++ pos;
return ( dummy );
}

const_iterator operator-- ( int ) {
const_iterator dummy ( *this );
-- pos;
return ( dummy );
}

bool operator< ( const_iterator other ) const {
assert( ptr == other.ptr );
return ( pos < other.pos );
}

bool operator<= ( const_iterator other ) const {
assert( ptr == other.ptr );
return ( pos <= other.pos );
}

bool operator( const_iterator other ) const {
assert( ptr == other.ptr );
return ( pos other.pos );
}

bool operator>= ( const_iterator other ) const {
assert( ptr == other.ptr );
return ( pos >= other.pos );
}

bool operator== ( const_iterator other ) const {
assert( ptr == other.ptr );
return ( pos == other.pos );
}

bool operator!= ( const_iterator other ) const {
assert( ptr == other.ptr );
return ( pos != other.pos );
}

}; // const_iterator
const_iterator begin ( void ) const {
return ( const_iterator( this, 0u ) );
}

const_iterator end ( void ) const {
return ( const_iterator( this, size() ) );
}
typedef std::reverse_it erator< iterator reverse_iterato r;
typedef std::reverse_it erator< const_iterator >
const_reverse_i terator;

reverse_iterato r rbegin ( void ) {
return ( reverse_iterato r( end() ) );
}

reverse_iterato r rend ( void ) {
return ( reverse_iterato r( begin() ) );
}

const_reverse_i terator rbegin ( void ) const {
return ( const_reverse_i terator( end() ) );
}

const_reverse_i terator rend ( void ) const {
return ( const_reverse_i terator( begin() ) );
}

iterator insert ( iterator where, value_type const & t ) {
ensure_unique() ;
the_ptr->insert( the_ptr->begin()+where. pos, t );
return ( where );
}

void insert ( iterator where, size_type n, value_type const & t ) {
ensure_unique() ;
the_ptr->insert( the_ptr->begin()+where. pos, n, t );
}

template < typename Iter >
void insert ( iterator where, Iter from, Iter to ) {
ensure_unique() ;
the_ptr->insert( the_ptr->begin()+where. pos, from, to );
}

iterator erase ( iterator where ) {
ensure_unique() ;
the_ptr->erase( the_ptr->begin()+where. pos );
return ( where );
}

iterator erase ( iterator from, iterator to ) {
ensure_unique() ;
the_ptr->erase( the_ptr->begin()+from.p os,
the_ptr->begin()+to.p os );
return ( from );
}

template < typename A, typename B >
void assign ( A const & a, B const & b ) {
ensure_unique() ;
the_ptr->assign( a, b );
}

void clear ( void ) {
ensure_unique() ;
the_ptr->clear();
}
reference front ( void ) {
return ( reference( this, 0 ) );
}

reference back ( void ) {
return ( reference( this, size()-1 ) );
}

const_reference front ( void ) const {
return ( const_reference ( this, 0 ) );
}

const_reference back ( void ) const {
return ( const_reference ( this, size()-1 ) );
}
void push_back ( value_type const & value ) {
ensure_unique() ;
the_ptr->push_back( value );
}

void push_front ( value_type const & value ) {
ensure_unique() ;
the_ptr->push_front( value );
}

void pop_back ( void ) {
ensure_unique() ;
the_ptr->pop_back();
}

void push_front ( void ) {
ensure_unique() ;
the_ptr->pop_front();
}

friend
bool operator== ( cow_container const & lhs,
cow_container const & rhs ) {
return ( lhs.size() == rhs.size()
&&
std::equal( lhs.begin(), lhs.end(), rhs.begin() ) );
}

friend
bool operator!= ( cow_container const & lhs,
cow_container const & rhs ) {
return ( ! ( lhs == rhs ) );
}

friend
bool operator< ( cow_container const & lhs,
cow_container const & rhs ) {
return ( std::lexicograp hical_compare
( lhs.begin(), lhs.end(), rhs.begin(), rhs.end() ) );
}

friend
bool operator( cow_container const & lhs,
cow_container const & rhs ) {
return ( rhs < lhs );
}

friend
bool operator<= ( cow_container const & lhs,
cow_container const & rhs ) {
return ( ! ( lhs rhs ) );
}

friend
bool operator>= ( cow_container const & lhs,
cow_container const & rhs ) {
return ( ! ( lhs < rhs ) );
}
void swap ( cow_container & rhs ) {
swap( the_ptr, rhs.the_ptr );
}

}; // cow_container
template < typename T, typename Alloc = std::allocator< T
class cow_vector
: public cow_container< std::vector< T, Alloc
{

typedef cow_container< std::vector< T, Alloc base;

public:

cow_vector ( void )
: base ()
{}

template < typename A >
cow_vector ( A const & a )
: base ( a )
{}

template < typename A, typename B >
cow_vector ( A const & a, B const & b )
: base ( a, b )
{}

typedef typename base::value_typ e value_type;
typedef typename base::size_type size_type;
typedef typename base::differenc e_type difference_type ;
typedef typename base::allocator _type allocator_type;

typedef typename base::pointer pointer;
typedef typename base::const_poi nter const_pointer;

typedef typename base::reference reference;
typedef typename base::const_ref erence const_reference ;

typedef typename base::iterator iterator;
typedef typename base::const_ite rator const_iterator;
typedef typename base::reverse_i terator reverse_iterato r;
typedef typename base::const_rev erse_iterator const_reverse_i terator;

}; // cow_vector
template < typename T, typename Alloc = std::allocator< T
class cow_deque
: public cow_container< std::deque< T, Alloc
{

typedef cow_container< std::deque< T, Alloc base;

public:

cow_deque ( void )
: base ()
{}

template < typename A >
cow_deque ( A const & a )
: base ( a )
{}

template < typename A, typename B >
cow_deque ( A const & a, B const & b )
: base ( a, b )
{}

typedef typename base::value_typ e value_type;
typedef typename base::size_type size_type;
typedef typename base::differenc e_type difference_type ;
typedef typename base::allocator _type allocator_type;

typedef typename base::pointer pointer;
typedef typename base::const_poi nter const_pointer;

typedef typename base::reference reference;
typedef typename base::const_ref erence const_reference ;

typedef typename base::iterator iterator;
typedef typename base::const_ite rator const_iterator;
typedef typename base::reverse_i terator reverse_iterato r;
typedef typename base::const_rev erse_iterator const_reverse_i terator;

}; // cow_deque

} // namespace kubux

// end of file
Best

Kai-Uwe Bux
Sep 14 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3380
by: Gandalf | last post by:
Hello. I have some questions about the standard containers. How does the standard containers behave if I do queue<Foo> myQ; queue<Foo> myQ2; .... insert into myQ... myQ = myQ2;
3
1635
by: Nudge | last post by:
I am slightly confused, so please keep that in mind if I make only little sense. Assume I have a base class: class Base { ... }; and two derived classes: class Int : public Base { ... int foo; };
14
1726
by: Jinjun Xu | last post by:
Hi, I have an array and I want to adjust the size (remove some elements). I use the following code. Can you help me to check whether it's correct. ////////////// start int *p1 = new int; // .... // set the values of p1
2
1314
by: joegen | last post by:
Hi, I am working on an o project that involves Lazy Parsing. To be more specific, Its a SIP Message class that implements zero copy and lazy parsing. Below is a sample function that is causing me problems. Since my object is a lazy parser, it needs to evaluate the object to be parsed during the time an accessor is called. BOOL SIPMessage::GetAuthorization( Authorization & h
11
7419
by: PengYu.UT | last post by:
The following program calls the normal constructor and the copy constructor. By calling the copy constuctor is redundandant, all I want is only a vector of a trial object. Is there any way to avoid the use of the copy constructor? #include <vector> #include <iostream>
26
15822
by: saxenavaibhav17 | last post by:
what is Deep Copy, Shallow copy and Bitwise copy, Memberwise copy? and what is the difference between them? pls help vaibhav
35
2883
by: dragoncoder | last post by:
Just a simple theoritical question to the experts. What was the rationale behind making STL containers follow copy semantics rather than reference semantics. References almost always make things easier without much of overhead. Then why not reference ? Thanks /P
17
2077
by: qazmlp1209 | last post by:
Will the following program cause any problem, because of the missing explicit copy constructor for the 'test' class? #include <vector> #include <iostream> class test { char* cPtr ;
3
1538
by: Cristiano Paris | last post by:
Hi everyone, I'm trying to write a Container which should mimic a list. Basically, the container pulls items on the fly from an unspecified source through a function and returns an instance of a given class over the pulled item. That is: class lazy(object): def __getitem__(self,index):
0
9898
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9748
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10735
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7907
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5736
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5934
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4549
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4143
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.