How to deep copy elements when using collectons?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to deep copy elements when using collectons?

Xinyu Liu
Hi,

I have a junior question when using collection to store/get my own defined element.
My elements is defined as below:

Object subclass: #MyItem
    instanceVariableNames: 'subItems'
    classVariableNames: ''
    poolDictionaries: ''
    category: 'MyTest'


Where the subItems instance var is also a collection.

But I don't know how to store/get such kind of MyItem by using collection, I think I need deep copy an element when add it to the collection.

I don't think the following code works.

|temp coll|
temp := MyItem new.
temp addSubItem: 'hello'; addSubItem 'world'; yourself.
coll := OrderedCollection new.
coll add: temp

How can I add the deep copied element to my collection and retrieve it later? shall I overload the #copy method?

Best regards and thanks
-Xinyu

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to deep copy elements when using collectons?

Herbert König
Hello Xinyu,


if you come from another programming language, a collection stores
only pointers to objects. No chance (and no need) to copy an object
into a collection.

XL> Object subclass: #MyItem
XL>     instanceVariableNames: 'subItems'
XL>     classVariableNames: ''
XL>     poolDictionaries: ''
XL>     category: 'MyTest'

XL> But I don't know how to store/get such kind of MyItem by
XL> using collection, I think I need deep copy an element when add it
XL> to the collection.
No imho, see below.

XL> I don't think the following code works.

XL> |temp coll|
XL> temp := MyItem new.
XL> temp addSubItem: 'hello'; addSubItem 'world'; yourself.
XL> coll := OrderedCollection new.
XL> coll add: temp

assuming subItems is initialised as:
OrderedCollection new
and addSubItem: is defined as:
subItems add: anObject
the code should work.


XL> How can I add the deep copied element to my collection and
XL> retrieve it later? shall I overload the #copy method?

The main question is: why would you want to copy the element?
Each MyItem holds to its collection of subItems and coll holds to your
temp.

There may be reasons you want to use a copy put usually you just put
your objects into the collection and retrieve them with coll at:

If you really want to store a copy you could:
temp1 := temp deepCopy.
coll add: temp1.

BTW, do you know of the free Smalltalk books, Squeak by Example being
the latest and specifically tailored to Squeak?


XL> Best regards and thanks
XL> -Xinyu

Cheers

Herbert                            mailto:[hidden email]

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to deep copy elements when using collectons?

Xinyu Liu
Hi, Herbert

Thank you very much. I got it. I referred to SBE(both smalltalk by example and squeak by example :-)).

the reason why I want to copy is because I don't want the element stored in collection to change when I modify temp var later.
and because the element has other collections as instance vars, so i am not sure if I need deep copy.

I tried something like this:

coll add: (temp deepCopy).

...modify temp ex. temp:= MyItem new ...

temp := (coll at: 1)  deepCopy.

The above code works when there are 2 levels of  collection.

Thanks again and best regards.
-Xinyu

On Jan 1, 2008 11:19 PM, Herbert König < [hidden email]> wrote:
Hello Xinyu,


if you come from another programming language, a collection stores
only pointers to objects. No chance (and no need) to copy an object
into a collection.

XL> Object subclass: #MyItem
XL> instanceVariableNames: 'subItems'
XL> classVariableNames: ''
XL> poolDictionaries: ''
XL> category: 'MyTest'

XL> But I don't know how to store/get such kind of MyItem by
XL> using collection, I think I need deep copy an element when add it
XL> to the collection.
No imho, see below.

XL> I don't think the following code works.

XL> |temp coll|
XL> temp := MyItem new.
XL> temp addSubItem: 'hello'; addSubItem 'world'; yourself.
XL> coll := OrderedCollection new.
XL> coll add: temp

assuming subItems is initialised as:
OrderedCollection new
and addSubItem: is defined as:
subItems add: anObject
the code should work.


XL> How can I add the deep copied element to my collection and
XL> retrieve it later? shall I overload the #copy method?

The main question is: why would you want to copy the element?
Each MyItem holds to its collection of subItems and coll holds to your
temp.

There may be reasons you want to use a copy put usually you just put
your objects into the collection and retrieve them with coll at:

If you really want to store a copy you could:
temp1 := temp deepCopy.
coll add: temp1.

BTW, do you know of the free Smalltalk books, Squeak by Example being
the latest and specifically tailored to Squeak?


XL> Best regards and thanks
XL> -Xinyu

Cheers

Herbert                            mailto:[hidden email]

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to deep copy elements when using collectons?

keith1y
To modify the behaviour of copy to be more appropriate for your class,
the facility is provided via #postCopy.

The basic copy implementation is as follows.

copy

^ self shallowCopy postCopy

specializing #postCopy for your class, it can perfrom further copies of
its inst vars if needed.

postCopy

instVarWithCollection := instVar withCollection copy.

So now the collection is a different collection although it contains the
same elements.


best regards

Keith


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners