comp.lang.ada
 help / color / mirror / Atom feed
* Array resizing
@ 1997-10-19  0:00 Dmitriy Anisimkov
  1997-10-19  0:00 ` Matthew Heaney
  0 siblings, 1 reply; 2+ messages in thread
From: Dmitriy Anisimkov @ 1997-10-19  0:00 UTC (permalink / raw)



Can I resize dinamicaly allocatad array 
or I mast allocate new array and copy previous to it?

If array is big copying is more slowely than reallocation.




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Array resizing
  1997-10-19  0:00 Array resizing Dmitriy Anisimkov
@ 1997-10-19  0:00 ` Matthew Heaney
  0 siblings, 0 replies; 2+ messages in thread
From: Matthew Heaney @ 1997-10-19  0:00 UTC (permalink / raw)



In article <01bcdc8c$924d4180$e7207cc1@anishome>, "Dmitriy Anisimkov"
<ts@quadrat.omsk.su> wrote:

>Can I resize dinamicaly allocatad array 
>or I mast allocate new array and copy previous to it?
>
>If array is big copying is more slowely than reallocation.

Once an array is allocated, its size is fixed forever.

However, if you know the maximum size that you'd ever need it to be, you
could declare a mutable type that can have different sizes during its
lifetime.

type Item_Array is array (Positive range <>) of Item;

subtype Length_Range is 0 .. Max_Size;

type Mutable_Item_Array (Length : Length_Range := 0) is
   record
      Items : Item_Array (1 .. Length);
   end record;

Item_Buffer : Mutable_Item_Array;

procedure Update (Items : Item_Array) is
begin
   Item_Buffer := (Items'Length, Items);
end;

So during its lifetime, the Item_Buffer contains an array whose length changes.

If this isn't what you want (perhaps you don't know the maximum size), then
yes, you would have to allocate another array, and copy the old items and
new items to the new array.

It may be that you really want some other data structure, at a higher level
of abstraction, implemented in terms of an array.  One idea is to allocate
array chunks, linked together as a list.  When you need more room, you just
allocate another, fixed-size chunk and attach it to the end of the list. 
You won't be able to directly access a specific item though; you'll have to
figure which node of the list it's on, then iterate though the list until
you find that node.

Another similar idea is to use a B-tree.

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1997-10-19  0:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-10-19  0:00 Array resizing Dmitriy Anisimkov
1997-10-19  0:00 ` Matthew Heaney

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox