arrays - 是否有 Erlang 数组 "with a defined representation"?

标签 arrays erlang storage portability

上下文:

在异构节点上运行的 Erlang 程序,检索和存储数据 来自 Mnesia 数据库。这些数据库条目将长期使用 时间(例如,跨多个 Erlang 版本发布)保持为 Erlang 对象(即没有序列化)。在存储的信息中,有 目前数组有两种用途:

  1. 大型(最多 16384 个元素)数组。快速访问元素 使用其索引是选择此类集合的基础。 一旦创建了数组,就永远不会修改元素。

  2. 小型(最多 64 个元素)数组。访问主要使用索引完成,但也有一些迭代(foldl/foldr)。元素的读取和替换都经常进行。集合的大小保持不变。

问题:

Erlang's documentation on arrays指出“代表不是 记录在案,如有更改,恕不另行通知。”显然,数组不应该在我的上下文中使用:包含数组的数据库条目可能是 根据执行程序的节点进行不同的解释,并且 对数组的实现方式进行未经宣布的更改会使它们无法使用。

我注意到 Erlang 具有“ordsets”/“orddict”来解决类似的问题 “sets”/“dict”的问题,因此我正在寻找等效的“array”。你知道吗?如果不存在,我的策略可能是使用列表的列表来替换我的大数组,并使用 orddict(以索引作为键)替换较小的数组。有更好的解决方案吗?

最佳答案

数组是嵌套元组和整数的元组,每个元组的大小固定为 10,代表一段单元格。如果某个段当前未使用,则整数 (10) 用作占位符。这没有抽象是我认为壁橱等价物。你确实可以从 otp 复制数组模块并添加到你自己的应用程序,因此它将是一个稳定的表示。

至于你应该在没有数组的情况下使用什么取决于数据以及你将如何处理它。如果数组中的数据是固定的,那么一个元组就会生成,因为它具有恒定的读取/查找访问时间。否则列表听起来像是赢家,无论是列表列表、元组列表等。但是,再一次,这是在黑暗中拍摄的,因为我不知道您的数据或您如何使用它。

在此处查看实现:https://github.com/erlang/otp/blob/master/lib/stdlib/src/array.erl

另请参阅 Robert Virding 关于数组实现的回答:Arrays implementation in erlang

Fred Hebert 在 A Short Visit to Common Data Structures 中对数组的评价

显示数组结构的示例:

1> A1 = array:new(30).
{array,30,0,undefined,100}
2> A2 = array:set(0, true, A1).
{array,30,0,undefined,
       {{true,undefined,undefined,undefined,undefined,undefined,
              undefined,undefined,undefined,undefined},
        10,10,10,10,10,10,10,10,10,10}}
3> A3 = array:set(19, true, A2).
{array,30,0,undefined,
       {{true,undefined,undefined,undefined,undefined,undefined,
              undefined,undefined,undefined,undefined},
        {undefined,undefined,undefined,undefined,undefined,
                   undefined,undefined,undefined,undefined,true},
        10,10,10,10,10,10,10,10,10}}
4> 

关于arrays - 是否有 Erlang 数组 "with a defined representation"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53877197/

相关文章:

c - 如何在 ANSI C 程序中返回字符串数组?

javascript - 带有对象的 Array.prototype.fill() 传递引用而不是新实例

c - Erlang c-nodes 随机崩溃,并出现双重释放、内存损坏 malloc 消息

Erlang 多处理消息接收和发送

python / NumPy : Efficiently store non-sparse large symmetric arrays?

PostgreSQL 错误 : could not extend file no space left on device

arrays - Kadane 的算法是否适用于运行长度编码的整数数组?

java - 如何在java中自动增加数组的大小

c++ - 在 gen_tcp 中使用 {packet, N} 以及如何在 C++ 程序中接收数据

Azure 存储投票模式