From: Slobodan Blazeski on 6 Mar 2010 12:46 I have an array defined as: (make-array 0 :element-type 'string :fill-pointer 0 :adjustable t) After adding some elements I want to know how to clear it , i.e. remove all elements from it? thanks Slobodan
From: Ron Garret on 6 Mar 2010 13:09 In article <7d69ae1c-2467-4298-974e-6fc98e6460b6(a)t23g2000yqt.googlegroups.com>, Slobodan Blazeski <slobodan.blazeski(a)gmail.com> wrote: > I have an array defined as: > (make-array 0 :element-type 'string :fill-pointer 0 > :adjustable t) > > After adding some elements I want to know how to clear it , i.e. > remove all elements from it? > > thanks > Slobodan ? (setf v (make-array 0 :element-type 'string :fill-pointer 0 :adjustable t)) #() ? (dotimes (i 10) (vector-push-extend i v)) ;Compiler warnings : ; In an anonymous lambda form at position 16: Undeclared free variable V NIL ? v #(0 1 2 3 4 5 6 7 8 9) ? (setf (fill-pointer v) 0) 0 ? v #() ?
From: Slobodan Blazeski on 6 Mar 2010 13:14 On Mar 6, 7:09 pm, Ron Garret <rNOSPA...(a)flownet.com> wrote: > In article > <7d69ae1c-2467-4298-974e-6fc98e646...(a)t23g2000yqt.googlegroups.com>, > Slobodan Blazeski <slobodan.blaze...(a)gmail.com> wrote: > > > I have an array defined as: > > (make-array 0 :element-type 'string :fill-pointer 0 > > :adjustable t) > > > After adding some elements I want to know how to clear it , i.e. > > remove all elements from it? > > > thanks > > Slobodan > > ? (setf v (make-array 0 :element-type 'string :fill-pointer 0 > :adjustable t)) > #() > ? (dotimes (i 10) (vector-push-extend i v)) > ;Compiler warnings : > ; In an anonymous lambda form at position 16: Undeclared free variable > V > NIL > ? v > #(0 1 2 3 4 5 6 7 8 9) > ? (setf (fill-pointer v) 0) > 0 > ? v > #() > ? I've thought about that but does just setf-ing the fill pointer releases the storage space? Slobodan
From: Slobodan Blazeski on 6 Mar 2010 13:18 On Mar 6, 7:14 pm, Slobodan Blazeski <slobodan.blaze...(a)gmail.com> wrote: > On Mar 6, 7:09 pm, Ron Garret <rNOSPA...(a)flownet.com> wrote: > > > > > In article > > <7d69ae1c-2467-4298-974e-6fc98e646...(a)t23g2000yqt.googlegroups.com>, > > Slobodan Blazeski <slobodan.blaze...(a)gmail.com> wrote: > > > > I have an array defined as: > > > (make-array 0 :element-type 'string :fill-pointer 0 > > > :adjustable t) > > > > After adding some elements I want to know how to clear it , i.e. > > > remove all elements from it? > > > > thanks > > > Slobodan > > > ? (setf v (make-array 0 :element-type 'string :fill-pointer 0 > > :adjustable t)) > > #() > > ? (dotimes (i 10) (vector-push-extend i v)) > > ;Compiler warnings : > > ; In an anonymous lambda form at position 16: Undeclared free variable > > V > > NIL > > ? v > > #(0 1 2 3 4 5 6 7 8 9) > > ? (setf (fill-pointer v) 0) > > 0 > > ? v > > #() > > ? > > I've thought about that but does just setf-ing the fill pointer > releases the storage space? > > Slobodan CL> (setf v (make-array 0 :element-type 'string :fill-pointer 0 :adjustable t)) #() CL> (dotimes (i 10) (vector-push-extend i v)) NIL CL> (array-dimensions v) (15) CL> (setf (fill-pointer v) 0) 0 CL> (array-dimensions v) (15) To answer my own questions at least on sbcl answer is no. Slobodan
From: Joshua Taylor on 6 Mar 2010 13:23
Ron Garret wrote: > In article > <7d69ae1c-2467-4298-974e-6fc98e6460b6(a)t23g2000yqt.googlegroups.com>, > Slobodan Blazeski <slobodan.blazeski(a)gmail.com> wrote: >> [...] >> After adding some elements I want to know how to clear it , i.e. >> remove all elements from it? > [...] > ? (setf (fill-pointer v) 0) > 0 > ? v > #() > ? Two things to be aware of are (i) that changing the fill pointer doesn't deallocate the memory used by the vector (which probably doesn't matter all that much) and (ii) there are still references to the elements in places past the fill pointer: CL-USER > (defparameter *v* (make-array 0 :fill-pointer 0 :adjustable t :initial-element 0)) *V* CL-USER > (dotimes (i 10) (vector-push-extend i *v*)) NIL CL-USER 12 > (aref *v* 5) 5 CL-USER 13 > (setf (fill-pointer *v*) 0) 0 CL-USER 14 > (aref *v* 5) 5 This can keep objects from being garbage collected in situations where you might have expected them to be (after all you did say "/remove/ all elements from it", not just "make the vector have length 0 again". As such, it's not a bad idea to set the array contents to NIL (or whatever you like) before setting the fill pointer to 0. E.g., CL-USER > (dotimes (i 10) (vector-push-extend i *v*)) NIL CL-USER > *v* #(0 1 2 3 4 5 6 7 8 9) CL-USER > (map-into *v* (constantly nil)) #(NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL) CL-USER > (setf (fill-pointer *v*) 0) 0 CL-USER 25 > *v* #() //JT |