packQ←{                             ⍝ Assorted uniQues packer.
    (⎕IO ⎕ML)←1 3                   ⍝ environment.
    key←↑,/{↓(⍳⍵)∘.>⍳⍵}¨⍳23         ⍝ binary keys (up to 276 uniq elts).

    cmp←{                           ⍝ compress:
        dt←,⍵                       ⍝ vectorize.
        u←{⍵{⍺[⍒⍵]}{+/⍵=dt}¨⍵}∪dt   ⍝ sort uniques according to frequency.
        k←1,¨keyd←∊k[udt]       ⍝ replace with keys.
        u((⍴⍴⍵),⍴⍵)d                ⍝ uniques (rank, shape) binary data vector.
    }

    unc←{                           ⍝ uncompress:
        (u q d)←⍵                   ⍝ split the argument.
        s(q)⍴1↓q                  ⍝ shape.
        pd≤¯1↓0,d                  ⍝ split with first 1's.
        su[keypd]                ⍝ reconstruct the result.
    }

    ⍺←1 ⋄ ⍺=1:cmp ⍵ ⋄ unc ⍵         ⍝ choose the tool.
}

code_colours

test script

Back to: notes

Back to: Workspaces