From: John Stivenson
Subject: Arrays
Date: 
Message-ID: <c7ff815a.0411251121.70f872ad@posting.google.com>
How can I make a copy of an array?
Is there some function which can do it for me?

From: Philip Haddad
Subject: Re: Arrays
Date: 
Message-ID: <ba57c4f9.0411251731.451fe89@posting.google.com>
··············@yahoo.com (John Stivenson) wrote in message news:<····························@posting.google.com>...
> How can I make a copy of an array?
> Is there some function which can do it for me?

Copy-tree can copy a tree for you, I don't know of any array-copying
functions....
Why do you really need a copy of an array? Why not just use it when
you need it? It's really not going to save you memory or anything to
make a copy of a array. You do know you can use arrays as variables
right?

-- 
Certum quod factum.
Philip Haddad
From: Pascal Bourguignon
Subject: Re: Arrays
Date: 
Message-ID: <87act5k665.fsf@thalassa.informatimago.com>
··············@yahoo.com (John Stivenson) writes:

> How can I make a copy of an array?
> Is there some function which can do it for me?

copy-seq

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
The world will now reboot; don't bother saving your artefacts.
From: Wade Humeniuk
Subject: Re: Arrays
Date: 
Message-ID: <xOrpd.207124$9b.13772@edtnps84>
Pascal Bourguignon wrote:
> ··············@yahoo.com (John Stivenson) writes:
> 
> 
>>How can I make a copy of an array?
>>Is there some function which can do it for me?
> 
> 
> copy-seq
> 

Nope.  Only if an array is one-dimensional.

CL-USER 3 > (setf a (make-array '(2 2) :initial-element 2))
#2A((2 2) (2 2))

CL-USER 4 > (copy-seq a)

Error: In a call to LENGTH: #2A((2 2) (2 2)) is not of type SEQUENCE.
   1 (abort) Return to level 0.
   2 Return to top loop level 0.

Type :b for backtrace, :c <option number> to proceed,  or :? for other options

CL-USER 5 : 1 > :a

CL-USER 6 >

Wade
From: Lars Brinkhoff
Subject: Re: Arrays
Date: 
Message-ID: <854qjdhbgz.fsf@junk.nocrew.org>
http://lemonodor.com/archives/000100.html
From: Lars Brinkhoff
Subject: Re: Arrays
Date: 
Message-ID: <85r7mgfiru.fsf@junk.nocrew.org>
Lars Brinkhoff <·········@nocrew.org> writes:
> http://lemonodor.com/archives/000100.html

So Marc LeBrun's function is not an option?

-- 
Lars Brinkhoff,         Services for Unix, Linux, GCC, HTTP
Brinkhoff Consulting    http://www.brinkhoff.se/
From: Peter Seibel
Subject: Re: Arrays
Date: 
Message-ID: <m38y8pd3yi.fsf@javamonkey.com>
Pascal Bourguignon <····@mouse-potato.com> writes:

> ··············@yahoo.com (John Stivenson) writes:
>
>> How can I make a copy of an array?
>> Is there some function which can do it for me?
>
> copy-seq

As long as by "array" you mean "vector" i.e. a one-dimensional array.
For dimensions >1 you'll have to write your own function, something
like:

  (defun copy-array (array)
    (with-accessors ((type array-element-type) (total-size array-total-size)) array
      (let ((copy (make-array (array-dimensions array) :element-type type)))
        (replace 
         (make-array total-size :element-type type :displaced-to copy)
         (make-array total-size :element-type type :displaced-to array))
        copy)))

You may also want to copy the fill-pointer and adjustabality of the
original array.

-Peter

-- 
Peter Seibel                                      ·····@javamonkey.com

         Lisp is the red pill. -- John Fraser, comp.lang.lisp
From: Lars Brinkhoff
Subject: Re: Arrays
Date: 
Message-ID: <85mzx4fgs6.fsf@junk.nocrew.org>
Peter Seibel <·····@javamonkey.com> writes:
> (with-accessors ((type array-element-type) (total-size array-total-size)) array

Language-lawyer-strictly speaking, is this guaranteed to work?

CLHS says that the "consequences are undefined if any accessor-name is
not the name of an accessor for the instance".  So, are
array-element-type and array-total-size names of accessors for the
instance?  CLHS says an accessor is "an operator that performs an
access".  An access is "an attempt to access[1] the value of a place".
(Array-element-type array) and (array-total-size array) aren't places.

But I guess it's not the intent that the accessors must be both
readers and writers, and the definition of reader is "a function that
reads a variable or slot".  A slot is "a component of an object that
can store a value".  So I think we should be ok.

-- 
Lars Brinkhoff,         Services for Unix, Linux, GCC, HTTP
Brinkhoff Consulting    http://www.brinkhoff.se/
From: Peter Seibel
Subject: Re: Arrays
Date: 
Message-ID: <m3r7mgi98c.fsf@javamonkey.com>
Lars Brinkhoff <·········@nocrew.org> writes:

> Peter Seibel <·····@javamonkey.com> writes:
>> (with-accessors ((type array-element-type) (total-size array-total-size)) array
>
> Language-lawyer-strictly speaking, is this guaranteed to work?

I think so (obviously) but wouldn't be surprised if someone could fine
some nook of the spec that proves me wrong.

> CLHS says that the "consequences are undefined if any accessor-name
> is not the name of an accessor for the instance". So, are
> array-element-type and array-total-size names of accessors for the
> instance? CLHS says an accessor is "an operator that performs an
> access". An access is "an attempt to access[1] the value of a
> place". (Array-element-type array) and (array-total-size array)
> aren't places.

But the variable `array' is a place and array-element-type and
array-total-size indeed attempt to access the value of that place, no?

> But I guess it's not the intent that the accessors must be both
> readers and writers, and the definition of reader is "a function that
> reads a variable or slot".  A slot is "a component of an object that
> can store a value".  So I think we should be ok.

That was my thinking.

-Peter

-- 
Peter Seibel                                      ·····@javamonkey.com

         Lisp is the red pill. -- John Fraser, comp.lang.lisp
From: JP Massar
Subject: Re: Arrays
Date: 
Message-ID: <nj2fq0hrvrn3dn0jrp0sjfvcaci72uqh7d@4ax.com>
On 25 Nov 2004 11:21:31 -0800, ··············@yahoo.com (John
Stivenson) wrote:

>How can I make a copy of an array?

The hard way.

>Is there some function which can do it for me?

No.

You'd need to access the information about the structure of the old
array:

array-dimensions
array-element-type
adjustable-array-p
array-has-fill-pointer-p

create a new array with the proper information using MAKE-ARRAY

and then use

ROW-MAJOR-AREF and (SETF (ROW-MAJOR-AREF ...

to copy the elements from the old array to the new one.

(I'm ignoring issues dealing with displaced arrays)

(If you just wanted to copy one-dimensional arrays you could use
COPY-SEQ)
From: David Sletten
Subject: Re: Arrays
Date: 
Message-ID: <OZUpd.112056$Kl3.84544@twister.socal.rr.com>
John Stivenson wrote:

> How can I make a copy of an array?
> Is there some function which can do it for me?

Is it really conceivable that this issue has not been discussed before?
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&c2coff=1&frame=right&th=171df44c9423ecdd&seekm=sfwwwmgwhuh.fsf%40world.std.com#link7

David Sletten
From: Steven M. Haflich
Subject: Re: Arrays
Date: 
Message-ID: <8y1qd.25731$zx1.6600@newssvr13.news.prodigy.com>
David Sletten wrote:

 > John Stivenson wrote:
 >
 >> How can I make a copy of an array?
 >> Is there some function which can do it for me?
 >
 > Is it really conceivable that this issue has not been discussed before?
 > 
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&c2coff=1&frame=right&th=171df44c9423ecdd&seekm=sfwwwmgwhuh.fsf%40world.std.com#link7

The original question is not fully specified, therefore it is not
clear that the particular referenced solution fulfills the
requirements.  Copying an array in CL raises similar issues
as for copying almost any kind of object: What attributes should
be shared with the original object?

The attribute that doesn't have a single obvious correct answer
is array displacement.  One can create the copy simply by
displacing it to the original, or one can create the copy with
the _same_ displacement as the original, or one can split off
the copy from the original array (or arrays, since displacement
can be chained to any depth) such that subsequent changes to
either array do not affect the other.

There is no a priori right answer whether displacement should
be preserved, but the issue cannot just be ignored.
From: David Sletten
Subject: Re: Arrays
Date: 
Message-ID: <RA7qd.116717$Kl3.86541@twister.socal.rr.com>
Steven M. Haflich wrote:


> 
> The original question is not fully specified, therefore it is not
> clear that the particular referenced solution fulfills the
> requirements.  Copying an array in CL raises similar issues
> as for copying almost any kind of object: What attributes should
> be shared with the original object?
> 
> The attribute that doesn't have a single obvious correct answer
> is array displacement.  One can create the copy simply by
> displacing it to the original, or one can create the copy with
> the _same_ displacement as the original, or one can split off
> the copy from the original array (or arrays, since displacement
> can be chained to any depth) such that subsequent changes to
> either array do not affect the other.
> 
> There is no a priori right answer whether displacement should
> be preserved, but the issue cannot just be ignored.

Steven,

The points you raise are legitimate when thinking about the issue of 
copying (array) objects. However, they are not relevant in response to 
my posting. There is precious little information in this thread that 
wasn't already discussed by Kent and Barry 7 years ago in the earlier 
thread (and probably numerous other times).

I don't think that I'm alone in my desire that c.l.l. will stay (or 
become!) a place where responsible adults can discuss issues about Lisp. 
Part of being an adult, in general, is realizing that you are not the 
center of the universe--there is a world aside from you and a history 
that predates you. One should acknowledge the achievements of the past 
and take advantage of its knowledge. This has become particularly 
convenient in our area of interest with the availability of Google, and 
we should encourage people (newbies in particular) to accept some 
responsibility for solving their own problems.

I am not anti-newbie. Far from it--I'm glad to help someone who's 
demonstrated that they're committed to learning. I don't want to chase 
newbies away for reasons of fairness--I don't want to be chased away 
myself by experts such as you.

David Sletten

P.s. I hope this doesn't come across as an attack on you, Steven. I 
simply intended to explain my motivation for my earlier post.
From: Steven M. Haflich
Subject: Re: Arrays
Date: 
Message-ID: <Izoqd.51178$QJ3.5965@newssvr21.news.prodigy.com>
David Sletten wrote:

> P.s. I hope this doesn't come across as an attack on you, Steven. I 
> simply intended to explain my motivation for my earlier post.

Not at all, but there was also a newbie motivation for my post.

Arrays are very rich objects in CL.  When a question is posed about
copying some sort of object, or testing one for equality, there are
unstated assumptions about the putpose of the copyu or the purpose
of the test.  The important thing for a newbie to learn is to make
those assumptions explicit -- not only to c.l.l, but also to himself
-- so the appropriate solution can be found.

The copy-array solution in your link is sophisticated and elegant,
but _not_ general.  No single solution sould be general, at least,
not without a zillion keyword arguments.

There are lots of circumstances in which one might want to copy an
array in CL, but one of the most common occurs when an adjustable
fill-pointer vector (often a string) is used to collect an
undetermined number of elements, and then when an entire entity has
been collected, a copy is made into a fixed-length, simple vector.
The Lisp reader does a lot of this when reading strings and symbols.

Now, the cited copy-array wouldn't be the appropriate kind of copy
for this application.  When one selects machinery to copy comething,
one needs to make explicit these sorts of possibilities in case they
matter.
From: David Sletten
Subject: Re: Arrays
Date: 
Message-ID: <Kpsqd.123361$Kl3.112025@twister.socal.rr.com>
Steven M. Haflich wrote:


> Not at all, but there was also a newbie motivation for my post.
> 
> Arrays are very rich objects in CL.  When a question is posed about
> copying some sort of object, or testing one for equality, there are
> unstated assumptions about the putpose of the copyu or the purpose
> of the test.  The important thing for a newbie to learn is to make
> those assumptions explicit -- not only to c.l.l, but also to himself
> -- so the appropriate solution can be found.
> 
> The copy-array solution in your link is sophisticated and elegant,
> but _not_ general.  No single solution sould be general, at least,
> not without a zillion keyword arguments.
> 
> There are lots of circumstances in which one might want to copy an
> array in CL, but one of the most common occurs when an adjustable
> fill-pointer vector (often a string) is used to collect an
> undetermined number of elements, and then when an entire entity has
> been collected, a copy is made into a fixed-length, simple vector.
> The Lisp reader does a lot of this when reading strings and symbols.
> 
> Now, the cited copy-array wouldn't be the appropriate kind of copy
> for this application.  When one selects machinery to copy comething,
> one needs to make explicit these sorts of possibilities in case they
> matter.

Fair enough. I see your point now. You're helping the newbie (and me) 
understand there are questions he maybe didn't even realize needed to be 
asked.

Thanks,
David Sletten
From: Nikodemus Siivola
Subject: Re: Arrays
Date: 
Message-ID: <cokbmo$i6anj$2@midnight.cs.hut.fi>
John Thingstad <··············@chello.no> wrote:

> For multidimentional arrays and arrays using offset into other arrays
> you need to roll your own.

Or read the ADJUST-ARRAY description carefully -- but I guess that can be
considered rolling your own. ;-)

Cheers,

 -- Nikodemus
From: Thomas A. Russ
Subject: Re: Arrays
Date: 
Message-ID: <ymi653l7pqr.fsf@sevak.isi.edu>
"John Thingstad" <··············@chello.no> writes:

> 
> On 25 Nov 2004 11:21:31 -0800, John Stivenson <··············@yahoo.com>  
> wrote:
> 
> > How can I make a copy of an array?
> > Is there some function which can do it for me?
> 
> Sometimes.
> If it is a simple-array of one dimention you can use
> copy-seq.
> For multidimentional arrays and arrays using offset into other arrays
> you need to roll your own.

Which isn't all too difficult if you don't care about the general case
and only need to do standard non-specialized arrays:

(defun simple-copy-array (original)
  (let ((copy (make-array (array-dimensions original))))
    (dotimes (i (array-total-size original))
      (setf (row-major-aref copy i) (row-major-aref original i)))
    copy))

For a more complex and featureful solution look at (one of?) Pascal
Bourguignon's messages in the thread "othello(reversi) game problem!"

-- 
Thomas A. Russ,  USC/Information Sciences Institute