Arrays: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (Automated syntax highlighting fixup (second round - minor fixes)) |
No edit summary |
||
Line 3,668: | Line 3,668: | ||
Semantically the <code>update</code> function returns a new array, but the compiler is at liberty to re-use the memory where array <code>as</code> is stored, rather than create a copy as is normally needed in pure languages. Whenever the compiler encounters a call <code>update(as,i,x)</code>, it checks that the <code>as</code> is not used again. This prevents the in-place update from being observable, except through the return value of <code>modify</code>. |
Semantically the <code>update</code> function returns a new array, but the compiler is at liberty to re-use the memory where array <code>as</code> is stored, rather than create a copy as is normally needed in pure languages. Whenever the compiler encounters a call <code>update(as,i,x)</code>, it checks that the <code>as</code> is not used again. This prevents the in-place update from being observable, except through the return value of <code>modify</code>. |
||
=={{header|FutureBasic}}== |
|||
<syntaxhighlight lang="futurebasic">window 1, @"FutureBasic Arrays" |
|||
begin globals |
|||
dynamic gA1(10) as long |
|||
dynamic gA2(10) as Str255 |
|||
end globals |
|||
void local fn CType |
|||
long i |
|||
text ,, fn ColorGray |
|||
print @"// C-type fixed-length" |
|||
text |
|||
long a1(4) |
|||
a1(0) = 10 |
|||
a1(1) = 5 |
|||
a1(2) = 12 |
|||
a1(3) = 8 |
|||
a1(4) = 7 |
|||
for i = 0 to 4 |
|||
print a1(i), |
|||
next |
|||
print |
|||
a1(0) = 24 |
|||
a1(2) = 18 |
|||
a1(4) = 76 |
|||
for i = 0 to 4 |
|||
print a1(i), |
|||
next |
|||
print |
|||
CFStringRef a2(4) |
|||
a2(0) = @"Alpha" |
|||
a2(1) = @"Bravo" |
|||
a2(2) = @"Tango" |
|||
a2(3) = @"Delta" |
|||
a2(4) = @"Echo" |
|||
for i = 0 to 4 |
|||
print a2(i), |
|||
next |
|||
print |
|||
a2(2) = @"Charlie" |
|||
for i = 0 to 4 |
|||
print a2(i), |
|||
next |
|||
print : print |
|||
end fn |
|||
void local fn CTypeDynamic |
|||
long i |
|||
text ,, fn ColorGray |
|||
print @"// C-type dynamic" |
|||
text |
|||
gA1(0) = 46 |
|||
gA1(1) = 38 |
|||
gA1(10) = 67 |
|||
for i = 0 to fn DynamicNextElement( dynamic(gA1) ) - 1 |
|||
print gA1(i), |
|||
next |
|||
print |
|||
gA1(5) = 19 |
|||
gA1(10) = 22 |
|||
for i = 0 to fn DynamicNextElement( dynamic(gA1) ) - 1 |
|||
print gA1(i), |
|||
next |
|||
print |
|||
gA2(0) = "Kilo" |
|||
gA2(1) = "Lima" |
|||
gA2(5) = "Mike" |
|||
for i = 0 to fn DynamicNextElement( dynamic(gA2) ) - 1 |
|||
print gA2(i), |
|||
next |
|||
print |
|||
gA2(1) = "November" |
|||
gA2(6) = "Oscar" |
|||
for i = 0 to fn DynamicNextElement( dynamic(gA2) ) - 1 |
|||
print gA2(i), |
|||
next |
|||
print : print |
|||
end fn |
|||
void local fn CoreFoundationImmutable |
|||
long i |
|||
text ,, fn ColorGray |
|||
print @"// CoreFoundation (CF) immutable" |
|||
text |
|||
CFArrayRef a5 = @[@10,@5,@12,@8,@7] |
|||
for i = 0 to 4 |
|||
print a5[i], |
|||
next |
|||
print |
|||
CFArrayRef a6 = @[@"Alpha",@"Bravo",@"Charlie",@"Delta",@"Echo"] |
|||
for i = 0 to 4 |
|||
print a6[i], |
|||
next |
|||
print : print |
|||
end fn |
|||
void local fn CoreFoundationMutableFixedLength |
|||
long i |
|||
text ,, fn ColorGray |
|||
print @"// CoreFoundation (CF) mutable, fixed-length" |
|||
text |
|||
CFMutableArrayRef a1 = fn MutableArrayWithCapacity(3) |
|||
MutableArrayAddObject( a1, @79 ) |
|||
MutableArrayAddObject( a1, @43 ) |
|||
MutableArrayAddObject( a1, @101) |
|||
for i = 0 to len(a1) - 1 |
|||
print a1[i], |
|||
next |
|||
print |
|||
MutableArrayReplaceObjectAtIndex( a1, @15, 2 ) |
|||
for i = 0 to len(a1) - 1 |
|||
print a1[i], |
|||
next |
|||
print |
|||
CFMutableArrayRef a2 = fn MutableArrayWithCapacity(4) |
|||
MutableArrayAddObject( a2, @"Whisky" ) |
|||
MutableArrayAddObject( a2, @"Oscar" ) |
|||
MutableArrayAddObject( a2, @"Yankee" ) |
|||
MutableArrayAddObject( a2, @"Sierra" ) |
|||
for i = 0 to len(a2) - 1 |
|||
print a2[i], |
|||
next |
|||
print |
|||
MutableArrayReplaceObjectAtIndex( a2, @"Xray", 1 ) |
|||
MutableArrayReplaceObjectAtIndex( a2, @"Zulu", 3 ) |
|||
for i = 0 to len(a2) - 1 |
|||
print a2[i], |
|||
next |
|||
print : print |
|||
end fn |
|||
void local fn CoreFoundationMutableDynamic |
|||
long i |
|||
text ,, fn ColorGray |
|||
print @"// CoreFoundation (CF) mutable, dynamic" |
|||
text |
|||
CFMutableArrayRef a1 = fn MutableArrayWithCapacity(0) |
|||
MutableArrayAddObject( a1, @"Juliet" ) |
|||
MutableArrayAddObject( a1, @"Golf" ) |
|||
MutableArrayAddObject( a1, @"India" ) |
|||
for i = 0 to len(a1) - 1 |
|||
print a1[i], |
|||
next |
|||
print |
|||
MutableArrayReplaceObjectAtIndex( a1, @"Foxtrot", 0 ) |
|||
MutableArrayReplaceObjectAtIndex( a1, @"Hotel", 2 ) |
|||
for i = 0 to len(a1) - 1 |
|||
print a1[i], |
|||
next |
|||
end fn |
|||
fn CType |
|||
fn CTypeDynamic |
|||
fn CoreFoundationImmutable |
|||
fn CoreFoundationMutableFixedLength |
|||
fn CoreFoundationMutableDynamic |
|||
HandleEvents</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
// C-type fixed-length |
|||
10 5 12 8 7 |
|||
24 5 18 8 76 |
|||
Alpha Bravo Tango Delta Echo |
|||
Alpha Bravo Charlie Delta Echo |
|||
// C-type dynamic |
|||
46 38 0 0 0 0 0 0 0 0 67 |
|||
46 38 0 0 0 19 0 0 0 0 22 |
|||
Kilo Lima Mike |
|||
Kilo November Mike Oscar |
|||
// CoreFoundation (CF) immutable |
|||
10 5 12 8 7 |
|||
Alpha Bravo Charlie Delta Echo |
|||
// CoreFoundation (CF) mutable, fixed-length |
|||
79 43 101 |
|||
79 43 15 |
|||
Whisky Oscar Yankee Sierra |
|||
Whisky Xray Yankee Zulu |
|||
// CoreFoundation (CF) mutable, dynamic |
|||
Juliet Golf India |
|||
Foxtrot Golf Hotel |
|||
</pre> |
|||
=={{header|Gambas}}== |
=={{header|Gambas}}== |