Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy a circular list to a fixed length list

Tags:

common-lisp

I store graph coordinates in a circular list:

(defun make-circular-list (size &key initial-element)
  (let ((list (make-list size :initial-element initial-element)))
    (nconc list list)))

(defvar *coordinates* (make-circular-list 1024 :initial-element 0.0))

Now it's easy to update *coordinates* whenever new coordinates must be set.

However, I have a library function that takes a sequence of coordinates to draw lines on a graph. Of course this function does not work with a circular structure, so I would like to make a copy of fixed length. A list or an array is fine.

So far I have tried subseq and make-array with the :initial-contents keyword, but it fails. loop or dotimes do work but I was hoping to avoid iterating on each elements of the list.

Is it possible to efficiently copy this circular structure or make a fixed length array in the spirit of a displaced-array?

like image 354
tuscland Avatar asked Dec 10 '25 20:12

tuscland


1 Answers

There is nothing wrong with using LOOP.

(loop for c in *coordinates* repeat 1024 collect c)

Btw., sometimes it might be useful to hide the circular list behind a CLOS object.

(defclass circular-list ()
   ((list)
    (size)
    (first-element)
    (last-element)))

And so on...

That way you can provide some CLOS methods for accessing and changing it (create, add, copy, delete, , as-list, ...). You can also control printing of it using a method for PRINT-OBJECT.

like image 98
Rainer Joswig Avatar answered Dec 13 '25 09:12

Rainer Joswig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!