def SNF (cases, layersize):
    """
                * Shelf Next Fit *

    For each case:
      (i)   try to place it in the current shelf,
      (ii)  if the ceiling of the shelf is too low, make it as high
            as the case,
      (iii) if it does not fit open a new shelf,
      (iv)  if it still does not fit, interrupt.
    End.

           _____________________________________
          |                                     |
          |                                     |
          |.....................................| -> Ceil of shelf (not definitive)
          |#####|    |                          |
          |#####|####|__________________________| -> Current Shelf


    """
    X, Y = layersize
    floor, lastx, ceil = 0, 0, 0
    for case in cases:
        # If it does not fit
        if case.sizex + lastx > X:
            # Start the new shelf
            lastx, floor = 0, ceil

        if case.sizey + floor > Y:
            # If it does not fit, interrupt.
            return False
        # Place the case and eventually update the ceiling.
        case.x, case.y = lastx, floor
        lastx, ceil = case.right, max(ceil, case.top)

    return True