/usr/include/seqan/pipe/pool_base.h is in seqan-dev 1.3.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 | // ==========================================================================
// SeqAn - The Library for Sequence Analysis
// ==========================================================================
// Copyright (c) 2006-2010, Knut Reinert, FU Berlin
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Knut Reinert or the FU Berlin nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.
//
// ==========================================================================
// Author: David Weese <david.weese@fu-berlin.de>
// ==========================================================================
#ifndef SEQAN_HEADER_POOL_BASE_H
#define SEQAN_HEADER_POOL_BASE_H
namespace SEQAN_NAMESPACE_MAIN
{
/**
.Spec.PoolConfigSize:
..cat:Pipelining
..general:Spec.PoolSpec
..summary:Configuration of Pool.
..signature:PoolConfig<TSize, TFile>
..param.TSize:The Pool's size type.
..param.TFile:The underlying @Class.File@ type.
...default:$File<>$, see @Class.File@.
..see:Spec.PoolConfig
..include:seqan/pipe.h
*/
template < typename TSize,
typename TFile = File<> > // default file type
struct PoolConfigSize {
typedef TSize SizeType;
typedef TFile File;
};
/**
.Spec.PoolConfig:
..cat:Pipelining
..general:Spec.PoolSpec
..summary:Configuration of Pool.
..signature:PoolConfig<TFile>
..param.TFile:The underlying @Class.File@ type.
...default:$File<>$, see @Class.File@.
..remarks:Using this configuration spec., the Pool's size type is $Size<TFile>::Type$. To use a custom size type @Spec.PoolConfigSize@ should be used.
..see:Spec.PoolConfigSize
..include:seqan/pipe.h
*/
template < typename TFile = File<> > // default file type
struct PoolConfig {
typedef typename Size<TFile>::Type SizeType;
typedef TFile File;
};
/**
.Spec.PoolSpec:
..cat:Pipelining
..general:Class.Pool
..summary:Stores/Retrieves all elements to/from disk.
..signature:Pool<TValue, PoolSpec<TConfig> >
..param.TValue:The value type, that is the type of the stream elements.
..param.TConfig:Configuration Spec. Defines destination function, size type, and file type.
...type:Spec.PoolConfig
...type:Spec.PoolConfigSize
..remarks:The Pool's input/output type is $TValue$ and the size type is determined by the $TConfig$.
..include:seqan/pipe.h
*/
template < typename TConfig = PoolConfig<> >
struct PoolSpec {
typedef TConfig Config;
};
/**
.Class.Pool:
..cat:Pipelining
..summary:Pools are push- and pop-passive pipeline modules.
..signature:Pool<TValue, TSpec>
..param.TValue:The value type, that is the type of the stream elements.
..param.TSpec:The specializing type.
...default:PoolSpec<>, see @Spec.PoolSpec@.
..remarks:Use @Metafunction.Value@ to get the output type of a given Pipe (returns $Value<TInput>::Type$ by default).
..remarks:Use @Metafunction.Size@ to get the size type of a given Pipe (returns $Size<TInput>::Type$ by default).
..include:seqan/pipe.h
*/
template < typename TValue,
typename TSpec = PoolSpec<> >
struct Pool;
struct PoolParameters
{
/*
enum { DefaultMemBufferSize = 384 * 1024*1024, // max memory config
DefaultPageSize = 32 * 1024*1024,
DefaultBucketBufferSize = 64 * 1024*1024,
DefaultReadAheadBuffers = 2,
DefaultWriteBackBuffers = 2,
DefaultWriteBackBuckets = 16,
DefaultAbsoluteSizes = true };
*/
enum { DefaultMemBufferSize = 128 * 1024*1024, // normal memory config
DefaultPageSize = 32 * 1024*1024,
DefaultBucketBufferSize = 64 * 1024*1024,
DefaultReadAheadBuffers = 2,
DefaultWriteBackBuffers = 2,
DefaultWriteBackBuckets = 16,
DefaultAbsoluteSizes = true };
/*
enum { DefaultMemBufferSize = 0*8192,//64 * 1024*1024, // low memory config
DefaultPageSize = 2 * 1024*1024,
DefaultBucketBufferSize = 6 * 1024*1024,
DefaultReadAheadBuffers = 4,
DefaultWriteBackBuffers = 4,
DefaultWriteBackBuckets = 16,
DefaultAbsoluteSizes = true };
*/
unsigned memBufferSize;
unsigned pageSize;
unsigned bucketBufferSize;
unsigned readAheadBuffers;
unsigned writeBackBuffers;
unsigned writeBackBuckets;
bool absoluteSizes; // when false, sizes are measured in units of TValue
// when true, sizes are measured in bytes
PoolParameters():
memBufferSize(DefaultMemBufferSize),
pageSize(DefaultPageSize),
bucketBufferSize(DefaultBucketBufferSize),
readAheadBuffers(DefaultReadAheadBuffers),
writeBackBuffers(DefaultWriteBackBuffers),
writeBackBuckets(DefaultWriteBackBuckets),
absoluteSizes(DefaultAbsoluteSizes) {}
template < typename TValue >
void absolutize(unsigned aligning, TValue *) {
if (!absoluteSizes) return;
memBufferSize = memBufferSize / sizeof(TValue);
bucketBufferSize = bucketBufferSize / sizeof(TValue);
pageSize = pageSize / sizeof(TValue);
pageSize = (pageSize / aligning) * aligning;
if (!pageSize) pageSize += aligning;
}
};
template < typename TBuffer, typename THandler >
inline TBuffer& processBuffer(TBuffer &h, THandler &) {
return h;
}
//////////////////////////////////////////////////////////////////////////////
// handler that manages a simple memory buffer
struct MemorySpec_;
typedef Tag<MemorySpec_> MemorySpec;
template < typename TPool >
struct BufferHandler< TPool, MemorySpec >
{
typedef TPool Pool;
typedef typename TPool::Type Type;
typedef SimpleBuffer<Type> Buffer;
typedef Buffer& BufferRef;
TPool &pool;
Buffer empty;
BufferHandler(TPool &_pool):
pool(_pool) {}
BufferHandler(TPool &_pool, unsigned):
pool(_pool) {}
inline BufferRef first() {
return pool.memBuffer;
}
inline BufferRef next() {
return empty;
}
inline void process() {
processBuffer(pool.memBuffer, *this);
}
inline void end() {}
inline void cancel() {}
};
//////////////////////////////////////////////////////////////////////////////
// generic block based asynchronous read handler
struct ReadFileSpec_;
typedef Tag<ReadFileSpec_> ReadFileSpec;
template < typename TPool >
struct BufferHandler< TPool, ReadFileSpec >
{
typedef TPool Pool;
typedef typename TPool::Type Type;
typedef typename TPool::File File;
typedef SimpleBuffer<Type> Buffer;
typedef PageFrame<Type, File, Dynamic<> > TPageFrame;
typedef TPageFrame& PageFrameRef;
typedef Buffer& BufferRef;
typedef PageChain<TPageFrame> TPageChain;
TPool &pool;
TPageChain chain;
unsigned pageSize;
unsigned readPageNo, _pages;
Buffer empty;
BufferHandler(TPool &_pool):
pool(_pool),
chain(_min(_pool.readAheadBuffers, _pool.pages())),
pageSize(_pool.pageSize) {}
BufferHandler(TPool &_pool, unsigned _requestedBufferSize, unsigned _readAheadBuffers = 1):
pool(_pool),
// pageSize(alignSize(_min(_pool.size(), _requestedBufferSize), _pool.pageSize)),
chain(_min(_readAheadBuffers, _pool.pages(pageSize = alignSize(_min(_pool.size(), _requestedBufferSize), _pool.pageSize))))
{
#ifdef SEQAN_HEADER_PIPE_DEBUG
::std::cerr << "___BufferHandler___" << ::std::endl;
::std::cerr << "pagesize: " << pageSize << ::std::endl;
::std::cerr << "readaheadbuffers: " << chain.maxFrames << ::std::endl;
::std::cerr << "pages: " << pool.pages(pageSize) << ::std::endl;
#endif
}
~BufferHandler() {
end();
}
inline BufferRef first() {
_pages = pool.pages(pageSize);
if (!_pages) return empty;
// enqueue reading of the <readAheadBuffers> first blocks
readPageNo = 0;
TPageFrame *p = chain.first;
while (p) {
p->pageNo = readPageNo++;
_read(*p);
p = p->next;
}
// retrieve the very first
waitFor(*chain.first);
return processBuffer(*chain.first, *this);
}
inline BufferRef next() {
// step one buffer ahead
chain.getReadyPage();
// read ahead
chain.last->pageNo = readPageNo++;
_read(*chain.last);
// retrieve the next buffer in order
waitFor(*chain.first);
return processBuffer(*chain.first, *this);
}
inline void end() {
cancel();
}
inline void cancel() {
TPageFrame *p = chain.first;
while (p) {
::seqan::cancel(*p, pool.file);
freePage(*p, pool.file);
p = p->next;
}
}
inline void process() {}
private:
bool _error() {
// ::std::cerr << "Error in BufWriteFileHandler::_read " << pool.file.error() << ::std::endl;
return true;
}
inline bool _read(TPageFrame &pf) {
if (pf.pageNo < _pages) {
// alloc if empty
if (!pf.begin)
allocPage(pf, pageSize, pool.file);
// set buffer size according to read size
resize(pf, pool.dataSize(pf.pageNo, pageSize));
// read asynchronously (if possible) from disk
return readPage(pf, pool.file) || _error();
} else {
// free if allocated
freePage(pf, pool.file);
return false;
}
}
};
//////////////////////////////////////////////////////////////////////////////
// generic block based asynchronous write handler
struct WriteFileSpec_;
typedef Tag<WriteFileSpec_> WriteFileSpec;
template < typename TPool >
struct BufferHandler< TPool, WriteFileSpec >
{
typedef TPool Pool;
typedef typename TPool::Type Type;
typedef typename TPool::File File;
typedef SimpleBuffer<Type> Buffer;
typedef PageFrame<Type, File, Dynamic<> > TPageFrame;
typedef TPageFrame& PageFrameRef;
typedef Buffer& BufferRef;
typedef PageChain<TPageFrame> TPageChain;
TPool &pool;
TPageChain chain;
unsigned pageSize;
unsigned writePageNo, _pages;
Buffer empty;
BufferHandler(TPool &_pool):
pool(_pool),
chain(_min(_pool.writeBackBuffers, _pool.pages())),
pageSize(_pool.pageSize) {}
BufferHandler(TPool &_pool, unsigned _requestedBufferSize, unsigned _writeBackBuffers = 1):
pool(_pool),
chain(_min(_writeBackBuffers, _pool.pages(pageSize))),
pageSize(alignSize(_min(_pool.size(), _requestedBufferSize), _pool.pageSize)) {}
~BufferHandler() {
cancel();
}
public:
inline BufferRef first() {
_pages = pool.pages(pageSize);
if (!_pages) return empty;
// get a ready page frame
PageFrameRef pf = *chain.getReadyPage();
if (!pf.begin)
allocPage(pf, pageSize, pool.file);
writePageNo = 0;
resize(pf, pool.dataSize(pf.pageNo = writePageNo++, pageSize));
return pf;
}
inline BufferRef next() {
// write previously provided buffer to disk
_write(processBuffer(*chain.last, *this));
// step one buffer ahead
PageFrameRef pf = *chain.getReadyPage();
if (!pf.begin)
allocPage(pf, pageSize, pool.file);
resize(pf, pool.dataSize(pf.pageNo = writePageNo++, pageSize));
return pf;
}
inline void end() {
// write previously provided buffer to disk
_write(processBuffer(*chain.last, *this));
flush();
}
inline void flush() {
TPageFrame *p = chain.first;
while (p) {
waitFor(*p);
freePage(*p, pool.file);
p = p->next;
}
::seqan::flush(pool.file);
}
inline void cancel() {
TPageFrame *p = chain.first;
while (p) {
::seqan::cancel(*p, pool.file);
freePage(*p, pool.file);
p = p->next;
}
}
inline void process() {}
private:
bool _error() {
// ::std::cerr << "Error in BufWriteFileHandler::_write " << pool.file.error() << ::std::endl;
return true;
}
bool _write(TPageFrame &pf) {
if (pf.pageNo < _pages) {
// write asynchronously (if possible) to disk
return writePage(pf, pool.file) || _error();
} else {
// free if allocated
freePage(pf, pool.file);
return false;
}
}
};
//////////////////////////////////////////////////////////////////////////////
// generic buffered multiplex handler
struct MultiplexSpec_;
typedef Tag<MultiplexSpec_> MultiplexSpec;
template < typename TBufferHandler1, typename TBufferHandler2 >
struct BufferHandler< Bundle2< TBufferHandler1, TBufferHandler2 >, MultiplexSpec >
{
typedef typename TBufferHandler1::Type Type;
typedef typename TBufferHandler1::Buffer Buffer;
TBufferHandler1 *handler1;
TBufferHandler2 *handler2;
template <typename TPool>
BufferHandler(TPool &_pool) {
if (_pool.memBuffer.begin || _pool._size == 0) { // decision to choose handler1 or handler2
handler1 = new TBufferHandler1(_pool);
handler2 = NULL;
} else {
handler1 = NULL;
handler2 = new TBufferHandler2(_pool);
}
}
template <typename TPool>
BufferHandler(TPool &_pool, unsigned _requestedBufferSize) {
if (_pool.memBuffer.begin || _pool._size == 0) { // decision to choose handler1 or handler2
handler1 = new TBufferHandler1(_pool, _requestedBufferSize);
handler2 = NULL;
} else {
handler1 = NULL;
handler2 = new TBufferHandler2(_pool, _requestedBufferSize);
}
}
~BufferHandler() {
delete handler1;
delete handler2;
}
inline Buffer first() {
if (handler1) return handler1->first();
else return handler2->first();
}
inline Buffer next() {
if (handler1) return handler1->next();
else return handler2->next();
}
inline void end() {
if (handler1) return handler1->end();
else return handler2->end();
}
inline void process() {
if (handler1) return handler1->process();
else return handler2->process();
}
inline void cancel() {
if (handler1) return handler1->cancel();
else return handler2->cancel();
}
};
template < typename THandler1, typename THandler2 >
struct Handler< Bundle2< THandler1, THandler2 >, MultiplexSpec >
{
typedef typename THandler1::Type Type;
THandler1 *handler1;
THandler2 *handler2;
template <typename TPool>
Handler(TPool &_pool) {
if (_pool.memBuffer.begin || _pool._size == 0) { // decision to choose handler1 or handler2
handler1 = new THandler1(_pool);
handler2 = NULL;
} else {
handler1 = NULL;
handler2 = new THandler2(_pool);
}
}
~Handler() {
delete handler1;
delete handler2;
}
inline bool begin() {
if (handler1) return handler1->begin();
else return handler2->begin();
}
inline Type const & front() const {
if (handler1) return handler1->front();
else return handler2->front();
}
inline void pop() {
if (handler1) handler1->pop();
else handler2->pop();
}
inline void pop(Type &Ref_) {
if (handler1) handler1->pop(Ref_);
else handler2->pop(Ref_);
}
inline void push(Type const & Val_) {
if (handler1) handler1->push(Val_);
else handler2->push(Val_);
}
inline bool eof() const {
if (handler1) return handler1->eof();
else return handler2->eof();
}
inline void end() {
if (handler1) return handler1->end();
else return handler2->end();
}
inline void process() {
if (handler1) return handler1->process();
else return handler2->process();
}
};
//////////////////////////////////////////////////////////////////////////////
// character and buffer based handler definitions
template < typename TValue, typename TSpec >
struct BufReadHandler< Pool< TValue, TSpec > >
{
typedef BufferHandler< Bundle2<
BufferHandler< Pool< TValue, TSpec >, MemorySpec >,
BufferHandler< Pool< TValue, TSpec >, ReadFileSpec >
>, MultiplexSpec > Type;
};
template < typename TValue, typename TSpec >
struct BufWriteHandler< Pool< TValue, TSpec > >
{
typedef BufferHandler< Bundle2<
BufferHandler< Pool< TValue, TSpec >, MemorySpec >,
BufferHandler< Pool< TValue, TSpec >, WriteFileSpec >
>, MultiplexSpec > Type;
};
template < typename TValue, typename TPoolSpec, typename TSpec >
struct Value< BufferHandler< Pool< TValue, TPoolSpec >, TSpec > > {
typedef SimpleBuffer< TValue > Type;
};
template < typename TPool >
struct HandlerArgs {
typedef Nothing Type;
};
//////////////////////////////////////////////////////////////////////////////
// base class of all pool classes like Pool, Mapper, Sorter
template < typename TValue,
typename TSpec >
struct Pool
{
typedef TValue Type;
typedef TSpec Spec;
typedef typename TSpec::Config Config;
typedef typename Config::File File;
typedef typename Config::SizeType SizeType;
typedef SimpleBuffer<TValue> Buffer;
// public handlers to read simultanously buffer- or character-wise
typedef typename ReadHandler<Pool>::Type ReadHandler;
typedef typename WriteHandler<Pool>::Type WriteHandler;
typedef typename HandlerArgs<Pool>::Type HandlerArgs;
File file;
bool _temporary, _ownFile;
SizeType _size;
unsigned _pages;
unsigned pageSize;
unsigned bucketBufferSize;
unsigned readAheadBuffers;
unsigned writeBackBuffers;
unsigned writeBackBuckets;
Buffer memBuffer;
unsigned memBufferSize;
HandlerArgs handlerArgs;
bool _partiallyFilled; // the pool is partially filled (it contains undefined values)
TValue undefinedValue; // value to represent undefined (unwritten) entries
protected:
unsigned _lastPageNo;
unsigned _lastPageSize;
int listeners;
ReadHandler *reader;
WriteHandler *writer;
public:
//////////////////////////////////////////////////////////////////////////////
// public iterator types
typedef SizeType size_type;
Pool(const PoolParameters &_conf = PoolParameters()):
file(NULL)
{
_init(_conf);
_setSize(0);
}
Pool(HandlerArgs const &args, PoolParameters const &_conf = PoolParameters()):
file(NULL),
handlerArgs(args)
{
_init(_conf);
_setSize(0);
}
template < typename TInput, typename TPipeSpec >
Pool(Pipe<TInput, TPipeSpec> &, const PoolParameters &_conf = PoolParameters()):
file(NULL)
{
_init(_conf);
_setSize(0);
}
template < typename TInput, typename TPipeSpec >
Pool(Pipe<TInput, TPipeSpec> &, HandlerArgs const &args, PoolParameters const &_conf = PoolParameters()):
file(NULL),
handlerArgs(args)
{
_init(_conf);
_setSize(0);
}
Pool(File &_file, const PoolParameters &_conf = PoolParameters()):
file(_file)
{
_init(_conf);
_ownFile = false;
_temporary = false;
memBufferSize = 0;
_setSize(::seqan::size(file) / sizeof(TValue));
}
Pool(const char *fileName, const PoolParameters &_conf = PoolParameters())
{
_init(_conf);
_temporary = false;
memBufferSize = 0;
_ownFile = open(file, fileName);
if (_ownFile)
_setSize(::seqan::size(file) / sizeof(TValue));
else
_setSize(0);
}
~Pool() {
endRead();
endWrite();
if (_temporary)
clear();
else
if (_ownFile) close(file);
}
inline void clear() {
resize(0);
}
inline size_type size() const {
return _size;
}
// this is not a real resize
void resize(size_type _newSize) {
typedef typename Size<File>::Type TFSize;
if (_newSize == _size) return;
_freeHandlers(); // if you forgot to call endRead/endWrite we have no trouble
if (_temporary && _ownFile) {
if (_size != 0) {
if (memBuffer.begin)
freePage(memBuffer, *this);
else {
close(file);
SEQAN_PROSUB(SEQAN_PROIOVOLUME, (_proFloat)((TFSize)_size * (TFSize)sizeof(TValue)));
}
}
if (_newSize != 0) {
if (_newSize <= (size_type)memBufferSize)
allocPage(memBuffer, _newSize, *this);
else {
openTemp(file);
SEQAN_PROADD(SEQAN_PROIOVOLUME, (_proFloat)((TFSize)_newSize * (TFSize)sizeof(TValue)));
}
}
}
_setSize(_newSize);
}
//////////////////////////////////////////////////////////////////////////////
// auto-disposal interface (deprecated)
inline void addListener() {
if (!listeners) return;
++listeners;
}
inline void delListener() {
if (!listeners) return;
if (--listeners == 0) {
clear();
}
}
//////////////////////////////////////////////////////////////////////////////
// queue interface
inline Type const & front() const {
return reader->front();
}
inline Type const & operator*() const {
return reader->front();
}
inline void pop() {
reader->pop();
}
inline Pool& operator++() {
reader->pop();
return *this;
}
inline void pop(Type &Ref_) {
reader->pop(Ref_);
}
inline void push(Type const &Val_) {
writer->push(Val_);
}
inline bool eof() {
if (reader) return reader->eof();
if (writer) return writer->eof();
return true;
}
//////////////////////////////////////////////////////////////////////////////
// flow control
bool beginWrite() {
_freeHandlers(); // if you forgot to call endRead/endWrite we have no trouble
return (
(writer = new WriteHandler(*this)) &&
writer->begin());
}
bool endWrite() {
if (writer) {
writer->end();
writer->process();
}
delete writer;
writer = NULL;
return true;
}
bool beginRead() {
_freeHandlers(); // if you forgot to call endRead/endWrite we have no trouble
return (
(reader = new ReadHandler(*this)) &&
reader->begin());
}
bool endRead() {
if (reader) reader->end();
delete reader;
reader = NULL;
delListener();
return true;
}
inline unsigned pages() const {
return _pages;
}
inline unsigned pages(unsigned pageSize__) const {
return enclosingBlocks(_size, (unsigned)pageSize__);
}
// used by buffer handlers ...
inline unsigned dataSize(unsigned pageNo__) const {
return (pageNo__ != _lastPageNo)? pageSize: _lastPageSize;
}
// used by buffer handlers with variable PageSize ...
inline unsigned dataSize(unsigned pageNo__, unsigned pageSize__) const {
return (pageNo__ != _size / pageSize__)? pageSize__: _size % pageSize__;
}
protected:
inline void _freeHandlers() {
if (reader) reader->end();
if (writer) writer->end();
delete reader;
delete writer;
reader = NULL;
writer = NULL;
}
inline void _setSize(size_type _newSize) {
_size = _newSize;
_pages = enclosingBlocks(_size, (unsigned)pageSize);
_lastPageNo = _size / pageSize;
_lastPageSize = _size % pageSize;
}
void _init(PoolParameters _conf = PoolParameters())
{
_conf.absolutize(16*1024/*sectorSize(file)*/, (Type*)NULL);
memBufferSize = _conf.memBufferSize;
pageSize = _conf.pageSize;
bucketBufferSize = _conf.bucketBufferSize;
readAheadBuffers = _conf.readAheadBuffers;
writeBackBuffers = _conf.writeBackBuffers;
writeBackBuckets = _conf.writeBackBuffers;
listeners = 0;
reader = NULL;
writer = NULL;
_ownFile = true;
_temporary = true;
}
};
template < typename TValue, typename TSpec, typename TIteratorSpec>
struct Iterator< Pool< TValue, TSpec > const, TIteratorSpec> {
typedef IPipeIterator< Pool< TValue, TSpec > > Type;
};
template < typename TValue, typename TSpec, typename TIteratorSpec>
struct Iterator< Pool< TValue, TSpec >, TIteratorSpec> {
typedef OPipeIterator< Pool< TValue, TSpec > > Type;
};
template < typename TValue, typename TSpec >
OPipeIterator< Pool< TValue, TSpec > >
begin(Pool< TValue, TSpec > &pool) {
return OPipeIterator< Pool< TValue, TSpec > >(pool);
}
template < typename TValue, typename TSpec >
OPipeIterator< Pool< TValue, TSpec > >
end(Pool< TValue, TSpec > &/*pool*/) {
return OPipeIterator< Pool< TValue, TSpec > >();
}
//////////////////////////////////////////////////////////////////////////////
// a pool is a queue-like container for a large amount of data
// in contrast to a queue you can read the whole content more than once
// but can't pop directly after a push
// instead, access to a pool looks like the following:
//
// 1. resize(<new size>)
// 2. beginWrite()
// 3. push(a), push(b), push(c) ...
// 4. endWrite()
// (5. do something else)
// 6. beginRead()
// 7. pop(a), pop(b), pop(c) ...
// 8. endRead()
// (9. clear() - if you want to save memory and refill the pool later)
//
// you can repeat steps 2-4 and 6-8 independently as often as you like
//template < typename TValue, typename TFile = File<> >
//struct Pool: public Pool< TValue, PoolSpec< PoolConfig< TFile > > >
//{
// typedef TValue Type;
// typedef TFile File;
// typedef typename Size<TFile>::Type SizeType;
//};
// seqan namespace traits
template < typename TValue, typename TSpec >
struct Value< Pool< TValue, TSpec > > {
typedef TValue Type;
};
template < typename TValue, typename TSpec >
struct Size< Pool< TValue, TSpec > > {
typedef typename Pool< TValue, TSpec >::SizeType Type;
};
template < typename TValue, typename TSpec >
struct Position< Pool< TValue, TSpec > > {
typedef typename Size<Pool< TValue, TSpec > >::Type Type;
};
template < typename TValue, typename TSpec >
struct Difference< Pool< TValue, TSpec > > {
typedef typename MakeSigned_<typename Size<Pool< TValue, TSpec > >::Type>::Type Type;
};
///.Function.clear.param.object.type:Class.Pool
template < typename TValue, typename TSpec >
inline void clear(Pool<TValue, TSpec> &me)
{
return me.clear();
}
// deprecated
template < typename TValue, typename TSpec >
inline typename Size< Pool<TValue, TSpec> >::Type
size(Pool<TValue, TSpec> const &me)
{
return me.size();
}
///.Function.length.param.object.type:Class.Pool
template < typename TValue, typename TSpec >
inline typename Size< Pool<TValue, TSpec> >::Type
length(Pool<TValue, TSpec> const &me)
{
return me.size();
}
///.Function.resize.param.object.type:Class.Pool
template < typename TValue, typename TSpec, typename TSize >
inline TSize resize(Pool<TValue, TSpec> &me, TSize new_length)
{
me.resize(new_length);
return me.size();
}
///.Function.Pipelining#front.param.object.type:Class.Pool
template < typename TValue, typename TSpec >
inline typename Value< Pool<TValue, TSpec> >::Type const & front(Pool<TValue, TSpec> &me) {
return me.front();
}
///.Function.pop.param.object.type:Class.Pool
template < typename TValue, typename TSpec >
inline void pop(Pool<TValue, TSpec> &me) {
me.pop();
}
template < typename TValue, typename TSpec >
inline void pop(Pool<TValue, TSpec> &me, TValue &Ref_) {
me.pop(Ref_);
}
/**
.Function.push:
..cat:Pipelining
..summary:Appends an item at the end of an input stream.
..signature:push(object, val)
..param.object:A push-passive pipeline module.
...type:Class.Pool
..param.val:Item to be pushed.
..remarks:@Function.push@ can only be called within a write process surrounded by @Function.beginWrite@ and @Function.endWrite@.
..include:seqan/pipe.h
*/
template < typename TValue, typename TSpec >
inline void push(Pool<TValue, TSpec> &me, TValue const &Val_) {
me.push(Val_);
}
template < typename TValue, typename TSpec >
::std::ostream& operator<<(::std::ostream &out, Pool<TValue, TSpec> &p) {
beginRead(p);
while (!eof(p)) {
out << front(p) << ::std::endl;
pop(p);
}
endRead(p);
return out;
}
// the pipe interface of pool classes
//namespace SEQAN_NAMESPACE_PIPELINING
//{
//template < typename TValue, typename TSpec >
//struct Value< Pool< TValue, TSpec > >
//{
// typedef TValue Type;
//};
//template < typename TValue, typename TSpec >
//struct Size< Pool< TValue, TSpec > >
//{
// typedef typename Size< TFile >::Type Type;
//};
template < typename TValue, typename TSpec >
inline bool control(Pool< TValue, TSpec > &me, ControlEof const &) {
return me.eof();
}
template < typename TValue, typename TSpec >
inline bool control(Pool< TValue, TSpec > &me, ControlEos const &) {
return me.eof();
}
template < typename TValue, typename TSpec >
inline bool control(Pool< TValue, TSpec > &me, ControlClear const &) {
me.clear();
return true;
}
template < typename TValue, typename TSpec >
inline bool control(Pool< TValue, TSpec > &me, ControlBeginRead const &) {
return me.beginRead();
}
template < typename TValue, typename TSpec >
inline bool control(Pool< TValue, TSpec > &me, ControlEndRead const &) {
return me.endRead();
}
/**
.Function.beginWrite:
..cat:Pipelining
..summary:Initiates a write process.
..signature:beginWrite(object)
..param.object:A push-passive pipeline module.
...type:Class.Pool
..returns:A $bool$ which is $true$ on success.
..remarks:$beginWrite$ prepares a @Class.Pool@ for succeeding writes.
..remarks:A write process must be terminated with @Function.endWrite@. Nested write processes are not allowed.
..see:Function.endWrite
..include:seqan/pipe.h
*/
template < typename TValue, typename TSpec >
inline bool beginWrite(Pool< TValue, TSpec > &me) {
SEQAN_CHECKPOINT
return me.beginWrite();
}
/**
.Function.endWrite:
..cat:Pipelining
..summary:Terminates a write process.
..signature:endWrite(object)
..param.object:A push-passive pipeline module.
...type:Class.Pool
..returns:A $bool$ which is $true$ on success.
..remarks:$endWrite$ closes the input stream and frees resources possibly allocated by @Function.beginWrite@.
..see:Function.beginWrite
..include:seqan/pipe.h
*/
template < typename TValue, typename TSpec >
inline bool endWrite(Pool< TValue, TSpec > &me) {
return me.endWrite();
}
///.Function.atEnd.param.iterator.type:Class.Pool
template < typename TValue, typename TSpec >
inline bool eof(Pool< TValue, TSpec > &me) {
SEQAN_CHECKPOINT
return control(me, ControlEof());
}
template < typename TValue, typename TSpec >
inline bool beginRead(Pool< TValue, TSpec > &me) {
SEQAN_CHECKPOINT
return control(me, ControlBeginRead());
}
template < typename TValue, typename TSpec >
inline bool endRead(Pool< TValue, TSpec > &me) {
SEQAN_CHECKPOINT
return control(me, ControlEndRead());
}
//}
// pipe/pool -> pool
template < typename TValue,
typename TSpec,
typename TSource >
inline bool append(Pool<TValue, TSpec> &dest, TSource &src) {
typename Size<TSource>::Type leftToRead = length(src);
if (!beginRead(src)) return false;
while (leftToRead) {
push(dest, *src);
++src;
--leftToRead;
}
endRead(src);
return true;
}
// string -> pool
template < typename TValue,
typename TSpec,
typename TStringSpec >
inline bool append(Pool<TValue, TSpec> &dest, String<TValue, TStringSpec> &src) {
typedef typename Iterator< String<TValue, TStringSpec> const, Standard >::Type TIter;
TIter _cur = begin(src, Standard());
TIter _end = end(src, Standard());
while (_cur != _end) {
push(dest, *_cur);
++_cur;
}
return true;
}
///.Function.assign.param.target.type:Class.Pool
template < typename TValue,
typename TSpec,
typename TSource >
inline bool assign(Pool<TValue, TSpec> &dest, TSource &src) {
typename Size<TSource>::Type _size = length(src);
resize(dest, _size);
return beginWrite(dest) && append(dest, src) && endWrite(dest);
}
template < typename TValue,
typename TSpec,
typename TSource >
inline bool operator<<(Pool<TValue, TSpec> &dest, TSource &src) {
return assign(dest, src);
}
///.Function.assign.param.source.type:Class.Pool
// pool -> string
template < typename TValue1,
typename TStringSpec,
typename TValue2,
typename TSpec >
inline bool assign(String<TValue1, TStringSpec> &dest, Pool<TValue2, TSpec> &src) {
typedef typename Iterator< String<TValue1, TStringSpec>, Standard >::Type TIter;
typename Size< String<TValue1, TStringSpec> >::Type _size = length(src);
resize(dest, _size);
if (!beginRead(src)) return false;
TIter _cur = begin(dest, Standard());
TIter _end = end(dest, Standard());
while (_cur != _end) {
*_cur = *src;
++_cur;
++src;
}
endRead(src);
return true;
}
template < typename TValue1,
typename TStringSpec,
typename TValue2,
typename TSpec >
inline bool operator<<(String<TValue1, TStringSpec> &dest, Pool<TValue2, TSpec> &src) {
return assign(dest, src);
}
}
#endif
|