From: Carl N Baldwin Date: Thu, 10 Nov 2005 17:02:56 +0000 (-0700) Subject: Cleaner comparators X-Git-Tag: release-0.4~15^2~4 X-Git-Url: http://git.pippins.net/embedvideo/.git/static/%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%01%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%0F%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD?a=commitdiff_plain;h=3bf11ff8fdf9fe809619f1e31747080ea1a12f88;p=backups%2F.git Cleaner comparators --- diff --git a/filedata.cpp b/filedata.cpp index 6b14517..81ebb58 100644 --- a/filedata.cpp +++ b/filedata.cpp @@ -25,6 +25,10 @@ vector split( const string &line, char c, int limit = -1 ) { return out; } +const FileData::LastBackupCmp FileData::lastbackupcmp = FileData::LastBackupCmp(); +const FileData::SizeCmp FileData::sizecmp = FileData::SizeCmp(); +const FileData::NameCmp FileData::namecmp = FileData::NameCmp(); + ostream &operator<<( ostream &o, const FileData *d) { return operator<<( o, *d ); } diff --git a/filedata.hpp b/filedata.hpp index b339f05..f98b520 100644 --- a/filedata.hpp +++ b/filedata.hpp @@ -29,6 +29,30 @@ class FileData { void setFileName( const std::string &arg ) { filename = arg; } void setLastBackupDate( unsigned long long arg ) { last_backup_date = arg; } + struct LastBackupCmp { + bool operator()( const FileData *a, const FileData *b ) { + return a->getLastBackupDate() < b->getLastBackupDate(); + } + }; + + struct SizeCmp { + bool operator()( const FileData *a, const FileData *b ) { + return a->getFileSize() < b->getFileSize(); + } + }; + + struct NameCmp { + bool operator()( const FileData *a, const FileData *b ) { + return cmp( a->getFileName(), b->getFileName() ); + } + private: + std::less cmp; + }; + + static const LastBackupCmp lastbackupcmp; + static const SizeCmp sizecmp; + static const NameCmp namecmp; + private: FileData( const FileData & ); @@ -48,29 +72,9 @@ std::istream &operator>>( std::istream &i, FileData *d ); std::ostream &operator<<( std::ostream &o, const FileData &d ); std::istream &operator>>( std::istream &i, FileData &d ); -struct FileDataLastBackupCmp { - bool operator()( const FileData *a, const FileData *b ) { - return a->getLastBackupDate() < b->getLastBackupDate(); - } -}; - -struct FileDataSizeCmp { - bool operator()( const FileData *a, const FileData *b ) { - return a->getFileSize() < b->getFileSize(); - } -}; - -struct FileDataNameCmp { - bool operator()( const FileData *a, const FileData *b ) { - return cmp( a->getFileName(), b->getFileName() ); - } - private: - std::less cmp; -}; - bool needs_backup( const FileData *before, const FileData *after ); -typedef std::set file_set; -typedef std::vector file_vector; +typedef std::set file_set; +typedef std::vector file_vector; #endif diff --git a/main.cpp b/main.cpp index 8540adf..06d1a2f 100644 --- a/main.cpp +++ b/main.cpp @@ -62,27 +62,25 @@ void populate_set( istream &in, SET &files ) { template void partition_sets( const SET ¤t, const SET &old, SET &added, SET &common, SET &old_common, SET &deleted ) { - FileDataNameCmp cmp; - set_difference( current.begin(), current.end(), old.begin(), old.end(), inserter( added, added.begin() ), - cmp ); + FileData::namecmp ); set_difference( old.begin(), old.end(), current.begin(), current.end(), inserter( deleted, deleted.begin() ), - cmp ); + FileData::namecmp ); set_intersection( current.begin(), current.end(), old.begin(), old.end(), inserter( common, common.begin() ), - cmp ); + FileData::namecmp ); set_intersection( old.begin(), old.end(), common.begin(), common.end(), inserter( old_common, old_common.begin() ), - cmp ); + FileData::namecmp ); } template @@ -182,8 +180,7 @@ int main() { file_vector backups_s; copy( backups.begin(), backups.end(), back_inserter( backups_s ) ); - FileDataSizeCmp sizecmp; - sort( backups_s.rbegin(), backups_s.rend(), sizecmp ); + sort( backups_s.rbegin(), backups_s.rend(), FileData::sizecmp ); file_set final; unsigned long long space = 0x100000000ULL; @@ -203,17 +200,15 @@ int main() { // collection left with actual content. if( 0 != space ) { file_vector leftovers; - FileDataNameCmp cmp; set_difference( current.begin(), current.end(), final.begin(), final.end(), back_inserter( leftovers ), - cmp ); + FileData::namecmp ); - FileDataLastBackupCmp lastbackupcmp; // Achieve 'last back date then by filesize' by first sorting by filesizing // and then running stable sort by last backup date. - sort( leftovers.rbegin(), leftovers.rend(), sizecmp ); - stable_sort( leftovers.begin(), leftovers.end(), lastbackupcmp ); + sort( leftovers.rbegin(), leftovers.rend(), FileData::sizecmp ); + stable_sort( leftovers.begin(), leftovers.end(), FileData::lastbackupcmp ); copy_until_full( leftovers.begin(), leftovers.end(), final_i, space ); }