Documentation
¶
Overview ¶
Package art summarizes the functions and inverse functions for mapping between a prefix and a baseIndex.
can inline IdxToPfx with cost 37 can inline IdxToRange with cost 68 can inline NetMask with cost 14 can inline OctetToIdx with cost 5 can inline PfxBits with cost 21 can inline PfxToIdx with cost 17
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IdxToRange ¶ added in v0.19.0
IdxToRange returns the first and last octets covered by a base index.
The base index encodes a prefix of up to 8 bits inside a single stride (octet). This function computes the numerical start and end of the value range for that prefix.
For example:
A 0-bit prefix (idx == 1) covers the full range: 0..255
A 3-bit prefix like 0b101xxxx (idx == 13) covers: 160..191
idx := PfxToIdx(0b10100000, 3) // 13 first, last := IdxToRange(13) // 160, 191
Internally, this function decodes (octet, prefixLength) via IdxToPfx, then computes the broadcast address (last octet) by masking all bits below the prefix length.
func NetMask ¶ added in v0.19.0
NetMask returns an 8-bit left-aligned network mask for the given number of prefix bits.
For example:
bits = 0 -> 0b00000000 bits = 1 -> 0b10000000 bits = 2 -> 0b11000000 bits = 3 -> 0b11100000 ... bits = 8 -> 0b11111111
This mask is used to extract or identify the fixed (prefix) portion of an octet. The rightmost (8 - bits) bits are cleared (set to zero), and the upper 'bits' are set to 1.
func OctetToIdx ¶ added in v0.20.5
OctetToIdx maps octet/8 prefixes from [256..511] => [128..255] to start with the parent (>>1) in the complete binary tree.
Same formula as PfxToIdx, but for octet/8 and a shift (>>1).
octet>>(8-pfxLen) + 1<<pfxLen
octet>>(8-8) + 1<<8
octet + 256
got to parent idx:
(octet+256)>>1 == octet>>1 + 128
func PfxBits ¶ added in v0.20.5
PfxBits returns the bit position of a prefix represented by a base index at a given trie depth.
Each trie level represents an 8-bit stride (one octet). The base index contains enough information to recover the prefix length. This function returns the full bit offset of the prefix in the address space.
For example:
depth = 2 (i.e. third trie level) idx = 13 (which encodes a prefix of length 3 bits within that stride) => PfxBits = 2*8 + 3 = 19
func PfxToIdx ¶
PfxToIdx maps 8bit prefixes to numbers. The prefixes range from 0/0 to 255/7 The return values range from 1 to 255.
[0x0000_00001 .. 0x1111_1111] = [1 .. 255] example: octet/pfxLen: 160/3 = 0b1010_0000/3 => IdxToPfx(160/3) => 13 0b1010_0000 => 0b0000_0101 ^^^ >> (8-3) ^^^ 0b0000_0001 => 0b0000_1000 ^ << 3 ^ + ----------------------- 0b0000_1101 = 13
Panics if `pfxLen > 7`.
Types ¶
This section is empty.