Exercise 19 Index   <<   >>


 

Adapt xix to compute (≢∪)x . Do the same for xixSR .




























 z←xixNC x;⎕io;h;hf;i;j;m;q
⍝ nub count, (≢∪)x; written for easy translation into C

 ⎕io←0
 hf←{123457×⍵}                          ⍝ hash function
 m←≢x
 q←2*1+⌈2⍟m                             ⍝ size of hash table
 h←q⍴m                                  ⍝ hash table; m means "free"
 z←0                                    ⍝ initial count

 :For i :In ⍳m                          ⍝ index for each x
   j←q|hf x[i]                          ⍝ index into hash table
   :While m>h[j] ⋄ :AndIf x[h[j]]≠x[i]  ⍝ stop on finding m or =
     j←q|1+j                            ⍝ the next hash table entry
   :End
   :If m=h[j] ⋄ h[j]←i ⋄ z+←1 ⋄ :End    ⍝ new hash continuation; ...
 :End                                   ⍝ ... increment count


 z←xixSRNC x;⎕io;max;min;t
⍝ adaptation of xixSR to compute the nub count, (≢∪)x
 ⎕io←0
 min←⌊/x
 max←⌈/x
 t←(1+max-min)⍴0
 t[x-min]←1
 z←+/t


_____________________

   x←?1e4⍴8000

   xixNC x
5731
   xixSRNC x
5731

   (≢∪)x
5731
   ≢∪x
5731