From: Joshua Taylor on
Slobodan Blazeski wrote:
> 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

Ah, my other post isn't as useful for you then. You're probably looking
for ADJUST-ARRAY.

CL-USER 29 > *v*
#()

CL-USER 30 > (array-total-size *v*)
16

CL-USER 31 > (adjust-array *v* 0)
#()

CL-USER 32 > (array-total-size *v*)
0

//JT
From: Slobodan Blazeski on
On Mar 6, 7:26 pm, Joshua Taylor <tay...(a)cs.rpi.edu> wrote:
> Slobodan Blazeski wrote:
> > 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
>
> Ah, my other post isn't as useful for you then.  You're probably looking
> for ADJUST-ARRAY.
>
> CL-USER 29 > *v*
> #()
>
> CL-USER 30 > (array-total-size *v*)
> 16
>
> CL-USER 31 > (adjust-array *v* 0)
> #()
>
> CL-USER 32 > (array-total-size *v*)
> 0
>
> //JT
That's exactly what I was looking for.

thanks
Slobodan
From: Joshua Taylor on
Slobodan Blazeski wrote:
> On Mar 6, 7:26 pm, Joshua Taylor <tay...(a)cs.rpi.edu> wrote:
>> Slobodan Blazeski wrote:
>>> 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
>> Ah, my other post isn't as useful for you then. You're probably looking
>> for ADJUST-ARRAY.
>>
>> CL-USER 29 > *v*
>> #()
>>
>> CL-USER 30 > (array-total-size *v*)
>> 16
>>
>> CL-USER 31 > (adjust-array *v* 0)
>> #()
>>
>> CL-USER 32 > (array-total-size *v*)
>> 0
>>
>> //JT
> That's exactly what I was looking for.
>
> thanks
> Slobodan

Glad it helps. I also realized that if you use ADJUST-ARRAY without
changing the fill-pointer first, you'll want to give the fill-pointer
argument to adjust-array, too. E.g., see the error in the following
(and the fixed call immediately following it):

CL-USER > *v*
#(0 1 2 3 4 5 6 7 8 9)

CL-USER > (adjust-array *v* 0)

Error: 10 is an illegal fill-pointer.
1 (abort) Return to level 0.
2 Return to top loop level 0.

Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other
options.

CL-USER : 1 > :a

CL-USER > (adjust-array *v* 0 :fill-pointer 0)
#()
From: Pascal Costanza on
On 06/03/2010 19:23, Joshua Taylor wrote:
> 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)

(fill *v* nil) is probably better.


Pascal

--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: Joshua Taylor on
Pascal Costanza wrote:
> On 06/03/2010 19:23, Joshua Taylor wrote:
>> CL-USER >
>> (map-into *v* (constantly nil))
>> #(NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL)
>
> (fill *v* nil) is probably better.

Much better indeed---good catch!