Stxxl  1.3.2
inmemsort.h
1 /***************************************************************************
2  * include/stxxl/bits/algo/inmemsort.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2003 Roman Dementiev <[email protected]>
7  * Copyright (C) 2010 Andreas Beckmann <[email protected]>
8  *
9  * Distributed under the Boost Software License, Version 1.0.
10  * (See accompanying file LICENSE_1_0.txt or copy at
11  * http://www.boost.org/LICENSE_1_0.txt)
12  **************************************************************************/
13 
14 #ifndef STXXL_IN_MEMORY_SORT_HEADER
15 #define STXXL_IN_MEMORY_SORT_HEADER
16 
17 #include <stxxl/bits/namespace.h>
18 #include <stxxl/bits/common/simple_vector.h>
19 #include <stxxl/bits/io/request_operations.h>
20 #include <stxxl/bits/algo/adaptor.h>
21 #include <stxxl/bits/mng/adaptor.h>
22 #include <stxxl/bits/parallel.h>
23 
24 #include <algorithm>
25 
26 
27 __STXXL_BEGIN_NAMESPACE
28 
29 template <typename ExtIterator_, typename StrictWeakOrdering_>
30 void stl_in_memory_sort(ExtIterator_ first, ExtIterator_ last, StrictWeakOrdering_ cmp)
31 {
32  typedef typename ExtIterator_::vector_type::value_type value_type;
33  typedef typename ExtIterator_::block_type block_type;
34 
35  STXXL_VERBOSE("stl_in_memory_sort, range: " << (last - first));
36  first.flush();
37  unsigned_type nblocks = last.bid() - first.bid() + (last.block_offset() ? 1 : 0);
38  simple_vector<block_type> blocks(nblocks);
39  simple_vector<request_ptr> reqs(nblocks);
40  unsigned_type i;
41 
42  for (i = 0; i < nblocks; ++i)
43  reqs[i] = blocks[i].read(*(first.bid() + i));
44 
45 
46  wait_all(reqs.begin(), nblocks);
47 
48  unsigned_type last_block_correction = last.block_offset() ? (block_type::size - last.block_offset()) : 0;
50  sort(make_element_iterator(blocks.begin(), first.block_offset()),
51  make_element_iterator(blocks.begin(), nblocks * block_type::size - last_block_correction),
52  cmp);
53 
54  for (i = 0; i < nblocks; ++i)
55  reqs[i] = blocks[i].write(*(first.bid() + i));
56 
57  wait_all(reqs.begin(), nblocks);
58 }
59 
60 
61 __STXXL_END_NAMESPACE
62 
63 #endif // !STXXL_IN_MEMORY_SORT_HEADER
void sort(ExtIterator_ first, ExtIterator_ last, StrictWeakOrdering_ cmp, unsigned_type M)
Sort records comparison-based.
Definition: sort.h:700
void wait_all(request_iterator_ reqs_begin, request_iterator_ reqs_end)
Collection of functions to track statuses of a number of requests.
Definition: request_operations.h:36