package org.batfish.datamodel.visitors;

import java.util.Optional;
import org.batfish.common.bdd.BDDInteger;
import org.batfish.common.bdd.BDDPacket;
import org.batfish.common.bdd.IpSpaceToBDD;
import org.batfish.datamodel.Ip;
import org.batfish.datamodel.IpSpace;

/** Simple class for finding representative {@link Ip IP Addresses} in an {@link IpSpace}. */
public final class IpSpaceRepresentative {
  private final IpSpaceToBDD _ipSpaceToBDD;

  public IpSpaceRepresentative() {
    BDDPacket bddPacket = new BDDPacket();
    BDDInteger ipAddrBdd = bddPacket.getDstIp();
    _ipSpaceToBDD = new IpSpaceToBDD(ipAddrBdd);
  }

  /** Returns some representative element of an {@link IpSpace ip space}, if any exists. */
  public Optional<Ip> getRepresentative(IpSpace ipSpace) {
    return _ipSpaceToBDD
        .getBDDInteger()
        .getValueSatisfying(_ipSpaceToBDD.visit(ipSpace))
        .map(Ip::create);
  }
}

@@ -34,16 +34,63 @@
 itors;%0A%0A
+import com.google.common.collect.ImmutableMap;%0A
 import j
@@ -235,16 +235,68 @@
 eToBDD;%0A
+import org.batfish.common.bdd.MemoizedIpSpaceToBDD;%0A
 import o
@@ -692,16 +692,24 @@
 D = new 
+Memoized
 IpSpaceT
@@ -722,16 +722,35 @@
 pAddrBdd
+, ImmutableMap.of()
 );%0A  %7D%0A%0A

use a memoized IpSpaceToBDD (#3270)