Informatika Komputer    
   
Daftar Isi
(Sebelumnya) Comparison of platform virtual ...Comparison of programming lang ... (Berikutnya)

Perbandingan -- programming languages (array)

Contents

Syntax

Array dimensions

The following list contains Syntax examples on how to determine the dimensions (index of the first element, the last element and/or the size in elements):

SizeFirstLastLanguages
name'Lengthname'Firstname'LastAda
UPB name - LWB name+1 or
2 UPB name - 2 LWB name+1 etc.
LWB name or 2 LWB name etc.UPB name or 2 UPB name etc.ALGOL 68
name.Lengthname.GetLowerBound(dimension)name.GetUpperBound(dimension)C#, Visual Basic .NET, Windows PowerShell, F#
max(shape(name))0max(shape(name))-1Ch
(length name)0(1- (length name))Common Lisp
SIZE(name)LBOUND(name)UBOUND(name)Fortran
len(name)0len(name) - 1Go
rangeSize (bounds name)fst (bounds name)snd (bounds name)Haskell
(length name)0(1- (length name))ISLISP
name.length0name.length - 1Java, JavaScript, D, Scala
#name1#nameLua
Length[name]1 or First[name]-1 or Last[name]Mathematica
length(name)1endMATLAB, GNU Octave
Length(name)name[0]name[Length(name)-1]Object Pascal
[name count]0[name count] - 1Objective-C (NSArray * only)
Array.length name0Array.length name - 1OCaml
scalar(@name)$[$#namePerl
@name.elems0@name.endPerl 6
count($name)0count($name) - 1PHP
len(name)0-1 or len(name) - 1Python
name.size0 (name.first will also refer to this element)-1 or name.size - 1 (name.last will also refer to this element)Ruby
length(name)0-1 or length(name)-1S-Lang
(vector-length vector)0(- (vector-length vector) 1)Scheme
name size1 (name first will also refer to this element)name size (name last will also refer to this element)Smalltalk
UBound(name)-LBound(name)+1LBound(name)UBound(name)Visual Basic

Indexing

The following list contains Syntax examples on how a single element of an array can be accessed.

FormatLanguages
name[index] or name[index1, index2] etc.ALGOL 68, Pascal, Object Pascal, C#, S-Lang1
name[index]C, Ch, C++, D, Go, Java, ActionScript 3.0, JavaScript, Perl1, PHP, Python1, Ruby1, Lua
$name[index]Perl1, PHP, Windows PowerShell1
@name[index]Perl 6
name(index)Ada, Fortran, BASIC, Visual Basic, Visual Basic .NET, RPG, MATLAB, Scala
name.(index)OCaml
name.[index]F#
name ! indexHaskell
(vector-ref name index)Scheme
(aref name index)Common Lisp
(elt name index)ISLISP
name [[index]]Mathematica1
name at:indexSmalltalk
[name objectAtIndex:index]Objective-C (NSArray * only)
  1. The index may be a negative number, indicating the corresponding number of places before the end of the array.

Slicing

The following list contains Syntax examples on how a range of element of an array can be accessed.

In the following table:

  • first - the index of the first element in the slice
  • last - the index of the last element in the slice
  • end - one more than the index of last element in the slice
  • len - the length of the slice (= end - first)
FormatLanguages
name[first:last]ALGOL 681
name[first:end:len]Python2,4, Go
name[first..last]Pascal, Object Pascal, Delphi, D
$name[first..last]Windows PowerShell
@name[first..last]Perl3
name[first..last]
name[first...end]
name[first, len]
Ruby4
name(first..last)Ada1
name(first:last)Fortran1,2, MATLAB1,3
name[[first;last]]Mathematica1,2,4
name[[first:last]]S-Lang1,2,3
name.[first..last]F#
name.slice(first, last)JavaScript, Scala
array_slice(name, first, len)PHP4
(subseq name first end)Common Lisp
(subseq name first end)ISLISP
Array.sub name first lenOCaml
[name subarrayWithRange:NSMakeRange(first, len)]Objective-C (NSArray * only)
  1. Slices for multidimensional arrays are also supported and defined similarly.
  2. Slices of the type first:last:step are also supported.
  3. More generally, for 1-d arrays Perl and S-Lang permit slices of the form array[indices], where indices can be a range such mentioned in footnote 2 or an explicit list of indices, e.g., '[0,9,3,4]', as well as a combination of the two, e.g., A[[[0:3]],7,9,[11:2:-3]]].
  4. last or end may be a negative number, indicating to stop at the corresponding number of places before the end of the array.

Array system cross-reference list

Programming languageDefault base indexSpecifiable index type16Specifiable base indexBound checkMultidimensionalDynamically-sizedVectorized operations
Adaindex type17yesyescheckedyesinit1some, others definable5
ALGOL 681no25yesvariesyesyesuser definable
APL ? ?0 or 17checkedyesinit1yes
BASIC0 ?nocheckednoinit1 ?
C0nono26uncheckedyes, also array of array2init1,4, heap3no
Ch0nonocheckedyes, also array of array2init1,4, heap3yes
C++50nono26uncheckedyes, also array of array2heap3no
C#0nonocheckedyesheap3,9yes (LINQ select)
COBOL1no27nocheckedarray of array2 28no14some intrinsics
Common Lisp0 ?nochecked15yesyesyes (map or map-into)
D0 ?novaries11yesyes ?
F#0nonocheckedyesheap3,9yes (map)
FreeBASIC0noyescheckedyesinit1,init21 ?
Fortran1yesyesvaries12yesyesyes
FoxPro1 ?nocheckedyesyes ?
Go0nonocheckedarray of array2nono
Haskellnone (specified on init)yes24yescheckedyes, also array of array2init1 ?
IDL0 ?nocheckedyesyesyes
ISLISP0 ?nocheckedyesinit1yes (map or map-into)
Java50nonocheckedarray of array2init1 ?
JavaScript0nonochecked22array of array2yesyes
Lua1 ?partial20checkedarray of array2yes ?
Mathematica1nonocheckedyesyesyes
MATLAB1 ?nocheckedyes8yesyes
Oberon0 ?nocheckedyesno ?
Oberon-20 ?nocheckedyesyes ?
Objective-C50nonocheckedarray of array2yesno
OCaml0nonochecked by defaultarray of array2init1 ?
Pascal, Object Pascalindex type17yesyesvaries13yesvaries10some
Perl0noyes ($[)checked22array of array2yesno18
Perl 60nonochecked22yesyesyes
PHP0yes23yes23checked23yesyesyes
PL/I1 ?yescheckedyesno ?
Python0nonocheckedarray of array2yesno19
RPG1nono ?nono ?
Ruby0nonochecked22array of array2yes ?
S-Lang0 ?nocheckedyesyesyes
Scala0nonocheckedarray of array2init1yes (map)
Scheme0 ?nocheckedarray of array2init1yes (map)
Smalltalk51 ?nocheckedarray of array2yes6 ?
Visual Basic0noyescheckedyesyes ?
Visual Basic .NET0nonocheckedyesyesyes (LINQ select)
Windows PowerShell0nonocheckedyesheap ?
XPath1nonocheckednoyesyes
Programming languageDefault base indexSpecifiable index type16Specifiable base indexBound checkMultidimensionalDynamically-sizedVectorized operations
  1. Size can only be chosen on initialization after which it is fixed.
  2. Allows arrays of arrays which can be used to emulate most—but not all—aspects multi-dimensional arrays.
  3. Size can only be chosen on initialization when memory is allocated on the heap, as distinguished from when it is allocated on the stack. This note need not be made for a language that always allocates arrays on the heap.
  4. C99 allows for variable size arrays; however there is almost no compiler available to support this new feature.
  5. This list is strictly comparing language features. In every language (even assembler) it is possible to provide improved array handling via add on libraries. This language has improved array handling as part of its standard library.
  6. The class Array is fixed-size, but OrderedCollection is dynamic.
  7. The indexing base can be 0 or 1, but is set for a whole "workspace".
  8. At least 2 dimensions (scalar numbers are 1×1 arrays, vectors are 1×n or n×1 arrays).
  9. Allows creation of fixed-size arrays in "unsafe" code, allowing for enhanced interoperability with other languages
  10. Varies by implementation. Newer implementations (FreePascal, Object Pascal (Delphi)) permit heap-based dynamic arrays.
  11. Behaviour can be tuned using compiler switches. As in DMD 1.0 bounds are checked in debug mode and unchecked in release mode for efficiency reasons.
  12. Almost all Fortran implementations offer bounds checking options via compiler switches. However by default, bounds checking is usually turned off for efficiency reasons.
  13. Many implementations (Turbo Pascal, Object Pascal (Delphi), FreePascal) allow the behaviour to be changed by compiler switches and in-line directives.
  14. COBOL provides a way to specify that the usable size of an array is variable, but this can never be greater than the declared maximum size, which is also the allocated size.
  15. Most Common Lisp implementations allow checking to be selectively disabled.
  16. The index type can be a freely chosen integer type, enumerated type, or character type. For arrays with non-compact index types see: Associative array.
  17. The default base index is the lowest value of the index type used.
  18. Standard Perl array data types do not support vectorized operations as defined here. However, the Perl Data Language extension adds array objects with this ability.
  19. The standard Python array type, list, does not support vectorized operations as defined here. However, the numpy extension adds array objects with this ability.
  20. By specifying a base index, arrays at an arbitrary base can be created. However, by default, Lua's length operator does not consider the base index of the array when calculating the length. This behavior can be changed via metamethods.
  21. FreeBASIC supports both variable array lengths and fixed length arrays. Arrays declared with no index range are created as variable-length arrays, while arrays with a declared range are created as fixed-length arrays.
  22. In these languages, one can access or write to an array index greater than or equal to the length of the array, and the array will implicitly grow to that size. This may appear at first as if the bounds are not checked; however, the bounds are checked in order to decide to grow the array, and you do not have unsafe memory access like you do in C.
  23. PHP's "arrays" are associative arrays. You can use integers and strings as the keys (indexes); floats can also be used as the key but are truncated to integers. There is not really any "base index" or "bounds".
  24. Haskell arrays (Data.Array) allow using any type which is an instance of Ix as index type. So a custom type can be defined and used as an index type as long as it instances Ix. Also, tuples of Ix types are also Ix types; this is commonly used to implement multi-dimensional arrays.
  25. ALGOL 68 arrays must be subscripted (and sliced) by type INT. However a hash function could be used to convert other types to INT. e.g. name[hash("string")]
  26. Because C does not bound-check indices, a pointer to the interior of any array can be defined that will symbolically act as a pseudo-array that accommodates negative indices or any integer index origin.
  27. COBOL arrays may be indexed with "INDEX" types, distinct from integer types.
  28. While COBOL only has arrays-of-arrays, array elements can be accessed with a multi-dimensional-array-like syntax, where the language automatically matches the indexes to the arrays enclosing the item being referenced.

Vectorized array operations

Some compiled languages such as Ada and Fortran, and some scripting languages such as IDL, MATLAB, and S-Lang, have native support for vectorized operations on arrays. For example, to perform an element by element sum of two arrays, a and b to produce a third c, it is only necessary to write

 c = a + b

In addition to support for vectorized arithmetic and relational operations, these languages also vectorize common mathematical functions such as sine. For example, if x is an array, then

 y = sin (x)

will result in an array y whose elements are sine of the corresponding elements of the array x.

Vectorized index operations are also supported. As an example,

 even = x(2::2); odd = x(::2);

is how one would use Fortran to create arrays from the even and odd entries of an array. Another common use of vectorized indices is a filtering operation. Consider a clipping operation of a sine wave where amplitudes larger than 0.5 are to be set to 0.5. Using S-Lang, this may accomplished by

 y = sin(x); y[where(abs(y)>0.5)] = 0.5;

Mathematical matrices management

Language/LibraryCreateDeterminantTransposeElementColumnRowEigenvalues
Fortranm=RESHAPE([x11,x12,...], SHAPE(m)) TRANSPOSE(m)m(i,j)m(:,j)m(i,:)
Ch [1]m = {...}determinant( m )transpose( m )m[i-1][j-1]shape(m,0)shape(m,1)eigen(output, m, NULL)
Mathematicam={{x11,x12,...},...}Det[m]Transpose[m]m[[i,j]]m[[; , j]]m[[i]]Eigenvalues[m]
MATLAB / GNU Octavem = [...]det( m )m'm( i, j )m( :, j )m( i, : )eig( m )
NumPym=mat(...) m.Tm[i-1,j-1]m[:,i-1]m[i-1,:]
S-Langm=reshape([x11,x12,...], [new-dims]) m=transpose(m)m[i,j]m[*,j]m[j,*]
SymPym=Matrix(...) m.Tm[i-1,j-1]  

References

(Sebelumnya) Comparison of platform virtual ...Comparison of programming lang ... (Berikutnya)