Cabbage Logo
Back to Cabbage Site

Problems Trimming a 2D Array

I’m not sure if this is the right category, but I’m trying to change the size of a 2d array at k-rate. I want it to work like the trim opcode, but for 2d arrays. (I don’t want it to maintain the array capacity like reshapearray). So far, I’ve tried using the getrow and setrow opcodes to separate the array into 1d arrays, trimming it, then putting it back into a 2d array, but that’s inefficient and I can’t get it to work. Here’s what I’d been using:

kSeq[][] init 3, 16
;-----------------------
kr1[] getrow kSeq, 1
kr2[] getrow kSeq, 2
kr3[] getrow kSeq, 3
;---------------------------
trim kr1, kLeng
trim kr2, kLeng
trim kr3, kLeng
;-----------------------------
kSeq setrow kr1, 1
kSeq setrow kr2, 2
kSeq setrow kr3, 3

The message I get when I increase kLeng is:

PERF ERROR in instr 1 line 62: Array too small (allocated 128 < needed 136), but can’t allocate during performance pass. Allocate a bigger array at init time

Line 62 is the first trim opcode.

Is there any better way to trim 2d arrays? If not, any idea why this doesn’t work?

Thanks!

Interesting problem. Is a global array out of the question? If not you could easily resize the array like this, where I use two sliders to set the number of rows and columns. It certainly seems less complex than trying to resize them during performance time, which I’m not even sure it possible.

instr 2
    kRows = chnget:k("rows")
    kCols = chnget:k("cols")

    kTrig changed2 kRows, kCols
    if kTrig == 1 then
        event "i", 3, 0, 0, kRows, kCols
    endif
endin

instr 3
    gkSeq[][] init p4, p5
    print lenarray(gkSeq)
    print lenarray(gkSeq, 2)
endin

FWIW, I recently had to come up with a solution to resizing single dimension arrays. In the end I just set each array to a large enough size to cater for everything, and then reserved the first element of each to hold the array size. This allowed me to quickly change ‘sizes’ on the fly, even though I wasn’t reallocated anything at all.

1 Like

Thank you! That will work for what I’m wanting to do. I just realized I could also probably use a reinit opcode to reinitialize the sequence if the kRows or kCols is changed, which is basically what you’re doing here. The other option of having a longer array would also work, I just never thought about that before. This will help me get on with my project, thanks again!

Actually, I am not sure a reinit will work :thinking: I remember trying that once and having issues. At some point the array size is set to 0 which isn’t valid and return an i-time error. But it was a while ago since I tried. Maybe I missed a trick :joy:

That’s interesting. I’ll do some experimenting, but I’ll probably end up going with your idea. A while back I tried reinit on 1d arrays, and I couldn’t get it to work either. I eventually just used the trim opcode, but I assumed I was just doing something wrong :sweat_smile:. Even if it is possible, the other way probably will be much more straightforward. Thanks again for the help!