File: sort.timpl.h

package info (click to toggle)
combblas 2.0.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 190,476 kB
  • sloc: cpp: 55,912; ansic: 25,134; sh: 3,691; makefile: 548; csh: 66; python: 49; perl: 21
file content (401 lines) | stat: -rw-r--r-- 11,734 bytes parent folder | download | duplicates (4)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// The Funnelsort Project  -  Cache oblivious sorting algorithm implementation
// Copyright (C) 2005  Kristoffer Vinther
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA


#include <cassert>
#include <algorithm>
#include <utility>
#include <memory>
#include <iterator>
#include <stdexcept>

namespace iosort
{

namespace base_
{

enum tag_RECURSELIMIT { RECURSELIMIT = 24 };

template<class It, class Pred>
inline void insertion_sort(It begin, It end, Pred comp)
{
	for( It j=begin+1; j<end; j++ )
	{
		typename std::iterator_traits<It>::value_type e = *j;
		for( It i=j; begin<i; *i=e )
			if( comp(e,*(i-1)) )
				*i = *(i-1), --i;
			else
				break;
	}
}

template<typename T, class Pred>
inline T median(const T& a, const T& b, const T& c, Pred comp)
{
	if( comp(a,b) )
		if( comp(b,c) )
			return b;
		else if( comp(a,c) )
			return c;
		else
			return a;
	else
		if( comp(a,c) )
			return a;
		else if( comp(b,c) )
			return c;
		else
			return b;
}

template<class BidIt, class T, class Pred>
inline BidIt partition(BidIt begin, BidIt end, T pivot, Pred comp)
{
	for( ;; )
	{
		for( ; comp(*begin,pivot); ++begin );
		for( --end; comp(pivot,*end); --end );
		if( !(begin<end) )
			return begin;
		std::iter_swap(begin, end);
		++begin;
	}
}

template<class RanIt, class Pred>
inline void quicksort(RanIt First, RanIt Last, Pred comp)
{
	typename std::iterator_traits<RanIt>::difference_type Count = Last-First;
	while( RECURSELIMIT < Count )
	{
		RanIt part = partition(First, Last, median(*First,*(First+(Last-First)/2),*(Last-1),comp),comp);
		if( part-First < Last-part ) // loop on larger half
			quicksort(First, part, comp), First = part;
		else
			quicksort(part, Last, comp), Last = part;
		Count = Last-First;
	}
	if( Count > 1 )
        insertion_sort(First, Last, comp);
}

template<class Int>
inline Int logc(Int k)
{
	height_t h = 0;
	for( order_t i=k-1; i; h++, i/=2 );
	return h;
}

template<class RanIt, class Pred>
void batcher_sort(RanIt begin, RanIt end, Pred comp)
{
	typedef typename std::iterator_traits<RanIt>::difference_type ItDiff;
	ItDiff t = logc(end-begin)-1;
	ItDiff p = 1<<t;
	for( ; p!=1; p=p/2 )
	{
		RanIt i, j;
		for( i=begin; i<end-p-p; i+=p )
		{
			j = i+p;
			for( ; i<j; i++ )
				if( comp(*(i+p),*i) )
					std::iter_swap(i+p, i);
		}
		for( ; i<end-p; i++ )
			if( comp(*(i+p),*i) )
				std::iter_swap(i+p, i);
		ItDiff q = 1<<t;
		while( q != p )
		{
			ItDiff d=q-p;
			q = q/2;
			for( i=begin+p; i<end-d-p; i+=p )
			{
				j = i+p;
				for( ; i<j; i++ )
					if( comp(*(i+d),*i) )
						std::iter_swap(i+d, i);
			}
			for( ; i<end-d; i++ )
				if( comp(*(i+d),*i) )
					std::iter_swap(i+d, i);
		}
	}
	if( p )
	{
		for( RanIt i=begin; i<end-1; i+=2 )
			if( comp(*(i+1),*i) )
				std::iter_swap(i+1, i);
		ItDiff q = 1<<t;
		while( q != 1 )
		{
			ItDiff d=q-1;
			q = q/2;
			for( RanIt i=begin+1; i<end-d; i+=2 )
				if( comp(*(i+d),*i) )
					std::iter_swap(i+d, i);
		}
	}
}

template<class RanIt, class Pred>
void inplace_base_sort(RanIt begin, RanIt end, Pred comp)
{
#ifdef __ia64__
	batcher_sort(begin, end, comp);
#else // __ia64__
	quicksort(begin, end, comp);
#endif // __ia64__
}

} // namespace base_

template<class RanIt, class Splitter>
class stream_slices
{
	typedef typename std::iterator_traits<RanIt>::difference_type diff;
public:
	class iterator : public std::iterator<std::bidirectional_iterator_tag, std::pair<RanIt,RanIt>, diff>
	{
		friend class stream_slices;
	public:
		inline iterator& operator++()
		{
			begin = end;
			end = end+run_size;
			if( --big_runs == 0 )
				--run_size;
			return *this;
		}
		inline iterator& operator--()
		{
			end = begin;
			begin = end-run_size;
			if( ++big_runs == 0 )
				++run_size;
			return *this;
		}
		std::pair<RanIt,RanIt> operator*()
			{ return make_pair(begin,end); }
		bool operator==(const iterator& rhs)
			{ return begin == rhs.begin; }
	private:
		inline iterator(RanIt begin, diff run_size, size_t big_runs) : begin(begin), run_size(run_size), big_runs(big_runs)
		{
			if( big_runs )
				++run_size;
			end = begin+run_size;
		}
		RanIt begin, end;
		diff run_size;
		size_t big_runs;
	};
	stream_slices(RanIt begin, RanIt end) : b(begin), e(end), order_(Splitter::prefered_order_output(end-begin))
	{
		if( order_ < 2 )
			throw std::logic_error("Splitter::prefered_order_output returned less than two");
		run_size = (end-begin)/order_;
		size_t rem = static_cast<size_t>((end-begin) % order_);
		run_size += rem/order_;
		big_runs = rem % order_;
	}
	inline diff order() const
		{ return order_; }
	inline iterator begin()
		{ return iterator(b, run_size, big_runs); }
	inline iterator end()
		{ return iterator(e, run_size, big_runs); }
private:
	RanIt b, e;
	diff run_size;
	size_t order_, big_runs;
};

template<class Splitter, class Diff>
inline Diff run_size_(Diff d)
{
	size_t order = Splitter::prefered_order_output(d);
	Diff run_size = d/order;
	size_t rem = static_cast<size_t>(d % order);
	run_size += rem/order;
	size_t big_runs = rem % order;
	if( big_runs )
		run_size++;
	return run_size;
}

template<class Merger, class Splitter, class Diff, class MAlloc, class TAlloc>
inline std::pair<Merger*,Merger*> build_cache_(Diff run_size, MAlloc& malloc, TAlloc& talloc)
{
	Merger *begin_cache, *end_cache;
	int rec_calls = 0;
	for( Diff d=run_size; d>static_cast<Diff>(Splitter::switch_to_cache_aware()); d=run_size_<Splitter>(d), ++rec_calls );
	begin_cache = end_cache = malloc.allocate(rec_calls);
	for( Diff d=run_size; d>static_cast<Diff>(Splitter::switch_to_cache_aware()); d=run_size_<Splitter>(d), ++end_cache )
		new(end_cache) Merger(Splitter::prefered_order_output(d), talloc);
	return make_pair(begin_cache,end_cache);
}

template<class Merger, class Splitter, class It, class OutIt, class Alloc>
void merge_sort_(It begin, It end, OutIt dest, Merger* cache, Alloc alloc);

template<class Merger, class Splitter, class MPtr, class It, class Alloc>
inline void sort_streams_(MPtr cache, It begin, size_t order, size_t run_size, size_t big_runs, Alloc alloc)
{
	typedef typename std::iterator_traits<It>::value_type T;
	typename Alloc::template rebind<T>::other talloc(alloc);
	typedef typename Alloc::template rebind<T>::other::pointer TPtr;
	TPtr tmp = talloc.allocate((size_t)(run_size));
	It last = begin;
	size_t i;
	if( big_runs )
	{
		merge_sort_<Merger,Splitter>(last, last+run_size, std::raw_storage_iterator<TPtr,T>(tmp), cache, alloc);
		for( i=1, last+=run_size; i!=big_runs; i++, last+=run_size )
			merge_sort_<Merger,Splitter>(last, last+run_size, last-run_size, cache, alloc);
		run_size--;
		for( ; i!=order; i++, last+=run_size )
			merge_sort_<Merger,Splitter>(last, last+run_size, last-run_size-1, cache, alloc);
		run_size++;
	}
	else
	{
		merge_sort_<Merger,Splitter>(last, last+run_size, tmp, cache, alloc);
		for( --order, last+=run_size; order; --order, last+=run_size )
			merge_sort_<Merger,Splitter>(last, last+run_size, last-run_size, cache, alloc);
	}
	std::copy(tmp, tmp+run_size, last-run_size);
	for( TPtr p=tmp+run_size-1; p>=tmp; --p )
		talloc.destroy(p);
	talloc.deallocate(tmp,static_cast<size_t>(run_size));
}

template<class MPtr, class It>
inline void add_sorted_streams_(MPtr merger, It end, size_t order, size_t run_size, size_t big_runs)
{
	if( big_runs )
	{
		merger->add_stream(end-run_size, end);
		end -= run_size;
		--run_size, --big_runs;
		for( --order; order!=big_runs; --order, end-=run_size )
			merger->add_stream(end-run_size, end);
		++run_size;
		for( ; order; --order, end-=run_size )
			merger->add_stream(end-run_size, end);
	}
	else
		for( ; order; --order, end-=run_size )
			merger->add_stream(end-run_size, end);
}

template<class MPtr, class It, class Pred>
inline void add_streams_(MPtr merger, It begin, size_t order, size_t run_size, size_t big_runs, Pred comp)
{
	size_t i;
	for( i=0; i!=order-big_runs; ++i, begin+=run_size )
		base_::inplace_base_sort(begin, begin+run_size, comp);
	++run_size;
	for( ; i!=order; ++i, begin+=run_size )
		base_::inplace_base_sort(begin, begin+run_size, comp);

	for( ; i!=order-big_runs; --i, begin-=run_size )
		merger->add_stream(begin-run_size, begin);
	--run_size;
	for( ; i; --i, begin-=run_size )
		merger->add_stream(begin-run_size, begin);
}

template<class Merger, class Splitter, class It, class OutIt, class Alloc>
void merge_sort_(It begin, It end, OutIt dest, Merger* cache, Alloc alloc)
{
	typedef typename std::iterator_traits<It>::value_type T;
	typedef typename std::iterator_traits<It>::difference_type ItDiff;
	typedef typename Merger::stream Stream;

	order_t order = Splitter::prefered_order_output(end-begin);
	if( order < 2 )
		throw std::logic_error("Splitter::prefered_order_output returned less than two");
	ItDiff run_size = (end-begin)/order;
	size_t rem = (size_t)((end-begin) % order);
	run_size += rem/order;
	size_t big_runs = rem % order;

	cache->reset();
	if( run_size > static_cast<ItDiff>(Splitter::switch_to_cache_aware()) )
	{
		if( big_runs )
			run_size++;
		sort_streams_<Merger,Splitter>(cache+1,begin,order,run_size,big_runs,alloc);
		add_sorted_streams_(cache,end,order,run_size,big_runs);
	}
	else
		add_streams_(cache,begin,order,run_size,big_runs, typename Merger::predicate());
	(*cache)(dest);
}

template<class Merger, class Splitter, class It, class OutIt>
inline void merge_sort(It begin, It end, OutIt dest, const typename Merger::allocator& alloc)
{
	typedef typename std::iterator_traits<It>::difference_type ItDiff;
	if( end-begin > static_cast<ItDiff>(Splitter::switch_to_cache_aware()) )
	{
		order_t order = Splitter::prefered_order_output(end-begin);
		if( order < 2 )
			throw std::logic_error("Splitter::prefered_order_output returned less than two");
		ItDiff run_size = (end-begin)/order;
		size_t rem = (size_t)((end-begin) % order);
		run_size += rem/order;
		size_t big_runs = rem % order;
		if( run_size > static_cast<ItDiff>(Splitter::switch_to_cache_aware()) )
		{
			if( big_runs )
				run_size++;
			typename Merger::allocator::template rebind<Merger>::other malloc(alloc);
			std::pair<Merger*,Merger*> cache = build_cache_<Merger,Splitter>(run_size, malloc, alloc);
			sort_streams_<Merger,Splitter>(cache.first, begin, order, run_size, big_runs, alloc);
			for( Merger *pm=cache.second-1; pm>=cache.first; --pm )
				malloc.destroy(pm);
			malloc.deallocate(cache.first, cache.second-cache.first);
		}
		Merger merger(order,alloc);
		if( run_size > static_cast<ItDiff>(Splitter::switch_to_cache_aware()) )
			add_sorted_streams_(&merger, end, order, run_size, big_runs);
		else
			add_streams_(&merger, begin, order, run_size, big_runs, typename Merger::predicate());
#ifdef _DEBUG
		OutIt e = merger(dest, dest+(end-begin));
		merger.empty(dest, dest+(end-begin));
		size_t N = 0;
		for( typename Merger::stream_iterator i=merger.begin(); i!=merger.end(); ++i )
			N += (*i).end()-(*i).begin();
		assert( e == dest+(end-begin) );
#else // _DEBUG
		merger(dest);
#endif // _DEBUG
	}
	else
	{
		base_::inplace_base_sort(begin, end, typename Merger::predicate());
		std::copy(begin, end, dest);
	}
}
} // namespace iosort