1///| 创建空的 RoaringBitmap
2pub fn 
() -> RoaringBitmap

创建空的 RoaringBitmap

new
() ->
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
{
3
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
::{
Array[HighContainer]
containers
: [],
Int
size
: 0 }
4} 5 6///| 从单个值创建 RoaringBitmap 7pub fn
(value : UInt) -> RoaringBitmap

从单个值创建 RoaringBitmap

singleton
(
UInt
value
:
UInt
UInt
) ->
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
{
8 let
UInt16
high
=
(value : UInt) -> UInt16

提取32位整数的高16位

extract_high_16
(
UInt
value
)
9 let
UInt16
low
=
(value : UInt) -> UInt16

提取32位整数的低16位

extract_low_16
(
UInt
value
)
10 let
Container
container
=
(Array[UInt16]) -> Container
ArrayContainer
([
UInt16
low
])
11 let
HighContainer
high_container
=
struct HighContainer {
  key: UInt16
  container: Container
  cardinality: Int
}

高级容器 - 处理 32 位整数的高 16 位

HighContainer
::{
UInt16
key
:
UInt16
high
,
Container
container
,
Int
cardinality
: 1 }
12
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
::{
Array[HighContainer]
containers
: [
HighContainer
high_container
],
Int
size
: 1 }
13} 14 15///| 从数组创建 RoaringBitmap 16pub fn
(values : Array[UInt]) -> RoaringBitmap

从数组创建 RoaringBitmap

from_array
(
Array[UInt]
values
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt
UInt
]) ->
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
{
17 let mut
RoaringBitmap
bitmap
=
() -> RoaringBitmap

创建空的 RoaringBitmap

new
()
18 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt]
values
.
(self : Array[UInt]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
19
RoaringBitmap
bitmap
=
RoaringBitmap
bitmap
.
(self : RoaringBitmap, value : UInt) -> RoaringBitmap

添加单个元素

add
(
Array[UInt]
values
(Array[UInt], Int) -> UInt

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i])
20 } 21
RoaringBitmap
bitmap
22} 23 24///| 添加单个元素 25pub fn
(self : RoaringBitmap, value : UInt) -> RoaringBitmap

添加单个元素

add
(
RoaringBitmap
self
:
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
,
UInt
value
:
UInt
UInt
) ->
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
{
26 let
UInt16
high
=
(value : UInt) -> UInt16

提取32位整数的高16位

extract_high_16
(
UInt
value
)
27 let
UInt16
low
=
(value : UInt) -> UInt16

提取32位整数的低16位

extract_low_16
(
UInt
value
)
28 29 // 查找对应的容器 30 match
(containers : Array[HighContainer], key : UInt16) -> SearchResult

在排序数组中查找容器索引

find_container_index
(
RoaringBitmap
self
.
Array[HighContainer]
containers
,
UInt16
high
) {
31
(Int) -> SearchResult
Found
(
Int
index
) => {
32 // 容器存在,添加到现有容器 33 let
HighContainer
container
=
RoaringBitmap
self
.
Array[HighContainer]
containers
(Array[HighContainer], Int) -> HighContainer

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
index]
34 if
(container : Container, value : UInt16) -> Bool

检查容器是否包含指定值

container_contains
(
HighContainer
container
.
Container
container
,
UInt16
low
) {
35 // 元素已存在,返回原bitmap 36
RoaringBitmap
self
37 } else { 38 // 添加元素到容器 39 let
Container
new_container
=
(container : Container, value : UInt16) -> Container

向容器添加元素

container_add
(
HighContainer
container
.
Container
container
,
UInt16
low
)
40 let
HighContainer
new_high_container
=
struct HighContainer {
  key: UInt16
  container: Container
  cardinality: Int
}

高级容器 - 处理 32 位整数的高 16 位

HighContainer
::{
41
UInt16
key
:
HighContainer
container
.
UInt16
key
,
42
Container
container
:
Container
new_container
,
43
Int
cardinality
:
HighContainer
container
.
Int
cardinality
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1,
44 } 45 let
Array[HighContainer]
new_containers
=
(arr : Array[HighContainer], index : Int, element : HighContainer) -> Array[HighContainer]

替换数组中指定位置的元素

array_replace_at
(
46
RoaringBitmap
self
.
Array[HighContainer]
containers
,
47
Int
index
,
48
HighContainer
new_high_container
,
49 ) 50
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
::{
Array[HighContainer]
containers
:
Array[HighContainer]
new_containers
,
Int
size
:
RoaringBitmap
self
.
Int
size
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 }
51 } 52 } 53
(Int) -> SearchResult
NotFound
(
Int
insert_pos
) => {
54 // 容器不存在,创建新容器 55 let
Container
new_container
=
(Array[UInt16]) -> Container
ArrayContainer
([
UInt16
low
])
56 let
HighContainer
new_high_container
=
struct HighContainer {
  key: UInt16
  container: Container
  cardinality: Int
}

高级容器 - 处理 32 位整数的高 16 位

HighContainer
::{
57
UInt16
key
:
UInt16
high
,
58
Container
container
:
Container
new_container
,
59
Int
cardinality
: 1,
60 } 61 let
Array[HighContainer]
new_containers
=
(arr : Array[HighContainer], index : Int, element : HighContainer) -> Array[HighContainer]

在指定位置插入元素到数组

array_insert_at
(
62
RoaringBitmap
self
.
Array[HighContainer]
containers
,
63
Int
insert_pos
,
64
HighContainer
new_high_container
,
65 ) 66
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
::{
Array[HighContainer]
containers
:
Array[HighContainer]
new_containers
,
Int
size
:
RoaringBitmap
self
.
Int
size
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 }
67 } 68 } 69} 70 71///| 移除单个元素 72pub fn
(self : RoaringBitmap, value : UInt) -> RoaringBitmap

移除单个元素

remove
(
RoaringBitmap
self
:
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
,
UInt
value
:
UInt
UInt
) ->
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
{
73 let
UInt16
high
=
(value : UInt) -> UInt16

提取32位整数的高16位

extract_high_16
(
UInt
value
)
74 let
UInt16
low
=
(value : UInt) -> UInt16

提取32位整数的低16位

extract_low_16
(
UInt
value
)
75 match
(containers : Array[HighContainer], key : UInt16) -> SearchResult

在排序数组中查找容器索引

find_container_index
(
RoaringBitmap
self
.
Array[HighContainer]
containers
,
UInt16
high
) {
76
(Int) -> SearchResult
Found
(
Int
index
) => {
77 let
HighContainer
container
=
RoaringBitmap
self
.
Array[HighContainer]
containers
(Array[HighContainer], Int) -> HighContainer

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
index]
78 if
(container : Container, value : UInt16) -> Bool

检查容器是否包含指定值

container_contains
(
HighContainer
container
.
Container
container
,
UInt16
low
) {
79 let
Container
new_container
=
(container : Container, value : UInt16) -> Container

从容器移除元素

container_remove
(
HighContainer
container
.
Container
container
,
UInt16
low
)
80 let
Int
new_cardinality
=
HighContainer
container
.
Int
cardinality
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
-
1
81 if
Int
new_cardinality
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
0 {
82 // 容器为空,移除整个容器 83 let
Array[HighContainer]
new_containers
=
(arr : Array[HighContainer], index : Int) -> Array[HighContainer]

移除数组中指定位置的元素

array_remove_at
(
RoaringBitmap
self
.
Array[HighContainer]
containers
,
Int
index
)
84
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
::{
Array[HighContainer]
containers
:
Array[HighContainer]
new_containers
,
Int
size
:
RoaringBitmap
self
.
Int
size
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
-
1 }
85 } else { 86 // 更新容器 87 let
HighContainer
new_high_container
=
struct HighContainer {
  key: UInt16
  container: Container
  cardinality: Int
}

高级容器 - 处理 32 位整数的高 16 位

HighContainer
::{
88
UInt16
key
:
HighContainer
container
.
UInt16
key
,
89
Container
container
:
Container
new_container
,
90
Int
cardinality
:
Int
new_cardinality
,
91 } 92 let
Array[HighContainer]
new_containers
=
(arr : Array[HighContainer], index : Int, element : HighContainer) -> Array[HighContainer]

替换数组中指定位置的元素

array_replace_at
(
93
RoaringBitmap
self
.
Array[HighContainer]
containers
,
94
Int
index
,
95
HighContainer
new_high_container
,
96 ) 97
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
::{
Array[HighContainer]
containers
:
Array[HighContainer]
new_containers
,
Int
size
:
RoaringBitmap
self
.
Int
size
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
-
1 }
98 } 99 } else { 100 // 元素不存在,返回原bitmap 101
RoaringBitmap
self
102 } 103 } 104
SearchResult
NotFound
(_) =>
105 // 容器不存在,元素不存在,返回原bitmap 106 return
RoaringBitmap
self
107 } 108} 109 110///| 检查元素是否存在 111pub fn
(self : RoaringBitmap, value : UInt) -> Bool

检查元素是否存在

contains
(
RoaringBitmap
self
:
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
,
UInt
value
:
UInt
UInt
) ->
Bool
Bool
{
112 let
UInt16
high
=
(value : UInt) -> UInt16

提取32位整数的高16位

extract_high_16
(
UInt
value
)
113 let
UInt16
low
=
(value : UInt) -> UInt16

提取32位整数的低16位

extract_low_16
(
UInt
value
)
114 match
(containers : Array[HighContainer], key : UInt16) -> SearchResult

在排序数组中查找容器索引

find_container_index
(
RoaringBitmap
self
.
Array[HighContainer]
containers
,
UInt16
high
) {
115
(Int) -> SearchResult
Found
(
Int
index
) => {
116 let
HighContainer
container
=
RoaringBitmap
self
.
Array[HighContainer]
containers
(Array[HighContainer], Int) -> HighContainer

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
index]
117
(container : Container, value : UInt16) -> Bool

检查容器是否包含指定值

container_contains
(
HighContainer
container
.
Container
container
,
UInt16
low
)
118 } 119
SearchResult
NotFound
(_) => false
120 } 121} 122 123///| 获取bitmap的大小(元素个数) 124pub fn
(self : RoaringBitmap) -> Int

获取bitmap的大小(元素个数)

size
(
RoaringBitmap
self
:
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
) ->
Int
Int
{
125
RoaringBitmap
self
.
Int
size
126} 127 128///| 检查bitmap是否为空 129pub fn
(self : RoaringBitmap) -> Bool

检查bitmap是否为空

is_empty
(
RoaringBitmap
self
:
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
) ->
Bool
Bool
{
130
RoaringBitmap
self
.
Int
size
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
0
131} 132 133///| 清空bitmap 134pub fn
(self : RoaringBitmap) -> RoaringBitmap

清空bitmap

clear
(
RoaringBitmap
self
:
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
) ->
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
{
135
RoaringBitmap
self
.
Array[HighContainer]
containers
.
(self : Array[HighContainer]) -> Unit

Clears the array, removing all values.

This method has no effect on the allocated capacity of the array, only setting the length to 0.

Example

  let v = [3, 4, 5]
  v.clear()
  assert_eq(v.length(), 0)
clear
()
136
RoaringBitmap
self
.
Int
size
= 0
137
RoaringBitmap
self
138} 139 140///| 转换为数组 141pub fn
(self : RoaringBitmap) -> Array[UInt]

转换为数组

to_array
(
RoaringBitmap
self
:
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
) ->
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt
UInt
] {
142 let
Array[UInt]
result
= []
143 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
RoaringBitmap
self
.
Array[HighContainer]
containers
.
(self : Array[HighContainer]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
144 let
HighContainer
container
=
RoaringBitmap
self
.
Array[HighContainer]
containers
(Array[HighContainer], Int) -> HighContainer

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
145 let
UInt
high_bits
=
HighContainer
container
.
UInt16
key
.
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
().
(self : Int) -> UInt

reinterpret the signed int as unsigned int, when the value is non-negative, i.e, 0..=2^31-1, the value is the same. When the value is negative, it turns into a large number, for example, -1 turns into 2^32-1

reinterpret_as_uint
()
(self : UInt, shift : Int) -> UInt

Performs a left shift operation on an unsigned 32-bit integer. Each bit in the integer is shifted left by the specified number of positions, and zeros are filled in from the right.

Parameters:

  • self : The unsigned 32-bit integer to be shifted.
  • shift : The number of positions to shift. Only the least significant 5 bits are used, effectively making the shift count always between 0 and 31.

Returns a new unsigned 32-bit integer that is the result of shifting self left by shift positions.

Example:

  let x = 1U
  inspect(x << 3, content="8") // Binary: 1 -> 1000
  let y = 0xFFFFFFFFU
  inspect(y << 16, content="4294901760") // All bits after position 16 are discarded
<<
16
146 let
Array[UInt16]
low_values
=
(container : Container) -> Array[UInt16]

容器转换为数组

container_to_array
(
HighContainer
container
.
Container
container
)
147 for
Int
j
= 0;
Int
j
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
low_values
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
148
Array[UInt]
result
.
(self : Array[UInt], value : UInt) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
UInt
high_bits
(UInt, UInt) -> UInt
|
Array[UInt16]
low_values
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j].
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
().
(self : Int) -> UInt

reinterpret the signed int as unsigned int, when the value is non-negative, i.e, 0..=2^31-1, the value is the same. When the value is negative, it turns into a large number, for example, -1 turns into 2^32-1

reinterpret_as_uint
())
149 } 150 } 151
Array[UInt]
result
152} 153 154///| 并集操作 155pub fn
(self : RoaringBitmap, other : RoaringBitmap) -> RoaringBitmap

并集操作

union
(
RoaringBitmap
self
:
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
,
RoaringBitmap
other
:
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
) ->
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
{
156 if
RoaringBitmap
self
.
(self : RoaringBitmap) -> Bool

检查bitmap是否为空

is_empty
() {
157 return
RoaringBitmap
other
158 } 159 if
RoaringBitmap
other
.
(self : RoaringBitmap) -> Bool

检查bitmap是否为空

is_empty
() {
160 return
RoaringBitmap
self
161 } 162 let
Array[HighContainer]
result_containers
= []
163 let mut
Int
i
= 0
164 let mut
Int
j
= 0
165 let mut
Int
total_size
= 0
166 while
Int
i
(self_ : Int, other : Int) -> Bool
<
RoaringBitmap
self
.
Array[HighContainer]
containers
.
(self : Array[HighContainer]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(Bool, Bool) -> Bool
&&
Int
j
(self_ : Int, other : Int) -> Bool
<
RoaringBitmap
other
.
Array[HighContainer]
containers
.
(self : Array[HighContainer]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
167 let
HighContainer
container1
=
RoaringBitmap
self
.
Array[HighContainer]
containers
(Array[HighContainer], Int) -> HighContainer

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
168 let
HighContainer
container2
=
RoaringBitmap
other
.
Array[HighContainer]
containers
(Array[HighContainer], Int) -> HighContainer

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j]
169 if
HighContainer
container1
.
UInt16
key
(self_ : UInt16, other : UInt16) -> Bool
<
HighContainer
container2
.
UInt16
key
{
170
Array[HighContainer]
result_containers
.
(self : Array[HighContainer], value : HighContainer) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
HighContainer
container1
)
171
Int
total_size
=
Int
total_size
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
HighContainer
container1
.
Int
cardinality
172
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
173 } else if
HighContainer
container1
.
UInt16
key
(self_ : UInt16, other : UInt16) -> Bool
>
HighContainer
container2
.
UInt16
key
{
174
Array[HighContainer]
result_containers
.
(self : Array[HighContainer], value : HighContainer) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
HighContainer
container2
)
175
Int
total_size
=
Int
total_size
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
HighContainer
container2
.
Int
cardinality
176
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
177 } else { 178 // 相同key,需要合并容器 179 let
Container
merged_container
=
(container1 : Container, container2 : Container) -> Container

容器并集操作

container_union
(
180
HighContainer
container1
.
Container
container
,
181
HighContainer
container2
.
Container
container
,
182 ) 183 let
Int
merged_cardinality
=
(container : Container) -> Int

获取容器基数

container_cardinality
(
Container
merged_container
)
184 let
HighContainer
merged_high_container
=
struct HighContainer {
  key: UInt16
  container: Container
  cardinality: Int
}

高级容器 - 处理 32 位整数的高 16 位

HighContainer
::{
185
UInt16
key
:
HighContainer
container1
.
UInt16
key
,
186
Container
container
:
Container
merged_container
,
187
Int
cardinality
:
Int
merged_cardinality
,
188 } 189
Array[HighContainer]
result_containers
.
(self : Array[HighContainer], value : HighContainer) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
HighContainer
merged_high_container
)
190
Int
total_size
=
Int
total_size
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
Int
merged_cardinality
191
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
192
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
193 } 194 } 195 196 // 添加剩余的容器 197 while
Int
i
(self_ : Int, other : Int) -> Bool
<
RoaringBitmap
self
.
Array[HighContainer]
containers
.
(self : Array[HighContainer]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
198 let
HighContainer
container
=
RoaringBitmap
self
.
Array[HighContainer]
containers
(Array[HighContainer], Int) -> HighContainer

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
199
Array[HighContainer]
result_containers
.
(self : Array[HighContainer], value : HighContainer) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
HighContainer
container
)
200
Int
total_size
=
Int
total_size
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
HighContainer
container
.
Int
cardinality
201
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
202 } 203 while
Int
j
(self_ : Int, other : Int) -> Bool
<
RoaringBitmap
other
.
Array[HighContainer]
containers
.
(self : Array[HighContainer]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
204 let
HighContainer
container
=
RoaringBitmap
other
.
Array[HighContainer]
containers
(Array[HighContainer], Int) -> HighContainer

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j]
205
Array[HighContainer]
result_containers
.
(self : Array[HighContainer], value : HighContainer) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
HighContainer
container
)
206
Int
total_size
=
Int
total_size
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
HighContainer
container
.
Int
cardinality
207
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
208 } 209
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
::{
Array[HighContainer]
containers
:
Array[HighContainer]
result_containers
,
Int
size
:
Int
total_size
}
210} 211 212///| 交集操作 213pub fn
(self : RoaringBitmap, other : RoaringBitmap) -> RoaringBitmap

交集操作

intersect
(
RoaringBitmap
self
:
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
,
RoaringBitmap
other
:
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
) ->
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
{
214 if
RoaringBitmap
self
.
(self : RoaringBitmap) -> Bool

检查bitmap是否为空

is_empty
()
(Bool, Bool) -> Bool
||
RoaringBitmap
other
.
(self : RoaringBitmap) -> Bool

检查bitmap是否为空

is_empty
() {
215 return
() -> RoaringBitmap

创建空的 RoaringBitmap

new
()
216 } 217 let
Array[HighContainer]
result_containers
= []
218 let mut
Int
i
= 0
219 let mut
Int
j
= 0
220 let mut
Int
total_size
= 0
221 while
Int
i
(self_ : Int, other : Int) -> Bool
<
RoaringBitmap
self
.
Array[HighContainer]
containers
.
(self : Array[HighContainer]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(Bool, Bool) -> Bool
&&
Int
j
(self_ : Int, other : Int) -> Bool
<
RoaringBitmap
other
.
Array[HighContainer]
containers
.
(self : Array[HighContainer]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
222 let
HighContainer
container1
=
RoaringBitmap
self
.
Array[HighContainer]
containers
(Array[HighContainer], Int) -> HighContainer

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
223 let
HighContainer
container2
=
RoaringBitmap
other
.
Array[HighContainer]
containers
(Array[HighContainer], Int) -> HighContainer

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j]
224 if
HighContainer
container1
.
UInt16
key
(self_ : UInt16, other : UInt16) -> Bool
<
HighContainer
container2
.
UInt16
key
{
225
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
226 } else if
HighContainer
container1
.
UInt16
key
(self_ : UInt16, other : UInt16) -> Bool
>
HighContainer
container2
.
UInt16
key
{
227
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
228 } else { 229 // 相同key,计算交集 230 let
Container
intersect_container
=
(container1 : Container, container2 : Container) -> Container

容器交集操作

container_intersect
(
231
HighContainer
container1
.
Container
container
,
232
HighContainer
container2
.
Container
container
,
233 ) 234 let
Int
intersect_cardinality
=
(container : Container) -> Int

获取容器基数

container_cardinality
(
Container
intersect_container
)
235 if
Int
intersect_cardinality
(self_ : Int, other : Int) -> Bool
>
0 {
236 let
HighContainer
intersect_high_container
=
struct HighContainer {
  key: UInt16
  container: Container
  cardinality: Int
}

高级容器 - 处理 32 位整数的高 16 位

HighContainer
::{
237
UInt16
key
:
HighContainer
container1
.
UInt16
key
,
238
Container
container
:
Container
intersect_container
,
239
Int
cardinality
:
Int
intersect_cardinality
,
240 } 241
Array[HighContainer]
result_containers
.
(self : Array[HighContainer], value : HighContainer) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
HighContainer
intersect_high_container
)
242
Int
total_size
=
Int
total_size
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
Int
intersect_cardinality
243 } 244
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
245
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
246 } 247 } 248
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
::{
Array[HighContainer]
containers
:
Array[HighContainer]
result_containers
,
Int
size
:
Int
total_size
}
249} 250 251///| 差集操作 (self - other) 252pub fn
(self : RoaringBitmap, other : RoaringBitmap) -> RoaringBitmap

差集操作 (self - other)

difference
(
RoaringBitmap
self
:
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
,
RoaringBitmap
other
:
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
) ->
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
{
253 if
RoaringBitmap
self
.
(self : RoaringBitmap) -> Bool

检查bitmap是否为空

is_empty
()
(Bool, Bool) -> Bool
||
RoaringBitmap
other
.
(self : RoaringBitmap) -> Bool

检查bitmap是否为空

is_empty
() {
254 return
RoaringBitmap
self
255 } 256 let
Array[HighContainer]
result_containers
= []
257 let mut
Int
i
= 0
258 let mut
Int
j
= 0
259 let mut
Int
total_size
= 0
260 while
Int
i
(self_ : Int, other : Int) -> Bool
<
RoaringBitmap
self
.
Array[HighContainer]
containers
.
(self : Array[HighContainer]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(Bool, Bool) -> Bool
&&
Int
j
(self_ : Int, other : Int) -> Bool
<
RoaringBitmap
other
.
Array[HighContainer]
containers
.
(self : Array[HighContainer]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
261 let
HighContainer
container1
=
RoaringBitmap
self
.
Array[HighContainer]
containers
(Array[HighContainer], Int) -> HighContainer

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
262 let
HighContainer
container2
=
RoaringBitmap
other
.
Array[HighContainer]
containers
(Array[HighContainer], Int) -> HighContainer

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j]
263 if
HighContainer
container1
.
UInt16
key
(self_ : UInt16, other : UInt16) -> Bool
<
HighContainer
container2
.
UInt16
key
{
264
Array[HighContainer]
result_containers
.
(self : Array[HighContainer], value : HighContainer) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
HighContainer
container1
)
265
Int
total_size
=
Int
total_size
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
HighContainer
container1
.
Int
cardinality
266
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
267 } else if
HighContainer
container1
.
UInt16
key
(self_ : UInt16, other : UInt16) -> Bool
>
HighContainer
container2
.
UInt16
key
{
268
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
269 } else { 270 // 相同key,计算差集 271 let
Container
diff_container
=
(container1 : Container, container2 : Container) -> Container

容器差集操作

container_difference
(
272
HighContainer
container1
.
Container
container
,
273
HighContainer
container2
.
Container
container
,
274 ) 275 let
Int
diff_cardinality
=
(container : Container) -> Int

获取容器基数

container_cardinality
(
Container
diff_container
)
276 if
Int
diff_cardinality
(self_ : Int, other : Int) -> Bool
>
0 {
277 let
HighContainer
diff_high_container
=
struct HighContainer {
  key: UInt16
  container: Container
  cardinality: Int
}

高级容器 - 处理 32 位整数的高 16 位

HighContainer
::{
278
UInt16
key
:
HighContainer
container1
.
UInt16
key
,
279
Container
container
:
Container
diff_container
,
280
Int
cardinality
:
Int
diff_cardinality
,
281 } 282
Array[HighContainer]
result_containers
.
(self : Array[HighContainer], value : HighContainer) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
HighContainer
diff_high_container
)
283
Int
total_size
=
Int
total_size
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
Int
diff_cardinality
284 } 285
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
286
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
287 } 288 } 289 290 // 添加剩余的容器 291 while
Int
i
(self_ : Int, other : Int) -> Bool
<
RoaringBitmap
self
.
Array[HighContainer]
containers
.
(self : Array[HighContainer]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
292 let
HighContainer
container
=
RoaringBitmap
self
.
Array[HighContainer]
containers
(Array[HighContainer], Int) -> HighContainer

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
293
Array[HighContainer]
result_containers
.
(self : Array[HighContainer], value : HighContainer) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
HighContainer
container
)
294
Int
total_size
=
Int
total_size
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
HighContainer
container
.
Int
cardinality
295
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
296 } 297
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
::{
Array[HighContainer]
containers
:
Array[HighContainer]
result_containers
,
Int
size
:
Int
total_size
}
298} 299 300///| 获取统计信息 301pub fn
(self : RoaringBitmap) -> RoaringStats

获取统计信息

get_stats
(
RoaringBitmap
self
:
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
) ->
struct RoaringStats {
  total_containers: Int
  array_containers: Int
  bitmap_containers: Int
  run_containers: Int
  total_bytes: Int
  compression_ratio: Double
}

统计信息结构

RoaringStats
{
302 let mut
Int
array_count
= 0
303 let mut
Int
bitmap_count
= 0
304 let mut
Int
run_count
= 0
305 let mut
Int
total_bytes
= 0
306 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
RoaringBitmap
self
.
Array[HighContainer]
containers
.
(self : Array[HighContainer]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
307 let
Container
container
=
RoaringBitmap
self
.
Array[HighContainer]
containers
(Array[HighContainer], Int) -> HighContainer

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i].
Container
container
308 match
Container
container
{
309
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr
) => {
310
Int
array_count
=
Int
array_count
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
311
Int
total_bytes
=
Int
total_bytes
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
Array[UInt16]
arr
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self : Int, other : Int) -> Int

Multiplies two 32-bit integers. This is the implementation of the * operator for Int.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns the product of the two integers. If the result overflows the range of Int, it wraps around according to two's complement arithmetic.

Example:

  inspect(42 * 2, content="84")
  inspect(-10 * 3, content="-30")
  let max = 2147483647 // Int.max_value
  inspect(max * 2, content="-2") // Overflow wraps around
*
2 // 每个UInt16占2字节
312 } 313
Container
BitmapContainer
(_) => {
314
Int
bitmap_count
=
Int
bitmap_count
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
315
Int
total_bytes
=
Int
total_bytes
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
Int
BITMAP_CONTAINER_SIZE
316 } 317
(Array[Run]) -> Container
RunContainer
(
Array[Run]
runs
) => {
318
Int
run_count
=
Int
run_count
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
319
Int
total_bytes
=
Int
total_bytes
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
Array[Run]
runs
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self : Int, other : Int) -> Int

Multiplies two 32-bit integers. This is the implementation of the * operator for Int.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns the product of the two integers. If the result overflows the range of Int, it wraps around according to two's complement arithmetic.

Example:

  inspect(42 * 2, content="84")
  inspect(-10 * 3, content="-30")
  let max = 2147483647 // Int.max_value
  inspect(max * 2, content="-2") // Overflow wraps around
*
4 // 每个Run占4字节
320 } 321 } 322 } 323 let
Int
original_size
= if
RoaringBitmap
self
.
Int
size
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
0 { 1 } else { (
RoaringBitmap
self
.
Int
size
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
7)
(self : Int, other : Int) -> Int

Performs integer division between two 32-bit integers. The result is truncated towards zero (rounds down for positive numbers and up for negative numbers).

Parameters:

  • dividend : The first integer operand to be divided.
  • divisor : The second integer operand that divides the dividend.

Returns the quotient of the division operation.

Throws a panic if divisor is zero.

Example:

  inspect(10 / 3, content="3") // truncates towards zero
  inspect(-10 / 3, content="-3")
  inspect(10 / -3, content="-3")
/
8 } // 避免除零
324 let
Double
compression_ratio
=
Int
total_bytes
.
(self : Int) -> Double

Converts a 32-bit integer to a double-precision floating-point number. The conversion preserves the exact value since all integers in the range of Int can be represented exactly as Double values.

Parameters:

  • self : The 32-bit integer to be converted.

Returns a double-precision floating-point number that represents the same numerical value as the input integer.

Example:

  let n = 42
  inspect(n.to_double(), content="42")
  let neg = -42
  inspect(neg.to_double(), content="-42")
to_double
()
(self : Double, other : Double) -> Double

Performs division between two double-precision floating-point numbers. Follows IEEE 754 standard for floating-point arithmetic, including handling of special cases like division by zero (returns infinity) and operations involving NaN.

Parameters:

  • self : The dividend (numerator) in the division operation.
  • other : The divisor (denominator) in the division operation.

Returns the result of dividing self by other. Special cases follow IEEE 754:

  • Division by zero returns positive or negative infinity based on the dividend's sign
  • Operations involving NaN return NaN
  • Division of infinity by infinity returns NaN

Example:

  inspect(6.0 / 2.0, content="3")
  inspect(-6.0 / 2.0, content="-3")
  inspect(1.0 / 0.0, content="Infinity")
/
Int
original_size
.
(self : Int) -> Double

Converts a 32-bit integer to a double-precision floating-point number. The conversion preserves the exact value since all integers in the range of Int can be represented exactly as Double values.

Parameters:

  • self : The 32-bit integer to be converted.

Returns a double-precision floating-point number that represents the same numerical value as the input integer.

Example:

  let n = 42
  inspect(n.to_double(), content="42")
  let neg = -42
  inspect(neg.to_double(), content="-42")
to_double
()
325
struct RoaringStats {
  total_containers: Int
  array_containers: Int
  bitmap_containers: Int
  run_containers: Int
  total_bytes: Int
  compression_ratio: Double
}

统计信息结构

RoaringStats
::{
326
Int
total_containers
:
RoaringBitmap
self
.
Array[HighContainer]
containers
.
(self : Array[HighContainer]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
(),
327
Int
array_containers
:
Int
array_count
,
328
Int
bitmap_containers
:
Int
bitmap_count
,
329
Int
run_containers
:
Int
run_count
,
330
Int
total_bytes
,
331
Double
compression_ratio
,
332 } 333} 334 335// =============== 辅助函数 =============== 336 337///| 提取32位整数的高16位 338fn
(value : UInt) -> UInt16

提取32位整数的高16位

extract_high_16
(
UInt
value
:
UInt
UInt
) ->
UInt16
UInt16
{
339 (
UInt
value
(self : UInt, shift : Int) -> UInt

Performs a logical right shift operation on an unsigned 32-bit integer. The operation shifts all bits to the right by a specified number of positions, filling the leftmost positions with zeros.

Parameters:

  • self : The unsigned 32-bit integer to be shifted.
  • shift : The number of positions to shift right. If this value is negative, the behavior is undefined. Values larger than 31 are masked with & 31.

Returns a new unsigned 32-bit integer containing the result of the right shift operation.

Example:

  let x = 0xFF000000U
  inspect(x >> 8, content="16711680") // 0x00FF0000
  inspect(x >> 24, content="255") // 0x000000FF

  let x = 0xFF000000U
  inspect(x >> 32, content="4278190080") // Same as x >> 0 due to masking
>>
16).
(self : UInt) -> Int

reinterpret the unsigned int as signed int For number within the range of 0..=2^31-1, the value is the same. For number within the range of 2^31..=2^32-1, the value is negative

reinterpret_as_int
().
(self : Int) -> UInt16

Converts a 32-bit signed integer to a 16-bit unsigned integer by truncating its value to fit within the range of 0 to 65535.

Parameters:

  • integer : The 32-bit signed integer to be converted. Values outside the range of UInt16 will be truncated to fit.

Returns a 16-bit unsigned integer containing the lower 16 bits of the input value.

Example:

  let n = 42
  inspect(n.to_uint16(), content="42")
  let neg = -1
  inspect(neg.to_uint16(), content="65535") // -1 becomes max value of UInt16
  let large = 65536
  inspect(large.to_uint16(), content="0") // Values wrap around
to_uint16
()
340} 341 342///| 提取32位整数的低16位 343fn
(value : UInt) -> UInt16

提取32位整数的低16位

extract_low_16
(
UInt
value
:
UInt
UInt
) ->
UInt16
UInt16
{
344 let
UInt
low_bits
=
UInt
value
(self : UInt, other : UInt) -> UInt

Calculates the remainder of dividing one unsigned integer by another.

Parameters:

  • self : The unsigned integer dividend.
  • other : The unsigned integer divisor.

Returns the remainder of the division operation.

Throws a panic if other is zero.

Example:

  let a = 17U
  let b = 5U
  inspect(a % b, content="2") // 17 divided by 5 gives quotient 3 and remainder 2
  inspect(7U % 4U, content="3")
%
65536U // 2^16 = 65536
345
UInt
low_bits
.
(self : UInt) -> Int

reinterpret the unsigned int as signed int For number within the range of 0..=2^31-1, the value is the same. For number within the range of 2^31..=2^32-1, the value is negative

reinterpret_as_int
().
(self : Int) -> UInt16

Converts a 32-bit signed integer to a 16-bit unsigned integer by truncating its value to fit within the range of 0 to 65535.

Parameters:

  • integer : The 32-bit signed integer to be converted. Values outside the range of UInt16 will be truncated to fit.

Returns a 16-bit unsigned integer containing the lower 16 bits of the input value.

Example:

  let n = 42
  inspect(n.to_uint16(), content="42")
  let neg = -1
  inspect(neg.to_uint16(), content="65535") // -1 becomes max value of UInt16
  let large = 65536
  inspect(large.to_uint16(), content="0") // Values wrap around
to_uint16
()
346} 347 348///| 在排序数组中查找容器索引 349fn
(containers : Array[HighContainer], key : UInt16) -> SearchResult

在排序数组中查找容器索引

find_container_index
(
350
Array[HighContainer]
containers
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct HighContainer {
  key: UInt16
  container: Container
  cardinality: Int
}

高级容器 - 处理 32 位整数的高 16 位

HighContainer
],
351
UInt16
key
:
UInt16
UInt16
352) ->
enum SearchResult {
  Found(Int)
  NotFound(Int)
}

二进制搜索结果

SearchResult
{
353 let mut
Int
left
= 0
354 let mut
Int
right
=
Array[HighContainer]
containers
.
(self : Array[HighContainer]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
355 while
Int
left
(self_ : Int, other : Int) -> Bool
<
Int
right
{
356 let
Int
mid
= (
Int
left
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
Int
right
)
(self : Int, other : Int) -> Int

Performs integer division between two 32-bit integers. The result is truncated towards zero (rounds down for positive numbers and up for negative numbers).

Parameters:

  • dividend : The first integer operand to be divided.
  • divisor : The second integer operand that divides the dividend.

Returns the quotient of the division operation.

Throws a panic if divisor is zero.

Example:

  inspect(10 / 3, content="3") // truncates towards zero
  inspect(-10 / 3, content="-3")
  inspect(10 / -3, content="-3")
/
2
357 let
UInt16
mid_key
=
Array[HighContainer]
containers
(Array[HighContainer], Int) -> HighContainer

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
mid].
UInt16
key
358 if
UInt16
mid_key
(self : UInt16, that : UInt16) -> Bool
==
UInt16
key
{
359 return
(Int) -> SearchResult
Found
(
Int
mid
)
360 } else if
UInt16
mid_key
(self_ : UInt16, other : UInt16) -> Bool
<
UInt16
key
{
361
Int
left
=
Int
mid
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
362 } else { 363
Int
right
=
Int
mid
364 } 365 } 366
(Int) -> SearchResult
NotFound
(
Int
left
)
367} 368 369///| 在指定位置插入元素到数组 370fn
(arr : Array[T], index : Int, element : T) -> Array[T]

在指定位置插入元素到数组

array_insert_at
[T](
Array[T]
arr
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[

type parameter T

T
],
Int
index
:
Int
Int
,
T
element
:

type parameter T

T
) ->
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[

type parameter T

T
] {
371 let
Array[T]
result
= []
372 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Int
index
;
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
373
Array[T]
result
.
(self : Array[T], value : T) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Array[T]
arr
(Array[T], Int) -> T

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i])
374 } 375
Array[T]
result
.
(self : Array[T], value : T) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
T
element
)
376 for
Int
i
=
Int
index
;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[T]
arr
.
(self : Array[T]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
377
Array[T]
result
.
(self : Array[T], value : T) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Array[T]
arr
(Array[T], Int) -> T

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i])
378 } 379
Array[T]
result
380} 381 382///| 替换数组中指定位置的元素 383fn
(arr : Array[T], index : Int, element : T) -> Array[T]

替换数组中指定位置的元素

array_replace_at
[T](
Array[T]
arr
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[

type parameter T

T
],
Int
index
:
Int
Int
,
T
element
:

type parameter T

T
) ->
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[

type parameter T

T
] {
384 let
Array[T]
result
= []
385 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[T]
arr
.
(self : Array[T]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
386 if
Int
i
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
Int
index
{
387
Array[T]
result
.
(self : Array[T], value : T) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
T
element
)
388 } else { 389
Array[T]
result
.
(self : Array[T], value : T) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Array[T]
arr
(Array[T], Int) -> T

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i])
390 } 391 } 392
Array[T]
result
393} 394 395///| 移除数组中指定位置的元素 396fn
(arr : Array[T], index : Int) -> Array[T]

移除数组中指定位置的元素

array_remove_at
[T](
Array[T]
arr
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[

type parameter T

T
],
Int
index
:
Int
Int
) ->
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[

type parameter T

T
] {
397 let
Array[T]
result
= []
398 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[T]
arr
.
(self : Array[T]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
399 if
Int
i
(x : Int, y : Int) -> Bool
!=
Int
index
{
400
Array[T]
result
.
(self : Array[T], value : T) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Array[T]
arr
(Array[T], Int) -> T

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i])
401 } 402 } 403
Array[T]
result
404} 405 406///| 检查容器是否包含指定值 407fn
(container : Container, value : UInt16) -> Bool

检查容器是否包含指定值

container_contains
(
Container
container
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
,
UInt16
value
:
UInt16
UInt16
) ->
Bool
Bool
{
408 match
Container
container
{
409
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr
) =>
(arr : Array[UInt16], value : UInt16) -> Bool

数组容器包含检查

array_container_contains
(
Array[UInt16]
arr
,
UInt16
value
)
410
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap
) =>
(bitmap : FixedArray[UInt64], value : UInt16) -> Bool

位图容器包含检查

bitmap_container_contains
(
FixedArray[UInt64]
bitmap
,
UInt16
value
)
411
(Array[Run]) -> Container
RunContainer
(
Array[Run]
runs
) =>
(runs : Array[Run], value : UInt16) -> Bool
run_container_contains
(
Array[Run]
runs
,
UInt16
value
)
412 } 413} 414 415///| 向容器添加元素 416fn
(container : Container, value : UInt16) -> Container

向容器添加元素

container_add
(
Container
container
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
,
UInt16
value
:
UInt16
UInt16
) ->
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
{
417 match
Container
container
{
418
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr
) => {
419 let
Array[UInt16]
new_arr
=
(arr : Array[UInt16], value : UInt16) -> Array[UInt16]

数组容器添加元素

array_container_add
(
Array[UInt16]
arr
,
UInt16
value
)
420 // 检查是否需要转换为位图容器 421 if
Array[UInt16]
new_arr
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self_ : Int, other : Int) -> Bool
>=
Int

容器配置常量

ARRAY_CONTAINER_MAX_SIZE
{
422
(arr : Array[UInt16]) -> Container

数组容器转换为位图容器

array_to_bitmap_container
(
Array[UInt16]
new_arr
)
423 } else { 424
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
new_arr
)
425 } 426 } 427
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap
) =>
428
(FixedArray[UInt64]) -> Container
BitmapContainer
(
(bitmap : FixedArray[UInt64], value : UInt16) -> FixedArray[UInt64]

位图容器添加元素

bitmap_container_add
(
FixedArray[UInt64]
bitmap
,
UInt16
value
))
429
(Array[Run]) -> Container
RunContainer
(
Array[Run]
runs
) => {
430 let
Array[Run]
new_runs
=
(runs : Array[Run], value : UInt16) -> Array[Run]

改进的运行容器添加元素操作

run_container_add
(
Array[Run]
runs
,
UInt16
value
)
431
(Array[Run]) -> Container
RunContainer
(
Array[Run]
new_runs
)
432 } 433 } 434} 435 436///| 从容器移除元素 437fn
(container : Container, value : UInt16) -> Container

从容器移除元素

container_remove
(
Container
container
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
,
UInt16
value
:
UInt16
UInt16
) ->
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
{
438 match
Container
container
{
439
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr
) =>
(Array[UInt16]) -> Container
ArrayContainer
(
(arr : Array[UInt16], value : UInt16) -> Array[UInt16]

数组容器移除元素

array_container_remove
(
Array[UInt16]
arr
,
UInt16
value
))
440
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap
) => {
441 let
FixedArray[UInt64]
new_bitmap
=
(bitmap : FixedArray[UInt64], value : UInt16) -> FixedArray[UInt64]

位图容器移除元素

bitmap_container_remove
(
FixedArray[UInt64]
bitmap
,
UInt16
value
)
442 let
Int
cardinality
=
(bitmap : FixedArray[UInt64]) -> Int

计算位图容器基数

bitmap_container_cardinality
(
FixedArray[UInt64]
new_bitmap
)
443 // 检查是否需要转换为数组容器 444 if
Int
cardinality
(self_ : Int, other : Int) -> Bool
<
Int

容器配置常量

ARRAY_CONTAINER_MAX_SIZE
{
445
(bitmap : FixedArray[UInt64]) -> Container

位图容器转换为数组容器

bitmap_to_array_container
(
FixedArray[UInt64]
new_bitmap
)
446 } else { 447
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
new_bitmap
)
448 } 449 } 450
(Array[Run]) -> Container
RunContainer
(
Array[Run]
runs
) =>
(Array[Run]) -> Container
RunContainer
(
(runs : Array[Run], value : UInt16) -> Array[Run]

改进的运行容器移除元素操作

run_container_remove
(
Array[Run]
runs
,
UInt16
value
))
451 } 452} 453 454///| 容器并集操作 455fn
(container1 : Container, container2 : Container) -> Container

容器并集操作

container_union
(
Container
container1
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
,
Container
container2
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
) ->
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
{
456 match (
Container
container1
,
Container
container2
) {
457 (
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr1
),
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr2
)) => {
458 let
Array[UInt16]
union_arr
=
(arr1 : Array[UInt16], arr2 : Array[UInt16]) -> Array[UInt16]

数组容器并集

array_container_union
(
Array[UInt16]
arr1
,
Array[UInt16]
arr2
)
459 if
Array[UInt16]
union_arr
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self_ : Int, other : Int) -> Bool
>=
Int

容器配置常量

ARRAY_CONTAINER_MAX_SIZE
{
460
(arr : Array[UInt16]) -> Container

数组容器转换为位图容器

array_to_bitmap_container
(
Array[UInt16]
union_arr
)
461 } else { 462
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
union_arr
)
463 } 464 } 465 (
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr
),
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap
)) =>
466
(FixedArray[UInt64]) -> Container
BitmapContainer
(
(bitmap : FixedArray[UInt64], arr : Array[UInt16]) -> FixedArray[UInt64]
bitmap_container_union_with_array
(
FixedArray[UInt64]
bitmap
,
Array[UInt16]
arr
))
467 (
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap
),
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr
)) =>
468
(FixedArray[UInt64]) -> Container
BitmapContainer
(
(bitmap : FixedArray[UInt64], arr : Array[UInt16]) -> FixedArray[UInt64]
bitmap_container_union_with_array
(
FixedArray[UInt64]
bitmap
,
Array[UInt16]
arr
))
469 (
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap1
),
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap2
)) =>
470
(FixedArray[UInt64]) -> Container
BitmapContainer
(
(bitmap1 : FixedArray[UInt64], bitmap2 : FixedArray[UInt64]) -> FixedArray[UInt64]

位图容器并集

bitmap_container_union
(
FixedArray[UInt64]
bitmap1
,
FixedArray[UInt64]
bitmap2
))
471 (
(Array[Run]) -> Container
RunContainer
(
Array[Run]
runs1
),
(Array[Run]) -> Container
RunContainer
(
Array[Run]
runs2
)) =>
472
(Array[Run]) -> Container
RunContainer
(
(runs1 : Array[Run], runs2 : Array[Run]) -> Array[Run]

运行容器并集操作(完善实现)

run_container_union
(
Array[Run]
runs1
,
Array[Run]
runs2
))
473 // 其他组合可以通过转换处理 474 _ =>
(container1 : Container, container2 : Container) -> Container
container_union_generic
(
Container
container1
,
Container
container2
)
475 } 476} 477 478///| 容器交集操作 479fn
(container1 : Container, container2 : Container) -> Container

容器交集操作

container_intersect
(
480
Container
container1
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
,
481
Container
container2
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
482) ->
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
{
483 match (
Container
container1
,
Container
container2
) {
484 (
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr1
),
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr2
)) =>
485
(Array[UInt16]) -> Container
ArrayContainer
(
(arr1 : Array[UInt16], arr2 : Array[UInt16]) -> Array[UInt16]

数组容器交集

array_container_intersect
(
Array[UInt16]
arr1
,
Array[UInt16]
arr2
))
486 (
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr
),
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap
)) =>
487
(Array[UInt16]) -> Container
ArrayContainer
(
(arr : Array[UInt16], bitmap : FixedArray[UInt64]) -> Array[UInt16]
array_container_intersect_with_bitmap
(
Array[UInt16]
arr
,
FixedArray[UInt64]
bitmap
))
488 (
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap
),
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr
)) =>
489
(Array[UInt16]) -> Container
ArrayContainer
(
(arr : Array[UInt16], bitmap : FixedArray[UInt64]) -> Array[UInt16]
array_container_intersect_with_bitmap
(
Array[UInt16]
arr
,
FixedArray[UInt64]
bitmap
))
490 (
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap1
),
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap2
)) => {
491 let
FixedArray[UInt64]
intersect_bitmap
=
(bitmap1 : FixedArray[UInt64], bitmap2 : FixedArray[UInt64]) -> FixedArray[UInt64]

位图容器交集

bitmap_container_intersect
(
FixedArray[UInt64]
bitmap1
,
FixedArray[UInt64]
bitmap2
)
492 let
Int
cardinality
=
(bitmap : FixedArray[UInt64]) -> Int

计算位图容器基数

bitmap_container_cardinality
(
FixedArray[UInt64]
intersect_bitmap
)
493 if
Int
cardinality
(self_ : Int, other : Int) -> Bool
<
Int

容器配置常量

ARRAY_CONTAINER_MAX_SIZE
{
494
(bitmap : FixedArray[UInt64]) -> Container

位图容器转换为数组容器

bitmap_to_array_container
(
FixedArray[UInt64]
intersect_bitmap
) // 修正:直接调用转换函数
495 } else { 496
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
intersect_bitmap
)
497 } 498 } 499 (
(Array[Run]) -> Container
RunContainer
(
Array[Run]
runs1
),
(Array[Run]) -> Container
RunContainer
(
Array[Run]
runs2
)) =>
500
(Array[Run]) -> Container
RunContainer
(
(runs1 : Array[Run], runs2 : Array[Run]) -> Array[Run]

运行容器交集操作

run_container_intersect
(
Array[Run]
runs1
,
Array[Run]
runs2
))
501 _ =>
(container1 : Container, container2 : Container) -> Container
container_intersect_generic
(
Container
container1
,
Container
container2
)
502 } 503} 504 505///| 容器差集操作 506///| 容器差集操作 507fn
(container1 : Container, container2 : Container) -> Container

容器差集操作

container_difference
(
508
Container
container1
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
,
509
Container
container2
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
510) ->
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
{
511 match (
Container
container1
,
Container
container2
) {
512 (
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr1
),
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr2
)) =>
513
(Array[UInt16]) -> Container
ArrayContainer
(
(arr1 : Array[UInt16], arr2 : Array[UInt16]) -> Array[UInt16]

数组容器差集

array_container_difference
(
Array[UInt16]
arr1
,
Array[UInt16]
arr2
))
514 (
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr
),
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap
)) =>
515
(Array[UInt16]) -> Container
ArrayContainer
(
(arr : Array[UInt16], bitmap : FixedArray[UInt64]) -> Array[UInt16]
array_container_difference_with_bitmap
(
Array[UInt16]
arr
,
FixedArray[UInt64]
bitmap
))
516 (
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap
),
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr
)) => {
517 let
FixedArray[UInt64]
diff_bitmap
=
(bitmap : FixedArray[UInt64], arr : Array[UInt16]) -> FixedArray[UInt64]
bitmap_container_difference_with_array
(
FixedArray[UInt64]
bitmap
,
Array[UInt16]
arr
)
518 let
Int
cardinality
=
(bitmap : FixedArray[UInt64]) -> Int

计算位图容器基数

bitmap_container_cardinality
(
FixedArray[UInt64]
diff_bitmap
)
519 if
Int
cardinality
(self_ : Int, other : Int) -> Bool
<
Int

容器配置常量

ARRAY_CONTAINER_MAX_SIZE
{
520
(bitmap : FixedArray[UInt64]) -> Container

位图容器转换为数组容器

bitmap_to_array_container
(
FixedArray[UInt64]
diff_bitmap
) // 修正:直接调用转换函数
521 } else { 522
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
diff_bitmap
)
523 } 524 } 525 (
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap1
),
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap2
)) => {
526 let
FixedArray[UInt64]
diff_bitmap
=
(bitmap1 : FixedArray[UInt64], bitmap2 : FixedArray[UInt64]) -> FixedArray[UInt64]

位图容器差集

bitmap_container_difference
(
FixedArray[UInt64]
bitmap1
,
FixedArray[UInt64]
bitmap2
)
527 let
Int
cardinality
=
(bitmap : FixedArray[UInt64]) -> Int

计算位图容器基数

bitmap_container_cardinality
(
FixedArray[UInt64]
diff_bitmap
)
528 if
Int
cardinality
(self_ : Int, other : Int) -> Bool
<
Int

容器配置常量

ARRAY_CONTAINER_MAX_SIZE
{
529
(bitmap : FixedArray[UInt64]) -> Container

位图容器转换为数组容器

bitmap_to_array_container
(
FixedArray[UInt64]
diff_bitmap
) // 修正:直接调用转换函数
530 } else { 531
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
diff_bitmap
)
532 } 533 } 534 (
(Array[Run]) -> Container
RunContainer
(
Array[Run]
runs1
),
(Array[Run]) -> Container
RunContainer
(
Array[Run]
runs2
)) =>
535
(Array[Run]) -> Container
RunContainer
(
(runs1 : Array[Run], runs2 : Array[Run]) -> Array[Run]

运行容器差集操作 (runs1 - runs2)

run_container_difference
(
Array[Run]
runs1
,
Array[Run]
runs2
))
536 _ =>
(container1 : Container, container2 : Container) -> Container
container_difference_generic
(
Container
container1
,
Container
container2
)
537 } 538} 539 540///| 获取容器基数 541fn
(container : Container) -> Int

获取容器基数

container_cardinality
(
Container
container
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
) ->
Int
Int
{
542 match
Container
container
{
543
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr
) =>
Array[UInt16]
arr
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
544
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap
) =>
(bitmap : FixedArray[UInt64]) -> Int

计算位图容器基数

bitmap_container_cardinality
(
FixedArray[UInt64]
bitmap
)
545
(Array[Run]) -> Container
RunContainer
(
Array[Run]
runs
) =>
(runs : Array[Run]) -> Int
run_container_cardinality
(
Array[Run]
runs
)
546 } 547} 548 549///| 容器转换为数组 550fn
(container : Container) -> Array[UInt16]

容器转换为数组

container_to_array
(
Container
container
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
) ->
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
] {
551 match
Container
container
{
552
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr
) =>
Array[UInt16]
arr
553
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap
) =>
(bitmap : FixedArray[UInt64]) -> Array[UInt16]

位图容器转数组

bitmap_container_to_array
(
FixedArray[UInt64]
bitmap
)
554
(Array[Run]) -> Container
RunContainer
(
Array[Run]
runs
) =>
(runs : Array[Run]) -> Array[UInt16]
run_container_to_array
(
Array[Run]
runs
)
555 } 556} 557 558// =============== 数组容器操作 =============== 559 560///| 数组容器包含检查 561fn
(arr : Array[UInt16], value : UInt16) -> Bool

数组容器包含检查

array_container_contains
(
Array[UInt16]
arr
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
],
UInt16
value
:
UInt16
UInt16
) ->
Bool
Bool
{
562
(arr : Array[UInt16], value : UInt16) -> Bool

二进制搜索包含检查

binary_search_contains
(
Array[UInt16]
arr
,
UInt16
value
)
563} 564 565///| 数组容器添加元素 566fn
(arr : Array[UInt16], value : UInt16) -> Array[UInt16]

数组容器添加元素

array_container_add
(
Array[UInt16]
arr
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
],
UInt16
value
:
UInt16
UInt16
) ->
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
] {
567 match
(arr : Array[UInt16], value : UInt16) -> SearchResult

二进制搜索

binary_search
(
Array[UInt16]
arr
,
UInt16
value
) {
568
SearchResult
Found
(_) =>
Array[UInt16]
arr
// 元素已存在
569
(Int) -> SearchResult
NotFound
(
Int
insert_pos
) =>
(arr : Array[UInt16], index : Int, value : UInt16) -> Array[UInt16]

在排序数组中插入元素

array_insert_sorted
(
Array[UInt16]
arr
,
Int
insert_pos
,
UInt16
value
)
570 } 571} 572 573///| 数组容器移除元素 574fn
(arr : Array[UInt16], value : UInt16) -> Array[UInt16]

数组容器移除元素

array_container_remove
(
Array[UInt16]
arr
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
],
UInt16
value
:
UInt16
UInt16
) ->
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
] {
575 match
(arr : Array[UInt16], value : UInt16) -> SearchResult

二进制搜索

binary_search
(
Array[UInt16]
arr
,
UInt16
value
) {
576
(Int) -> SearchResult
Found
(
Int
index
) =>
(arr : Array[UInt16], index : Int) -> Array[UInt16]

从数组移除指定位置的UInt16元素

array_remove_at_uint16
(
Array[UInt16]
arr
,
Int
index
)
577
SearchResult
NotFound
(_) =>
Array[UInt16]
arr
// 元素不存在
578 } 579} 580 581///| 数组容器并集 582fn
(arr1 : Array[UInt16], arr2 : Array[UInt16]) -> Array[UInt16]

数组容器并集

array_container_union
(
583
Array[UInt16]
arr1
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
],
584
Array[UInt16]
arr2
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
]
585) ->
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
] {
586 let
Array[UInt16]
result
= []
587 let mut
Int
i
= 0
588 let mut
Int
j
= 0
589 while
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr1
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(Bool, Bool) -> Bool
&&
Int
j
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr2
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
590 let
UInt16
val1
=
Array[UInt16]
arr1
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
591 let
UInt16
val2
=
Array[UInt16]
arr2
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j]
592 if
UInt16
val1
(self_ : UInt16, other : UInt16) -> Bool
<
UInt16
val2
{
593
Array[UInt16]
result
.
(self : Array[UInt16], value : UInt16) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
UInt16
val1
)
594
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
595 } else if
UInt16
val1
(self_ : UInt16, other : UInt16) -> Bool
>
UInt16
val2
{
596
Array[UInt16]
result
.
(self : Array[UInt16], value : UInt16) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
UInt16
val2
)
597
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
598 } else { 599
Array[UInt16]
result
.
(self : Array[UInt16], value : UInt16) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
UInt16
val1
) // 相等,只添加一次
600
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
601
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
602 } 603 } 604 605 // 添加剩余元素 606 while
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr1
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
607
Array[UInt16]
result
.
(self : Array[UInt16], value : UInt16) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Array[UInt16]
arr1
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i])
608
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
609 } 610 while
Int
j
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr2
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
611
Array[UInt16]
result
.
(self : Array[UInt16], value : UInt16) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Array[UInt16]
arr2
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j])
612
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
613 } 614
Array[UInt16]
result
615} 616 617///| 数组容器交集 618fn
(arr1 : Array[UInt16], arr2 : Array[UInt16]) -> Array[UInt16]

数组容器交集

array_container_intersect
(
619
Array[UInt16]
arr1
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
],
620
Array[UInt16]
arr2
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
]
621) ->
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
] {
622 let
Array[UInt16]
result
= []
623 let mut
Int
i
= 0
624 let mut
Int
j
= 0
625 while
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr1
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(Bool, Bool) -> Bool
&&
Int
j
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr2
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
626 let
UInt16
val1
=
Array[UInt16]
arr1
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
627 let
UInt16
val2
=
Array[UInt16]
arr2
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j]
628 if
UInt16
val1
(self_ : UInt16, other : UInt16) -> Bool
<
UInt16
val2
{
629
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
630 } else if
UInt16
val1
(self_ : UInt16, other : UInt16) -> Bool
>
UInt16
val2
{
631
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
632 } else { 633
Array[UInt16]
result
.
(self : Array[UInt16], value : UInt16) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
UInt16
val1
) // 相等,添加到结果
634
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
635
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
636 } 637 } 638
Array[UInt16]
result
639} 640 641///| 数组容器差集 642fn
(arr1 : Array[UInt16], arr2 : Array[UInt16]) -> Array[UInt16]

数组容器差集

array_container_difference
(
643
Array[UInt16]
arr1
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
],
644
Array[UInt16]
arr2
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
]
645) ->
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
] {
646 let
Array[UInt16]
result
= []
647 let mut
Int
i
= 0
648 let mut
Int
j
= 0
649 while
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr1
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(Bool, Bool) -> Bool
&&
Int
j
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr2
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
650 let
UInt16
val1
=
Array[UInt16]
arr1
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
651 let
UInt16
val2
=
Array[UInt16]
arr2
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j]
652 if
UInt16
val1
(self_ : UInt16, other : UInt16) -> Bool
<
UInt16
val2
{
653
Array[UInt16]
result
.
(self : Array[UInt16], value : UInt16) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
UInt16
val1
) // val1 不在 arr2 中
654
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
655 } else if
UInt16
val1
(self_ : UInt16, other : UInt16) -> Bool
>
UInt16
val2
{
656
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
657 } else { 658 // 相等,跳过(从 arr1 中移除) 659
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
660
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
661 } 662 } 663 664 // 添加 arr1 的剩余元素 665 while
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr1
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
666
Array[UInt16]
result
.
(self : Array[UInt16], value : UInt16) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Array[UInt16]
arr1
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i])
667
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
668 } 669
Array[UInt16]
result
670} 671 672// =============== 位图容器操作 =============== 673 674///| 位图容器包含检查 675fn
(bitmap : FixedArray[UInt64], value : UInt16) -> Bool

位图容器包含检查

bitmap_container_contains
(
676
FixedArray[UInt64]
bitmap
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
],
677
UInt16
value
:
UInt16
UInt16
678) ->
Bool
Bool
{
679 let
Int
word_index
=
UInt16
value
.
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
()
(self : Int, other : Int) -> Int

Performs integer division between two 32-bit integers. The result is truncated towards zero (rounds down for positive numbers and up for negative numbers).

Parameters:

  • dividend : The first integer operand to be divided.
  • divisor : The second integer operand that divides the dividend.

Returns the quotient of the division operation.

Throws a panic if divisor is zero.

Example:

  inspect(10 / 3, content="3") // truncates towards zero
  inspect(-10 / 3, content="-3")
  inspect(10 / -3, content="-3")
/
64
680 let
Int
bit_index
=
UInt16
value
.
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
()
(self : Int, other : Int) -> Int

Calculates the remainder of dividing one integer by another. The result follows the formula dividend - (dividend / divisor) * divisor, maintaining the same sign as the dividend.

Parameters:

  • self : The dividend (the number being divided).
  • other : The divisor (the number to divide by).

Returns the remainder of the division. If other is 0, the behavior is undefined.

Example:

  inspect(7 % 3, content="1")
  inspect(-7 % 3, content="-1")
  inspect(7 % -3, content="1")
%
64
681 let
UInt64
word
=
FixedArray[UInt64]
bitmap
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
word_index]
682 (
UInt64
word
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise AND operation between two unsigned 64-bit integers.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns the result of performing a bitwise AND operation on the two operands.

Example:

  let a = 0xF0F0F0F0F0F0F0F0UL
  let b = 0xFF00FF00FF00FF00UL
  inspect(a & b, content="17294086455919964160") // 0xF000F000F000F000
&
(1UL
(self : UInt64, shift : Int) -> UInt64

Performs a bitwise left shift operation on an unsigned 64-bit integer.

Parameters:

  • self : The unsigned 64-bit integer to be shifted.
  • shift : The number of positions to shift the bits to the left. Must be between 0 and 63 inclusive, values outside this range will be masked with & 63.

Returns the result of shifting self left by shift positions. For each position shifted, the rightmost bit is filled with 0, and the leftmost bit is discarded.

Example:

  let x = 1UL
  inspect(x << 3, content="8") // 1 shifted left by 3 positions becomes 8
  inspect(x << 63, content="9223372036854775808") // 1 shifted left by 63 positions
<<
Int
bit_index
))
(x : UInt64, y : UInt64) -> Bool
!=
0UL
683} 684 685///| 位图容器添加元素 686fn
(bitmap : FixedArray[UInt64], value : UInt16) -> FixedArray[UInt64]

位图容器添加元素

bitmap_container_add
(
687
FixedArray[UInt64]
bitmap
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
],
688
UInt16
value
:
UInt16
UInt16
689) ->
type FixedArray[A]
FixedArray
[
UInt64
UInt64
] {
690 let
FixedArray[UInt64]
new_bitmap
=
type FixedArray[A]
FixedArray
::
(len : Int, init : UInt64) -> FixedArray[UInt64]

Creates a new fixed-size array with the specified length, initializing all elements with the given value.

Parameters:

  • length : The length of the array to create. Must be non-negative.
  • initial_value : The value used to initialize all elements in the array.

Returns a new fixed-size array of type FixedArray[T] with length elements, where each element is initialized to initial_value.

Throws a panic if length is negative.

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[0], content="42")
  inspect(arr.length(), content="3")

WARNING: A common pitfall is creating with the same initial value, for example:

  let two_dimension_array = FixedArray::make(10, FixedArray::make(10, 0))
  two_dimension_array[0][5] = 10
  assert_eq(two_dimension_array[5][5], 10)

This is because all the cells reference to the same object (the FixedArray[Int] in this case). One should use makei() instead which creates an object for each index.

make
(1024, 0UL)
691 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
1024;
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
692
FixedArray[UInt64]
new_bitmap
(FixedArray[UInt64], Int, UInt64) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
i] =
FixedArray[UInt64]
bitmap
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
i]
693 } 694 let
Int
word_index
=
UInt16
value
.
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
()
(self : Int, other : Int) -> Int

Performs integer division between two 32-bit integers. The result is truncated towards zero (rounds down for positive numbers and up for negative numbers).

Parameters:

  • dividend : The first integer operand to be divided.
  • divisor : The second integer operand that divides the dividend.

Returns the quotient of the division operation.

Throws a panic if divisor is zero.

Example:

  inspect(10 / 3, content="3") // truncates towards zero
  inspect(-10 / 3, content="-3")
  inspect(10 / -3, content="-3")
/
64
695 let
Int
bit_index
=
UInt16
value
.
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
()
(self : Int, other : Int) -> Int

Calculates the remainder of dividing one integer by another. The result follows the formula dividend - (dividend / divisor) * divisor, maintaining the same sign as the dividend.

Parameters:

  • self : The dividend (the number being divided).
  • other : The divisor (the number to divide by).

Returns the remainder of the division. If other is 0, the behavior is undefined.

Example:

  inspect(7 % 3, content="1")
  inspect(-7 % 3, content="-1")
  inspect(7 % -3, content="1")
%
64
696
FixedArray[UInt64]
new_bitmap
(FixedArray[UInt64], Int, UInt64) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
word_index] =
FixedArray[UInt64]
new_bitmap
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
word_index]
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise OR operation between two unsigned 64-bit integers.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns a new UInt64 value with bits set to 1 where either or both operands have bits set to 1, and 0 where both operands have bits set to 0.

Example:

  let a = 0xF0F0F0F0UL
  let b = 0x0F0F0F0FUL
  inspect(a | b, content="4294967295") // All bits set to 1
|
(1UL
(self : UInt64, shift : Int) -> UInt64

Performs a bitwise left shift operation on an unsigned 64-bit integer.

Parameters:

  • self : The unsigned 64-bit integer to be shifted.
  • shift : The number of positions to shift the bits to the left. Must be between 0 and 63 inclusive, values outside this range will be masked with & 63.

Returns the result of shifting self left by shift positions. For each position shifted, the rightmost bit is filled with 0, and the leftmost bit is discarded.

Example:

  let x = 1UL
  inspect(x << 3, content="8") // 1 shifted left by 3 positions becomes 8
  inspect(x << 63, content="9223372036854775808") // 1 shifted left by 63 positions
<<
Int
bit_index
)
697
FixedArray[UInt64]
new_bitmap
698} 699 700///| 位图容器移除元素 701fn
(bitmap : FixedArray[UInt64], value : UInt16) -> FixedArray[UInt64]

位图容器移除元素

bitmap_container_remove
(
702
FixedArray[UInt64]
bitmap
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
],
703
UInt16
value
:
UInt16
UInt16
704) ->
type FixedArray[A]
FixedArray
[
UInt64
UInt64
] {
705 let
FixedArray[UInt64]
new_bitmap
=
type FixedArray[A]
FixedArray
::
(len : Int, init : UInt64) -> FixedArray[UInt64]

Creates a new fixed-size array with the specified length, initializing all elements with the given value.

Parameters:

  • length : The length of the array to create. Must be non-negative.
  • initial_value : The value used to initialize all elements in the array.

Returns a new fixed-size array of type FixedArray[T] with length elements, where each element is initialized to initial_value.

Throws a panic if length is negative.

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[0], content="42")
  inspect(arr.length(), content="3")

WARNING: A common pitfall is creating with the same initial value, for example:

  let two_dimension_array = FixedArray::make(10, FixedArray::make(10, 0))
  two_dimension_array[0][5] = 10
  assert_eq(two_dimension_array[5][5], 10)

This is because all the cells reference to the same object (the FixedArray[Int] in this case). One should use makei() instead which creates an object for each index.

make
(1024, 0UL)
706 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
1024;
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
707
FixedArray[UInt64]
new_bitmap
(FixedArray[UInt64], Int, UInt64) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
i] =
FixedArray[UInt64]
bitmap
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
i]
708 } 709 let
Int
word_index
=
UInt16
value
.
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
()
(self : Int, other : Int) -> Int

Performs integer division between two 32-bit integers. The result is truncated towards zero (rounds down for positive numbers and up for negative numbers).

Parameters:

  • dividend : The first integer operand to be divided.
  • divisor : The second integer operand that divides the dividend.

Returns the quotient of the division operation.

Throws a panic if divisor is zero.

Example:

  inspect(10 / 3, content="3") // truncates towards zero
  inspect(-10 / 3, content="-3")
  inspect(10 / -3, content="-3")
/
64
710 let
Int
bit_index
=
UInt16
value
.
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
()
(self : Int, other : Int) -> Int

Calculates the remainder of dividing one integer by another. The result follows the formula dividend - (dividend / divisor) * divisor, maintaining the same sign as the dividend.

Parameters:

  • self : The dividend (the number being divided).
  • other : The divisor (the number to divide by).

Returns the remainder of the division. If other is 0, the behavior is undefined.

Example:

  inspect(7 % 3, content="1")
  inspect(-7 % 3, content="-1")
  inspect(7 % -3, content="1")
%
64
711 // Replace ~(1UL << bit_index) with (0xFFFFFFFFFFFFFFFFUL ^ (1UL << bit_index)) 712
FixedArray[UInt64]
new_bitmap
(FixedArray[UInt64], Int, UInt64) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
word_index] =
FixedArray[UInt64]
new_bitmap
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
word_index]
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise AND operation between two unsigned 64-bit integers.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns the result of performing a bitwise AND operation on the two operands.

Example:

  let a = 0xF0F0F0F0F0F0F0F0UL
  let b = 0xFF00FF00FF00FF00UL
  inspect(a & b, content="17294086455919964160") // 0xF000F000F000F000
&
713 (0xFFFFFFFFFFFFFFFFUL
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise XOR operation between two unsigned 64-bit integers. For each bit position, the result is 1 if the bits at that position in the two operands are different, and 0 if they are the same.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns a new UInt64 value representing the bitwise XOR of the two operands.

Example:

  let a = 0xF0F0F0F0UL
  let b = 0x0F0F0F0FUL
  inspect(a ^ b, content="4294967295") // 0xFFFFFFFF
^
(1UL
(self : UInt64, shift : Int) -> UInt64

Performs a bitwise left shift operation on an unsigned 64-bit integer.

Parameters:

  • self : The unsigned 64-bit integer to be shifted.
  • shift : The number of positions to shift the bits to the left. Must be between 0 and 63 inclusive, values outside this range will be masked with & 63.

Returns the result of shifting self left by shift positions. For each position shifted, the rightmost bit is filled with 0, and the leftmost bit is discarded.

Example:

  let x = 1UL
  inspect(x << 3, content="8") // 1 shifted left by 3 positions becomes 8
  inspect(x << 63, content="9223372036854775808") // 1 shifted left by 63 positions
<<
Int
bit_index
))
714
FixedArray[UInt64]
new_bitmap
715} 716 717///| 位图容器并集 718fn
(bitmap1 : FixedArray[UInt64], bitmap2 : FixedArray[UInt64]) -> FixedArray[UInt64]

位图容器并集

bitmap_container_union
(
719
FixedArray[UInt64]
bitmap1
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
],
720
FixedArray[UInt64]
bitmap2
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
]
721) ->
type FixedArray[A]
FixedArray
[
UInt64
UInt64
] {
722 let
FixedArray[UInt64]
result
=
type FixedArray[A]
FixedArray
::
(len : Int, init : UInt64) -> FixedArray[UInt64]

Creates a new fixed-size array with the specified length, initializing all elements with the given value.

Parameters:

  • length : The length of the array to create. Must be non-negative.
  • initial_value : The value used to initialize all elements in the array.

Returns a new fixed-size array of type FixedArray[T] with length elements, where each element is initialized to initial_value.

Throws a panic if length is negative.

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[0], content="42")
  inspect(arr.length(), content="3")

WARNING: A common pitfall is creating with the same initial value, for example:

  let two_dimension_array = FixedArray::make(10, FixedArray::make(10, 0))
  two_dimension_array[0][5] = 10
  assert_eq(two_dimension_array[5][5], 10)

This is because all the cells reference to the same object (the FixedArray[Int] in this case). One should use makei() instead which creates an object for each index.

make
(1024, 0UL)
723 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
1024;
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
724
FixedArray[UInt64]
result
(FixedArray[UInt64], Int, UInt64) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
i] =
FixedArray[UInt64]
bitmap1
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
i]
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise OR operation between two unsigned 64-bit integers.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns a new UInt64 value with bits set to 1 where either or both operands have bits set to 1, and 0 where both operands have bits set to 0.

Example:

  let a = 0xF0F0F0F0UL
  let b = 0x0F0F0F0FUL
  inspect(a | b, content="4294967295") // All bits set to 1
|
FixedArray[UInt64]
bitmap2
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
i]
725 } 726
FixedArray[UInt64]
result
727} 728 729///| 位图容器交集 730fn
(bitmap1 : FixedArray[UInt64], bitmap2 : FixedArray[UInt64]) -> FixedArray[UInt64]

位图容器交集

bitmap_container_intersect
(
731
FixedArray[UInt64]
bitmap1
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
],
732
FixedArray[UInt64]
bitmap2
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
]
733) ->
type FixedArray[A]
FixedArray
[
UInt64
UInt64
] {
734 let
FixedArray[UInt64]
result
=
type FixedArray[A]
FixedArray
::
(len : Int, init : UInt64) -> FixedArray[UInt64]

Creates a new fixed-size array with the specified length, initializing all elements with the given value.

Parameters:

  • length : The length of the array to create. Must be non-negative.
  • initial_value : The value used to initialize all elements in the array.

Returns a new fixed-size array of type FixedArray[T] with length elements, where each element is initialized to initial_value.

Throws a panic if length is negative.

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[0], content="42")
  inspect(arr.length(), content="3")

WARNING: A common pitfall is creating with the same initial value, for example:

  let two_dimension_array = FixedArray::make(10, FixedArray::make(10, 0))
  two_dimension_array[0][5] = 10
  assert_eq(two_dimension_array[5][5], 10)

This is because all the cells reference to the same object (the FixedArray[Int] in this case). One should use makei() instead which creates an object for each index.

make
(1024, 0UL)
735 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
1024;
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
736
FixedArray[UInt64]
result
(FixedArray[UInt64], Int, UInt64) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
i] =
FixedArray[UInt64]
bitmap1
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
i]
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise AND operation between two unsigned 64-bit integers.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns the result of performing a bitwise AND operation on the two operands.

Example:

  let a = 0xF0F0F0F0F0F0F0F0UL
  let b = 0xFF00FF00FF00FF00UL
  inspect(a & b, content="17294086455919964160") // 0xF000F000F000F000
&
FixedArray[UInt64]
bitmap2
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
i]
737 } 738
FixedArray[UInt64]
result
739} 740 741///| 位图容器差集 742fn
(bitmap1 : FixedArray[UInt64], bitmap2 : FixedArray[UInt64]) -> FixedArray[UInt64]

位图容器差集

bitmap_container_difference
(
743
FixedArray[UInt64]
bitmap1
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
],
744
FixedArray[UInt64]
bitmap2
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
]
745) ->
type FixedArray[A]
FixedArray
[
UInt64
UInt64
] {
746 let
FixedArray[UInt64]
result
=
type FixedArray[A]
FixedArray
::
(len : Int, init : UInt64) -> FixedArray[UInt64]

Creates a new fixed-size array with the specified length, initializing all elements with the given value.

Parameters:

  • length : The length of the array to create. Must be non-negative.
  • initial_value : The value used to initialize all elements in the array.

Returns a new fixed-size array of type FixedArray[T] with length elements, where each element is initialized to initial_value.

Throws a panic if length is negative.

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[0], content="42")
  inspect(arr.length(), content="3")

WARNING: A common pitfall is creating with the same initial value, for example:

  let two_dimension_array = FixedArray::make(10, FixedArray::make(10, 0))
  two_dimension_array[0][5] = 10
  assert_eq(two_dimension_array[5][5], 10)

This is because all the cells reference to the same object (the FixedArray[Int] in this case). One should use makei() instead which creates an object for each index.

make
(1024, 0UL)
747 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
1024;
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
748 // Replace ~bitmap2[i] with (0xFFFFFFFFFFFFFFFFUL ^ bitmap2[i]) 749
FixedArray[UInt64]
result
(FixedArray[UInt64], Int, UInt64) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
i] =
FixedArray[UInt64]
bitmap1
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
i]
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise AND operation between two unsigned 64-bit integers.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns the result of performing a bitwise AND operation on the two operands.

Example:

  let a = 0xF0F0F0F0F0F0F0F0UL
  let b = 0xFF00FF00FF00FF00UL
  inspect(a & b, content="17294086455919964160") // 0xF000F000F000F000
&
(0xFFFFFFFFFFFFFFFFUL
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise XOR operation between two unsigned 64-bit integers. For each bit position, the result is 1 if the bits at that position in the two operands are different, and 0 if they are the same.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns a new UInt64 value representing the bitwise XOR of the two operands.

Example:

  let a = 0xF0F0F0F0UL
  let b = 0x0F0F0F0FUL
  inspect(a ^ b, content="4294967295") // 0xFFFFFFFF
^
FixedArray[UInt64]
bitmap2
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
i])
750 } 751
FixedArray[UInt64]
result
752} 753 754///| 计算位图容器基数 755fn
(bitmap : FixedArray[UInt64]) -> Int

计算位图容器基数

bitmap_container_cardinality
(
FixedArray[UInt64]
bitmap
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
]) ->
Int
Int
{
756 let mut
Int
count
= 0
757 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
1024;
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
758
Int
count
=
Int
count
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
(value : UInt64) -> Int

计算64位整数的人口计数(置位数)

popcount
(
FixedArray[UInt64]
bitmap
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
i])
759 } 760
Int
count
761} 762 763///| 位图容器转数组 764fn
(bitmap : FixedArray[UInt64]) -> Array[UInt16]

位图容器转数组

bitmap_container_to_array
(
FixedArray[UInt64]
bitmap
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
]) ->
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
] {
765 let
Array[UInt16]
result
= []
766 for
Int
word_index
= 0;
Int
word_index
(self_ : Int, other : Int) -> Bool
<
1024;
Int
word_index
=
Int
word_index
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
767 let
UInt64
word
=
FixedArray[UInt64]
bitmap
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
word_index]
768 if
UInt64
word
(x : UInt64, y : UInt64) -> Bool
!=
0UL {
769 for
Int
bit_index
= 0;
Int
bit_index
(self_ : Int, other : Int) -> Bool
<
64;
Int
bit_index
=
Int
bit_index
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
770 if (
UInt64
word
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise AND operation between two unsigned 64-bit integers.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns the result of performing a bitwise AND operation on the two operands.

Example:

  let a = 0xF0F0F0F0F0F0F0F0UL
  let b = 0xFF00FF00FF00FF00UL
  inspect(a & b, content="17294086455919964160") // 0xF000F000F000F000
&
(1UL
(self : UInt64, shift : Int) -> UInt64

Performs a bitwise left shift operation on an unsigned 64-bit integer.

Parameters:

  • self : The unsigned 64-bit integer to be shifted.
  • shift : The number of positions to shift the bits to the left. Must be between 0 and 63 inclusive, values outside this range will be masked with & 63.

Returns the result of shifting self left by shift positions. For each position shifted, the rightmost bit is filled with 0, and the leftmost bit is discarded.

Example:

  let x = 1UL
  inspect(x << 3, content="8") // 1 shifted left by 3 positions becomes 8
  inspect(x << 63, content="9223372036854775808") // 1 shifted left by 63 positions
<<
Int
bit_index
))
(x : UInt64, y : UInt64) -> Bool
!=
0UL {
771 let
Int
value
=
Int
word_index
(self : Int, other : Int) -> Int

Multiplies two 32-bit integers. This is the implementation of the * operator for Int.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns the product of the two integers. If the result overflows the range of Int, it wraps around according to two's complement arithmetic.

Example:

  inspect(42 * 2, content="84")
  inspect(-10 * 3, content="-30")
  let max = 2147483647 // Int.max_value
  inspect(max * 2, content="-2") // Overflow wraps around
*
64
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
Int
bit_index
772
Array[UInt16]
result
.
(self : Array[UInt16], value : UInt16) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Int
value
.
(self : Int) -> UInt16

Converts a 32-bit signed integer to a 16-bit unsigned integer by truncating its value to fit within the range of 0 to 65535.

Parameters:

  • integer : The 32-bit signed integer to be converted. Values outside the range of UInt16 will be truncated to fit.

Returns a 16-bit unsigned integer containing the lower 16 bits of the input value.

Example:

  let n = 42
  inspect(n.to_uint16(), content="42")
  let neg = -1
  inspect(neg.to_uint16(), content="65535") // -1 becomes max value of UInt16
  let large = 65536
  inspect(large.to_uint16(), content="0") // Values wrap around
to_uint16
())
773 } 774 } 775 } 776 } 777
Array[UInt16]
result
778} 779 780// =============== 辅助函数 =============== 781 782///| 二进制搜索 783fn
(arr : Array[UInt16], value : UInt16) -> SearchResult

二进制搜索

binary_search
(
Array[UInt16]
arr
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
],
UInt16
value
:
UInt16
UInt16
) ->
enum SearchResult {
  Found(Int)
  NotFound(Int)
}

二进制搜索结果

SearchResult
{
784 let mut
Int
left
= 0
785 let mut
Int
right
=
Array[UInt16]
arr
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
786 while
Int
left
(self_ : Int, other : Int) -> Bool
<
Int
right
{
787 let
Int
mid
= (
Int
left
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
Int
right
)
(self : Int, other : Int) -> Int

Performs integer division between two 32-bit integers. The result is truncated towards zero (rounds down for positive numbers and up for negative numbers).

Parameters:

  • dividend : The first integer operand to be divided.
  • divisor : The second integer operand that divides the dividend.

Returns the quotient of the division operation.

Throws a panic if divisor is zero.

Example:

  inspect(10 / 3, content="3") // truncates towards zero
  inspect(-10 / 3, content="-3")
  inspect(10 / -3, content="-3")
/
2
788 let
UInt16
mid_val
=
Array[UInt16]
arr
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
mid]
789 if
UInt16
mid_val
(self : UInt16, that : UInt16) -> Bool
==
UInt16
value
{
790 return
(Int) -> SearchResult
Found
(
Int
mid
)
791 } else if
UInt16
mid_val
(self_ : UInt16, other : UInt16) -> Bool
<
UInt16
value
{
792
Int
left
=
Int
mid
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
793 } else { 794
Int
right
=
Int
mid
795 } 796 } 797
(Int) -> SearchResult
NotFound
(
Int
left
)
798} 799 800///| 二进制搜索包含检查 801fn
(arr : Array[UInt16], value : UInt16) -> Bool

二进制搜索包含检查

binary_search_contains
(
Array[UInt16]
arr
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
],
UInt16
value
:
UInt16
UInt16
) ->
Bool
Bool
{
802 match
(arr : Array[UInt16], value : UInt16) -> SearchResult

二进制搜索

binary_search
(
Array[UInt16]
arr
,
UInt16
value
) {
803
SearchResult
Found
(_) => true
804
SearchResult
NotFound
(_) => false
805 } 806} 807 808///| 在排序数组中插入元素 809fn
(arr : Array[UInt16], index : Int, value : UInt16) -> Array[UInt16]

在排序数组中插入元素

array_insert_sorted
(
810
Array[UInt16]
arr
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
],
811
Int
index
:
Int
Int
,
812
UInt16
value
:
UInt16
UInt16
813) ->
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
] {
814 let
Array[UInt16]
result
= []
815 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Int
index
;
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
816
Array[UInt16]
result
.
(self : Array[UInt16], value : UInt16) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Array[UInt16]
arr
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i])
817 } 818
Array[UInt16]
result
.
(self : Array[UInt16], value : UInt16) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
UInt16
value
)
819 for
Int
i
=
Int
index
;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
820
Array[UInt16]
result
.
(self : Array[UInt16], value : UInt16) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Array[UInt16]
arr
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i])
821 } 822
Array[UInt16]
result
823} 824 825///| 从数组移除指定位置的UInt16元素 826fn
(arr : Array[UInt16], index : Int) -> Array[UInt16]

从数组移除指定位置的UInt16元素

array_remove_at_uint16
(
Array[UInt16]
arr
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
],
Int
index
:
Int
Int
) ->
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
] {
827 let
Array[UInt16]
result
= []
828 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
829 if
Int
i
(x : Int, y : Int) -> Bool
!=
Int
index
{
830
Array[UInt16]
result
.
(self : Array[UInt16], value : UInt16) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Array[UInt16]
arr
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i])
831 } 832 } 833
Array[UInt16]
result
834} 835 836///| 计算64位整数的人口计数(置位数) 837fn
(value : UInt64) -> Int

计算64位整数的人口计数(置位数)

popcount
(
UInt64
value
:
UInt64
UInt64
) ->
Int
Int
{
838 let mut
Int
count
= 0
839 let mut
UInt64
v
=
UInt64
value
840 while
UInt64
v
(x : UInt64, y : UInt64) -> Bool
!=
0UL {
841
Int
count
=
Int
count
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
842
UInt64
v
=
UInt64
v
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise AND operation between two unsigned 64-bit integers.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns the result of performing a bitwise AND operation on the two operands.

Example:

  let a = 0xF0F0F0F0F0F0F0F0UL
  let b = 0xFF00FF00FF00FF00UL
  inspect(a & b, content="17294086455919964160") // 0xF000F000F000F000
&
(
UInt64
v
(self : UInt64, other : UInt64) -> UInt64

Performs subtraction between two unsigned 64-bit integers. When the result would be negative, the function wraps around using modular arithmetic (2^64).

Parameters:

  • self : The first unsigned 64-bit integer (minuend).
  • other : The second unsigned 64-bit integer to subtract from the first (subtrahend).

Returns the difference between the two numbers, wrapped around if necessary.

Example:

  let a = 5UL
  let b = 3UL
  inspect(a - b, content="2")
  let c = 3UL
  let d = 5UL
  inspect(c - d, content="18446744073709551614") // wraps around to 2^64 - 2
-
1UL) // 清除最低位的1
843 } 844
Int
count
845} 846 847// 暂时用简单实现替代复杂的混合操作 848///| 849fn
(container1 : Container, container2 : Container) -> Container
container_union_generic
(
850
Container
container1
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
,
851
Container
container2
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
852) ->
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
{
853 let
Array[UInt16]
arr1
=
(container : Container) -> Array[UInt16]

容器转换为数组

container_to_array
(
Container
container1
)
854 let
Array[UInt16]
arr2
=
(container : Container) -> Array[UInt16]

容器转换为数组

container_to_array
(
Container
container2
)
855 let
Array[UInt16]
union_arr
=
(arr1 : Array[UInt16], arr2 : Array[UInt16]) -> Array[UInt16]

数组容器并集

array_container_union
(
Array[UInt16]
arr1
,
Array[UInt16]
arr2
)
856 if
Array[UInt16]
union_arr
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self_ : Int, other : Int) -> Bool
>=
Int

容器配置常量

ARRAY_CONTAINER_MAX_SIZE
{
857 // 需要实现数组转位图容器的逻辑 858
(arr : Array[UInt16]) -> Container

数组容器转换为位图容器

array_to_bitmap_container
(
Array[UInt16]
union_arr
)
859 } else { 860
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
union_arr
)
861 } 862} 863 864///| 865fn
(container1 : Container, container2 : Container) -> Container
container_intersect_generic
(
866
Container
container1
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
,
867
Container
container2
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
868) ->
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
{
869 let
Array[UInt16]
arr1
=
(container : Container) -> Array[UInt16]

容器转换为数组

container_to_array
(
Container
container1
)
870 let
Array[UInt16]
arr2
=
(container : Container) -> Array[UInt16]

容器转换为数组

container_to_array
(
Container
container2
)
871
(Array[UInt16]) -> Container
ArrayContainer
(
(arr1 : Array[UInt16], arr2 : Array[UInt16]) -> Array[UInt16]

数组容器交集

array_container_intersect
(
Array[UInt16]
arr1
,
Array[UInt16]
arr2
))
872} 873 874///| 875fn
(container1 : Container, container2 : Container) -> Container
container_difference_generic
(
876
Container
container1
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
,
877
Container
container2
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
878) ->
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
{
879 let
Array[UInt16]
arr1
=
(container : Container) -> Array[UInt16]

容器转换为数组

container_to_array
(
Container
container1
)
880 let
Array[UInt16]
arr2
=
(container : Container) -> Array[UInt16]

容器转换为数组

container_to_array
(
Container
container2
)
881
(Array[UInt16]) -> Container
ArrayContainer
(
(arr1 : Array[UInt16], arr2 : Array[UInt16]) -> Array[UInt16]

数组容器差集

array_container_difference
(
Array[UInt16]
arr1
,
Array[UInt16]
arr2
))
882} 883 884// 简化的混合操作实现 885///| 886fn
(bitmap : FixedArray[UInt64], arr : Array[UInt16]) -> FixedArray[UInt64]
bitmap_container_union_with_array
(
887
FixedArray[UInt64]
bitmap
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
],
888
Array[UInt16]
arr
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
]
889) ->
type FixedArray[A]
FixedArray
[
UInt64
UInt64
] {
890 let
FixedArray[UInt64]
result
=
type FixedArray[A]
FixedArray
::
(len : Int, init : UInt64) -> FixedArray[UInt64]

Creates a new fixed-size array with the specified length, initializing all elements with the given value.

Parameters:

  • length : The length of the array to create. Must be non-negative.
  • initial_value : The value used to initialize all elements in the array.

Returns a new fixed-size array of type FixedArray[T] with length elements, where each element is initialized to initial_value.

Throws a panic if length is negative.

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[0], content="42")
  inspect(arr.length(), content="3")

WARNING: A common pitfall is creating with the same initial value, for example:

  let two_dimension_array = FixedArray::make(10, FixedArray::make(10, 0))
  two_dimension_array[0][5] = 10
  assert_eq(two_dimension_array[5][5], 10)

This is because all the cells reference to the same object (the FixedArray[Int] in this case). One should use makei() instead which creates an object for each index.

make
(1024, 0UL)
891 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
1024;
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
892
FixedArray[UInt64]
result
(FixedArray[UInt64], Int, UInt64) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
i] =
FixedArray[UInt64]
bitmap
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
i]
893 } 894 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
895 let
UInt16
value
=
Array[UInt16]
arr
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
896 let
Int
word_index
=
UInt16
value
.
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
()
(self : Int, other : Int) -> Int

Performs integer division between two 32-bit integers. The result is truncated towards zero (rounds down for positive numbers and up for negative numbers).

Parameters:

  • dividend : The first integer operand to be divided.
  • divisor : The second integer operand that divides the dividend.

Returns the quotient of the division operation.

Throws a panic if divisor is zero.

Example:

  inspect(10 / 3, content="3") // truncates towards zero
  inspect(-10 / 3, content="-3")
  inspect(10 / -3, content="-3")
/
64
897 let
Int
bit_index
=
UInt16
value
.
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
()
(self : Int, other : Int) -> Int

Calculates the remainder of dividing one integer by another. The result follows the formula dividend - (dividend / divisor) * divisor, maintaining the same sign as the dividend.

Parameters:

  • self : The dividend (the number being divided).
  • other : The divisor (the number to divide by).

Returns the remainder of the division. If other is 0, the behavior is undefined.

Example:

  inspect(7 % 3, content="1")
  inspect(-7 % 3, content="-1")
  inspect(7 % -3, content="1")
%
64
898
FixedArray[UInt64]
result
(FixedArray[UInt64], Int, UInt64) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
word_index] =
FixedArray[UInt64]
result
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
word_index]
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise OR operation between two unsigned 64-bit integers.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns a new UInt64 value with bits set to 1 where either or both operands have bits set to 1, and 0 where both operands have bits set to 0.

Example:

  let a = 0xF0F0F0F0UL
  let b = 0x0F0F0F0FUL
  inspect(a | b, content="4294967295") // All bits set to 1
|
(1UL
(self : UInt64, shift : Int) -> UInt64

Performs a bitwise left shift operation on an unsigned 64-bit integer.

Parameters:

  • self : The unsigned 64-bit integer to be shifted.
  • shift : The number of positions to shift the bits to the left. Must be between 0 and 63 inclusive, values outside this range will be masked with & 63.

Returns the result of shifting self left by shift positions. For each position shifted, the rightmost bit is filled with 0, and the leftmost bit is discarded.

Example:

  let x = 1UL
  inspect(x << 3, content="8") // 1 shifted left by 3 positions becomes 8
  inspect(x << 63, content="9223372036854775808") // 1 shifted left by 63 positions
<<
Int
bit_index
)
899 } 900
FixedArray[UInt64]
result
901} 902 903///| 904fn
(arr : Array[UInt16], bitmap : FixedArray[UInt64]) -> Array[UInt16]
array_container_intersect_with_bitmap
(
905
Array[UInt16]
arr
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
],
906
FixedArray[UInt64]
bitmap
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
]
907) ->
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
] {
908 let
Array[UInt16]
result
= []
909 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
910 let
UInt16
value
=
Array[UInt16]
arr
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
911 if
(bitmap : FixedArray[UInt64], value : UInt16) -> Bool

位图容器包含检查

bitmap_container_contains
(
FixedArray[UInt64]
bitmap
,
UInt16
value
) {
912
Array[UInt16]
result
.
(self : Array[UInt16], value : UInt16) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
UInt16
value
)
913 } 914 } 915
Array[UInt16]
result
916} 917 918///| 919fn
(arr : Array[UInt16], bitmap : FixedArray[UInt64]) -> Array[UInt16]
array_container_difference_with_bitmap
(
920
Array[UInt16]
arr
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
],
921
FixedArray[UInt64]
bitmap
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
]
922) ->
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
] {
923 let
Array[UInt16]
result
= []
924 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
925 let
UInt16
value
=
Array[UInt16]
arr
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
926 if
(x : Bool) -> Bool

Performs logical negation on a boolean value.

Parameters:

  • value : The boolean value to negate.

Returns the logical NOT of the input value: true if the input is false, and false if the input is true.

Example:

  inspect(not(true), content="false")
  inspect(not(false), content="true")
not
(
(bitmap : FixedArray[UInt64], value : UInt16) -> Bool

位图容器包含检查

bitmap_container_contains
(
FixedArray[UInt64]
bitmap
,
UInt16
value
)) {
927
Array[UInt16]
result
.
(self : Array[UInt16], value : UInt16) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
UInt16
value
)
928 } 929 } 930
Array[UInt16]
result
931} 932 933///| 934fn
(bitmap : FixedArray[UInt64], arr : Array[UInt16]) -> FixedArray[UInt64]
bitmap_container_difference_with_array
(
935
FixedArray[UInt64]
bitmap
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
],
936
Array[UInt16]
arr
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
]
937) ->
type FixedArray[A]
FixedArray
[
UInt64
UInt64
] {
938 let
FixedArray[UInt64]
result
=
type FixedArray[A]
FixedArray
::
(len : Int, init : UInt64) -> FixedArray[UInt64]

Creates a new fixed-size array with the specified length, initializing all elements with the given value.

Parameters:

  • length : The length of the array to create. Must be non-negative.
  • initial_value : The value used to initialize all elements in the array.

Returns a new fixed-size array of type FixedArray[T] with length elements, where each element is initialized to initial_value.

Throws a panic if length is negative.

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[0], content="42")
  inspect(arr.length(), content="3")

WARNING: A common pitfall is creating with the same initial value, for example:

  let two_dimension_array = FixedArray::make(10, FixedArray::make(10, 0))
  two_dimension_array[0][5] = 10
  assert_eq(two_dimension_array[5][5], 10)

This is because all the cells reference to the same object (the FixedArray[Int] in this case). One should use makei() instead which creates an object for each index.

make
(1024, 0UL)
939 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
1024;
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
940
FixedArray[UInt64]
result
(FixedArray[UInt64], Int, UInt64) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
i] =
FixedArray[UInt64]
bitmap
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
i]
941 } 942 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
943 let
UInt16
value
=
Array[UInt16]
arr
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
944 let
Int
word_index
=
UInt16
value
.
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
()
(self : Int, other : Int) -> Int

Performs integer division between two 32-bit integers. The result is truncated towards zero (rounds down for positive numbers and up for negative numbers).

Parameters:

  • dividend : The first integer operand to be divided.
  • divisor : The second integer operand that divides the dividend.

Returns the quotient of the division operation.

Throws a panic if divisor is zero.

Example:

  inspect(10 / 3, content="3") // truncates towards zero
  inspect(-10 / 3, content="-3")
  inspect(10 / -3, content="-3")
/
64
945 let
Int
bit_index
=
UInt16
value
.
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
()
(self : Int, other : Int) -> Int

Calculates the remainder of dividing one integer by another. The result follows the formula dividend - (dividend / divisor) * divisor, maintaining the same sign as the dividend.

Parameters:

  • self : The dividend (the number being divided).
  • other : The divisor (the number to divide by).

Returns the remainder of the division. If other is 0, the behavior is undefined.

Example:

  inspect(7 % 3, content="1")
  inspect(-7 % 3, content="-1")
  inspect(7 % -3, content="1")
%
64
946 // Replace ~(1UL << bit_index) with (0xFFFFFFFFFFFFFFFFUL ^ (1UL << bit_index)) 947
FixedArray[UInt64]
result
(FixedArray[UInt64], Int, UInt64) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
word_index] =
FixedArray[UInt64]
result
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
word_index]
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise AND operation between two unsigned 64-bit integers.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns the result of performing a bitwise AND operation on the two operands.

Example:

  let a = 0xF0F0F0F0F0F0F0F0UL
  let b = 0xFF00FF00FF00FF00UL
  inspect(a & b, content="17294086455919964160") // 0xF000F000F000F000
&
948 (0xFFFFFFFFFFFFFFFFUL
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise XOR operation between two unsigned 64-bit integers. For each bit position, the result is 1 if the bits at that position in the two operands are different, and 0 if they are the same.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns a new UInt64 value representing the bitwise XOR of the two operands.

Example:

  let a = 0xF0F0F0F0UL
  let b = 0x0F0F0F0FUL
  inspect(a ^ b, content="4294967295") // 0xFFFFFFFF
^
(1UL
(self : UInt64, shift : Int) -> UInt64

Performs a bitwise left shift operation on an unsigned 64-bit integer.

Parameters:

  • self : The unsigned 64-bit integer to be shifted.
  • shift : The number of positions to shift the bits to the left. Must be between 0 and 63 inclusive, values outside this range will be masked with & 63.

Returns the result of shifting self left by shift positions. For each position shifted, the rightmost bit is filled with 0, and the leftmost bit is discarded.

Example:

  let x = 1UL
  inspect(x << 3, content="8") // 1 shifted left by 3 positions becomes 8
  inspect(x << 63, content="9223372036854775808") // 1 shifted left by 63 positions
<<
Int
bit_index
))
949 } 950
FixedArray[UInt64]
result
951} 952 953// 运行容器的简化实现(占位符) 954///| 955fn
(runs : Array[Run], value : UInt16) -> Bool
run_container_contains
(
Array[Run]
runs
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
],
UInt16
value
:
UInt16
UInt16
) ->
Bool
Bool
{
956 // 简化实现:遍历所有运行段 957 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[Run]
runs
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
958 let
Run
run
=
Array[Run]
runs
(Array[Run], Int) -> Run

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
959 if
UInt16
value
(self_ : UInt16, other : UInt16) -> Bool
>=
Run
run
.
UInt16
start
(Bool, Bool) -> Bool
&&
UInt16
value
(self_ : UInt16, other : UInt16) -> Bool
<=
Run
run
.
UInt16
start
(self : UInt16, that : UInt16) -> UInt16
+
Run
run
.
UInt16
length
{
960 return true 961 } 962 } 963 false 964} 965 966///| 967fn
(runs : Array[Run]) -> Int
run_container_cardinality
(
Array[Run]
runs
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
]) ->
Int
Int
{
968 let mut
Int
count
= 0
969 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[Run]
runs
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
970
Int
count
=
Int
count
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
Array[Run]
runs
(Array[Run], Int) -> Run

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i].
UInt16
length
.
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
()
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
971 } 972
Int
count
973} 974 975///| 976fn
(runs : Array[Run]) -> Array[UInt16]
run_container_to_array
(
Array[Run]
runs
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
]) ->
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
] {
977 let
Array[UInt16]
result
= []
978 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[Run]
runs
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
979 let
Run
run
=
Array[Run]
runs
(Array[Run], Int) -> Run

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
980 for
Int
j
= 0;
Int
j
(self_ : Int, other : Int) -> Bool
<=
Run
run
.
UInt16
length
.
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
();
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
981
Array[UInt16]
result
.
(self : Array[UInt16], value : UInt16) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Run
run
.
UInt16
start
(self : UInt16, that : UInt16) -> UInt16
+
Int
j
.
(self : Int) -> UInt16

Converts a 32-bit signed integer to a 16-bit unsigned integer by truncating its value to fit within the range of 0 to 65535.

Parameters:

  • integer : The 32-bit signed integer to be converted. Values outside the range of UInt16 will be truncated to fit.

Returns a 16-bit unsigned integer containing the lower 16 bits of the input value.

Example:

  let n = 42
  inspect(n.to_uint16(), content="42")
  let neg = -1
  inspect(neg.to_uint16(), content="65535") // -1 becomes max value of UInt16
  let large = 65536
  inspect(large.to_uint16(), content="0") // Values wrap around
to_uint16
())
982 } 983 } 984
Array[UInt16]
result
985} 986// 添加到 RoaringBitmap.mbt 文件末尾 987 988// =============== 容器转换函数 =============== 989 990///| 数组容器转换为位图容器 991fn
(arr : Array[UInt16]) -> Container

数组容器转换为位图容器

array_to_bitmap_container
(
Array[UInt16]
arr
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
]) ->
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
{
992 let
FixedArray[UInt64]
bitmap
=
type FixedArray[A]
FixedArray
::
(len : Int, init : UInt64) -> FixedArray[UInt64]

Creates a new fixed-size array with the specified length, initializing all elements with the given value.

Parameters:

  • length : The length of the array to create. Must be non-negative.
  • initial_value : The value used to initialize all elements in the array.

Returns a new fixed-size array of type FixedArray[T] with length elements, where each element is initialized to initial_value.

Throws a panic if length is negative.

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[0], content="42")
  inspect(arr.length(), content="3")

WARNING: A common pitfall is creating with the same initial value, for example:

  let two_dimension_array = FixedArray::make(10, FixedArray::make(10, 0))
  two_dimension_array[0][5] = 10
  assert_eq(two_dimension_array[5][5], 10)

This is because all the cells reference to the same object (the FixedArray[Int] in this case). One should use makei() instead which creates an object for each index.

make
(1024, 0UL)
993 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
994 let
UInt16
value
=
Array[UInt16]
arr
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
995 let
Int
word_index
=
UInt16
value
.
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
()
(self : Int, other : Int) -> Int

Performs integer division between two 32-bit integers. The result is truncated towards zero (rounds down for positive numbers and up for negative numbers).

Parameters:

  • dividend : The first integer operand to be divided.
  • divisor : The second integer operand that divides the dividend.

Returns the quotient of the division operation.

Throws a panic if divisor is zero.

Example:

  inspect(10 / 3, content="3") // truncates towards zero
  inspect(-10 / 3, content="-3")
  inspect(10 / -3, content="-3")
/
64
996 let
Int
bit_index
=
UInt16
value
.
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
()
(self : Int, other : Int) -> Int

Calculates the remainder of dividing one integer by another. The result follows the formula dividend - (dividend / divisor) * divisor, maintaining the same sign as the dividend.

Parameters:

  • self : The dividend (the number being divided).
  • other : The divisor (the number to divide by).

Returns the remainder of the division. If other is 0, the behavior is undefined.

Example:

  inspect(7 % 3, content="1")
  inspect(-7 % 3, content="-1")
  inspect(7 % -3, content="1")
%
64
997
FixedArray[UInt64]
bitmap
(FixedArray[UInt64], Int, UInt64) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
word_index] =
FixedArray[UInt64]
bitmap
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
word_index]
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise OR operation between two unsigned 64-bit integers.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns a new UInt64 value with bits set to 1 where either or both operands have bits set to 1, and 0 where both operands have bits set to 0.

Example:

  let a = 0xF0F0F0F0UL
  let b = 0x0F0F0F0FUL
  inspect(a | b, content="4294967295") // All bits set to 1
|
(1UL
(self : UInt64, shift : Int) -> UInt64

Performs a bitwise left shift operation on an unsigned 64-bit integer.

Parameters:

  • self : The unsigned 64-bit integer to be shifted.
  • shift : The number of positions to shift the bits to the left. Must be between 0 and 63 inclusive, values outside this range will be masked with & 63.

Returns the result of shifting self left by shift positions. For each position shifted, the rightmost bit is filled with 0, and the leftmost bit is discarded.

Example:

  let x = 1UL
  inspect(x << 3, content="8") // 1 shifted left by 3 positions becomes 8
  inspect(x << 63, content="9223372036854775808") // 1 shifted left by 63 positions
<<
Int
bit_index
)
998 } 999
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap
)
1000} 1001 1002///| 位图容器转换为数组容器 1003fn
(bitmap : FixedArray[UInt64]) -> Container

位图容器转换为数组容器

bitmap_to_array_container
(
FixedArray[UInt64]
bitmap
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
]) ->
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
{
1004
(Array[UInt16]) -> Container
ArrayContainer
(
(bitmap : FixedArray[UInt64]) -> Array[UInt16]

位图容器转数组

bitmap_container_to_array
(
FixedArray[UInt64]
bitmap
))
1005} 1006 1007///| 数组容器转换为运行长度编码容器(简化实现) 1008fn
(arr : Array[UInt16]) -> Container

数组容器转换为运行长度编码容器(简化实现)

array_to_run_container
(
Array[UInt16]
arr
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
]) ->
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
{
1009 if
Array[UInt16]
arr
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
0 {
1010 return
(Array[Run]) -> Container
RunContainer
([])
1011 } 1012 let
Array[Run]
runs
= []
1013 let mut
UInt16
current_start
=
Array[UInt16]
arr
[0]
1014 let mut
Int
current_length
= 0
1015 for
Int
i
= 1;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1016 let
UInt16
current_val
=
Array[UInt16]
arr
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
1017 let
UInt16
prev_val
=
Array[UInt16]
arr
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
-
1]
1018 if
UInt16
current_val
(self : UInt16, that : UInt16) -> Bool
==
UInt16
prev_val
(self : UInt16, that : UInt16) -> UInt16
+
1 {
1019 // 连续,增加当前运行长度 1020
Int
current_length
=
Int
current_length
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
1021 } else { 1022 // 不连续,结束当前运行,开始新运行 1023 let
Run
run
=
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
::{
1024
UInt16
start
:
UInt16
current_start
,
1025
UInt16
length
:
Int
current_length
.
(self : Int) -> UInt16

Converts a 32-bit signed integer to a 16-bit unsigned integer by truncating its value to fit within the range of 0 to 65535.

Parameters:

  • integer : The 32-bit signed integer to be converted. Values outside the range of UInt16 will be truncated to fit.

Returns a 16-bit unsigned integer containing the lower 16 bits of the input value.

Example:

  let n = 42
  inspect(n.to_uint16(), content="42")
  let neg = -1
  inspect(neg.to_uint16(), content="65535") // -1 becomes max value of UInt16
  let large = 65536
  inspect(large.to_uint16(), content="0") // Values wrap around
to_uint16
(),
1026 } 1027
Array[Run]
runs
.
(self : Array[Run], value : Run) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Run
run
)
1028
UInt16
current_start
=
UInt16
current_val
1029
Int
current_length
= 0
1030 } 1031 } 1032 1033 // 添加最后一个运行 1034 let
Run
last_run
=
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
::{
1035
UInt16
start
:
UInt16
current_start
,
1036
UInt16
length
:
Int
current_length
.
(self : Int) -> UInt16

Converts a 32-bit signed integer to a 16-bit unsigned integer by truncating its value to fit within the range of 0 to 65535.

Parameters:

  • integer : The 32-bit signed integer to be converted. Values outside the range of UInt16 will be truncated to fit.

Returns a 16-bit unsigned integer containing the lower 16 bits of the input value.

Example:

  let n = 42
  inspect(n.to_uint16(), content="42")
  let neg = -1
  inspect(neg.to_uint16(), content="65535") // -1 becomes max value of UInt16
  let large = 65536
  inspect(large.to_uint16(), content="0") // Values wrap around
to_uint16
(),
1037 } 1038
Array[Run]
runs
.
(self : Array[Run], value : Run) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Run
last_run
)
1039
(Array[Run]) -> Container
RunContainer
(
Array[Run]
runs
)
1040} 1041 1042///| 运行容器转换为数组容器 1043fn
(runs : Array[Run]) -> Container

运行容器转换为数组容器

run_to_array_container
(
Array[Run]
runs
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
]) ->
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
{
1044
(Array[UInt16]) -> Container
ArrayContainer
(
(runs : Array[Run]) -> Array[UInt16]
run_container_to_array
(
Array[Run]
runs
))
1045} 1046 1047///| 位图容器转换为运行长度编码容器(简化实现) 1048fn
(bitmap : FixedArray[UInt64]) -> Container

位图容器转换为运行长度编码容器(简化实现)

bitmap_to_run_container
(
FixedArray[UInt64]
bitmap
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
]) ->
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
{
1049 let
Array[UInt16]
arr
=
(bitmap : FixedArray[UInt64]) -> Array[UInt16]

位图容器转数组

bitmap_container_to_array
(
FixedArray[UInt64]
bitmap
)
1050
(arr : Array[UInt16]) -> Container

数组容器转换为运行长度编码容器(简化实现)

array_to_run_container
(
Array[UInt16]
arr
)
1051} 1052 1053///| 运行容器转换为位图容器 1054fn
(runs : Array[Run]) -> Container

运行容器转换为位图容器

run_to_bitmap_container
(
Array[Run]
runs
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
]) ->
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
{
1055 let
Array[UInt16]
arr
=
(runs : Array[Run]) -> Array[UInt16]
run_container_to_array
(
Array[Run]
runs
)
1056
(arr : Array[UInt16]) -> Container

数组容器转换为位图容器

array_to_bitmap_container
(
Array[UInt16]
arr
)
1057} 1058 1059///| 自动优化容器类型 1060pub fn
(container : Container) -> Container

自动优化容器类型

optimize_container
(
Container
container
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
) ->
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
{
1061 match
Container
container
{
1062
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr
) => {
1063 let
Int
size
=
Array[UInt16]
arr
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
1064 if
Int
size
(self_ : Int, other : Int) -> Bool
>=
Int

容器配置常量

ARRAY_CONTAINER_MAX_SIZE
{
1065 // 检查是否适合运行长度编码 1066 if
(arr : Array[UInt16]) -> Bool

判断是否应该使用运行长度编码容器

should_use_run_container
(
Array[UInt16]
arr
) {
1067
(arr : Array[UInt16]) -> Container

数组容器转换为运行长度编码容器(简化实现)

array_to_run_container
(
Array[UInt16]
arr
)
1068 } else { 1069
(arr : Array[UInt16]) -> Container

数组容器转换为位图容器

array_to_bitmap_container
(
Array[UInt16]
arr
)
1070 } 1071 } else { 1072
Container
container
1073 } 1074 } 1075
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap
) => {
1076 let
Int
cardinality
=
(bitmap : FixedArray[UInt64]) -> Int

计算位图容器基数

bitmap_container_cardinality
(
FixedArray[UInt64]
bitmap
)
1077 if
Int
cardinality
(self_ : Int, other : Int) -> Bool
<
Int

容器配置常量

ARRAY_CONTAINER_MAX_SIZE
{
1078
(bitmap : FixedArray[UInt64]) -> Container

位图容器转换为数组容器

bitmap_to_array_container
(
FixedArray[UInt64]
bitmap
)
1079 } else { 1080 // 检查是否适合运行长度编码 1081 let
Array[UInt16]
arr
=
(bitmap : FixedArray[UInt64]) -> Array[UInt16]

位图容器转数组

bitmap_container_to_array
(
FixedArray[UInt64]
bitmap
)
1082 if
(arr : Array[UInt16]) -> Bool

判断是否应该使用运行长度编码容器

should_use_run_container
(
Array[UInt16]
arr
) {
1083
(bitmap : FixedArray[UInt64]) -> Container

位图容器转换为运行长度编码容器(简化实现)

bitmap_to_run_container
(
FixedArray[UInt64]
bitmap
)
1084 } else { 1085
Container
container
1086 } 1087 } 1088 } 1089
(Array[Run]) -> Container
RunContainer
(
Array[Run]
runs
) => {
1090 let
Int
cardinality
=
(runs : Array[Run]) -> Int
run_container_cardinality
(
Array[Run]
runs
)
1091 if
Int
cardinality
(self_ : Int, other : Int) -> Bool
<
Int

容器配置常量

ARRAY_CONTAINER_MAX_SIZE
{
1092
(runs : Array[Run]) -> Container

运行容器转换为数组容器

run_to_array_container
(
Array[Run]
runs
)
1093 } else if
Int
cardinality
(self_ : Int, other : Int) -> Bool
>=
Int

容器配置常量

ARRAY_CONTAINER_MAX_SIZE
(Bool, Bool) -> Bool
&&
1094
(runs : Array[Run]) -> Bool

判断运行长度编码容器是否高效

is_efficient_run_container
(
Array[Run]
runs
)
(self : Bool, other : Bool) -> Bool

Compares two boolean values for equality.

Parameters:

  • self : The first boolean value to compare.
  • other : The second boolean value to compare.

Returns true if both boolean values are equal (either both true or both false), false otherwise.

Example:

  inspect(true == true, content="true")
  inspect(false == true, content="false")
  inspect(true == false, content="false")
  inspect(false == false, content="true")
==
false {
1095
(runs : Array[Run]) -> Container

运行容器转换为位图容器

run_to_bitmap_container
(
Array[Run]
runs
)
1096 } else { 1097
Container
container
1098 } 1099 } 1100 } 1101} 1102 1103///| 判断是否应该使用运行长度编码容器 1104fn
(arr : Array[UInt16]) -> Bool

判断是否应该使用运行长度编码容器

should_use_run_container
(
Array[UInt16]
arr
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
]) ->
Bool
Bool
{
1105 if
Array[UInt16]
arr
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self_ : Int, other : Int) -> Bool
<
2 {
1106 return false 1107 } 1108 let mut
Int
run_count
= 1
1109 let mut
Int
consecutive_count
= 0
1110 for
Int
i
= 1;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1111 if
Array[UInt16]
arr
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
(self : UInt16, that : UInt16) -> Bool
==
Array[UInt16]
arr
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
-
1]
(self : UInt16, that : UInt16) -> UInt16
+
1 {
1112
Int
consecutive_count
=
Int
consecutive_count
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
1113 } else { 1114 if
Int
consecutive_count
(self_ : Int, other : Int) -> Bool
>
0 {
1115
Int
run_count
=
Int
run_count
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
1116 } 1117
Int
consecutive_count
= 0
1118 } 1119 } 1120 1121 // 如果运行数量少于数组长度的一半,说明有很多连续段,适合RLE 1122
Int
run_count
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self : Int, other : Int) -> Int

Performs integer division between two 32-bit integers. The result is truncated towards zero (rounds down for positive numbers and up for negative numbers).

Parameters:

  • dividend : The first integer operand to be divided.
  • divisor : The second integer operand that divides the dividend.

Returns the quotient of the division operation.

Throws a panic if divisor is zero.

Example:

  inspect(10 / 3, content="3") // truncates towards zero
  inspect(-10 / 3, content="-3")
  inspect(10 / -3, content="-3")
/
2
1123} 1124 1125///| 判断运行长度编码容器是否高效 1126fn
(runs : Array[Run]) -> Bool

判断运行长度编码容器是否高效

is_efficient_run_container
(
Array[Run]
runs
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
]) ->
Bool
Bool
{
1127 if
Array[Run]
runs
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
0 {
1128 return false 1129 } 1130 let
Int
total_elements
=
(runs : Array[Run]) -> Int
run_container_cardinality
(
Array[Run]
runs
)
1131 let
Int
storage_cost
=
Array[Run]
runs
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self : Int, other : Int) -> Int

Multiplies two 32-bit integers. This is the implementation of the * operator for Int.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns the product of the two integers. If the result overflows the range of Int, it wraps around according to two's complement arithmetic.

Example:

  inspect(42 * 2, content="84")
  inspect(-10 * 3, content="-30")
  let max = 2147483647 // Int.max_value
  inspect(max * 2, content="-2") // Overflow wraps around
*
4 // 每个Run占4字节
1132 let
Int
array_cost
=
Int
total_elements
(self : Int, other : Int) -> Int

Multiplies two 32-bit integers. This is the implementation of the * operator for Int.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns the product of the two integers. If the result overflows the range of Int, it wraps around according to two's complement arithmetic.

Example:

  inspect(42 * 2, content="84")
  inspect(-10 * 3, content="-30")
  let max = 2147483647 // Int.max_value
  inspect(max * 2, content="-2") // Overflow wraps around
*
2 // 数组容器每个元素占2字节
1133 1134 // 如果RLE存储成本低于数组存储成本,则认为高效 1135
Int
storage_cost
(self_ : Int, other : Int) -> Bool
<
Int
array_cost
1136} 1137 1138///| 运行容器交集操作 1139fn
(runs1 : Array[Run], runs2 : Array[Run]) -> Array[Run]

运行容器交集操作

run_container_intersect
(
1140
Array[Run]
runs1
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
],
1141
Array[Run]
runs2
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
]
1142) ->
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
] {
1143 if
Array[Run]
runs1
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
0
(Bool, Bool) -> Bool
||
Array[Run]
runs2
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
0 {
1144 return [] 1145 } 1146 let
Array[Run]
result
= []
1147 let mut
Int
i
= 0
1148 let mut
Int
j
= 0
1149 while
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[Run]
runs1
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(Bool, Bool) -> Bool
&&
Int
j
(self_ : Int, other : Int) -> Bool
<
Array[Run]
runs2
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
1150 let
Run
run1
=
Array[Run]
runs1
(Array[Run], Int) -> Run

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
1151 let
Run
run2
=
Array[Run]
runs2
(Array[Run], Int) -> Run

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j]
1152 1153 // 计算每个运行段的结束位置 1154 let
UInt16
end1
=
Run
run1
.
UInt16
start
(self : UInt16, that : UInt16) -> UInt16
+
Run
run1
.
UInt16
length
1155 let
UInt16
end2
=
Run
run2
.
UInt16
start
(self : UInt16, that : UInt16) -> UInt16
+
Run
run2
.
UInt16
length
1156 1157 // 找到重叠区间 1158 let
UInt16
overlap_start
= if
Run
run1
.
UInt16
start
(self_ : UInt16, other : UInt16) -> Bool
>
Run
run2
.
UInt16
start
{
1159
Run
run1
.
UInt16
start
1160 } else { 1161
Run
run2
.
UInt16
start
1162 } 1163 let
UInt16
overlap_end
= if
UInt16
end1
(self_ : UInt16, other : UInt16) -> Bool
<
UInt16
end2
{
UInt16
end1
} else {
UInt16
end2
}
1164 if
UInt16
overlap_start
(self_ : UInt16, other : UInt16) -> Bool
<=
UInt16
overlap_end
{
1165 // 有重叠,创建交集运行段 1166 let
Run
intersect_run
=
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
::{
1167
UInt16
start
:
UInt16
overlap_start
,
1168
UInt16
length
:
UInt16
overlap_end
(self : UInt16, that : UInt16) -> UInt16
-
UInt16
overlap_start
,
1169 } 1170
Array[Run]
result
.
(self : Array[Run], value : Run) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Run
intersect_run
)
1171 } 1172 1173 // 移动指针:结束位置较小的运行段先移动 1174 if
UInt16
end1
(self_ : UInt16, other : UInt16) -> Bool
<=
UInt16
end2
{
1175
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
1176 } 1177 if
UInt16
end2
(self_ : UInt16, other : UInt16) -> Bool
<=
UInt16
end1
{
1178
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
1179 } 1180 } 1181
Array[Run]
result
1182} 1183 1184///| 运行容器差集操作 (runs1 - runs2) 1185fn
(runs1 : Array[Run], runs2 : Array[Run]) -> Array[Run]

运行容器差集操作 (runs1 - runs2)

run_container_difference
(
1186
Array[Run]
runs1
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
],
1187
Array[Run]
runs2
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
]
1188) ->
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
] {
1189 if
Array[Run]
runs1
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
0 {
1190 return [] 1191 } 1192 if
Array[Run]
runs2
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
0 {
1193 return
Array[Run]
runs1
1194 } 1195 let
Array[Run]
result
= []
1196 let mut
Int
i
= 0
1197 let mut
Int
j
= 0
1198 while
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[Run]
runs1
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
1199 let mut
Run
current_run
=
Array[Run]
runs1
(Array[Run], Int) -> Run

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
1200 let mut
UInt16
current_end
=
Run
current_run
.
UInt16
start
(self : UInt16, that : UInt16) -> UInt16
+
Run
current_run
.
UInt16
length
1201 let mut
Bool
processed
= false
1202 1203 // 检查当前运行段与所有 runs2 中的运行段的关系 1204 while
Int
j
(self_ : Int, other : Int) -> Bool
<
Array[Run]
runs2
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(Bool, Bool) -> Bool
&&
(x : Bool) -> Bool

Performs logical negation on a boolean value.

Parameters:

  • value : The boolean value to negate.

Returns the logical NOT of the input value: true if the input is false, and false if the input is true.

Example:

  inspect(not(true), content="false")
  inspect(not(false), content="true")
not
(
Bool
processed
) {
1205 let
Run
run2
=
Array[Run]
runs2
(Array[Run], Int) -> Run

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j]
1206 let
UInt16
end2
=
Run
run2
.
UInt16
start
(self : UInt16, that : UInt16) -> UInt16
+
Run
run2
.
UInt16
length
1207 if
Run
run2
.
UInt16
start
(self_ : UInt16, other : UInt16) -> Bool
>
UInt16
current_end
{
1208 // run2 完全在当前运行段右边,当前运行段完全保留 1209
Array[Run]
result
.
(self : Array[Run], value : Run) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Run
current_run
)
1210
Bool
processed
= true
1211 } else if
UInt16
end2
(self_ : UInt16, other : UInt16) -> Bool
<
Run
current_run
.
UInt16
start
{
1212 // run2 完全在当前运行段左边,移动到下一个 run2 1213
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
1214 } else { 1215 // 有重叠,需要分割当前运行段 1216 1217 // 如果当前运行段的开始部分不重叠,保留这部分 1218 if
Run
current_run
.
UInt16
start
(self_ : UInt16, other : UInt16) -> Bool
<
Run
run2
.
UInt16
start
{
1219 let
Run
left_part
=
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
::{
1220
UInt16
start
:
Run
current_run
.
UInt16
start
,
1221
UInt16
length
:
Run
run2
.
UInt16
start
(self : UInt16, that : UInt16) -> UInt16
-
Run
current_run
.
UInt16
start
(self : UInt16, that : UInt16) -> UInt16
-
1,
1222 } 1223
Array[Run]
result
.
(self : Array[Run], value : Run) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Run
left_part
)
1224 } 1225 1226 // 如果当前运行段的结束部分不重叠,更新当前运行段为右边部分 1227 if
UInt16
current_end
(self_ : UInt16, other : UInt16) -> Bool
>
UInt16
end2
{
1228
Run
current_run
=
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
::{
UInt16
start
:
UInt16
end2
(self : UInt16, that : UInt16) -> UInt16
+
1,
UInt16
length
:
UInt16
current_end
(self : UInt16, that : UInt16) -> UInt16
-
UInt16
end2
(self : UInt16, that : UInt16) -> UInt16
-
1 }
1229
UInt16
current_end
=
Run
current_run
.
UInt16
start
(self : UInt16, that : UInt16) -> UInt16
+
Run
current_run
.
UInt16
length
1230
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
1231 } else { 1232 // 当前运行段完全被覆盖或结束 1233
Bool
processed
= true
1234 if
UInt16
end2
(self_ : UInt16, other : UInt16) -> Bool
>=
UInt16
current_end
{
1235
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
1236 } 1237 } 1238 } 1239 } 1240 1241 // 如果当前运行段还有剩余且没有被处理,添加到结果 1242 if
(x : Bool) -> Bool

Performs logical negation on a boolean value.

Parameters:

  • value : The boolean value to negate.

Returns the logical NOT of the input value: true if the input is false, and false if the input is true.

Example:

  inspect(not(true), content="false")
  inspect(not(false), content="true")
not
(
Bool
processed
)
(Bool, Bool) -> Bool
&&
Int
j
(self_ : Int, other : Int) -> Bool
>=
Array[Run]
runs2
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
1243
Array[Run]
result
.
(self : Array[Run], value : Run) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Run
current_run
)
1244 } 1245
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
1246 // 重置 j 为下一个可能相关的运行段 1247
Int
j
=
(runs : Array[Run], position : UInt16) -> Int

找到下一个可能与给定位置相关的运行段索引

find_next_relevant_run
(
Array[Run]
runs2
,
Run
current_run
.
UInt16
start
)
1248 } 1249
Array[Run]
result
1250} 1251 1252///| 找到下一个可能与给定位置相关的运行段索引 1253fn
(runs : Array[Run], position : UInt16) -> Int

找到下一个可能与给定位置相关的运行段索引

find_next_relevant_run
(
Array[Run]
runs
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
],
UInt16
position
:
UInt16
UInt16
) ->
Int
Int
{
1254 let mut
Int
index
= 0
1255 while
Int
index
(self_ : Int, other : Int) -> Bool
<
Array[Run]
runs
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
1256 let
Run
run
=
Array[Run]
runs
(Array[Run], Int) -> Run

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
index]
1257 let
UInt16
end
=
Run
run
.
UInt16
start
(self : UInt16, that : UInt16) -> UInt16
+
Run
run
.
UInt16
length
1258 if
UInt16
end
(self_ : UInt16, other : UInt16) -> Bool
>=
UInt16
position
{
1259 return
Int
index
1260 } 1261
Int
index
=
Int
index
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
1262 } 1263
Array[Run]
runs
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
1264} 1265 1266///| 运行容器并集操作(完善实现) 1267fn
(runs1 : Array[Run], runs2 : Array[Run]) -> Array[Run]

运行容器并集操作(完善实现)

run_container_union
(
Array[Run]
runs1
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
],
Array[Run]
runs2
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
]) ->
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
] {
1268 if
Array[Run]
runs1
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
0 {
1269 return
Array[Run]
runs2
1270 } 1271 if
Array[Run]
runs2
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
0 {
1272 return
Array[Run]
runs1
1273 } 1274 1275 // 合并两个运行段数组并排序 1276 let mut
Array[Run]
all_runs
= []
1277 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[Run]
runs1
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1278
Array[Run]
all_runs
.
(self : Array[Run], value : Run) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Array[Run]
runs1
(Array[Run], Int) -> Run

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i])
1279 } 1280 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[Run]
runs2
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1281
Array[Run]
all_runs
.
(self : Array[Run], value : Run) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Array[Run]
runs2
(Array[Run], Int) -> Run

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i])
1282 } 1283 1284 // 按开始位置排序 1285
Array[Run]
all_runs
=
(runs : Array[Run]) -> Array[Run]

对运行段数组按开始位置排序

sort_runs
(
Array[Run]
all_runs
)
1286 1287 // 合并重叠的运行段 1288
(runs : Array[Run]) -> Array[Run]

合并重叠的运行段

merge_overlapping_runs
(
Array[Run]
all_runs
)
1289} 1290 1291///| 对运行段数组按开始位置排序 1292fn
(runs : Array[Run]) -> Array[Run]

对运行段数组按开始位置排序

sort_runs
(
Array[Run]
runs
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
]) ->
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
] {
1293 if
Array[Run]
runs
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self_ : Int, other : Int) -> Bool
<=
1 {
1294 return
Array[Run]
runs
1295 } 1296 1297 // 简单的插入排序(对于小数组足够高效) 1298 let mut
Array[Run]
sorted
= [
Array[Run]
runs
[0]]
1299 for
Int
i
= 1;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[Run]
runs
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1300 let
Run
current
=
Array[Run]
runs
(Array[Run], Int) -> Run

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
1301 let mut
Bool
inserted
= false
1302 for
Int
j
= 0;
Int
j
(self_ : Int, other : Int) -> Bool
<
Array[Run]
sorted
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1303 if
Run
current
.
UInt16
start
(self_ : UInt16, other : UInt16) -> Bool
<
Array[Run]
sorted
(Array[Run], Int) -> Run

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j].
UInt16
start
{
1304
Array[Run]
sorted
=
(arr : Array[Run], index : Int, run : Run) -> Array[Run]

在指定位置插入运行段

array_insert_at_run
(
Array[Run]
sorted
,
Int
j
,
Run
current
)
1305
Bool
inserted
= true
1306 break 1307 } 1308 } 1309 if
(x : Bool) -> Bool

Performs logical negation on a boolean value.

Parameters:

  • value : The boolean value to negate.

Returns the logical NOT of the input value: true if the input is false, and false if the input is true.

Example:

  inspect(not(true), content="false")
  inspect(not(false), content="true")
not
(
Bool
inserted
) {
1310
Array[Run]
sorted
.
(self : Array[Run], value : Run) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Run
current
)
1311 } 1312 } 1313
Array[Run]
sorted
1314} 1315 1316///| 在指定位置插入运行段 1317fn
(arr : Array[Run], index : Int, run : Run) -> Array[Run]

在指定位置插入运行段

array_insert_at_run
(
Array[Run]
arr
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
],
Int
index
:
Int
Int
,
Run
run
:
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
) ->
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
] {
1318 let
Array[Run]
result
= []
1319 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Int
index
;
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1320
Array[Run]
result
.
(self : Array[Run], value : Run) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Array[Run]
arr
(Array[Run], Int) -> Run

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i])
1321 } 1322
Array[Run]
result
.
(self : Array[Run], value : Run) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Run
run
)
1323 for
Int
i
=
Int
index
;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[Run]
arr
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1324
Array[Run]
result
.
(self : Array[Run], value : Run) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Array[Run]
arr
(Array[Run], Int) -> Run

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i])
1325 } 1326
Array[Run]
result
1327} 1328 1329///| 合并重叠的运行段 1330fn
(runs : Array[Run]) -> Array[Run]

合并重叠的运行段

merge_overlapping_runs
(
Array[Run]
runs
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
]) ->
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
] {
1331 if
Array[Run]
runs
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self_ : Int, other : Int) -> Bool
<=
1 {
1332 return
Array[Run]
runs
1333 } 1334 let
Array[Run]
result
= []
1335 let mut
Run
current
=
Array[Run]
runs
[0]
1336 for
Int
i
= 1;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[Run]
runs
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1337 let
Run
next
=
Array[Run]
runs
(Array[Run], Int) -> Run

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
1338 let
UInt16
current_end
=
Run
current
.
UInt16
start
(self : UInt16, that : UInt16) -> UInt16
+
Run
current
.
UInt16
length
1339 let
UInt16
next_start
=
Run
next
.
UInt16
start
1340 if
UInt16
next_start
(self_ : UInt16, other : UInt16) -> Bool
<=
UInt16
current_end
(self : UInt16, that : UInt16) -> UInt16
+
1 {
1341 // 运行段重叠或相邻,合并 1342 let
UInt16
next_end
=
Run
next
.
UInt16
start
(self : UInt16, that : UInt16) -> UInt16
+
Run
next
.
UInt16
length
1343 let
UInt16
merged_end
= if
UInt16
current_end
(self_ : UInt16, other : UInt16) -> Bool
>
UInt16
next_end
{
1344
UInt16
current_end
1345 } else { 1346
UInt16
next_end
1347 } 1348
Run
current
=
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
::{
1349
UInt16
start
:
Run
current
.
UInt16
start
,
1350
UInt16
length
:
UInt16
merged_end
(self : UInt16, that : UInt16) -> UInt16
-
Run
current
.
UInt16
start
,
1351 } 1352 } else { 1353 // 运行段不重叠,保存当前运行段,开始新的运行段 1354
Array[Run]
result
.
(self : Array[Run], value : Run) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Run
current
)
1355
Run
current
=
Run
next
1356 } 1357 } 1358 1359 // 添加最后一个运行段 1360
Array[Run]
result
.
(self : Array[Run], value : Run) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Run
current
)
1361
Array[Run]
result
1362} 1363 1364///| 改进的运行容器添加元素操作 1365fn
(runs : Array[Run], value : UInt16) -> Array[Run]

改进的运行容器添加元素操作

run_container_add
(
Array[Run]
runs
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
],
UInt16
value
:
UInt16
UInt16
) ->
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
] {
1366 if
Array[Run]
runs
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
0 {
1367 return [
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
::{
UInt16
start
:
UInt16
value
,
UInt16
length
: 0 }]
1368 } 1369 let
Array[Run]
result
= []
1370 let mut
Bool
inserted
= false
1371 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[Run]
runs
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1372 let
Run
run
=
Array[Run]
runs
(Array[Run], Int) -> Run

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
1373 let
UInt16
run_end
=
Run
run
.
UInt16
start
(self : UInt16, that : UInt16) -> UInt16
+
Run
run
.
UInt16
length
1374 if
UInt16
value
(self_ : UInt16, other : UInt16) -> Bool
<
Run
run
.
UInt16
start
{
1375 if
UInt16
value
(self : UInt16, that : UInt16) -> UInt16
+
1
(self : UInt16, that : UInt16) -> Bool
==
Run
run
.
UInt16
start
{
1376 // 扩展运行段的开始 1377 let
Run
extended_run
=
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
::{
UInt16
start
:
UInt16
value
,
UInt16
length
:
Run
run
.
UInt16
length
(self : UInt16, that : UInt16) -> UInt16
+
1 }
1378
Array[Run]
result
.
(self : Array[Run], value : Run) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Run
extended_run
)
1379 } else { 1380 // 插入新的运行段 1381 if
(x : Bool) -> Bool

Performs logical negation on a boolean value.

Parameters:

  • value : The boolean value to negate.

Returns the logical NOT of the input value: true if the input is false, and false if the input is true.

Example:

  inspect(not(true), content="false")
  inspect(not(false), content="true")
not
(
Bool
inserted
) {
1382
Array[Run]
result
.
(self : Array[Run], value : Run) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
::{
UInt16
start
:
UInt16
value
,
UInt16
length
: 0 })
1383
Bool
inserted
= true
1384 } 1385
Array[Run]
result
.
(self : Array[Run], value : Run) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Run
run
)
1386 } 1387 } else if
UInt16
value
(self_ : UInt16, other : UInt16) -> Bool
>=
Run
run
.
UInt16
start
(Bool, Bool) -> Bool
&&
UInt16
value
(self_ : UInt16, other : UInt16) -> Bool
<=
UInt16
run_end
{
1388 // 值已在运行段中,直接添加运行段 1389
Array[Run]
result
.
(self : Array[Run], value : Run) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Run
run
)
1390
Bool
inserted
= true
1391 } else if
UInt16
value
(self : UInt16, that : UInt16) -> Bool
==
UInt16
run_end
(self : UInt16, that : UInt16) -> UInt16
+
1 {
1392 // 扩展运行段的结束 1393 let
Run
extended_run
=
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
::{
UInt16
start
:
Run
run
.
UInt16
start
,
UInt16
length
:
Run
run
.
UInt16
length
(self : UInt16, that : UInt16) -> UInt16
+
1 }
1394
Array[Run]
result
.
(self : Array[Run], value : Run) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Run
extended_run
)
1395
Bool
inserted
= true
1396 } else { 1397
Array[Run]
result
.
(self : Array[Run], value : Run) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Run
run
)
1398 } 1399 } 1400 1401 // 如果还没插入,添加到末尾 1402 if
(x : Bool) -> Bool

Performs logical negation on a boolean value.

Parameters:

  • value : The boolean value to negate.

Returns the logical NOT of the input value: true if the input is false, and false if the input is true.

Example:

  inspect(not(true), content="false")
  inspect(not(false), content="true")
not
(
Bool
inserted
) {
1403
Array[Run]
result
.
(self : Array[Run], value : Run) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
::{
UInt16
start
:
UInt16
value
,
UInt16
length
: 0 })
1404 } 1405 1406 // 合并可能的重叠运行段 1407
(runs : Array[Run]) -> Array[Run]

合并重叠的运行段

merge_overlapping_runs
(
Array[Run]
result
)
1408} 1409 1410///| 改进的运行容器移除元素操作 1411fn
(runs : Array[Run], value : UInt16) -> Array[Run]

改进的运行容器移除元素操作

run_container_remove
(
Array[Run]
runs
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
],
UInt16
value
:
UInt16
UInt16
) ->
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
] {
1412 let
Array[Run]
result
= []
1413 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[Run]
runs
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1414 let
Run
run
=
Array[Run]
runs
(Array[Run], Int) -> Run

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
1415 let
UInt16
run_end
=
Run
run
.
UInt16
start
(self : UInt16, that : UInt16) -> UInt16
+
Run
run
.
UInt16
length
1416 if
UInt16
value
(self_ : UInt16, other : UInt16) -> Bool
<
Run
run
.
UInt16
start
(Bool, Bool) -> Bool
||
UInt16
value
(self_ : UInt16, other : UInt16) -> Bool
>
UInt16
run_end
{
1417 // 值不在当前运行段中,保持运行段不变 1418
Array[Run]
result
.
(self : Array[Run], value : Run) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Run
run
)
1419 } else if
Run
run
.
UInt16
start
(self : UInt16, that : UInt16) -> Bool
==
UInt16
run_end
(Bool, Bool) -> Bool
&&
Run
run
.
UInt16
start
(self : UInt16, that : UInt16) -> Bool
==
UInt16
value
{
1420 // 运行段只有一个元素且就是要移除的值,跳过这个运行段 1421 continue 1422 } else if
UInt16
value
(self : UInt16, that : UInt16) -> Bool
==
Run
run
.
UInt16
start
{
1423 // 移除运行段的第一个元素 1424 if
Run
run
.
UInt16
length
(self_ : UInt16, other : UInt16) -> Bool
>
0 {
1425 let
Run
new_run
=
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
::{
UInt16
start
:
Run
run
.
UInt16
start
(self : UInt16, that : UInt16) -> UInt16
+
1,
UInt16
length
:
Run
run
.
UInt16
length
(self : UInt16, that : UInt16) -> UInt16
-
1 }
1426
Array[Run]
result
.
(self : Array[Run], value : Run) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Run
new_run
)
1427 } 1428 } else if
UInt16
value
(self : UInt16, that : UInt16) -> Bool
==
UInt16
run_end
{
1429 // 移除运行段的最后一个元素 1430 let
Run
new_run
=
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
::{
UInt16
start
:
Run
run
.
UInt16
start
,
UInt16
length
:
Run
run
.
UInt16
length
(self : UInt16, that : UInt16) -> UInt16
-
1 }
1431
Array[Run]
result
.
(self : Array[Run], value : Run) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Run
new_run
)
1432 } else { 1433 // 移除运行段中间的元素,需要分割运行段 1434 let
Run
left_run
=
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
::{
UInt16
start
:
Run
run
.
UInt16
start
,
UInt16
length
:
UInt16
value
(self : UInt16, that : UInt16) -> UInt16
-
Run
run
.
UInt16
start
(self : UInt16, that : UInt16) -> UInt16
-
1 }
1435 let
Run
right_run
=
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
::{
UInt16
start
:
UInt16
value
(self : UInt16, that : UInt16) -> UInt16
+
1,
UInt16
length
:
UInt16
run_end
(self : UInt16, that : UInt16) -> UInt16
-
UInt16
value
}
1436
Array[Run]
result
.
(self : Array[Run], value : Run) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Run
left_run
)
1437
Array[Run]
result
.
(self : Array[Run], value : Run) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Run
right_run
)
1438 } 1439 } 1440
Array[Run]
result
1441} 1442 1443///| 检查两个bitmap是否相等 1444pub fn
(self : RoaringBitmap, other : RoaringBitmap) -> Bool

检查两个bitmap是否相等

equals
(
RoaringBitmap
self
:
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
,
RoaringBitmap
other
:
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
) ->
Bool
Bool
{
1445 if
RoaringBitmap
self
.
Int
size
(x : Int, y : Int) -> Bool
!=
RoaringBitmap
other
.
Int
size
{
1446 return false 1447 } 1448 if
RoaringBitmap
self
.
Array[HighContainer]
containers
.
(self : Array[HighContainer]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(x : Int, y : Int) -> Bool
!=
RoaringBitmap
other
.
Array[HighContainer]
containers
.
(self : Array[HighContainer]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
1449 return false 1450 } 1451 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
RoaringBitmap
self
.
Array[HighContainer]
containers
.
(self : Array[HighContainer]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1452 let
HighContainer
c1
=
RoaringBitmap
self
.
Array[HighContainer]
containers
(Array[HighContainer], Int) -> HighContainer

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
1453 let
HighContainer
c2
=
RoaringBitmap
other
.
Array[HighContainer]
containers
(Array[HighContainer], Int) -> HighContainer

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
1454 if
HighContainer
c1
.
UInt16
key
(x : UInt16, y : UInt16) -> Bool
!=
HighContainer
c2
.
UInt16
key
(Bool, Bool) -> Bool
||
HighContainer
c1
.
Int
cardinality
(x : Int, y : Int) -> Bool
!=
HighContainer
c2
.
Int
cardinality
{
1455 return false 1456 } 1457 // 这里需要实现容器相等性检查 1458 if
(x : Bool) -> Bool

Performs logical negation on a boolean value.

Parameters:

  • value : The boolean value to negate.

Returns the logical NOT of the input value: true if the input is false, and false if the input is true.

Example:

  inspect(not(true), content="false")
  inspect(not(false), content="true")
not
(
(container1 : Container, container2 : Container) -> Bool

检查两个容器是否相等

containers_equal
(
HighContainer
c1
.
Container
container
,
HighContainer
c2
.
Container
container
)) {
1459 return false 1460 } 1461 } 1462 true 1463} 1464 1465///| 获取最小值 1466pub fn
(self : RoaringBitmap) -> UInt?

获取最小值

min
(
RoaringBitmap
self
:
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
) ->
UInt
UInt
? {
1467 if
RoaringBitmap
self
.
(self : RoaringBitmap) -> Bool

检查bitmap是否为空

is_empty
() {
1468 return
UInt?
None
1469 } 1470 let
HighContainer
first_container
=
RoaringBitmap
self
.
Array[HighContainer]
containers
[0]
1471 let
UInt16
min_in_container
=
(container : Container) -> UInt16

获取容器中的最小值

container_min
(
HighContainer
first_container
.
Container
container
)
1472 let
UInt
high_bits
=
HighContainer
first_container
.
UInt16
key
.
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
().
(self : Int) -> UInt

reinterpret the signed int as unsigned int, when the value is non-negative, i.e, 0..=2^31-1, the value is the same. When the value is negative, it turns into a large number, for example, -1 turns into 2^32-1

reinterpret_as_uint
()
(self : UInt, shift : Int) -> UInt

Performs a left shift operation on an unsigned 32-bit integer. Each bit in the integer is shifted left by the specified number of positions, and zeros are filled in from the right.

Parameters:

  • self : The unsigned 32-bit integer to be shifted.
  • shift : The number of positions to shift. Only the least significant 5 bits are used, effectively making the shift count always between 0 and 31.

Returns a new unsigned 32-bit integer that is the result of shifting self left by shift positions.

Example:

  let x = 1U
  inspect(x << 3, content="8") // Binary: 1 -> 1000
  let y = 0xFFFFFFFFU
  inspect(y << 16, content="4294901760") // All bits after position 16 are discarded
<<
16
1473
(UInt) -> UInt?
Some
(
UInt
high_bits
(UInt, UInt) -> UInt
|
UInt16
min_in_container
.
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
().
(self : Int) -> UInt

reinterpret the signed int as unsigned int, when the value is non-negative, i.e, 0..=2^31-1, the value is the same. When the value is negative, it turns into a large number, for example, -1 turns into 2^32-1

reinterpret_as_uint
())
1474} 1475 1476///| 获取最大值 1477pub fn
(self : RoaringBitmap) -> UInt?

获取最大值

max
(
RoaringBitmap
self
:
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
) ->
UInt
UInt
? {
1478 if
RoaringBitmap
self
.
(self : RoaringBitmap) -> Bool

检查bitmap是否为空

is_empty
() {
1479 return
UInt?
None
1480 } 1481 let
HighContainer
last_container
=
RoaringBitmap
self
.
Array[HighContainer]
containers
(Array[HighContainer], Int) -> HighContainer

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
self.
Array[HighContainer]
containers
.
(self : Array[HighContainer]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
-
1]
1482 let
UInt16
max_in_container
=
(container : Container) -> UInt16

获取容器中的最大值

container_max
(
HighContainer
last_container
.
Container
container
)
1483 let
UInt
high_bits
=
HighContainer
last_container
.
UInt16
key
.
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
().
(self : Int) -> UInt

reinterpret the signed int as unsigned int, when the value is non-negative, i.e, 0..=2^31-1, the value is the same. When the value is negative, it turns into a large number, for example, -1 turns into 2^32-1

reinterpret_as_uint
()
(self : UInt, shift : Int) -> UInt

Performs a left shift operation on an unsigned 32-bit integer. Each bit in the integer is shifted left by the specified number of positions, and zeros are filled in from the right.

Parameters:

  • self : The unsigned 32-bit integer to be shifted.
  • shift : The number of positions to shift. Only the least significant 5 bits are used, effectively making the shift count always between 0 and 31.

Returns a new unsigned 32-bit integer that is the result of shifting self left by shift positions.

Example:

  let x = 1U
  inspect(x << 3, content="8") // Binary: 1 -> 1000
  let y = 0xFFFFFFFFU
  inspect(y << 16, content="4294901760") // All bits after position 16 are discarded
<<
16
1484
(UInt) -> UInt?
Some
(
UInt
high_bits
(UInt, UInt) -> UInt
|
UInt16
max_in_container
.
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
().
(self : Int) -> UInt

reinterpret the signed int as unsigned int, when the value is non-negative, i.e, 0..=2^31-1, the value is the same. When the value is negative, it turns into a large number, for example, -1 turns into 2^32-1

reinterpret_as_uint
())
1485} 1486 1487///| 迭代器支持 1488pub fn
(self : RoaringBitmap, f : (UInt) -> Unit) -> Unit

迭代器支持

iter
(
RoaringBitmap
self
:
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
,
(UInt) -> Unit
f
: (
UInt
UInt
) ->
Unit
Unit
) ->
Unit
Unit
{
1489 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
RoaringBitmap
self
.
Array[HighContainer]
containers
.
(self : Array[HighContainer]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1490 let
HighContainer
container
=
RoaringBitmap
self
.
Array[HighContainer]
containers
(Array[HighContainer], Int) -> HighContainer

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
1491 let
UInt
high_bits
=
HighContainer
container
.
UInt16
key
.
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
().
(self : Int) -> UInt

reinterpret the signed int as unsigned int, when the value is non-negative, i.e, 0..=2^31-1, the value is the same. When the value is negative, it turns into a large number, for example, -1 turns into 2^32-1

reinterpret_as_uint
()
(self : UInt, shift : Int) -> UInt

Performs a left shift operation on an unsigned 32-bit integer. Each bit in the integer is shifted left by the specified number of positions, and zeros are filled in from the right.

Parameters:

  • self : The unsigned 32-bit integer to be shifted.
  • shift : The number of positions to shift. Only the least significant 5 bits are used, effectively making the shift count always between 0 and 31.

Returns a new unsigned 32-bit integer that is the result of shifting self left by shift positions.

Example:

  let x = 1U
  inspect(x << 3, content="8") // Binary: 1 -> 1000
  let y = 0xFFFFFFFFU
  inspect(y << 16, content="4294901760") // All bits after position 16 are discarded
<<
16
1492 let
Array[UInt16]
values
=
(container : Container) -> Array[UInt16]

容器转换为数组

container_to_array
(
HighContainer
container
.
Container
container
)
1493 for
Int
j
= 0;
Int
j
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
values
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1494
(UInt) -> Unit
f
(
UInt
high_bits
(UInt, UInt) -> UInt
|
Array[UInt16]
values
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j].
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
().
(self : Int) -> UInt

reinterpret the signed int as unsigned int, when the value is non-negative, i.e, 0..=2^31-1, the value is the same. When the value is negative, it turns into a large number, for example, -1 turns into 2^32-1

reinterpret_as_uint
())
1495 } 1496 } 1497} 1498 1499///| 范围查询 1500pub fn
(self : RoaringBitmap, start : UInt, end : UInt) -> RoaringBitmap

范围查询

range
(
RoaringBitmap
self
:
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
,
UInt
start
:
UInt
UInt
,
UInt
end
:
UInt
UInt
) ->
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
{
1501 let mut
RoaringBitmap
result
=
() -> RoaringBitmap

创建空的 RoaringBitmap

new
()
1502 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
RoaringBitmap
self
.
Array[HighContainer]
containers
.
(self : Array[HighContainer]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1503 let
HighContainer
container
=
RoaringBitmap
self
.
Array[HighContainer]
containers
(Array[HighContainer], Int) -> HighContainer

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
1504 let
UInt
high_bits
=
HighContainer
container
.
UInt16
key
.
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
().
(self : Int) -> UInt

reinterpret the signed int as unsigned int, when the value is non-negative, i.e, 0..=2^31-1, the value is the same. When the value is negative, it turns into a large number, for example, -1 turns into 2^32-1

reinterpret_as_uint
()
(self : UInt, shift : Int) -> UInt

Performs a left shift operation on an unsigned 32-bit integer. Each bit in the integer is shifted left by the specified number of positions, and zeros are filled in from the right.

Parameters:

  • self : The unsigned 32-bit integer to be shifted.
  • shift : The number of positions to shift. Only the least significant 5 bits are used, effectively making the shift count always between 0 and 31.

Returns a new unsigned 32-bit integer that is the result of shifting self left by shift positions.

Example:

  let x = 1U
  inspect(x << 3, content="8") // Binary: 1 -> 1000
  let y = 0xFFFFFFFFU
  inspect(y << 16, content="4294901760") // All bits after position 16 are discarded
<<
16
1505 let
Array[UInt16]
values
=
(container : Container) -> Array[UInt16]

容器转换为数组

container_to_array
(
HighContainer
container
.
Container
container
)
1506 for
Int
j
= 0;
Int
j
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
values
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1507 let
UInt
value
=
UInt
high_bits
(UInt, UInt) -> UInt
|
Array[UInt16]
values
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j].
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
().
(self : Int) -> UInt

reinterpret the signed int as unsigned int, when the value is non-negative, i.e, 0..=2^31-1, the value is the same. When the value is negative, it turns into a large number, for example, -1 turns into 2^32-1

reinterpret_as_uint
()
1508 if
UInt
value
(self_ : UInt, other : UInt) -> Bool
>=
UInt
start
(Bool, Bool) -> Bool
&&
UInt
value
(self_ : UInt, other : UInt) -> Bool
<=
UInt
end
{
1509
RoaringBitmap
result
=
RoaringBitmap
result
.
(self : RoaringBitmap, value : UInt) -> RoaringBitmap

添加单个元素

add
(
UInt
value
)
1510 } 1511 } 1512 } 1513
RoaringBitmap
result
1514} 1515 1516///| 计算两个bitmap的Jaccard相似度 1517pub fn
(self : RoaringBitmap, other : RoaringBitmap) -> Double

计算两个bitmap的Jaccard相似度

jaccard_similarity
(
1518
RoaringBitmap
self
:
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
,
1519
RoaringBitmap
other
:
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
1520) ->
Double
Double
{
1521 let
Int
intersection_size
=
RoaringBitmap
self
.
(self : RoaringBitmap, other : RoaringBitmap) -> RoaringBitmap

交集操作

intersect
(
RoaringBitmap
other
).
(self : RoaringBitmap) -> Int

获取bitmap的大小(元素个数)

size
()
1522 let
Int
union_size
=
RoaringBitmap
self
.
(self : RoaringBitmap, other : RoaringBitmap) -> RoaringBitmap

并集操作

union
(
RoaringBitmap
other
).
(self : RoaringBitmap) -> Int

获取bitmap的大小(元素个数)

size
()
1523 if
Int
union_size
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
0 {
1524 return 1.0 1525 } 1526
Int
intersection_size
.
(self : Int) -> Double

Converts a 32-bit integer to a double-precision floating-point number. The conversion preserves the exact value since all integers in the range of Int can be represented exactly as Double values.

Parameters:

  • self : The 32-bit integer to be converted.

Returns a double-precision floating-point number that represents the same numerical value as the input integer.

Example:

  let n = 42
  inspect(n.to_double(), content="42")
  let neg = -42
  inspect(neg.to_double(), content="-42")
to_double
()
(self : Double, other : Double) -> Double

Performs division between two double-precision floating-point numbers. Follows IEEE 754 standard for floating-point arithmetic, including handling of special cases like division by zero (returns infinity) and operations involving NaN.

Parameters:

  • self : The dividend (numerator) in the division operation.
  • other : The divisor (denominator) in the division operation.

Returns the result of dividing self by other. Special cases follow IEEE 754:

  • Division by zero returns positive or negative infinity based on the dividend's sign
  • Operations involving NaN return NaN
  • Division of infinity by infinity returns NaN

Example:

  inspect(6.0 / 2.0, content="3")
  inspect(-6.0 / 2.0, content="-3")
  inspect(1.0 / 0.0, content="Infinity")
/
Int
union_size
.
(self : Int) -> Double

Converts a 32-bit integer to a double-precision floating-point number. The conversion preserves the exact value since all integers in the range of Int can be represented exactly as Double values.

Parameters:

  • self : The 32-bit integer to be converted.

Returns a double-precision floating-point number that represents the same numerical value as the input integer.

Example:

  let n = 42
  inspect(n.to_double(), content="42")
  let neg = -42
  inspect(neg.to_double(), content="-42")
to_double
()
1527} 1528 1529// =============== 容器最小值/最大值函数 =============== 1530 1531///| 获取容器中的最小值 1532fn
(container : Container) -> UInt16

获取容器中的最小值

container_min
(
Container
container
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
) ->
UInt16
UInt16
{
1533 match
Container
container
{
1534
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr
) =>
(arr : Array[UInt16]) -> UInt16

数组容器最小值

array_container_min
(
Array[UInt16]
arr
)
1535
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap
) =>
(bitmap : FixedArray[UInt64]) -> UInt16

位图容器最小值

bitmap_container_min
(
FixedArray[UInt64]
bitmap
)
1536
(Array[Run]) -> Container
RunContainer
(
Array[Run]
runs
) =>
(runs : Array[Run]) -> UInt16

运行容器最小值

run_container_min
(
Array[Run]
runs
)
1537 } 1538} 1539 1540///| 获取容器中的最大值 1541fn
(container : Container) -> UInt16

获取容器中的最大值

container_max
(
Container
container
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
) ->
UInt16
UInt16
{
1542 match
Container
container
{
1543
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr
) =>
(arr : Array[UInt16]) -> UInt16

数组容器最大值

array_container_max
(
Array[UInt16]
arr
)
1544
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap
) =>
(bitmap : FixedArray[UInt64]) -> UInt16

位图容器最大值

bitmap_container_max
(
FixedArray[UInt64]
bitmap
)
1545
(Array[Run]) -> Container
RunContainer
(
Array[Run]
runs
) =>
(runs : Array[Run]) -> UInt16

运行容器最大值

run_container_max
(
Array[Run]
runs
)
1546 } 1547} 1548 1549///| 检查两个容器是否相等 1550fn
(container1 : Container, container2 : Container) -> Bool

检查两个容器是否相等

containers_equal
(
Container
container1
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
,
Container
container2
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
) ->
Bool
Bool
{
1551 match (
Container
container1
,
Container
container2
) {
1552 (
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr1
),
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr2
)) =>
1553
(arr1 : Array[UInt16], arr2 : Array[UInt16]) -> Bool

检查两个UInt16数组是否相等

arrays_equal_uint16
(
Array[UInt16]
arr1
,
Array[UInt16]
arr2
)
1554 (
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap1
),
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap2
)) =>
1555
(bitmap1 : FixedArray[UInt64], bitmap2 : FixedArray[UInt64]) -> Bool

检查两个位图是否相等

bitmaps_equal
(
FixedArray[UInt64]
bitmap1
,
FixedArray[UInt64]
bitmap2
)
1556 (
(Array[Run]) -> Container
RunContainer
(
Array[Run]
runs1
),
(Array[Run]) -> Container
RunContainer
(
Array[Run]
runs2
)) =>
(runs1 : Array[Run], runs2 : Array[Run]) -> Bool

检查两个运行段数组是否相等

runs_equal
(
Array[Run]
runs1
,
Array[Run]
runs2
)
1557 _ => { 1558 // 不同类型的容器,转换为数组后比较 1559 let
Array[UInt16]
arr1
=
(container : Container) -> Array[UInt16]

容器转换为数组

container_to_array
(
Container
container1
)
1560 let
Array[UInt16]
arr2
=
(container : Container) -> Array[UInt16]

容器转换为数组

container_to_array
(
Container
container2
)
1561
(arr1 : Array[UInt16], arr2 : Array[UInt16]) -> Bool

检查两个UInt16数组是否相等

arrays_equal_uint16
(
Array[UInt16]
arr1
,
Array[UInt16]
arr2
)
1562 } 1563 } 1564} 1565 1566// =============== 数组容器最小值/最大值 =============== 1567 1568///| 数组容器最小值 1569fn
(arr : Array[UInt16]) -> UInt16

数组容器最小值

array_container_min
(
Array[UInt16]
arr
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
]) ->
UInt16
UInt16
{
1570 if
Array[UInt16]
arr
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
0 {
1571
(msg : String) -> Unit

Aborts the program with an error message. Always causes a panic, regardless of the message provided.

Parameters:

  • message : A string containing the error message to be displayed when aborting.

Returns a value of type T. However, this function never actually returns a value as it always causes a panic.

abort
("Empty array container")
1572 } 1573
Array[UInt16]
arr
[0] // 数组已排序,第一个元素是最小值
1574} 1575 1576///| 数组容器最大值 1577fn
(arr : Array[UInt16]) -> UInt16

数组容器最大值

array_container_max
(
Array[UInt16]
arr
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
]) ->
UInt16
UInt16
{
1578 if
Array[UInt16]
arr
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
0 {
1579
(msg : String) -> Unit

Aborts the program with an error message. Always causes a panic, regardless of the message provided.

Parameters:

  • message : A string containing the error message to be displayed when aborting.

Returns a value of type T. However, this function never actually returns a value as it always causes a panic.

abort
("Empty array container")
1580 } 1581
Array[UInt16]
arr
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
arr.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
-
1] // 数组已排序,最后一个元素是最大值
1582} 1583 1584///| 检查两个UInt16数组是否相等 1585fn
(arr1 : Array[UInt16], arr2 : Array[UInt16]) -> Bool

检查两个UInt16数组是否相等

arrays_equal_uint16
(
Array[UInt16]
arr1
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
],
Array[UInt16]
arr2
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
]) ->
Bool
Bool
{
1586 if
Array[UInt16]
arr1
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(x : Int, y : Int) -> Bool
!=
Array[UInt16]
arr2
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
1587 return false 1588 } 1589 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr1
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1590 if
Array[UInt16]
arr1
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
(x : UInt16, y : UInt16) -> Bool
!=
Array[UInt16]
arr2
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i] {
1591 return false 1592 } 1593 } 1594 true 1595} 1596 1597// =============== 位图容器最小值/最大值 =============== 1598 1599///| 位图容器最小值 1600fn
(bitmap : FixedArray[UInt64]) -> UInt16

位图容器最小值

bitmap_container_min
(
FixedArray[UInt64]
bitmap
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
]) ->
UInt16
UInt16
{
1601 for
Int
word_index
= 0;
Int
word_index
(self_ : Int, other : Int) -> Bool
<
1024;
Int
word_index
=
Int
word_index
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1602 let
UInt64
word
=
FixedArray[UInt64]
bitmap
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
word_index]
1603 if
UInt64
word
(x : UInt64, y : UInt64) -> Bool
!=
0UL {
1604 // 找到第一个非零的64位字 1605 for
Int
bit_index
= 0;
Int
bit_index
(self_ : Int, other : Int) -> Bool
<
64;
Int
bit_index
=
Int
bit_index
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1606 if (
UInt64
word
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise AND operation between two unsigned 64-bit integers.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns the result of performing a bitwise AND operation on the two operands.

Example:

  let a = 0xF0F0F0F0F0F0F0F0UL
  let b = 0xFF00FF00FF00FF00UL
  inspect(a & b, content="17294086455919964160") // 0xF000F000F000F000
&
(1UL
(self : UInt64, shift : Int) -> UInt64

Performs a bitwise left shift operation on an unsigned 64-bit integer.

Parameters:

  • self : The unsigned 64-bit integer to be shifted.
  • shift : The number of positions to shift the bits to the left. Must be between 0 and 63 inclusive, values outside this range will be masked with & 63.

Returns the result of shifting self left by shift positions. For each position shifted, the rightmost bit is filled with 0, and the leftmost bit is discarded.

Example:

  let x = 1UL
  inspect(x << 3, content="8") // 1 shifted left by 3 positions becomes 8
  inspect(x << 63, content="9223372036854775808") // 1 shifted left by 63 positions
<<
Int
bit_index
))
(x : UInt64, y : UInt64) -> Bool
!=
0UL {
1607 let
Int
value
=
Int
word_index
(self : Int, other : Int) -> Int

Multiplies two 32-bit integers. This is the implementation of the * operator for Int.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns the product of the two integers. If the result overflows the range of Int, it wraps around according to two's complement arithmetic.

Example:

  inspect(42 * 2, content="84")
  inspect(-10 * 3, content="-30")
  let max = 2147483647 // Int.max_value
  inspect(max * 2, content="-2") // Overflow wraps around
*
64
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
Int
bit_index
1608 return
Int
value
.
(self : Int) -> UInt16

Converts a 32-bit signed integer to a 16-bit unsigned integer by truncating its value to fit within the range of 0 to 65535.

Parameters:

  • integer : The 32-bit signed integer to be converted. Values outside the range of UInt16 will be truncated to fit.

Returns a 16-bit unsigned integer containing the lower 16 bits of the input value.

Example:

  let n = 42
  inspect(n.to_uint16(), content="42")
  let neg = -1
  inspect(neg.to_uint16(), content="65535") // -1 becomes max value of UInt16
  let large = 65536
  inspect(large.to_uint16(), content="0") // Values wrap around
to_uint16
()
1609 } 1610 } 1611 } 1612 } 1613
(msg : String) -> UInt16

Aborts the program with an error message. Always causes a panic, regardless of the message provided.

Parameters:

  • message : A string containing the error message to be displayed when aborting.

Returns a value of type T. However, this function never actually returns a value as it always causes a panic.

abort
("Empty bitmap container")
1614} 1615 1616///| 位图容器最大值 1617fn
(bitmap : FixedArray[UInt64]) -> UInt16

位图容器最大值

bitmap_container_max
(
FixedArray[UInt64]
bitmap
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
]) ->
UInt16
UInt16
{
1618 // 从后往前搜索 1619 for
Int
word_index
= 1023;
Int
word_index
(self_ : Int, other : Int) -> Bool
>=
0;
Int
word_index
=
Int
word_index
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
-
1 {
1620 let
UInt64
word
=
FixedArray[UInt64]
bitmap
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
word_index]
1621 if
UInt64
word
(x : UInt64, y : UInt64) -> Bool
!=
0UL {
1622 // 找到最后一个非零的64位字 1623 for
Int
bit_index
= 63;
Int
bit_index
(self_ : Int, other : Int) -> Bool
>=
0;
Int
bit_index
=
Int
bit_index
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
-
1 {
1624 if (
UInt64
word
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise AND operation between two unsigned 64-bit integers.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns the result of performing a bitwise AND operation on the two operands.

Example:

  let a = 0xF0F0F0F0F0F0F0F0UL
  let b = 0xFF00FF00FF00FF00UL
  inspect(a & b, content="17294086455919964160") // 0xF000F000F000F000
&
(1UL
(self : UInt64, shift : Int) -> UInt64

Performs a bitwise left shift operation on an unsigned 64-bit integer.

Parameters:

  • self : The unsigned 64-bit integer to be shifted.
  • shift : The number of positions to shift the bits to the left. Must be between 0 and 63 inclusive, values outside this range will be masked with & 63.

Returns the result of shifting self left by shift positions. For each position shifted, the rightmost bit is filled with 0, and the leftmost bit is discarded.

Example:

  let x = 1UL
  inspect(x << 3, content="8") // 1 shifted left by 3 positions becomes 8
  inspect(x << 63, content="9223372036854775808") // 1 shifted left by 63 positions
<<
Int
bit_index
))
(x : UInt64, y : UInt64) -> Bool
!=
0UL {
1625 let
Int
value
=
Int
word_index
(self : Int, other : Int) -> Int

Multiplies two 32-bit integers. This is the implementation of the * operator for Int.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns the product of the two integers. If the result overflows the range of Int, it wraps around according to two's complement arithmetic.

Example:

  inspect(42 * 2, content="84")
  inspect(-10 * 3, content="-30")
  let max = 2147483647 // Int.max_value
  inspect(max * 2, content="-2") // Overflow wraps around
*
64
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
Int
bit_index
1626 return
Int
value
.
(self : Int) -> UInt16

Converts a 32-bit signed integer to a 16-bit unsigned integer by truncating its value to fit within the range of 0 to 65535.

Parameters:

  • integer : The 32-bit signed integer to be converted. Values outside the range of UInt16 will be truncated to fit.

Returns a 16-bit unsigned integer containing the lower 16 bits of the input value.

Example:

  let n = 42
  inspect(n.to_uint16(), content="42")
  let neg = -1
  inspect(neg.to_uint16(), content="65535") // -1 becomes max value of UInt16
  let large = 65536
  inspect(large.to_uint16(), content="0") // Values wrap around
to_uint16
()
1627 } 1628 } 1629 } 1630 } 1631
(msg : String) -> UInt16

Aborts the program with an error message. Always causes a panic, regardless of the message provided.

Parameters:

  • message : A string containing the error message to be displayed when aborting.

Returns a value of type T. However, this function never actually returns a value as it always causes a panic.

abort
("Empty bitmap container")
1632} 1633 1634///| 检查两个位图是否相等 1635fn
(bitmap1 : FixedArray[UInt64], bitmap2 : FixedArray[UInt64]) -> Bool

检查两个位图是否相等

bitmaps_equal
(
1636
FixedArray[UInt64]
bitmap1
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
],
1637
FixedArray[UInt64]
bitmap2
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
]
1638) ->
Bool
Bool
{
1639 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
1024;
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1640 if
FixedArray[UInt64]
bitmap1
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
i]
(x : UInt64, y : UInt64) -> Bool
!=
FixedArray[UInt64]
bitmap2
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
i] {
1641 return false 1642 } 1643 } 1644 true 1645} 1646 1647// =============== 运行容器最小值/最大值 =============== 1648 1649///| 运行容器最小值 1650fn
(runs : Array[Run]) -> UInt16

运行容器最小值

run_container_min
(
Array[Run]
runs
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
]) ->
UInt16
UInt16
{
1651 if
Array[Run]
runs
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
0 {
1652
(msg : String) -> Unit

Aborts the program with an error message. Always causes a panic, regardless of the message provided.

Parameters:

  • message : A string containing the error message to be displayed when aborting.

Returns a value of type T. However, this function never actually returns a value as it always causes a panic.

abort
("Empty run container")
1653 } 1654 1655 // 找到起始位置最小的运行段 1656 let mut
UInt16
min_start
=
Array[Run]
runs
[0].
UInt16
start
1657 for
Int
i
= 1;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[Run]
runs
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1658 if
Array[Run]
runs
(Array[Run], Int) -> Run

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i].
UInt16
start
(self_ : UInt16, other : UInt16) -> Bool
<
UInt16
min_start
{
1659
UInt16
min_start
=
Array[Run]
runs
(Array[Run], Int) -> Run

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i].
UInt16
start
1660 } 1661 } 1662
UInt16
min_start
1663} 1664 1665///| 运行容器最大值 1666fn
(runs : Array[Run]) -> UInt16

运行容器最大值

run_container_max
(
Array[Run]
runs
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
]) ->
UInt16
UInt16
{
1667 if
Array[Run]
runs
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
0 {
1668
(msg : String) -> Unit

Aborts the program with an error message. Always causes a panic, regardless of the message provided.

Parameters:

  • message : A string containing the error message to be displayed when aborting.

Returns a value of type T. However, this function never actually returns a value as it always causes a panic.

abort
("Empty run container")
1669 } 1670 1671 // 找到结束位置最大的运行段 1672 let mut
UInt16
max_end
=
Array[Run]
runs
[0].
UInt16
start
(self : UInt16, that : UInt16) -> UInt16
+
Array[Run]
runs
[0].
UInt16
length
1673 for
Int
i
= 1;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[Run]
runs
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1674 let
UInt16
end
=
Array[Run]
runs
(Array[Run], Int) -> Run

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i].
UInt16
start
(self : UInt16, that : UInt16) -> UInt16
+
Array[Run]
runs
(Array[Run], Int) -> Run

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i].
UInt16
length
1675 if
UInt16
end
(self_ : UInt16, other : UInt16) -> Bool
>
UInt16
max_end
{
1676
UInt16
max_end
=
UInt16
end
1677 } 1678 } 1679
UInt16
max_end
1680} 1681 1682///| 检查两个运行段数组是否相等 1683fn
(runs1 : Array[Run], runs2 : Array[Run]) -> Bool

检查两个运行段数组是否相等

runs_equal
(
Array[Run]
runs1
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
],
Array[Run]
runs2
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
]) ->
Bool
Bool
{
1684 if
Array[Run]
runs1
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(x : Int, y : Int) -> Bool
!=
Array[Run]
runs2
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
1685 return false 1686 } 1687 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[Run]
runs1
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1688 let
Run
run1
=
Array[Run]
runs1
(Array[Run], Int) -> Run

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
1689 let
Run
run2
=
Array[Run]
runs2
(Array[Run], Int) -> Run

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
1690 if
Run
run1
.
UInt16
start
(x : UInt16, y : UInt16) -> Bool
!=
Run
run2
.
UInt16
start
(Bool, Bool) -> Bool
||
Run
run1
.
UInt16
length
(x : UInt16, y : UInt16) -> Bool
!=
Run
run2
.
UInt16
length
{
1691 return false 1692 } 1693 } 1694 true 1695} 1696 1697// =============== 容器检查函数 =============== 1698 1699///| 检查容器是否为空 1700fn
(container : Container) -> Bool

检查容器是否为空

container_is_empty
(
Container
container
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
) ->
Bool
Bool
{
1701 match
Container
container
{
1702
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr
) =>
Array[UInt16]
arr
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
0
1703
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap
) =>
(bitmap : FixedArray[UInt64]) -> Int

计算位图容器基数

bitmap_container_cardinality
(
FixedArray[UInt64]
bitmap
)
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
0
1704
(Array[Run]) -> Container
RunContainer
(
Array[Run]
runs
) =>
Array[Run]
runs
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
0
1705 } 1706} 1707 1708///| 获取容器中的第一个元素(最小值) 1709pub fn
(container : Container) -> UInt16?

获取容器中的第一个元素(最小值)

container_first
(
Container
container
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
) ->
UInt16
UInt16
? {
1710 if
(container : Container) -> Bool

检查容器是否为空

container_is_empty
(
Container
container
) {
1711 return
UInt16?
None
1712 } 1713
(UInt16) -> UInt16?
Some
(
(container : Container) -> UInt16

获取容器中的最小值

container_min
(
Container
container
))
1714} 1715 1716///| 获取容器中的最后一个元素(最大值) 1717pub fn
(container : Container) -> UInt16?

获取容器中的最后一个元素(最大值)

container_last
(
Container
container
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
) ->
UInt16
UInt16
? {
1718 if
(container : Container) -> Bool

检查容器是否为空

container_is_empty
(
Container
container
) {
1719 return
UInt16?
None
1720 } 1721
(UInt16) -> UInt16?
Some
(
(container : Container) -> UInt16

获取容器中的最大值

container_max
(
Container
container
))
1722} 1723 1724///| 获取容器中指定索引的元素 1725pub fn
(container : Container, index : Int) -> UInt16?

获取容器中指定索引的元素

container_get_at
(
Container
container
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
,
Int
index
:
Int
Int
) ->
UInt16
UInt16
? {
1726 if
Int
index
(self_ : Int, other : Int) -> Bool
<
0
(Bool, Bool) -> Bool
||
Int
index
(self_ : Int, other : Int) -> Bool
>=
(container : Container) -> Int

获取容器基数

container_cardinality
(
Container
container
) {
1727 return
UInt16?
None
1728 } 1729 match
Container
container
{
1730
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr
) =>
(UInt16) -> UInt16?
Some
(
Array[UInt16]
arr
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
index])
1731
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap
) => {
1732 let
Array[UInt16]
arr
=
(bitmap : FixedArray[UInt64]) -> Array[UInt16]

位图容器转数组

bitmap_container_to_array
(
FixedArray[UInt64]
bitmap
)
1733
(UInt16) -> UInt16?
Some
(
Array[UInt16]
arr
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
index])
1734 } 1735
(Array[Run]) -> Container
RunContainer
(
Array[Run]
runs
) => {
1736 let
Array[UInt16]
arr
=
(runs : Array[Run]) -> Array[UInt16]
run_container_to_array
(
Array[Run]
runs
)
1737
(UInt16) -> UInt16?
Some
(
Array[UInt16]
arr
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
index])
1738 } 1739 } 1740} 1741 1742///| 查找容器中指定值的排名(从0开始) 1743pub fn
(container : Container, value : UInt16) -> Int

查找容器中指定值的排名(从0开始)

container_rank
(
Container
container
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
,
UInt16
value
:
UInt16
UInt16
) ->
Int
Int
{
1744 match
Container
container
{
1745
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr
) =>
(arr : Array[UInt16], value : UInt16) -> Int

数组容器中查找值的排名

array_container_rank
(
Array[UInt16]
arr
,
UInt16
value
)
1746
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap
) =>
(bitmap : FixedArray[UInt64], value : UInt16) -> Int

位图容器中查找值的排名

bitmap_container_rank
(
FixedArray[UInt64]
bitmap
,
UInt16
value
)
1747
(Array[Run]) -> Container
RunContainer
(
Array[Run]
runs
) =>
(runs : Array[Run], value : UInt16) -> Int

运行容器中查找值的排名

run_container_rank
(
Array[Run]
runs
,
UInt16
value
)
1748 } 1749} 1750 1751///| 数组容器中查找值的排名 1752fn
(arr : Array[UInt16], value : UInt16) -> Int

数组容器中查找值的排名

array_container_rank
(
Array[UInt16]
arr
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
],
UInt16
value
:
UInt16
UInt16
) ->
Int
Int
{
1753 let mut
Int
rank
= 0
1754 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1755 if
Array[UInt16]
arr
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
(self_ : UInt16, other : UInt16) -> Bool
<
UInt16
value
{
1756
Int
rank
=
Int
rank
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
1757 } else { 1758 break 1759 } 1760 } 1761
Int
rank
1762} 1763 1764///| 位图容器中查找值的排名 1765fn
(bitmap : FixedArray[UInt64], value : UInt16) -> Int

位图容器中查找值的排名

bitmap_container_rank
(
FixedArray[UInt64]
bitmap
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
],
UInt16
value
:
UInt16
UInt16
) ->
Int
Int
{
1766 let
Int
word_index
=
UInt16
value
.
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
()
(self : Int, other : Int) -> Int

Performs integer division between two 32-bit integers. The result is truncated towards zero (rounds down for positive numbers and up for negative numbers).

Parameters:

  • dividend : The first integer operand to be divided.
  • divisor : The second integer operand that divides the dividend.

Returns the quotient of the division operation.

Throws a panic if divisor is zero.

Example:

  inspect(10 / 3, content="3") // truncates towards zero
  inspect(-10 / 3, content="-3")
  inspect(10 / -3, content="-3")
/
64
1767 let
Int
bit_index
=
UInt16
value
.
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
()
(self : Int, other : Int) -> Int

Calculates the remainder of dividing one integer by another. The result follows the formula dividend - (dividend / divisor) * divisor, maintaining the same sign as the dividend.

Parameters:

  • self : The dividend (the number being divided).
  • other : The divisor (the number to divide by).

Returns the remainder of the division. If other is 0, the behavior is undefined.

Example:

  inspect(7 % 3, content="1")
  inspect(-7 % 3, content="-1")
  inspect(7 % -3, content="1")
%
64
1768 let mut
Int
rank
= 0
1769 1770 // 计算前面完整字的位数 1771 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Int
word_index
;
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1772
Int
rank
=
Int
rank
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
(value : UInt64) -> Int

计算64位整数的人口计数(置位数)

popcount
(
FixedArray[UInt64]
bitmap
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
i])
1773 } 1774 1775 // 计算当前字中前面位的位数 1776 let
UInt64
current_word
=
FixedArray[UInt64]
bitmap
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
word_index]
1777 let
UInt64
mask
= (1UL
(self : UInt64, shift : Int) -> UInt64

Performs a bitwise left shift operation on an unsigned 64-bit integer.

Parameters:

  • self : The unsigned 64-bit integer to be shifted.
  • shift : The number of positions to shift the bits to the left. Must be between 0 and 63 inclusive, values outside this range will be masked with & 63.

Returns the result of shifting self left by shift positions. For each position shifted, the rightmost bit is filled with 0, and the leftmost bit is discarded.

Example:

  let x = 1UL
  inspect(x << 3, content="8") // 1 shifted left by 3 positions becomes 8
  inspect(x << 63, content="9223372036854775808") // 1 shifted left by 63 positions
<<
Int
bit_index
)
(self : UInt64, other : UInt64) -> UInt64

Performs subtraction between two unsigned 64-bit integers. When the result would be negative, the function wraps around using modular arithmetic (2^64).

Parameters:

  • self : The first unsigned 64-bit integer (minuend).
  • other : The second unsigned 64-bit integer to subtract from the first (subtrahend).

Returns the difference between the two numbers, wrapped around if necessary.

Example:

  let a = 5UL
  let b = 3UL
  inspect(a - b, content="2")
  let c = 3UL
  let d = 5UL
  inspect(c - d, content="18446744073709551614") // wraps around to 2^64 - 2
-
1UL
1778
Int
rank
=
Int
rank
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
(value : UInt64) -> Int

计算64位整数的人口计数(置位数)

popcount
(
UInt64
current_word
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise AND operation between two unsigned 64-bit integers.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns the result of performing a bitwise AND operation on the two operands.

Example:

  let a = 0xF0F0F0F0F0F0F0F0UL
  let b = 0xFF00FF00FF00FF00UL
  inspect(a & b, content="17294086455919964160") // 0xF000F000F000F000
&
UInt64
mask
)
1779
Int
rank
1780} 1781 1782///| 运行容器中查找值的排名 1783fn
(runs : Array[Run], value : UInt16) -> Int

运行容器中查找值的排名

run_container_rank
(
Array[Run]
runs
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
],
UInt16
value
:
UInt16
UInt16
) ->
Int
Int
{
1784 let mut
Int
rank
= 0
1785 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[Run]
runs
.
(self : Array[Run]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1786 let
Run
run
=
Array[Run]
runs
(Array[Run], Int) -> Run

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
1787 let
UInt16
run_end
=
Run
run
.
UInt16
start
(self : UInt16, that : UInt16) -> UInt16
+
Run
run
.
UInt16
length
1788 if
UInt16
value
(self_ : UInt16, other : UInt16) -> Bool
<
Run
run
.
UInt16
start
{
1789 // 值在当前运行段之前 1790 break 1791 } else if
UInt16
value
(self_ : UInt16, other : UInt16) -> Bool
>
UInt16
run_end
{
1792 // 值在当前运行段之后,加上整个运行段的长度 1793
Int
rank
=
Int
rank
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
Run
run
.
UInt16
length
.
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
()
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
1794 } else { 1795 // 值在当前运行段内 1796
Int
rank
=
Int
rank
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
(
UInt16
value
(self : UInt16, that : UInt16) -> UInt16
-
Run
run
.
UInt16
start
).
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
()
1797 break 1798 } 1799 } 1800
Int
rank
1801} 1802 1803///| 检查是否为子集 1804pub fn
(self : RoaringBitmap, other : RoaringBitmap) -> Bool

检查是否为子集

is_subset
(
RoaringBitmap
self
:
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
,
RoaringBitmap
other
:
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
) ->
Bool
Bool
{
1805 if
RoaringBitmap
self
.
Int
size
(self_ : Int, other : Int) -> Bool
>
RoaringBitmap
other
.
Int
size
{
1806 return false 1807 } 1808 1809 // 逐容器检查 1810 let mut
Int
i
= 0 // self的容器索引
1811 let mut
Int
j
= 0 // other的容器索引
1812 while
Int
i
(self_ : Int, other : Int) -> Bool
<
RoaringBitmap
self
.
Array[HighContainer]
containers
.
(self : Array[HighContainer]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(Bool, Bool) -> Bool
&&
Int
j
(self_ : Int, other : Int) -> Bool
<
RoaringBitmap
other
.
Array[HighContainer]
containers
.
(self : Array[HighContainer]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
1813 let
HighContainer
self_container
=
RoaringBitmap
self
.
Array[HighContainer]
containers
(Array[HighContainer], Int) -> HighContainer

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
1814 let
HighContainer
other_container
=
RoaringBitmap
other
.
Array[HighContainer]
containers
(Array[HighContainer], Int) -> HighContainer

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j]
1815 if
HighContainer
self_container
.
UInt16
key
(self_ : UInt16, other : UInt16) -> Bool
<
HighContainer
other_container
.
UInt16
key
{
1816 // self有一个容器在other中不存在,不是子集 1817 return false 1818 } else if
HighContainer
self_container
.
UInt16
key
(self_ : UInt16, other : UInt16) -> Bool
>
HighContainer
other_container
.
UInt16
key
{
1819 // other的这个容器不包含self的元素,继续 1820
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
1821 } else { 1822 // 相同的高16位,检查容器是否为子集 1823 if
(x : Bool) -> Bool

Performs logical negation on a boolean value.

Parameters:

  • value : The boolean value to negate.

Returns the logical NOT of the input value: true if the input is false, and false if the input is true.

Example:

  inspect(not(true), content="false")
  inspect(not(false), content="true")
not
(
1824
(container1 : Container, container2 : Container) -> Bool

检查容器是否为子集

container_is_subset
(
1825
HighContainer
self_container
.
Container
container
,
1826
HighContainer
other_container
.
Container
container
,
1827 ), 1828 ) { 1829 return false 1830 } 1831
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
1832
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
1833 } 1834 } 1835 1836 // 如果self还有剩余容器,说明不是子集 1837
Int
i
(self_ : Int, other : Int) -> Bool
>=
RoaringBitmap
self
.
Array[HighContainer]
containers
.
(self : Array[HighContainer]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
1838} 1839 1840///| 检查容器是否为子集 1841fn
(container1 : Container, container2 : Container) -> Bool

检查容器是否为子集

container_is_subset
(
Container
container1
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
,
Container
container2
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
) ->
Bool
Bool
{
1842 match (
Container
container1
,
Container
container2
) {
1843 (
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr1
),
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr2
)) =>
1844
(arr1 : Array[UInt16], arr2 : Array[UInt16]) -> Bool

数组容器子集检查

array_container_is_subset
(
Array[UInt16]
arr1
,
Array[UInt16]
arr2
)
1845 (
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr
),
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap
)) =>
1846
(arr : Array[UInt16], bitmap : FixedArray[UInt64]) -> Bool

数组是否为位图的子集

array_subset_of_bitmap
(
Array[UInt16]
arr
,
FixedArray[UInt64]
bitmap
)
1847 (
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap1
),
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap2
)) =>
1848
(bitmap1 : FixedArray[UInt64], bitmap2 : FixedArray[UInt64]) -> Bool

位图容器子集检查

bitmap_container_is_subset
(
FixedArray[UInt64]
bitmap1
,
FixedArray[UInt64]
bitmap2
)
1849 (
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap
),
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr
)) =>
1850
(bitmap : FixedArray[UInt64], arr : Array[UInt16]) -> Bool

位图是否为数组的子集

bitmap_subset_of_array
(
FixedArray[UInt64]
bitmap
,
Array[UInt16]
arr
)
1851 (
(Array[Run]) -> Container
RunContainer
(
Array[Run]
runs1
),
(Array[Run]) -> Container
RunContainer
(
Array[Run]
runs2
)) =>
1852
(runs1 : Array[Run], runs2 : Array[Run]) -> Bool

运行容器子集检查

run_container_is_subset
(
Array[Run]
runs1
,
Array[Run]
runs2
)
1853 _ => { 1854 // 混合类型,转换为数组后比较 1855 let
Array[UInt16]
arr1
=
(container : Container) -> Array[UInt16]

容器转换为数组

container_to_array
(
Container
container1
)
1856 let
Array[UInt16]
arr2
=
(container : Container) -> Array[UInt16]

容器转换为数组

container_to_array
(
Container
container2
)
1857
(arr1 : Array[UInt16], arr2 : Array[UInt16]) -> Bool

数组容器子集检查

array_container_is_subset
(
Array[UInt16]
arr1
,
Array[UInt16]
arr2
)
1858 } 1859 } 1860} 1861 1862///| 数组容器子集检查 1863fn
(arr1 : Array[UInt16], arr2 : Array[UInt16]) -> Bool

数组容器子集检查

array_container_is_subset
(
1864
Array[UInt16]
arr1
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
],
1865
Array[UInt16]
arr2
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
]
1866) ->
Bool
Bool
{
1867 if
Array[UInt16]
arr1
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self_ : Int, other : Int) -> Bool
>
Array[UInt16]
arr2
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
1868 return false 1869 } 1870 let mut
Int
i
= 0
1871 let mut
Int
j
= 0
1872 while
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr1
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(Bool, Bool) -> Bool
&&
Int
j
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr2
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
1873 if
Array[UInt16]
arr1
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
(self_ : UInt16, other : UInt16) -> Bool
<
Array[UInt16]
arr2
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j] {
1874 // arr1中的元素在arr2中不存在 1875 return false 1876 } else if
Array[UInt16]
arr1
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
(self_ : UInt16, other : UInt16) -> Bool
>
Array[UInt16]
arr2
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j] {
1877
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
1878 } else { 1879 // 相等,继续 1880
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
1881
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
1882 } 1883 } 1884 1885 // 如果arr1还有剩余元素,说明不是子集 1886
Int
i
(self_ : Int, other : Int) -> Bool
>=
Array[UInt16]
arr1
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
1887} 1888 1889///| 数组是否为位图的子集 1890fn
(arr : Array[UInt16], bitmap : FixedArray[UInt64]) -> Bool

数组是否为位图的子集

array_subset_of_bitmap
(
1891
Array[UInt16]
arr
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
],
1892
FixedArray[UInt64]
bitmap
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
]
1893) ->
Bool
Bool
{
1894 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1895 if
(x : Bool) -> Bool

Performs logical negation on a boolean value.

Parameters:

  • value : The boolean value to negate.

Returns the logical NOT of the input value: true if the input is false, and false if the input is true.

Example:

  inspect(not(true), content="false")
  inspect(not(false), content="true")
not
(
(bitmap : FixedArray[UInt64], value : UInt16) -> Bool

位图容器包含检查

bitmap_container_contains
(
FixedArray[UInt64]
bitmap
,
Array[UInt16]
arr
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i])) {
1896 return false 1897 } 1898 } 1899 true 1900} 1901 1902///| 位图容器子集检查 1903fn
(bitmap1 : FixedArray[UInt64], bitmap2 : FixedArray[UInt64]) -> Bool

位图容器子集检查

bitmap_container_is_subset
(
1904
FixedArray[UInt64]
bitmap1
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
],
1905
FixedArray[UInt64]
bitmap2
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
]
1906) ->
Bool
Bool
{
1907 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
1024;
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
1908 // bitmap1的每一位都必须在bitmap2中存在 1909 if (
FixedArray[UInt64]
bitmap1
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
i]
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise AND operation between two unsigned 64-bit integers.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns the result of performing a bitwise AND operation on the two operands.

Example:

  let a = 0xF0F0F0F0F0F0F0F0UL
  let b = 0xFF00FF00FF00FF00UL
  inspect(a & b, content="17294086455919964160") // 0xF000F000F000F000
&
FixedArray[UInt64]
bitmap2
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
i])
(x : UInt64, y : UInt64) -> Bool
!=
FixedArray[UInt64]
bitmap1
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
i] {
1910 return false 1911 } 1912 } 1913 true 1914} 1915 1916///| 位图是否为数组的子集 1917fn
(bitmap : FixedArray[UInt64], arr : Array[UInt16]) -> Bool

位图是否为数组的子集

bitmap_subset_of_array
(
1918
FixedArray[UInt64]
bitmap
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
],
1919
Array[UInt16]
arr
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
]
1920) ->
Bool
Bool
{
1921 // 将位图转换为数组,然后检查 1922 let
Array[UInt16]
bitmap_arr
=
(bitmap : FixedArray[UInt64]) -> Array[UInt16]

位图容器转数组

bitmap_container_to_array
(
FixedArray[UInt64]
bitmap
)
1923
(arr1 : Array[UInt16], arr2 : Array[UInt16]) -> Bool

数组容器子集检查

array_container_is_subset
(
Array[UInt16]
bitmap_arr
,
Array[UInt16]
arr
)
1924} 1925 1926///| 运行容器子集检查 1927fn
(runs1 : Array[Run], runs2 : Array[Run]) -> Bool

运行容器子集检查

run_container_is_subset
(
Array[Run]
runs1
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
],
Array[Run]
runs2
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
]) ->
Bool
Bool
{
1928 // 简化实现:转换为数组后比较 1929 let
Array[UInt16]
arr1
=
(runs : Array[Run]) -> Array[UInt16]
run_container_to_array
(
Array[Run]
runs1
)
1930 let
Array[UInt16]
arr2
=
(runs : Array[Run]) -> Array[UInt16]
run_container_to_array
(
Array[Run]
runs2
)
1931
(arr1 : Array[UInt16], arr2 : Array[UInt16]) -> Bool

数组容器子集检查

array_container_is_subset
(
Array[UInt16]
arr1
,
Array[UInt16]
arr2
)
1932} 1933 1934///| 对称差集 (XOR) - 在两个集合中但不在交集中的元素 1935pub fn
(self : RoaringBitmap, other : RoaringBitmap) -> RoaringBitmap

对称差集 (XOR) - 在两个集合中但不在交集中的元素

xor
(
RoaringBitmap
self
:
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
,
RoaringBitmap
other
:
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
) ->
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
{
1936 if
RoaringBitmap
self
.
(self : RoaringBitmap) -> Bool

检查bitmap是否为空

is_empty
() {
1937 return
RoaringBitmap
other
1938 } 1939 if
RoaringBitmap
other
.
(self : RoaringBitmap) -> Bool

检查bitmap是否为空

is_empty
() {
1940 return
RoaringBitmap
self
1941 } 1942 let
Array[HighContainer]
result_containers
= []
1943 let mut
Int
i
= 0
1944 let mut
Int
j
= 0
1945 let mut
Int
total_size
= 0
1946 while
Int
i
(self_ : Int, other : Int) -> Bool
<
RoaringBitmap
self
.
Array[HighContainer]
containers
.
(self : Array[HighContainer]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(Bool, Bool) -> Bool
&&
Int
j
(self_ : Int, other : Int) -> Bool
<
RoaringBitmap
other
.
Array[HighContainer]
containers
.
(self : Array[HighContainer]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
1947 let
HighContainer
container1
=
RoaringBitmap
self
.
Array[HighContainer]
containers
(Array[HighContainer], Int) -> HighContainer

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
1948 let
HighContainer
container2
=
RoaringBitmap
other
.
Array[HighContainer]
containers
(Array[HighContainer], Int) -> HighContainer

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j]
1949 if
HighContainer
container1
.
UInt16
key
(self_ : UInt16, other : UInt16) -> Bool
<
HighContainer
container2
.
UInt16
key
{
1950 // 只在self中存在 1951
Array[HighContainer]
result_containers
.
(self : Array[HighContainer], value : HighContainer) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
HighContainer
container1
)
1952
Int
total_size
=
Int
total_size
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
HighContainer
container1
.
Int
cardinality
1953
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
1954 } else if
HighContainer
container1
.
UInt16
key
(self_ : UInt16, other : UInt16) -> Bool
>
HighContainer
container2
.
UInt16
key
{
1955 // 只在other中存在 1956
Array[HighContainer]
result_containers
.
(self : Array[HighContainer], value : HighContainer) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
HighContainer
container2
)
1957
Int
total_size
=
Int
total_size
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
HighContainer
container2
.
Int
cardinality
1958
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
1959 } else { 1960 // 相同key,计算XOR 1961 let
Container
xor_container
=
(container1 : Container, container2 : Container) -> Container

容器XOR操作

container_xor
(
1962
HighContainer
container1
.
Container
container
,
1963
HighContainer
container2
.
Container
container
,
1964 ) 1965 let
Int
xor_cardinality
=
(container : Container) -> Int

获取容器基数

container_cardinality
(
Container
xor_container
)
1966 if
Int
xor_cardinality
(self_ : Int, other : Int) -> Bool
>
0 {
1967 let
HighContainer
xor_high_container
=
struct HighContainer {
  key: UInt16
  container: Container
  cardinality: Int
}

高级容器 - 处理 32 位整数的高 16 位

HighContainer
::{
1968
UInt16
key
:
HighContainer
container1
.
UInt16
key
,
1969
Container
container
:
Container
xor_container
,
1970
Int
cardinality
:
Int
xor_cardinality
,
1971 } 1972
Array[HighContainer]
result_containers
.
(self : Array[HighContainer], value : HighContainer) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
HighContainer
xor_high_container
)
1973
Int
total_size
=
Int
total_size
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
Int
xor_cardinality
1974 } 1975
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
1976
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
1977 } 1978 } 1979 1980 // 添加剩余的容器 1981 while
Int
i
(self_ : Int, other : Int) -> Bool
<
RoaringBitmap
self
.
Array[HighContainer]
containers
.
(self : Array[HighContainer]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
1982 let
HighContainer
container
=
RoaringBitmap
self
.
Array[HighContainer]
containers
(Array[HighContainer], Int) -> HighContainer

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
1983
Array[HighContainer]
result_containers
.
(self : Array[HighContainer], value : HighContainer) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
HighContainer
container
)
1984
Int
total_size
=
Int
total_size
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
HighContainer
container
.
Int
cardinality
1985
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
1986 } 1987 while
Int
j
(self_ : Int, other : Int) -> Bool
<
RoaringBitmap
other
.
Array[HighContainer]
containers
.
(self : Array[HighContainer]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
1988 let
HighContainer
container
=
RoaringBitmap
other
.
Array[HighContainer]
containers
(Array[HighContainer], Int) -> HighContainer

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j]
1989
Array[HighContainer]
result_containers
.
(self : Array[HighContainer], value : HighContainer) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
HighContainer
container
)
1990
Int
total_size
=
Int
total_size
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
HighContainer
container
.
Int
cardinality
1991
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
1992 } 1993
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
::{
Array[HighContainer]
containers
:
Array[HighContainer]
result_containers
,
Int
size
:
Int
total_size
}
1994} 1995 1996///| 容器XOR操作 1997fn
(container1 : Container, container2 : Container) -> Container

容器XOR操作

container_xor
(
Container
container1
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
,
Container
container2
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
) ->
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
{
1998 match (
Container
container1
,
Container
container2
) {
1999 (
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr1
),
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr2
)) =>
2000
(Array[UInt16]) -> Container
ArrayContainer
(
(arr1 : Array[UInt16], arr2 : Array[UInt16]) -> Array[UInt16]

数组容器XOR

array_container_xor
(
Array[UInt16]
arr1
,
Array[UInt16]
arr2
))
2001 (
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr
),
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap
)) =>
2002
(arr : Array[UInt16], bitmap : FixedArray[UInt64]) -> Container

数组与位图容器XOR

array_xor_bitmap_container
(
Array[UInt16]
arr
,
FixedArray[UInt64]
bitmap
)
2003 (
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap
),
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr
)) =>
2004
(arr : Array[UInt16], bitmap : FixedArray[UInt64]) -> Container

数组与位图容器XOR

array_xor_bitmap_container
(
Array[UInt16]
arr
,
FixedArray[UInt64]
bitmap
)
2005 (
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap1
),
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap2
)) => {
2006 let
FixedArray[UInt64]
xor_bitmap
=
(bitmap1 : FixedArray[UInt64], bitmap2 : FixedArray[UInt64]) -> FixedArray[UInt64]

位图容器XOR

bitmap_container_xor
(
FixedArray[UInt64]
bitmap1
,
FixedArray[UInt64]
bitmap2
)
2007 let
Int
cardinality
=
(bitmap : FixedArray[UInt64]) -> Int

计算位图容器基数

bitmap_container_cardinality
(
FixedArray[UInt64]
xor_bitmap
)
2008 if
Int
cardinality
(self_ : Int, other : Int) -> Bool
<
Int

容器配置常量

ARRAY_CONTAINER_MAX_SIZE
{
2009
(bitmap : FixedArray[UInt64]) -> Container

位图容器转换为数组容器

bitmap_to_array_container
(
FixedArray[UInt64]
xor_bitmap
)
2010 } else { 2011
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
xor_bitmap
)
2012 } 2013 } 2014 (
(Array[Run]) -> Container
RunContainer
(
Array[Run]
runs1
),
(Array[Run]) -> Container
RunContainer
(
Array[Run]
runs2
)) =>
2015
(Array[Run]) -> Container
RunContainer
(
(runs1 : Array[Run], runs2 : Array[Run]) -> Array[Run]

运行容器XOR(简化实现)

run_container_xor
(
Array[Run]
runs1
,
Array[Run]
runs2
))
2016 _ =>
(container1 : Container, container2 : Container) -> Container

容器XOR通用实现

container_xor_generic
(
Container
container1
,
Container
container2
)
2017 } 2018} 2019 2020///| 数组容器XOR 2021fn
(arr1 : Array[UInt16], arr2 : Array[UInt16]) -> Array[UInt16]

数组容器XOR

array_container_xor
(
2022
Array[UInt16]
arr1
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
],
2023
Array[UInt16]
arr2
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
]
2024) ->
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
] {
2025 let
Array[UInt16]
result
= []
2026 let mut
Int
i
= 0
2027 let mut
Int
j
= 0
2028 while
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr1
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(Bool, Bool) -> Bool
&&
Int
j
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr2
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
2029 let
UInt16
val1
=
Array[UInt16]
arr1
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
2030 let
UInt16
val2
=
Array[UInt16]
arr2
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j]
2031 if
UInt16
val1
(self_ : UInt16, other : UInt16) -> Bool
<
UInt16
val2
{
2032
Array[UInt16]
result
.
(self : Array[UInt16], value : UInt16) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
UInt16
val1
) // 只在arr1中
2033
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
2034 } else if
UInt16
val1
(self_ : UInt16, other : UInt16) -> Bool
>
UInt16
val2
{
2035
Array[UInt16]
result
.
(self : Array[UInt16], value : UInt16) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
UInt16
val2
) // 只在arr2中
2036
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
2037 } else { 2038 // 相等,跳过(XOR中相同元素抵消) 2039
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
2040
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
2041 } 2042 } 2043 2044 // 添加剩余元素 2045 while
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr1
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
2046
Array[UInt16]
result
.
(self : Array[UInt16], value : UInt16) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Array[UInt16]
arr1
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i])
2047
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
2048 } 2049 while
Int
j
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr2
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
2050
Array[UInt16]
result
.
(self : Array[UInt16], value : UInt16) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Array[UInt16]
arr2
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j])
2051
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
2052 } 2053
Array[UInt16]
result
2054} 2055 2056///| 数组与位图容器XOR 2057fn
(arr : Array[UInt16], bitmap : FixedArray[UInt64]) -> Container

数组与位图容器XOR

array_xor_bitmap_container
(
2058
Array[UInt16]
arr
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
],
2059
FixedArray[UInt64]
bitmap
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
]
2060) ->
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
{
2061 let
FixedArray[UInt64]
result_bitmap
=
type FixedArray[A]
FixedArray
::
(len : Int, init : UInt64) -> FixedArray[UInt64]

Creates a new fixed-size array with the specified length, initializing all elements with the given value.

Parameters:

  • length : The length of the array to create. Must be non-negative.
  • initial_value : The value used to initialize all elements in the array.

Returns a new fixed-size array of type FixedArray[T] with length elements, where each element is initialized to initial_value.

Throws a panic if length is negative.

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[0], content="42")
  inspect(arr.length(), content="3")

WARNING: A common pitfall is creating with the same initial value, for example:

  let two_dimension_array = FixedArray::make(10, FixedArray::make(10, 0))
  two_dimension_array[0][5] = 10
  assert_eq(two_dimension_array[5][5], 10)

This is because all the cells reference to the same object (the FixedArray[Int] in this case). One should use makei() instead which creates an object for each index.

make
(1024, 0UL)
2062 2063 // 复制原位图 2064 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
1024;
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
2065
FixedArray[UInt64]
result_bitmap
(FixedArray[UInt64], Int, UInt64) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
i] =
FixedArray[UInt64]
bitmap
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
i]
2066 } 2067 2068 // 对数组中的每个元素执行XOR 2069 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
2070 let
UInt16
value
=
Array[UInt16]
arr
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
2071 let
Int
word_index
=
UInt16
value
.
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
()
(self : Int, other : Int) -> Int

Performs integer division between two 32-bit integers. The result is truncated towards zero (rounds down for positive numbers and up for negative numbers).

Parameters:

  • dividend : The first integer operand to be divided.
  • divisor : The second integer operand that divides the dividend.

Returns the quotient of the division operation.

Throws a panic if divisor is zero.

Example:

  inspect(10 / 3, content="3") // truncates towards zero
  inspect(-10 / 3, content="-3")
  inspect(10 / -3, content="-3")
/
64
2072 let
Int
bit_index
=
UInt16
value
.
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
()
(self : Int, other : Int) -> Int

Calculates the remainder of dividing one integer by another. The result follows the formula dividend - (dividend / divisor) * divisor, maintaining the same sign as the dividend.

Parameters:

  • self : The dividend (the number being divided).
  • other : The divisor (the number to divide by).

Returns the remainder of the division. If other is 0, the behavior is undefined.

Example:

  inspect(7 % 3, content="1")
  inspect(-7 % 3, content="-1")
  inspect(7 % -3, content="1")
%
64
2073 let
UInt64
bit_mask
= 1UL
(self : UInt64, shift : Int) -> UInt64

Performs a bitwise left shift operation on an unsigned 64-bit integer.

Parameters:

  • self : The unsigned 64-bit integer to be shifted.
  • shift : The number of positions to shift the bits to the left. Must be between 0 and 63 inclusive, values outside this range will be masked with & 63.

Returns the result of shifting self left by shift positions. For each position shifted, the rightmost bit is filled with 0, and the leftmost bit is discarded.

Example:

  let x = 1UL
  inspect(x << 3, content="8") // 1 shifted left by 3 positions becomes 8
  inspect(x << 63, content="9223372036854775808") // 1 shifted left by 63 positions
<<
Int
bit_index
2074
FixedArray[UInt64]
result_bitmap
(FixedArray[UInt64], Int, UInt64) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
word_index] =
FixedArray[UInt64]
result_bitmap
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
word_index]
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise XOR operation between two unsigned 64-bit integers. For each bit position, the result is 1 if the bits at that position in the two operands are different, and 0 if they are the same.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns a new UInt64 value representing the bitwise XOR of the two operands.

Example:

  let a = 0xF0F0F0F0UL
  let b = 0x0F0F0F0FUL
  inspect(a ^ b, content="4294967295") // 0xFFFFFFFF
^
UInt64
bit_mask
2075 } 2076 let
Int
cardinality
=
(bitmap : FixedArray[UInt64]) -> Int

计算位图容器基数

bitmap_container_cardinality
(
FixedArray[UInt64]
result_bitmap
)
2077 if
Int
cardinality
(self_ : Int, other : Int) -> Bool
<
Int

容器配置常量

ARRAY_CONTAINER_MAX_SIZE
{
2078
(bitmap : FixedArray[UInt64]) -> Container

位图容器转换为数组容器

bitmap_to_array_container
(
FixedArray[UInt64]
result_bitmap
)
2079 } else { 2080
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
result_bitmap
)
2081 } 2082} 2083 2084///| 位图容器XOR 2085fn
(bitmap1 : FixedArray[UInt64], bitmap2 : FixedArray[UInt64]) -> FixedArray[UInt64]

位图容器XOR

bitmap_container_xor
(
2086
FixedArray[UInt64]
bitmap1
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
],
2087
FixedArray[UInt64]
bitmap2
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
]
2088) ->
type FixedArray[A]
FixedArray
[
UInt64
UInt64
] {
2089 let
FixedArray[UInt64]
result
=
type FixedArray[A]
FixedArray
::
(len : Int, init : UInt64) -> FixedArray[UInt64]

Creates a new fixed-size array with the specified length, initializing all elements with the given value.

Parameters:

  • length : The length of the array to create. Must be non-negative.
  • initial_value : The value used to initialize all elements in the array.

Returns a new fixed-size array of type FixedArray[T] with length elements, where each element is initialized to initial_value.

Throws a panic if length is negative.

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[0], content="42")
  inspect(arr.length(), content="3")

WARNING: A common pitfall is creating with the same initial value, for example:

  let two_dimension_array = FixedArray::make(10, FixedArray::make(10, 0))
  two_dimension_array[0][5] = 10
  assert_eq(two_dimension_array[5][5], 10)

This is because all the cells reference to the same object (the FixedArray[Int] in this case). One should use makei() instead which creates an object for each index.

make
(1024, 0UL)
2090 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
1024;
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
2091
FixedArray[UInt64]
result
(FixedArray[UInt64], Int, UInt64) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
i] =
FixedArray[UInt64]
bitmap1
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
i]
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise XOR operation between two unsigned 64-bit integers. For each bit position, the result is 1 if the bits at that position in the two operands are different, and 0 if they are the same.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns a new UInt64 value representing the bitwise XOR of the two operands.

Example:

  let a = 0xF0F0F0F0UL
  let b = 0x0F0F0F0FUL
  inspect(a ^ b, content="4294967295") // 0xFFFFFFFF
^
FixedArray[UInt64]
bitmap2
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
i]
2092 } 2093
FixedArray[UInt64]
result
2094} 2095 2096///| 运行容器XOR(简化实现) 2097fn
(runs1 : Array[Run], runs2 : Array[Run]) -> Array[Run]

运行容器XOR(简化实现)

run_container_xor
(
Array[Run]
runs1
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
],
Array[Run]
runs2
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
]) ->
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
] {
2098 // 简化实现:转换为数组后计算XOR 2099 let
Array[UInt16]
arr1
=
(runs : Array[Run]) -> Array[UInt16]
run_container_to_array
(
Array[Run]
runs1
)
2100 let
Array[UInt16]
arr2
=
(runs : Array[Run]) -> Array[UInt16]
run_container_to_array
(
Array[Run]
runs2
)
2101 let
Array[UInt16]
xor_arr
=
(arr1 : Array[UInt16], arr2 : Array[UInt16]) -> Array[UInt16]

数组容器XOR

array_container_xor
(
Array[UInt16]
arr1
,
Array[UInt16]
arr2
)
2102 2103 // 转换回运行段格式 2104 if
(arr : Array[UInt16]) -> Bool

判断是否应该使用运行长度编码容器

should_use_run_container
(
Array[UInt16]
xor_arr
) {
2105 match
(arr : Array[UInt16]) -> Container

数组容器转换为运行长度编码容器(简化实现)

array_to_run_container
(
Array[UInt16]
xor_arr
) {
2106
(Array[Run]) -> Container
RunContainer
(
Array[Run]
runs
) =>
Array[Run]
runs
2107 _ => [] // 不应该发生 2108 } 2109 } else { 2110 // 如果不适合运行长度编码,返回空运行段 2111 [] 2112 } 2113} 2114 2115///| 容器XOR通用实现 2116fn
(container1 : Container, container2 : Container) -> Container

容器XOR通用实现

container_xor_generic
(
2117
Container
container1
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
,
2118
Container
container2
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
2119) ->
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
{
2120 let
Array[UInt16]
arr1
=
(container : Container) -> Array[UInt16]

容器转换为数组

container_to_array
(
Container
container1
)
2121 let
Array[UInt16]
arr2
=
(container : Container) -> Array[UInt16]

容器转换为数组

container_to_array
(
Container
container2
)
2122 let
Array[UInt16]
xor_arr
=
(arr1 : Array[UInt16], arr2 : Array[UInt16]) -> Array[UInt16]

数组容器XOR

array_container_xor
(
Array[UInt16]
arr1
,
Array[UInt16]
arr2
)
2123 if
Array[UInt16]
xor_arr
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self_ : Int, other : Int) -> Bool
>=
Int

容器配置常量

ARRAY_CONTAINER_MAX_SIZE
{
2124
(arr : Array[UInt16]) -> Container

数组容器转换为位图容器

array_to_bitmap_container
(
Array[UInt16]
xor_arr
)
2125 } else { 2126
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
xor_arr
)
2127 } 2128} 2129 2130///| 批量添加元素(高性能版本) 2131pub fn
(self : RoaringBitmap, values : Array[UInt]) -> RoaringBitmap

批量添加元素(高性能版本)

add_many
(
RoaringBitmap
self
:
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
,
Array[UInt]
values
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt
UInt
]) ->
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
{
2132 if
Array[UInt]
values
.
(self : Array[UInt]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
0 {
2133 return
RoaringBitmap
self
2134 } 2135 2136 // 按高16位分组 2137 let
Array[(UInt16, Array[UInt16])]
groups
= []
2138 let mut
UInt16?
current_high
=
UInt16?
None
2139 let mut
Array[UInt16]
current_lows
= []
2140 2141 // 先对values按值排序(简化分组逻辑) 2142 let
Array[UInt]
sorted_values
=
(arr : Array[UInt]) -> Array[UInt]

UInt数组排序

sort_uint_array
(
Array[UInt]
values
)
2143 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt]
sorted_values
.
(self : Array[UInt]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
2144 let
UInt
value
=
Array[UInt]
sorted_values
(Array[UInt], Int) -> UInt

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
2145 let
UInt16
high
=
(value : UInt) -> UInt16

提取32位整数的高16位

extract_high_16
(
UInt
value
)
2146 let
UInt16
low
=
(value : UInt) -> UInt16

提取32位整数的低16位

extract_low_16
(
UInt
value
)
2147 match
UInt16?
current_high
{
2148
UInt16?
None
=> {
2149
UInt16?
current_high
=
(UInt16) -> UInt16?
Some
(
UInt16
high
)
2150
Array[UInt16]
current_lows
= [
UInt16
low
]
2151 } 2152
(UInt16) -> UInt16?
Some
(
UInt16
prev_high
) =>
2153 if
UInt16
high
(self : UInt16, that : UInt16) -> Bool
==
UInt16
prev_high
{
2154 // 同一组,避免重复 2155 if
Array[UInt16]
current_lows
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self : Int, other : Int) -> Bool

Compares two integers for equality.

Parameters:

  • self : The first integer to compare.
  • other : The second integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  inspect(42 == 42, content="true")
  inspect(42 == -42, content="false")
==
0
(Bool, Bool) -> Bool
||
2156
Array[UInt16]
current_lows
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
current_lows.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
-
1]
(x : UInt16, y : UInt16) -> Bool
!=
UInt16
low
{
2157
Array[UInt16]
current_lows
.
(self : Array[UInt16], value : UInt16) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
UInt16
low
)
2158 } 2159 } else { 2160 // 新组,保存当前组 2161
Array[(UInt16, Array[UInt16])]
groups
.
(self : Array[(UInt16, Array[UInt16])], value : (UInt16, Array[UInt16])) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
((
UInt16
prev_high
,
Array[UInt16]
current_lows
))
2162
UInt16?
current_high
=
(UInt16) -> UInt16?
Some
(
UInt16
high
)
2163
Array[UInt16]
current_lows
= [
UInt16
low
]
2164 } 2165 } 2166 } 2167 2168 // 保存最后一组 2169 match
UInt16?
current_high
{
2170
(UInt16) -> UInt16?
Some
(
UInt16
high
) =>
Array[(UInt16, Array[UInt16])]
groups
.
(self : Array[(UInt16, Array[UInt16])], value : (UInt16, Array[UInt16])) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
((
UInt16
high
,
Array[UInt16]
current_lows
))
2171
UInt16?
None
=> ()
2172 } 2173 2174 // 批量处理每个组 2175 let mut
RoaringBitmap
result
=
RoaringBitmap
self
2176 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[(UInt16, Array[UInt16])]
groups
.
(self : Array[(UInt16, Array[UInt16])]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
2177 let (
UInt16
high
,
Array[UInt16]
lows
) =
Array[(UInt16, Array[UInt16])]
groups
(Array[(UInt16, Array[UInt16])], Int) -> (UInt16, Array[UInt16])

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
2178
RoaringBitmap
result
=
(bitmap : RoaringBitmap, high : UInt16, lows : Array[UInt16]) -> RoaringBitmap

批量添加排序的低16位值到指定高16位

add_sorted_lows_to_bitmap
(
RoaringBitmap
result
,
UInt16
high
,
Array[UInt16]
lows
)
2179 } 2180
RoaringBitmap
result
2181} 2182 2183///| 批量添加排序的低16位值到指定高16位 2184fn
(bitmap : RoaringBitmap, high : UInt16, lows : Array[UInt16]) -> RoaringBitmap

批量添加排序的低16位值到指定高16位

add_sorted_lows_to_bitmap
(
2185
RoaringBitmap
bitmap
:
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
,
2186
UInt16
high
:
UInt16
UInt16
,
2187
Array[UInt16]
lows
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
]
2188) ->
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
{
2189 match
(containers : Array[HighContainer], key : UInt16) -> SearchResult

在排序数组中查找容器索引

find_container_index
(
RoaringBitmap
bitmap
.
Array[HighContainer]
containers
,
UInt16
high
) {
2190
(Int) -> SearchResult
Found
(
Int
index
) => {
2191 // 容器存在,批量添加 2192 let
HighContainer
container
=
RoaringBitmap
bitmap
.
Array[HighContainer]
containers
(Array[HighContainer], Int) -> HighContainer

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
index]
2193 let (
Container
new_container
,
Int
added_count
) =
(container : Container, lows : Array[UInt16]) -> (Container, Int)

批量添加到现有容器

batch_add_to_existing_container
(
2194
HighContainer
container
.
Container
container
,
2195
Array[UInt16]
lows
,
2196 ) 2197 let
Int
new_cardinality
=
HighContainer
container
.
Int
cardinality
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
Int
added_count
2198 let
HighContainer
new_high_container
=
struct HighContainer {
  key: UInt16
  container: Container
  cardinality: Int
}

高级容器 - 处理 32 位整数的高 16 位

HighContainer
::{
2199
UInt16
key
:
UInt16
high
,
2200
Container
container
:
Container
new_container
,
2201
Int
cardinality
:
Int
new_cardinality
,
2202 } 2203 let
Array[HighContainer]
new_containers
=
(arr : Array[HighContainer], index : Int, element : HighContainer) -> Array[HighContainer]

替换数组中指定位置的元素

array_replace_at
(
2204
RoaringBitmap
bitmap
.
Array[HighContainer]
containers
,
2205
Int
index
,
2206
HighContainer
new_high_container
,
2207 ) 2208
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
::{
2209
Array[HighContainer]
containers
:
Array[HighContainer]
new_containers
,
2210
Int
size
:
RoaringBitmap
bitmap
.
Int
size
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
Int
added_count
,
2211 } 2212 } 2213
(Int) -> SearchResult
NotFound
(
Int
insert_pos
) => {
2214 // 容器不存在,创建新容器 2215 let
Container
new_container
= if
Array[UInt16]
lows
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self_ : Int, other : Int) -> Bool
>=
Int

容器配置常量

ARRAY_CONTAINER_MAX_SIZE
{
2216
(arr : Array[UInt16]) -> Container

数组容器转换为位图容器

array_to_bitmap_container
(
Array[UInt16]
lows
)
2217 } else { 2218
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
lows
)
2219 } 2220 let
HighContainer
new_high_container
=
struct HighContainer {
  key: UInt16
  container: Container
  cardinality: Int
}

高级容器 - 处理 32 位整数的高 16 位

HighContainer
::{
2221
UInt16
key
:
UInt16
high
,
2222
Container
container
:
Container
new_container
,
2223
Int
cardinality
:
Array[UInt16]
lows
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
(),
2224 } 2225 let
Array[HighContainer]
new_containers
=
(arr : Array[HighContainer], index : Int, element : HighContainer) -> Array[HighContainer]

在指定位置插入元素到数组

array_insert_at
(
2226
RoaringBitmap
bitmap
.
Array[HighContainer]
containers
,
2227
Int
insert_pos
,
2228
HighContainer
new_high_container
,
2229 ) 2230
struct RoaringBitmap {
  containers: Array[HighContainer]
  mut size: Int
}

RoaringBitmap 主结构

RoaringBitmap
::{
2231
Array[HighContainer]
containers
:
Array[HighContainer]
new_containers
,
2232
Int
size
:
RoaringBitmap
bitmap
.
Int
size
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
Array[UInt16]
lows
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
(),
2233 } 2234 } 2235 } 2236} 2237 2238///| 批量添加到现有容器 2239fn
(container : Container, lows : Array[UInt16]) -> (Container, Int)

批量添加到现有容器

batch_add_to_existing_container
(
2240
Container
container
:
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
,
2241
Array[UInt16]
lows
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
]
2242) -> (
enum Container {
  ArrayContainer(Array[UInt16])
  BitmapContainer(FixedArray[UInt64])
  RunContainer(Array[Run])
}

容器类型枚举 - Roaring Bitmap 的核心优化策略

Container
,
Int
Int
) {
2243 match
Container
container
{
2244
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
arr
) => {
2245 let (
Array[UInt16]
merged_arr
,
Int
added_count
) =
(arr1 : Array[UInt16], arr2 : Array[UInt16]) -> (Array[UInt16], Int)

合并两个排序数组,返回合并结果和新增元素数量

merge_sorted_arrays_count_new
(
Array[UInt16]
arr
,
Array[UInt16]
lows
)
2246 let
Container
new_container
= if
Array[UInt16]
merged_arr
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self_ : Int, other : Int) -> Bool
>=
Int

容器配置常量

ARRAY_CONTAINER_MAX_SIZE
{
2247
(arr : Array[UInt16]) -> Container

数组容器转换为位图容器

array_to_bitmap_container
(
Array[UInt16]
merged_arr
)
2248 } else { 2249
(Array[UInt16]) -> Container
ArrayContainer
(
Array[UInt16]
merged_arr
)
2250 } 2251 (
Container
new_container
,
Int
added_count
)
2252 } 2253
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
bitmap
) => {
2254 let (
FixedArray[UInt64]
new_bitmap
,
Int
added_count
) =
(bitmap : FixedArray[UInt64], lows : Array[UInt16]) -> (FixedArray[UInt64], Int)

批量添加到位图容器

batch_add_to_bitmap
(
FixedArray[UInt64]
bitmap
,
Array[UInt16]
lows
)
2255 (
(FixedArray[UInt64]) -> Container
BitmapContainer
(
FixedArray[UInt64]
new_bitmap
),
Int
added_count
)
2256 } 2257
(Array[Run]) -> Container
RunContainer
(
Array[Run]
runs
) => {
2258 let (
Array[Run]
new_runs
,
Int
added_count
) =
(runs : Array[Run], lows : Array[UInt16]) -> (Array[Run], Int)

批量添加到运行容器

batch_add_to_runs
(
Array[Run]
runs
,
Array[UInt16]
lows
)
2259 (
(Array[Run]) -> Container
RunContainer
(
Array[Run]
new_runs
),
Int
added_count
)
2260 } 2261 } 2262} 2263 2264///| 合并两个排序数组,返回合并结果和新增元素数量 2265fn
(arr1 : Array[UInt16], arr2 : Array[UInt16]) -> (Array[UInt16], Int)

合并两个排序数组,返回合并结果和新增元素数量

merge_sorted_arrays_count_new
(
2266
Array[UInt16]
arr1
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
],
2267
Array[UInt16]
arr2
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
]
2268) -> (
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
],
Int
Int
) {
2269 let
Array[UInt16]
result
= []
2270 let mut
Int
i
= 0
2271 let mut
Int
j
= 0
2272 let mut
Int
added_count
= 0
2273 while
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr1
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(Bool, Bool) -> Bool
&&
Int
j
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr2
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
2274 if
Array[UInt16]
arr1
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
(self_ : UInt16, other : UInt16) -> Bool
<
Array[UInt16]
arr2
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j] {
2275
Array[UInt16]
result
.
(self : Array[UInt16], value : UInt16) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Array[UInt16]
arr1
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i])
2276
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
2277 } else if
Array[UInt16]
arr1
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
(self_ : UInt16, other : UInt16) -> Bool
>
Array[UInt16]
arr2
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j] {
2278
Array[UInt16]
result
.
(self : Array[UInt16], value : UInt16) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Array[UInt16]
arr2
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j])
2279
Int
added_count
=
Int
added_count
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
2280
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
2281 } else { 2282
Array[UInt16]
result
.
(self : Array[UInt16], value : UInt16) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Array[UInt16]
arr1
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]) // 相等,只添加一次
2283
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
2284
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
2285 } 2286 } 2287 2288 // 添加剩余元素 2289 while
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr1
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
2290
Array[UInt16]
result
.
(self : Array[UInt16], value : UInt16) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Array[UInt16]
arr1
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i])
2291
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
2292 } 2293 while
Int
j
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
arr2
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
() {
2294
Array[UInt16]
result
.
(self : Array[UInt16], value : UInt16) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Array[UInt16]
arr2
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j])
2295
Int
added_count
=
Int
added_count
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
2296
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
2297 } 2298 (
Array[UInt16]
result
,
Int
added_count
)
2299} 2300 2301///| 批量添加到位图容器 2302fn
(bitmap : FixedArray[UInt64], lows : Array[UInt16]) -> (FixedArray[UInt64], Int)

批量添加到位图容器

batch_add_to_bitmap
(
2303
FixedArray[UInt64]
bitmap
:
type FixedArray[A]
FixedArray
[
UInt64
UInt64
],
2304
Array[UInt16]
lows
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
]
2305) -> (
type FixedArray[A]
FixedArray
[
UInt64
UInt64
],
Int
Int
) {
2306 let
FixedArray[UInt64]
new_bitmap
=
type FixedArray[A]
FixedArray
::
(len : Int, init : UInt64) -> FixedArray[UInt64]

Creates a new fixed-size array with the specified length, initializing all elements with the given value.

Parameters:

  • length : The length of the array to create. Must be non-negative.
  • initial_value : The value used to initialize all elements in the array.

Returns a new fixed-size array of type FixedArray[T] with length elements, where each element is initialized to initial_value.

Throws a panic if length is negative.

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[0], content="42")
  inspect(arr.length(), content="3")

WARNING: A common pitfall is creating with the same initial value, for example:

  let two_dimension_array = FixedArray::make(10, FixedArray::make(10, 0))
  two_dimension_array[0][5] = 10
  assert_eq(two_dimension_array[5][5], 10)

This is because all the cells reference to the same object (the FixedArray[Int] in this case). One should use makei() instead which creates an object for each index.

make
(1024, 0UL)
2307 let mut
Int
added_count
= 0
2308 2309 // 复制原位图 2310 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
1024;
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
2311
FixedArray[UInt64]
new_bitmap
(FixedArray[UInt64], Int, UInt64) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
i] =
FixedArray[UInt64]
bitmap
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
i]
2312 } 2313 2314 // 批量添加元素 2315 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
lows
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
2316 let
UInt16
value
=
Array[UInt16]
lows
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
2317 let
Int
word_index
=
UInt16
value
.
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
()
(self : Int, other : Int) -> Int

Performs integer division between two 32-bit integers. The result is truncated towards zero (rounds down for positive numbers and up for negative numbers).

Parameters:

  • dividend : The first integer operand to be divided.
  • divisor : The second integer operand that divides the dividend.

Returns the quotient of the division operation.

Throws a panic if divisor is zero.

Example:

  inspect(10 / 3, content="3") // truncates towards zero
  inspect(-10 / 3, content="-3")
  inspect(10 / -3, content="-3")
/
64
2318 let
Int
bit_index
=
UInt16
value
.
(self : UInt16) -> Int

Converts an unsigned 16-bit integer to a 32-bit signed integer. The value is zero-extended to fill the higher bits.

Parameters:

  • value : The unsigned 16-bit integer to be converted.

Returns a 32-bit signed integer. Since the input value is always non-negative and less than 65536, the conversion never results in overflow.

Example:

  let x = Int::to_uint16(42)
  inspect(x.to_int(), content="42")
  let max = Int::to_uint16(65535) // maximum value of UInt16
  inspect(max.to_int(), content="65535")
to_int
()
(self : Int, other : Int) -> Int

Calculates the remainder of dividing one integer by another. The result follows the formula dividend - (dividend / divisor) * divisor, maintaining the same sign as the dividend.

Parameters:

  • self : The dividend (the number being divided).
  • other : The divisor (the number to divide by).

Returns the remainder of the division. If other is 0, the behavior is undefined.

Example:

  inspect(7 % 3, content="1")
  inspect(-7 % 3, content="-1")
  inspect(7 % -3, content="1")
%
64
2319 let
UInt64
bit_mask
= 1UL
(self : UInt64, shift : Int) -> UInt64

Performs a bitwise left shift operation on an unsigned 64-bit integer.

Parameters:

  • self : The unsigned 64-bit integer to be shifted.
  • shift : The number of positions to shift the bits to the left. Must be between 0 and 63 inclusive, values outside this range will be masked with & 63.

Returns the result of shifting self left by shift positions. For each position shifted, the rightmost bit is filled with 0, and the leftmost bit is discarded.

Example:

  let x = 1UL
  inspect(x << 3, content="8") // 1 shifted left by 3 positions becomes 8
  inspect(x << 63, content="9223372036854775808") // 1 shifted left by 63 positions
<<
Int
bit_index
2320 if (
FixedArray[UInt64]
new_bitmap
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
word_index]
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise AND operation between two unsigned 64-bit integers.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns the result of performing a bitwise AND operation on the two operands.

Example:

  let a = 0xF0F0F0F0F0F0F0F0UL
  let b = 0xFF00FF00FF00FF00UL
  inspect(a & b, content="17294086455919964160") // 0xF000F000F000F000
&
UInt64
bit_mask
)
(self : UInt64, other : UInt64) -> Bool

Compares two unsigned 64-bit integers for equality.

Parameters:

  • self : The first unsigned 64-bit integer to compare.
  • other : The second unsigned 64-bit integer to compare.

Returns true if both integers have the same value, false otherwise.

Example:

  let a = 42UL
  let b = 42UL
  let c = 24UL
  inspect(a == b, content="true")
  inspect(a == c, content="false")
==
0UL {
2321 // 元素不存在,添加它 2322
FixedArray[UInt64]
new_bitmap
(FixedArray[UInt64], Int, UInt64) -> Unit

Sets a value at the specified index in a fixed-size array. The original value at that index is overwritten.

Parameters:

  • array : The fixed-size array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws a runtime error if the index is out of bounds (less than 0 or greater than or equal to the length of the array).

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
word_index] =
FixedArray[UInt64]
new_bitmap
(FixedArray[UInt64], Int) -> UInt64

Retrieves an element at the specified index from a fixed-size array. This function implements the array indexing operator [].

Parameters:

  • array : The fixed-size array to access.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a runtime error if the index is out of bounds (negative or greater than or equal to the length of the array).

Example:

  let arr = FixedArray::make(3, 42)
  inspect(arr[1], content="42")
[
word_index]
(self : UInt64, other : UInt64) -> UInt64

Performs a bitwise OR operation between two unsigned 64-bit integers.

Parameters:

  • self : The first unsigned 64-bit integer operand.
  • other : The second unsigned 64-bit integer operand.

Returns a new UInt64 value with bits set to 1 where either or both operands have bits set to 1, and 0 where both operands have bits set to 0.

Example:

  let a = 0xF0F0F0F0UL
  let b = 0x0F0F0F0FUL
  inspect(a | b, content="4294967295") // All bits set to 1
|
UInt64
bit_mask
2323
Int
added_count
=
Int
added_count
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
2324 } 2325 } 2326 (
FixedArray[UInt64]
new_bitmap
,
Int
added_count
)
2327} 2328 2329///| 批量添加到运行容器 2330fn
(runs : Array[Run], lows : Array[UInt16]) -> (Array[Run], Int)

批量添加到运行容器

batch_add_to_runs
(
2331
Array[Run]
runs
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
],
2332
Array[UInt16]
lows
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt16
UInt16
]
2333) -> (
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
struct Run {
  start: UInt16
  length: UInt16
}

运行长度编码的运行段

Run
],
Int
Int
) {
2334 let mut
Array[Run]
new_runs
=
Array[Run]
runs
2335 let mut
Int
added_count
= 0
2336 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt16]
lows
.
(self : Array[UInt16]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
2337 let
UInt16
value
=
Array[UInt16]
lows
(Array[UInt16], Int) -> UInt16

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i]
2338 if
(x : Bool) -> Bool

Performs logical negation on a boolean value.

Parameters:

  • value : The boolean value to negate.

Returns the logical NOT of the input value: true if the input is false, and false if the input is true.

Example:

  inspect(not(true), content="false")
  inspect(not(false), content="true")
not
(
(runs : Array[Run], value : UInt16) -> Bool
run_container_contains
(
Array[Run]
new_runs
,
UInt16
value
)) {
2339
Array[Run]
new_runs
=
(runs : Array[Run], value : UInt16) -> Array[Run]

改进的运行容器添加元素操作

run_container_add
(
Array[Run]
new_runs
,
UInt16
value
)
2340
Int
added_count
=
Int
added_count
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1
2341 } 2342 } 2343 (
Array[Run]
new_runs
,
Int
added_count
)
2344} 2345 2346///| UInt数组排序 2347fn
(arr : Array[UInt]) -> Array[UInt]

UInt数组排序

sort_uint_array
(
Array[UInt]
arr
:
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt
UInt
]) ->
type Array[T]

An Array is a collection of values that supports random access and can grow in size.

Array
[
UInt
UInt
] {
2348 if
Array[UInt]
arr
.
(self : Array[UInt]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self_ : Int, other : Int) -> Bool
<=
1 {
2349 return
Array[UInt]
arr
2350 } 2351 let
Array[UInt]
sorted
= []
2352 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt]
arr
.
(self : Array[UInt]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
();
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
2353
Array[UInt]
sorted
.
(self : Array[UInt], value : UInt) -> Unit

Adds an element to the end of the array.

If the array is at capacity, it will be reallocated.

Example

  let v = []
  v.push(3)
push
(
Array[UInt]
arr
(Array[UInt], Int) -> UInt

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
i])
2354 } 2355 for
Int
i
= 0;
Int
i
(self_ : Int, other : Int) -> Bool
<
Array[UInt]
sorted
.
(self : Array[UInt]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
-
1;
Int
i
=
Int
i
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
2356 for
Int
j
= 0;
Int
j
(self_ : Int, other : Int) -> Bool
<
Array[UInt]
sorted
.
(self : Array[UInt]) -> Int

Returns the number of elements in the array.

Parameters:

  • array : The array whose length is to be determined.

Returns the number of elements in the array as an integer.

Example:

  let arr = [1, 2, 3]
  inspect(arr.length(), content="3")
  let empty : Array[Int] = []
  inspect(empty.length(), content="0")
length
()
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
-
1
(self : Int, other : Int) -> Int

Performs subtraction between two 32-bit integers, following standard two's complement arithmetic rules. When the result overflows or underflows, it wraps around within the 32-bit integer range.

Parameters:

  • self : The minuend (the number being subtracted from).
  • other : The subtrahend (the number to subtract).

Returns the difference between self and other.

Example:

  let a = 42
  let b = 10
  inspect(a - b, content="32")
  let max = 2147483647 // Int maximum value
  inspect(max - -1, content="-2147483648") // Overflow case
-
Int
i
;
Int
j
=
Int
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1 {
2357 if
Array[UInt]
sorted
(Array[UInt], Int) -> UInt

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j]
(self_ : UInt, other : UInt) -> Bool
>
Array[UInt]
sorted
(Array[UInt], Int) -> UInt

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1] {
2358 let
UInt
temp
=
Array[UInt]
sorted
(Array[UInt], Int) -> UInt

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j]
2359
Array[UInt]
sorted
(Array[UInt], Int, UInt) -> Unit

Sets the element at the specified index in the array to a new value. The original value at that index is overwritten.

Parameters:

  • array : The array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws an error if index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
j] =
Array[UInt]
sorted
(Array[UInt], Int) -> UInt

Retrieves an element from the array at the specified index.

Parameters:

  • array : The array to get the element from.
  • index : The position in the array from which to retrieve the element.

Returns the element at the specified index.

Throws a panic if the index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  inspect(arr[1], content="2")
[
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1]
2360
Array[UInt]
sorted
(Array[UInt], Int, UInt) -> Unit

Sets the element at the specified index in the array to a new value. The original value at that index is overwritten.

Parameters:

  • array : The array to modify.
  • index : The position in the array where the value will be set.
  • value : The new value to assign at the specified index.

Throws an error if index is negative or greater than or equal to the length of the array.

Example:

  let arr = [1, 2, 3]
  arr[1] = 42
  inspect(arr, content="[1, 42, 3]")
[
j
(self : Int, other : Int) -> Int

Adds two 32-bit signed integers. Performs two's complement arithmetic, which means the operation will wrap around if the result exceeds the range of a 32-bit integer.

Parameters:

  • self : The first integer operand.
  • other : The second integer operand.

Returns a new integer that is the sum of the two operands. If the mathematical sum exceeds the range of a 32-bit integer (-2,147,483,648 to 2,147,483,647), the result wraps around according to two's complement rules.

Example:

  inspect(42 + 1, content="43")
  inspect(2147483647 + 1, content="-2147483648") // Overflow wraps around to minimum value
+
1] =
UInt
temp
2361 } 2362 } 2363 } 2364
Array[UInt]
sorted
2365} 2366