2
3
4
5
6
7
8
9
10
12#ifndef IPADDRESS_IP_FUNCTIONS_HPP
13#define IPADDRESS_IP_FUNCTIONS_HPP
22template <
typename T1,
typename T2>
23struct ip_address_type {
27 std::is_same<T1, T2>::value,
28 "The IP address versions must match or at least be ip_address."
31 using type =
typename std::conditional<
38template <
typename T1,
typename T2>
39struct ip_network_type {
43 std::is_same<T1, T2>::value,
44 "The IP network versions must match or at least be ip_network."
47 using type =
typename std::conditional<
54template <
typename... Args>
55struct is_ip_network_types {
56 static constexpr bool value =
false;
59template <
typename T,
typename... Args>
60struct is_ip_network_types<T, Args...> {
61 static constexpr bool value = is_ip_network_types<T>::value && (
sizeof...(Args) > 0 ? is_ip_network_types<Args...>::value :
true);
65struct is_ip_network_types<T> {
66 static constexpr bool value = std::is_same<T, ipv4_network>::value || std::is_same<T, ipv6_network>::value || std::is_same<T,
ip_network>::value;
69template <
typename... Args>
70struct ip_network_type_extract;
72template <
typename T1,
typename T2,
typename... Args>
73struct ip_network_type_extract<T1, T2, Args...> {
74 using type =
typename ip_network_type<
typename ip_network_type<T1, T2>::type,
typename ip_network_type_extract<Args...>::type>::type;
77template <
typename T1,
typename T2>
78struct ip_network_type_extract<T1, T2> {
79 using type =
typename ip_network_type<T1, T2>::type;
83struct ip_network_type_extract<T> {
84 using type =
typename ip_network_type<T, T>::type;
88struct summarize_sequence_type;
91struct summarize_sequence_type<ipv4_address> {
92 using type = summarize_sequence<ipv4_network>;
96struct summarize_sequence_type<ipv6_address> {
97 using type = summarize_sequence<ipv6_network>;
102 using type = summarize_sequence<ip_network, ip_any_summarize_iterator>;
105template <
typename Key,
typename Value>
111template <
typename Key,
typename Value>
114 return item.key < key;
118template <
typename It,
typename T,
typename Cmp = std::less<T>>
120 auto size = last - first;
122 auto half = size / 2;
125 if (cmp(*middle, value)) {
128 size = size - half - 1;
137#if defined(__clang__
) && !defined(__apple_build_version__) && __clang_major__
<= 8
144template <
typename Ip>
147 if (first.version() != last.version()) {
155 return { first, last };
158template <size_t N,
typename It>
160 ->
fixed_vector<
typename std::iterator_traits<It>::value_type, N> {
161 using network_type =
typename std::iterator_traits<It>::value_type;
162 using address_type =
typename network_type::ip_address_type;
164 const auto version = first->version();
165 const auto max_prefixlen = version == ip_version::V4
166 ? ipv4_network::base_max_prefixlen
167 : ipv6_network::base_max_prefixlen;
171 fixed_vector<key_value<network_type, network_type>, N> subnets;
173 for (
auto it = first; it != last; ++it) {
174 const auto& net = *it;
175 if (net.version() != version) {
179 if (net.prefixlen() != max_prefixlen) {
180 nets.emplace_back(net);
182 const auto network_address = net.network_address();
183 auto lower = find_lower_bound(ips.begin(), ips.end(), network_address);
184 if (lower == ips.end() || *lower != network_address) {
185 ips.insert(lower, network_address);
190 auto it = ips.begin();
193 auto lastUint = (
typename network_type::uint_type) last;
194 for (; it != ips.end(); ++it) {
195 const auto ipUint = (
typename network_type::uint_type) *it;
196 if (ipUint != lastUint + 1) {
197 auto range = summarize_address_range(first, last, code);
201 for (
const auto& net : range) {
202 nets.emplace_back(net);
209 auto range = summarize_address_range(first, last, code);
213 for (
const auto& net : range) {
214 nets.emplace_back(net);
217 while (!nets.empty()) {
218 const auto net = nets.back();
219 const auto supernet = net.supernet();
221 auto it = find_lower_bound(subnets.begin(), subnets.end(), supernet, less_pair<network_type, network_type>{});
222 if (it != subnets.end() && it->key == supernet) {
223 if (it->value != net) {
225 nets.emplace_back(supernet);
228 subnets.emplace(it, supernet, net);
232 if (!subnets.empty()) {
234 for (
auto it = subnets.begin(); it != subnets.end(); ++it) {
235 auto lower = find_lower_bound(subnet_values.begin(), subnet_values.end(), it->key);
236 subnet_values.insert(lower, it->value);
238 auto it = subnet_values.begin();
240 result.emplace_back(last);
242 for (; it != subnet_values.end(); ++it) {
243 if (last.broadcast_address() >= it->broadcast_address()) {
247 result.emplace_back(last);
253template <
typename Result,
typename It>
255 using network_type =
typename std::iterator_traits<It>::value_type;
256 using address_type =
typename network_type::ip_address_type;
263 const auto version = first->version();
264 const auto max_prefixlen = version == ip_version::V4
265 ? ipv4_network::base_max_prefixlen
266 : ipv6_network::base_max_prefixlen;
268 std::vector<network_type> nets;
269 std::set<address_type> ips;
270 std::map<network_type, network_type> subnets;
272 for (
auto it = first; it != last; ++it) {
273 const auto& net = *it;
274 if (net.version() != version) {
278 if (net.prefixlen() != max_prefixlen) {
279 nets.emplace_back(net);
281 const auto network_address = net.network_address();
282 ips.insert(network_address);
286 auto it = ips.begin();
289 auto lastUint = (
typename network_type::uint_type) last;
290 for (; it != ips.end(); ++it) {
291 const auto ipUint = (
typename network_type::uint_type) *it;
292 if (ipUint != lastUint + 1) {
293 auto range = summarize_address_range(first, last, code);
294 if (code != error_code::no_error) {
297 for (
const auto& net : range) {
298 nets.emplace_back(net);
305 auto range = summarize_address_range(first, last, code);
309 for (
const auto& net : range) {
310 nets.emplace_back(net);
313 while (!nets.empty()) {
314 const auto net = nets.back();
315 const auto supernet = net.supernet();
317 auto existing = subnets.find(supernet);
318 if (existing != subnets.end()) {
319 subnets.erase(existing);
320 nets.emplace_back(supernet);
322 subnets.emplace(std::make_pair(supernet, net));
325 if (!subnets.empty()) {
326 std::vector<network_type> subnet_values;
327 subnet_values.reserve(subnets.size());
328 for (
auto it = subnets.begin(); it != subnets.end(); ++it) {
329 subnet_values.emplace_back(it->second);
331 std::sort(subnet_values.begin(), subnet_values.end());
332 auto it = subnet_values.begin();
334 result.emplace_back(last);
336 for (; it != subnet_values.end(); ++it) {
337 if (last.broadcast_address() >= it->broadcast_address()) {
341 result.emplace_back(last);
347template <size_t N,
typename It>
349 ->
fixed_vector<
typename std::iterator_traits<It>::value_type, N> {
350 using result_type =
fixed_vector<
typename std::iterator_traits<It>::value_type, N>;
360 ? fixed_collapse_addresses<N>(first, last, code)
361 : runtime_collapse_addresses<result_type>(first, last, code);
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
410 ->
decltype(internal::summarize_address_range(
411 typename internal::ip_address_type<FirstIp, LastIp>::type{first},
412 typename internal::ip_address_type<FirstIp, LastIp>::type{last},
414 return internal::summarize_address_range(
typename internal::ip_address_type<FirstIp, LastIp>::type{first},
typename internal::ip_address_type<FirstIp, LastIp>::type{last}, code);
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
454 const auto result = summarize_address_range(first, last, code);
456 raise_error(code, 0,
"", 0);
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
499 return N != 0 ? internal::collapse_addresses<N>(&nets[0], &nets[0] + nets.size(), code) :
fixed_vector<Net, N>{};
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
540 return internal::collapse_addresses<N>(&nets[0], &nets[0] + N, code);
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
578IPADDRESS_EXPORT template <
typename... Nets,
typename std::enable_if<internal::is_ip_network_types<Nets...>::value,
bool>::type =
true>
580 ->
fixed_vector<
typename internal::ip_network_type_extract<Nets...>::
type,
sizeof...(Nets)> {
581 using Net =
typename internal::ip_network_type_extract<Nets...>::type;
582 const Net net_array[] = { Net(nets)... };
583 return collapse_addresses(net_array, code);
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
624 return internal::runtime_collapse_addresses<std::vector<
typename std::iterator_traits<It>::value_type>>(first, last, code);
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
659 const auto result = collapse_addresses(nets, code);
661 raise_error(code, 0,
"", 0);
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
698 const auto result = collapse_addresses(nets, code);
700 raise_error(code, 0,
"", 0);
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
732IPADDRESS_EXPORT template <
typename... Nets,
typename std::enable_if<internal::is_ip_network_types<Nets...>::value,
bool>::type =
true>
734 ->
fixed_vector<
typename internal::ip_network_type_extract<Nets...>::
type,
sizeof...(Nets)> {
736 const auto result = collapse_addresses(code, nets...);
738 raise_error(code, 0,
"", 0);
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
779 const auto result = collapse_addresses(first, last, code);
781 raise_error(code, 0,
"", 0);
783 return std::move(result);
A fixed-size vector class template.
Definition fixed-vector.hpp:308
A class that represents an IP address, supporting both IPv4 and IPv6 formats.
Definition ip-any-address.hpp:73
A class that encapsulates both IPv4 and IPv6 network functionalities.
Definition ip-any-network.hpp:67
#define IPADDRESS_NOEXCEPT_WHEN_NO_EXCEPTIONS
Definition config.hpp:96
#define IPADDRESS_EXPORT
Definition config.hpp:45
#define IPADDRESS_NODISCARD
Definition config.hpp:101
#define IPADDRESS_IS_CONST_EVALUATED(x)
Definition config.hpp:154
#define IPADDRESS_FORCE_INLINE
Definition config.hpp:115
#define IPADDRESS_NAMESPACE
Definition config.hpp:41
#define IPADDRESS_NOEXCEPT
Definition config.hpp:92
constexpr inline auto collapse_addresses(It first, It last) -> std::vector< typename std::iterator_traits< It >::value_type >
Collapses a collection of IP networks into the smallest set of contiguous networks.
Definition ip-functions.hpp:776
constexpr inline auto collapse_addresses(const Nets &... nets) -> fixed_vector< typename internal::ip_network_type_extract< Nets... >::type, sizeof...(Nets)>
Collapses a collection of IP networks into the smallest set of contiguous networks.
Definition ip-functions.hpp:733
constexpr inline auto summarize_address_range(const FirstIp &first, const LastIp &last) -> decltype(summarize_address_range(first, last, *std::declval< error_code * >()))
Summarizes an IP address range into the smallest set of contiguous network blocks.
Definition ip-functions.hpp:451
constexpr inline fixed_vector< Net, N > collapse_addresses(const std::array< Net, N > &nets)
Collapses a collection of IP networks into the smallest set of contiguous networks.
Definition ip-functions.hpp:657
constexpr inline auto collapse_addresses(It first, It last, error_code &code) noexcept -> std::vector< typename std::iterator_traits< It >::value_type >
Collapses a collection of IP networks into the smallest set of contiguous networks.
Definition ip-functions.hpp:622
constexpr inline auto summarize_address_range(const FirstIp &first, const LastIp &last, error_code &code) noexcept -> decltype(internal::summarize_address_range(typename internal::ip_address_type< FirstIp, LastIp >::type{first}, typename internal::ip_address_type< FirstIp, LastIp >::type{last}, code))
Summarizes an IP address range into the smallest set of contiguous network blocks.
Definition ip-functions.hpp:409
error_code
Enumeration of error codes for IP address parsing and validation.
Definition errors.hpp:52
@ last_address_must_be_greater_than_first
The last IP address in the range must be greater than the first IP address.
@ invalid_version
The IP address version does not match the expected version.
@ no_error
Indicates the absence of any errors.
constexpr inline fixed_vector< Net, N > collapse_addresses(const std::array< Net, N > &nets, error_code &code) noexcept
Collapses a collection of IP networks into the smallest set of contiguous networks.
Definition ip-functions.hpp:498
constexpr inline fixed_vector< Net, N > collapse_addresses(const Net(&nets)[N])
Collapses a collection of IP networks into the smallest set of contiguous networks.
Definition ip-functions.hpp:696
constexpr inline auto collapse_addresses(error_code &code, const Nets &... nets) noexcept -> fixed_vector< typename internal::ip_network_type_extract< Nets... >::type, sizeof...(Nets)>
Collapses a collection of IP networks into the smallest set of contiguous networks.
Definition ip-functions.hpp:579
constexpr inline fixed_vector< Net, N > collapse_addresses(const Net(&nets)[N], error_code &code) noexcept
Collapses a collection of IP networks into the smallest set of contiguous networks.
Definition ip-functions.hpp:539