Keyed Properties Example 1
The Sparse2 Class illustrates the implementation and use of a Keyed Property.
Sparse2
represents a 2-dimensional sparse array each of whose dimensions are indexed by arbitrary character keys. The sparse array is implemented as a Keyed Property named Values
. The following expressions show how it might be used.
SA1←⎕NEW Sparse2
SA1.Values[⊂'Widgets';⊂'Jan']←100
SA1.Values[⊂'Widgets';⊂'Jan']
100
SA1.Values['Widgets' 'Grommets';'Jan' 'Mar' 'Oct']←10×2 3⍴⍳6
SA1.Values['Widgets' 'Grommets';'Jan' 'Mar' 'Oct']
10 20 30
40 50 60
SA1.Values[⊂'Widgets';'Jan' 'Oct']
10 30
SA1.Values['Grommets' 'Widgets';⊂'Oct']
60
30
Sparse2 Class Example
:Class Sparse2 ⍝ 2D Sparse Array
:Field Private keys
:Field Private values
∇ make
:Access Public
:Implements Constructor
keys←0⍴⊂'' ''
values←⍬
∇
:Property Keyed Values
:Access Public Instance
∇ v←get arg;k
k←arg.Indexers
⎕SIGNAL(2≠⍴k)/4
k←fixkeys k
v←(values,0)[keys⍳k]
∇
∇ set arg;new;k;v;n
v←arg.NewValue
k←arg.Indexers
⎕SIGNAL(2≠⍴k)/4
k←fixkeys k
v←(⍴k)(⍴⍣(⊃1=⍴,v))v
⎕SIGNAL((⍴k)≠⍴v)/5
k v←,¨k v
:If ∨/new←~k∊keys
values,←new/v
keys,←new/k
k v/⍨←⊂~new
:EndIf
:If 0<⍴k
values[keys⍳k]←v
:EndIf
∇
:EndProperty
∇ k←fixkeys k
k←(2≠≡¨k){,(⊂⍣⍺)⍵}¨k
k←⊃(∘.{⊃,/⊂¨⍺ ⍵})/k
∇
:EndClass ⍝ 2D Sparse Array
Internally, Sparse2
maintains a list of keys and a list of values which are initialised to empty arrays by its constructor.
When an indexed assignment is made, the set
function receives a list of keys (indices) in arg.Indexer
and values in arg.NewValue
. The function updates the values of existing keys, and adds new keys and their values to the internal lists.
When an indexed reference is made, the get
function receives a list of keys (indices) in arg.Indexer
. The function uses these keys to retrieve the corresponding values, inserting 0s for non-existent keys.
Note that in the expression:
SA1.Values['Widgets' 'Grommets';'Jan' 'Mar' 'Oct']
the structure of arg.Indexer
is:
.→-----------------------------------------------.
| .→---------------------. .→------------------. |
| | .→------. .→-------. | | .→--. .→--. .→--. | |
| | |Widgets| |Grommets| | | |Jan| |Mar| |Oct| | |
| | '-------' '--------' | | '---' '---' '---' | |
| '∊---------------------' '∊------------------' |
'∊-----------------------------------------------'