tasks a-s

This commit is contained in:
Ingy döt Net 2013-04-10 23:57:08 -07:00
parent 47bf37c096
commit b83f433714
12433 changed files with 156208 additions and 123 deletions

View file

@ -0,0 +1 @@
{{basic data operation}}In this task, the goal is to demonstrate common operations on pointers and references.

View file

@ -0,0 +1,2 @@
---
note: Basic Data Operations

View file

@ -0,0 +1,2 @@
INT var := 3;
REF INT pointer := var;

View file

@ -0,0 +1,3 @@
[9,9]INT sudoku;
REF [,]INT middle;
middle := sudoku[4:6,4:6];

View file

@ -0,0 +1,4 @@
[30]CHAR hay stack := "straw straw needle straw straw";
REF[]CHAR needle = hay stack[13:18];
needle[2:3] := "oo";
print((hay stack))

View file

@ -0,0 +1,2 @@
INT v = pointer; # sets v to the value of var (i.e. 3) #
REF INT(pointer) := 42; # sets var to 42 #

View file

@ -0,0 +1,2 @@
INT othervar;
pointer := othervar;

View file

@ -0,0 +1 @@
pointer := NIL; # 0 cannot be cast to NIL #

View file

@ -0,0 +1,2 @@
[9]INT array;
pointer := array[LWB array];

View file

@ -0,0 +1 @@
REF INT alias = var;

View file

@ -0,0 +1,2 @@
INT v2 = alias; # sets v2 to the value of var, that is, 3 #
alias := 42; # sets var to 42 #

View file

@ -0,0 +1 @@
printf(($"alias "b("IS","ISNT")" var!"l$, alias IS var));

View file

@ -0,0 +1,2 @@
[9]INT array2;
REF INT ref3 = array2[LWB array2];

View file

@ -0,0 +1,2 @@
type Int_Access is access Integer;
Int_Acc : Int_Access := new Integer'(5);

View file

@ -0,0 +1 @@
type Safe_Int_Access is not null access Integer;

View file

@ -0,0 +1,7 @@
declare
type Int_Ptr is access all Integer;
Ref : Int_Ptr;
Var : aliased Integer := 3;
Val : Integer := Var;
begin
Ref := Var'Access; -- "Ref := Val'Access;" would be a syntax error

View file

@ -0,0 +1 @@
type Safe_Int_Ptr is not null access all Integer;

View file

@ -0,0 +1,2 @@
Var : Integer;
Var_Address : Address := Var'Address;

View file

@ -0,0 +1,5 @@
-- Demonstrate the overlay of one object on another
A : Integer;
B : Integer;
for B'Address use A'Address;
-- A and B start at the same address

View file

@ -0,0 +1,8 @@
type Container is array (Positive range <>) of Element;
for I in Container'Range loop
declare
Item : Element renames Container (I);
begin
Do_Something(Item); -- Here Item is a reference to Container (I)
end;
end loop;

View file

@ -0,0 +1,4 @@
type Container is array (Positive range <>) of Element;
for Item of Container loop
Do_Something(Item);
end loop;

View file

@ -0,0 +1,5 @@
VarSetCapacity(var, 100) ; allocate memory
NumPut(87, var, 0, "Char") ; store 87 at offset 0
MsgBox % NumGet(var, 0, "Char") ; get character at offset 0 (87)
MsgBox % &var ; address of contents pointed to by var structure
MsgBox % *&var ; integer at address of var contents (87)

View file

@ -0,0 +1,26 @@
REM Pointer to integer variable:
pointer_to_varA = ^varA%
!pointer_to_varA = 123456
PRINT !pointer_to_varA
REM Pointer to variant variable:
pointer_to_varB = ^varB
|pointer_to_varB = PI
PRINT |pointer_to_varB
REM Pointer to procedure:
PROCmyproc : REM conventional call to initialise
pointer_to_myproc = ^PROCmyproc
PROC(pointer_to_myproc)
REM Pointer to function:
pointer_to_myfunc = ^FNmyfunc
PRINT FN(pointer_to_myfunc)
END
DEF PROCmyproc
PRINT "Executing myproc"
ENDPROC
DEF FNmyfunc
= "Returned from myfunc"

View file

@ -0,0 +1,2 @@
int var;
int* ptr = &var;

View file

@ -0,0 +1,9 @@
struct S{}
class C{}
void foo(S s){} // pass by value
void foo(C c){} // pass by reference
void foo(int i){} // pass by value
void foo(int[4] i){} // pass by value
void foo(int[] i){} // pass by reference
void foo(ref T t){} // pass by reference regardless of what type T really is

View file

@ -0,0 +1 @@
real, pointer :: pointertoreal

View file

@ -0,0 +1,6 @@
type intpointer
integer, pointer :: p
end type intpointer
!...
type(intpointer), dimension(100) :: parray

View file

@ -0,0 +1 @@
nullify(pointertoreal)

View file

@ -0,0 +1 @@
real, pointer :: apointer => NULL()

View file

@ -0,0 +1,2 @@
real, target :: areal
pointertoreal => areal

View file

@ -0,0 +1 @@
if ( associated(pointertoreal) ) !...

View file

@ -0,0 +1,2 @@
integer, dimension(:), pointer :: array
allocate(array(100))

View file

@ -0,0 +1,4 @@
integer, target :: i
integer, pointer :: pi
!... ...
if ( associated(pi, target=i) ) !...

View file

@ -0,0 +1,8 @@
real, dimension(20), target :: a
real, dimension(20,20), target :: b
real, dimension(:), pointer :: p
p => a(5:20)
! p(1) == a(5), p(2) == a(6) ...
p => b(10,1:20)
! p(1) == b(10,1), p(2) == b(10,2) ...

View file

@ -0,0 +1,4 @@
real, dimension(20) :: a
real, dimension(16) :: p
p = a(5:20)

View file

@ -0,0 +1,2 @@
var p *int // declare p to be a pointer to an int
i = &p // assign i to be the int value pointed to by p

View file

@ -0,0 +1 @@
var m map[string]int

View file

@ -0,0 +1 @@
m := make(map[string]int)

View file

@ -0,0 +1,4 @@
b := []byte(hello world)
c := b
c[0] = 'H'
fmt.Println(string(b))

View file

@ -0,0 +1,4 @@
func three() *int {
i := 3
return &i // valid. no worry, no crash.
}

View file

@ -0,0 +1,5 @@
type pt struct {
x, y int
}
return &pt{22, 79} // allocates a pt object and returns a pointer to it.

View file

@ -0,0 +1,7 @@
import Data.STRef
example :: ST s ()
example = do
p <- newSTRef 1
k <- readSTRef p
writeSTRef p (k+1)

View file

@ -0,0 +1,9 @@
public class Foo { public int x = 0; }
void somefunction() {
Foo a; // this declares a reference to Foo object; if this is a class field, it is initialized to null
a = new Foo(); // this assigns a to point to a new Foo object
Foo b = a; // this declares another reference to point to the same object that "a" points to
a.x = 5; // this modifies the "x" field of the object pointed to by "a"
System.out.println(b.x); // this prints 5, because "b" points to the same object as "a"
}

View file

@ -0,0 +1,5 @@
TYPE IntRef = REF INTEGER;
VAR intref := NEW(IntRef);
intref^ := 10

View file

@ -0,0 +1,2 @@
VAR any: REFANY;
any := NEW(REF INTEGER); (* Modula-3 knows that any is now REF INTEGER with a tag added by NEW. *)

View file

@ -0,0 +1,13 @@
PROCEDURE Sum(READONLY a: ARRAY OF REFANY): REAL =
VAR sum := 0.0;
BEGIN
FOR i := FIRST(a) TO LAST(a) DO
TYPECASE a[i] OF
| NULL => (* skip *)
| REF INTEGER (e) => sum := sum + FLOAT(e^);
| REF REAL(e) => sum := sum + e^;
ELSE (* skip *)
END;
END;
RETURN sum;
END Sum;

View file

@ -0,0 +1,3 @@
let p = ref 1;; (* create a new "reference" data structure with initial value 1 *)
let k = !p;; (* "dereference" the reference, returning the value inside *)
p := k + 1;; (* set the value inside to a new value *)

View file

@ -0,0 +1,3 @@
n=1;
issquare(9,&n);
print(n); \\ prints 3

View file

@ -0,0 +1,28 @@
<?php
/* Assignment of scalar variables */
$a = 1;
$b =& $a; // $b and $a are now linked together
$b = 2; //both $b and $a now equal 2
$c = $b;
$c = 7; //$c is not a reference; no change to $a or $b
unset($a); //won't unset $b, just $a.
/* Passing by Reference in and out of functions */
function &pass_out() {
global $filestr; //$exactly equivalent to: $filestr =& $_GLOBALS['filestr'];
$filestr = get_file_contents("./bigfile.txt");
return $_GLOBALS['filestr'];
}
function pass_in(&$in_filestr) {
echo "File Content Length: ". strlen($in_filestr);
/* Changing $in_filestr also changes the global $filestr and $tmp */
$in_filestr .= "EDIT";
echo "File Content Length is now longer: ". strlen($in_filestr);
}
$tmp = &pass_out(); // now $tmp and the global variable $filestr are linked
pass_in($tmp); // changes $tmp and prints the length
?>

View file

@ -0,0 +1,13 @@
my $foo = 42; # place a reference to 42 in $foo's item container
$foo++; # deref $foo name, then increment the container's contents to 43
$foo.say; # deref $foo name, then $foo's container, and call a method on 43.
$foo := 42; # bind a direct ref to 42
$foo++; # ERROR, cannot modify immutable value
my @bar = 1,2,3; # deref @bar name to array container, then set its values
@bar»++; # deref @bar name to array container, then increment each value with a hyper
@bar.say; # deref @bar name to array container, then call say on that, giving 2 3 4
@bar := (1,2,3); # bind name directly to a Parcel
@bar»++; # ERROR, parcels are not mutable

View file

@ -0,0 +1,9 @@
# start with some var definitions
my $scalar = 'a string';
my @array = ('an', 'array');
my %hash = ( firstkey => 'a', secondkey => 'hash' );
# make references
my $scalarref = \$scalar;
my $arrayref = \@array;
my $hashref = \%hash;

View file

@ -0,0 +1,9 @@
# printing the value
print ${$scalar};
print $arrayref->[1]; # this would print "array"
print $hashref->{'secondkey'}; # this would print "hash"
# changing the value
${$scalar} = 'a new string'; # would change $scalar as well
$arrayref->[0] = 'an altered'; # would change the first value of @array as well
$hashref->{'firstkey'} = 'a good'; # would change the value of the firstkey name value pair in %hash

View file

@ -0,0 +1,3 @@
my $scalarref = \'a scalar';
my $arrayref = ['an', 'array'];
my $hashref = { firstkey => 'a', secondkey => 'hash' }

View file

@ -0,0 +1,11 @@
: (setq L (1 a 2 b 3 c)) # Create a list of 6 items in 'L'
-> (1 a 2 b 3 c)
: (nth L 4) # Get a pointer to the 4th item
-> (b 3 c)
: (set (nth L 4) "Hello") # Store "Hello" in that location
-> "Hello"
: L # Look at the modified list in 'L'
-> (1 a 2 "Hello" 3 c)

View file

@ -0,0 +1,4 @@
Define varA.i = 5, varB.i = 0, *myInteger.Integer
*myInteger = @varA ;set pointer to address of an integer variable
varB = *myInteger\i + 3 ;set variable to the 3 + value of dereferenced pointer, i.e varB = 8

View file

@ -0,0 +1,2 @@
Define varC.i = 12
*myInteger = @varC

View file

@ -0,0 +1 @@
*myInteger = #Null ;or anything evaluating to zero

View file

@ -0,0 +1,6 @@
Dim myArray(10)
*myInteger = myArray()
;Or alternatively:
*myInteger = @myArray(0)
;any specific element
*myInteger = @myArray(4) ;element 4

View file

@ -0,0 +1,2 @@
*myInteger + 3 * SizeOf(Integer) ;pointer now points to myArray(3)
*myInteger - 2 * SizeOf(Integer) ;pointer now points to myArray(1)

View file

@ -0,0 +1,10 @@
Structure employee
id.i
name.s
jobs.s[20] ;array of job descriptions
EndStructure
Dim employees.employee(10) ;an array of employee's
;set a string pointer to the 6th job of the 4th employee
*myString.String = @employees(3) + OffsetOf(employee\jobs) + 5 * SizeOf(String)

View file

@ -0,0 +1,2 @@
*pointer = @varA
*pointer = @myFunction()

View file

@ -0,0 +1,20 @@
; Getting the address of a lable in the code
text$="'Lab' is at address "+Str(?lab)
MessageRequester("Info",text$)
; Using lables to calculate size
text$="Size of the datasetion is "+Str(?lab2-?lab)+" bytes."
MessageRequester("Info",text$)
; Using above to copy specific datas
Define individes=(?lab2-?lab1)/SizeOf(Integer)
Dim Stuff(individes-1) ; As PureBasic uses 0-based arrays
CopyMemory(?lab1,@Stuff(),?lab2-?lab1)
DataSection
lab:
Data.s "Foo", "Fuu"
lab1:
Data.i 3,1,4,5,9,2,1,6
lab2:
EndDataSection

View file

@ -0,0 +1,32 @@
# Bind a literal string object to a name:
a = "foo"
# Bind an empty list to another name:
b = []
# Classes are "factories" for creating new objects: invoke class name as a function:
class Foo(object):
pass
c = Foo()
# Again, but with optional initialization:
class Bar(object):
def __init__(self, initializer = None)
# "initializer is an arbitrary identifier, and "None" is an arbitrary default value
if initializer is not None:
self.value = initializer
d = Bar(10)
print d.value
# Test if two names are references to the same object:
if a is b: pass
# Alternatively:
if id(a) == id(b): pass
# Re-bind a previous used name to a function:
def a(fmt, *args):
if fmt is None:
fmt = "%s"
print fmt % (args)
# Append reference to a list:
b.append(a)
# Unbind a reference:
del(a)
# Call (anymous function object) from inside a list
b[0]("foo") # Note that the function object we original bound to the name "a" continues to exist
# even if its name is unbound or rebound to some other object.

View file

@ -0,0 +1,13 @@
/* Using ADDR to get memory address, and PEEKC / POKE. There is also PEEK for numeric values. */
data _null_;
length a b c $4;
adr_a=addr(a);
adr_b=addr(b);
adr_c=addr(c);
a="ABCD";
b="EFGH";
c="IJKL";
b=peekc(adr_a,1);
call poke(b,adr_c,1);
put a b c;
run;

View file

@ -0,0 +1,3 @@
val p = ref 1; (* create a new "reference" data structure with initial value 1 *)
val k = !p; (* "dereference" the reference, returning the value inside *)
p := k + 1; (* set the value inside to a new value *)

View file

@ -0,0 +1,5 @@
set var 3
set pointer var; # assign name "var" not value 3
set pointer; # returns "var"
set $pointer; # returns 3
set $pointer 42; # variable var now has value 42

View file

@ -0,0 +1,9 @@
set arr(var) 3
set pointer var
set arr($pointer); # returns 3
set arr($pointer) 42; # arr(var) now has value 42
set var 3
set pointer var
upvar 0 $pointer varAlias; # varAlias is now the same variable as var
set varAlias 42; # var now has value 42

View file

@ -0,0 +1,32 @@
\Paraphrasing the C example:
\This creates a pointer to an integer variable:
int Var, Ptr, V;
Ptr:= @Var;
\Access the integer variable through the pointer:
Var:= 3;
V:= Ptr(0); \set V to the value of Var, i.e. 3
Ptr(0):= 42; \set Var to 42
\Change the pointer to refer to another integer variable:
int OtherVar;
Ptr:= @OtherVar;
\Change the pointer to not point to anything:
Ptr:= 0; \or any integer expression that evaluates to 0
\Set the pointer to the first item of an array:
int Array(10);
Ptr:= Array;
\Or alternatively:
Ptr:= @Array(0);
\Move the pointer to another item in the array:
def IntSize = 4; \number of bytes in an integer
Ptr:= Ptr + 3*IntSize; \pointer now points to Array(3)
Ptr:= Ptr - 2*IntSize; \pointer now points to Array(1)
\Access an item in the array using the pointer:
V:= Ptr(3); \get third item after Array(1), i.e. Array(4)
V:= Ptr(-1); \get item immediately preceding Array(1), i.e. Array(0)
]