多次元の配列を1つのインデックスで扱う - lisp-cookbook-ja/common-lisp GitHub Wiki

配列

row-major-arefを利用すれば、多次元の配列を1つのインデックスで扱うことができます。

(let ((a10x10 (make-array '(10 10))))
  (dotimes (i (floor (array-total-size a10x10) 2))
    (setf (row-major-aref a10x10 i) 1))
  a10x10)
;=> #2A((1 1 1 1 1 1 1 1 1 1)
;       (1 1 1 1 1 1 1 1 1 1)
;       (1 1 1 1 1 1 1 1 1 1)
;       (1 1 1 1 1 1 1 1 1 1)
;       (1 1 1 1 1 1 1 1 1 1)
;       (0 0 0 0 0 0 0 0 0 0)
;       (0 0 0 0 0 0 0 0 0 0)
;       (0 0 0 0 0 0 0 0 0 0)
;       (0 0 0 0 0 0 0 0 0 0)
;       (0 0 0 0 0 0 0 0 0 0))

上記は、make-arrayのオプションである:displaced-toを利用し、元の多次元配列をベクタにマッピングしたものと同様の効果を持ちます。

(let* ((a10x10 (make-array '(10 10)))
       (a100 (make-array 100 :displaced-to a10x10)))
  (dotimes (i 50)
    (setf (aref a100 i) 1))
  a10x10)

議論