From 509d136e9adb60c56369565b9545e613cac3678e Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 12 Nov 2009 17:05:30 +0000 Subject: I've started my campaign to clean up all of the header files in libbu++ as far as includes go. This required a little bit of reworking as far as archive goes, but I've been planning on changing it aronud for a bit anyway. The final result here is that you may need to add some more includes in your own code, libbu++ doesn't include as many random things you didn't ask for anymore, most of these seem to be bu/hash.h, unistd.h, and time.h. Also, any Archive functions and operators should use ArchiveBase when they can instead of Archive, archivebase.h is a much lighterweight include that will be used everywhere in core that it can be, there are a few classes that actually want a specific archiver to be used, they will use it (such as the nids storage class). So far, except for adding header files, nothing has changed in functionality, and no other code changes should be required, although the above mentioned archive changeover is reccomended. --- src/list.h | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 112 insertions(+), 15 deletions(-) (limited to 'src/list.h') diff --git a/src/list.h b/src/list.h index d8c5a4a..f76c505 100644 --- a/src/list.h +++ b/src/list.h @@ -11,7 +11,8 @@ #include #include "bu/exceptionbase.h" #include "bu/sharedcore.h" -//#include "bu/util.h" +#include "bu/archivebase.h" +#include "bu/heap.h" namespace Bu { @@ -23,7 +24,7 @@ namespace Bu ListLink *pPrev; }; - template struct ListCore { @@ -42,7 +43,6 @@ namespace Bu Link *pFirst; Link *pLast; long nSize; - cmpfunc cmp; linkalloc la; valuealloc va; @@ -189,16 +189,15 @@ namespace Bu *@param linkalloc (typename) Memory Allocator for the list links. *@ingroup Containers */ - template, - typename valuealloc=std::allocator, + template, typename linkalloc=std::allocator > > - class List : public SharedCore< struct ListCore > { private: typedef struct ListLink Link; - typedef class List MyType; - typedef struct ListCore Core; + typedef class List MyType; + typedef struct ListCore Core; protected: using SharedCore< Core >::core; @@ -241,6 +240,26 @@ namespace Bu return *this; } + bool operator==( const MyType &rhs ) + { + if( getSize() != rhs.getSize() ) + return false; + + for( typename MyType::const_iterator a = begin(), b = rhs.begin(); + a; a++, b++ ) + { + if( *a != *b ) + return false; + } + + return true; + } + + bool operator!=( const MyType &rhs ) + { + return !(*this == rhs); + } + /** * Clear the data from the list. */ @@ -350,6 +369,41 @@ namespace Bu return *this; } + template + void sort( cmptype cmp ) + { + Heap hSort( cmp, getSize() ); + for( typename MyType::iterator i = begin(); i; i++ ) + { + hSort.enqueue( *i ); + } + clear(); + while( !hSort.isEmpty() ) + { + append( hSort.dequeue() ); + } + } + + void sort() + { + sort<__basicLTCmp >(); + } + + template + void sort() + { + Heap hSort( getSize() ); + for( typename MyType::iterator i = begin(); i; i++ ) + { + hSort.enqueue( *i ); + } + clear(); + while( !hSort.isEmpty() ) + { + append( hSort.dequeue() ); + } + } + /** * Insert a new item in sort order by searching for the first item that * is larger and inserting this before it, or at the end if none are @@ -357,7 +411,8 @@ namespace Bu * List all items will be sorted. To use this, the value type must * support the > operator. */ - MyType &insertSorted( const value &v ) + template + MyType &insertSorted( cmptype cmp, const value &v ) { _hardCopy(); if( core->pFirst == NULL ) @@ -371,7 +426,7 @@ namespace Bu Link *pCur = core->pFirst; for(;;) { - if( !core->cmp( v, *(pCur->pValue)) ) + if( cmp( v, *(pCur->pValue)) ) { core->insert( pCur, v ); return *this; @@ -385,6 +440,18 @@ namespace Bu } } } + + MyType &insertSorted( const value &v ) + { + return insertSorted<__basicLTCmp >( v ); + } + + template + MyType &insertSorted( const value &v ) + { + cmptype cmp; + return insertSorted( cmp, v ); + } /** * An iterator to iterate through your list. @@ -392,7 +459,7 @@ namespace Bu typedef struct iterator { friend struct const_iterator; - friend class List; + friend class List; private: Link *pLink; @@ -559,7 +626,7 @@ namespace Bu */ typedef struct const_iterator { - friend class List; + friend class List; private: Link *pLink; @@ -844,11 +911,11 @@ namespace Bu class Formatter; Formatter &operator<<( Formatter &rOut, char *sStr ); Formatter &operator<<( Formatter &rOut, signed char c ); - template - Formatter &operator<<( Formatter &f, const Bu::List &l ) + template + Formatter &operator<<( Formatter &f, const Bu::List &l ) { f << '['; - for( typename Bu::List::const_iterator i = l.begin(); i; i++ ) + for( typename Bu::List::const_iterator i = l.begin(); i; i++ ) { if( i != l.begin() ) f << ", "; @@ -858,6 +925,36 @@ namespace Bu return f; } + + template + ArchiveBase &operator<<( ArchiveBase &ar, const List &h ) + { + ar << h.getSize(); + for( typename List::const_iterator i = h.begin(); i != h.end(); i++ ) + { + ar << (*i); + } + + return ar; + } + + template + ArchiveBase &operator>>( ArchiveBase &ar, List &h ) + { + h.clear(); + long nSize; + ar >> nSize; + + for( long j = 0; j < nSize; j++ ) + { + value v; + ar >> v; + h.append( v ); + } + + return ar; + } + } #endif -- cgit v1.2.3