def _collapse_addresses_internal(addresses):
<EOL>to_merge = list(addresses)<EOL>subnets = {}<EOL>while to_merge:<EOL><INDENT>net = to_merge.pop()<EOL>supernet = net.supernet()<EOL>existing = subnets.get(supernet)<EOL>if existing is None:<EOL><INDENT>subnets[supernet] = net<EOL><DEDENT>elif existing != net:<EOL><INDENT>del subnets[supernet]<EOL>to_merge.append(supernet)<EOL><DEDENT><DEDENT>last = None<EOL>for net in sorted(subnets.values()):<EOL><INDENT>if last is not None:<EOL><INDENT>if last.broadcast_address >= net.broadcast_address:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>yield net<EOL>last = net<EOL><DEDENT>
Loops through the addresses, collapsing concurrent netblocks.

    Example:

        ip1 = IPv4Network('192.0.2.0/26')
        ip2 = IPv4Network('192.0.2.64/26')
        ip3 = IPv4Network('192.0.2.128/26')
        ip4 = IPv4Network('192.0.2.192/26')

        _collapse_addresses_internal([ip1, ip2, ip3, ip4]) ->
          [IPv4Network('192.0.2.0/24')]

        This shouldn't be called directly; it is called via
          collapse_addresses([]).

    Args:
        addresses: A list of IPv4Network's or IPv6Network's

    Returns:
        A list of IPv4Network's or IPv6Network's depending on what we were
        passed.