STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
inmemsort.h
Go to the documentation of this file.
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_ALGO_INMEMSORT_HEADER
15 #define STXXL_ALGO_INMEMSORT_HEADER
16 
17 #include <stxxl/bits/namespace.h>
21 #include <stxxl/bits/mng/adaptor.h>
22 #include <stxxl/bits/parallel.h>
23 
24 #include <algorithm>
25 
26 
28 
29 template <typename ExtIterator_, typename StrictWeakOrdering_>
30 void stl_in_memory_sort(ExtIterator_ first, ExtIterator_ last, StrictWeakOrdering_ cmp)
31 {
32  typedef typename ExtIterator_::block_type block_type;
33 
34  STXXL_VERBOSE("stl_in_memory_sort, range: " << (last - first));
35  first.flush();
36  unsigned_type nblocks = last.bid() - first.bid() + (last.block_offset() ? 1 : 0);
37  simple_vector<block_type> blocks(nblocks);
38  simple_vector<request_ptr> reqs(nblocks);
39  unsigned_type i;
40 
41  for (i = 0; i < nblocks; ++i)
42  reqs[i] = blocks[i].read(*(first.bid() + i));
43 
44 
45  wait_all(reqs.begin(), nblocks);
46 
47  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 
62 
63 #endif // !STXXL_ALGO_INMEMSORT_HEADER
#define STXXL_VERBOSE(x)
Definition: verbose.h:102
void stl_in_memory_sort(ExtIterator_ first, ExtIterator_ last, StrictWeakOrdering_ cmp)
Definition: inmemsort.h:30
void sort(ExtIterator first, ExtIterator last, StrictWeakOrdering cmp, unsigned_type M)
Sort records comparison-based, see stxxl::sort -- Sorting Comparison-Based.
Definition: sort.h:673
void check_sort_settings()
Definition: parallel.h:98
element_iterator_traits< BlockType >::element_iterator make_element_iterator(BlockType *blocks, unsigned_type offset)
Definition: adaptor.h:631
void wait_all(request_iterator_ reqs_begin, request_iterator_ reqs_end)
Collection of functions to track statuses of a number of requests.
iterator begin()
return mutable iterator to first element
Definition: simple_vector.h:92
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
Simpler non-growing vector without initialization.
Definition: simple_vector.h:37
choose_int_types< my_pointer_size >::unsigned_type unsigned_type
Definition: types.h:67
#define STXXL_END_NAMESPACE
Definition: namespace.h:17