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:
a comparator list (
hermax.utils.SortingNetwork)a layered representation (
hermax.utils.SortingNetworkLayers)
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 >= 1by generating the next power-of-two network and pruning comparators that touch padded wires.- Parameters:
n (int)
- Return type:
- 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:
- 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]