1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
/*=========================================================================
Program: GDCM (Grassroots DICOM). A DICOM library
Copyright (c) 2006-2011 Mathieu Malaterre
All rights reserved.
See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "gdcmSorter.h"
#include "gdcmElement.h"
#include "gdcmSerieHelper.h"
#include "gdcmFile.h"
#include "gdcmReader.h"
#include <map>
#include <algorithm>
namespace gdcm
{
Sorter::Sorter()
{
SortFunc = NULL;
}
Sorter::~Sorter()
{
}
void Sorter::SetSortFunction( SortFunction f )
{
SortFunc = f;
}
namespace {
class SortFunctor
{
public:
bool operator() (File const *file1, File const *file2)
{
return (SortFunc)(file1->GetDataSet(), file2->GetDataSet());
}
Sorter::SortFunction SortFunc;
SortFunctor()
{
SortFunc = 0;
}
SortFunctor(SortFunctor const &sf)
{
SortFunc = sf.SortFunc;
}
void operator=(Sorter::SortFunction sf)
{
SortFunc = sf;
}
};
}
bool Sorter::StableSort(std::vector<std::string> const & filenames)
{
// BUG: I cannot clear Filenames since input filenames could also be the output of ourself...
// Filenames.clear();
if( filenames.empty() || !SortFunc )
{
Filenames.clear();
return true;
}
std::vector< SmartPointer<FileWithName> > filelist;
filelist.resize( filenames.size() );
std::vector< SmartPointer<FileWithName> >::iterator it2 = filelist.begin();
for( Directory::FilenamesType::const_iterator it = filenames.begin();
it != filenames.end() && it2 != filelist.end(); ++it, ++it2)
{
Reader reader;
reader.SetFileName( it->c_str() );
SmartPointer<FileWithName> &f = *it2;
if( reader.Read() )
{
f = new FileWithName( reader.GetFile() );
f->filename = *it;
}
else
{
gdcmErrorMacro( "File could not be read: " << it->c_str() );
return false;
}
}
SortFunctor sf;
sf = Sorter::SortFunc;
std::stable_sort( filelist.begin(), filelist.end(), sf);
Filenames.clear(); // cleanup any previous call
for(it2 = filelist.begin(); it2 != filelist.end(); ++it2 )
{
SmartPointer<FileWithName> const & f = *it2;
Filenames.push_back( f->filename );
}
return true;
}
bool Sorter::Sort(std::vector<std::string> const & filenames)
{
(void)filenames;
Filenames.clear();
if( filenames.empty() || !SortFunc ) return true;
std::vector< SmartPointer<FileWithName> > filelist;
filelist.resize( filenames.size() );
std::vector< SmartPointer<FileWithName> >::iterator it2 = filelist.begin();
for( Directory::FilenamesType::const_iterator it = filenames.begin();
it != filenames.end() && it2 != filelist.end(); ++it, ++it2)
{
Reader reader;
reader.SetFileName( it->c_str() );
SmartPointer<FileWithName> &f = *it2;
if( reader.Read() )
{
f = new FileWithName( reader.GetFile() );
f->filename = *it;
}
else
{
gdcmErrorMacro( "File could not be read: " << it->c_str() );
return false;
}
}
//std::sort( filelist.begin(), filelist.end(), Sorter::SortFunc);
SortFunctor sf;
sf = Sorter::SortFunc;
std::sort( filelist.begin(), filelist.end(), sf);
for(it2 = filelist.begin(); it2 != filelist.end(); ++it2 )
{
SmartPointer<FileWithName> const & f = *it2;
Filenames.push_back( f->filename );
}
return true;
}
bool Sorter::AddSelect( Tag const &tag, const char *value )
{
Selection.insert( SelectionMap::value_type(tag,value) );
return true;
}
void Sorter::Print( std::ostream &os) const
{
std::vector<std::string>::const_iterator it = Filenames.begin();
for( ; it != Filenames.end(); ++it)
{
os << *it <<std::endl;
}
}
} // end namespace gdcm
|