[go: up one dir, main page]

File: chunk_collector.h

package info (click to toggle)
seqan 1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 21,368 kB
  • ctags: 17,376
  • sloc: cpp: 81,339; ansic: 58,225; makefile: 96; sh: 14
file content (542 lines) | stat: -rw-r--r-- 17,542 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
 /*==========================================================================
                SeqAn - The Library for Sequence Analysis
                          http://www.seqan.de 
 ============================================================================
  Copyright (C) 2007

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 3 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

 ============================================================================
  $Id: chunk_collector.h 4615 2009-07-25 07:09:21Z doering@PCPOOL.MI.FU-BERLIN.DE $
 ==========================================================================*/

#ifndef SEQAN_HEADER_CHUNK_COLLECTOR_H
#define SEQAN_HEADER_CHUNK_COLLECTOR_H


namespace SEQAN_NAMESPACE_MAIN
{
//////////////////////////////////////////////////////////////////////////////

/**
.Internal._ChunkCollector:
..cat:Classes
..summary:Reads piecewise from stream, collects pieces (chunks) in a vector.
..signature:_ChunkCollector<Host>
..param.Host:Type of host object that is used as allocator.
*/

//////////////////////////////////////////////////////////////////////////////

template <typename T>
struct ChunkLength
{
	enum { VALUE = 1024 };
};

//////////////////////////////////////////////////////////////////////////////
// _StreamChunkCollector class: collects content of a stream in chunks
//////////////////////////////////////////////////////////////////////////////

template <typename THost>
class _ChunkCollector
{
public:
	THost * data_host;
	typename Size<THost>::Type data_length;

	typedef ::std::vector<typename Value<THost>::Type *, ToStdAllocator<THost, typename Value<THost>::Type *> > Chunk_Holder;
	Chunk_Holder data_chunks; 

public:
	static int const CHUNK_LENGTH = ChunkLength<_ChunkCollector>::VALUE;

public:
	_ChunkCollector(THost & _host):
		data_host(& _host),
		data_length(0),
		data_chunks(typename Chunk_Holder::allocator_type(_host))
	{
	}

	~_ChunkCollector()
	{
		clear(*this);
	}

};

template <typename THost>
inline void
clear(_ChunkCollector<THost> & me)
{
   typedef _ChunkCollector<THost> TChunkCollector;
   typedef typename TChunkCollector::Chunk_Holder Chunk_Holder;
      
	typename Chunk_Holder::iterator it = me.data_chunks.begin();
	typename Chunk_Holder::iterator it_end = me.data_chunks.end();

	for (; it != it_end; ++it)
	{
		deallocate(me.data_host, *it, TChunkCollector::CHUNK_LENGTH);
	}

	me.data_chunks.clear();
	me.data_length = 0;
}

template <typename THost>
inline typename Size<THost>::Type
length(_ChunkCollector<THost> const & me)
{
	return me.data_length;
}

template <typename THost>
inline void
_setLength(_ChunkCollector<THost> & me, typename Size<THost>::Type new_length)
{
	me.data_length = new_length;
}

template <typename THost>
inline int
chunkCount(_ChunkCollector<THost> const & me)
{
	return me.data_chunks.size();
}

template <typename THost>
inline typename Value<THost>::Type *
getChunk(_ChunkCollector<THost> const & me, int chunk_number)
{
	return me.data_chunks[chunk_number];
}

template <typename THost>
inline typename Value<THost>::Type *
createChunk(_ChunkCollector<THost> & me)
{
   typedef _ChunkCollector<THost> TChunkCollector;
	typename Value<THost>::Type * new_chunk;
	allocate(me.data_host, new_chunk, TChunkCollector::CHUNK_LENGTH);
	me.data_chunks.push_back(new_chunk);
	return new_chunk;
}
	
//////////////////////////////////////////////////////////////////////////////

template <typename THost>
struct Host<_ChunkCollector<THost> >
{
	typedef THost Type;
};

template <typename THost>
struct Host<_ChunkCollector<THost> const >
{
	typedef THost Type;
};

//////////////////////////////////////////////////////////////////////////////

template <typename THost>
struct Value<_ChunkCollector<THost> >
{
	typedef typename Value<THost>::Type Type;
};

template <typename THost>
struct Value<_ChunkCollector<THost> const >
{
	typedef typename Value<THost>::Type Type;
};

//////////////////////////////////////////////////////////////////////////////

template <typename THost>
struct GetValue<_ChunkCollector<THost> >
{
	typedef typename GetValue<THost>::Type Type;
};

template <typename THost>
struct GetValue<_ChunkCollector<THost> const >
{
	typedef typename GetValue<THost>::Type Type;
};

//////////////////////////////////////////////////////////////////////////////

template <typename THost>
struct Size<_ChunkCollector<THost> >
{
	typedef typename Size<THost>::Type Type;
};

template <typename THost>
struct Size<_ChunkCollector<THost> const >
{
	typedef typename Size<THost>::Type Type;
};

//////////////////////////////////////////////////////////////////////////////

struct _Assign_Stream_2_ChunkCollector
{
	template <typename THost, typename TSource>
	static inline void 
	assign_(_ChunkCollector<THost> & target,
		TSource & source)
	{
		clear(target);

		while (!_streamEOF(source))
		{
			typename Value<THost>::Type * chunk = createChunk(target);
			typename Size<THost>::Type count = _streamRead(chunk, source, ChunkLength< _ChunkCollector<THost> >::VALUE);
			_setLength(target, length(target) + count);
		}
	}

	template <typename THost, typename TSource>
	static inline void 
	assign_(_ChunkCollector<THost> & target,
		TSource & source,
		typename Size< _ChunkCollector<THost> >::Type limit)
	{
		clear(target);

		while (!_streamEOF(source))
		{
			typename Value<THost>::Type * chunk = createChunk(target);
			typename Size<THost>::Type count = _streamRead(chunk, source, ChunkLength< _ChunkCollector<THost> >::VALUE);
			_setLength(target, length(target) + count);

			if (length(target) >= limit)
			{
				_setLength(target, limit);
				break;
			}
		}
	}
};

//////////////////////////////////////////////////////////////////////////////

template <typename THost, typename TSource>
inline void 
assign(_ChunkCollector<THost> & target,
	   TSource & source)
{
	_Assign_Stream_2_ChunkCollector::assign_(target, source);
}
template <typename THost, typename TSource>
inline void 
assign(_ChunkCollector<THost> & target,
	   TSource const & source)
{
	_Assign_Stream_2_ChunkCollector::assign_(target, source);
}

template <typename THost, typename TSource, typename TSize>
inline void 
assign(_ChunkCollector<THost> & target,
	   TSource & source,
	   TSize limit)
{
	_Assign_Stream_2_ChunkCollector::assign_(target, source, limit);
}
template <typename THost, typename TSource, typename TSize>
inline void 
assign(_ChunkCollector<THost> & target,
	   TSource const & source,
	   TSize limit)
{
	_Assign_Stream_2_ChunkCollector::assign_(target, source, limit);
}

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

template <typename TExpand>
struct _Assign_ChunkCollector_2_String
{
	template <typename TTarget, typename TSource>
	static void assign_(
		TTarget & target, 
		TSource & source)
	{
		typename Size<TTarget>::Type part_length = _clearSpace(target, length(source), TExpand());

		int i_end = chunkCount(source);
		typename Value<TTarget>::Type * pos = begin(target);
		for (int i = 0; i < i_end; ++i)
		{
			bool is_last_chunk = ( part_length <= ChunkLength<TSource>::VALUE);
			typename Size<TTarget>::Type chunk_length = (is_last_chunk) ? part_length : ChunkLength<TSource>::VALUE;
			typename Value<TSource>::Type * chunk = getChunk(source, i);
			
			arrayConstructCopy(chunk, chunk + chunk_length, pos);
			if (is_last_chunk) break;
			pos += chunk_length;
		}
	}

	template <typename TTarget, typename TSource>
	static void assign_(
		TTarget & target, 
		TSource & source,
		typename Size<TTarget>::Type limit)
	{
		typename Size<TTarget>::Type part_length = _clearSpace(target, length(source), limit, TExpand());

		int i_end = chunkCount(source);
		typename Value<TTarget>::Type * pos = begin(target);
		for (int i = 0; i < i_end; ++i)
		{
			bool is_last_chunk = ( part_length <= ChunkLength<TSource>::VALUE);
			typename Size<TTarget>::Type chunk_length = (is_last_chunk) ? part_length : ChunkLength<TSource>::VALUE;
			typename Value<TSource>::Type * chunk = getChunk(source, i);
			
			arrayConstructCopy(chunk, chunk + chunk_length, pos);
			if (is_last_chunk) break;
			pos += chunk_length;
		}
	}
};

//////////////////////////////////////////////////////////////////////////////

template <typename TTargetValue, typename TTargetSpec, typename TSourceHost, typename TExpand>
inline void 
assign(String<TTargetValue, TTargetSpec> & target,
	   _ChunkCollector<TSourceHost> const & source,
	   Tag<TExpand> const tag)
{
	_Assign_ChunkCollector_2_String<Tag<TExpand> const>::assign_(target, source);
}
template <typename TTargetValue, typename TTargetSpec, typename TSourceHost, typename TExpand>
inline void 
assign(String<TTargetValue, TTargetSpec> & target,
	   _ChunkCollector<TSourceHost> const & source,
	   typename Size< String<TTargetValue, TTargetSpec> >::Type limit,
	   Tag<TExpand> const tag)
{
	_Assign_ChunkCollector_2_String<Tag<TExpand> const>::assign_(target, source, limit);
}


//////////////////////////////////////////////////////////////////////////////

template <typename TExpand>
struct _Append_ChunkCollector_2_String
{
	template <typename TTarget, typename TSource>
	static void append_(
		TTarget & target, 
		TSource & source)
	{
		typedef typename Size<TTarget>::Type TSize;
		TSize target_length_old = length(target);
		TSize part_length = _clearSpace(target, length(source), target_length_old, target_length_old, TExpand());

		int i_end = chunkCount(source);
		typename Value<TTarget>::Type * pos = begin(target) + target_length_old; //begin(target) was possibly changed by _clearSpace
		for (int i = 0; i < i_end; ++i)
		{
			bool is_last_chunk = ( part_length <= ChunkLength<TSource>::VALUE);
			typename Size<TTarget>::Type chunk_length = (is_last_chunk) ? part_length : (TSize) ChunkLength<TSource>::VALUE;
			typename Value<TSource>::Type * chunk = getChunk(source, i);
			
			arrayConstructCopy(chunk, chunk + chunk_length, pos);
			if (is_last_chunk) break;
			pos += chunk_length;
		}
	}

	template <typename TTarget, typename TSource>
	static void append_(
		TTarget & target, 
		TSource & source,
		typename Size<TTarget>::Type limit)
	{
		typedef typename Size<TTarget>::Type TSize;
		TSize target_length_old = length(target);
		TSize part_length = _clearSpace(target, length(source), target_length_old, target_length_old, limit, TExpand());

		int i_end = chunkCount(source);
		typename Value<TTarget>::Type * pos = begin(target) + target_length_old; //begin(target) was possibly changed by _clearSpace
		for (int i = 0; i < i_end; ++i)
		{
			bool is_last_chunk = ( part_length <= ChunkLength<TSource>::VALUE);
			typename Size<TTarget>::Type chunk_length = (is_last_chunk) ? part_length : (TSize) ChunkLength<TSource>::VALUE;
			typename Value<TSource>::Type * chunk = getChunk(source, i);
			
			arrayConstructCopy(chunk, chunk + chunk_length, pos);
			if (is_last_chunk) break;
			pos += chunk_length;
		}
	}
};

//////////////////////////////////////////////////////////////////////////////

template <typename TTargetValue, typename TTargetSpec, typename TSourceHost, typename TExpand>
inline void 
append(String<TTargetValue, TTargetSpec> & target,
	   _ChunkCollector<TSourceHost> const & source,
	   Tag<TExpand> const )
{
	_Append_ChunkCollector_2_String<Tag<TExpand> const>::append_(target, source);
}
template <typename TTargetValue, typename TTargetSpec, typename TSourceHost, typename TExpand>
inline void 
append(String<TTargetValue, TTargetSpec> & target,
	   _ChunkCollector<TSourceHost> const & source,
	   typename Size< String<TTargetValue, TTargetSpec> >::Type limit,
	   Tag<TExpand> const )
{
	_Append_ChunkCollector_2_String<Tag<TExpand> const>::append_(target, source, limit);
}

//////////////////////////////////////////////////////////////////////////////

template <typename TExpand>
struct _Replace_ChunkCollector_2_String
{
	template <typename TTarget, typename TSource>
	static void replace_(
		TTarget & target,
		typename Size<TTarget>::Type pos_begin,
		typename Size<TTarget>::Type pos_end,
		TSource & source)
	{
		typename Size<TTarget>::Type part_length = _clearSpace(target, length(source), pos_begin, pos_end, TExpand());

		int i_end = chunkCount(source);
		typename Value<TTarget>::Type * pos = begin(target) + pos_begin;
		for (int i = 0; i < i_end; ++i)
		{
			bool is_last_chunk = ( part_length <= ChunkLength<TSource>::VALUE);
			typename Size<TTarget>::Type chunk_length = (is_last_chunk) ? part_length : ChunkLength<TSource>::VALUE;
			typename Value<TSource>::Type * chunk = getChunk(source, i);
			
			arrayConstructCopy(chunk, chunk + chunk_length, pos);
			if (is_last_chunk) break;
			pos += chunk_length;
		}
	}

	template <typename TTarget, typename TSource>
	static void replace_(
		TTarget & target, 
		typename Size<TTarget>::Type pos_begin,
		typename Size<TTarget>::Type pos_end,
		TSource & source,
		typename Size<TTarget>::Type limit)
	{
		typename Size<TTarget>::Type part_length = _clearSpace(target, length(source), pos_begin, pos_end, limit, TExpand());

		int i_end = chunkCount(source);
		typename Value<TTarget>::Type * pos = begin(target) + pos_begin;
		for (int i = 0; i < i_end; ++i)
		{
			bool is_last_chunk = ( part_length <= ChunkLength<TSource>::VALUE);
			typename Size<TTarget>::Type chunk_length = (is_last_chunk) ? part_length : ChunkLength<TSource>::VALUE;
			typename Value<TSource>::Type * chunk = getChunk(source, i);
			
			arrayConstructCopy(chunk, chunk + chunk_length, pos);
			if (is_last_chunk) break;
			pos += chunk_length;
		}
	}
};

//////////////////////////////////////////////////////////////////////////////

template <typename TTargetValue, typename TTargetSpec, typename TSourceHost, typename TExpand>
inline void 
replace(String<TTargetValue, TTargetSpec> & target,
		typename Size< String<TTargetValue, TTargetSpec> >::Type pos_begin,
		typename Size< String<TTargetValue, TTargetSpec> >::Type pos_end,
	   _ChunkCollector<TSourceHost> const & source,
	   Tag<TExpand> const tag)
{
	_Replace_ChunkCollector_2_String<Tag<TExpand> const>::replace_(target, pos_begin, pos_end, source);
}

template <typename TTargetValue, typename TTargetSpec, typename TSourceHost, typename TExpand>
inline void 
replace(String<TTargetValue, TTargetSpec> & target,
		typename Size< String<TTargetValue, TTargetSpec> >::Type pos_begin,
		typename Size< String<TTargetValue, TTargetSpec> >::Type pos_end,
	   _ChunkCollector<TSourceHost> const & source,
	   typename Size< String<TTargetValue, TTargetSpec> >::Type limit,
	   Tag<TExpand> const tag)
{
	_Replace_ChunkCollector_2_String<Tag<TExpand> const>::replace_(target, pos_begin, pos_end, source, limit);
}

//////////////////////////////////////////////////////////////////////////////

template <typename TTargetValue, typename TSourceHost, typename TExpand>
inline void 
replace(TTargetValue * target,
		size_t pos_begin,
		size_t pos_end,
		_ChunkCollector<TSourceHost> const & source,
		Tag<TExpand> const tag)
{
	_Replace_ChunkCollector_2_String<Tag<TExpand> const>::replace_(target, pos_begin, pos_end, source);
}

template <typename TTargetValue, typename TSourceHost, typename TExpand>
inline void 
replace(TTargetValue * target,
		size_t pos_begin,
		size_t pos_end,
		_ChunkCollector<TSourceHost> const & source,
		size_t limit,
		Tag<TExpand> const tag)
{
	_Replace_ChunkCollector_2_String<Tag<TExpand> const>::replace_(target, pos_begin, pos_end, source, limit);
}
//____________________________________________________________________________
/*
template <typename TTargetValue, typename TSourceHost, typename TExpand>
inline void 
replace(TTargetValue * target,
		size_t pos_begin,
		size_t pos_end,
	   _ChunkCollector<TSourceHost> const & source,
	   Tag<TExpand> const tag)
{
	_Replace_ChunkCollector_2_String<Tag<TExpand> const>::replace_(target, pos_begin, pos_end, source);
}

template <typename TTargetValue, typename TTargetSpec, typename TSourceHost, typename TExpand>
inline void 
replace(String<TTargetValue, TTargetSpec> & target,
		typename Size< String<TTargetValue, TTargetSpec> >::Type pos_begin,
		typename Size< String<TTargetValue, TTargetSpec> >::Type pos_end,
	   _ChunkCollector<TSourceHost> const & source,
	   typename Size< String<TTargetValue, TTargetSpec> >::Type limit,
	   Tag<TExpand> const tag)
{
	_Replace_ChunkCollector_2_String<Tag<TExpand> const>::replace_(target, pos_begin, pos_end, source, limit);
}
*/
//////////////////////////////////////////////////////////////////////////////

} //namespace SEQAN_NAMESPACE_MAIN

#endif //#ifndef SEQAN_HEADER_...