Login
7 branches 0 tags
Ben (X13/Arch) Fixed stdlib indentation b228937 3 years ago 658 Commits
nujel / stdlib / collections / array2d.nuj
;; Nujel - Copyright (C) 2020-2021 - Benjamin Vincent Schulenburg
;; This project uses the MIT license, a copy should be included under /LICENSE
;;
;; Contains some routines for working with 2d data

[defn array/2d/allocate [width height]
      @[ :data [-> [array/allocate [* width height]]
                   [array/fill! 0]]
         :width width
         :height height]]

[defn array/2d/fill! [data v]
      [array/fill! [tree/ref data :data] v]
      data]

[defn array/2d/ref [data x y oob-val]
      [if [or [>= x [tree/ref data :width]]
              [>= y [tree/ref data :height]]
              [< x 0]
              [< y 0]]
          oob-val
          [array/ref [tree/ref data :data] [+ x [* y [tree/ref data :width]]]]]]

[defn array/2d/set! [data x y val]
      [if [or [>= x [tree/ref data :width]]
              [>= y [tree/ref data :height]]
              [< x 0]
              [< y 0]]
          [throw [list :out-of-bounds "Trying to set an array out of bounds" data [current-lambda]]]
          [array/set! [tree/ref data :data] [+ x [* y [tree/ref data :width]]] val]]
      data]

[defn array/2d/print [data]
      [for [y 0 [tree/ref data :height]]
           [for [x 0 [tree/ref data :width]]
                [display [cat [array/2d/ref data x y] " "]]]
           [newline]]
      data]