STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sorted_runs.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/stream/sorted_runs.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002-2005 Roman Dementiev <[email protected]>
7  * Copyright (C) 2009, 2010 Andreas Beckmann <[email protected]>
8  * Copyright (C) 2013 Timo Bingmann <[email protected]>
9  *
10  * Distributed under the Boost Software License, Version 1.0.
11  * (See accompanying file LICENSE_1_0.txt or copy at
12  * http://www.boost.org/LICENSE_1_0.txt)
13  **************************************************************************/
14 
15 #ifndef STXXL_STREAM_SORTED_RUNS_HEADER
16 #define STXXL_STREAM_SORTED_RUNS_HEADER
17 
18 #include <vector>
23 
24 
26 
27 namespace stream {
28 
29 //! \addtogroup streampack Stream Package
30 //! \{
31 
32 
33 ////////////////////////////////////////////////////////////////////////
34 // SORTED RUNS //
35 ////////////////////////////////////////////////////////////////////////
36 
37 //! All sorted runs of a sort operation.
38 template <typename TriggerEntryType, typename CompareType>
39 struct sorted_runs : private noncopyable, public counted_object
40 {
41  typedef TriggerEntryType trigger_entry_type;
42  typedef typename trigger_entry_type::block_type block_type;
43  typedef typename block_type::value_type value_type; // may differ from trigger_entry_type::value_type
44  typedef std::vector<trigger_entry_type> run_type;
45  typedef std::vector<value_type> small_run_type;
47  typedef typename std::vector<run_type>::size_type run_index_type;
48 
49  typedef CompareType cmp_type;
50 
51  //! total number of elements in all runs
53 
54  //! vector of runs (containing vectors of block ids)
55  std::vector<run_type> runs;
56 
57  //! vector of the number of elements in each individual run
58  std::vector<size_type> runs_sizes;
59 
60  //! Small sort optimization:
61  // if the input is small such that its total size is at most B
62  // (block_type::size) then input is sorted internally and kept in the
63  // array "small_run"
65 
66 public:
68  : elements(0)
69  { }
70 
72  {
73  deallocate_blocks();
74  }
75 
76  //! Clear the internal state of the object: release all runs and reset.
77  void clear()
78  {
79  deallocate_blocks();
80 
81  elements = 0;
82  runs.clear();
83  runs_sizes.clear();
84  small_run.clear();
85  }
86 
87  //! Add a new run with given number of elements
88  void add_run(const run_type& run, size_type run_size)
89  {
90  runs.push_back(run);
91  runs_sizes.push_back(run_size);
92  elements += run_size;
93  }
94 
95  //! Swap contents with another object. This is used by the recursive
96  //! merger to swap in a sorted_runs object with fewer runs.
97  void swap(sorted_runs& b)
98  {
99  std::swap(elements, b.elements);
100  std::swap(runs, b.runs);
101  std::swap(runs_sizes, b.runs_sizes);
102  std::swap(small_run, b.small_run);
103  }
104 
105 private:
106  //! Deallocates the blocks which the runs occupy.
107  //!
108  //! \remark There is no need in calling this method, the blocks are
109  //! deallocated by the destructor. However, if you wish to reuse the
110  //! object, then this function can be used to clear its state.
112  {
113  block_manager* bm = block_manager::get_instance();
114  for (unsigned_type i = 0; i < runs.size(); ++i)
115  {
116  bm->delete_blocks(make_bid_iterator(runs[i].begin()),
117  make_bid_iterator(runs[i].end()));
118  }
119  }
120 };
121 
122 
123 //! \}
124 
125 } // namespace stream
126 
128 
129 #endif // !STXXL_STREAM_SORTED_RUNS_HEADER
130 // vim: et:ts=4:sw=4
void swap(sorted_runs &b)
Swap contents with another object. This is used by the recursive merger to swap in a sorted_runs obje...
Definition: sorted_runs.h:97
Block manager class.
Definition: block_manager.h:63
trigger_entry_iterator< Iterator > make_bid_iterator(Iterator iter)
Definition: adaptor.h:241
All sorted runs of a sort operation.
Definition: sorted_runs.h:39
std::vector< run_type > runs
vector of runs (containing vectors of block ids)
Definition: sorted_runs.h:55
void deallocate_blocks()
Deallocates the blocks which the runs occupy.
Definition: sorted_runs.h:111
small_run_type small_run
Small sort optimization:
Definition: sorted_runs.h:64
stxxl::external_size_type size_type
Definition: sorted_runs.h:46
void delete_blocks(const BIDIteratorClass &bidbegin, const BIDIteratorClass &bidend)
Deallocates blocks.
void clear()
Clear the internal state of the object: release all runs and reset.
Definition: sorted_runs.h:77
Provides reference counting abilities for use with counting_ptr.
Definition: counting_ptr.h:327
trigger_entry_type::block_type block_type
Definition: sorted_runs.h:42
std::vector< size_type > runs_sizes
vector of the number of elements in each individual run
Definition: sorted_runs.h:58
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
std::vector< run_type >::size_type run_index_type
Definition: sorted_runs.h:47
void add_run(const run_type &run, size_type run_size)
Add a new run with given number of elements.
Definition: sorted_runs.h:88
block_type::value_type value_type
Definition: sorted_runs.h:43
std::vector< value_type > small_run_type
Definition: sorted_runs.h:45
uint64 external_size_type
Definition: types.h:70
choose_int_types< my_pointer_size >::unsigned_type unsigned_type
Definition: types.h:67
TriggerEntryType trigger_entry_type
Definition: sorted_runs.h:41
std::vector< trigger_entry_type > run_type
Definition: sorted_runs.h:44
size_type elements
total number of elements in all runs
Definition: sorted_runs.h:52
#define STXXL_END_NAMESPACE
Definition: namespace.h:17