Prev: Comparing Lisp to Python, what you consider more important: speed or macros.
Next: LispBox - truncated result?
From: Peter Keller on 29 Apr 2010 10:21 Hello, I understand that I can adjust an array like this: * (setf x (make-array 3 :initial-element 100)) #(100 100 100) * (setf y (adjust-array x 6 :initial-element 42)) #(100 100 100 42 42 42) My question is: This adjustment happens at the _end_ of the array, can I expand the array at the beginning? Could I do an adjustment on x where the final result is: #(42 42 42 100 100 100) Or do I have to write the code to allocate the larger array and manually copy the elements myself? Thank you. -pete
From: Tamas K Papp on 29 Apr 2010 10:42 On Thu, 29 Apr 2010 14:21:49 +0000, Peter Keller wrote: > My question is: This adjustment happens at the _end_ of the array, can I > expand the array at the beginning? > [...] > Or do I have to write the code to allocate the larger array and manually > copy the elements myself? I think that you have to copy yourself - but that is rather easy to do, see eg REPLACE. In general, if you know that you will not prepend more than a given number of elements (or you are willing to copy elements occasionally), you can create an array that is large enough, and displace to it using an offset. Then if you need extra elements at the beginning, just displace to a smaller offset. Tamas
From: RG on 29 Apr 2010 10:54 In article <4bd995fd$0$9895$80265adb(a)spool.cs.wisc.edu>, Peter Keller <psilord(a)merlin.cs.wisc.edu> wrote: > Hello, > > I understand that I can adjust an array like this: > > * (setf x (make-array 3 :initial-element 100)) > > #(100 100 100) > > * (setf y (adjust-array x 6 :initial-element 42)) > > #(100 100 100 42 42 42) > > My question is: This adjustment happens at the _end_ of the array, can I > expand the array at the beginning? > > Could I do an adjustment on x where the final result is: > > #(42 42 42 100 100 100) > > Or do I have to write the code to allocate the larger array and manually copy > the elements myself? > > Thank you. > > -pete You want a displaced (and adjustable) array. ? (setf a1 (make-array 30)) #(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) ? (dotimes (i 30) (setf (aref a1 i) i)) .... ? a1 #(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29) ? (setf a2 (make-array 10 :displaced-to a1 :displaced-index-offset 15 :adjustable t)) #(15 16 17 18 19 20 21 22 23 24) ? (adjust-array a1 '(15) :displaced-to a1 :displaced-index-offset 10) #(10 11 12 13 14 15 16 17 18 19 20 21 22 23 24) rg
From: Peter Keller on 29 Apr 2010 11:13 RG <rNOSPAMon(a)flownet.com> wrote: > You want a displaced (and adjustable) array. > > ? (setf a1 (make-array 30)) > #(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) > ? (dotimes (i 30) (setf (aref a1 i) i)) > ... > ? a1 > #(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 > 27 28 29) > ? (setf a2 (make-array 10 :displaced-to a1 :displaced-index-offset 15 > :adjustable t)) > #(15 16 17 18 19 20 21 22 23 24) > ? (adjust-array a1 '(15) :displaced-to a1 :displaced-index-offset 10) > #(10 11 12 13 14 15 16 17 18 19 20 21 22 23 24) I see. If I knew how big the final sizes of the arrays were I could use this method, but sadly I don't. I'll file away the method for another day. It looks like under my circumstances, a new array into which I copy all of my disparate pieces looks to be one of the only solutions. Thank you for your time. -pete
From: RG on 29 Apr 2010 11:34 In article <4bd9a21a$0$9898$80265adb(a)spool.cs.wisc.edu>, Peter Keller <psilord(a)merlin.cs.wisc.edu> wrote: > RG <rNOSPAMon(a)flownet.com> wrote: > > You want a displaced (and adjustable) array. > > > > ? (setf a1 (make-array 30)) > > #(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) > > ? (dotimes (i 30) (setf (aref a1 i) i)) > > ... > > ? a1 > > #(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 > > 27 28 29) > > ? (setf a2 (make-array 10 :displaced-to a1 :displaced-index-offset 15 > > :adjustable t)) > > #(15 16 17 18 19 20 21 22 23 24) > > ? (adjust-array a1 '(15) :displaced-to a1 :displaced-index-offset 10) > > #(10 11 12 13 14 15 16 17 18 19 20 21 22 23 24) > > I see. > > If I knew how big the final sizes of the arrays were I could use > this method, but sadly I don't. I'll file away the method for another > day. It looks like under my circumstances, a new array into which I copy > all of my disparate pieces looks to be one of the only solutions. > > Thank you for your time. > > -pete You can make the underlying array adjustable (but not displaced) so that it can grow too. If you need to grow it at both ends you can use two adjustable arrays and roll your own displaced array class to do the indirection. rg
|
Next
|
Last
Pages: 1 2 3 4 5 6 7 Prev: Comparing Lisp to Python, what you consider more important: speed or macros. Next: LispBox - truncated result? |