36 #ifndef AMI_SORT_IMPL_H
37 #define AMI_SORT_IMPL_H
57 static void initializeRunFormation(
AMI_STREAM<T> *instream,
size_t &run_size,
58 size_t &last_run_size,
unsigned int &nb_runs)
66 mm_avail = mm_avail / 2;
68 run_size = mm_avail /
sizeof(T);
72 nb_runs = last_run_size = 0;
75 if (strlen % run_size == 0) {
76 nb_runs = strlen / run_size;
77 last_run_size = run_size;
80 nb_runs = strlen / run_size + 1;
81 last_run_size = strlen % run_size;
85 SDEBUG cout <<
"nb_runs=" << nb_runs <<
", run_size=" << run_size
86 <<
", last_run_size=" << last_run_size <<
"\n";
92 template <
class T,
class Compare>
97 off_t new_run_size = 0;
116 template <
class T,
class Compare>
120 unsigned int nblocks, last_block_size, crt_block_size, block_size;
124 if (run_size % block_size == 0) {
125 nblocks = run_size / block_size;
126 last_block_size = block_size;
129 nblocks = run_size / block_size + 1;
130 last_block_size = run_size % block_size;
137 for (
unsigned int i = 0; i < nblocks; i++) {
138 crt_block_size = (i == nblocks - 1) ? last_block_size : block_size;
139 makeRun_Block(instream, &(data[i * block_size]), crt_block_size, cmp);
140 str =
new MEM_STREAM<T>(&(data[i * block_size]), crt_block_size);
150 T *outdata =
new T[run_size];
151 while (!rheap.
empty()) {
179 template <
class T,
class Compare>
183 size_t run_size, last_run_size, crt_run_size;
184 unsigned int nb_runs;
191 SDEBUG cout <<
"runFormation: ";
200 initializeRunFormation(instream, run_size, last_run_size, nb_runs);
208 data =
new T[last_run_size];
211 data =
new T[run_size];
215 for (
size_t i = 0; i < nb_runs; i++) {
217 crt_run_size = (i == nb_runs - 1) ? last_run_size : run_size;
219 SDEBUG cout <<
"i=" << i <<
": runsize=" << crt_run_size <<
", ";
223 makeRun(instream, data, crt_run_size, cmp);
236 if (crt_run_size > 0) {
254 SDEBUG cout <<
"runFormation: done.\n";
277 template <
class T,
class Compare>
281 size_t mm_avail, blocksize;
282 unsigned int arity, max_arity;
285 assert(streamList && cmp);
287 SDEBUG cout <<
"singleMerge: ";
295 max_arity = mm_avail / blocksize;
297 cerr << __FILE__
":" << __LINE__
298 <<
": OUT OF MEMORY in singleMerge (going over limit)" << endl;
305 (streamList->
length() < max_arity) ? streamList->
length() : max_arity;
307 SDEBUG cout <<
"arity=" << arity <<
" (max_arity=" << max_arity <<
")\n";
316 while (!rheap.
empty()) {
324 SDEBUG cout <<
"..done\n";
343 template <
class T,
class Compare>
351 SDEBUG cout <<
"multiMerge: " << runList->
length() <<
" runs" << endl;
353 while (runList->
length() > 1) {
356 mergedStr = singleMerge<T, Compare>(runList, cmp);
362 if (runList->
length() > 0) {
AMI_STREAM< T > * singleMerge(queue< char * > *streamList, Compare *cmp)
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)
queue< char * > * runFormation(AMI_STREAM< T > *instream, Compare *cmp)
AMI_STREAM< T > * multiMerge(queue< char * > *runList, Compare *cmp)
#define STREAM_BUFFER_SIZE
@ AMI_ERROR_END_OF_STREAM
static AMI_err main_memory_usage(size_t *usage, MM_stream_usage usage_type=MM_STREAM_USAGE_OVERHEAD)
AMI_err write_item(const T &elt)
AMI_err write_array(const T *data, off_t len)
AMI_err name(char **stream_name)
AMI_err read_array(T *data, off_t len, off_t *lenp=NULL)
AMI_err seek(off_t offset)
void persist(persistence p)
size_t memory_available()
ostream & print(ostream &s) const
ostream & print(ostream &s) const
unsigned int length() const
#define assert(condition)
@ MM_STREAM_USAGE_MAXIMUM
void quicksort(T *data, size_t n, CMPR &cmp, size_t min_len=20)
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)