From: Sungwoo, Lim
Subject: [Q] Empty an array
Date: 
Message-ID: <230320011251043295%sungwoo@cad.strath.ac.uk>
Hello,
I am trying to empty an temporary array for a next input.
With following codes, I got the error as
> Error: #() is not an array with a fill pointer.
> While executing: FILL-POINTER

How can I empty an array without this error?
Thanks,

Sungwoo
 
;-------------------
(loop for i from 0 to (- ver-num 1)
      when (= (aref arr i) 1)
          do (vector-push-extend (aref arr1 i) temp1)
      else 
          do (vector-push-extend (max-probability temp1) temp2)
          and do (setf temp1 (vector ))) ; empty the temp array.
;--------------------

From: Kent M Pitman
Subject: Re: [Q] Empty an array
Date: 
Message-ID: <sfwd7b8o753.fsf@world.std.com>
"Sungwoo, Lim" <·······@cad.strath.ac.uk> writes:

> Hello,
> I am trying to empty an temporary array for a next input.
> With following codes, I got the error as
> > Error: #() is not an array with a fill pointer.
> > While executing: FILL-POINTER
> 
> How can I empty an array without this error?
> Thanks,

To make the array have a fill pointer, you have to use MAKE-ARRAY with
a :FILL-POINTER argument to create it.  However, you're also using
VECTOR-PUSH-EXTEND.  It will not be able to do the "extend" part if you
actually overrun the buffer (you'll get an "is not adjustable" error)
unless you also use the :ADJUSTABLE argument to MAKE-ARRAY.

(let ((arr1 (make-array 100 :fill-pointer 0 :adjustable t)))
  ...)

Emptying the array might mean several things.

You can set the FILL-POINTER to zero as in
 (setf (fill-pointer arr1) 0)
This will set the working size of the array to zero without affecting
its actual contents.

Setting the fill-pointer does not eliminate what the array points to,
so those things still cannot be garbage-collected.  If it's an array
of characters, that doesn't matter since characters aren't garbage
collected.  But if it's an array of large structures no one else
points to, this may matter.  In that case, do the following as well:

You can empty the individual slots with
 (dotimes (i (array-dimension arr1 0)) (setf (aref arr1) nil))
This will make sure the array isn't pointing to old data.  Note that
you ought to use the ARRAY-DIMENSION function and not LENGTH, since 
LENGTH will respect the FILL-POINTER and will not get you the true
allocated length.  AREF (and its SETF) are allowed to go beyond the
length as long as there is allocated storage, and ARRAY-DIMENSION will
tell you how much allocated storage there is.

In the rare case that you think your array has grown too big due to
previous times being extended, you can use ADJUST-ARRAY to adjust its
size down to a smaller size.  This is rarely advisable.  Almost always
you should just leave the large array there in case it has to grow again
rather than churning the garbage collector by repeatedly discarding
large arrays for smaller ones that must again grow.
From: Sungwoo, Lim
Subject: Re: [Q] Empty an array
Date: 
Message-ID: <260320011227167520%sungwoo@cad.strath.ac.uk>
In article <···············@world.std.com>, Kent M Pitman
<······@world.std.com> wrote:
> You can empty the individual slots with
>  (dotimes (i (array-dimension arr1 0)) (setf (aref arr1) nil))
> This will make sure the array isn't pointing to old data.  Note that
> you ought to use the ARRAY-DIMENSION function and not LENGTH, since 
> LENGTH will respect the FILL-POINTER and will not get you the true
> allocated length.  AREF (and its SETF) are allowed to go beyond the
> length as long as there is allocated storage, and ARRAY-DIMENSION will
> tell you how much allocated storage there is.

I tried following codes, but got an error as
> Error: Array index 1 out of bounds for #<SIMPLE-VECTOR 1> .
> While executing: CCL::%ASET1

When I used ADJUST-ARRAY lines, it was OK, but below codes doesn't work.
What is wrong here?

;-------
(setf temp-array (make-array 1
                             :adjustable t
                             :fill-pointer 0)
      maxpro-group (make-array 1
                               :adjustable t
                               :fill-pointer 0))
(loop for i from 1 to (- ver-num 2) 
      when (= (aref arr i) 1)
          do (vector-push-extend (aref arr1 i) temp-array)
      else
          when (= (aref arr (- i 1)) 1)
              do (vector-push-extend (max-probability temp-array)
maxpro-group)
            ; and do (adjust-array temp-array 0 :fill-pointer 0) 
              and do (array-dimension temp-array 0)
    error->   and do (setf (aref temp-array i) nil)
          else 
            ; do (adjust-array temp-array 0 :fill-pointer 0))
              do (array-dimension temp-array 0) 
    error->   and do (setf (aref temp-array i) nil))
 
> In the rare case that you think your array has grown too big due to
> previous times being extended, you can use ADJUST-ARRAY to adjust its
> size down to a smaller size.  This is rarely advisable.  Almost always
> you should just leave the large array there in case it has to grow again
> rather than churning the garbage collector by repeatedly discarding
> large arrays for smaller ones that must again grow.

I see.
Thanks alot. =)

Sungwoo
From: Markus B. Kr�ger
Subject: Re: [Q] Empty an array
Date: 
Message-ID: <du3vgp05ym8.fsf@proto.pvv.ntnu.no>
"Sungwoo, Lim" <·······@cad.strath.ac.uk> writes:

> Hello,
> I am trying to empty an temporary array for a next input.
> With following codes, I got the error as
> > Error: #() is not an array with a fill pointer.
> > While executing: FILL-POINTER
> 
> How can I empty an array without this error?

To reset the fill pointer:

  (setf (fill-pointer temp1) 0)

To make the memory previously claimed by the array available for
garbage collection:

  (adjust-array temp1 0 :fill-pointer 0)

-- 
 ,-------------------  Markus Bjartveit Kr�ger  ---------------------.
'                                                                     `
` E-mail: ·······@pvv.org           WWW: http://www.pvv.org/~markusk/ '
 )-------------------------------------------------------------------(
From: Markus B. Kr�ger
Subject: Re: [Q] Empty an array
Date: 
Message-ID: <du3snk45yiv.fsf@proto.pvv.ntnu.no>
·······@pvv.org (Markus B. Kr�ger) writes:

> To make the memory previously claimed by the array available for
> garbage collection:
> 
>   (adjust-array temp1 0 :fill-pointer 0)

Or rather, this resizes the array dimensions; I presume that whether
it makes memory available for garbage collection will be
implementation dependant.

-- 
 ,-------------------  Markus Bjartveit Kr�ger  ---------------------.
'                                                                     `
` E-mail: ·······@pvv.org           WWW: http://www.pvv.org/~markusk/ '
 )-------------------------------------------------------------------(
From: Sungwoo, Lim
Subject: Re: [Q] Empty an array
Date: 
Message-ID: <230320011506131103%sungwoo@cad.strath.ac.uk>
In article <···············@proto.pvv.ntnu.no>, Markus B. Kr�ger
<·······@pvv.org> wrote:

> ·······@pvv.org (Markus B. Kr�ger) writes:
> 
> > To make the memory previously claimed by the array available for
> > garbage collection:
> > 
> >   (adjust-array temp1 0 :fill-pointer 0)
> 
> Or rather, this resizes the array dimensions; I presume that whether
> it makes memory available for garbage collection will be
> implementation dependant.

Thanks, =)

Sungwoo
From: Pierre R. Mai
Subject: Re: [Q] Empty an array
Date: 
Message-ID: <87ae6cfs78.fsf@orion.bln.pmsf.de>
"Sungwoo, Lim" <·······@cad.strath.ac.uk> writes:

> I am trying to empty an temporary array for a next input.
> With following codes, I got the error as
> > Error: #() is not an array with a fill pointer.
> > While executing: FILL-POINTER
> 
> How can I empty an array without this error?
> Thanks,
> 
> Sungwoo
>  
> ;-------------------
> (loop for i from 0 to (- ver-num 1)
>       when (= (aref arr i) 1)
>           do (vector-push-extend (aref arr1 i) temp1)
>       else 
>           do (vector-push-extend (max-probability temp1) temp2)
>           and do (setf temp1 (vector ))) ; empty the temp array.
> ;--------------------

If you are using an array with a fill-pointer (i.e. to use
vector-push(-extend)), then just reset the fill-pointer to 0 on each
iteration:

(loop with temp1 = (make-array 0 :adjustable t :fill-pointer 0)
      for i from 0 to (- ver-num 1)
      if (= (aref arr i) 1)
        do (vector-push-extend (aref arr1 i) temp1)
      else
        do (vector-push-extend (max-probability temp1) temp2)
        and do (setf (fill-pointer temp1) 0))

If the temporary array might grow to large sizes, and you want to free
that space on each major iteration, you might want to call
adjust-array in addition, in order to decrease the size of the
temporary array, instead of only resetting the fill-pointer.

At no time should a call to vector or make-array be necessary on each
major iteration.

Regs, Pierre.

-- 
Pierre R. Mai <····@acm.org>                    http://www.pmsf.de/pmai/
 The most likely way for the world to be destroyed, most experts agree,
 is by accident. That's where we come in; we're computer professionals.
 We cause accidents.                           -- Nathaniel Borenstein
From: Sungwoo, Lim
Subject: Re: [Q] Empty an array
Date: 
Message-ID: <260320011147061835%sungwoo@cad.strath.ac.uk>
In article <··············@orion.bln.pmsf.de>, Pierre R. Mai
<····@acm.org> wrote:

> If you are using an array with a fill-pointer (i.e. to use
> vector-push(-extend)), then just reset the fill-pointer to 0 on each
> iteration:
> 
> (loop with temp1 = (make-array 0 :adjustable t :fill-pointer 0)
>       for i from 0 to (- ver-num 1)
>       if (= (aref arr i) 1)
>         do (vector-push-extend (aref arr1 i) temp1)
>       else
>         do (vector-push-extend (max-probability temp1) temp2)
>         and do (setf (fill-pointer temp1) 0))
> 
> If the temporary array might grow to large sizes, and you want to free
> that space on each major iteration, you might want to call
> adjust-array in addition, in order to decrease the size of the
> temporary array, instead of only resetting the fill-pointer.
> 
> At no time should a call to vector or make-array be necessary on each
> major iteration.
> 
> Regs, Pierre.

Thanks alot. =)

Sungwoo
From: Martti Halminen
Subject: Re: [Q] Empty an array
Date: 
Message-ID: <3ABB5D01.3555A113@solibri.com>
"Sungwoo, Lim" wrote:
> 
> Hello,
> I am trying to empty an temporary array for a next input.
> With following codes, I got the error as
> > Error: #() is not an array with a fill pointer.
> > While executing: FILL-POINTER
> 
> How can I empty an array without this error?
> Thanks,
> 
> Sungwoo
> 
> ;-------------------
> (loop for i from 0 to (- ver-num 1)
>       when (= (aref arr i) 1)
>           do (vector-push-extend (aref arr1 i) temp1)
>       else
>           do (vector-push-extend (max-probability temp1) temp2)
>           and do (setf temp1 (vector ))) ; empty the temp array.
> ;--------------------

How about reading the Hyperspec regarding vector-push-extend:

----
Exceptional Situations:

An error of type error is signaled by vector-push-extend if it tries to
extend vector and vector is not actually adjustable. 

An error of type error is signaled if vector does not have a fill
pointer. 
----

and on vector: 

----

Creates a fresh simple general vector whose size corresponds to the
number of objects. 

----

In other words, if you want to VECTOR-PUSH-EXTEND something, don't use
VECTOR to create it.


--
From: Sungwoo, Lim
Subject: Re: [Q] Empty an array
Date: 
Message-ID: <230320011501525400%sungwoo@cad.strath.ac.uk>
In article <·················@solibri.com>, Martti Halminen
<···············@solibri.com> wrote:

> How about reading the Hyperspec regarding vector-push-extend:
> 
> ----
> Exceptional Situations:
> 
> An error of type error is signaled by vector-push-extend if it tries to
> extend vector and vector is not actually adjustable. 
> 
> An error of type error is signaled if vector does not have a fill
> pointer. 
> ----
> 
> and on vector: 
> 
> ----
> 
> Creates a fresh simple general vector whose size corresponds to the
> number of objects. 
> 
> ----
> 
> In other words, if you want to VECTOR-PUSH-EXTEND something, don't use
> VECTOR to create it.
> 
> 
> --
Thanks, I tried this one, and it seems work.

(defun empty-array (arr)
  (loop for i from 0 to (- (fill-pointer arr) 1)
        do (vector-pop arr)))

Thanks again.

Sungwoo
From: Barry Margolin
Subject: Re: [Q] Empty an array
Date: 
Message-ID: <g5Ku6.81$U4.4076@burlma1-snr2>
In article <··························@cad.strath.ac.uk>,
Sungwoo, Lim <·······@cad.strath.ac.uk> wrote:
>Thanks, I tried this one, and it seems work.
>
>(defun empty-array (arr)
>  (loop for i from 0 to (- (fill-pointer arr) 1)
>        do (vector-pop arr)))

That should fail in the same way if the vector doesn't have a fill pointer.
The problem isn't with the code that clears the vector, it's with the code
that *creates* it.  You have to use MAKE-ARRAY with :FILL-POINTER T.

-- 
Barry Margolin, ······@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Sungwoo, Lim
Subject: Re: [Q] Empty an array
Date: 
Message-ID: <260320011142214643%sungwoo@cad.strath.ac.uk>
In article <················@burlma1-snr2>, Barry Margolin
<······@genuity.net> wrote:

> In article <··························@cad.strath.ac.uk>,
> Sungwoo, Lim <·······@cad.strath.ac.uk> wrote:
> >Thanks, I tried this one, and it seems work.
> >
> >(defun empty-array (arr)
> >  (loop for i from 0 to (- (fill-pointer arr) 1)
> >        do (vector-pop arr)))
> 
> That should fail in the same way if the vector doesn't have a fill pointer.
> The problem isn't with the code that clears the vector, it's with the code
> that *creates* it.  You have to use MAKE-ARRAY with :FILL-POINTER T.

Yes, of course, I used MAKE-ARRAY as follows.

(setf temp-array (make-array 1
                             :adjustable t
                             :fill-pointer 0)
      maxpro-group (make-array 1
                               :adjustable t
                               :fill-pointer 0))

Thanks alot. =)

Sungwoo