sum_first_found static method

List<IPAddress> sum_first_found(
  1. List<IPAddress> arr
)

Splits a network into different subnets

If the IP Address is a network, it can be divided into multiple networks. If +self+ is not a network, this method will calculate the network from the IP and then subnet it.

If +subnets+ is an power of two number, the resulting networks will be divided evenly from the supernet.

network = IPAddress("172.16.10.0/24")

network / 4 /// implies map{|i| i.to_string} /// "172.16.10.0/26", "172.16.10.64/26", "172.16.10.128/26", "172.16.10.192/26"

If +num+ is any other number, the supernet will be divided into some networks with a even number of hosts and other networks with the remaining addresses.

network = IPAddress("172.16.10.0/24")

network / 3 /// implies map{|i| i.to_string} /// "172.16.10.0/26", "172.16.10.64/26", "172.16.10.128/25"

Returns an array of IPv4 objects

Implementation

static List<IPAddress> sum_first_found(List<IPAddress> arr) {
  var dup = List<IPAddress>.from(arr);
  if (dup.length < 2) {
    return dup;
  }
  for (var i = dup.length - 2; i >= 0; i--) {
    final a = IPAddress.summarize([dup[i], dup[i + 1]]);
    // println!("dup:{}:{}:{}", dup.len(), i, a.len());
    if (a.length == 1) {
      dup[i] = a[0];
      dup.removeAt(i + 1);
      return dup;
    }
  }
  return dup;
}