-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathconcatenated_sequences.hpp
1536 lines (1419 loc) · 55.5 KB
/
concatenated_sequences.hpp
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
// -----------------------------------------------------------------------------------------------------
// Copyright (c) 2006-2021, Knut Reinert & Freie Universität Berlin
// Copyright (c) 2016-2021, Knut Reinert & MPI für molekulare Genetik
// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
// shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
// -----------------------------------------------------------------------------------------------------
/*!\file
* \brief Provides seqan3::concatenated_sequences.
* \author Hannes Hauswedell <hannes.hauswedell AT fu-berlin.de>
*/
#pragma once
#include <seqan3/std/iterator>
#include <seqan3/std/ranges>
#include <type_traits>
#include <vector>
#include <seqan3/core/concept/cereal.hpp>
#include <seqan3/utility/container/concept.hpp>
#include <seqan3/utility/views/repeat_n.hpp>
#include <seqan3/utility/views/slice.hpp>
#if SEQAN3_WITH_CEREAL
#include <cereal/types/vector.hpp>
#endif
namespace seqan3::detail
{
//!\brief Function object for seqan3::detail::as_const.
//!\ingroup alphabet_container
struct as_const_fn
{
//!\brief Operator that returns rvalues as rvalues.
template <typename t>
t operator()(t const && arg) const
{
return std::move(arg);
}
//!\brief Operator that returns lvalue references as lvalue-to-const-references.
template <typename t>
t const & operator()(t const & arg) const
{
return arg;
}
};
/*!\brief A view that provides only `const &` to elements of the underlying range.
* \tparam urng_t The type of the range being processed. See below for requirements. [template parameter is
* omitted in pipe notation]
* \param[in] urange The range being processed. [parameter is omitted in pipe notation]
* \returns A range of `const`-protected elements.
* \ingroup alphabet_container
*
* \details
*
* \header_file{seqan3/alphabet/container/concatenated_sequences.hpp}
*
* ### View properties
*
*
* | Concepts and traits | `urng_t` (underlying range type) | `rrng_t` (returned range type) |
* |----------------------------------|:-------------------------------------:|:--------------------------------------------------:|
* | std::ranges::input_range | *required* | *preserved* |
* | std::ranges::forward_range | | *preserved* |
* | std::ranges::bidirectional_range | | *preserved* |
* | std::ranges::random_access_range | | *preserved* |
* | std::ranges::contiguous_range | | *preserved* |
* | | | |
* | std::ranges::viewable_range | *required* | *guaranteed* |
* | std::ranges::view | | *guaranteed* |
* | std::ranges::sized_range | | *preserved* |
* | std::ranges::common_range | | *preserved* |
* | std::ranges::output_range | | *lost* |
* | seqan3::const_iterable_range | | *preserved* |
* | std::semiregular | | *preserved* |
* | | | |
* | std::ranges::range_reference_t | | `t &` -> `t const &` but `t` -> `t` |
*
* See the \link views views submodule documentation \endlink for detailed descriptions of the view properties.
*
* \hideinitializer
*/
inline constexpr auto as_const = std::views::transform(seqan3::detail::as_const_fn{});
/*!\brief The reference type of seqan3::concatenated_sequences.
* \ingroup alphabet_container
* \tparam value_type The value_type of the seqan3::concatenated_sequences.
* \tparam is_const_ref Reference type or const reference type.
*
* \details
*
* A light-weight type that inherits from the returned type of the seqan3::views::slice adaptor (std::span, std::ranges::subrange, ...),
* but additionally provides implicit convertibility to the `value_type`. This is needed so that `value_type` and
* `reference` type of seqan3::concatenated_sequences satisfy std::common_reference_with.
*
* The const version of this type additionally ensures deep constness to maintain container-like behaviour.
*/
template <typename value_type, bool const_>
struct concatenated_sequences_reference_proxy :
public std::conditional_t<const_,
decltype(std::declval<value_type const &>() | detail::as_const | views::slice(0,1)),
decltype(std::declval<value_type &>() | views::slice(0,1))>
{
//!\brief The base type.
using base_t =
std::conditional_t<const_,
decltype(std::declval<value_type const &>() | detail::as_const | views::slice(0,1)),
decltype(std::declval<value_type &>() | views::slice(0,1))>;
//!\brief Inherit the base type's constructors.
using base_t::base_t;
//!\brief Construct from base type.
concatenated_sequences_reference_proxy(base_t && rhs) : base_t{std::move(rhs)} {}
//!\brief Implicitly convert to the `value_type` of seqan3::concatenated_sequences.
operator value_type() const
{
value_type ret;
ret.resize(std::ranges::size(*this));
std::ranges::copy(*this, std::ranges::begin(ret));
return ret;
}
};
} // namespace seqan3::detail
namespace seqan3
{
/*!\brief Container that stores sequences concatenated internally.
* \tparam inner_type The type of sequences that will be stored. Must satisfy seqan3::reservible_container.
* \tparam data_delimiters_type A container that stores the begin/end positions in the inner_type. Must be
* seqan3::reservible_container and have inner_type's size_type as value_type.
* \implements seqan3::cerealisable
* \implements seqan3::reservible_container
* \ingroup alphabet_container
*
* This class may be used whenever you would usually use `std::vector<std::vector<some_alphabet>>` or
* `std::vector<std::string>`, i.e. whenever you have a collection of sequences. It is the spiritual successor of
* the `StringSet<TString, Owner<ConcatDirect>>` from SeqAn2.
*
* It saves all of the member sequences inside one concatenated sequence internally. If you access an element,
* you instead get a view on the internal string as a proxy. This has the following
* advantages:
*
* * Better cache locality when parsing the sequences linearly (and often also on random access).
* * Constant time access to the concatenation of the sequences via concat().
* * This access is also writable so that certain transformations can be done globally, instead of element-wise.
* * Also direct access to the delimiters via raw_data() [this is used by some algorithms].
*
* The disadvantages are:
*
* * Slower inserts and erases because the entire concatenation might have to be copied.
* * No emplace operations.
* * Modifying elements is limited to operations on elements of that element, i.e. you can change a character,
* but you can't assign a new member sequence to an existing position.
*
* ### Example
*
* \include test/snippet/alphabet/container/concatenated_sequences.cpp
*
* ### Exceptions
*
* Whenever a strong exception guarantee is given for this class, it presumes that
* `std::is_nothrow_move_constructible<typename inner_type::value_type>` otherwise only basic exception safety can
* be assumed.
*
* ### Thread safety
*
* This container provides no thread-safety beyond the promise given also by the STL that all
* calls to `const` member function are safe from multiple threads (as long as no thread calls
* a non-`const` member function at the same time).
*
* \experimentalapi{Experimental since version 3.1.}
*/
template <typename inner_type,
typename data_delimiters_type = std::vector<typename inner_type::size_type>>
//!\cond
requires reservible_container<std::remove_reference_t<inner_type>> &&
reservible_container<std::remove_reference_t<data_delimiters_type>> &&
std::is_same_v<std::ranges::range_size_t<inner_type>, std::ranges::range_value_t<data_delimiters_type>>
//!\endcond
class concatenated_sequences
{
protected:
//!\privatesection
//!\brief Where the concatenation is stored.
std::decay_t<inner_type> data_values;
//!\brief Where the delimiters are stored; begins with 0, has size of size() + 1.
data_delimiters_type data_delimiters{0};
public:
//!\publicsection
/*!\name Member types
* \{
*/
/*!\brief == inner_type.
* \hideinitializer
* \details
* \experimentalapi{Experimental since version 3.1.}
*/
using value_type = std::decay_t<inner_type>;
/*!\brief A proxy of type views::slice that represents the range on the concatenated vector.
* \hideinitializer
* \details
* \experimentalapi{Experimental since version 3.1.}
*/
using reference = detail::concatenated_sequences_reference_proxy<value_type, false>;
/*!\brief An immutable proxy of type views::slice that represents the range on the concatenated vector.
* \hideinitializer
* \details
* \experimentalapi{Experimental since version 3.1.}
*/
using const_reference = detail::concatenated_sequences_reference_proxy<value_type, true>;
/*!\brief The iterator type of this container (a random access iterator).
* \hideinitializer
* \details
* \experimentalapi{Experimental since version 3.1.}
*/
using iterator = detail::random_access_iterator<concatenated_sequences>;
/*!\brief The const iterator type of this container (a random access iterator).
* \hideinitializer
* \details
* \experimentalapi{Experimental since version 3.1.}
*/
using const_iterator = detail::random_access_iterator<concatenated_sequences const>;
/*!\brief A signed integer type (usually std::ptrdiff_t)
* \hideinitializer
* \details
* \experimentalapi{Experimental since version 3.1.}
*/
using difference_type = std::ranges::range_difference_t<data_delimiters_type>;
/*!\brief An unsigned integer type (usually std::size_t)
* \hideinitializer
* \details
* \experimentalapi{Experimental since version 3.1.}
*/
using size_type = std::ranges::range_size_t<data_delimiters_type>;
//!\}
//!\cond
// this signals to range-v3 that something is a container :|
using allocator_type = void;
//!\endcond
protected:
/*!\name Compatibility
* \brief Static constexpr variables that emulate whether the ranges are compatible
* (which doesn't work for types during their definition).
* \{
*/
//!\cond
// unfortunately we cannot specialise the variable template so we have to add an auxiliary here
template <std::ranges::range t>
static constexpr bool is_compatible_with_value_type_aux(std::type_identity<t>)
{
return range_dimension_v<t> == range_dimension_v<value_type> &&
std::convertible_to<std::ranges::range_reference_t<t>, std::ranges::range_value_t<value_type>>;
}
static constexpr bool is_compatible_with_value_type_aux(...)
{
return false;
}
//!\endcond
/*!\brief Whether a type is compatible with this class's `value_type` or `reference` type.
* \hideinitializer
* \details
* \noapi{Exposition only.}
*/
// we explicitly check same-ness, because these types may not be fully resolved, yet
template <std::ranges::range t>
static constexpr bool is_compatible_with_value_type = is_compatible_with_value_type_aux(std::type_identity<t>{});
/*!\brief Whether a type is compatible with this class.
* \hideinitializer
* \details
* \noapi{Exposition only.}
*/
// cannot use the concept, because this class is not yet fully defined
template <typename t>
//!\cond
requires is_compatible_with_value_type<std::iter_reference_t<t>>
//!\endcond
static constexpr bool iter_value_t_is_compatible_with_value_type = true;
/*!\brief Whether a type is compatible with this class.
* \hideinitializer
* \details
* \noapi{Exposition only.}
*/
// cannot use the concept, because this class is not yet fully defined
template <std::ranges::range t>
//!\cond
requires is_compatible_with_value_type<std::ranges::range_reference_t<t>>
//!\endcond
static constexpr bool range_value_t_is_compatible_with_value_type = true;
//!\}
public:
/*!\name Constructors, destructor and assignment
* \{
*/
//!\brief Default constructors.
concatenated_sequences() = default;
//!\brief Default constructors.
constexpr concatenated_sequences(concatenated_sequences const &) = default;
//!\brief Default constructors.
constexpr concatenated_sequences(concatenated_sequences &&) = default;
//!\brief Default constructors.
constexpr concatenated_sequences & operator=(concatenated_sequences const &) = default;
//!\brief Default constructors.
constexpr concatenated_sequences & operator=(concatenated_sequences &&) = default;
//!\brief Default constructors.
~concatenated_sequences() = default;
/*!\brief Construct/assign from a different range.
* \tparam rng_of_rng_type The type of range to be inserted; must satisfy
* \ref range_value_t_is_compatible_with_value_type.
* \param rng_of_rng The sequences to construct/assign from.
*
* ### Complexity
*
* Linear in the cumulative size of `rng_of_rng`.
*
* ### Exceptions
*
* Strong exception guarantee (no data is modified in case an exception is thrown).
*
* \experimentalapi{Experimental since version 3.1.}
*/
template <std::ranges::input_range rng_of_rng_type>
concatenated_sequences(rng_of_rng_type && rng_of_rng)
//!\cond
requires range_value_t_is_compatible_with_value_type<rng_of_rng_type>
//!\endcond
{
if constexpr (std::ranges::sized_range<rng_of_rng_type>)
data_delimiters.reserve(std::ranges::size(rng_of_rng) + 1);
for (auto && val : rng_of_rng)
{
data_values.insert(data_values.end(), val.begin(), val.end());
data_delimiters.push_back(data_delimiters.back() + val.size());
}
}
/*!\brief Construct/assign with `count` times `value`.
* \tparam rng_type The type of range to be inserted; must satisfy \ref is_compatible_with_value_type.
* \param count Number of elements.
* \param value The initial value to be assigned.
*
* ### Complexity
*
* In \f$O(count*value)\f$.
*
* ### Exceptions
*
* Strong exception guarantee (no data is modified in case an exception is thrown).
*
* \experimentalapi{Experimental since version 3.1.}
*/
template <std::ranges::forward_range rng_type>
concatenated_sequences(size_type const count, rng_type && value)
//!\cond
requires is_compatible_with_value_type<rng_type>
//!\endcond
{
// TODO SEQAN_UNLIKELY
if (count == 0)
return;
insert(cend(), count, std::forward<rng_type>(value));
}
/*!\brief Construct/assign from pair of iterators.
* \tparam begin_iterator_type Must satisfy std::forward_iterator and must satisfy
* \ref iter_value_t_is_compatible_with_value_type.
* \tparam end_iterator_type Must satisfy std::sized_sentinel_for.
* \param begin_it begin of range to construct/assign from.
* \param end_it end of range to construct/assign from.
*
* ### Complexity
*
* Linear in the cumulative size of the ranges between `begin_it` and `end_it`.
*
* ### Exceptions
*
* Strong exception guarantee (no data is modified in case an exception is thrown).
*
* \experimentalapi{Experimental since version 3.1.}
*/
template <std::forward_iterator begin_iterator_type, typename end_iterator_type>
concatenated_sequences(begin_iterator_type begin_it, end_iterator_type end_it)
//!\cond
requires std::sized_sentinel_for<end_iterator_type, begin_iterator_type> &&
iter_value_t_is_compatible_with_value_type<begin_iterator_type>
//!\endcond
{
insert(cend(), begin_it, end_it);
}
/*!\brief Construct/assign from `std::initializer_list`.
* \tparam value_type_t The type of range to be inserted; must satisfy \ref is_compatible_with_value_type.
* \param ilist a `std::initializer_list` of `value_type_t`.
*
* ### Complexity
*
* Linear in the cumulative size of the ranges in `ilist`.
*
* ### Exceptions
*
* Strong exception guarantee (no data is modified in case an exception is thrown).
*
* \experimentalapi{Experimental since version 3.1.}
*/
template <std::ranges::forward_range value_type_t = value_type>
//!\cond
requires is_compatible_with_value_type<value_type_t>
//!\endcond
concatenated_sequences(std::initializer_list<value_type_t> ilist)
{
assign(std::begin(ilist), std::end(ilist));
}
/*!\brief Construct/assign from `std::initializer_list`.
* \tparam value_type_t The type of range to be inserted; must satisfy \ref is_compatible_with_value_type.
* \param ilist a `std::initializer_list` of `value_type_t`.
*
* ### Complexity
*
* Linear in the cumulative size of the elements in `ilist`.
*
* ### Exceptions
*
* Strong exception guarantee (no data is modified in case an exception is thrown).
*
* \experimentalapi{Experimental since version 3.1.}
*/
template <std::ranges::forward_range value_type_t>
concatenated_sequences & operator=(std::initializer_list<value_type_t> ilist)
//!\cond
requires is_compatible_with_value_type<value_type_t>
//!\endcond
{
assign(std::begin(ilist), std::end(ilist));
return *this;
}
/*!\brief Construct/assign from a different range.
* \tparam rng_of_rng_type The type of range to be inserted; must satisfy
* \ref range_value_t_is_compatible_with_value_type.
* \param rng_of_rng The sequences to construct/assign from.
*
* ### Complexity
*
* Linear in the cumulative size of `rng_of_rng`.
*
* ### Exceptions
*
* Strong exception guarantee (no data is modified in case an exception is thrown).
*
* \experimentalapi{Experimental since version 3.1.}
*/
template <std::ranges::input_range rng_of_rng_type>
void assign(rng_of_rng_type && rng_of_rng)
//!\cond
requires range_value_t_is_compatible_with_value_type<rng_of_rng_type>
//!\endcond
{
concatenated_sequences rhs{std::forward<rng_of_rng_type>(rng_of_rng)};
swap(rhs);
}
/*!\brief Construct/assign with `count` times `value`.
* \tparam rng_type The type of range to be inserted; must satisfy \ref is_compatible_with_value_type.
* \param count Number of elements.
* \param value The initial value to be assigned.
*
* ### Complexity
*
* In \f$O(count*value)\f$.
*
* ### Exceptions
*
* Strong exception guarantee (no data is modified in case an exception is thrown).
*
* \experimentalapi{Experimental since version 3.1.}
*/
template <std::ranges::forward_range rng_type>
void assign(size_type const count, rng_type && value)
//!\cond
requires (is_compatible_with_value_type<rng_type>)
//!\endcond
{
concatenated_sequences rhs{count, value};
swap(rhs);
}
/*!\brief Construct/assign from pair of iterators.
* \tparam begin_iterator_type Must satisfy std::forward_iterator and satisfy
* \ref iter_value_t_is_compatible_with_value_type.
* \tparam end_iterator_type Must satisfy std::sized_sentinel_for.
* \param begin_it begin of range to construct/assign from.
* \param end_it end of range to construct/assign from.
*
* ### Complexity
*
* Linear in the cumulative size of the ranges between `begin_it` and `end_it`.
*
* ### Exceptions
*
* Strong exception guarantee (no data is modified in case an exception is thrown).
*
* \experimentalapi{Experimental since version 3.1.}
*/
template <std::forward_iterator begin_iterator_type, typename end_iterator_type>
void assign(begin_iterator_type begin_it, end_iterator_type end_it)
//!\cond
requires iter_value_t_is_compatible_with_value_type<begin_iterator_type> &&
std::sized_sentinel_for<end_iterator_type, begin_iterator_type>
//!\endcond
{
concatenated_sequences rhs{begin_it, end_it};
swap(rhs);
}
/*!\brief Construct/assign from `std::initializer_list`.
* \tparam rng_type The type of range to be inserted; must satisfy \ref is_compatible_with_value_type.
* \param ilist a `std::initializer_list` of `rng_type`.
*
* ### Complexity
*
* Linear in the cumulative size of the elements in `ilist`.
*
* ### Exceptions
*
* Strong exception guarantee (no data is modified in case an exception is thrown).
*
* \experimentalapi{Experimental since version 3.1.}
*/
template <std::ranges::forward_range rng_type = value_type>
void assign(std::initializer_list<rng_type> ilist)
//!\cond
requires is_compatible_with_value_type<rng_type>
//!\endcond
{
assign(std::begin(ilist), std::end(ilist));
}
//!\}
/*!\name Iterators
* \{
*/
/*!\brief Returns an iterator to the first element of the container.
* \returns Iterator to the first element.
*
* If the container is empty, the returned iterator will be equal to end().
*
* ### Complexity
*
* Constant.
*
* ### Exceptions
*
* No-throw guarantee.
*
* \experimentalapi{Experimental since version 3.1.}
*/
iterator begin() noexcept
{
return iterator{*this};
}
//!\copydoc begin()
const_iterator begin() const noexcept
{
return const_iterator{*this};
}
//!\copydoc begin()
const_iterator cbegin() const noexcept
{
return const_iterator{*this};
}
/*!\brief Returns an iterator to the element following the last element of the container.
* \returns Iterator to the first element.
*
* This element acts as a placeholder; attempting to dereference it results in undefined behaviour.
*
* ### Complexity
*
* Constant.
*
* ### Exceptions
*
* No-throw guarantee.
*
* \experimentalapi{Experimental since version 3.1.}
*/
iterator end() noexcept
{
return iterator{*this, size()};
}
//!\copydoc end()
const_iterator end() const noexcept
{
return const_iterator{*this, size()};
}
//!\copydoc end()
const_iterator cend() const noexcept
{
return const_iterator{*this, size()};
}
//!\}
/*!\name Element access
* \{
*/
/*!\brief Return the i-th element as a view.
* \param i The element to retrieve.
* \throws std::out_of_range If you access an element behind the last.
* \returns A std::ranges::view on the underlying concatenated sequences that acts as a proxy for the element.
*
* ### Complexity
*
* Constant.
*
* ### Exceptions
*
* Strong exception guarantee (never modifies data).
*
* \experimentalapi{Experimental since version 3.1.}
*/
reference at(size_type const i)
{
//TODO add SEQAN_UNLIKELY
if (i >= size())
throw std::out_of_range{"Trying to access element behind the last in concatenated_sequences."};
return (*this)[i];
}
//!\copydoc at()
const_reference at(size_type const i) const
{
//TODO add SEQAN_UNLIKELY
if (i >= size())
throw std::out_of_range{"Trying to access element behind the last in concatenated_sequences."};
return (*this)[i];
}
/*!\brief Return the i-th element as a view.
* \param i The element to retrieve.
* \returns A std::ranges::view on the underlying concatenated sequences that acts as a proxy for the element.
*
* Accessing an element behind the last causes undefined behaviour. In debug mode an assertion checks the size of
* the container.
* ### Complexity
*
* Constant.
*
* ### Exceptions
*
* Strong exception guarantee (never modifies data).
*
* \experimentalapi{Experimental since version 3.1.}
*/
reference operator[](size_type const i)
{
assert(i < size());
return data_values | views::slice(data_delimiters[i], data_delimiters[i+1]);
}
//!\copydoc operator[]()
const_reference operator[](size_type const i) const
{
assert(i < size());
return data_values | detail::as_const | views::slice(data_delimiters[i], data_delimiters[i+1]);
}
/*!\brief Return the first element as a view. Calling front on an empty container is undefined.
* \returns A std::ranges::view on the underlying concatenated sequences that acts as a proxy for the element.
*
* Calling front on an empty container is undefined. In debug mode an assertion checks the size of the container.
* ### Complexity
*
* Constant.
*
* ### Exceptions
*
* Strong exception guarantee (never modifies data).
*
* \experimentalapi{Experimental since version 3.1.}
*/
reference front()
{
assert(size() > 0);
return (*this)[0];
}
//!\copydoc front()
const_reference front() const
{
assert(size() > 0);
return (*this)[0];
}
/*!\brief Return the last element as a view.
* \returns A std::ranges::view on the underlying concatenated sequences that acts as a proxy for the element.
*
* Calling back on an empty container is undefined. In debug mode an assertion checks the size of the container.
* ### Complexity
*
* Constant.
*
* ### Exceptions
*
* Strong exception guarantee (never modifies data).
*
* \experimentalapi{Experimental since version 3.1.}
*/
reference back()
{
assert(size() > 0);
return (*this)[size()-1];
}
//!\copydoc back()
const_reference back() const
{
assert(size() > 0);
return (*this)[size()-1];
}
/*!\brief Return the concatenation of all members.
* \returns A std::ranges::view proxy on the concatenation of underlying sequences.
*
* This is a safe way of accessing the internal concatenated representation, i.e. you cannot do operations
* that would invalidate this container (like insert or resize), but you can write to the individual positions.
*
* ### Complexity
*
* Constant.
*
* ### Exceptions
*
* Strong exception guarantee (never modifies data).
*
* \experimentalapi{Experimental since version 3.1.}
*/
reference concat()
{
return data_values | views::slice(static_cast<size_type>(0), concat_size());
}
//!\copydoc concat()
const_reference concat() const
{
return data_values | detail::as_const | views::slice(static_cast<size_type>(0), concat_size());
}
/*!\brief Provides direct, unsafe access to underlying data structures.
* \returns A std::pair of the concatenated sequences and the delimiter string.
*
* \details
*
* \noapi{The exact representation of the data is implementation defined.}
*/
std::pair<decltype(data_values) &, decltype(data_delimiters) &> raw_data()
{
return {data_values, data_delimiters};
}
//!\copydoc raw_data()
std::pair<decltype(data_values) const &, decltype(data_delimiters) const &> raw_data() const
{
return {std::as_const(data_values), std::as_const(data_delimiters)};
}
//!\}
/*!\name Capacity
* \{
*/
/*!\brief Checks whether the container is empty.
* \returns `true` if the container is empty, `false` otherwise.
*
* ### Complexity
*
* Constant.
*
* ### Exceptions
*
* No-throw guarantee.
*
* \experimentalapi{Experimental since version 3.1.}
*/
bool empty() const noexcept
{
return size() == 0;
}
/*!\brief Returns the number of elements in the container, i.e. std::distance(begin(), end()).
* \returns The number of elements in the container.
*
* ### Complexity
*
* Constant.
*
* ### Exceptions
*
* No-throw guarantee.
*
* \experimentalapi{Experimental since version 3.1.}
*/
size_type size() const noexcept
{
return data_delimiters.size() - 1;
}
/*!\brief Returns the maximum number of elements the container is able to hold due to system or library
* implementation limitations, i.e. std::distance(begin(), end()) for the largest container.
* \returns The number of elements in the container.
*
* This value typically reflects the theoretical limit on the size of the container. At runtime, the size
* of the container may be limited to a value smaller than max_size() by the amount of RAM available.
* ### Complexity
*
* Constant.
*
* ### Exceptions
*
* No-throw guarantee.
*
* \experimentalapi{Experimental since version 3.1.}
*/
size_type max_size() const noexcept
{
return data_delimiters.max_size() - 1;
}
/*!\brief Returns the number of elements that the container has currently allocated space for.
* \returns The capacity of the currently allocated storage.
*
* \attention
*
* This does not operate on underlying concat container, see concat_capacity().
*
* ### Complexity
*
* Constant.
*
* ### Exceptions
*
* No-throw guarantee.
*
* \experimentalapi{Experimental since version 3.1.}
*/
size_type capacity() const noexcept
{
return data_delimiters.capacity();
}
/*!\brief Increase the capacity to a value that's greater or equal to new_cap.
* \param new_cap The new capacity.
* \throws std::length_error If new_cap > max_size().
* \throws std::exception Any exception thrown by `Allocator::allocate()` (typically `std::bad_alloc`).
*
* Increase the capacity of the vector to a value that's greater or equal to new_cap.
* If new_cap is greater than the current capacity(), new storage is allocated, otherwise the method does nothing.
* If new_cap is greater than capacity(), all iterators, including the past-the-end iterator, and all references
* to the elements are invalidated. Otherwise, no iterators or references are invalidated.
*
* \attention This does not operate on underlying concat container, see concat_reserve().
*
* ### Complexity
*
* At most linear in the size() of the container.
*
* ### Exceptions
*
* Strong exception guarantee (no data is modified in case an exception is thrown).
*
* \experimentalapi{Experimental since version 3.1.}
*/
void reserve(size_type const new_cap)
{
data_delimiters.reserve(new_cap + 1);
}
/*!\brief Requests the removal of unused capacity.
*
* It is a non-binding request to reduce capacity() to size() and concat_capacity() to concat_size().
* It depends on the implementation if the request is fulfilled.
* If reallocation occurs, all iterators, including the past the end iterator, and all references to the elements
* are invalidated. If no reallocation takes place, no iterators or references are invalidated.
*
* \attention This affects both underlying data structures.
*
* ### Complexity
*
* At most linear in the size() of the container.
*
* ### Exceptions
*
* Strong exception guarantee (no data is modified in case an exception is thrown).
*
* \experimentalapi{Experimental since version 3.1.}
*/
void shrink_to_fit()
{
data_values.shrink_to_fit();
data_delimiters.shrink_to_fit();
}
//!\}
/*!\name Capacity (concat)
* \{
*/
/*!\brief Returns the cumulative size of all elements in the container.
* \returns The cumulative size of elements in the container.
*
* ### Complexity
*
* Constant.
*
* ### Exceptions
*
* No-throw guarantee.
*
* \experimentalapi{Experimental since version 3.1.}
*/
size_type concat_size() const noexcept
{
return data_values.size();
}
/*!\brief Returns the concatenated size the container has currently allocated space for.
* \returns The capacity of the currently allocated storage.
*
* ### Complexity
*
* Constant.
*
* ### Exceptions
*
* No-throw guarantee.
*
* \experimentalapi{Experimental since version 3.1.}
*/
size_type concat_capacity() const noexcept
{
return data_values.capacity();
}
/*!\brief Increase the concat_capacity() to a value that's greater or equal to new_cap.
* \param new_cap The new capacity.
* \throws std::length_error If new_cap > max_size().
* \throws std::exception Any exception thrown by `Allocator::allocate()` (typically `std::bad_alloc`).
*
* Increase the capacity of the underlying concatenated sequence to a value that's greater or equal to new_cap.
* If new_cap is greater than the current concat_capacity(), new storage is allocated, otherwise the method does
* nothing. If new_cap is greater than concat_capacity(), all iterators, including the past-the-end iterator, and
* all references to the elements are invalidated. Otherwise, no iterators or references are invalidated.
*
* ### Complexity
*
* At most linear in the concat_size() of the container.
*
* ### Exceptions
*
* Strong exception guarantee (no data is modified in case an exception is thrown).
*
* \experimentalapi{Experimental since version 3.1.}
*/
void concat_reserve(size_type const new_cap)
{
data_values.reserve(new_cap);
}
//!\}
/*!\name Modifiers