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 1263 1264 1265 1266 1267 1268 1269 1270
|
// ==========================================================================
// SeqAn - The Library for Sequence Analysis
// ==========================================================================
// Copyright (c) 2006-2018, 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: Andreas Gogol-Döring <andreas.doering@mdc-berlin.de>
// ==========================================================================
// Functions for destructing and several ways of constructing (e.g. copy,
// move) values or arrays of values.
// ==========================================================================
// TODO(holtgrew): Order of parameters should be (target1, target2, ..., source1, source2, ...).
// TODO(holtgrew): Can we maybe replace at least part with http://www.cplusplus.com/reference/std/memory/?
// TODO(holtgrew): The metafunction should go into the alphabet submodule, the functions into the sequence/string module.
#include <new>
#ifndef SEQAN_INCLUDE_SEQAN_BASIC_ARRAY_CONSTRUCT_DESTRUCT_H_
#define SEQAN_INCLUDE_SEQAN_BASIC_ARRAY_CONSTRUCT_DESTRUCT_H_
namespace seqan {
// ============================================================================
// Forwards
// ============================================================================
// ============================================================================
// Tags, Classes, Enums
// ============================================================================
// ============================================================================
// Metafunctions
// ============================================================================
// ----------------------------------------------------------------------------
// Metafunction IsSimple
// ----------------------------------------------------------------------------
// TODO(holtgrew): This has to go to alphabet sub module, storage section, adaption.
/*!
* @mfn IsSimple
* @headerfile <seqan/basic.h>
* @brief Tests type to be simple.
*
* @signature IsSimple<T>::Type;
*
* @tparam T Type that is tested.
*
* @return Type Either True or False, depending on T being a "POD" type.
*
* A simple type is a type that does not need constructors to be created, a destructor to be destroyed, and copy
* assignment operators or copy constructors to be copied. All POD ("plain old data") types are simple, but some
* non-POD types could be simple too, e.g. some specializations of SimpleType.
*
* @see SimpleType
*/
template <typename T>
struct IsSimple_
{
typedef False Type;
enum { VALUE = 0 };
};
// ----------------------------------------------------------------------------
// Metafunction IsSimple
// ----------------------------------------------------------------------------
template <> struct IsSimple_<bool> { typedef True Type; };
template <> struct IsSimple_<char> { typedef True Type; };
template <> struct IsSimple_<unsigned char> { typedef True Type; };
template <> struct IsSimple_<unsigned short> { typedef True Type; };
template <> struct IsSimple_<unsigned int> { typedef True Type; };
template <> struct IsSimple_<unsigned long> { typedef True Type; };
template <> struct IsSimple_<signed char> { typedef True Type; };
template <> struct IsSimple_<signed short> { typedef True Type; };
template <> struct IsSimple_<signed int> { typedef True Type; };
template <> struct IsSimple_<signed long> { typedef True Type; };
template <> struct IsSimple_<float> { typedef True Type; };
template <> struct IsSimple_<double> { typedef True Type; };
template <> struct IsSimple_<long double> { typedef True Type; };
template <typename T>
struct IsSimple
{
typedef typename IsSimple_<T>::Type Type;
enum { VALUE = Type::VALUE };
};
// user defined types (re-specializations are allowed here)
template <> struct IsSimple<wchar_t> { typedef True Type; };
template <> struct IsSimple<int64_t> { typedef True Type; };
template <> struct IsSimple<uint64_t> { typedef True Type; };
template <typename T>
struct IsSimple<T const> : public IsSimple<T> {};
// ----------------------------------------------------------------------------
// Metafunction Value
// ----------------------------------------------------------------------------
// TODO(holtgrew): This should probably to into sequence module along with this header.
template <typename TValue>
struct Value<TValue *>
{
typedef TValue Type;
};
template <typename TValue>
struct Value<TValue * const>
{
typedef TValue Type;
};
// TODO(holtgrew): Is this still a problem with dropped 2003 support of VC++?
//The next two metafunctions dont work in VC++ due to a compiler bug.
//(the default implementation in common_type.h is called instead)
//work-around: convert arrays to pointers.
template <typename TValue, size_t SIZE>
struct Value<TValue [SIZE]>
{
typedef TValue Type;
};
template <typename TValue, size_t SIZE>
struct Value<TValue const [SIZE]>
{
typedef TValue Type;
};
// ----------------------------------------------------------------------------
// Metafunction Reference
// ----------------------------------------------------------------------------
// TODO(holtgrew): This should probably to into sequence module along with this header.
template <typename TValue>
struct Reference<TValue *>
{
typedef TValue & Type;
};
template <typename TValue>
struct Reference<TValue * const>
{
typedef TValue const & Type;
};
// ============================================================================
// Functions
// ============================================================================
// ----------------------------------------------------------------------------
// Function value() for pointers.
// ----------------------------------------------------------------------------
// TODO(holtgrew): This has to go to iterator module, adaption of pointers to iterators.
template <typename T>
inline T &
value(T * me)
{
return *me;
}
// ----------------------------------------------------------------------------
// Function getValue() for pointers.
// ----------------------------------------------------------------------------
// TODO(holtgrew): This has to go to iterator module, adaption of pointers to iterators.
template <typename T>
inline T &
getValue(T * me)
{
return value(me);
}
// TODO(holtgrew): All of the helper structs could be replaced by global functions.
// TODO(holtgrew): First, the generic versions for iterators are defined. Below are the versions for pointers.
// ----------------------------------------------------------------------------
// Function valueConstruct() using iterators
// ----------------------------------------------------------------------------
/*!
* @fn valueConstruct
* @headerfile <seqan/basic.h>
* @brief Constructs an object at specified position.
*
* @signature void valueConstruct(iterator [, param [, move_tag]]);
*
* @param[in,out] iterator Pointer or iterator to position where the object should be constructed.
* @param[in] param Parameter that is forwarded to constructor.
* @param[in] moveTag Instance of the move tag. If the tag is specified, it is forwarded to the constructor,
* so the constructed object must support move construction.
*
* The type of the destructed object is the value type of <tt>iterator</tt>.
*/
// Helper code for constructing values behind iterators that do not return
// proxies from their value() functions but references.
struct ValueConstructor_
{
template <typename TIterator>
static inline void
construct(TIterator it)
{
typedef typename Value<TIterator>::Type TValue;
typedef typename RemoveConst<TValue>::Type TNonConstValue;
new( (void*) & value(it) ) TNonConstValue;
}
template <typename TIterator, typename TParam>
static inline void
construct(TIterator it,
TParam && param_)
{
typedef typename Value<TIterator>::Type TValue;
typedef typename RemoveConst<TValue>::Type TNonConstValue;
new( (void*) & value(it) ) TNonConstValue(std::forward<TParam>(param_));
}
};
// Helper code for constructing values behind iterators that return proxies
// from their value() function.
//
// TODO(holtgrew): These implementations are empty and to be overwritten. Should we have dynamic/static asserstions here?
struct ValueConstructorProxy_
{
template <typename TIterator>
static inline void construct(TIterator) {}
template <typename TIterator, typename TParam>
static inline void construct(TIterator, TParam &&) {}
};
template <typename TIterator>
inline void
valueConstruct(TIterator it)
{
typedef typename IfC<
IsSameType<
typename Value<TIterator>::Type &,
typename Reference<TIterator>::Type
>::VALUE,
// THEN
ValueConstructor_, // true, types are equal
// ELSE
ValueConstructorProxy_ // false, types differ -> value() returns a proxy
>::Type TConstructor;
TConstructor::construct(it);
}
template <typename TIterator, typename TParam>
inline void
valueConstruct(TIterator it,
TParam && param_)
{
typedef typename IfC<
IsSameType<
typename Value<TIterator>::Type &,
typename Reference<TIterator>::Type
>::VALUE,
// THEN
ValueConstructor_, // true, types are equal
// ELSE
ValueConstructorProxy_ // false, types differ -> value() returns a proxy
>::Type TConstructor;
TConstructor::construct(it, std::forward<TParam>(param_));
}
// ----------------------------------------------------------------------------
// Function valueDestruct() using iterators
// ----------------------------------------------------------------------------
// Helper code for destructing values behind iterators that do not return
// proxies from their value() function but references.
struct ValueDestructor_
{
template <typename TValue>
static inline void
_destruct(TValue * p)
{
p->~TValue();
}
template <typename TIterator>
static inline void
destruct(TIterator it)
{
_destruct(&value(it));
}
};
// Helper code for destructing values behind iterators that return proxies
// from their value() function.
//
// TODO(holtgrew): These implementations are empty and to be overwritten. Should we have dynamic/static asserstions here?
struct ValueDestructorProxy_
{
template <typename TIterator>
static inline void destruct(TIterator) {}
};
/*!
* @fn valueDestruct
* @headerfile <seqan/basic.h>
* @brief Destroys an object at specified position.
*
* @signature void valueDestruct(iterator);
*
* @param[in,out] iterator Pointer or iterator to position where the object should be destructed.
*
* The type of the constructed object is the value type of <tt>iterator</tt>.
*/
template <typename TIterator>
inline void
valueDestruct(TIterator it)
{
typedef typename IfC<
IsSameType<
typename Value<TIterator>::Type &,
typename Reference<TIterator>::Type
>::VALUE,
// THEN
ValueDestructor_, // true, types are equal
// ELSE
ValueDestructorProxy_ // false, types differ -> value() returns a proxy
>::Type TDestructor;
TDestructor::destruct(it);
}
// ----------------------------------------------------------------------------
// Function arrayConstruct() using iterators
// ----------------------------------------------------------------------------
/*!
* @fn arrayConstruct
* @headerfile <seqan/basic.h>
* @brief Construct objects in a given memory buffer.
*
* @signature void arrayConstruct(begin, end[, value]);
*
* @param[in] begin Iterator to the begin of the range that is to be constructed.
* @param[in] end Iterator behind the end of the range.
* @param[in] value Argument that is forwarded to the constructor. An appropriate constructor is required. If
* <tt>value</tt> is not specified, the default constructor is used.
*
* The type of the constructed Objects is the value type of <tt>begin</tt> and <tt>end</tt>.
*/
// NOTE(holtgrew): Of course, it does not make sense to declare this in a move version!
template<typename TIterator1, typename TIterator2>
inline void
_arrayConstructDefault(TIterator1 begin_,
TIterator2 end_)
{
while (begin_ != end_)
{
valueConstruct(begin_);
++begin_;
}
}
template<typename TIterator1, typename TIterator2>
inline void
arrayConstruct(TIterator1 begin_,
TIterator2 end_)
{
_arrayConstructDefault(begin_, end_);
}
template<typename TIterator1, typename TIterator2, typename TParam>
inline void
_arrayConstructDefault(TIterator1 begin_,
TIterator2 end_,
TParam const & param_)
{
while (begin_ != end_)
{
valueConstruct(begin_, param_);
++begin_;
}
}
template<typename TIterator1, typename TIterator2, typename TParam>
inline void
arrayConstruct(TIterator1 begin_,
TIterator2 end_,
TParam const & param_)
{
_arrayConstructDefault(begin_, end_, param_);
}
// ----------------------------------------------------------------------------
// Function arrayConstructCopy() using iterators
// ----------------------------------------------------------------------------
/*!
* @fn arrayConstructCopy
* @headerfile <seqan/basic.h>
* @brief Copy constructs an array of objects into in a given memory buffer.
*
* @signature void arrayConstructCopy(sourceBegin, sourceEnd, target);
*
* @param[in] sourceBegin Iterator to the first element of the source range.
* @param[in] sourceEnd Iterator behind the last element of the source range. <tt>sourceEnd</tt> should have the same
* type as <tt>sourceBegin</tt>.
* @param[in] target Pointer to the memory block the new objects will be constructed in. The type of <tt>target</tt>
* specifies the type of the constructed objects: If <tt>T*</tt> is the type of <tt>target</tt>, then
* the function constructs objects of type <tt>T</tt>. The memory buffer should be large enough to
* store <tt>sourceEnd</tt> - <tt>sourceBegin</tt> objects. An appropriate (copy-) constructor that
* constructs an target objects given a source object is required.
*/
template<typename TTarget, typename TSource1, typename TSource2>
inline void
_arrayConstructCopyDefault(TSource1 source_begin,
TSource2 source_end,
TTarget target_begin)
{
while (source_begin != source_end)
{
valueConstruct(target_begin, *source_begin);
++source_begin;
++target_begin;
}
}
template<typename TTarget, typename TSource1, typename TSource2>
inline void
arrayConstructCopy(TSource1 source_begin,
TSource2 source_end,
TTarget target_begin)
{
_arrayConstructCopyDefault(source_begin, source_end, target_begin);
}
// ----------------------------------------------------------------------------
// Function arrayConstructMove() using iterators
// ----------------------------------------------------------------------------
/*!
* @fn arrayConstructMove
* @headerfile <seqan/basic.h>
* @brief Move constructs an array of objects into in a given memory buffer.
*
* @signature void arrayConstructMove(sourceBegin, sourceEnd, target);
*
* @param[in] sourceEnd Iterator behind the last element of the source range. <tt>sourceEnd</tt> should have the same
* type as <tt>sourceBegin</tt>.
* @param[in] sourceBegin Iterator to the first element of the source range.
* @param[in] target Pointer to the memory block the new objects will be constructed in. The type of <tt>target</tt>
* specifies the type of the constructed objects: If <tt>T*</tt> is the type of <tt>target</tt>, then
* the function constructs objects of type <tt>T</tt>. The memory buffer should be large enough to
* store <tt>sourceEnd</tt> - <tt>sourceBegin</tt> objects. An appropriate move constructor that
* constructs an target objects given a source object is required.
*/
template<typename TTarget, typename TSource1, typename TSource2>
inline void
_arrayConstructMoveDefault(TSource1 source_begin,
TSource2 source_end,
TTarget target_begin)
{
while (source_begin < source_end)
{
// NOTE(holtgrew): Using value() here, used to be getValue() but
// cannot move from const reference or proxy.
// valueConstruct(target_begin, value(source_begin), Move());
// TODO(holtgrew): We need a "has move constructor" metafunction to switch between move/copy constructing before we can use the line here.
valueConstruct(target_begin, std::move<decltype(*source_begin)>(*source_begin));
++source_begin;
++target_begin;
}
}
template<typename TTarget, typename TSource1, typename TSource2>
inline void
arrayConstructMove(TSource1 source_begin,
TSource2 source_end,
TTarget target_begin)
{
_arrayConstructMoveDefault(source_begin, source_end, target_begin);
}
// ----------------------------------------------------------------------------
// Function arrayDestruct() using iterators
// ----------------------------------------------------------------------------
/*!
* @fn arrayDestruct
* @headerfile <seqan/basic.h>
* @brief Destroys an array of objects.
*
* @signature void arrayDestruct(begin, end);
*
* @param[in] begin Iterator to the begin of the range that is to be destructed.
* @param[in] end Iterator behind the end of the range.
*
* This function does not deallocates the memory.
*/
template<typename TIterator1, typename TIterator2>
inline void
_arrayDestructDefault(TIterator1 begin_,
TIterator2 end_)
{
while (begin_ != end_)
{
valueDestruct(begin_);
++begin_;
}
}
template<typename TIterator1, typename TIterator2>
inline void
arrayDestruct(TIterator1 begin_,
TIterator2 end_)
{
_arrayDestructDefault(begin_, end_);
}
// ----------------------------------------------------------------------------
// Function arrayFill() using iterators
// ----------------------------------------------------------------------------
// TODO(holtgrew): What is the advantage over arrayConstruct() with prototype?
/*!
* @fn arrayFill
* @headerfile <seqan/basic.h>
* @brief Assigns one object to each element of a range.
*
* @signature void arrayFill(begin, end, value[, parallelTag]);
*
* @param[in] begin Iterator to the begin of the range that is to be filled.
* @param[in] end Iterator behind the end of the range.
* @param[in] value Argument that is assigned to all <tt>count</tt> objects in <tt>array</tt>.
* @param[in] parallelTag Tag to enable/disable parallelism. Types: Serial, Parallel
*
* All objects <tt>target_begin[0]</tt> to <tt>target_begin[count-1]</tt> are set to <tt>value</tt>.
*/
// TODO(holtgrew): Redirects to fill_n. What are the exact semantics here? Do the array elements have to be initialized already? fill_n uses assignment, not copy construction!
template <typename TIterator, typename TValue>
inline void
arrayFill(TIterator begin_,
TIterator end_,
TValue const & value)
{
std::fill_n(begin_, end_ - begin_, value);
}
template <typename TIterator, typename TValue>
inline void
arrayFill(TIterator begin_,
TIterator end_,
TValue const & value,
Serial)
{
arrayFill(begin_, end_, value);
}
// ----------------------------------------------------------------------------
// Function arrayCopyForward() using iterators
// ----------------------------------------------------------------------------
/*!
* @fn arrayCopyForward
* @headerfile <seqan/basic.h>
* @brief Copies a range of objects into another range of objects starting from the first element.
*
* @signature void arrayCopyForward(sourceBegin, sourceEnd, target);
*
* @param[in] sourceEnd Iterator behind the last element of the source array. <tt>sourceEnd</tt> must have the same type
* as <tt>sourceBegin</tt>.
* @param[in] sourceBegin Iterator to the first element of the source array.
* @param[in] target Iterator to the first element of the target array. The target capacity should be at least as
* long as the source range.
*
* Be careful if source and target range overlap, because in this case some source elements could be accidently
* overwritten before they are copied.
*
* If there is no need for the source elements to persist, consider to use arrayMoveForward instead to improve
* performance.
*/
template<typename TTarget, typename TSource1, typename TSource2>
inline void
_arrayCopyForwardDefault(TSource1 source_begin,
TSource2 source_end,
TTarget target_begin)
{
std::copy(source_begin, source_end, target_begin);
}
template<typename TTarget, typename TSource1, typename TSource2>
inline void
arrayCopyForward(TSource1 source_begin,
TSource2 source_end,
TTarget target_begin)
{
_arrayCopyForwardDefault(source_begin, source_end, target_begin);
}
// ----------------------------------------------------------------------------
// Function arrayCopyBackward() using iterators
// ----------------------------------------------------------------------------
/*!
* @fn arrayCopyBackward
* @headerfile <seqan/basic.h>
* @brief Copies a range of objects into another range of objects starting from the last element.
*
* @signature void arrayCopyBackward(source_begin, source_end, target);
*
* @param[in] sourceBegin Iterator to the first element of the source array.
* @param[in] sourceEnd Iterator behind the last element of the source array. <tt>sourceEnd</tt> must have the same type
* as <tt>source_begin</tt>.
* @param[in] target Iterator to the first element of the target array. The target capacity should be at least as
* long as the source range.
*
* Be careful if source and target range overlap, because in this case some source elements could be accidently
* overwritten before they are moved.
*
* If source and target do not overlap, consider to use the function arrayCopyForward instead that is faster in some
* cases.
*
* If there is no need for the source elements to persist, consider to use arrayMoveBackward instead to improve
* performance.
*
* The semantic of this function's argument <tt>target</tt> differ from the arguments of <tt>std::copy_backward</tt>.
*/
template<typename TTarget, typename TSource1, typename TSource2>
inline void
_arrayCopyBackwardDefault(TSource1 source_begin,
TSource2 source_end,
TTarget target_begin)
{
std::copy_backward(source_begin, source_end, target_begin + (source_end - source_begin));
}
template<typename TTarget, typename TSource1, typename TSource2>
inline void
arrayCopyBackward(TSource1 source_begin,
TSource2 source_end,
TTarget target_begin)
{
_arrayCopyBackwardDefault(source_begin, source_end, target_begin);
}
// ----------------------------------------------------------------------------
// Function arrayCopy() using iterators
// ----------------------------------------------------------------------------
/*!
* @fn arrayCopy
*
* @headerfile <seqan/basic.h>
*
* @brief Copies a range of objects into another range of objects.
*
* @signature void arrayCopy(sourceBegin, sourceEnd, target);
*
* @param[in] sourceEnd Iterator behind the last element of the source range. <tt>sourceEnd</tt> must have the same type
* as <tt>sourceBegin</tt>.
* @param[in] sourceBegin Iterator to the first element of the source range.
* @param[in] target Iterator to the first element of the target range.The target capacity should be at least as long
* as the source range.
*
* If source and target range do not overlap, consider to use arrayCopyForward instead to improve performance.
*
* If there is no need for the source elements to persist, consider to use arrayMoveForward instead to improve
* performance.
*/
template<typename TTarget, typename TSource1, typename TSource2>
inline void arrayCopy(TSource1 source_begin,
TSource2 source_end,
TTarget target_begin)
{
if (target_begin <= source_begin)
arrayCopyForward(source_begin, source_end, target_begin);
else
arrayCopyBackward(source_begin, source_end, target_begin);
}
// ----------------------------------------------------------------------------
// Function arrayMoveForward() using iterators
// ----------------------------------------------------------------------------
/*!
* @fn arrayMoveForward
* @headerfile <seqan/basic.h>
* @brief Moves a range of objects into another range of objects starting from the first element.
*
* @signature void arrayMoveForward(sourceBegin, sourceEnd, target);
*
* @param[in] sourceEnd Iterator behind the last element of the source array. <tt>sourceEnd</tt> must have the same type
* as <tt>sourceBegin</tt>.
* @param[in] sourceBegin Iterator to the first element of the source array.
* @param[in] target Iterator to the first element of the target array. The target capacity should be at least as
* long as the source range.
*
* The function possibly clears (but does not destroy) the source elements. If source elements must persist, consider
* to use arrayCopyForward instead.
*
* Be careful if source and target range overlap, because in this case some source elements could be accidently
* overwritten before they are moved.
*/
template<typename TTarget, typename TSource1, typename TSource2>
inline void
_arrayMoveForwardDefault(TSource1 source_begin,
TSource2 source_end,
TTarget target_begin)
{
std::move(source_begin, source_end, target_begin);
}
template<typename TTarget, typename TSource1, typename TSource2>
inline void
arrayMoveForward(TSource1 source_begin,
TSource2 source_end,
TTarget target_begin)
{
_arrayMoveForwardDefault(source_begin, source_end, target_begin);
}
// ----------------------------------------------------------------------------
// Function arrayMoveBackward() using iterators
// ----------------------------------------------------------------------------
/*!
* @fn arrayMoveBackward
* @headerfile <seqan/basic.h>
* @brief Moves a range of objects into another range of objects starting from the last element.
*
* @signature void arrayMoveBackward(sourceBegin, sourceEnd, target);
*
* @param[in] sourceEnd Iterator behind the last element of the source array. <tt>sourceEnd</tt> must have the same type
* as <tt>sourceBegin</tt>.
* @param[in] sourceBegin Iterator to the first element of the source array.
* @param[in] target Iterator to the first element of the target array.The target capacity should be at least as long
* as the source range.
*
* The function possibly clears (but does not destroy) the source elements. If source elements must persist, consider
* to use arrayCopyBackward instead.
*
* Be careful if source and target range overlap, because in this case some source elements could be accidently
* overwritten before they are moved.
*
* If source and target do not overlap, consider to use the function arrayMoveForward instead that is faster in some
* cases.
*
* The semantic of this function's argument <tt>target</tt> differ from the arguments of <tt>std::copy_backward</tt>.
*/
template<typename TTarget, typename TSource1, typename TSource2>
inline void
_arrayMoveBackwardDefault(TSource1 source_begin,
TSource2 source_end,
TTarget target_begin)
{
std::move_backward(source_begin, source_end, target_begin + (source_end - source_begin));
}
template<typename TTarget, typename TSource1, typename TSource2>
inline void
arrayMoveBackward(TSource1 source_begin,
TSource2 source_end,
TTarget target_begin)
{
_arrayMoveBackwardDefault(source_begin, source_end, target_begin);
}
// ----------------------------------------------------------------------------
// Function arrayMove using iterators
// ----------------------------------------------------------------------------
/*!
* @fn arrayMove
* @headerfile <seqan/basic.h>
* @brief Moves a range of objects into another range of objects.
*
* @signature void arrayMove(sourceBegin, sourceEnd, target);
*
* @param[in] sourceBegin Iterator to the first element of the source range.
* @param[in] sourceEnd Iterator behind the last element of the source range. <tt>sourceEnd</tt> must have the same type
* as <tt>sourceBegin</tt>.
* @param[in] target Iterator to the first element of the target range. The target capacity should be at least as
* long as the source range.
*
* The function possibly clears (but does not destroy) the source elements. If source elements must persist, consider
* to use arrayCopy instead.
*
* If source and target range do not overlap, consider to use arrayMoveForward instead to improve performance.
*
* Don't confuse this function with the standard <tt>move</tt> function that resembles arrayCopy.
*/
template<typename TTarget, typename TSource1, typename TSource2>
inline void
arrayMove(TSource1 source_begin,
TSource2 source_end,
TTarget target_begin)
{
if (target_begin <= source_begin)
arrayMoveForward(source_begin, source_end, target_begin);
else
arrayMoveBackward(source_begin, source_end, target_begin);
}
// ----------------------------------------------------------------------------
// Function arrayClearSpace() using iterators
// ----------------------------------------------------------------------------
/*!
* @fn arrayClearSpace
* @headerfile <seqan/basic.h>
* @brief Destroys the begin of an array and keeps the rest.
*
* @signature void arrayClearSpace(arrBegin, arrLength, keepFrom, moveTo);
*
* @param[in] arrBegin Pointer to the first element of the array.
* @param[in] keepFrom Offset of the first object that will be kept.
* @param[in] arrLength Length of the array.
* @param[in] moveTo Offset the first kept object will get at the end of the function.
*
* The objects <tt>arr[keep_from]</tt> to <tt>arr[arr_length-1]</tt> are moved to the area beginning at positions
* <tt>move_to</tt>. All objects in <tt>arr[0]</tt> to <tt>arr[keep_from-1]</tt> are destroyed. After this function, the
* first <tt>move_to</tt> positions of the array are free and dont contain objects.
*
* The array must have at least enough space to store <tt>arr_length + move_to - keep_from</tt> objects.
*
* The objects from <tt>arr[0]</tt> to <tt>arr[array_length-1]</tt> have to be initialized/constructed, arrays beyond
* <tt>arr[array_length-1]</tt> are assumed not to be constructed. If this assumption is violated, memory might leak.
*/
// TODO(holtgrew): The feature that the range [0, array_begin) is deleted is used nowhere. Can this be removed to simplify behaviour?
template <typename TIterator>
void _arrayClearSpaceDefault(TIterator array_begin,
size_t array_length,
size_t keep_from,
size_t move_to)
{
if (keep_from == array_length) {
// In the simplest case, we only destruct elements.
arrayDestruct(array_begin, array_begin + array_length);
return;
}
// Otherwise, we will perform the destruction & movement.
SEQAN_ASSERT_LT(keep_from, array_length);
if (keep_from == move_to) {
// Case 1: No movement, just destroy elements.
arrayDestruct(array_begin, array_begin + move_to);
return;
} else if (keep_from < move_to) {
// Case 2: Move to the right.
if (array_length > move_to) {
// Case 2a: Moving right of array_length, i.e. we can move a part
// of the objects and have to move-construct the rest.
size_t middle = array_length - (move_to - keep_from);
arrayConstructMove(array_begin + middle, array_begin + array_length, array_begin + array_length);
arrayMove(array_begin + keep_from, array_begin + middle, array_begin + move_to);
arrayDestruct(array_begin, array_begin + move_to);
} else {
// Case 2b: We have to move-construct all target objects.
arrayConstructMove(array_begin + keep_from, array_begin + array_length, array_begin + move_to);
arrayDestruct(array_begin, array_begin + array_length);
}
} else {
// Case 3: Move to the left.
arrayMove(array_begin + keep_from, array_begin + array_length, array_begin + move_to);
arrayDestruct(array_begin, array_begin + move_to);
arrayDestruct(array_begin + array_length - (keep_from - move_to), array_begin + array_length);
}
}
template <typename TIterator>
void arrayClearSpace(TIterator array_begin,
size_t array_length,
size_t keep_from,
size_t move_to)
{
_arrayClearSpaceDefault(array_begin, array_length, keep_from, move_to);
}
// ----------------------------------------------------------------------------
// Function arrayConstruct() using pointers
// ----------------------------------------------------------------------------
template<typename TIterator>
inline void
_arrayConstructPointer(TIterator,
TIterator,
True)
{
//nothing to do
}
template<typename TIterator>
inline void
_arrayConstructPointer(TIterator begin_,
TIterator end_,
False)
{
_arrayConstructDefault(begin_, end_);
}
template <typename TValue>
inline void
arrayConstruct(TValue * begin_,
TValue * end_)
{
_arrayConstructPointer(begin_, end_, typename IsSimple<TValue>::Type() );
}
template <typename TValue, typename TParam>
inline void
_arrayConstructPointer(TValue * begin_,
TValue * end_,
TParam const & param_,
True)
{
arrayFill(begin_, end_, static_cast<TValue>(param_));
}
template <typename TValue, typename TParam>
inline void
_arrayConstructPointer(TValue * begin_,
TValue * end_,
TParam const & param_,
False)
{
_arrayConstructDefault(begin_, end_, param_);
}
template<typename TValue, typename TParam>
inline void
arrayConstruct(TValue * begin_,
TValue * end_,
TParam const & param_)
{
_arrayConstructPointer(begin_, end_, param_, typename IsSimple<TValue>::Type());
}
// ----------------------------------------------------------------------------
// Function arrayConstructCopy() using pointers
// ----------------------------------------------------------------------------
template<typename TValueSource, typename TValueTarget>
inline void
_arrayConstructCopyPointer(TValueSource * source_begin,
TValueSource * source_end,
TValueTarget * target_begin,
True)
{
arrayCopyForward(source_begin, source_end, target_begin);
}
template<typename TValueSource, typename TValueTarget>
inline void
_arrayConstructCopyPointer(TValueSource * source_begin,
TValueSource * source_end,
TValueTarget const* target_begin,
True)
{
arrayCopyForward(source_begin, source_end, const_cast<TValueTarget *>(target_begin));
}
template<typename TValueSource, typename TValueTarget>
inline void
_arrayConstructCopyPointer(TValueSource * source_begin,
TValueSource * source_end,
TValueTarget * target_begin,
False)
{
_arrayConstructCopyDefault(source_begin, source_end, target_begin);
}
template<typename TValueSource, typename TValueTarget>
inline void
arrayConstructCopy(TValueSource * source_begin,
TValueSource * source_end,
TValueTarget * target_begin)
{
_arrayConstructCopyPointer(source_begin, source_end, target_begin, typename IsSimple<TValueTarget>::Type() );
}
// ----------------------------------------------------------------------------
// Function arrayConstructMove() using pointers
// ----------------------------------------------------------------------------
template<typename TValue>
inline void
_arrayConstructMovePointer(TValue * source_begin,
TValue * source_end,
TValue * target_begin,
True)
{
arrayMoveForward(source_begin, source_end, target_begin);
}
template<typename TValue>
inline void
_arrayConstructMovePointer(TValue * source_begin,
TValue * source_end,
TValue * target_begin,
False)
{
_arrayConstructMoveDefault(source_begin, source_end, target_begin);
}
template<typename TValue>
inline void
arrayConstructMove(TValue * source_begin,
TValue * source_end,
TValue * target_begin)
{
_arrayConstructMovePointer(source_begin, source_end, target_begin, typename IsSimple<TValue>::Type() );
}
// ----------------------------------------------------------------------------
// Function arrayDestruct() using pointers
// ----------------------------------------------------------------------------
template<typename TValue>
inline void
_arrayDestructPointer(TValue * /*begin_*/,
TValue * /*end_*/,
True)
{
//do nothing
}
template<typename TValue>
inline void
_arrayDestructPointer(TValue * begin_,
TValue * end_,
False)
{
_arrayDestructDefault(begin_, end_);
}
template<typename TValue>
inline void
arrayDestruct(TValue * begin_,
TValue * end_)
{
_arrayDestructPointer(begin_, end_, typename IsSimple<TValue>::Type() );
}
// ----------------------------------------------------------------------------
// Function arrayFill() using pointers
// ----------------------------------------------------------------------------
// TODO(holtgrew): Missing?
//no specializiation for pointer to simple
// ----------------------------------------------------------------------------
// Function arrayCopyBackward() using pointers
// ----------------------------------------------------------------------------
template<typename TValue>
inline void
_arrayCopyForwardPointer(TValue * source_begin,
TValue * source_end,
TValue * target_begin,
True)
{
std::memmove(target_begin, source_begin, (source_end - source_begin) * sizeof(TValue));
}
template<typename TValue>
inline void
_arrayCopyForwardPointer(TValue * source_begin,
TValue * source_end,
TValue * target_begin,
False)
{
_arrayCopyForwardDefault(source_begin, source_end, target_begin);
}
template<typename TValue>
inline void
arrayCopyForward(TValue * source_begin,
TValue * source_end,
TValue * target_begin)
{
_arrayCopyForwardPointer(source_begin, source_end, target_begin, typename IsSimple<TValue>::Type() );
}
template <typename TValue>
inline void
_arrayCopyBackwardPointer(TValue * source_begin,
TValue * source_end,
TValue * target_begin,
True)
{
std::memmove(target_begin, source_begin, (source_end - source_begin) * sizeof(TValue));
}
template <typename TValue>
inline void
_arrayCopyBackwardPointer(TValue * source_begin,
TValue * source_end,
TValue * target_begin,
False)
{
_arrayCopyBackwardDefault(source_begin, source_end, target_begin);
}
template<typename TValue>
inline void
arrayCopyBackward(TValue * source_begin,
TValue * source_end,
TValue * target_begin)
{
_arrayCopyBackwardPointer(source_begin, source_end, target_begin, typename IsSimple<TValue>::Type() );
}
// ----------------------------------------------------------------------------
// Function arrayMoveBackward() using pointers
// ----------------------------------------------------------------------------
template<typename TValue>
inline void
_arrayMoveForwardPointer(TValue * source_begin,
TValue * source_end,
TValue * target_begin,
True)
{
std::memmove(target_begin, source_begin, (source_end - source_begin) * sizeof(TValue));
}
template<typename TValue>
inline void
_arrayMoveForwardPointer(TValue * source_begin,
TValue * source_end,
TValue * target_begin,
False)
{
_arrayMoveForwardDefault(source_begin, source_end, target_begin);
}
template<typename TValue>
inline void
arrayMoveForward(TValue * source_begin,
TValue * source_end,
TValue * target_begin)
{
_arrayMoveForwardPointer(source_begin, source_end, target_begin, typename IsSimple<TValue>::Type() );
}
template <typename TValue>
inline void
_arrayMoveBackwardPointer(TValue * source_begin,
TValue * source_end,
TValue * target_begin,
True)
{
std::memmove(target_begin, source_begin, (source_end - source_begin) * sizeof(TValue));
}
template <typename TValue>
inline void
_arrayMoveBackwardPointer(TValue * source_begin,
TValue * source_end,
TValue * target_begin,
False)
{
_arrayMoveBackwardDefault(source_begin, source_end, target_begin);
}
template<typename TValue>
inline void
arrayMoveBackward(TValue * source_begin,
TValue * source_end,
TValue * target_begin)
{
_arrayMoveBackwardPointer(source_begin, source_end, target_begin, typename IsSimple<TValue>::Type() );
}
// ----------------------------------------------------------------------------
// Function arrayClearSpace() using pointers
// ----------------------------------------------------------------------------
// clearSpace() on simple type using pointers.
template <typename TValue>
inline void
_arrayClearSpacePointer(TValue * array_begin,
size_t array_length,
size_t keep_from,
size_t move_to,
True const & /*isSimple*/)
{
if (keep_from == move_to) return;
// TODO(holtgrew): arrayCopy is more appropriate here since we are dealing with the IsSimple case.
arrayMove(array_begin + keep_from, array_begin + array_length, array_begin + move_to);
}
// clearSpace() on non-simple type using pointers.
template <typename TValue>
inline void
_arrayClearSpacePointer(TValue * array_begin,
size_t array_length,
size_t keep_from,
size_t move_to,
False const & /*isSimple*/)
{
_arrayClearSpaceDefault(array_begin, array_length, keep_from, move_to);
}
template <typename TValue>
void arrayClearSpace(TValue * array_begin,
size_t array_length,
size_t keep_from,
size_t move_to)
{
_arrayClearSpacePointer(array_begin, array_length, keep_from, move_to, typename IsSimple<TValue>::Type());
}
} // namespace seqan
#endif // #ifndef SEQAN_INCLUDE_SEQAN_BASIC_ARRAY_CONSTRUCT_DESTRUCT_H_
|