TPIE

2362a60
merge_sorted_runs.h
Go to the documentation of this file.
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; c-file-style: "stroustrup"; -*-
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 _MERGE_SORTED_RUNS_H
21 #define _MERGE_SORTED_RUNS_H
22 
29 
30 // Get definitions for working with Unix and Windows
31 #include <tpie/portability.h>
32 
33 // Includes needed from TPIE
34 #include <tpie/mergeheap.h> //For templated heaps
35 
37 
38 #include <tpie/file_stream.h>
39 
40 
41 namespace tpie {
42 
43  namespace ami {
44 
46  typedef TPIE_OS_SIZE_T arity_t;
47 
72  template <class T, class M>
73  void merge_sorted_runs(typename tpie::array<tpie::unique_ptr<file_stream<T> > >::iterator start,
74  typename tpie::array<tpie::unique_ptr<file_stream<T> > >::iterator end,
75  file_stream<T> *outStream, M* MergeHeap,
76  TPIE_OS_OFFSET cutoff=-1,
77  progress_indicator_base* indicator = NULL) {
78 
79  size_t i;
80  size_t arity = end-start;
81 
82  //Pointers to current leading elements of streams
83  tpie::array<const T *> in_objects(arity);
84  tpie::array<TPIE_OS_OFFSET> nread(arity);
85 
86  // **************************************************************
87  // * Read first element from stream. Do not rewind! We may read *
88  // * more elements from the same stream later. *
89  // **************************************************************
90 
91  for (i = 0; i < arity; i++) {
92  file_stream<T> * stream = (start+i)->get();
93  if (stream->can_read()) {
94  in_objects[i] = &stream->read();
95  MergeHeap->insert( in_objects[i], i );
96  } else
97  in_objects[i] = NULL;
98  nread[i] = 1;
99  if (indicator) indicator->step();
100  }
101 
102  // *********************************************************
103  // * Build a heap from the smallest items of each stream *
104  // *********************************************************
105 
106  MergeHeap->initialize ( );
107 
108  // *********************************************************
109  // * Perform the merge until the inputs are exhausted. *
110  // *********************************************************
111  while (MergeHeap->sizeofheap() > 0) {
112  i = MergeHeap->get_min_run_id ();
113  outStream->write(*in_objects[i]);
114 
115  bool eof = false;
116 
117  //Check if we read as many elements as we are allowed to
118  if ( (cutoff != -1) && (nread[i]>=cutoff))
119  eof = true;
120  else {
121  file_stream<T> * stream = (start+i)->get();
122  if (stream->can_read()) in_objects[i] = &stream->read();
123  else eof = true;
124 
125  if (indicator) indicator->step();
126  }
127 
128  if (eof)
129  MergeHeap->delete_min_and_insert(NULL);
130  else {
131  nread[i]++;
132  MergeHeap->delete_min_and_insert (in_objects[i]);
133  }
134  } // while
135  }
136 
137 
150  template <class T, class CMPR>
151  void merge_sorted(typename tpie::array<std::unique_ptr<file_stream<T> > >::iterator start,
152  typename tpie::array<std::unique_ptr<file_stream<T> > >::iterator end,
153  file_stream<T> *outStream, CMPR *cmp) {
154 
155  // make a merge heap which uses the user's comparison object
156  // and initialize it
157  merge_heap_obj<T,CMPR> mrgheap (cmp);
158  mrgheap.allocate(end-start);
159 
160  //Rewind all the input streams
161  for (typename tpie::array<std::unique_ptr<file_stream<T> > >::iterator i=start;
162  i != end; ++i)
163  i->seek(0);
164 
165  merge_sorted_runs(start, end, outStream, mrgheap);
166  }
167 
168 
182  template <class T, class CMPR>
183  void ptr_merge_sorted(typename tpie::array<tpie::unique_ptr<file_stream<T> > >::iterator start,
184  typename tpie::array<tpie::unique_ptr<file_stream<T> > >::iterator end,
185  file_stream<T> *outStream,
186  CMPR *cmp) {
187 
188  // make a merge heap of pointers which uses the user's comparison
189  // object and initialize it
190  merge_heap_ptr_obj<T,CMPR> mrgheap (cmp);
191  mrgheap.allocate(end-start);
192 
193  // Rewind all the input streams
194  for (typename tpie::array<std::unique_ptr<file_stream<T> > >::iteratorarity_t i=start;
195  i != end; ++i)
196  i->seek(0);
197 
198  merge_sorted_runs(start, end, outStream, mrgheap);
199  }
200  } // ami namespace
201 
202 } // tpie namespace
203 
204 #endif //_MERGE_SORTED_RUNS_H
The base class for indicating the progress of some task.
A generic array with a fixed size.
Definition: array.h:144
bool can_read()
Check if the next call to read() will succeed or not.
Definition: stream.h:1010
TPIE_OS_SIZE_T arity_t
Intended to signal the number of input streams in a merge.
const T & read()
Reads next item from stream if can_read() == true.
Definition: stream.h:947
This file contains a few deprecated definitions for legacy code.
void merge_sorted_runs(typename tpie::array< tpie::unique_ptr< file_stream< T > > >::iterator start, typename tpie::array< tpie::unique_ptr< file_stream< T > > >::iterator end, file_stream< T > *outStream, M *MergeHeap, TPIE_OS_OFFSET cutoff=-1, progress_indicator_base *indicator=NULL)
This is a common merge routine for all of the AMI_merge_sorted, AMI_ptr_merge_sorted and AMI_key_merg...
Merge heap templates.
A record pointer heap that uses a comparison object.
Definition: mergeheap.h:141
void ptr_merge_sorted(typename tpie::array< tpie::unique_ptr< file_stream< T > > >::iterator start, typename tpie::array< tpie::unique_ptr< file_stream< T > > >::iterator end, file_stream< T > *outStream, CMPR *cmp)
Merging with a heap that keeps a pointer to the records rather than the records themselves: CMPR is t...
Progress indicator base.
void allocate(size_t size)
Allocates space for the heap.
Definition: mergeheap.h:87
void merge_sorted(typename tpie::array< std::unique_ptr< file_stream< T > > >::iterator start, typename tpie::array< std::unique_ptr< file_stream< T > > >::iterator end, file_stream< T > *outStream, CMPR *cmp)
Merging with a heap that contains the records to be merged.
std::unique_ptr< T, tpie_deleter > unique_ptr
like std::unique_ptr, but delete the object with tpie_delete.
Definition: memory.h:338
Compressed stream.
Definition: predeclare.h:46
void allocate(size_t size)
Allocates space for the heap.
Definition: mergeheap.h:194