STXXL  1.4-dev
 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 
27 
28 template <typename ExtIterator, typename StrictWeakOrdering>
29 void stl_in_memory_sort(ExtIterator first, ExtIterator last, StrictWeakOrdering cmp)
30 {
31  typedef typename ExtIterator::block_type block_type;
32 
33  STXXL_VERBOSE("stl_in_memory_sort, range: " << (last - first));
34  first.flush();
35  unsigned_type nblocks = last.bid() - first.bid() + (last.block_offset() ? 1 : 0);
36  simple_vector<block_type> blocks(nblocks);
37  simple_vector<request_ptr> reqs(nblocks);
38  unsigned_type i;
39 
40  for (i = 0; i < nblocks; ++i)
41  reqs[i] = blocks[i].read(*(first.bid() + i));
42 
43  wait_all(reqs.begin(), nblocks);
44 
45  unsigned_type last_block_correction = last.block_offset() ? (block_type::size - last.block_offset()) : 0;
48  sort(make_element_iterator(blocks.begin(), first.block_offset()),
49  make_element_iterator(blocks.begin(), nblocks * block_type::size - last_block_correction),
50  cmp);
51 
52  for (i = 0; i < nblocks; ++i)
53  reqs[i] = blocks[i].write(*(first.bid() + i));
54 
55  wait_all(reqs.begin(), nblocks);
56 }
57 
59 
60 #endif // !STXXL_ALGO_INMEMSORT_HEADER
#define STXXL_VERBOSE(x)
Definition: verbose.h:116
void sort(ExtIterator first, ExtIterator last, StrictWeakOrdering cmp, unsigned_type M)
Sort records comparison-based, see stxxl::sort -- Sorting Comparison-Based.
Definition: sort.h:676
void stl_in_memory_sort(ExtIterator first, ExtIterator last, StrictWeakOrdering cmp)
Definition: inmemsort.h:29
void check_sort_settings()
Definition: parallel.h:82
iterator begin()
return mutable iterator to first element
Definition: simple_vector.h:94
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
element_iterator_traits< BlockType, SizeType >::element_iterator make_element_iterator(BlockType *blocks, SizeType offset)
Definition: adaptor.h:646
Simpler non-growing vector without initialization.
Definition: simple_vector.h:39
void wait_all(RequestIterator reqs_begin, RequestIterator reqs_end)
Collection of functions to track statuses of a number of requests.
choose_int_types< my_pointer_size >::unsigned_type unsigned_type
Definition: types.h:64
#define STXXL_END_NAMESPACE
Definition: namespace.h:17