POSIX_ME_HARDER
POSIX_ME_HARDER

Reputation: 762

Are there Erlang arrays "with a defined representation"?

Context:

Erlang programs running on heterogeneous nodes, retrieving and storing data from Mnesia databases. These database entries are meant to be used for a long time (e.g. across multiple Erlang version releases) remains in the form of Erlang objects (i.e. no serialization). Among the information stored, there are currently two uses for arrays:

  1. Large (up to 16384 elements) arrays. Fast access to an element using its index was the basis for choosing this type of collection. Once the array has been created, the elements are never modified.

  2. Small (up to 64 elements) arrays. Accesses are mostly done using indices, but there are also some iterations (foldl/foldr). Both reading and replacement of the elements is done frequently. The size of the collection remains constant.

Problem:

Erlang's documentation on arrays states that "The representation is not documented and is subject to change without notice." Clearly, arrays should not be used in my context: database entries containing arrays may be interpreted differently depending on the node executing the program and unannounced changes to how arrays are implemented would make them unusable.

I have noticed that Erlang features "ordsets"/"orddict" to address a similar issue with "sets"/"dict", and am thus looking for the "array" equivalent. Do you know of any? If none exists, my strategy is likely going to be using lists of lists to replace my large arrays, and orddict (with the index as key) to replace the smaller ones. Is there a better solution?

Upvotes: 3

Views: 399

Answers (1)

starbelly
starbelly

Reputation: 254

An array is a tuple of nested tuples and integers, with each tuple being a fixed size of 10 and representing a segment of cells. Where a segment is not currently used an integer (10) acts as a place holder. This without the abstraction is I suppose the closet equivalent.You could indeed copy the array module from otp and add to your own app and thus it would be a stable representation.

As to what you should use devoid of array depends on the data and what you will do with it. If data that would be in your array is fixed, then a tuple makes since, it has constant access time for reads/lookups. Otherwise a list sounds like a winner, be it a list of lists, list of tuples, etc. However, once again, that's a shot in the dark, because I don't know your data or how you use it.

See the implementation here: https://github.com/erlang/otp/blob/master/lib/stdlib/src/array.erl

Also see Robert Virding's answer on the implementation of array here: Arrays implementation in erlang

And what Fred Hebert says about the array in A Short Visit to Common Data Structures

An example showing the structure of an array:

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> 

Upvotes: 3

Related Questions