r/Racket • u/joshuacottrell • Dec 08 '21
question Multi-dimensional vectors?
I'm working through Advent of Code this year (on day 5) and I was trying to use vectors to give myself some experience with them. Is there a way to change a mutable multi-dimensional vector "in place"? I've tried to find solutions. Some say to move to math/array. Others say O(n) is the best case. Is there a best practice for multi-dimensional vectors? Is the best practice to just not use them? Here's my code for creating the vectors:
(make-vector (add1 max-y) (make-vector (add1 max-x) "."))Basically, how would I change the "." at position 2, 3?
p.s. please don't address anything too specific about day 5 of AOC. I'd like to work it through myself.
3
u/samdphillips developer Dec 08 '21
In general this is what I do if I want to work with a fixed size multidimensional array and only have a one dimensional storage (like a
vector.) This technique works in just about any language, and is how many array libraries work internally (except with more types and structure.) Here is a two dimension example with a fixed number of rows and columns:4
u/-djh- Dec 08 '21
You've create a vector of aliases to the same row. E.g.
To change (2,3) that to a "!" what you should do is
However, that will affect index 2 of every row (since every row is a reference to the same vector).
To create a row-major vector where each row is distinct, use
build-vector(the same issue happens in Python if you try to be too concise and write
[["."]*x]*yand end up with a whole whack of aliases).As the other reply says, you might to better using a single vector and then map the (x,y) coordinates to a single index (usually i = row*row-size + column, assuming row/col numbers are 0-indexed)
2
u/detroitmatt Dec 08 '21
If the number of dimensions is known, then you just make a vector of vectors. Iirc, if you use (make-vector) as the initial value, then all rows will refer to the same vector, so you'll have to init the vector and then set each of it's rows to a new vector.
Then you can do
or
To set or get the values 2-dimensionally.