1 /* Copyright (c) 1997-2021
2    Ewgenij Gawrilow, Michael Joswig, and the polymake team
3    Technische Universität Berlin, Germany
4    https://polymake.org
5 
6    This program is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by the
8    Free Software Foundation; either version 2, or (at your option) any
9    later version: http://www.gnu.org/licenses/gpl.txt.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 --------------------------------------------------------------------------------
16 */
17 
18 #pragma once
19 
20 #if !defined(__clang__) && __GNUC__ >= 9
21 #pragma GCC diagnostic push
22 #pragma GCC diagnostic ignored "-Wpessimizing-move"
23 #endif
24 
25 namespace pm {
26 
27 template <typename Iterator1, typename Iterator2>
copy_range_impl(Iterator1 && src,Iterator2 && dst,std::true_type,std::false_type)28 void copy_range_impl(Iterator1&& src, Iterator2&& dst, std::true_type, std::false_type)
29 {
30    for (; !src.at_end(); ++src, ++dst) *dst=*src;
31 }
32 
33 template <typename Iterator1, typename Iterator2>
copy_range_impl(Iterator1 && src,Iterator2 && dst,std::false_type,std::true_type)34 void copy_range_impl(Iterator1&& src, Iterator2&& dst, std::false_type, std::true_type)
35 {
36    for (; !dst.at_end(); ++src, ++dst) *dst=*src;
37 }
38 
39 template <typename Iterator1, typename Iterator2>
copy_range_impl(Iterator1 && src,Iterator2 && dst,std::true_type,std::true_type)40 void copy_range_impl(Iterator1&& src, Iterator2&& dst, std::true_type, std::true_type)
41 {
42    for (; !src.at_end() && !dst.at_end(); ++src, ++dst) *dst=*src;
43 }
44 
45 template <typename Iterator1, typename Iterator2,
46           typename=std::enable_if_t<check_iterator_feature<Iterator1, end_sensitive>::value ||
47                                     check_iterator_feature<Iterator2, end_sensitive>::value>>
copy_range(Iterator1 && src,Iterator2 && dst)48 private_mutable_t<Iterator2> copy_range(Iterator1&& src, Iterator2&& dst)
49 {
50    private_mutable_t<Iterator2> dst_it=ensure_private_mutable(std::forward<Iterator2>(dst));
51    copy_range_impl(ensure_private_mutable(std::forward<Iterator1>(src)), dst_it,
52                    bool_constant<check_iterator_feature<Iterator1, end_sensitive>::value>(),
53                    bool_constant<check_iterator_feature<Iterator2, end_sensitive>::value>());
54    return std::move(dst_it);
55 }
56 
57 
58 template <typename Iterator1, typename Iterator2>
equal_ranges_impl(Iterator1 && it1,Iterator2 && it2,std::true_type,std::false_type)59 bool equal_ranges_impl(Iterator1&& it1, Iterator2&& it2, std::true_type, std::false_type)
60 {
61    for (; !it1.at_end(); ++it1, ++it2)
62       if (*it1 != *it2) return false;
63    return true;
64 }
65 
66 template <typename Iterator1, typename Iterator2>
equal_ranges_impl(Iterator1 && it1,Iterator2 && it2,std::false_type,std::true_type)67 bool equal_ranges_impl(Iterator1&& it1, Iterator2&& it2, std::false_type, std::true_type)
68 {
69    for (; !it2.at_end(); ++it1, ++it2)
70       if (*it1 != *it2) return false;
71    return true;
72 }
73 
74 template <typename Iterator1, typename Iterator2>
equal_ranges_impl(Iterator1 && it1,Iterator2 && it2,std::true_type,std::true_type)75 bool equal_ranges_impl(Iterator1&& it1, Iterator2&& it2, std::true_type, std::true_type)
76 {
77    for (; !it1.at_end() && !it2.at_end(); ++it1, ++it2)
78       if (*it1 != *it2) return false;
79    return it1.at_end() && it2.at_end();
80 }
81 
82 template <typename Iterator1, typename Iterator2,
83           typename=std::enable_if_t<check_iterator_feature<Iterator1, end_sensitive>::value ||
84                                     check_iterator_feature<Iterator2, end_sensitive>::value>>
equal_ranges(Iterator1 && it1,Iterator2 && it2)85 bool equal_ranges(Iterator1&& it1, Iterator2&& it2)
86 {
87    return equal_ranges_impl(ensure_private_mutable(std::forward<Iterator1>(it1)),
88                             ensure_private_mutable(std::forward<Iterator2>(it2)),
89                             bool_constant<check_iterator_feature<Iterator1, end_sensitive>::value>(),
90                             bool_constant<check_iterator_feature<Iterator2, end_sensitive>::value>());
91 }
92 
93 
94 template <typename Iterator1, typename Iterator2>
swap_ranges_impl(Iterator1 && it1,Iterator2 && it2,std::true_type,std::false_type)95 void swap_ranges_impl(Iterator1&& it1, Iterator2&& it2, std::true_type, std::false_type)
96 {
97    for (; !it1.at_end(); ++it1, ++it2) std::swap(*it1, *it2);
98 }
99 
100 template <typename Iterator1, typename Iterator2>
swap_ranges_impl(Iterator1 && it1,Iterator2 && it2,std::false_type,std::true_type)101 void swap_ranges_impl(Iterator1&& it1, Iterator2&& it2, std::false_type, std::true_type)
102 {
103    for (; !it2.at_end(); ++it1, ++it2) std::swap(*it1, *it2);
104 }
105 
106 template <typename Iterator1, typename Iterator2>
swap_ranges_impl(Iterator1 && it1,Iterator2 && it2,std::true_type,std::true_type)107 void swap_ranges_impl(Iterator1&& it1, Iterator2&& it2, std::true_type, std::true_type)
108 {
109    for (; !it1.at_end() && !it2.at_end(); ++it1, ++it2) std::swap(*it1, *it2);
110 }
111 
112 template <typename Iterator1, typename Iterator2,
113           typename=std::enable_if_t<check_iterator_feature<Iterator1, end_sensitive>::value ||
114                                     check_iterator_feature<Iterator2, end_sensitive>::value>>
swap_ranges(Iterator1 && it1,Iterator2 && it2)115 void swap_ranges(Iterator1&& it1, Iterator2&& it2)
116 {
117    swap_ranges_impl(ensure_private_mutable(std::forward<Iterator1>(it1)),
118                     ensure_private_mutable(std::forward<Iterator2>(it2)),
119                     bool_constant<check_iterator_feature<Iterator1, end_sensitive>::value>(),
120                     bool_constant<check_iterator_feature<Iterator2, end_sensitive>::value>());
121 }
122 
123 
124 template <typename Iterator,
125           typename=std::enable_if_t<check_iterator_feature<Iterator, end_sensitive>::value>>
126 typename iterator_traits<Iterator>::value_type
first_differ_in_range(Iterator && src,const typename iterator_traits<Iterator>::value_type & from)127 first_differ_in_range(Iterator&& src, const typename iterator_traits<Iterator>::value_type& from)
128 {
129    auto&& it=ensure_private_mutable(std::forward<Iterator>(src));
130    for (; !it.at_end(); ++it) {
131       const typename iterator_traits<Iterator>::value_type v=*it;
132       if (v != from) return v;
133    }
134    return from;
135 }
136 
137 template <typename Iterator, typename Value,
138           typename=std::enable_if_t<check_iterator_feature<Iterator, end_sensitive>::value>>
fill_range(Iterator && dst,const Value & x)139 void fill_range(Iterator&& dst, const Value& x)
140 {
141    auto&& it=ensure_private_mutable(std::forward<Iterator>(dst));
142    for (; !it.at_end(); ++it) *it=x;
143 }
144 
145 template <typename Iterator, typename Value,
146           typename=std::enable_if_t<check_iterator_feature<Iterator, end_sensitive>::value>>
147 private_mutable_t<Iterator>
find_in_range(Iterator && src,const Value & x)148 find_in_range(Iterator&& src, const Value& x)
149 {
150    private_mutable_t<Iterator> it=ensure_private_mutable(std::forward<Iterator>(src));
151    while (!it.at_end() && *it != x) ++it;
152    return std::move(it);
153 }
154 
155 template <typename Iterator, typename Predicate,
156           typename=std::enable_if_t<check_iterator_feature<Iterator, end_sensitive>::value>>
157 private_mutable_t<Iterator>
find_in_range_if(Iterator && src,const Predicate & pred_arg)158 find_in_range_if(Iterator&& src, const Predicate& pred_arg)
159 {
160    typedef pm::unary_helper<pure_type_t<Iterator>, Predicate> helper;
161    const typename helper::operation& pred=helper::create(pred_arg);
162    private_mutable_t<Iterator> it=ensure_private_mutable(std::forward<Iterator>(src));
163    while (!it.at_end() && !pred(*helper::get(it))) ++it;
164    return std::move(it);
165 }
166 
167 } // end namespace pm
168 
169 #if !defined(__clang__) && __GNUC__ >= 9
170 #pragma GCC diagnostic pop
171 #endif
172 
173 
174 // Local Variables:
175 // mode:C++
176 // c-basic-offset:3
177 // indent-tabs-mode:nil
178 // End:
179