Login
7 branches 0 tags
Ben (X13/Arch) Simplified things a little 0643405 9 days ago 1260 Commits
;; 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 allocate (width height)
      :export
      { :data (-> (:alloc Array (* width height))
                  (array/fill! 0))
        :width width
        :height height})

(defn fill! (data v)
      :export
      (array/fill! (ref data :data) v)
      (return data))

(defn two-dee-ref (data x y oob-val)
      (if (or (>= x (ref data :width))
              (>= y (ref data :height))
              (< x 0)
              (< y 0))
          oob-val
          (ref (ref data :data) (+ x (* y (ref data :width))))))
(export ref two-dee-ref)

(defn set! (data x y val)
      :export
      (if (or (>= x (ref data :width))
              (>= y (ref data :height))
              (< x 0)
              (< y 0))
          (exception :out-of-bounds "Trying to set an array out of bounds" data)
          (set! (ref data :data) (+ x (* y (ref data :width))) val))
      (return data))

(defn print (data)
      :export
      (dotimes (y (ref data :height))
        (dotimes (x (ref data :width))
          (display (cat (array/2d/ref data x y) " ")))
        (newline))
      (return data))

(deftest #t (-> (array/2d/allocate 4 4) (array/2d/set! 1 1 #t) (array/2d/ref 1 1)))
(deftest #t (-> (array/2d/allocate 3 3) (array/2d/fill! #t) (array/2d/ref 1 1)))