在 bash 5.0.7
的 man
页面中,有一句话说
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/