11 #ifndef __UTILITY_COLLECTIONS_H__
12 #define __UTILITY_COLLECTIONS_H__
22 template<
typename BIter,
typename EIter = BIter>
27 template<
typename BIter_C,
typename EIter_C = BIter_C>
29 :
b(std::forward<BIter_C>(b_))
30 ,
e(std::forward<EIter_C>(e_))
38 const BIter&
begin()
const {
return b; }
39 BIter&
begin() {
return b; }
41 const EIter&
end()
const {
return e; }
42 EIter&
end() {
return e; }
47 template<
typename BIter,
typename EIter = BIter>
50 return irange_type(std::forward<BIter>(b), std::forward<EIter>(e));
53 template<
typename BIter,
typename EIter = BIter>
56 return irange_type(p.first, p.second);
59 template<typename Range, typename = decltype(begin(std::declval<Range>()))>
71 template<
int get_index,
typename Range>
74 using Biter = std::remove_cv_t<std::remove_reference_t<decltype(
r.begin())>>;
75 using Eiter = std::remove_cv_t<std::remove_reference_t<decltype(
r.end())>>;
77 template<
typename Iter>
82 auto&
operator*()
const {
using std::get;
return get<get_index>(*
impl); }
86 template<
typename RHSIter>
88 template<
typename RHSIter>
90 template<
typename RHSIter>
100 template<
int get_index,
typename Range>
105 template<
typename Range>
110 template<
typename Range>
115 template<
typename Range>
124 template<
typename VertexID>
129 auto end()
const {
return &
v + 1; }
131 template<
typename Q>
auto find(
const Q& q)
const {
return (
v == q) ?
begin() :
end(); }
132 template<
typename Q> std::ptrdiff_t
count(
const Q& q) {
return (
v == q) ? 1 : 0; }
142 template<
typename VertexID>
144 return { std::move(v) };
147 template<
template <
typename...>
class Map,
typename... InitialParams>
149 template<
typename... RestParams>
151 return Map<InitialParams..., RestParams...>();
155 template<
template <
typename...>
class Set,
typename... InitialParams>
157 template<
typename... RestParams>
159 return Set<InitialParams..., RestParams...>();
167 template<
typename IndexMap,
typename Key,
typename MappedType>
173 typename DataStore::const_iterator
impl;
180 typename DataStore::iterator
impl;
191 ,
data(size, {
imap.invalid_key(), MappedType()}) {}
193 const MappedType&
at(
const Key& k)
const {
196 MappedType&
at(
const Key& k) {
206 auto i =
iterAt(vt.first);
208 if (had_no_data) *i = vt;
209 return {i, had_no_data};
221 std::size_t
count(
const Key& k)
const {
return find(k) ==
end() ? 0 : 1; }
238 template<
typename IndexMap,
typename Value>
244 typename DataStore::const_iterator
impl;
251 typename DataStore::iterator
impl;
267 if (had_no_data) *i = vt;
268 return {i, had_no_data};
294 template<
typename... InitialParams>
297 template<
typename... RestParams>
303 template<
typename... InitialParams>
306 template<
typename...>
316 template<
typename COLLECTION,
typename UNARY_PREDICATE>
319 std::copy_if(
begin(base_container),
end(base_container), std::inserter(result,
end(result)),
filter);
327 template<
typename R1,
typename R2>
338 template<
typename ASSOCIATIVE_COLLECTION,
typename KEY,
typename DEFAULT_VALUE>
339 auto value_for_key_or(ASSOCIATIVE_COLLECTION& assoc_collection,
const KEY& key, DEFAULT_VALUE& default_value)
340 -> std::conditional_t<
341 std::is_lvalue_reference<DEFAULT_VALUE>::value,
342 decltype(
false ? default_value : assoc_collection.find(key)->second)&,
343 decltype(
false ? default_value : assoc_collection.find(key)->second)
346 const auto search_result = assoc_collection.find(key);
347 if (search_result ==
end(assoc_collection)) {
348 return default_value;
350 return search_result->second;
357 template<
typename ASSOCIATIVE_COLLECTION,
typename KEY,
typename VALUE>
358 auto insert_or_assign(ASSOCIATIVE_COLLECTION& assoc_collection, KEY&& key, VALUE&& value) {
359 const auto search_result = assoc_collection.find(key);
360 if (search_result ==
end(assoc_collection)) {
361 return assoc_collection.emplace(std::forward<KEY>(key), std::forward<VALUE>(value));
363 search_result->second = std::forward<KEY>(value);
364 return std::make_pair(search_result,
true);
372 template<
typename InputIt,
typename ForwardIt,
typename BinaryPredicate>
374 InputIt first, InputIt last,
375 ForwardIt s_first, ForwardIt s_last,
378 for (; first != last; ++first) {
379 for (ForwardIt it = s_first; it != s_last; ++it) {
380 if (p(*first, *it)) {
385 return {last, s_last};
389 template<
typename InputIt,
typename ForwardIt>
391 InputIt first, InputIt last,
392 ForwardIt s_first, ForwardIt s_last
395 first, last, s_first, s_last,
396 [](
auto&& lhs,
auto&& rhs) {
return lhs == rhs; }
401 template<
typename Range1,
typename Range2,
typename BinaryPredicate>
408 template<
typename Range1,
typename Range2>
416 template<
typename STREAM,
typename T>
433 typename T1,
typename T2,
typename T3
438 const T1& element_separator,
439 const T2& container_prefix,
440 const T3& container_suffix,
441 PRINTER&& element_printer = PRINTER{}
447 os << container_prefix;
449 element_printer(os,*beg);
450 std::for_each(std::next(beg), en, [&](
const auto& v){
451 os << element_separator;
452 element_printer(os,v);
455 os << container_suffix;
466 template<
typename CONTAINER,
typename OSTREAM,
typename PRINTER = detail::shift_in_pr
inter>
470 PRINTER&& element_printer = PRINTER{}
482 typename ASSOC_CONTAINER,
486 typename T1,
typename T2,
typename T3,
typename T4,
typename T5,
typename T6
490 const ASSOC_CONTAINER& c,
491 const T1& element_separator,
492 const T2& key_value_separator,
493 const T3& element_prefix,
494 const T4& element_suffix,
495 const T5& container_prefix,
496 const T6& container_suffix,
497 KEY_PRINTER&& value_printer = KEY_PRINTER{},
498 VALUE_PRINTER&& key_printer = VALUE_PRINTER{}
504 os << container_prefix;
506 os << element_prefix;
507 key_printer(os,beg->first);
508 os << key_value_separator;
509 value_printer(os,beg->second);
510 os << element_suffix;
511 std::for_each(std::next(beg), en, [&](
const auto& v){
512 os << element_separator;
513 os << element_prefix;
514 key_printer(os,v.first);
515 os << key_value_separator;
516 value_printer(os,v.second);
517 os << element_suffix;
520 os << container_suffix;
531 template<
typename ASSOC_CONTAINER,
typename OSTREAM,
typename KEY_PRINTER = detail::shift_in_pr
inter,
typename VALUE_PRINTER = detail::shift_in_pr
inter>
534 const ASSOC_CONTAINER& c,
535 KEY_PRINTER&& value_printer = KEY_PRINTER{},
536 VALUE_PRINTER&& key_printer = VALUE_PRINTER{}
538 print_assoc_container(os, c,
", ",
" -> ",
"{ ",
" }",
"{",
"}", value_printer, key_printer);
541 template<
typename T = std::vector<
int>>
544 std::iota(result.begin(), result.end(), 0);