SeqAn3
concept.hpp
Go to the documentation of this file.
1 // ============================================================================
2 // SeqAn - The Library for Sequence Analysis
3 // ============================================================================
4 //
5 // Copyright (c) 2006-2018, Knut Reinert & Freie Universitaet Berlin
6 // Copyright (c) 2016-2018, Knut Reinert & MPI Molekulare Genetik
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // * Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // * Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 // * Neither the name of Knut Reinert or the FU Berlin nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 // ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
25 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 // DAMAGE.
32 //
33 // ============================================================================
34 
40 #pragma once
41 
42 #include <initializer_list>
43 #include <iterator>
44 #include <type_traits>
45 
46 // remove if sequence_container_concept_modified_by_const_iterator_bug vanished from travis
47 #include <string>
48 
49 #include <seqan3/std/iterator>
50 
51 // TODO:
52 // * merge sequence_container_concept_modified_by_const_iterator back into
53 // sequence_container_concept
54 // * remove is_basic_string
55 // * fix test cases
56 // * remove #include <string> in this file
57 // once the ubuntu::ppa [1] of g++-7 has a newer update than
58 // 7.2.0-1ubuntu1~16.04 (2017-08-20)
59 //
60 // [1] https://launchpad.net/~ubuntu-toolchain-r/+archive/ubuntu/test?field.series_filter=xenial
61 namespace seqan3::detail
62 {
64 
67 template <typename basic_string_t>
68 struct is_basic_string : std::false_type
69 {};
70 
73 template <typename value_t, typename traits_t, typename allocator_t>
74 struct is_basic_string<std::basic_string<value_t, traits_t, allocator_t>> : std::true_type
75 {};
76 
79 template <typename basic_string_t>
80 constexpr bool is_basic_string_v = is_basic_string<basic_string_t>::value;
81 
88 template <typename type>
90 concept sequence_container_concept_modified_by_const_iterator = requires (type val, type val2)
91 {
92  { val.insert(val.cbegin(), val2.front()) } -> typename type::iterator;
93  { val.insert(val.cbegin(), typename type::value_type{}) } -> typename type::iterator;
94  { val.insert(val.cbegin(), typename type::size_type{}, typename type::value_type{})} -> typename type::iterator;
95  { val.insert(val.cbegin(), val2.begin(), val2.end()) } -> typename type::iterator;
96  requires is_basic_string_v<type> || requires(type val)
97  {
98  // TODO this function is not defined on strings (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83328)
99  { val.insert(val.cbegin(), std::initializer_list<typename type::value_type>{}) } -> typename type::iterator;
100  };
101  { val.erase(val.cbegin()) } -> typename type::iterator;
102  { val.erase(val.cbegin(), val.cend()) } -> typename type::iterator;
103 
104  { val.insert(val.begin(), typename type::size_type{}, typename type::value_type{}) } -> typename type::iterator;
105  { val.insert(val.begin(), val2.begin(), val2.end()) } -> typename type::iterator;
106 };
108 
121 template<typename string_t = std::string>
122 constexpr bool sequence_container_concept_modified_by_const_iterator_bug =
123  is_basic_string_v<string_t> && !sequence_container_concept_modified_by_const_iterator<string_t>;
124 
126 
127 } // seqan3::detail
128 
129 namespace seqan3
130 {
131 
149 template <typename type>
151 concept container_concept = requires (type val, type val2, type const cval, typename type::iterator it)
152 {
153  // member types
154  typename type::value_type;
155  typename type::reference;
156  typename type::const_reference;
157 /*
158  typename type::iterator;
159  requires std::ForwardIterator<typename type::iterator>;
160  { it } -> typename type::const_iterator; // NOTE check whether iterator is const convertible
161 
162  typename type::const_iterator;
163  requires std::ForwardIterator<typename type::const_iterator>;
164 
165  typename type::difference_type;
166  typename type::size_type;
167  requires std::is_same_v<
168  typename type::difference_type,
169  typename std::iterator_traits<typename type::iterator>::difference_type
170  >;
171  requires std::is_same_v<
172  typename std::iterator_traits<typename type::iterator>::difference_type,
173  typename std::iterator_traits<typename type::const_iterator>::difference_type
174  >;
175 */
176  // methods and operator
177  { type{} } -> type; // default constructor
178  { type{type{}} } -> type; // copy/move constructor
179  { val = val2 } -> type &; // assignment
180  { (&val)->~type() } -> void; // destructor
181 
182  { val.begin() } -> typename type::iterator;
183  { val.end() } -> typename type::iterator;
184  { cval.begin() } -> typename type::const_iterator;
185  { cval.end() } -> typename type::const_iterator;
186  { val.cbegin() } -> typename type::const_iterator;
187  { val.cend() } -> typename type::const_iterator;
188 
189  { val == val2 } -> bool;
190  { val != val2 } -> bool;
191 
192  { val.swap(val2) } -> void;
193  { swap(val, val2) } -> void;
194  { std::swap(val, val2) } -> void;
195 
196  { val.size() } -> typename type::size_type;
197  { val.max_size() } -> typename type::size_type;
198  { val.empty() } -> bool;
199 };
201 
214 template <typename type>
216 concept sequence_container_concept = requires (type val, type val2, type const cval)
217 {
218  requires container_concept<type>;
219 
220  // construction
221  { type{typename type::size_type{}, typename type::value_type{}} };
222  { type{val2.begin(), val2.end()} }; // NOTE that this could be any input iterator:
223  { type{std::initializer_list<typename type::value_type>{}} };
224  { val = std::initializer_list<typename type::value_type>{} } -> type &;
225 
226  // assignment NOTE return type is type & for std::string and void for other stl containers:
227  { val.assign(val2.begin(), val2.end()) };
228  { val.assign(std::initializer_list<typename type::value_type>{}) };
229  { val.assign(typename type::size_type{}, typename type::value_type{}) };
230 
231  // modify container
232  // TODO: how do you model this?
233  // { val.emplace(typename type::const_iterator{}, ? } -> typename type::iterator;
234 
235  { val.insert(val.begin(), val2.front()) } -> typename type::iterator;
236  { val.insert(val.begin(), typename type::value_type{}) } -> typename type::iterator;
237  // because of a travis bug we can't assume typename type::iterator as return type
238  { val.insert(val.begin(), typename type::size_type{}, typename type::value_type{}) };
239  { val.insert(val.begin(), val2.begin(), val2.end()) };
240  //TODO should return type::iterator on strings (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83328)
241  { val.insert(val.begin(), std::initializer_list<typename type::value_type>{}) };
242  { val.erase(val.begin()) } -> typename type::iterator;
243  { val.erase(val.begin(), val.end()) } -> typename type::iterator;
244 
245  // workaround a travis bug where insert/erase can't take a const iterator, e.g. cbegin()
246  requires detail::sequence_container_concept_modified_by_const_iterator_bug<type> ||
247  detail::sequence_container_concept_modified_by_const_iterator<type>;
248 
249  { val.push_back(val.front()) } -> void;
250  { val.push_back(typename type::value_type{}) } -> void;
251  { val.pop_back() } -> void;
252  { val.clear() } -> void;
253 
254  // access container
255  { val.front() } -> typename type::reference;
256  { val.front() } -> typename type::const_reference;
257  { cval.front() } -> typename type::const_reference;
258  { val.back() } -> typename type::reference;
259  { val.back() } -> typename type::const_reference;
260  { cval.back() } -> typename type::const_reference;
261 };
263 
278 template <typename type>
280 concept random_access_container_concept = requires (type val)
281 {
282  requires sequence_container_concept<type>;
283 
284  // access container
285  { val[0] } -> typename type::reference;
286  { val.at(0) } -> typename type::reference;
287 
288  // modify container
289  { val.resize(0) } -> void;
290  { val.resize(0, typename type::value_type{}) } -> void;
291 };
293 
304 template <typename type>
306 concept reservable_container_concept = requires (type val)
307 {
308  requires random_access_container_concept<type>;
309 
310  { val.capacity() } -> typename type::size_type;
311  { val.reserve(0) } -> void;
312  { val.shrink_to_fit() } -> void;
313 };
315 
317 
318 } // namespace seqan3
Provides C++20 additions to the <iterator> header.
SeqAn specific customisations in the standard namespace.
Definition: align_result.hpp:221
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:58
Definition: aligned_sequence_concept.hpp:288
Provides C++20 additions to the type_traits header.