2
3
4
5
6
7
8
9
10
11
12
14#ifndef IPADDRESS_IP_NETWORK_ITERATOR_HPP
15#define IPADDRESS_IP_NETWORK_ITERATOR_HPP
23
24
25
26
27
28
29
30
31
35 using iterator_category = std::random_access_iterator_tag;
37 using difference_type = int64_t;
38 using pointer =
const value_type*;
39 using reference =
const value_type&;
41 using ip_address_type =
typename value_type::ip_address_type;
42 using uint_type =
typename value_type::uint_type;
45
46
50
51
52
53
54
55
56
62
63
64
65
66
67
68
70 return _it.uint_diff(other._it) / _step;
74
75
76
77
83
84
85
86
92
93
94
95
96
98 const auto& it = *
this;
99 return it[uint_type(n)];
103
104
105
106
107
109 const auto address = _it + _step * n;
110 return value_type::from_address(*address, _prefixlen);
114
115
116
117
118
119
126
127
128
129
130
131
139
140
141
142
143
144
151
152
153
154
155
156
164
165
166
167
168
169
170
177
178
179
180
181
182
183
190
191
192
193
194
195
196
203
204
205
206
207
208
209
216
217
218
219
220
221
222
230
231
232
233
234
235
236
244
245
246
247
248
249
250
251
257
258
259
260
261
262
263
264
270
271
272
273
274
275
276
284
285
286
287
288
289
290
298
299
300
301
302
303
304
306 return difference_type(_it - other._it);
310
311
312
313
314
315
316
318 return _it._carry == other._it._carry && _current == other._current;
322
323
324
325
326
327
328
330 return !(*
this == other);
333#ifdef IPADDRESS_HAS_SPACESHIP_OPERATOR
336
337
338
339
340
341
342
354
355
356
357
358
359
360
362 return _it._carry < other._it._carry || (_it._carry == other._it._carry && _current < other._current);
366
367
368
369
370
371
372
374 return !(other < *
this);
378
379
380
381
382
383
384
386 return other < *
this;
390
391
392
393
394
395
396
398 return !(*
this < other);
406 const auto old = result._it._offset;
407 result._it._offset += _step;
408 if (result._it._offset < old) {
409 result._it._carry = 1 - result._it._carry;
411 result._it._current = ip_address_type::from_uint(result._it._offset);
412 result._current = value_type::from_address(*result._it, result._prefixlen);
418 const auto old = result._it._offset;
419 result._it._offset -= _step;
420 if (result._it._offset > old) {
421 result._it._carry = 1 - result._it._carry;
423 result._it._current = ip_address_type::from_uint(result._it._offset);
424 result._current = value_type::from_address(*result._it, result._prefixlen);
430 _current = value_type::from_address(*_it, _prefixlen);
435 _current = value_type::from_address(*_it, _prefixlen);
441 template <
typename,
typename,
typename>
444 value_type _current{};
446 ip_address_iterator<ip_address_type> _it{};
452
453
454
455
456
457
458
459
460
464 using iterator_category = std::forward_iterator_tag;
465 using value_type = T;
466 using difference_type = int64_t;
467 using pointer =
const value_type*;
468 using reference =
const value_type&;
471
472
476
477
478
479
480
482 const auto subnets = network.subnets();
489
490
491
492
498
499
500
501
507
508
509
510
512 if (_s1 != _other && _s2 != _other) {
513 if (_other.subnet_of(_s1)) {
514 if (_current != _s2) {
516 const auto subnets = _s1.subnets();
522 }
else if (_other.subnet_of(_s2)) {
523 if (_current != _s1) {
525 const auto subnets = _s2.subnets();
532 #ifndef IPADDRESS_NO_EXCEPTIONS
533 throw std::out_of_range(
"index out of range");
538 if (_current != _s2) {
543 }
else if (_s2 == _other) {
544 if (_current != _s1) {
550 #ifndef IPADDRESS_NO_EXCEPTIONS
551 throw std::out_of_range(
"index out of range");
559
560
561
562
570
571
572
573
574
575
576
578 return _current == other._current;
582
583
584
585
586
587
588
590 return !(*
this == other);
593#ifdef IPADDRESS_HAS_SPACESHIP_OPERATOR
596
597
598
599
600
601
602
610
611
612
613
614
615
616
618 return other._current < _current;
622
623
624
625
626
627
628
630 return !(other < *
this);
634
635
636
637
638
639
640
642 return other < *
this;
646
647
648
649
650
651
652
654 return !(*
this < other);
663 value_type _current{};
667
668
669
670
671
672
673
674
678 using iterator_category = std::forward_iterator_tag;
679 using value_type = T;
680 using difference_type =
typename value_type::uint_type;
681 using pointer =
const value_type*;
682 using reference =
const value_type&;
684 using ip_address_type =
typename value_type::ip_address_type;
685 using uint_type =
typename value_type::uint_type;
688
689
694
695
696
697
698
700 : _current((uint_type) current), _last((uint_type) last), _end(_current > _last) {
707
708
709
710
716
717
718
719
725
726
727
728
729
730
737
738
739
740
741
742
750
751
752
753
754
755
756
758 if (_end && other._end) {
761 return _current == other._current && _last == other._last && _end == other._end;
765
766
767
768
769
770
771
773 return !(*
this == other);
790 number = (~number & (number - 1));
792 while (number != 0) {
793 count += size_t(number & 0x1);
805 constexpr auto max_prefixlen = ip_address_type::base_max_prefixlen;
807 auto nbits = count_righthand_zero_bits(_current, max_prefixlen);
808 auto nbits_max = bit_length(_last - _current + 1) - 1;
809 _nbits = nbits < nbits_max ? nbits : nbits_max;
811 const auto prefixlen = max_prefixlen - _nbits;
812 const auto address = ip_address_type::from_uint(_current);
813 _network = value_type::from_address(address, prefixlen);
818 const auto block = _nbits != 0 ? uint_type(1) << (_nbits - 1) << 1 : uint_type(1);
820 IPADDRESS_CONSTEXPR
auto all_ones = ~uint_type{};
821 if (_current >= all_ones - block + 1) {
825 if (_current > _last) {
834 uint_type _current{};
838 value_type _network{};
842
843
844
845
846
847
848
849
850
851
855 using value_type = T;
857 using difference_type =
typename value_type::uint_type;
858 using pointer = value_type*;
859 using const_pointer =
const value_type*;
860 using reference = value_type&;
861 using const_reference =
const value_type&;
869 using ip_address_type =
typename value_type::ip_address_type;
872
873
877
878
879
880
881
882
883
884
885
887 const auto begin = ip_address_type::from_bytes(network_address.bytes());
888 const auto end = ip_address_type::from_uint(network_address.to_uint() + 1);
889 const auto step = difference_type(1);
890 _begin = const_iterator(begin, step, new_prefixlen);
891 _end = const_iterator(end, step, new_prefixlen, end < begin ? 1 : 0);
896
897
898
899
900
901
902
903
904
905
906
908 const auto begin_uint = network_address.to_uint();
909 const auto end_uint = broadcast_address.to_uint();
910 const auto begin = ip_address_type::from_uint(begin_uint);
911 const auto end = ip_address_type::from_uint(end_uint + 1);
912 const auto step = (hostmask.to_uint() >> prefixlen_diff) + 1;
913 _begin = const_iterator(begin, step, new_prefixlen);
914 _end = const_iterator(end, step, new_prefixlen, begin == end ? 1 : 0);
915 _size = (end_uint - begin_uint) / step + 1;
919
920
921
922
928
929
930
931
937
938
939
940
942 return const_reverse_iterator(
end());
946
947
948
949
951 return const_reverse_iterator(
begin());
955
956
957
958
964
965
966
967
973
974
975
976
978 return const_reverse_iterator(
cend());
982
983
984
985
987 return const_reverse_iterator(
cbegin());
991
992
993
994
996 return _begin == _end;
1000
1001
1002
1003
1009
1010
1011
1012
1013
1019
1020
1021
1022
1023
1025 return *(_begin + n);
1029
1030
1031
1032
1038
1039
1040
1041
1043 return *(_end - 1U);
1047 const_iterator _begin{};
1048 const_iterator _end{};
1049 difference_type _size{};
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1067 using value_type = T;
1069 using difference_type =
typename value_type::uint_type;
1070 using pointer = value_type*;
1071 using const_pointer =
const value_type*;
1072 using reference = value_type&;
1073 using const_reference =
const value_type&;
1078
1079
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1094 : _begin(const_iterator(network, other)) {
1098
1099
1100
1101
1107
1108
1109
1110
1116
1117
1118
1119
1125
1126
1127
1128
1134
1135
1136
1137
1139 return _begin == _end;
1143 const_iterator _begin{};
1144 const_iterator _end{};
1148
1149
1150
1151
1152
1153
1154
1155
1159 using value_type = T;
1161 using difference_type =
typename It<value_type>::difference_type;
1162 using pointer = value_type*;
1163 using const_pointer =
const value_type*;
1164 using reference = value_type&;
1165 using const_reference =
const value_type&;
1166 using iterator = It<value_type>;
1167 using const_iterator = It<value_type>;
1169 using ip_address_type =
typename value_type::ip_address_type;
1172
1173
1177
1178
1179
1180
1181
1183 : _begin(const_iterator(first, last)) {
1187
1188
1189
1190
1196
1197
1198
1199
1200
1201
1203 return const_iterator();
1207
1208
1209
1210
1211
1212
1218
1219
1220
1221
1222
1223
1229 const_iterator _begin{};
A sequence container for networks excluding specified subnets.
Definition ip-network-iterator.hpp:1065
constexpr inline const_iterator begin() const noexcept
Gets the beginning iterator of the sequence.
Definition ip-network-iterator.hpp:1102
constexpr inline bool empty() const noexcept
Checks if the sequence is empty.
Definition ip-network-iterator.hpp:1138
constexpr inline exclude_network_sequence() noexcept=default
Default constructor.
constexpr inline const_iterator cend() const noexcept
Gets the end const iterator of the sequence.
Definition ip-network-iterator.hpp:1129
constexpr inline exclude_network_sequence(const_reference network, const_reference other) noexcept
Constructs a sequence for a network, excluding addresses from another network.
Definition ip-network-iterator.hpp:1093
constexpr inline const_iterator cbegin() const noexcept
Gets the beginning const iterator of the sequence.
Definition ip-network-iterator.hpp:1120
constexpr inline const_iterator end() const noexcept
Gets the end iterator of the sequence.
Definition ip-network-iterator.hpp:1111
An iterator for unified traversal over IPv4 and IPv6 address spaces.
Definition ip-any-iterator.hpp:45
An iterator to traverse IP addresses within a network, excluding specified subnets.
Definition ip-network-iterator.hpp:462
constexpr inline bool operator>=(const ip_exclude_network_iterator &other) const noexcept
Greater-than-or-equal-to operator.
Definition ip-network-iterator.hpp:653
constexpr inline ip_exclude_network_iterator operator++(int)
Post-increment operator.
Definition ip-network-iterator.hpp:563
constexpr inline bool operator<=(const ip_exclude_network_iterator &other) const noexcept
Less-than-or-equal-to operator.
Definition ip-network-iterator.hpp:629
constexpr inline ip_exclude_network_iterator(reference network, reference other) noexcept
Constructs an iterator for a network, excluding addresses from another network.
Definition ip-network-iterator.hpp:481
constexpr inline ip_exclude_network_iterator() noexcept=default
Default constructor.
constexpr inline bool operator!=(const ip_exclude_network_iterator &other) const noexcept
Inequality operator.
Definition ip-network-iterator.hpp:589
constexpr inline bool operator>(const ip_exclude_network_iterator &other) const noexcept
Greater-than operator.
Definition ip-network-iterator.hpp:641
constexpr inline ip_exclude_network_iterator & operator++()
Pre-increment operator.
Definition ip-network-iterator.hpp:511
constexpr inline pointer operator->() const noexcept
Returns a pointer to the current element.
Definition ip-network-iterator.hpp:502
constexpr inline bool operator==(const ip_exclude_network_iterator &other) const noexcept
Equality operator.
Definition ip-network-iterator.hpp:577
constexpr inline bool operator<(const ip_exclude_network_iterator &other) const noexcept
Less-than operator.
Definition ip-network-iterator.hpp:617
constexpr inline reference operator*() const noexcept
Returns a reference to the current element.
Definition ip-network-iterator.hpp:493
An iterator for traversing IP addresses within a network range.
Definition ip-network-iterator.hpp:33
constexpr inline bool operator>(const ip_network_iterator &other) const noexcept
Greater-than operator.
Definition ip-network-iterator.hpp:385
friend constexpr inline ip_network_iterator operator+(const uint_type &n, const ip_network_iterator &it) noexcept
Addition operator.
Definition ip-network-iterator.hpp:265
constexpr inline ip_network_iterator & operator+=(const uint_type &n) noexcept
Addition assignment operator.
Definition ip-network-iterator.hpp:184
constexpr inline ip_network_iterator operator+(const uint_type &n) const noexcept
Addition operator.
Definition ip-network-iterator.hpp:237
constexpr inline value_type operator[](difference_type n) const noexcept
Accesses an element by index.
Definition ip-network-iterator.hpp:97
constexpr inline value_type operator[](const uint_type &n) const noexcept
Accesses an element by index.
Definition ip-network-iterator.hpp:108
constexpr inline ip_network_iterator(const ip_address_type &ref, const uint_type &step, size_t prefixlen, int carry=0) noexcept
Constructs an iterator with a reference IP address, step size, prefix length, and carry.
Definition ip-network-iterator.hpp:57
constexpr inline ip_network_iterator & operator+=(difference_type n) noexcept
Addition assignment operator.
Definition ip-network-iterator.hpp:171
constexpr inline bool operator<(const ip_network_iterator &other) const noexcept
Less-than operator.
Definition ip-network-iterator.hpp:361
constexpr inline bool operator<=(const ip_network_iterator &other) const noexcept
Less-than-or-equal-to operator.
Definition ip-network-iterator.hpp:373
constexpr inline ip_network_iterator operator+(difference_type n) const noexcept
Addition operator.
Definition ip-network-iterator.hpp:223
friend constexpr inline ip_network_iterator operator+(difference_type n, const ip_network_iterator &it) noexcept
Addition operator.
Definition ip-network-iterator.hpp:252
constexpr inline ip_network_iterator() noexcept=default
Default constructor.
constexpr inline bool operator==(const ip_network_iterator &other) const noexcept
Equality operator.
Definition ip-network-iterator.hpp:317
constexpr inline ip_network_iterator operator--(int) noexcept
Post-decrement operator.
Definition ip-network-iterator.hpp:157
constexpr inline ip_network_iterator & operator-=(difference_type n) noexcept
Subtraction assignment operator.
Definition ip-network-iterator.hpp:197
constexpr inline uint_type uint_diff(const ip_network_iterator &other) const noexcept
Calculates the difference in the number of elements between this and another ip_network_iterator.
Definition ip-network-iterator.hpp:69
constexpr inline bool operator!=(const ip_network_iterator &other) const noexcept
Inequality operator.
Definition ip-network-iterator.hpp:329
constexpr inline ip_network_iterator operator-(difference_type n) const noexcept
Subtraction operator.
Definition ip-network-iterator.hpp:277
constexpr inline ip_network_iterator & operator--() noexcept
Pre-decrement operator.
Definition ip-network-iterator.hpp:145
constexpr inline ip_network_iterator & operator-=(const uint_type &n) noexcept
Subtraction assignment operator.
Definition ip-network-iterator.hpp:210
constexpr inline difference_type operator-(const ip_network_iterator &other) const noexcept
Subtraction operator.
Definition ip-network-iterator.hpp:305
constexpr inline ip_network_iterator & operator++() noexcept
Pre-increment operator.
Definition ip-network-iterator.hpp:120
constexpr inline ip_network_iterator operator++(int) noexcept
Post-increment operator.
Definition ip-network-iterator.hpp:132
constexpr inline pointer operator->() const noexcept
Returns a pointer to the current element.
Definition ip-network-iterator.hpp:87
constexpr inline bool operator>=(const ip_network_iterator &other) const noexcept
Greater-than-or-equal-to operator.
Definition ip-network-iterator.hpp:397
constexpr inline reference operator*() const noexcept
Returns a reference to the current element.
Definition ip-network-iterator.hpp:78
constexpr inline ip_network_iterator operator-(const uint_type &n) const noexcept
Subtraction operator.
Definition ip-network-iterator.hpp:291
A reverse iterator template class for IP addresses.
Definition ip-address-iterator.hpp:35
Forward iterator for summarizing an IP address range.
Definition ip-network-iterator.hpp:676
constexpr inline bool operator!=(const ip_summarize_iterator &other) const noexcept
Inequality operator.
Definition ip-network-iterator.hpp:772
constexpr inline bool operator==(const ip_summarize_iterator &other) const noexcept
Equality operator.
Definition ip-network-iterator.hpp:757
constexpr inline ip_summarize_iterator & operator++() noexcept
Pre-increment operator.
Definition ip-network-iterator.hpp:731
constexpr inline ip_summarize_iterator operator++(int) noexcept
Post-increment operator.
Definition ip-network-iterator.hpp:743
constexpr inline pointer operator->() const noexcept
Returns a pointer to the current element.
Definition ip-network-iterator.hpp:720
constexpr inline ip_summarize_iterator(const ip_address_type ¤t, const ip_address_type &last) noexcept
Constructs a ip_summarize_iterator for an address range.
Definition ip-network-iterator.hpp:699
constexpr inline ip_summarize_iterator() noexcept
Default constructor.
Definition ip-network-iterator.hpp:690
constexpr inline reference operator*() const noexcept
Returns a reference to the current element.
Definition ip-network-iterator.hpp:711
A sequence container for subnet ranges within a network.
Definition ip-network-iterator.hpp:853
constexpr inline const_reverse_iterator crbegin() const noexcept
Gets the beginning const reverse iterator of the sequence.
Definition ip-network-iterator.hpp:977
constexpr inline value_type operator[](difference_type n) const noexcept
Accesses an element by index.
Definition ip-network-iterator.hpp:1014
constexpr inline const_iterator begin() const noexcept
Gets the beginning iterator of the sequence.
Definition ip-network-iterator.hpp:923
constexpr inline bool empty() const noexcept
Checks if the sequence is empty.
Definition ip-network-iterator.hpp:995
constexpr inline value_type back() const noexcept
Accesses the last element in the sequence.
Definition ip-network-iterator.hpp:1042
constexpr inline const_reverse_iterator crend() const noexcept
Gets the end const reverse iterator of the sequence.
Definition ip-network-iterator.hpp:986
constexpr inline value_type front() const noexcept
Accesses the first element in the sequence.
Definition ip-network-iterator.hpp:1033
constexpr inline difference_type size() const noexcept
Gets the size of the sequence.
Definition ip-network-iterator.hpp:1004
constexpr inline const_reverse_iterator rend() const noexcept
Gets the end reverse iterator of the sequence.
Definition ip-network-iterator.hpp:950
constexpr inline const_iterator cend() const noexcept
Gets the end const iterator of the sequence.
Definition ip-network-iterator.hpp:968
constexpr inline subnets_sequence(const ip_address_type &network_address, size_t new_prefixlen) noexcept
Constructs a sequence of subnets from a single network address with a new prefix length.
Definition ip-network-iterator.hpp:886
constexpr inline const_iterator cbegin() const noexcept
Gets the beginning const iterator of the sequence.
Definition ip-network-iterator.hpp:959
constexpr inline const_iterator end() const noexcept
Gets the end iterator of the sequence.
Definition ip-network-iterator.hpp:932
constexpr inline value_type at(difference_type n) const noexcept
Accesses an element by index with bounds checking.
Definition ip-network-iterator.hpp:1024
constexpr inline const_reverse_iterator rbegin() const noexcept
Gets the beginning reverse iterator of the sequence.
Definition ip-network-iterator.hpp:941
constexpr inline subnets_sequence() noexcept=default
Default constructor.
constexpr inline subnets_sequence(const ip_address_type &network_address, const ip_address_type &broadcast_address, const ip_address_type &hostmask, size_t prefixlen_diff, size_t new_prefixlen) noexcept
Constructs a sequence of subnets from a network address range with a new prefix length.
Definition ip-network-iterator.hpp:907
A container class for iterating over a summarized range of networks.
Definition ip-network-iterator.hpp:1157
constexpr inline const_iterator begin() const noexcept
Returns an iterator to the beginning of the summarized IP networks.
Definition ip-network-iterator.hpp:1191
constexpr inline summarize_sequence() noexcept=default
Default constructor.
constexpr inline summarize_sequence(const ip_address_type &first, const ip_address_type &last) noexcept
Constructs a summarize_sequence for a given IP address range.
Definition ip-network-iterator.hpp:1182
constexpr inline const_iterator cend() const noexcept
Returns a constant iterator to the end of the summarized IP networks.
Definition ip-network-iterator.hpp:1224
constexpr inline const_iterator cbegin() const noexcept
Returns a constant iterator to the beginning of the summarized IP networks.
Definition ip-network-iterator.hpp:1213
constexpr inline const_iterator end() const noexcept
Returns an iterator representing the end of the summarized IP networks.
Definition ip-network-iterator.hpp:1202
#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_FORCE_INLINE
Definition config.hpp:115
#define IPADDRESS_NAMESPACE
Definition config.hpp:41
#define IPADDRESS_NOEXCEPT
Definition config.hpp:92