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
675
676
680 using value_type = T;
682 using difference_type =
typename value_type::uint_type;
683 using pointer = value_type*;
684 using const_pointer =
const value_type*;
685 using reference = value_type&;
686 using const_reference =
const value_type&;
694 using ip_address_type =
typename value_type::ip_address_type;
697
698
702
703
704
705
706
707
708
709
710
712 const auto begin = ip_address_type::from_bytes(network_address.bytes());
713 const auto end = ip_address_type::from_uint(network_address.to_uint() + 1);
714 const auto step = difference_type(1);
715 _begin = const_iterator(begin, step, new_prefixlen);
716 _end = const_iterator(end, step, new_prefixlen, end < begin ? 1 : 0);
721
722
723
724
725
726
727
728
729
730
731
733 const auto begin_uint = network_address.to_uint();
734 const auto end_uint = broadcast_address.to_uint();
735 const auto begin = ip_address_type::from_uint(begin_uint);
736 const auto end = ip_address_type::from_uint(end_uint + 1);
737 const auto step = (hostmask.to_uint() >> prefixlen_diff) + 1;
738 _begin = const_iterator(begin, step, new_prefixlen);
739 _end = const_iterator(end, step, new_prefixlen, begin == end ? 1 : 0);
740 _size = (end_uint - begin_uint) / step + 1;
744
745
746
747
753
754
755
756
762
763
764
765
767 return const_reverse_iterator(
end());
771
772
773
774
776 return const_reverse_iterator(
begin());
780
781
782
783
789
790
791
792
798
799
800
801
803 return const_reverse_iterator(
cend());
807
808
809
810
812 return const_reverse_iterator(
cbegin());
816
817
818
819
821 return _begin == _end;
825
826
827
828
834
835
836
837
838
844
845
846
847
848
850 return *(_begin + n);
854
855
856
857
863
864
865
866
872 const_iterator _begin{};
873 const_iterator _end{};
874 difference_type _size{};
878
879
880
881
882
883
884
885
886
887
888
892 using value_type = T;
894 using difference_type =
typename value_type::uint_type;
895 using pointer = value_type*;
896 using const_pointer =
const value_type*;
897 using reference = value_type&;
898 using const_reference =
const value_type&;
903
904
908
909
910
911
912
913
914
915
916
917
919 : _begin(const_iterator(network, other)) {
923
924
925
926
932
933
934
935
941
942
943
944
950
951
952
953
959
960
961
962
964 return _begin == _end;
968 const_iterator _begin{};
969 const_iterator _end{};
A sequence container for networks excluding specified subnets.
Definition ip-network-iterator.hpp:890
constexpr inline const_iterator begin() const noexcept
Gets the beginning iterator of the sequence.
Definition ip-network-iterator.hpp:927
constexpr inline bool empty() const noexcept
Checks if the sequence is empty.
Definition ip-network-iterator.hpp:963
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:954
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:918
constexpr inline const_iterator cbegin() const noexcept
Gets the beginning const iterator of the sequence.
Definition ip-network-iterator.hpp:945
constexpr inline const_iterator end() const noexcept
Gets the end iterator of the sequence.
Definition ip-network-iterator.hpp:936
An iterator for unified traversal over IPv4 and IPv6 address spaces.
Definition ip-any-iterator.hpp:44
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
A sequence container for subnet ranges within a network.
Definition ip-network-iterator.hpp:678
constexpr inline const_reverse_iterator crbegin() const noexcept
Gets the beginning const reverse iterator of the sequence.
Definition ip-network-iterator.hpp:802
constexpr inline value_type operator[](difference_type n) const noexcept
Accesses an element by index.
Definition ip-network-iterator.hpp:839
constexpr inline const_iterator begin() const noexcept
Gets the beginning iterator of the sequence.
Definition ip-network-iterator.hpp:748
constexpr inline bool empty() const noexcept
Checks if the sequence is empty.
Definition ip-network-iterator.hpp:820
constexpr inline value_type back() const noexcept
Accesses the last element in the sequence.
Definition ip-network-iterator.hpp:867
constexpr inline const_reverse_iterator crend() const noexcept
Gets the end const reverse iterator of the sequence.
Definition ip-network-iterator.hpp:811
constexpr inline value_type front() const noexcept
Accesses the first element in the sequence.
Definition ip-network-iterator.hpp:858
constexpr inline difference_type size() const noexcept
Gets the size of the sequence.
Definition ip-network-iterator.hpp:829
constexpr inline const_reverse_iterator rend() const noexcept
Gets the end reverse iterator of the sequence.
Definition ip-network-iterator.hpp:775
constexpr inline const_iterator cend() const noexcept
Gets the end const iterator of the sequence.
Definition ip-network-iterator.hpp:793
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:711
constexpr inline const_iterator cbegin() const noexcept
Gets the beginning const iterator of the sequence.
Definition ip-network-iterator.hpp:784
constexpr inline const_iterator end() const noexcept
Gets the end iterator of the sequence.
Definition ip-network-iterator.hpp:757
constexpr inline value_type at(difference_type n) const noexcept
Accesses an element by index with bounds checking.
Definition ip-network-iterator.hpp:849
constexpr inline const_reverse_iterator rbegin() const noexcept
Gets the beginning reverse iterator of the sequence.
Definition ip-network-iterator.hpp:766
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:732
#define IPADDRESS_NOEXCEPT_WHEN_NO_EXCEPTIONS
Definition config.hpp:93
#define IPADDRESS_EXPORT
Definition config.hpp:42
#define IPADDRESS_NODISCARD
Definition config.hpp:98
#define IPADDRESS_FORCE_INLINE
Definition config.hpp:112
#define IPADDRESS_NAMESPACE
Definition config.hpp:38
#define IPADDRESS_NOEXCEPT
Definition config.hpp:89