dynamic arrays 
Author Message
 dynamic arrays

does Smalltalk support dynamic creation of collection size or file size, or
assigning a collection to a file size not known until read. What sort of syntax
is created / used for this? Can someone give an example?
Luce


Sun, 24 Apr 2005 00:20:54 GMT  
 dynamic arrays

Play with

Array new: 10.   "creates an array with ten nils"

OrderedCollection new: 10.  "creates a collection of size 0 with storage
preallocated for 10 items"

Quote:

> does Smalltalk support dynamic creation of collection size or file size, or
> assigning a collection to a file size not known until read. What sort of syntax
> is created / used for this? Can someone give an example?
> Luce

--
Reinout Heeck
-------------
Soops - Specialist in object technology.
http://www.soops.nl/


Sun, 24 Apr 2005 15:42:51 GMT  
 dynamic arrays
But the point is, to create a collection of size  unknown, not of size 10


Sun, 24 Apr 2005 19:41:33 GMT  
 dynamic arrays

Quote:

> But the point is, to create a collection of size  unknown, not of size 10

Oh,

in that case use a collection that can grow and use #new.
It's initial size will report as 0.

If you want 'unkown' as size, you will need to explain more clearly what
you need...

--
Reinout Heeck
-------------
Soops - Specialist in object technology.
http://www.soops.nl/



Sun, 24 Apr 2005 20:27:48 GMT  
 dynamic arrays

Quote:

> But the point is, to create a collection of size  unknown, not of size 10

"unknown" is not a valid size.
If you mean variable size, that's what most Smalltalk collections are.
Arrays (including their specialized subclasses) and Strings are the most
visible exception.
Everything else will just grow when needed.
The OrderedCollection case is probably a bad example. OrderedCollection
new: 10 creates an OrderedCollection whose initial size is 0 (it does
not contain any objects) but which can accomodate up to 10 elements
without the need to grow (which can be expensive).
So if you're reading a huge file with 1000000 lines, and you create an
object for every line, and you put them all into an OrderedCollection,
you should maybe create the OrderedCollection big enough to begin with.

Cheers,
Hans-Martin



Sun, 24 Apr 2005 19:52:30 GMT  
 dynamic arrays


Quote:
>But the point is, to create a collection of size  unknown, not of size 10

- If I understand it right what you're asking -
Just create them with #new. Some dialects treat Array as not
efficient to grow but others do not.

ordcol := OrderedCollection new.
ordcol size.
ordcol add: 'TheFirstElementAndSoOn'.
ordcol size.

aset := Set new.
aset size.
aset add: ordcol.
aset size.

adict := Dictionary new.
adict size.
adict at: #ordered put: ordol;
at: #set put: aset.
adict size.
..............

Best regards,


---------------------------
An old amateur



Sun, 24 Apr 2005 21:20:51 GMT  
 dynamic arrays

Quote:

> But the point is, to create a collection of size  unknown, not of size 10

Let's check what you are asking about:
- you're going to read a size n from a file (or from somewhere else)
at runtime
- you want to create a collection at runtime that can contain n items

You're concerned Smalltalk might insist that you set the size of the
collection before runtime. Is that it?
Smalltalk doesn't require you to set that information before runtime,
most things (including "type" checking) happen at runtime.

If you wanted a collection that would change size as you added and
removed stuff then-

myCollection := OrderedCollection new: n.

If you knew the collection would always be the same size then-

myCollection := Array new: n.

(Of course you're going to get the value of n from somewhere at
runtime and pass it to the method that has those statements)



Mon, 25 Apr 2005 00:55:58 GMT  
 dynamic arrays

Quote:

> But the point is, to create a collection of size  unknown, not of size 10

In Smalltalk, Arrays are created with a fixed size, which cannot be
changed.

OrderedCollections, on the other hand, can dynamically grow as new
objects are added to them.  Every OrderedCollection is created with an
initial capacity.

OrderedCollection new: <anInteger>

creates an OrderedCollection with an initial capacity of <anInteger>.

OrderedCollection new

creates an OrderedCollection with an initial capacity specified by the
given implementation.

In either case, the OrderedCollection created will grow to accommodate
new objects added to it.

OrderedCollections can contain multiple copies of the same object.  If
you want a dynamically sizable collection that contains no duplicates,
you would want to use Set or IdentitySet.  And if you want a
collection that contains duplicates and keeps a tally of how many
times a given element occurs, you want Bag.  (Typically Bag is
implemented using a private Dictionary whose keys are the elements and
whose values are the tally for each element.  So a Bag to which a
given element had been added 5 times, would actually contain the
element only once, assigning it a tally of 5.)

I hope this answers your question in enough detail to be useful.  If
not, perhaps it would be best to describe your envisioned application,
so we would know what Smalltalk facilities best suit your needs.

-30-
Bob Ingria
As always, at a slight angle to the universe



Mon, 25 Apr 2005 01:37:09 GMT  
 dynamic arrays
thanking for the replies, although still uncertain. In some langauges "new"
seems to be exclusively a heap-related issue, whereas in smalltalk, you use it
to create just any new object(?).


Mon, 25 Apr 2005 02:36:32 GMT  
 dynamic arrays
Yes that.  And any other creation method that exists or you want to create.
Quote:

> thanking for the replies, although still uncertain. In some langauges "new"
> seems to be exclusively a heap-related issue, whereas in smalltalk, you use it
> to create just any new object(?).



Mon, 25 Apr 2005 02:47:45 GMT  
 dynamic arrays

Quote:

> thanking for the replies, although still uncertain. In some langauges "new"
> seems to be exclusively a heap-related issue, whereas in smalltalk, you use it
> to create just any new object(?).

In Smalltalk, all new objects are created on the heap.  There are
different heaps (new space, old space, fixed space, etc), but all new
objects are dynamically allocated in a similar way.

David Buck
Simberon Inc.
www.simberon.com



Mon, 25 Apr 2005 03:40:33 GMT  
 dynamic arrays



Quote:
> > But the point is, to create a collection of size  unknown, not of size
10

> In Smalltalk, Arrays are created with a fixed size, which cannot be
> changed.

As an addendum, here are comments regarding QKS Smalltalk-91 and derived
forms leading into my involvement in the ANSI Smalltalk process and
thereafter into the evolution of SmallScript/SSharp.

In (QKS and SmallScript) Smalltalk <Array> is resizeable. You can declare a
particular instance to be non-resizeable, and you can declare a particular
instance to be a const, and you can request that an instance be intern'ed to
return a canonical const version. (The preceding comments are actually true
for all objects since those are basic features of the object model).

There are different types of arrays available.

    Array
    ArrayConst
    TypedArray
    ByteArray
    ByteArrayConst
    DimensionedArray
    PointerArray
    ...

And all the generics derivatives of those. I.e.,

    SomeBehavior[]
    SomeBehavior[*]


    ...
    SomeBehavior[][][]
    SomeBehavior[*][][]
    ...
    etc
--------------

OrderedCollection differs from Array only in its constructor. Based on
classic Smalltalk evolution choices the OrderedCollection constructor #new:
takes a "hint" as to the size the collection will probably grow to, whereas
the Array constructor #new: takes an explicit value that indicates the size
you want it to be at the time it is constructed.

    (Array new: 10) size => 10

    (OrderedCollection new: 10) size => 0

Which is odd, confusing, and generally undesirable [but alas classic
Smalltalk implementations are what they are]. It would have been a more
consistent collection constructor protocol for #new: if it had been a
message of the form:

    Array new: 0 growthFactor: 10.

    OrderedCollection new: 0 growthFactor: 10.

The "OrderedCollection" vs "Array" issues are an exposure of implementation
that should not surface so explicitly in the framework.

SmallScript's selector namespace system allows us to correct the issue going
forward, while simultaneously allowing us to support the legacy behavior for
classic smalltalk programs. I.e., For classic Smalltalk compatibility, the
SmallScript system uses selector-namespaces to provide a classic
scoped-version of #new: that creates indexed-slot instances that are
not-resizeable. Similarly, it uses a mixin to define OrderedCollection as a
constructor or resizeable indexed slot collections.

-- Dave S. [www.smallscript.org]

Quote:

> OrderedCollections, on the other hand, can dynamically grow as new
> objects are added to them.  Every OrderedCollection is created with an
> initial capacity.

> OrderedCollection new: <anInteger>

> creates an OrderedCollection with an initial capacity of <anInteger>.

> OrderedCollection new

> creates an OrderedCollection with an initial capacity specified by the
> given implementation.

> In either case, the OrderedCollection created will grow to accommodate
> new objects added to it.

> OrderedCollections can contain multiple copies of the same object.  If
> you want a dynamically sizable collection that contains no duplicates,
> you would want to use Set or IdentitySet.  And if you want a
> collection that contains duplicates and keeps a tally of how many
> times a given element occurs, you want Bag.  (Typically Bag is
> implemented using a private Dictionary whose keys are the elements and
> whose values are the tally for each element.  So a Bag to which a
> given element had been added 5 times, would actually contain the
> element only once, assigning it a tally of 5.)

> I hope this answers your question in enough detail to be useful.  If
> not, perhaps it would be best to describe your envisioned application,
> so we would know what Smalltalk facilities best suit your needs.

> -30-
> Bob Ingria
> As always, at a slight angle to the universe



Thu, 28 Apr 2005 10:39:44 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Dynamic Array?

2. Dynamic arrays question

3. Dynamic array dimensioning

4. Dynamic Arrays in PBU's PB 3.2

5. dynamic arrays in FB/PB?

6. Dynamic Arrays in Clarion

7. Dynamic array of groups: what problems

8. Dynamic Array access

9. Pointers and dynamic arrays - not for the faint hearted - very invloved

10. sparse matrix or dynamic array help!

11. sparse matrix or dynamic array

12. Creating a Dynamic Array

 

 
Powered by phpBB® Forum Software