What is this algorithm mapping coordinates to numbers called?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
The name depends on what the mapping is trying to achieve. If you are taking an integer coordinate pair such as (x, y) and turning it into one integer uniquely, the general name is usually a pairing function, and the best-known example is the Cantor pairing function. If the goal is to preserve spatial locality across a grid, then the mapping is more likely a space-filling curve such as Morton order or a Hilbert curve.
The Pairing-Function Case
A pairing function is a bijection from two non-negative integers to one non-negative integer. The canonical example is Cantor pairing:
This guarantees that every (x, y) pair maps to a unique integer and every integer corresponds to one pair. That is useful for:
- hashing grid coordinates deterministically
- encoding table keys
- turning 2D integer positions into a single sortable value
If uniqueness is the main property you care about, "pairing function" is probably the right name.
Inverting the Mapping
One reason Cantor pairing is popular is that it is reversible. You can reconstruct the original coordinates:
That reversibility distinguishes pairing functions from many simpler hash-like mappings.
When the Name Is Morton Code Instead
If the algorithm interleaves the bits of x and y, the usual name is a Morton code or Z-order curve, not Cantor pairing.
Morton encoding is common in:
- quadtrees
- spatial indexes
- GPU and graphics workloads
Its main appeal is that points near each other in space often remain relatively near each other in the 1D ordering.
When the Name Is Hilbert Curve
If the algorithm is recursively walking a grid in a locality-preserving path, it may be a Hilbert curve. Hilbert ordering usually preserves locality better than Morton order, but it is more complex to compute.
So there are really three related families people often mix together:
- pairing functions for unique reversible mapping
- Morton codes for simple locality-aware indexing
- Hilbert curves for stronger locality preservation
How to Tell Which One You Have
Ask what property the algorithm emphasizes.
If it:
- uses a closed-form arithmetic formula and is reversible, think pairing function
- interleaves coordinate bits, think Morton code
- recursively traverses cells in a rotated pattern, think Hilbert curve
That distinction is more useful than memorizing one label for every coordinate-to-number transformation.
Practical Example Choice
If you only need a unique key for (x, y) in a dictionary or database, Cantor pairing is easy:
If you are building a spatial index where nearby points should cluster together on disk or in memory, Morton or Hilbert ordering is usually the more relevant concept.
Common Pitfalls
The biggest mistake is assuming every coordinate-to-number mapping is "a space-filling curve." Many are not. A pairing function and a space-filling curve solve related but different problems.
Another issue is ignoring the input domain. Classic Cantor pairing is defined for non-negative integers. If your coordinates can be negative or floating-point values, you need an additional encoding step before applying the pairing function.
Finally, do not confuse uniqueness with locality preservation. Cantor pairing gives a unique reversible number, but it does not preserve spatial proximity the way Morton or Hilbert methods aim to.
Summary
- The general name for a unique reversible
(x, y) -> nmapping is a pairing function. - The best-known example is the Cantor pairing function.
- Bit-interleaving coordinate mappings are usually called Morton codes or Z-order curves.
- Locality-preserving recursive orderings are often Hilbert curves.
- Choose the term based on whether the goal is uniqueness, reversibility, or spatial locality.
Related reading
- What is this feature column and how does it affect the training?
- What is tuning in machine learning?
- What machine learning benchmarks are out there?
- What preprocessing.scale do? How does it work?
- What is this odd sorting algorithm?
- What is wrong with this algorithm execution in Java?
- What is vector in terms of machine learning
- What to compute on school project Distributed calculation? (Useful)

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.