utils.segment_tree¶
SegmentTree¶
Please Reference ding/ding/utils/segment_tree.py for usage.
- class ding.utils.segment_tree.SegmentTree(capacity: int, operation: Callable, neutral_element: Optional[float] = None)[source]¶
- Overview:
Segment tree data structure, implemented by the tree-like array. Only the leaf nodes are real value, non-leaf nodes are to do some operations on its left and right child.
- Interface:
__init__
,reduce
,__setitem__
,__getitem__
- __getitem__(idx: int) float [source]¶
- Overview:
Get
leaf[idx]
- Arguments:
idx (
int
): Leaf nodeindex(relative index)
, addcapacity
to change to absolute index.
- Returns:
val (
float
): The value ofleaf[idx]
- __init__(capacity: int, operation: Callable, neutral_element: Optional[float] = None) None [source]¶
- Overview:
Initialize the segment tree. Tree’s root node is at index 1.
- Arguments:
capacity (
int
): Capacity of the tree (the number of the leaf nodes), should be the power of 2.operation (
function
): The operation function to construct the tree, e.g. sum, max, min, etc.neutral_element (
float
orNone
): The value of the neutral element, which is used to init all nodes value in the tree.
- __setitem__(idx: int, val: float) None [source]¶
- Overview:
Set
leaf[idx] = val
; Then update the related nodes.- Arguments:
idx (
int
): Leaf node index(relative index), should addcapacity
to change to absolute index.val (
float
): The value that will be assigned toleaf[idx]
.
- reduce(start: int = 0, end: Optional[int] = None) float [source]¶
- Overview:
Reduce the tree in range
[start, end)
- Arguments:
start (
int
): Start index(relative index, the first leaf node is 0), default set to 0end (
int
orNone
): End index(relative index), default set toself.capacity
- Returns:
reduce_result (
float
): The reduce result value, which is dependent on data type and operation
- class ding.utils.segment_tree.SumSegmentTree(capacity: int)[source]¶
- __getitem__(idx: int) float ¶
- Overview:
Get
leaf[idx]
- Arguments:
idx (
int
): Leaf nodeindex(relative index)
, addcapacity
to change to absolute index.
- Returns:
val (
float
): The value ofleaf[idx]
- __setitem__(idx: int, val: float) None ¶
- Overview:
Set
leaf[idx] = val
; Then update the related nodes.- Arguments:
idx (
int
): Leaf node index(relative index), should addcapacity
to change to absolute index.val (
float
): The value that will be assigned toleaf[idx]
.
- find_prefixsum_idx(prefixsum: float, trust_caller: bool = True) int [source]¶
- Overview:
Find the highest non-zero index i, sum_{j}leaf[j] <=
prefixsum
(where 0 <= j < i) and sum_{j}leaf[j] >prefixsum
(where 0 <= j < i+1)- Arguments:
prefixsum (
float
): The target prefixsum.- trust_caller (
bool
): Whether to trust caller, which means whether to check whether this tree’s sum is greater than the inputprefixsum
by callingreduce
function. Default set to True.
- trust_caller (
- Returns:
idx (
int
): Eligible index.
- reduce(start: int = 0, end: Optional[int] = None) float ¶
- Overview:
Reduce the tree in range
[start, end)
- Arguments:
start (
int
): Start index(relative index, the first leaf node is 0), default set to 0end (
int
orNone
): End index(relative index), default set toself.capacity
- Returns:
reduce_result (
float
): The reduce result value, which is dependent on data type and operation
- class ding.utils.segment_tree.MinSegmentTree(capacity: int)[source]¶
- __getitem__(idx: int) float ¶
- Overview:
Get
leaf[idx]
- Arguments:
idx (
int
): Leaf nodeindex(relative index)
, addcapacity
to change to absolute index.
- Returns:
val (
float
): The value ofleaf[idx]
- __setitem__(idx: int, val: float) None ¶
- Overview:
Set
leaf[idx] = val
; Then update the related nodes.- Arguments:
idx (
int
): Leaf node index(relative index), should addcapacity
to change to absolute index.val (
float
): The value that will be assigned toleaf[idx]
.
- reduce(start: int = 0, end: Optional[int] = None) float ¶
- Overview:
Reduce the tree in range
[start, end)
- Arguments:
start (
int
): Start index(relative index, the first leaf node is 0), default set to 0end (
int
orNone
): End index(relative index), default set toself.capacity
- Returns:
reduce_result (
float
): The reduce result value, which is dependent on data type and operation