arrays - 稀疏时,BASH 空间中的整数索引数组是否有效?

标签 arrays linux bash hashtable space-complexity

bash 5.0.7man 页面中,有一句话说

There is no maximum limit on the size of an array, nor any requirement that members be indexed or assigned contiguously.

这意味着我可以做这样的事情:

bash $ foo=
bash $ foo[34]=hello
bash $ foo[182]=world
bash $ echo ${foo[34]} ${foo[182]}
hello world

我在官方手册页和网上都找不到任何关于稀疏数组的空间复杂度的信息。如果我在元素 381425381 或其他地方初始化一个数组,那么内部实现肯定会将数据存储在哈希表中,而不是在它下面分配 381425380 空间?这似乎是合理的选择;如果这是正确的,是否有任何文件可以确认这是如何实现的?

最佳答案

来自mirror of bash , 看起来

Arrays are sparse doubly-linked lists. An element's index is stored with it.

这意味着是的,稀疏数组是节省空间的,只有有效的索引才会占用空间。

关于arrays - 稀疏时,BASH 空间中的整数索引数组是否有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57066492/

相关文章:

c - 0 转换为 128

linux - 当 find 提供文件时如何在文件名中使用空格 cp 文件

php - 计算数组中具有给定值的值的数量

linux - 高级 while 控制语句

java - 在我的 printArray 方法中查看数组

python - 如何在shell中启动 "run" "execute"“initiate”.py文件

bash - 如何使用 printf 打印粗体文本?

linux - BASH - 添加一个作为开关的参数

javascript - 在JS中用const之类的东西定义一个数组

javascript - 使用 Angular 本地存储模块将元素存储在本地存储中的数组中