mCRL2
Loading...
Searching...
No Matches
aterm_list.h
Go to the documentation of this file.
1// Author(s): Wieger Wesselink, Jan Friso Groote
2// Copyright: see the accompanying file COPYING or copy at
3// https://github.com/mCRL2org/mCRL2/blob/master/COPYING
4//
5// Distributed under the Boost Software License, Version 1.0.
6// (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
9
10#ifndef MCRL2_ATERMPP_ATERM_LIST_H
11#define MCRL2_ATERMPP_ATERM_LIST_H
12
13#include "mcrl2/atermpp/aterm.h"
17
18namespace atermpp
19{
20
22template <typename Term>
23class term_list: public aterm
24{
25protected:
27 explicit term_list(detail::_aterm_appl<>* t) noexcept : aterm(t)
28 {
29 assert(!defined() || type_is_list());
30 }
31
32public:
34 typedef Term value_type;
35
37 typedef Term* pointer;
38
40 typedef Term& reference;
41
43 typedef const Term& const_reference;
44
46 typedef std::size_t size_type;
47
49 typedef ptrdiff_t difference_type;
50
53
56
59
61 term_list() noexcept
62 : aterm(detail::g_term_pool().empty_list())
63 {}
64
67 explicit term_list(const aterm& t) noexcept
68 : aterm(t)
69 {
70 // assert(!defined() || type_is_list());
71 assert(type_is_list()); // A list should not be a default aterm.
72 }
73
76 term_list(const term_list<Term>& t) noexcept
77 : aterm(t)
78 {
79 assert(!defined() || type_is_list());
80 }
81
85 : aterm(std::move(t))
86 {
87 assert(!defined() || type_is_list());
88 }
89
91 term_list& operator=(const term_list& other) noexcept = default;
92 term_list& operator=(term_list&& other) noexcept = default;
93
98 template <class Iter>
99 explicit term_list(Iter first, Iter last, typename std::enable_if<std::is_base_of<
100 std::bidirectional_iterator_tag,
101 typename std::iterator_traits<Iter>::iterator_category
102 >::value>::type* = nullptr) :
103 aterm(detail::make_list_backward<Term,Iter,
104 detail::do_not_convert_term<Term> >(first, last,detail::do_not_convert_term<Term>()))
105 {
106 assert(!defined() || type_is_list());
107 }
108
116 template <class Iter, class ATermConverter>
117 explicit term_list(Iter first, Iter last, const ATermConverter& convert_to_aterm,
118 typename std::enable_if<std::is_base_of<
119 std::bidirectional_iterator_tag,
120 typename std::iterator_traits<Iter>::iterator_category
121 >::value>::type* = 0):
122 aterm(detail::make_list_backward<Term,Iter,ATermConverter>(first, last, convert_to_aterm))
123 {
124 assert(!defined() || type_is_list());
125 }
126
136 template <class Iter, class ATermConverter, class ATermFilter>
137 explicit term_list(Iter first, Iter last, const ATermConverter& convert_to_aterm, const ATermFilter& aterm_filter,
138 typename std::enable_if<std::is_base_of<
139 std::bidirectional_iterator_tag,
140 typename std::iterator_traits<Iter>::iterator_category
141 >::value>::type* = 0):
142 aterm(detail::make_list_backward<Term,Iter,ATermConverter,ATermFilter>(first, last, convert_to_aterm, aterm_filter))
143 {
144 assert(!defined() || type_is_list());
145 }
146
153 template <class Iter>
154 explicit term_list(Iter first, Iter last,
155 typename std::enable_if< !std::is_base_of<
156 std::bidirectional_iterator_tag,
157 typename std::iterator_traits<Iter>::iterator_category
158 >::value>::type* = nullptr):
159 aterm(detail::make_list_forward<Term,Iter,detail::do_not_convert_term<Term> >
160 (first, last, detail::do_not_convert_term<Term>()))
161 {
162 assert(!defined() || type_is_list());
163 }
164
175 template <class Iter, class ATermConverter>
176 explicit term_list(Iter first, Iter last, const ATermConverter& convert_to_aterm,
177 typename std::enable_if< !std::is_base_of<
178 std::bidirectional_iterator_tag,
179 typename std::iterator_traits<Iter>::iterator_category
180 >::value>::type* = nullptr):
181 aterm(detail::make_list_forward<Term,Iter,ATermConverter>
182 (first, last, convert_to_aterm))
183 {
184 assert(!defined() || type_is_list());
185 }
186
199 template <class Iter, class ATermConverter, class ATermFilter>
200 explicit term_list(Iter first, Iter last, const ATermConverter& convert_to_aterm, const ATermFilter& aterm_filter,
201 typename std::enable_if< !std::is_base_of<
202 std::random_access_iterator_tag,
203 typename std::iterator_traits<Iter>::iterator_category
204 >::value>::type* = nullptr):
205 aterm(detail::make_list_forward<Term,Iter,ATermConverter>
206 (first, last, convert_to_aterm, aterm_filter))
207 {
208 assert(!defined() || type_is_list());
209 }
210
214 term_list(std::initializer_list<Term> init)
215 : aterm(detail::make_list_backward<Term,
216 typename std::initializer_list<Term>::const_iterator,
217 detail::do_not_convert_term<Term> >
218 (init.begin(), init.end(), detail::do_not_convert_term<Term>()))
219 {
220 assert(!defined() || type_is_list());
221 }
222
225 const term_list<Term>& tail() const
226 {
227 assert(!empty());
228 return (static_cast<const detail::_aterm_list<Term>&>(*m_term)).tail();
229 }
230
233 {
234 *this = tail();
235 }
236
239 const Term& front() const
240 {
241 return static_cast<const detail::_aterm_list<Term>&>(*m_term).head();
242 }
243
246 void push_front(const Term& el);
247
250 template<typename ...Args>
251 void emplace_front(Args&&... arguments);
252
257 {
258 std::size_t size=0;
259 for(const_iterator i=begin(); i!=end(); ++i)
260 {
261 ++size;
262 }
263 return size;
264 }
265
268 bool empty() const
269 {
271 }
272
276 {
277 return const_iterator(m_term);
278 }
279
283 {
284 return const_iterator(detail::address(detail::g_term_pool().empty_list()));
285 }
286
291 {
293 }
294
298 {
299 return const_reverse_iterator();
300 }
301
305 {
306 return std::numeric_limits<std::size_t>::max();
307 }
308};
309
310
313template <class Term>
315{
316 target=atermpp::down_cast<term_list<Term>>(detail::g_term_pool().empty_list());
317}
318
324template <class Term, class Iter>
325void make_term_list(term_list<Term>& target, Iter first, Iter last, typename std::enable_if<std::is_base_of<
326 std::bidirectional_iterator_tag,
327 typename std::iterator_traits<Iter>::iterator_category >::value>::type* = nullptr)
328{
331 assert(!target.defined() || target.type_is_list());
332}
333
342template <class Term, class Iter, class ATermConverter>
343void make_term_list(term_list<Term>& target, Iter first, Iter last, const ATermConverter& convert_to_aterm,
344 typename std::enable_if<std::is_base_of<
345 std::bidirectional_iterator_tag,
346 typename std::iterator_traits<Iter>::iterator_category
347 >::value>::type* = 0)
348{
349 detail::make_list_backward<Term,Iter,ATermConverter>(target, first, last, convert_to_aterm);
350 assert(!target.defined() || target.type_is_list());
351}
352
363template <class Term, class Iter, class ATermConverter, class ATermFilter>
364void make_term_list(term_list<Term>& target, Iter first, Iter last, const ATermConverter& convert_to_aterm, const ATermFilter& aterm_filter,
365 typename std::enable_if<std::is_base_of<
366 std::bidirectional_iterator_tag,
367 typename std::iterator_traits<Iter>::iterator_category
368 >::value>::type* = 0)
369{
370 detail::make_list_backward<Term,Iter,ATermConverter,ATermFilter>(target, first, last, convert_to_aterm, aterm_filter);
371 assert(!target.defined() || target.type_is_list());
372}
373
381template <class Term, class Iter>
382void make_term_list(term_list<Term>& target, Iter first, Iter last,
383 typename std::enable_if< !std::is_base_of<
384 std::bidirectional_iterator_tag,
385 typename std::iterator_traits<Iter>::iterator_category
386 >::value>::type* = nullptr)
387{
388 detail::make_list_forward<Term,Iter,detail::do_not_convert_term<Term> >
389 (target, first, last, detail::do_not_convert_term<Term>());
390 assert(!target.defined() || target.type_is_list());
391}
392
404template <class Term, class Iter, class ATermConverter>
405void make_term_list(term_list<Term>& target, Iter first, Iter last, const ATermConverter& convert_to_aterm,
406 typename std::enable_if< !std::is_base_of<
407 std::bidirectional_iterator_tag,
408 typename std::iterator_traits<Iter>::iterator_category
409 >::value>::type* = nullptr)
410{
411 detail::make_list_forward<Term,Iter,ATermConverter>
412 (target, first, last, convert_to_aterm);
413 assert(!target.defined() || target.type_is_list());
414}
415
429template <class Term, class Iter, class ATermConverter, class ATermFilter>
430void make_term_list(term_list<Term>& target, Iter first, Iter last, const ATermConverter& convert_to_aterm, const ATermFilter& aterm_filter,
431 typename std::enable_if< !std::is_base_of<
432 std::random_access_iterator_tag,
433 typename std::iterator_traits<Iter>::iterator_category
434 >::value>::type* = nullptr)
435{
436 detail::make_list_forward<Term,Iter,ATermConverter>
437 (target, first, last, convert_to_aterm, aterm_filter);
438 assert(!target.defined() || target.type_is_list());
439}
440
445template <class Term>
446void make_term_list(term_list<Term>& target, std::initializer_list<Term> init)
447{
448 target=detail::make_list_backward<Term,
449 typename std::initializer_list<Term>::const_iterator,
451 (init.begin(), init.end(), detail::do_not_convert_term<Term>());
452 assert(!target.defined() || target.type_is_list());
453}
454
455
456
458namespace detail
459{
460
463template < typename T >
464struct is_container_impl< atermpp::term_list< T > > : public std::true_type
465{ };
466
467
468template <class Term>
469class _aterm_list : public _aterm_appl<2>
470{
471public:
473 const Term& head() const { return static_cast<const Term&>(arg(0)); }
474
476 const term_list<Term>& tail() const { return static_cast<const term_list<Term>&>(arg(1)); }
477
478 std::size_t size() const
479 {
480 std::size_t size=0;
481 for(_aterm_list const* i=this;
482 i->function()!=detail::g_term_pool().as_empty_list();
483 i=static_cast<_aterm_list const*>(address(i->tail())))
484 {
485 ++size;
486 }
487 return size;
488 }
489
490};
491
492} // namespace detail
494
495
498
499
504template <typename Term>
505inline
507
513template <typename Term>
514inline
516 const std::function<bool(const Term&, const Term&)>& ordering
517 = [](const Term& t1, const Term& t2){ return t1<t2;});
518
519
529
530template <typename Term1, typename Term2>
531inline
532typename std::conditional<std::is_convertible<Term2,Term1>::value,term_list<Term1>,term_list<Term2>>::type
533operator+(const term_list<Term1>& l, const term_list<Term2>& m);
534
535
542template <typename Term>
543inline
544term_list<Term> push_back(const term_list<Term>& l, const Term& el);
545
547template <typename T>
548std::vector<T> as_vector(const atermpp::term_list<T>& x)
549{
550 return std::vector<T>(x.begin(), x.end());
551}
552
554template <typename T>
555std::set<T> as_set(const atermpp::term_list<T>& x)
556{
557 return std::set<T>(x.begin(), x.end());
558}
559
560} // namespace atermpp
561
562
563namespace std
564{
565//
571template <class T>
573{
574 t1.swap(t2);
575}
576
578template <class Term>
579struct hash<atermpp::term_list<Term> >
580{
584 std::size_t operator()(const atermpp::term_list<Term>& l) const
585 {
586 std::hash<atermpp::aterm> hasher;
587 return hasher(l);
588 }
589};
590
591} // namespace std
592
594
595#endif // MCRL2_ATERMPP_ATERM_LIST_H
The term_appl class represents function application.
Iterator for term_list.
A class containing some type traits.
aterm()
Default constructor.
Definition aterm.h:48
This class stores a term followed by N arguments. Where N should be equal to the arity of the functio...
Definition aterm.h:34
const function_symbol & function() const noexcept
Definition aterm_core.h:55
const function_symbol & as_empty_list() noexcept
Definition aterm_pool.h:124
aterm & empty_list() noexcept
Definition aterm_pool.h:115
Reverse iterator for term_list.
Iterator for term_list.
A list of aterm objects.
Definition aterm_list.h:24
size_type size() const
Returns the size of the term_list.
Definition aterm_list.h:256
term_list(const term_list< Term > &t) noexcept
Copy constructor.
Definition aterm_list.h:76
const_iterator end() const
Returns a const_iterator pointing to the end of the term_list.
Definition aterm_list.h:282
Term value_type
The type of object, T stored in the term_list.
Definition aterm_list.h:34
const_reverse_iterator rend() const
Returns a const_iterator pointing to the end of the term_list.
Definition aterm_list.h:297
term_list(Iter first, Iter last, const ATermConverter &convert_to_aterm, const ATermFilter &aterm_filter, typename std::enable_if< std::is_base_of< std::bidirectional_iterator_tag, typename std::iterator_traits< Iter >::iterator_category >::value >::type *=0)
Creates a term_list with the elements from first to last, converting and filtering the list.
Definition aterm_list.h:137
term_list(Iter first, Iter last, const ATermConverter &convert_to_aterm, const ATermFilter &aterm_filter, typename std::enable_if< !std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< Iter >::iterator_category >::value >::type *=nullptr)
Creates a term_list from the elements from first to last converting and filtering the elements before...
Definition aterm_list.h:200
term_list(Iter first, Iter last, typename std::enable_if< !std::is_base_of< std::bidirectional_iterator_tag, typename std::iterator_traits< Iter >::iterator_category >::value >::type *=nullptr)
Creates a term_list from the elements from first to last.
Definition aterm_list.h:154
std::size_t size_type
An unsigned integral type.
Definition aterm_list.h:46
term_list & operator=(const term_list &other) noexcept=default
This class has user-declared copy constructor so declare copy and move assignment.
reverse_term_list_iterator< Term > const_reverse_iterator
Const iterator used to iterate through an term_list.
Definition aterm_list.h:58
const Term & const_reference
Const reference to T.
Definition aterm_list.h:43
const term_list< Term > & tail() const
Returns the tail of the list.
Definition aterm_list.h:225
term_list(Iter first, Iter last, const ATermConverter &convert_to_aterm, typename std::enable_if< !std::is_base_of< std::bidirectional_iterator_tag, typename std::iterator_traits< Iter >::iterator_category >::value >::type *=nullptr)
Creates a term_list from the elements from first to last converting the elements before inserting.
Definition aterm_list.h:176
const Term & front() const
Returns the first element of the list.
Definition aterm_list.h:239
term_list(term_list< Term > &&t) noexcept
Move constructor.
Definition aterm_list.h:84
ptrdiff_t difference_type
A signed integral type.
Definition aterm_list.h:49
term_list & operator=(term_list &&other) noexcept=default
void pop_front()
Removes the first element of the list.
Definition aterm_list.h:232
term_list(const aterm &t) noexcept
Constructor from an aterm.
Definition aterm_list.h:67
term_list_iterator< Term > iterator
Iterator used to iterate through an term_list.
Definition aterm_list.h:52
Term & reference
Reference to T.
Definition aterm_list.h:40
const_iterator begin() const
Returns a const_iterator pointing to the beginning of the term_list.
Definition aterm_list.h:275
size_type max_size() const
Returns the largest possible size of the term_list.
Definition aterm_list.h:304
void emplace_front(Args &&... arguments)
Construct and insert a new element at the beginning of the current list.
term_list(Iter first, Iter last, const ATermConverter &convert_to_aterm, typename std::enable_if< std::is_base_of< std::bidirectional_iterator_tag, typename std::iterator_traits< Iter >::iterator_category >::value >::type *=0)
Creates a term_list with the elements from first to last converting the elements before inserting.
Definition aterm_list.h:117
term_list_iterator< Term > const_iterator
Const iterator used to iterate through an term_list.
Definition aterm_list.h:55
Term * pointer
Pointer to T.
Definition aterm_list.h:37
term_list(detail::_aterm_appl<> *t) noexcept
Constructor for term lists from internally constructed terms delivered as reference.
Definition aterm_list.h:27
bool empty() const
Returns true if the list's size is 0.
Definition aterm_list.h:268
term_list(std::initializer_list< Term > init)
A constructor based on an initializer list.
Definition aterm_list.h:214
term_list(Iter first, Iter last, typename std::enable_if< std::is_base_of< std::bidirectional_iterator_tag, typename std::iterator_traits< Iter >::iterator_category >::value >::type *=nullptr)
Creates a term_list with the elements from first to last.
Definition aterm_list.h:99
void push_front(const Term &el)
Inserts a new element at the beginning of the current list.
const_reverse_iterator rbegin() const
Returns a const_reverse_iterator pointing to the end of the term_list.
Definition aterm_list.h:290
term_list() noexcept
Default constructor. Creates an empty list.
Definition aterm_list.h:61
bool type_is_list() const noexcept
Dynamic check whether the term is an aterm_list.
Definition aterm_core.h:72
bool defined() const
Returns true if this term is not equal to the term assigned by the default constructor of aterms,...
Definition aterm_core.h:143
const detail::_aterm * m_term
Definition aterm_core.h:36
MCRL2_EXPORT bool init(rewriter_interface *i, RewriterCompilingJitty *this_rewriter)
_aterm * address(const unprotected_aterm_core &t)
Definition aterm_core.h:239
aterm_pool & g_term_pool()
obtain a reference to the global aterm pool.
aterm make_list_backward(Iter first, Iter last, ATermConverter convert_to_aterm)
Constructs a list starting from first to last. The iterators are traversed backwards and each element...
The main namespace for the aterm++ library.
Definition algorithm.h:21
void make_term_list(term_list< Term > &target)
Make an empty list and put it in target;.
Definition aterm_list.h:314
term_list< aterm > aterm_list
A term_list with elements of type aterm.
Definition aterm_list.h:497
std::set< T > as_set(const atermpp::term_list< T > &x)
Converts the given term list to a set.
Definition aterm_list.h:555
term_list< Term > sort_list(const term_list< Term > &l, const std::function< bool(const Term &, const Term &)> &ordering=[](const Term &t1, const Term &t2){ return t1< t2;})
Returns the list with the elements sorted according to the <-operator on the addresses of terms.
term_list< Term > reverse(const term_list< Term > &l)
Returns the list with the elements in reversed order.
std::conditional< std::is_convertible< Term2, Term1 >::value, term_list< Term1 >, term_list< Term2 > >::type operator+(const term_list< Term1 > &l, const term_list< Term2 > &m)
Returns the concatenation of two lists with convertible element types.
std::vector< T > as_vector(const atermpp::term_list< T > &x)
Converts the given term list to a vector.
Definition aterm_list.h:548
term_list< Term > push_back(const term_list< Term > &l, const Term &el)
Appends a new element at the end of the list. Note that the complexity of this function is O(n),...
const data_expression & arg(const data_expression &e)
Function for projecting out argument. arg from an application.
Definition bag1.h:1648
function_symbol head(const sort_expression &s)
Constructor for function symbol head.
Definition list1.h:526
function_symbol tail(const sort_expression &s)
Constructor for function symbol tail.
Definition list1.h:588
STL namespace.
void swap(atermpp::unprotected_aterm_core &t1, atermpp::unprotected_aterm_core &t2) noexcept
Swaps two aterms.
Definition aterm.h:462
std::size_t operator()(const atermpp::term_list< Term > &l) const
A specialization of the standard std::hash function.
Definition aterm_list.h:584
#define hash(l, r, m)
Definition tree_set.cpp:23