Difference between revisions of "References and Data Structures"

m
m
Line 4: Line 4:
 
A '''reference''' is an indirect link to a value, an atom or an array. A variable can contain a single reference to a value, or it can contain an array of references. Variables and arrays can themselves contain references, nested to any depth. This lets you create complex data structures, such as linked lists, trees, and non-rectangular structures. Use of references is provided by two operators:
 
A '''reference''' is an indirect link to a value, an atom or an array. A variable can contain a single reference to a value, or it can contain an array of references. Variables and arrays can themselves contain references, nested to any depth. This lets you create complex data structures, such as linked lists, trees, and non-rectangular structures. Use of references is provided by two operators:
  
* '''\e''' is the '''''reference operation'''''. It creates a reference to the value of expression e.
+
* '''\e''' is the '''''reference operation'''''. It creates a reference to the value of expression <code>e</code>.
 
* '''#e''' is the '''''dereference operation'''''. It obtains the value referred to by e. If e is not a reference, it issues a warning and returns Null.
 
* '''#e''' is the '''''dereference operation'''''. It obtains the value referred to by e. If e is not a reference, it issues a warning and returns Null.
  
 
An example:
 
An example:
Variable M
 
Definition: 100
 
  
Variable Ref_to_M
+
<code>Variable M</code>
Definition: \ M
 
  
The result of Ref_to_M looks like this:
+
<code>Definition: 100</code>
 +
 
 +
<code>Variable Ref_to_M</code>
 +
 
 +
<code>Definition: \ M</code>
 +
 
 +
The result of '''Ref_to_M''' looks like this:
  
 
[[File:result_ref-to-m.png|300px]]
 
[[File:result_ref-to-m.png|300px]]
Line 24: Line 27:
 
You can also create an array of references. Suppose:
 
You can also create an array of references. Suppose:
  
Index K
+
<code>Index K</code>
Definition: 1..5
 
  
Variable Ksquare
+
<code>Definition: 1..5</code>
Definition: K^2
 
  
Ksquare →
+
<code>Variable Ksquare</code>
 +
 
 +
<code>Definition: K^2</code>
 +
 
 +
<code>Ksquare →</code>
  
 
[[File:result-ksquare.png|300px]]
 
[[File:result-ksquare.png|300px]]
  
Variable Ref_to_Ksquare
+
<code>Variable Ref_to_Ksquare</code>
Definition: \ Ksquare
 
  
Ref_to_Ksquare →
+
<code>Definition: \ Ksquare</code>
 +
 
 +
<code>Ref_to_Ksquare →</code>
  
 
[[File:result_ref_ksquare.png|300px]]
 
[[File:result_ref_ksquare.png|300px]]
Line 47: Line 53:
 
You can also create an array of references from an array, for example:
 
You can also create an array of references from an array, for example:
  
Variable Ref_Ksquare_array
+
<code>Variable Ref_Ksquare_array</code>
Definition: \ [] Ksquare
 
Ksquare →
 
  
The empty square brackets [ ] specify that the values referred to have no indexes, i.e., they are atoms. You can now click
+
<code>Definition: \ [] Ksquare</code>
any of these cells to see what it refers to.
+
 
 +
<code>Ksquare →</code>
 +
 
 +
The empty square brackets [ ] specify that the values referred to have no indexes, i.e., they are atoms. You can now click any of these cells to see what it refers to.
  
 
[[File:result_ref_ksquare_array.png|300px]]
 
[[File:result_ref_ksquare_array.png|300px]]
Line 62: Line 69:
 
==Managing indexes of referenced subarrays: \[i, j,...] e==
 
==Managing indexes of referenced subarrays: \[i, j,...] e==
  
More generally, you can list in the square brackets any indexes of e that you want to be indexes of each subarray referenced by the result. The other indexes of e (if any) are used as indexes for the referencing array. Thus, in the example above, since there were no indexes in square brackets, the index K was used as an index of the reference array. If instead we write:
+
More generally, you can list in the square brackets any indexes of <code>e</code> that you want to be indexes of each subarray referenced by the result. The other indexes of <code>e</code> (if any) are used as indexes for the referencing array. Thus, in the example above, since there were no indexes in square brackets, the index <code>K</code> was used as an index of the reference array. If instead we write:
  
\ [K] Ksquare →
+
<code>\ [K] Ksquare →</code>
  
 
[[File:result_ref_ksquare_array3.png|300px]]
 
[[File:result_ref_ksquare_array3.png|300px]]
  
It creates a similar result to \ Ksquare, since K is the only index of Ksquare.
+
It creates a similar result to <code>\ Ksquare</code>, since <code>K</code> is the only index of <code>Ksquare</code>.
  
 
To summarize:
 
To summarize:
Line 74: Line 81:
 
:{| class="wikitable"
 
:{| class="wikitable"
 
|-
 
|-
|'''<code>\ e</code>'''
+
|<code>\ e</code>
|Creates a reference to the value of expression e, whether it is an atom or an array.  
+
|Creates a reference to the value of expression <code>e</code>, whether it is an atom or an array.  
 
|-
 
|-
|'''<code>\ [] e</code>'''
+
|<code>\ [] e</code>
|Creates an array indexed by all indexes of e containing references to all atoms from e.
+
|Creates an array indexed by all indexes of <code>e</code> containing references to all atoms from <code>e</code>.
 
|-
 
|-
 
|<code>\ [i] e</code>
 
|<code>\ [i] e</code>
|Creates an array indexed by any indexes of e other than i of references to subarrays of e each indexed by i.
+
|Creates an array indexed by any indexes of <code>e</code> other than <code>i</code> of references to subarrays of <code>e</code> each indexed by <code>i</code>.
 
|-
 
|-
 
|<code>\ [i, j…] e</code>
 
|<code>\ [i, j…] e</code>
|Creates an array indexed by any indexes of e other than i, j … of references to subarrays of e each indexed by i, j ….
+
|Creates an array indexed by any indexes of <code>e</code> other than <code>i, j …</code> of references to subarrays of <code>e</code> each indexed by <code>i, j ….</code>
 
|}  
 
|}  
  
Line 97: Line 104:
 
Linked lists are a common way for programmers to represent an ordered set of items. They are more efficient than arrays when you want often to add or remove items, thereby changing the length of the list (which is more time consuming for arrays). In Analytica, we can represent a linked list as an element with two elements, the item — that is, a reference to the value of the item — and a link — that is, a reference, to the next item:
 
Linked lists are a common way for programmers to represent an ordered set of items. They are more efficient than arrays when you want often to add or remove items, thereby changing the length of the list (which is more time consuming for arrays). In Analytica, we can represent a linked list as an element with two elements, the item — that is, a reference to the value of the item — and a link — that is, a reference, to the next item:
  
Index Linked_list
+
<code>Index Linked_list</code>
Definition: ['Item', 'Link']
+
 
 +
<code>Definition: ['Item', 'Link']</code>
 +
 
 +
 
 +
<code>Function LL_Put(x, LL)</code>
 +
 
 +
<code>Description: Puts item x onto linked list LL.</code>
 +
 
 +
<code>Definition: \Array(Linked_List, [\x, LL])</code>
 +
 
 +
 
 +
<code>Function LL_Get_Item(LL)</code>
 +
 
 +
<code>Description: Gets the value of the first
 +
item from linked list LL.</code>
 +
 
 +
<code>Definition: # Subscript(#LL, Linked_list, 'Item')</code>
 +
 
 +
 
 +
<code>Function LL_length(LL)</code>
 +
 
 +
<code>Parameters: (LL: Atom)</code>
 +
 
 +
<code>Description: Returns the number of items in
 +
linked list LL</code>
 +
 
 +
<code>Definition: VAR len := 0;
 +
WHILE (IsReference(LL)) BEGIN</code>
 +
 
 +
<code>LL := subscript(#LL, Linked_List, "Next");
 +
len := len + 1</code>
 +
 
 +
<code>END;</code>
 +
 
 +
<code>len</code>
 +
 
  
 +
<code>Function LL_from_array(a, i)</code>
  
Function LL_Put(x, LL)
+
<code>Parameters: (a; i: Index)</code>
Description: Puts item x onto linked list LL.
 
Definition: \Array(Linked_List, [\x, LL])
 
  
 +
<code>Description: Creates a linked list from the
 +
elements of array a over index i</code>
  
Function LL_Get_Item(LL)
+
<code>Definition:</code>
Description: Gets the value of the first
 
item from linked list LL.
 
Definition: # Subscript(#LL, Linked_list, 'Item')
 
  
 +
<code>VAR LL := NULL;</code>
  
Function LL_length(LL)
+
<code>Index iRev := Size(i) .. 1;</code>
Parameters: (LL: Atom)
 
Description: Returns the number of items in
 
linked list LL
 
Definition: VAR len := 0;
 
WHILE (IsReference(LL)) BEGIN
 
LL := subscript(#LL, Linked_List, "Next");
 
len := len + 1
 
END;
 
len
 
  
 +
<code>FOR j := iRev</code>
  
Function LL_from_array(a, i)
+
<code>DO LL := LL_Push(LL, Slice(a, i, j));</code>
Parameters: (a; i: Index)
 
Description: Creates a linked list from the
 
elements of array a over index i
 
Definition:
 
VAR LL := NULL;
 
Index iRev := Size(i) .. 1;
 
  
FOR j := iRev
+
<code>LL</code>
DO LL := LL_Push(LL, Slice(a, i, j));
 
LL
 
  
See Linked List Library.ana in the Libraries folder for these and other functions for working with linked lists.
+
See '''Linked List Library.ana''' in the '''Libraries''' folder for these and other functions for working with linked lists.
  
 
<footer>Ensuring Array Abstraction / {{PAGENAME}} / Handles to Objects</footer>
 
<footer>Ensuring Array Abstraction / {{PAGENAME}} / Handles to Objects</footer>

Revision as of 15:35, 17 December 2015


A reference is an indirect link to a value, an atom or an array. A variable can contain a single reference to a value, or it can contain an array of references. Variables and arrays can themselves contain references, nested to any depth. This lets you create complex data structures, such as linked lists, trees, and non-rectangular structures. Use of references is provided by two operators:

  • \e is the reference operation. It creates a reference to the value of expression e.
  • #e is the dereference operation. It obtains the value referred to by e. If e is not a reference, it issues a warning and returns Null.

An example:

Variable M

Definition: 100

Variable Ref_to_M

Definition: \ M

The result of Ref_to_M looks like this:

Result ref-to-m.png

You can double-click the cell containing «ref» to view the value referenced, in this case:

Result ref-to-m2.png

You can also create an array of references. Suppose:

Index K

Definition: 1..5

Variable Ksquare

Definition: K^2

Ksquare →

Result-ksquare.png

Variable Ref_to_Ksquare

Definition: \ Ksquare

Ref_to_Ksquare →

Result ref ksquare.png

If you click the «ref» cell, it opens:

Result ref ksquare2.png

You can also create an array of references from an array, for example:

Variable Ref_Ksquare_array

Definition: \ [] Ksquare

Ksquare →

The empty square brackets [ ] specify that the values referred to have no indexes, i.e., they are atoms. You can now click any of these cells to see what it refers to.

Result ref ksquare array.png

Clicking the third cell, for example, gives:

Result ref ksquare array2.png

Managing indexes of referenced subarrays: \[i, j,...] e

More generally, you can list in the square brackets any indexes of e that you want to be indexes of each subarray referenced by the result. The other indexes of e (if any) are used as indexes for the referencing array. Thus, in the example above, since there were no indexes in square brackets, the index K was used as an index of the reference array. If instead we write:

\ [K] Ksquare →

Result ref ksquare array3.png

It creates a similar result to \ Ksquare, since K is the only index of Ksquare.

To summarize:

\ e Creates a reference to the value of expression e, whether it is an atom or an array.
\ [] e Creates an array indexed by all indexes of e containing references to all atoms from e.
\ [i] e Creates an array indexed by any indexes of e other than i of references to subarrays of e each indexed by i.
\ [i, j…] e Creates an array indexed by any indexes of e other than i, j … of references to subarrays of e each indexed by i, j ….

In general, it is better to include the square brackets after the reference operator, and avoid the unadorned reference operator, as in the first row of the table. Being explicit about which indexes to include generally leads to expressions that array abstract as intended.

IsReference(x)

This Is a test to see whether its parameter x is a reference. It returns True (1) if x is a reference, False (0) otherwise.

Using references for linked lists: Example functions

Linked lists are a common way for programmers to represent an ordered set of items. They are more efficient than arrays when you want often to add or remove items, thereby changing the length of the list (which is more time consuming for arrays). In Analytica, we can represent a linked list as an element with two elements, the item — that is, a reference to the value of the item — and a link — that is, a reference, to the next item:

Index Linked_list

Definition: ['Item', 'Link']


Function LL_Put(x, LL)

Description: Puts item x onto linked list LL.

Definition: \Array(Linked_List, [\x, LL])


Function LL_Get_Item(LL)

Description: Gets the value of the first item from linked list LL.

Definition: # Subscript(#LL, Linked_list, 'Item')


Function LL_length(LL)

Parameters: (LL: Atom)

Description: Returns the number of items in linked list LL

Definition: VAR len := 0; WHILE (IsReference(LL)) BEGIN

LL := subscript(#LL, Linked_List, "Next"); len := len + 1

END;

len


Function LL_from_array(a, i)

Parameters: (a; i: Index)

Description: Creates a linked list from the elements of array a over index i

Definition:

VAR LL := NULL;

Index iRev := Size(i) .. 1;

FOR j := iRev

DO LL := LL_Push(LL, Slice(a, i, j));

LL

See Linked List Library.ana in the Libraries folder for these and other functions for working with linked lists.

Comments


You are not allowed to post comments.