docs: def _net_cost(net, placements, has_wrap_around_links, machine):
      """Get the cost of a given net.
  
      This function, in principle at least, should estimate the total network
      resources consumed by the given net. In practice this estimate is based on
      the size of the bounding-box of the net (i.e. HPWL). This should be
      improved at some later time to better account for the effects of large
      fan-outs.
  
      Parameters
      ----------
      net : :py:class:`rig.netlist.Net`
      placements : {vertex: (x, y), ...}
      has_wrap_around_links : bool
      machine : :py:class:`rig.place_and_route.Machine`
  
      Returns
      -------
      float
      """
      # This function is by far the hottest code in the entire algorithm, as a
      # result, small performance improvements in here can have significant
      # impact on the runtime of the overall algorithm. As an unfortunate side
      # effect, this code is rather ugly since many higher-level constructs (e.g.
      # min/max) are outrageously slow.
  
      # XXX: This does not account for the hexagonal properties of the SpiNNaker
      # topology.
      if has_wrap_around_links:
          # When wrap-around links exist, we find the minimal bounding box and
          # return the HPWL weighted by the net weight. To do this the largest
          # gap between any pair of vertices is found::
          #
          #     |    x     x             x   |
          #                ^-------------^
          #                    max gap
          #
          # The minimal bounding box then goes the other way around::
          #
          #     |    x     x             x   |
          #      ----------^             ^---
  
          # First we collect the x and y coordinates of all vertices in the net
          # into a pair of (sorted) lists, xs and ys.
          x, y = placements[net.source]
          num_vertices = len(net.sinks) + 1
          xs = [x] * num_vertices
          ys = [y] * num_vertices
          i = 1
          for v in net.sinks:
              x, y = placements[v]
              xs[i] = x
              ys[i] = y
              i += 1
  
          xs.sort()
          ys.sort()
  
          # The minimal bounding box is then found as above.
          x_max_delta = 0
          last_x = xs[-1] - machine.width
          for x in xs:
              delta = x - last_x
              last_x = x
              if delta > x_max_delta:
                  x_max_delta = delta
  
          y_max_delta = 0
          last_y = ys[-1] - machine.height
          for y in ys:
              delta = y - last_y
              last_y = y
              if delta > y_max_delta:
                  y_max_delta = delta
  
          return (((machine.width - x_max_delta) +
                   (machine.height - y_max_delta)) *
                  net.weight *
                  math.sqrt(len(net.sinks) + 1))
      else:
          # When no wrap-around links, find the bounding box around the vertices
          # in the net and return the HPWL weighted by the net weight.
          x1, y1 = x2, y2 = placements[net.source]
          for vertex in net.sinks:
              x, y = placements[vertex]
              x1 = x if x < x1 else x1
              y1 = y if y < y1 else y1
              x2 = x if x > x2 else x2
              y2 = y if y > y2 else y2
  
          return (((x2 - x1) + (y2 - y1)) *
                  float(net.weight) *
                  math.sqrt(len(net.sinks) + 1))