This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -0,0 +1,20 @@
PROC array_test = VOID:
(
[1:20]INT a;
a := others; # assign whole array #
a[1] := -1; # assign individual element #
a[3:5] := (2, 4, -1); # assign a slice #
[1:3]INT slice = a[3:5]; # copy a slice #
REF []INT rslice = a[3:5]; # create a reference to a slice #
print((LWB rslice, UPB slice)); # query the bounds of the slice #
rslice := (2, 4, -1); # assign to the slice, modifying original array #
[1:3, 1:3]INT matrix; # create a two dimensional array #
REF []INT hvector = matrix[2,]; # create a reference to a row #
REF []INT vvector = matrix[,2]; # create a reference to a column #
REF [,]INT block = matrix[1:2, 1:2]; # create a reference to an area of the array #
FLEX []CHAR string := "Hello, world!"; # create an array with variable bounds #
string := "shorter" # flexible arrays automatically resize themselves on assignment #
)

View file

@ -0,0 +1,17 @@
DEF ai[100] : ARRAY OF CHAR, -> static
da: PTR TO CHAR,
la: PTR TO CHAR
PROC main()
da := New(100)
-> or
NEW la[100]
IF da <> NIL
ai[0] := da[0] -> first is 0
ai[99] := da[99] -> last is "size"-1
Dispose(da)
ENDIF
-> using NEW, we must specify the size even when
-> "deallocating" the array
IF la <> NIL THEN END la[100]
ENDPROC

View file

@ -0,0 +1,2 @@
set empty to {}
set ints to {1, 2, 3}

View file

@ -0,0 +1 @@
set any to {1, "foo", 2.57, missing value, ints}

View file

@ -0,0 +1,39 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. arrays.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 fixed-length-table.
03 fixed-table-elt PIC X OCCURS 5 TIMES.
01 table-length PIC 9(5) VALUE 1.
01 variable-length-table.
03 variable-table-elt PIC X OCCURS 1 TO 5 TIMES
DEPENDING ON table-length.
01 initial-value-area.
03 initial-values.
05 FILLER PIC X(10) VALUE "One".
05 FILLER PIC X(10) VALUE "Two".
05 FILLER PIC X(10) VALUE "Three".
03 initial-value-table REDEFINES initial-values.
05 initial-table-elt PIC X(10) OCCURS 3 TIMES.
01 indexed-table.
03 indexed-elt PIC X OCCURS 5 TIMES
INDEXED BY table-index.
PROCEDURE DIVISION.
*> Assigning the contents of an entire table.
MOVE "12345" TO fixed-length-table
*> Indexing an array (using an index)
MOVE 1 TO table-index
MOVE "1" TO indexed-elt (table-index)
*> Pushing a value into a variable-length table.
ADD 1 TO table-length
MOVE "1" TO variable-table-elt (2)
GOBACK
.

View file

@ -0,0 +1,2 @@
array :: {String}
array = {"Hello", "World"}

View file

@ -0,0 +1,2 @@
array :: {Real}
array = createArray 10 3.1415

View file

@ -0,0 +1,2 @@
array :: {Int}
array = {x \\ x <- [1 .. 10]}

View file

@ -0,0 +1,2 @@
array :: {!Int}
array = {x \\ x <- [1 .. 10]}

View file

@ -0,0 +1,2 @@
array :: {#Char}
array = {x \\ x <- ['a' .. 'z']}

View file

@ -0,0 +1,11 @@
// Declare and initialize two-dimensional array
Local arr1 := { { "NITEM","N",10,0 }, { "CONTENT","C",60,0} }
// Create an empty array
Local arr2 := {}
// Declare three-dimensional array
Local arr3[2,100,3]
// Create an array
Local arr4 := Array(50)
// Array can be dynamically resized:
arr4 := ASize( arr4, 80 )

View file

@ -0,0 +1,6 @@
// Adding new item to array, its size is incremented
Aadd( arr1, { "LBASE","L",1,0 } )
// Delete the first item of arr3, The size of arr3 remains the same, all items are shifted to one position, the last item is replaced by Nil:
ADel( arr1, 1 )
// Assigning a value to array item
arr3[1,1,1] := 11.4

View file

@ -0,0 +1,2 @@
x := arr3[1,10,2]
// The retrieved item can be nested array, in this case it isn't copied, the pointer to it is assigned

View file

@ -0,0 +1,7 @@
// Fill the 20 items of array with 0, starting from 5-th item:
AFill( arr4, 0, 5, 20 )
//Copy 10 items from arr4 to arr3[2], starting from the first position:
ACopy( arr4, arr3[2], 1, 10 )
//Duplicate the whole or nested array:
arr5 := AClone( arr1 )
arr6 := AClone( arr1[3] )

View file

@ -0,0 +1,8 @@
array1 = []
array1[0] = "Dillenidae"
array1[1] = "animus"
array1[2] = "Kona"
alert "Elements of array1: " + array1 # Dillenidae,animus,Kona
array2 = ["Cepphus", "excreta", "Gansu"]
alert "Value of array2[1]: " + array2[1] # excreta

View file

@ -0,0 +1 @@
<cfset arr1 = ArrayNew(1)>

View file

@ -0,0 +1,3 @@
<cfscript>
arr2 = ArrayNew(2);
</cfscript>

41
Task/Arrays/D/arrays-1.d Normal file
View file

@ -0,0 +1,41 @@
// All D arrays are capable of bounds checks.
import std.stdio, core.stdc.stdlib;
import std.container: Array;
void main() {
// GC-managed heap allocated dynamic array:
auto array1 = new int[1];
array1[0] = 1;
array1 ~= 3; // append a second item
// array1[10] = 4; // run-time error
writeln("A) Element 0: ", array1[0]);
writeln("A) Element 1: ", array1[1]);
// Stack-allocated fixed-size array:
int[5] array2;
array2[0] = 1;
array2[1] = 3;
// array2[2] = 4; // compile-time error
writeln("B) Element 0: ", array2[0]);
writeln("B) Element 1: ", array2[1]);
// Stack-allocated dynamic fixed-sized array,
// length known only at run-time:
int n = 2;
int[] array3 = (cast(int*)alloca(n * int.sizeof))[0 .. n];
array3[0] = 1;
array3[1] = 3;
// array3[10] = 4; // run-time error
writeln("C) Element 0: ", array3[0]);
writeln("C) Element 1: ", array3[1]);
// Phobos-defined heap allocated not GC-managed array:
Array!int array4;
array4.length = 2;
array4[0] = 1;
array4[1] = 3;
// array4[10] = 4; // run-time exception
writeln("D) Element 0: ", array4[0]);
writeln("D) Element 1: ", array4[1]);
}

11
Task/Arrays/D/arrays-2.d Normal file
View file

@ -0,0 +1,11 @@
import std.stdio, core.simd;
void main() {
// Stack-allocated vector for SIMD registers:
ubyte16 vector5;
vector5.array[0] = 1;
vector5.array[1] = 3;
// vector5.array[17] = 4; // Compile-time error.
writeln("E) Element 0: ", vector5.array[0]);
writeln("E) Element 1: ", vector5.array[1]);
}

View file

@ -0,0 +1,20 @@
#create a new list
local :l []
#add something to it
push-to l "Hi"
#add something else to it
push-to l "Boo"
#the list could also have been built up this way:
local :l2 [ "Hi" "Boo" ]
#this prints 2
print len l
#this prints Hi
print get-from l 0
#this prints Boo
print pop-from l

View file

@ -1,3 +1,3 @@
#var anArray := (1, 2, 3).
'program'Output << anArray@1.
system'console writeLine:(anArray@1).

View file

@ -1,4 +1,4 @@
#var anArray := basic'NewArray::3.
anArray@0 content'set:2.
#var anArray := system'Array new &type'length:5.
anArray setAt:0:2.
'program'Output << anArray@0.
system'console writeLine:(anArray@0).