art

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2025 License: MIT Imports: 1 Imported by: 0

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

Please read the ART paper ./doc/artlookup.pdf to understand the baseIndex algorithm.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IdxToPfx added in v0.0.2

func IdxToPfx(idx uint8) (octet, pfxLen uint8)

IdxToPfx returns the octet and prefix len of baseIdx. It's the inverse to PfxToIdx.

func IdxToRange added in v0.0.2

func IdxToRange(idx uint8) (first, last uint8)

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

func NetMask(bits uint8) uint8

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.0.2

func OctetToIdx(octet uint8) uint8

OctetToIdx maps octet/8 prefixes to numbers in the range [128..255].

func PfxBits added in v0.0.2

func PfxBits(depth int, idx uint8) uint8

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 added in v0.0.2

func PfxToIdx(octet, pfxLen uint8) uint8

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL