Stxxl
1.4.0
|
00001 /*************************************************************************** 00002 * include/stxxl/bits/stream/sorted_runs.h 00003 * 00004 * Part of the STXXL. See http://stxxl.sourceforge.net 00005 * 00006 * Copyright (C) 2002-2005 Roman Dementiev <dementiev@mpi-sb.mpg.de> 00007 * Copyright (C) 2009, 2010 Andreas Beckmann <beckmann@cs.uni-frankfurt.de> 00008 * 00009 * Distributed under the Boost Software License, Version 1.0. 00010 * (See accompanying file LICENSE_1_0.txt or copy at 00011 * http://www.boost.org/LICENSE_1_0.txt) 00012 **************************************************************************/ 00013 00014 #ifndef STXXL_STREAM__SORTED_RUNS_H 00015 #define STXXL_STREAM__SORTED_RUNS_H 00016 00017 #include <vector> 00018 #include <stxxl/bits/mng/mng.h> 00019 #include <stxxl/bits/mng/typed_block.h> 00020 #include <stxxl/bits/algo/adaptor.h> 00021 00022 00023 __STXXL_BEGIN_NAMESPACE 00024 00025 namespace stream 00026 { 00027 //! \addtogroup streampack Stream package 00028 //! \{ 00029 00030 00031 //////////////////////////////////////////////////////////////////////// 00032 // SORTED RUNS // 00033 //////////////////////////////////////////////////////////////////////// 00034 00035 //! \brief All sorted runs of a sort operation. 00036 template <typename TriggerEntryType, typename CompareType_> 00037 struct sorted_runs : private noncopyable 00038 { 00039 typedef TriggerEntryType trigger_entry_type; 00040 typedef typename trigger_entry_type::block_type block_type; 00041 typedef typename block_type::value_type value_type; // may differ from trigger_entry_type::value_type 00042 typedef std::vector<trigger_entry_type> run_type; 00043 typedef std::vector<value_type> small_run_type; 00044 typedef stxxl::external_size_type size_type; 00045 typedef typename std::vector<run_type>::size_type run_index_type; 00046 00047 typedef CompareType_ cmp_type; 00048 00049 /// total number of elements in all runs 00050 size_type elements; 00051 00052 /// vector of runs (containing vectors of block ids) 00053 std::vector<run_type> runs; 00054 00055 /// vector of the number of elements in each individual run 00056 std::vector<size_type> runs_sizes; 00057 00058 //! \brief Small sort optimization: 00059 // if the input is small such that its total size is at most B 00060 // (block_type::size) then input is sorted internally and kept in the 00061 // array "small_run" 00062 small_run_type small_run; 00063 00064 /// reference counter used by counting_ptr<> 00065 size_type refs; 00066 00067 public: 00068 sorted_runs() 00069 : elements(0), refs(0) 00070 { } 00071 00072 ~sorted_runs() 00073 { 00074 deallocate_blocks(); 00075 } 00076 00077 /// Increment internal reference counter 00078 void inc_ref() 00079 { 00080 ++refs; 00081 } 00082 00083 /// Decrement internal reference counter. If it hits zero, the object is freed. 00084 void dec_ref() 00085 { 00086 if (--refs == 0) delete this; 00087 } 00088 00089 /// Clear the internal state of the object: release all runs and reset. 00090 void clear() 00091 { 00092 deallocate_blocks(); 00093 00094 elements = 0; 00095 runs.clear(); 00096 runs_sizes.clear(); 00097 small_run.clear(); 00098 } 00099 00100 /// Add a new run with given number of elements 00101 void add_run(const run_type& run, size_type run_size) 00102 { 00103 runs.push_back(run); 00104 runs_sizes.push_back(run_size); 00105 elements += run_size; 00106 } 00107 00108 /// Swap contents with another object. This is used by the recursive 00109 /// merger to swap in a sorted_runs object with fewer runs. 00110 void swap(sorted_runs& b) 00111 { 00112 std::swap(elements, b.elements); 00113 std::swap(runs, b.runs); 00114 std::swap(runs_sizes, b.runs_sizes); 00115 std::swap(small_run, b.small_run); 00116 } 00117 00118 private: 00119 //! \brief Deallocates the blocks which the runs occupy 00120 //! 00121 //! \remark There is no need in calling this method, the blocks are 00122 //! deallocated by the destructor. However, if you wish to reuse the 00123 //! object, then this function can be used to clear its state. 00124 void deallocate_blocks() 00125 { 00126 block_manager * bm = block_manager::get_instance(); 00127 for (unsigned_type i = 0; i < runs.size(); ++i) 00128 { 00129 bm->delete_blocks(make_bid_iterator(runs[i].begin()), 00130 make_bid_iterator(runs[i].end())); 00131 } 00132 } 00133 }; 00134 00135 00136 //! \} 00137 } 00138 00139 __STXXL_END_NAMESPACE 00140 00141 #endif // !STXXL_STREAM__SORTED_RUNS_H 00142 // vim: et:ts=4:sw=4