in scripts/ValidateParameters/netaddr/ip/sets.py [0:0]
def difference(self, other):
"""
:param other: an IP set.
:return: the difference between this IP set and another as a new IP
set (all IP addresses and subnets that are in this IP set but
not found in the other.)
"""
result_ranges = []
result_cidrs = {}
own_nets = sorted(self._cidrs)
other_nets = sorted(other._cidrs)
own_idx = 0
other_idx = 0
own_len = len(own_nets)
other_len = len(other_nets)
while own_idx < own_len and other_idx < other_len:
own_cur = own_nets[own_idx]
other_cur = other_nets[other_idx]
if own_cur == other_cur:
own_idx += 1
other_idx += 1
elif own_cur in other_cur:
own_idx += 1
elif other_cur in own_cur:
other_idx = _subtract(own_cur, other_nets, other_idx,
result_ranges)
own_idx += 1
else:
# own_cur and other_cur have nothing in common
if own_cur < other_cur:
result_cidrs[own_cur] = True
own_idx += 1
else:
other_idx += 1
# If the above loop terminated because it processed all cidrs of
# "other", then any remaining cidrs in self must be part of the result.
while own_idx < own_len:
result_cidrs[own_nets[own_idx]] = True
own_idx += 1
for start, stop in _iter_merged_ranges(result_ranges):
for cidr in iprange_to_cidrs(start, stop):
result_cidrs[cidr] = True
result = IPSet()
result._cidrs = result_cidrs
return result