38 #ifndef AMI_SORT_IMPL_H 39 #define AMI_SORT_IMPL_H 63 size_t &run_size,
size_t &last_run_size,
64 unsigned int &nb_runs) {
71 mm_avail = mm_avail/2;
73 run_size = mm_avail/
sizeof(T);
78 nb_runs = last_run_size = 0;
80 if (strlen % run_size == 0) {
81 nb_runs = strlen/run_size;
82 last_run_size = run_size;
84 nb_runs = strlen/run_size + 1;
85 last_run_size = strlen % run_size;
89 SDEBUG cout <<
"nb_runs=" << nb_runs
90 <<
", run_size=" << run_size
91 <<
", last_run_size=" << last_run_size
100 template<
class T,
class Compare>
102 unsigned int run_size, Compare *cmp) {
104 off_t new_run_size = 0;
107 err = instream->
read_array(data, run_size, &new_run_size);
124 template<
class T,
class Compare>
126 int run_size, Compare *cmp) {
128 unsigned int nblocks, last_block_size, crt_block_size, block_size;
133 if (run_size % block_size == 0) {
134 nblocks = run_size / block_size;
135 last_block_size = block_size;
137 nblocks = run_size / block_size + 1;
138 last_block_size = run_size % block_size;
145 for (
unsigned int i=0; i < nblocks; i++) {
146 crt_block_size = (i == nblocks-1) ? last_block_size: block_size;
147 makeRun_Block(instream, &(data[i*block_size]), crt_block_size, cmp);
148 str =
new MEM_STREAM<T>( &(data[i*block_size]), crt_block_size);
158 T* outdata =
new T [run_size];
159 while (!rheap.empty()) {
160 elt = rheap.extract_min();
189 template<
class T,
class Compare>
193 size_t run_size,last_run_size, crt_run_size;
194 unsigned int nb_runs;
201 SDEBUG cout <<
"runFormation: ";
210 initializeRunFormation(instream, run_size, last_run_size, nb_runs);
218 data =
new T[last_run_size];
220 data =
new T[run_size];
224 for (
size_t i=0; i< nb_runs; i++) {
226 crt_run_size = (i == nb_runs-1) ? last_run_size: run_size;
228 SDEBUG cout <<
"i=" << i <<
": runsize=" << crt_run_size <<
", ";
232 makeRun(instream, data, crt_run_size, cmp);
245 if(crt_run_size > 0) {
264 SDEBUG cout <<
"runFormation: done.\n";
293 template<
class T,
class Compare>
298 size_t mm_avail, blocksize;
299 unsigned int arity, max_arity;
302 assert(streamList && cmp);
304 SDEBUG cout <<
"singleMerge: ";
312 max_arity = mm_avail / blocksize;
314 cerr << __FILE__
":" << __LINE__ <<
": OUT OF MEMORY in singleMerge (going over limit)" << endl;
319 arity = (streamList->
length() < max_arity) ?
320 streamList->
length() : max_arity;
322 SDEBUG cout <<
"arity=" << arity <<
" (max_arity=" <<max_arity<<
")\n";
332 while (!rheap.empty()) {
335 elt = rheap.extract_min();
341 SDEBUG cout <<
"..done\n";
364 template<
class T,
class Compare>
373 SDEBUG cout <<
"multiMerge: " << runList->
length() <<
" runs" << endl;
375 while (runList->
length() > 1) {
378 mergedStr = singleMerge<T,Compare>(runList, cmp);
384 if (runList->length() > 0) {
385 mergedStr->
name(&path);
386 runList->enqueue(path);
AMI_STREAM< T > * singleMerge(queue< char *> *streamList, Compare *cmp)
static AMI_err main_memory_usage(size_t *usage, MM_stream_usage usage_type=MM_STREAM_USAGE_OVERHEAD)
#define STREAM_BUFFER_SIZE
AMI_STREAM< T > * multiMerge(queue< char *> *runList, Compare *cmp)
AMI_err read_array(T *data, off_t len, off_t *lenp=NULL)
AMI_err write_item(const T &elt)
size_t makeRun_Block(AMI_STREAM< T > *instream, T *data, unsigned int run_size, Compare *cmp)
void makeRun(AMI_STREAM< T > *instream, T *&data, int run_size, Compare *cmp)
unsigned int length() const
AMI_err seek(off_t offset)
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
queue< char * > * runFormation(AMI_STREAM< T > *instream, Compare *cmp)
#define assert(condition)
AMI_err write_array(const T *data, off_t len)
void quicksort(T *data, size_t n, CMPR &cmp, size_t min_len=20)
AMI_err name(char **stream_name)
void persist(persistence p)
size_t memory_available()