6.7 布尔向量

布尔向量(bool-vector)与普通向量(vector)非常相似,区别仅在于它仅存储 tnil 两种值。如果你尝试将任何非 nil 的值存入布尔向量的某个元素中,其效果等同于在该位置存入 t。与所有数组一样,布尔向量的索引从 0 开始,且一旦创建完成,其长度便无法修改。布尔向量在求值时会被视为常量。

有若干函数是专门用于操作布尔向量的;除此之外,你也可以使用操作其他类型数组的通用函数来处理布尔向量。

Function: make-bool-vector length initial

创建并返回一个新的布尔向量,该向量包含 length 个元素,每个元素都会初始化为 initial 的值。

Function: bool-vector &rest objects

该函数创建并返回一个布尔向量,其元素为传入的参数 objects(可传入多个)。

Function: bool-vector-p object

object 是布尔向量,该函数返回 t;否则返回 nil

此外,还有若干布尔向量集合操作函数,具体说明如下:

Function: bool-vector-exclusive-or a b &optional c

返回布尔向量 ab按位异或(bitwise exclusive or) 结果。若传入可选参数 c,该操作的结果会存入 c 中。所有参数都必须是长度相同的布尔向量。

Function: bool-vector-union a b &optional c

返回布尔向量 ab按位或(bitwise or) 结果。若传入可选参数 c,该操作的结果会存入 c 中。所有参数都必须是长度相同的布尔向量。

Function: bool-vector-intersection a b &optional c

返回布尔向量 ab按位与(bitwise and) 结果。若传入可选参数 c,该操作的结果会存入 c 中。所有参数都必须是长度相同的布尔向量。 Return bitwise and of bool vectors a and b. If optional argument c is given, the result of this operation is stored into c. All arguments should be bool vectors of the same length.

Function: bool-vector-set-difference a b &optional c

返回布尔向量 a 相对于 b集合差(set difference) 结果。若传入可选参数 c,该操作的结果会存入 c 中。所有参数都必须是长度相同的布尔向量。

Function: bool-vector-not a &optional b

返回布尔向量 a集合补(set complement) 结果。若传入可选参数 b,该操作的结果会存入 b 中。所有参数都必须是长度相同的布尔向量。

Function: bool-vector-subsetp a b

a 中所有值为 t 的位置在 b 中对应位置的值也为 t,则返回 t;否则返回 nil。所有参数都必须是长度相同的布尔向量。

Function: bool-vector-count-consecutive a b i

返回布尔向量 a 中从索引 i 开始、连续等于 b 的元素个数。其中 a 是布尔向量,b 的值为 tnilia 的有效索引。

Function: bool-vector-count-population a

返回布尔向量 a 中值为 t 的元素总数。

其打印形式会将最多 8 个布尔值合并表示为一个字符:

(bool-vector t nil t nil)
     ⇒ #&4"^E"
(bool-vector)
     ⇒ #&0""

你可以使用 vconcat 函数像打印其他向量一样打印布尔向量:

(vconcat (bool-vector nil t nil t))
     ⇒ [nil t nil t]

下面是另一个创建、查看和更新布尔向量的示例:

(setq bv (make-bool-vector 5 t))
     ⇒ #&5"^_"
(aref bv 1)
     ⇒ t
(aset bv 3 nil)
     ⇒ nil
bv
     ⇒ #&5"^W"

这些结果是合理的,因为控制键 control-_ 和 control-W 对应的二进制码分别是 11111 和 10111。


emacs

Emacs

org-mode

Orgmode

Donations

打赏

Copyright

© 2025 Jasper Hsu

Creative Commons

Creative Commons

Attribute

Attribute

Noncommercial

Noncommercial

Share Alike

Share Alike