My math-fu is failing me! I need an efficient way of reducing network ranges to supersets, e.g. if I input list of IP ranges:
- 1.1.1.1 to 2.2.2.5
- 1.1.1.2 to 2.2.2.4
- 10.5.5.5 to 155.5.5.5
- 10.5.5.6 to 10.5.5.7
I want to return the following ranges:
- 1.1.1.1 to 2.2.2.5
- 10.5.5.5 to 155.5.5.5
Note: the input lists are not ordered (though they could be?). The naive way to do this is to check every range in the list to see if the input range x is a subset, and if so, NOT insert range x. However, whenever you insert a new range it might be a superset of existing ranges, so you have to check the existing ranges to see if they can be collapsed (e.g., removed from my list).
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…