Skip to content
LogoLogo

RadixPriceTree

Git Source

Patricia radix index for uint128 price levels. Inserts and removals traverse at most 128 branch bits; each distinct price creates one leaf and at most one branch node. Node ids released by remove are recycled by later inserts, so the id counter only grows with the peak number of simultaneously stored prices and can never be exhausted in practice (2^31 simultaneous levels).

Functions

insert

function insert(Tree storage self, uint128 price) internal returns (bool created);

Parameters

NameTypeDescription
selfTree
priceuint128

Returns

NameTypeDescription
createdbool

remove

function remove(Tree storage self, uint128 price) internal;

Parameters

NameTypeDescription
selfTree
priceuint128

best

function best(Tree storage self, bool highest) internal view returns (bool found, uint128 price);

Parameters

NameTypeDescription
selfTree
highestbool

Returns

NameTypeDescription
foundbool
priceuint128

_newNode

Reuse a released id first; only grow the counter when none is free. A freed id is referenced by nothing (its parent, sibling and leaves entries were rewired in remove), so reusing it cannot alias a live node.

function _newNode(Tree storage self) private returns (uint32 id);

Parameters

NameTypeDescription
selfTree

Returns

NameTypeDescription
iduint32

_freeNode

function _freeNode(Tree storage self, uint32 id) private;

Parameters

NameTypeDescription
selfTree
iduint32

_bitSet

function _bitSet(uint128 value, uint8 bit) private pure returns (bool);

Parameters

NameTypeDescription
valueuint128
bituint8

Returns

NameTypeDescription
<none>bool

_mostSignificantBit

function _mostSignificantBit(uint128 value) private pure returns (uint8 bit);

Parameters

NameTypeDescription
valueuint128

Returns

NameTypeDescription
bituint8

Errors

PriceLevelMissing

error PriceLevelMissing();

NodeIdOverflow

error NodeIdOverflow();

Structs

Node

struct Node {
        uint32 parent;
        uint32 left;
        uint32 right;
        uint128 price;
        uint8 bit;
        bool leaf;
    }

Properties

NameTypeDescription
parentuint32
leftuint32
rightuint32
priceuint128
bituint8
leafbool

Tree

struct Tree {
        uint32 root;
        uint32 nextNodeId;
        /// @dev Ids released by `remove`; `_newNode` pops from here before growing `nextNodeId`.
        uint32[] freeIds;
        mapping(uint32 id => Node) nodes;
        mapping(uint128 price => uint32 id) leaves;
    }

Properties

NameTypeDescription
rootuint32
nextNodeIduint32
freeIdsuint32[]
nodesmapping(uint32 id => Node)
leavesmapping(uint128 price => uint32 id)