Gapless indexing of hexagon neighbours (disclaimer: I am not well versed in mathematics so please excuse my poor notation / explanation)
Given a hexagon grid that defines it's "neighbours" via offsets on the axis' $q$ & $r$ like this :

(the hexagon containing just $q$ & $r$ is the $(0,0)$ node)
I am looking for a function $f$ that calculates a unique natural number (like an identifier) for a given $(q,r)$ offset and a given max neighbour distance $r$. The generated natural numbers should be between 1 and the neighbour count in regards to a maximum distance $d$[2] while also being "gapless": for $d = 1$ the numbers 1 to 6 should be assigned assigned to the neighbours while $d = 2$ implies the numbers from 1 to 18 are assigned.
I know that the value pairs of the $(q,r)$ offsets are unique but struggle to find a way to map them onto the wanted value range. A classic square grid based algorithm like $row*columncount + column$ is a good starter but of course has gaps in a hexagon based grid.
[2]: $3(d^2+d)$,d > 0
 A: Given hexagon integer coordinates $\,q\,$ and $\,r,\,$ define $\,s:=-q-r\,$
which implies $\,0=q+r+s.\,$ Two hexagons are "neighbors" exactly when one
of the $\,q,r,s\,$ coordinates is the same and one of the other coordinates
differs by $\,1\,$ in one direction and the third coordinate differs by
$\,1\,$ in the opposite direction.
The hexagons whose coordinates satisfy the conditions
$\,q\ge 0\,$ and $\,s\ge 1\,$ form a wedge shaped region.
The function $\,f(q,r,s):=3r^2+2r-s+1\,$ defines a numbering of the
hexagons in the region yielding $\,1,7,8,19,20,21,\dots.$
For $\,r\le -1,$ and $\,s\le 0\,$ the function $\,f(q,r,s) :=
3q^2-2q-s+1\,$ yielding $\,2,9,10,22,23,24,\dots.$
For $\,q\ge 1\,$ and $\,r\ge 0\,$ the function $\,f(q,r,s) := 3s^2+s+r+1\,$ yielding $\,3,11,12,25,26,27,\dots.$
For $\,q\le 0\,$ and $\,s\le -1\,$ the $\,f:=3r^2+r+s+1.\,$
For $\,r\ge 1\,$ and $\,s\ge 0\,$ the $\, f:=3q^2-2q-r+1.\,$
For $\,q\le -1\,$ and $\,r\le 0\,$ the $\, f:=3s^2+2s-r+1.$
The numbering I used starts with $0$ at the origin and the numbers
go clockwise around each hexagonal ring of hexagons. There are
other choices for numbering and some may be better than others.
A: (disclaimer: I had a really hard time to explain how this works and will describe some stuff that just "comes into existance" without explaining how I got there because I really can't explain it)
Given the following image:

The following observations can be made that are the basis of that image:

*

*for a fixed $q$ there is a diagonal

*the amount of "smaller" cells than $(0,0)$ is the same as the amount of cells "greater" than $(0,0)$. Thus $(0,0)$ can trivially be assigned $\frac{neighbourcount}{2}$

*the diagonal for $q = 0$ has 7 cells which equals $2d + 1$ wherein $d$ is the maximum neighbour distance

*for every increment of $q$ for $q > 0$ the field count per diagonal decreases by 1 making the total preceding field count for a given columns $r = 0$ cell: $\frac{neighbourcount}{2} + \sum_{n=0}^{2d+1}n - \sum_{n=0}^{2d+1-q}n$. For the given images diagonal for $q = 2$ that equals $18 + 28 - 15 = 31$

*the value of the other cells in a given diagonal are simply the value of the $r = 0$ cell with the cells $r$ value added

*this outlined procedure can be applied to the "left" side by doing the same steps with the absolute value of $q$ but subtracting it from $\frac{neighbourcount}{2}$ instead of adding it

Thus the following function arises:
$signum(v) := \begin{cases}0&,v = 0\\ 1&,v > 0\\-1&, v < 0\end{cases}$
$neighbour\_count(distance) := 3\cdot(distance^2+distance)$
$index(cell, distance) := \frac{neighbour\_count(distance)}{2} + signum(cell_q)\cdot (\sum_{n=0}^{2d+1}n - \sum_{n=0}^{2d+1-|cell_q|}n + cell_r)$
Some context for this question in general: I wanted to employ perfect hashing for my hex-based game. Employing the explained function / algorithm yielded a significant performance gain compare to the general purpose hashing variant
