TPIE

2362a60
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 166 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 177 of file internal_sort.h.

177 :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 182 of file internal_sort.h.

182 {};

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 128 of file internal_sort.h.

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

Clean up internal array ItemArray.

Definition at line 134 of file internal_sort.h.

134  {
135  ItemArray.resize(0);
136  len=0;
137 }
array< T > ItemArray
Array that holds items to be sorted.
Definition: internal_sort.h:82
void resize(size_t size, const T &elm)
Change the size of the array.
Definition: array.h:485
TPIE_OS_SIZE_T len
length of ItemArray
Definition: internal_sort.h:84
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 140 of file internal_sort.h.

140  {
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 }
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 202 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::file_stream< T >::seek(), tpie::progress_indicator_base::step(), tp_assert, TPIE_FSI, and tpie::file_stream< T >::truncate().

205  {
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 }
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:82
iterator begin()
Return an iterator to the beginning of the array.
Definition: array.h:307
size_type size() const
Return the size of the array.
Definition: array.h:526
#define tp_assert(condition, message)
Definition: tpie_assert.h:48
TPIE_OS_SIZE_T len
length of ItemArray
Definition: internal_sort.h:84
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 150 of file internal_sort.h.

150  {
151  // Space usage independent of space_per_item
152  // accounts MM_manager space overhead on "new" call
153  return 0;
154 }
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 157 of file internal_sort.h.

157  {
158  return sizeof(T);
159 }

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 171 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 82 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 84 of file internal_sort.h.


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