TPIE

2362a60
internal_sort.h
Go to the documentation of this file.
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2008, The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 
20 #ifndef _INTERNAL_SORT_H
21 #define _INTERNAL_SORT_H
22 
29 
30 // Get definitions for working with Unix and Windows
31 #include <tpie/portability.h>
32 #include <tpie/parallel_sort.h>
34 #include <tpie/memory.h>
35 #include <tpie/tpie_assert.h>
36 #include <tpie/file_stream.h>
37 
38 namespace tpie {
39 namespace ami {
40 
47 template<class Key>
48 class qsort_item {
49 public:
50  Key keyval;
51  TPIE_OS_SIZE_T source;
52 
53  friend int operator==(const qsort_item &x, const qsort_item &y)
54  {return (x.keyval == y.keyval);}
55 
56  friend int operator!=(const qsort_item &x, const qsort_item &y)
57  {return (x.keyval != y.keyval);}
58 
59  friend int operator<=(const qsort_item &x, const qsort_item &y)
60  {return (x.keyval <= y.keyval);}
61 
62  friend int operator>=(const qsort_item &x, const qsort_item &y)
63  {return (x.keyval >= y.keyval);}
64 
65  friend int operator<(const qsort_item &x, const qsort_item &y)
66  {return (x.keyval < y.keyval);}
67 
68  friend int operator>(const qsort_item &x, const qsort_item &y)
69  {return (x.keyval > y.keyval);}
70 };
71 
72 
78 template<class T>
80 protected:
84  TPIE_OS_SIZE_T len;
85 
86 public:
91  // No code in this constructor.
92  };
93 
97  void allocate(TPIE_OS_SIZE_T nItems);
98 
102  void deallocate(void);
103 
108  TPIE_OS_SIZE_T MaxItemCount(TPIE_OS_SIZE_T memSize);
109 
113  TPIE_OS_SIZE_T space_per_item();
114 
119  TPIE_OS_SIZE_T space_overhead();
120 
121 private:
122  // Prohibit these
124  Internal_Sorter_Base<T> operator=(const Internal_Sorter_Base<T>& other);
125 };
126 
127 template<class T>
128 inline void Internal_Sorter_Base<T>::allocate(TPIE_OS_SIZE_T nitems) {
129  len=nitems;
130  ItemArray.resize(len);
131 }
132 
133 template<class T>
135  ItemArray.resize(0);
136  len=0;
137 }
138 
139 template<class T>
140 inline TPIE_OS_SIZE_T Internal_Sorter_Base<T>::MaxItemCount(TPIE_OS_SIZE_T memSize) {
141  //Space available for items
142  TPIE_OS_SIZE_T memAvail=memSize-space_overhead();
143 
144  if (memAvail < space_per_item()) return 0;
145  return memAvail/space_per_item();
146 }
147 
148 
149 template<class T>
150 inline TPIE_OS_SIZE_T Internal_Sorter_Base<T>::space_overhead(void) {
151  // Space usage independent of space_per_item
152  // accounts MM_manager space overhead on "new" call
153  return 0;
154 }
155 
156 template<class T>
157 inline TPIE_OS_SIZE_T Internal_Sorter_Base<T>::space_per_item(void) {
158  return sizeof(T);
159 }
160 
165 template<class T, class Compare>
167 protected:
171  Compare cmp_o;
172 
173 public:
177  Internal_Sorter_Obj(Compare cmp) :cmp_o(cmp) {};
178 
183 
185 
186  //Sort nItems from input stream and write to output stream
187  void sort(file_stream<T>* InStr, file_stream<T>* OutStr, TPIE_OS_SIZE_T nItems,
188  progress_indicator_base * pi=0);
189 
190 private:
191  // Prohibit these
194 };
195 
201 template<class T, class Compare>
203  file_stream<T>* OutStr,
204  TPIE_OS_SIZE_T nItems,
206  tp_assert ( nItems <= len, "Internal buffer overfull (nItems > len)");
207 
208 
209  TPIE_OS_SIZE_T i = 0;
210 
211  //make sure we called allocate earlier
212  if (ItemArray.size() == 0) throw stream_exception("NULL_POINTER");
213 
214  tp_assert ( nItems <= len, "Internal buffer overfull (nItems > len)");
215 
216  fractional_progress fp(pi);
217  fp.id() << __FILE__ << __FUNCTION__ << typeid(T) << typeid(Compare);
218  fractional_subindicator read_progress(fp, "read", TPIE_FSI, nItems, "Reading");
219  fractional_subindicator sort_progress(fp, "sort", TPIE_FSI, nItems, "Sorting");
220  fractional_subindicator write_progress(fp, "write", TPIE_FSI, nItems, "Writing");
221  fp.init();
222 
223  read_progress.init(nItems);
224  // Read a memory load out of the input stream one item at a time,
225  for (i = 0; i < nItems; i++) {
226  ItemArray[i] = InStr->read();
227  read_progress.step();
228  }
229  read_progress.done();
230 
231  //Sort the array.
232  tpie::parallel_sort<true>(ItemArray.begin(), ItemArray.begin()+nItems, sort_progress, cmp_o);
233  if (InStr==OutStr) { //Do the right thing if we are doing 2x sort
234  //Internal sort objects should probably be re-written so that
235  //the interface is cleaner and they don't have to worry about I/O
236  InStr->truncate(0); //delete original items
237  InStr->seek(0); //rewind
238  }
239 
240  write_progress.init(nItems);
241  //Write sorted array to OutStr
242  for (i = 0; i < nItems; i++) {
243  OutStr->write(ItemArray[i]);
244  write_progress.step();
245  }
246  write_progress.done();
247  fp.done();
248 }
249 
250 } // ami namespace
251 } // tpie namespace
252 #endif // _INTERNAL_SORT_H
253 
254 
255 
256 
257 
258 
259 
260 
261 
262 
263 
264 
265 
266 
267 
268 
269 
270 
271 
272 
273 
274 
275 
276 
277 
278 
279 
280 
281 
282 
283 
284 
285 
286 
287 
288 
289 
290 
291 
292 
293 
294 
295 
296 
297 
298 
Defines the tp_assert macro.
Compare cmp_o
Comparison object used for sorting.
Memory management subsystem.
The base class for indicating the progress of some task.
unique_id_type & id()
Return this progress indicator's unique id.
Comparision object based Internal_Sorter_base subclass implementation; uses quick_sort_obj().
The base class for internal sorters.
Definition: internal_sort.h:79
void sort(file_stream< T > *InStr, file_stream< T > *OutStr, TPIE_OS_SIZE_T nItems, progress_indicator_base *pi=0)
Reads nItems sequentially from InStr, starting at the current file position; writes the sorted output...
void truncate(stream_size_type offset)
Truncate to given size.
Definition: stream.h:698
TPIE_OS_SIZE_T MaxItemCount(TPIE_OS_SIZE_T memSize)
Returns maximum number of items that can be sorted using memSize bytes.
#define TPIE_FSI
For use when constructing a fractional subindicator.
const T & read()
Reads next item from stream if can_read() == true.
Definition: stream.h:947
Fractional progress reporter.
This file contains a few deprecated definitions for legacy code.
void deallocate(void)
Clean up internal array ItemArray.
void allocate(TPIE_OS_SIZE_T nItems)
Allocate ItemArray as array that can hold nItems.
void seek(stream_offset_type offset, offset_type whence=beginning)
Precondition: is_open() Precondition: offset == 0.
Definition: stream.h:627
array< T > ItemArray
Array that holds items to be sorted.
Definition: internal_sort.h:82
void done()
Advance the indicator to the end.
void init(stream_size_type range=0)
Initialize progress indicator.
TPIE_OS_SIZE_T space_overhead()
Returns fixed memory usage overhead in bytes per class instantiation.
Simple parallel quick sort implementation with progress tracking.
virtual void init(stream_size_type range)
Initialize progress indicator.
void step(stream_size_type step=1)
Record an increment to the indicator and advance the indicator.
Fractional progress reporting.
virtual void done()
Advance the indicator to the end.
TPIE_OS_SIZE_T space_per_item()
Returns memory usage in bytes per sort item.
Compressed stream.
Definition: predeclare.h:46
A simple class that facilitates doing key sorting followed by in-memory permuting to sort items in-me...
Definition: internal_sort.h:48
#define tp_assert(condition, message)
Definition: tpie_assert.h:48
Internal_Sorter_Base(void)
Constructor.
Definition: internal_sort.h:90
~Internal_Sorter_Obj()
Empty destructor.
TPIE_OS_SIZE_T len
length of ItemArray
Definition: internal_sort.h:84
Subindicator for fractional progress reporting.
Internal_Sorter_Obj(Compare cmp)
Empty constructor.