TPIE

v1.1rc1-6-g0c97303
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 
37 namespace tpie {
38 namespace ami {
39 
46 template<class Key>
47 class qsort_item {
48 public:
49  Key keyval;
50  TPIE_OS_SIZE_T source;
51 
52  friend int operator==(const qsort_item &x, const qsort_item &y)
53  {return (x.keyval == y.keyval);}
54 
55  friend int operator!=(const qsort_item &x, const qsort_item &y)
56  {return (x.keyval != y.keyval);}
57 
58  friend int operator<=(const qsort_item &x, const qsort_item &y)
59  {return (x.keyval <= y.keyval);}
60 
61  friend int operator>=(const qsort_item &x, const qsort_item &y)
62  {return (x.keyval >= y.keyval);}
63 
64  friend int operator<(const qsort_item &x, const qsort_item &y)
65  {return (x.keyval < y.keyval);}
66 
67  friend int operator>(const qsort_item &x, const qsort_item &y)
68  {return (x.keyval > y.keyval);}
69 };
70 
71 
77 template<class T>
79 protected:
83  TPIE_OS_SIZE_T len;
84 
85 public:
90  // No code in this constructor.
91  };
92 
96  void allocate(TPIE_OS_SIZE_T nItems);
97 
101  void deallocate(void);
102 
107  TPIE_OS_SIZE_T MaxItemCount(TPIE_OS_SIZE_T memSize);
108 
112  TPIE_OS_SIZE_T space_per_item();
113 
118  TPIE_OS_SIZE_T space_overhead();
119 
120 private:
121  // Prohibit these
123  Internal_Sorter_Base<T> operator=(const Internal_Sorter_Base<T>& other);
124 };
125 
126 template<class T>
127 inline void Internal_Sorter_Base<T>::allocate(TPIE_OS_SIZE_T nitems) {
128  len=nitems;
129  ItemArray.resize(len);
130 }
131 
132 template<class T>
134  ItemArray.resize(0);
135  len=0;
136 }
137 
138 template<class T>
139 inline TPIE_OS_SIZE_T Internal_Sorter_Base<T>::MaxItemCount(TPIE_OS_SIZE_T memSize) {
140  //Space available for items
141  TPIE_OS_SIZE_T memAvail=memSize-space_overhead();
142 
143  if (memAvail < space_per_item()) return 0;
144  return memAvail/space_per_item();
145 }
146 
147 
148 template<class T>
149 inline TPIE_OS_SIZE_T Internal_Sorter_Base<T>::space_overhead(void) {
150  // Space usage independent of space_per_item
151  // accounts MM_manager space overhead on "new" call
152  return 0;
153 }
154 
155 template<class T>
156 inline TPIE_OS_SIZE_T Internal_Sorter_Base<T>::space_per_item(void) {
157  return sizeof(T);
158 }
159 
164 template<class T, class Compare>
166 protected:
170  Compare cmp_o;
171 
172 public:
176  Internal_Sorter_Obj(Compare cmp) :cmp_o(cmp) {};
177 
182 
184 
185  //Sort nItems from input stream and write to output stream
186  void sort(file_stream<T>* InStr, file_stream<T>* OutStr, TPIE_OS_SIZE_T nItems,
187  progress_indicator_base * pi=0);
188 
189 private:
190  // Prohibit these
193 };
194 
200 template<class T, class Compare>
202  file_stream<T>* OutStr,
203  TPIE_OS_SIZE_T nItems,
205  tp_assert ( nItems <= len, "Internal buffer overfull (nItems > len)");
206 
207 
208  TPIE_OS_SIZE_T i = 0;
209 
210  //make sure we called allocate earlier
211  if (ItemArray.size() == 0) throw stream_exception("NULL_POINTER");
212 
213  tp_assert ( nItems <= len, "Internal buffer overfull (nItems > len)");
214 
215  fractional_progress fp(pi);
216  fp.id() << __FILE__ << __FUNCTION__ << typeid(T) << typeid(Compare);
217  fractional_subindicator read_progress(fp, "read", TPIE_FSI, nItems, "Reading");
218  fractional_subindicator sort_progress(fp, "sort", TPIE_FSI, nItems, "Sorting");
219  fractional_subindicator write_progress(fp, "write", TPIE_FSI, nItems, "Writing");
220  fp.init();
221 
222  read_progress.init(nItems);
223  // Read a memory load out of the input stream one item at a time,
224  for (i = 0; i < nItems; i++) {
225  ItemArray[i] = InStr->read();
226  read_progress.step();
227  }
228  read_progress.done();
229 
230  //Sort the array.
231  tpie::parallel_sort<true>(ItemArray.begin(), ItemArray.begin()+nItems, sort_progress, cmp_o);
232  if (InStr==OutStr) { //Do the right thing if we are doing 2x sort
233  //Internal sort objects should probably be re-written so that
234  //the interface is cleaner and they don't have to worry about I/O
235  InStr->truncate(0); //delete original items
236  InStr->seek(0); //rewind
237  }
238 
239  write_progress.init(nItems);
240  //Write sorted array to OutStr
241  for (i = 0; i < nItems; i++) {
242  OutStr->write(ItemArray[i]);
243  write_progress.step();
244  }
245  write_progress.done();
246  fp.done();
247 }
248 
249 } // ami namespace
250 } // tpie namespace
251 #endif // _INTERNAL_SORT_H
252 
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 
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:78
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...
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 item_type & read()
Read an item from the stream.
Definition: file_stream.h:91
Fractional progress reporter.
This file contains a few deprecated definitions for legacy code.
void deallocate(void)
Clean up internal array ItemArray.
void write(const item_type &item)
Write an item to the stream.
Definition: file_stream.h:64
void allocate(TPIE_OS_SIZE_T nItems)
Allocate ItemArray as array that can hold nItems.
void truncate(stream_size_type size)
Truncate file to given size.
array< T > ItemArray
Array that holds items to be sorted.
Definition: internal_sort.h:81
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.
void seek(stream_offset_type offset, offset_type whence=beginning)
Moves the logical offset in the stream.
Definition: stream_crtp.h:50
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.
Simple class acting both as file and a file::stream.
Definition: file_stream.h:44
A simple class that facilitates doing key sorting followed by in-memory permuting to sort items in-me...
Definition: internal_sort.h:47
#define tp_assert(condition, message)
Definition: tpie_assert.h:47
Internal_Sorter_Base(void)
Constructor.
Definition: internal_sort.h:89
~Internal_Sorter_Obj()
Empty destructor.
TPIE_OS_SIZE_T len
length of ItemArray
Definition: internal_sort.h:83
Subindicator for fractional progress reporting.
Internal_Sorter_Obj(Compare cmp)
Empty constructor.