Implement a class that iterates through IPv4 addresses across multiple inclusive ranges.
Each range is given as a pair of IPv4 addresses in dotted-decimal format, such as (10.0.0.1, 10.0.0.3). The input may contain multiple ranges. Build an iterator that returns every individual IP address in ascending order.
Requirements:
-
constructor(ranges)
: initialize the iterator from a list of ranges.
-
has_next() -> bool
: return whether another IP address is available.
-
next() -> string
: return the next IP address in ascending order.
You may assume each range is valid and inclusive. If helpful, you may first normalize the input by sorting ranges and merging any overlapping or adjacent intervals.
Follow-up:
-
Represent IPv4 addresses internally as unsigned 32-bit integers.
-
Explain how to convert between dotted-decimal strings and integers using bit operations.
-
Discuss the time and space complexity of your design.