Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To which category Scala Arrays belong?

I'm doing an analysis about Arrays in Scala. I'm doing this analysis following a ​​book (Concepts of Programming Languages by Robert W. Sebesta). According to the book, there are five categories of arrays, based on the binding to subscript ranges, the binding to storage, and from where the storage is allocated. The categories are:

  • static array : is one in which the subscript ranges are statically bound and storage allocation is static (done before run time). The advantage of static arrays is efficiency: No dynamic allocation or deallocation is required. The disadvantage is that the storage for the array is fixed for the entire execution time of the program.

  • fixed stack-dynamic array : is one in which the subscript ranges are statically bound, but the allocation is done at declaration elaboration time during execution. The advantage of fixed stack-dynamic arrays over static arrays is space efficiency. A large array in one subprogram can use the same space as a large array in a different subprogram, as long as both subprograms are not active at the same time. The same is true if the two arrays are in different blocks that are not active at the same time. The disadvantage is the required allocation and deallocation time.

  • stack-dynamic array : is one in which both the subscript ranges and the storage allocation are dynamically bound at elaboration time. Once the subscript ranges are bound and the storage is allocated, however, they remain fixed during the lifetime of the variable. The advantage of stack-dynamic arrays over static and fixed stack-dynamic arrays is flexibility. The size of an array need not be known until the array is about to be used.

  • fixed heap-dynamic array : is similar to a fixed stack-dynamic array, in that the subscript ranges and the storage binding are both fixed after storage is allocated. The differences are that both the subscript ranges and storage bindings are done when the user program requests them during execution, and the storage is allocated from the heap, rather than the stack. The advantage of fixed heap-dynamic arrays is flexibility—the array’s size always fits the problem. The disadvantage is allocation time from the heap, which is longer than allocation time from the stack.

  • heap-dynamic array : is one in which the binding of subscript ranges and storage allocation is dynamic and can change any number of times during the array’s lifetime. The advantage of heap-dynamic arrays over the others is flexibility: Arrays can grow and shrink during program execution as the need for space changes. The disadvantage is that allocation and deallocation take longer and may happen many times during execution of the program. Examples of the five categories are given in the following paragraphs.

Which of these types of matrices the Scala language is using?

like image 666
Priscylla Avatar asked Jan 15 '23 11:01

Priscylla


1 Answers

Scala and Java both use fixed heap-dynamic array on primitive arrays. There are classes, such as Java's Vector and Scala's ArrayBuffer, that build on the primitive to provide the characteristics of the heap-dynamic array.

like image 86
Richard Sitze Avatar answered Jan 21 '23 19:01

Richard Sitze