From: Jm
Subject: array looses fill-pointer after removing an element
Date: 
Message-ID: <37k7i3F5d2ttkU1@individual.net>
Hi, I have an array with a bunch of structures:

(setq myarray (make-array 10 :element-type 'mystructure :adjustable t
:fill-pointer 0))

I fill the array then proceed to remove an element from the array with
"remove" and setf the new resulting array back to its original variable:

(setf myarray (remove stuff myarray :test #'equal :key #'(lambda (x)
blablabla)))

At this point myarray looses its fill-pointer.
When trying to insert a new element with vector-push-extend i get an error:
array does not have a fill-pointer

Is there something i can do?

From: Jm
Subject: Re: array looses fill-pointer after removing an element
Date: 
Message-ID: <37k8ajF5bq2rmU1@individual.net>
I fixed the problem by using delete.
From: Tayssir John Gabbour
Subject: Re: array looses fill-pointer after removing an element
Date: 
Message-ID: <1108700261.858823.242250@o13g2000cwo.googlegroups.com>
Jm wrote:
> (setf myarray (remove stuff myarray :test #'equal :key #'(lambda (x)
> blablabla)))
>
> At this point myarray looses its fill-pointer.
> When trying to insert a new element with vector-push-extend i get an
> error:
> array does not have a fill-pointer
>
> Is there something i can do?

Maybe something vaguely like the following?

;; not seriously tested!
(defun vector-delete-if (function vector &rest rest)
  (let ((result (apply #'delete-if function vector rest)))
    (cond ((eq vector result)
           result)
          ((array-has-fill-pointer-p vector)
           (adjust-array vector (length result) :initial-contents
result
                         :fill-pointer (length result)))
          (t
           (adjust-array vector (length result) :initial-contents
result)))))


What it does is, if DELETE-IF isn't man/woman enough to do the job
100%, it'll call ADJUST-ARRAY to finish it.

Of course, keep in mind my code has killed before, and I probably
didn't think this one through.


MfG,
Tayssir
From: Jm
Subject: Re: array looses fill-pointer after removing an element
Date: 
Message-ID: <37kdnaF5dcn73U1@individual.net>
Nop... seems like it didn't solve my problem.
delete only works in some cases... do you guys have any suggestion?
From: David Sletten
Subject: Re: array looses fill-pointer after removing an element
Date: 
Message-ID: <5_7Rd.12388$xX3.9477@twister.socal.rr.com>
Jm wrote:
> Nop... seems like it didn't solve my problem.
> delete only works in some cases... do you guys have any suggestion?
> 
> 
I can understand your problem when using REMOVE, which returns a copy of 
its input (at least when elements need to be removed). If this copy is 
generated a la COPY-SEQ, then you get a simple array, which has no fill 
pointer:

   (http://www.lispworks.com/documentation/HyperSpec/Body/f_cp_seq.htm)
   If sequence is a vector, the result is a fresh simple array of rank
   one that has the same actual array element type as sequence.

I don't see what's wrong using DELETE since, as I understand, it either 
modifies the existing sequence or returns it as is. That shouldn't 
remove the fill pointer?

David Sletten
From: Pascal Bourguignon
Subject: Re: array looses fill-pointer after removing an element
Date: 
Message-ID: <87vf8q7srm.fsf@thalassa.informatimago.com>
David Sletten <·····@slytobias.com> writes:

> Jm wrote:
> > Nop... seems like it didn't solve my problem.
> > delete only works in some cases... do you guys have any suggestion?
> >
> I can understand your problem when using REMOVE, which returns a copy
> of its input (at least when elements need to be removed). If this copy
> is generated a la COPY-SEQ, then you get a simple array, which has no
> fill pointer:
> 
>    (http://www.lispworks.com/documentation/HyperSpec/Body/f_cp_seq.htm)
>    If sequence is a vector, the result is a fresh simple array of rank
>    one that has the same actual array element type as sequence.
> 
> I don't see what's wrong using DELETE since, as I understand, it
> either modifies the existing sequence or returns it as is. That
> shouldn't remove the fill pointer?

No, DELETE doesn't HAVE to use the same array.  It may as well behave
like REMOVE if it finds it convenient.  

Jm, if you want to keep the same array, or if you want to pass over
all the attributes of the original array, you will have to implement
your own function to remove the element.


Note that if you have to delete often elements in this way, you may
choose to implement a sparse array (eg. set slots to NIL instead of
packing the array each time) or rather use a list.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
Our enemies are innovative and resourceful, and so are we. They never
stop thinking about new ways to harm our country and our people, and
neither do we. -- Georges W. Bush
From: Bulent Murtezaoglu
Subject: Re: array looses fill-pointer after removing an element
Date: 
Message-ID: <87oeeizvpg.fsf@p4.internal>
>>>>> "DS" == David Sletten <·····@slytobias.com> writes:
[...]
    DS> I don't see what's wrong using DELETE since, as I understand,
    DS> it either modifies the existing sequence or returns it as
    DS> is. That shouldn't remove the fill pointer?

It may, it doesn't have to.

http://www.lispworks.com/documentation/HyperSpec/Body/f_rm_rm.htm

"Sequence may be destroyed and used to construct the result; however, 
the result might or might not be identical to sequence."

Also see notes there:

"If sequence is a vector, the result might or might not be simple, and 
might or might not be identical to sequence."

So it seems the observed behaviour is conforming.  I don't immediately
see why implementors would need the leeway to return vectors that are
simple when the argument is non-simple though.  What am I missing?
(Also note: there's a distinction between a vector that is simple in
the array sense and a simple-vector.  I think the above 'simple'
refers to an simple-array of one dimension -- not a vector of
type simple-vector.  Since I have next to no hope of transliterating
precise specese this late, I will not attempt to.  See the good
spec. )

cheers,

BM
From: Kalle Olavi Niemitalo
Subject: Re: array looses fill-pointer after removing an element
Date: 
Message-ID: <87u0o82xgh.fsf@Astalo.kon.iki.fi>
Bulent Murtezaoglu <··@acm.org> writes:

> (Also note: there's a distinction between a vector that is simple in
> the array sense and a simple-vector.

I don't think so.  The dictionary entry for SIMPLE-VECTOR says it
is equivalent to (simple-array t (size)), so I don't believe it can
set any extra requirements, other than the number of dimensions.
From: Christophe Rhodes
Subject: Re: array looses fill-pointer after removing an element
Date: 
Message-ID: <sq3bvsqd7p.fsf@cam.ac.uk>
Kalle Olavi Niemitalo <···@iki.fi> writes:

> Bulent Murtezaoglu <··@acm.org> writes:
>
>> (Also note: there's a distinction between a vector that is simple in
>> the array sense and a simple-vector.
>
> I don't think so.  The dictionary entry for SIMPLE-VECTOR says it
> is equivalent to (simple-array t (size)), so I don't believe it can
> set any extra requirements, other than the number of dimensions.

Bulent was quite correct: one-dimensional simple-arrays are a strict
superset of simple-vectors, because simple-strings and
simple-bit-vectors are also simple-arrays.  simple-vectors are
specialised on type T, which is not the same as saying that they have
arbitrary specializations.

Christophe
From: Kalle Olavi Niemitalo
Subject: Re: array looses fill-pointer after removing an element
Date: 
Message-ID: <87k6p33a4l.fsf@Astalo.kon.iki.fi>
Christophe Rhodes <·····@cam.ac.uk> writes:

> simple-vectors are specialised on type T, which is not the same
> as saying that they have arbitrary specializations.

Thank you.  I did not notice that.
From: Alan Crowe
Subject: Re: array looses fill-pointer after removing an element
Date: 
Message-ID: <86brai6od1.fsf@cawtech.freeserve.co.uk>
Jm asked
> Hi, I have an array with a bunch of structures:
>
> (setq myarray (make-array 10 :element-type 'mystructure :adjustable t
>                           :fill-pointer 0))
>
> I fill the array then proceed to remove an element from
> the array with "remove" and setf the new resulting array
> back to its original variable:
>
> (setf myarray (remove stuff myarray :test #'equal :key #'(lambda (x)
> blablabla)))
>
> At this point myarray looses its fill-pointer.
> When trying to insert a new element with vector-push-extend i get an error:
> array does not have a fill-pointer
>
> Is there something i can do?

I think that wrapping REMOVE like this

(defun bunch-up (unwanted-item vector-with-fill-pointer
				 &rest keywords)
    (let* ((data (apply (function remove)
			unwanted-item
			vector-with-fill-pointer
			keywords))
	   (number-of-items-remaining (length data)))
      (setf (subseq vector-with-fill-pointer 0
		    number-of-items-remaining)
	    data
	    (fill-pointer vector-with-fill-pointer)
			  number-of-items-remaining)
      ;; is this the best choice of return value?
      vector-with-fill-pointer))

keeps your original vector with its fill pointer and element
type without requiring you re-implement REMOVE's keywords,
at the expense of generating quite a lot of garbage.

Alan Crowe
Edinburgh
Scotland
From: Rahul Jain
Subject: Re: array looses fill-pointer after removing an element
Date: 
Message-ID: <87u0o6a3g7.fsf@nyct.net>
"Jm" <··@noemail.com> writes:

> I fill the array then proceed to remove an element from the array with
> "remove" and setf the new resulting array back to its original variable:

This is a strange operation to be doing on a vector. Usually one would
use a list if such operations are needed, or do something a bit more
complex if both direct index-based access and element removal are needed
in the same data structure. What exactly are you using this data
structure for?

-- 
Rahul Jain
·····@nyct.net
Professional Software Developer, Amateur Quantum Mechanicist