Utilities API#

The hermax.utils module exposes small, solver-agnostic helpers that are useful across modelling, testing, and experimentation. At the moment it contains pure-Python sorting network utilities (Batcher’s odd-even merge sort networks [1] [2]) and helper functions to apply them to Python sequences.

Sorting Networks#

The exported API provides two representations:

Both are generated for a requested width n and support arbitrary n >= 1 by building the next power-of-two Batcher network and pruning comparators that would touch padded wires.

hermax.utils.batcher_odd_even_sorting_network(n)#

Return a Batcher odd-even merge sorting network for width n.

Supports arbitrary n >= 1 by generating the next power-of-two network and pruning comparators that touch padded wires.

Parameters:

n (int)

Return type:

SortingNetwork

hermax.utils.batcher_odd_even_sorting_network_layers(n)#

Return a layered Batcher odd-even sorting network for width n.

Parameters:

n (int)

Return type:

SortingNetworkLayers

hermax.utils.apply_sorting_network(values, network, *, key=None)#

Apply a compare-swap network to a sequence and return a sorted copy.

Parameters:
  • values (Sequence[T])

  • network (Sequence[Tuple[int, int]])

  • key (Callable[[T], object] | None)

Return type:

list[T]

hermax.utils.apply_sorting_network_layers(values, layers, *, key=None)#

Apply a layered compare-swap network and return a sorted copy.

Parameters:
  • values (Sequence[T])

  • layers (Sequence[Sequence[Tuple[int, int]]])

  • key (Callable[[T], object] | None)

Return type:

list[T]

class hermax.utils.SortingNetwork#

List of comparators annotated with the intended input width.

__init__(n, comps=())#
Parameters:
  • n (int)

  • comps (Iterable[Tuple[int, int]])

n#
class hermax.utils.SortingNetworkLayers#

Layered sorting network annotated with the intended input width.

__init__(n, layers=())#
Parameters:
  • n (int)

  • layers (Iterable[list[Tuple[int, int]]])

n#

References#