Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. (defun bit-array-serialize (bit-array)
  2. (format nil "~S~%~:{~A*~A~:^ ~}~%"
  3. (array-dimensions bit-array)
  4. (bit-vector-rle (make-array (reduce (function *) (array-dimensions bit-array))
  5. :displaced-to bit-array))))
  6.  
  7. (bit-array-serialize (make-sparse-bit-array '(100 200 50) 1/10000))
  8.  
  9. (defun bit-array-deserialize (data)
  10. (with-input-from-string (*standard-input* data)
  11. (let ((dimensions (read)))
  12. (check-type dimensions list)
  13. (assert (every (function integerp) dimensions))
  14. (let* ((bit-array (make-array dimensions :element-type 'bit :initial-element 0))
  15. (bit-vector (make-array (reduce (function *) dimensions) :displaced-to bit-array)))
  16. (loop
  17. :for run := (read *standard-input* nil)
  18. :while run
  19. :do (let ((srun (string run)))
  20. (multiple-value-bind (start star) (parse-integer srun :junk-allowed t)
  21. (let ((length (parse-integer srun :start (1+ star))))
  22. (fill bit-vector 1 :start start :end (+ start length))))))
  23. bit-array))))
  24.  
  25. (let ((small-bit-array (make-sparse-bit-array '(4 5 6) 1/10)))
  26. (assert (equalp small-bit-array (bit-array-deserialize (bit-array-serialize small-bit-array)))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement