TPIE

v1.1rc1-6-g0c97303
tpie::ami::Internal_Sorter_Obj< T, Compare > Class Template Reference

Comparision object based Internal_Sorter_base subclass implementation; uses quick_sort_obj(). More...

#include <tpie/internal_sort.h>

Inherits tpie::ami::Internal_Sorter_Base< T >.

Public Member Functions

 Internal_Sorter_Obj (Compare cmp)
 Empty constructor. More...
 
 ~Internal_Sorter_Obj ()
 Empty destructor. More...
 
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 to OutStr, starting from the current file position. More...
 
void allocate (TPIE_OS_SIZE_T nItems)
 Allocate ItemArray as array that can hold nItems. More...
 
void deallocate (void)
 Clean up internal array ItemArray. More...
 
TPIE_OS_SIZE_T MaxItemCount (TPIE_OS_SIZE_T memSize)
 Returns maximum number of items that can be sorted using memSize bytes. More...
 
TPIE_OS_SIZE_T space_per_item ()
 Returns memory usage in bytes per sort item. More...
 
TPIE_OS_SIZE_T space_overhead ()
 Returns fixed memory usage overhead in bytes per class instantiation. More...
 

Protected Attributes

Compare cmp_o
 Comparison object used for sorting. More...
 
array< T > ItemArray
 Array that holds items to be sorted. More...
 
TPIE_OS_SIZE_T len
 length of ItemArray More...
 

Detailed Description

template<class T, class Compare>
class tpie::ami::Internal_Sorter_Obj< T, Compare >

Comparision object based Internal_Sorter_base subclass implementation; uses quick_sort_obj().

Definition at line 165 of file internal_sort.h.

Constructor & Destructor Documentation

template<class T, class Compare>
tpie::ami::Internal_Sorter_Obj< T, Compare >::Internal_Sorter_Obj ( Compare  cmp)
inline

Empty constructor.

Definition at line 176 of file internal_sort.h.

176 :cmp_o(cmp) {};
Compare cmp_o
Comparison object used for sorting.
template<class T, class Compare>
tpie::ami::Internal_Sorter_Obj< T, Compare >::~Internal_Sorter_Obj ( )
inline

Empty destructor.

Definition at line 181 of file internal_sort.h.

181 {};

Member Function Documentation

template<class T >
void tpie::ami::Internal_Sorter_Base< T >::allocate ( TPIE_OS_SIZE_T  nItems)
inlineinherited

Allocate ItemArray as array that can hold nItems.

Definition at line 127 of file internal_sort.h.

127  {
128  len=nitems;
130 }
array< T > ItemArray
Array that holds items to be sorted.
Definition: internal_sort.h:81
void resize(size_t size, const T &elm)
Change the size of the array.
Definition: array.h:431
TPIE_OS_SIZE_T len
length of ItemArray
Definition: internal_sort.h:83
template<class T >
void tpie::ami::Internal_Sorter_Base< T >::deallocate ( void  )
inlineinherited

Clean up internal array ItemArray.

Definition at line 133 of file internal_sort.h.

133  {
134  ItemArray.resize(0);
135  len=0;
136 }
array< T > ItemArray
Array that holds items to be sorted.
Definition: internal_sort.h:81
void resize(size_t size, const T &elm)
Change the size of the array.
Definition: array.h:431
TPIE_OS_SIZE_T len
length of ItemArray
Definition: internal_sort.h:83
template<class T >
TPIE_OS_SIZE_T tpie::ami::Internal_Sorter_Base< T >::MaxItemCount ( TPIE_OS_SIZE_T  memSize)
inlineinherited

Returns maximum number of items that can be sorted using memSize bytes.

Definition at line 139 of file internal_sort.h.

139  {
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 }
TPIE_OS_SIZE_T space_overhead()
Returns fixed memory usage overhead in bytes per class instantiation.
TPIE_OS_SIZE_T space_per_item()
Returns memory usage in bytes per sort item.
template<class T , class Compare >
void tpie::ami::Internal_Sorter_Obj< T, Compare >::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 to OutStr, starting from the current file position.

Definition at line 201 of file internal_sort.h.

References tpie::fractional_subindicator::done(), tpie::fractional_progress::done(), tpie::fractional_progress::id(), tpie::fractional_subindicator::init(), tpie::fractional_progress::init(), tpie::file_stream< T >::read(), tpie::stream_crtp< child_t >::seek(), tpie::progress_indicator_base::step(), tp_assert, TPIE_FSI, tpie::file_stream_base::truncate(), and tpie::file_stream< T >::write().

204  {
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 }
Compare cmp_o
Comparison object used for sorting.
#define TPIE_FSI
For use when constructing a fractional subindicator.
array< T > ItemArray
Array that holds items to be sorted.
Definition: internal_sort.h:81
iterator begin()
Return an iterator to the beginning of the array.
Definition: array.h:274
size_type size() const
Return the size of the array.
Definition: array.h:472
#define tp_assert(condition, message)
Definition: tpie_assert.h:47
TPIE_OS_SIZE_T len
length of ItemArray
Definition: internal_sort.h:83
template<class T >
TPIE_OS_SIZE_T tpie::ami::Internal_Sorter_Base< T >::space_overhead ( void  )
inlineinherited

Returns fixed memory usage overhead in bytes per class instantiation.

Definition at line 149 of file internal_sort.h.

149  {
150  // Space usage independent of space_per_item
151  // accounts MM_manager space overhead on "new" call
152  return 0;
153 }
template<class T >
TPIE_OS_SIZE_T tpie::ami::Internal_Sorter_Base< T >::space_per_item ( void  )
inlineinherited

Returns memory usage in bytes per sort item.

Definition at line 156 of file internal_sort.h.

156  {
157  return sizeof(T);
158 }

Member Data Documentation

template<class T, class Compare>
Compare tpie::ami::Internal_Sorter_Obj< T, Compare >::cmp_o
protected

Comparison object used for sorting.

Definition at line 170 of file internal_sort.h.

template<class T>
array<T> tpie::ami::Internal_Sorter_Base< T >::ItemArray
protectedinherited

Array that holds items to be sorted.

Definition at line 81 of file internal_sort.h.

template<class T>
TPIE_OS_SIZE_T tpie::ami::Internal_Sorter_Base< T >::len
protectedinherited

length of ItemArray

Definition at line 83 of file internal_sort.h.


The documentation for this class was generated from the following file: