Guest User

Untitled

a guest
May 26th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. (ns cellular-automata-basic)
  2.  
  3. (definterface AutomataOps
  4. (^long getCell [^long i ^long j])
  5. (^long sumEightNeighbors [^long i ^long j])
  6. (^long urEightNeighMinmax [^long i ^long j])
  7. (^long rule [^long i ^long j]))
  8.  
  9. (defprotocol Automata
  10. (update [this]))
  11.  
  12. (deftype CellularAutomata [^long nrows ^long ncols ^longs cells ^longs others]
  13. Automata
  14. (update [this] (CellularAutomata. ncols nrows
  15. (amap cells idx result
  16. (let [idx (long idx)
  17. i (unchecked-remainder idx ncols)
  18. j (if (zero? idx) (long 0) (unchecked-divide idx ncols))]
  19. (aset result idx (.urEightNeighMinmax this i j))))
  20. others))
  21. AutomataOps
  22. (getCell [this i j] (aget cells (+ (unchecked-multiply i ncols) j)))
  23. (sumEightNeighbors [this i j]
  24. (-> (.getCell this (dec i) (dec j))
  25. (+ (.getCell this (dec i) j))
  26. (+ (.getCell this (dec i) (inc j)))
  27. (+ (.getCell this i (inc j)))
  28. (+ (.getCell this (inc i) (inc j)))
  29. (+ (.getCell this (inc i) j))
  30. (+ (.getCell this (inc i) (dec j)))
  31. (+ (.getCell this i (dec j)))))
  32. (urEightNeighMinmax [this i j]
  33. (if (or (= i (dec nrows)) (zero? i) (= j (dec ncols)) (zero? j))
  34. (.getCell this i j)
  35. (let [sum (.sumEightNeighbors this i j)]
  36. (if (and (>= sum (aget others 0)) (<= sum (aget others 1)))
  37. (aget others 2)
  38. (.getCell this i j))))))
  39.  
  40. (defn random-ca [nrows ncols]
  41. (CellularAutomata. nrows ncols
  42. (let [a (longs (make-array Long/TYPE (* nrows ncols)))]
  43. (amap a i result
  44. (aset result i (long (rand-int 2)))))
  45. (long-array [2 5 1])))
  46.  
  47. (def a (random-ca 2000 2000))
  48. (def b (random-ca 500 500))
  49.  
  50. (comment
  51. (dotimes [_ 100]
  52. (time
  53. (dotimes [_ 1]
  54. (update b))))
  55. )
Add Comment
Please, Sign In to add comment