packX←{                           ⍝ TeXt packer.

    ⎕IO←0                         ⍝ environment

    cmp←{                         ⍝ packer
        u←∪,⍵                     ⍝ unique items, odd or even
        p←↓{((⌈0.5×⍴⍵),2)⍴⍵},⍵    ⍝ divide into pairs
        j←,u∘.,u                  ⍝ unique couples
        bjpgb/j             ⍝ take only the needed pairs
        igp                     ⍝ indices
        w←⌈2⍟⍴g                   ⍝ bit width
        q←0∨b,,(w⍴2)i            ⍝ bits to bitvec
        u((⍴⍴⍵),⍴⍵)q              ⍝ return uniques, shape, bits
    }

    unc←{                         ⍝ unpack
        (u d a)←⍵                 ⍝ handle arg
        s←0⊃drs⍴1↓d           ⍝ shape
        j←,u∘.,u                  ⍝ unique pairs..
        p((j)a)/j              ⍝ ..used couples
        q(j)a                  ⍝ indices
        w←⌈2⍟⍴p                   ⍝ bitwidth
        i←2⊥w{(⍺,⌊(⍴⍵)÷⍺)⍴⍵}q     ⍝ calc indices
        r⍴∊p[i]                   ⍝ return array
    }

    ⍺←1 ⋄ ⍺=1:cmp ⍵ ⋄ unc ⍵       ⍝ pack/unpack
}

code_colours

test script

Back to: notes

Back to: Workspaces