comp.lang.ada
 help / color / mirror / Atom feed
* Problem with Gnat.Dynamic_Tables
@ 2002-07-09 23:04 achrist
  2002-07-11  0:19 ` achrist
  2002-07-11 15:13 ` Robert Dewar
  0 siblings, 2 replies; 10+ messages in thread
From: achrist @ 2002-07-09 23:04 UTC (permalink / raw)


I've got a problem with Gnat.Dynamic_Tables.  I want to use a dynamic
table of Ada.Strings.Unbounded.Unbounded_String data.  To do this I
use:
      
      package Csv_Table is new Gnat.Dynamic_Tables(
         Ada.Strings.Unbounded.Unbounded_String,
         Csv_Row_Index_Type,
         0, 1200, 100               
      );                           
       
      type Object is access all Csv_Table.Instance;

I set the size of the table with

      Csv_Table.Increment_Last( The_Table.all );

Before I assign to elements in the table, I check that the 
subscript is equal to Last.  This all works, but sometimes 
it crashes with a program error in the assignment:      

   procedure Add_Row (
         The_Table : in out Object; 
         New_Row   :        String  ) is 
   begin
      Csv_Table.Increment_Last( The_Table.all );
      The_Table.Table( Csv_Table.Last(The_Table.all) )
         := Ada.Strings.Unbounded.To_Unbounded_String(New_Row);
   end Add_Row;

The statement number that comes back from the crash is a-strunb.ads 368.
This is (gnat 3.14p1) the statement of the declaration of the 
Unbounded_String type.  I'm loading the data from a file, and the crash
happens around row 169 of my data, ie I get 168 values into the table
fine, then it crashes.  The crash happens in the statement that 
attempts to assign the unbounded string to the table entry.

I see in the Gnat.Dynamic_Tables comments that no initialization of
the array elements is done.  Does an unbounded_string need some
initialization?  If so, how can I make this work?

TIA for any help.


Al



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

* Re: Problem with Gnat.Dynamic_Tables
  2002-07-09 23:04 Problem with Gnat.Dynamic_Tables achrist
@ 2002-07-11  0:19 ` achrist
  2002-07-11 15:13 ` Robert Dewar
  1 sibling, 0 replies; 10+ messages in thread
From: achrist @ 2002-07-11  0:19 UTC (permalink / raw)


Update:

I got several kinds of memory errors and problems trying to use the
Gnat Dynamic_Tables, so I tried switching to the mw_components 
Dynamic_Arrays.  This package was similar enough that I could
pretty much substitute the operations of one package for the other,
just about one-for-one.  That fixed everthing; the whole program 
works fine now.

Al

achrist@easystreet.com wrote:
> 
> I've got a problem with Gnat.Dynamic_Tables.  I want to use a dynamic
> table of Ada.Strings.Unbounded.Unbounded_String data.  To do this I
> use:
> 
>       package Csv_Table is new Gnat.Dynamic_Tables(
>          Ada.Strings.Unbounded.Unbounded_String,
>          Csv_Row_Index_Type,
>          0, 1200, 100
>       );
> 
>       type Object is access all Csv_Table.Instance;
> 
> I set the size of the table with
> 
>       Csv_Table.Increment_Last( The_Table.all );
> 
> Before I assign to elements in the table, I check that the
> subscript is equal to Last.  This all works, but sometimes
> it crashes with a program error in the assignment:
> 
>    procedure Add_Row (
>          The_Table : in out Object;
>          New_Row   :        String  ) is
>    begin
>       Csv_Table.Increment_Last( The_Table.all );
>       The_Table.Table( Csv_Table.Last(The_Table.all) )
>          := Ada.Strings.Unbounded.To_Unbounded_String(New_Row);
>    end Add_Row;
> 
> The statement number that comes back from the crash is a-strunb.ads 368.
> This is (gnat 3.14p1) the statement of the declaration of the
> Unbounded_String type.  I'm loading the data from a file, and the crash
> happens around row 169 of my data, ie I get 168 values into the table
> fine, then it crashes.  The crash happens in the statement that
> attempts to assign the unbounded string to the table entry.
> 
> I see in the Gnat.Dynamic_Tables comments that no initialization of
> the array elements is done.  Does an unbounded_string need some
> initialization?  If so, how can I make this work?
> 
> TIA for any help.
> 
> Al



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

* Re: Problem with Gnat.Dynamic_Tables
  2002-07-09 23:04 Problem with Gnat.Dynamic_Tables achrist
  2002-07-11  0:19 ` achrist
@ 2002-07-11 15:13 ` Robert Dewar
  2002-07-11 20:35   ` achrist
  1 sibling, 1 reply; 10+ messages in thread
From: Robert Dewar @ 2002-07-11 15:13 UTC (permalink / raw)


achrist@easystreet.com wrote in message news:<3D2B6BFB.D4468F57@easystreet.com>...

> I see in the Gnat.Dynamic_Tables comments that no
> initialization of the array elements is done. 

Exactly!

> Does an unbounded_string need some
> initialization?

Of course it does, so this is just a misuse of the package



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

* Re: Problem with Gnat.Dynamic_Tables
  2002-07-11 15:13 ` Robert Dewar
@ 2002-07-11 20:35   ` achrist
  2002-07-12  3:42     ` Robert Dewar
  0 siblings, 1 reply; 10+ messages in thread
From: achrist @ 2002-07-11 20:35 UTC (permalink / raw)


Robert Dewar wrote:
> 
> achrist@easystreet.com wrote in message news:<3D2B6BFB.D4468F57@easystreet.com>...
> 
> > I see in the Gnat.Dynamic_Tables comments that no
> > initialization of the array elements is done.
> 
> Exactly!
> 
> > Does an unbounded_string need some
> > initialization?
> 
> Of course it does, so this is just a misuse of the package

Thanks.

I'm puzzled why the mw_components Dynamic_Array seens to work without
me, as user of the generic package, doing anything different (as far
as I see) when using that package from what didn't work with GNAT
dynamic tables.  The GNAT code appeared to work, but crashed when
element #167 or so was added to the table.  The Dynamic_Array 
appears to work for as much data as I have (about 1500 entries in
my table), but is it likely that there will be a lurking bug in 
this usage?  If there is something about an Unbounded_String that
means it must be initialized before it can be assigned to, how  could 
that initialization happen to the mw_conponents Dynamic_Array 
components when I don't pass the generic any initialization procedure
or anything related to initialization?      


Al



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

* Re: Problem with Gnat.Dynamic_Tables
  2002-07-11 20:35   ` achrist
@ 2002-07-12  3:42     ` Robert Dewar
  2002-07-12  7:01       ` achrist
  0 siblings, 1 reply; 10+ messages in thread
From: Robert Dewar @ 2002-07-12  3:42 UTC (permalink / raw)


achrist@easystreet.com wrote in message news:<3D2DEC07.9E47AC3@easystreet.com>...
> Robert Dewar wrote:
> > 
> I'm puzzled why the mw_components Dynamic_Array seens to 
> work without
> me, as user of the generic package, doing anything 
> different (as far
> as I see) when using that package from what didn't work 
> with GNAT
> dynamic tables.  


The GNAT dynamic table package malfunctioned it because
you misused it and ignored a critical rule in the spec.
The Dynamic_Array package is a completely different package. You need
to read the documentation for this package carefully and make sure you
are following it.

Nothing puzzling about two different packages having two
different specifications!



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

* Re: Problem with Gnat.Dynamic_Tables
  2002-07-12  3:42     ` Robert Dewar
@ 2002-07-12  7:01       ` achrist
  2002-07-12 14:23         ` Mats Weber
  0 siblings, 1 reply; 10+ messages in thread
From: achrist @ 2002-07-12  7:01 UTC (permalink / raw)


Robert Dewar wrote:
> 
> The GNAT dynamic table package malfunctioned it because
> you misused it and ignored a critical rule in the spec.
> The Dynamic_Array package is a completely different package. You need
> to read the documentation for this package carefully and make sure you
> are following it.
> 
> Nothing puzzling about two different packages having two
> different specifications!

Thanks.

I don't see any examples of how to use for either package.  For 
the mw_components (includes the Dybanic_Array), there is the page:

http://lglwww.epfl.ch/Team/MW/mw_components.html
This doesn't say anything about initialization, but it does say that
there is no finalization.  I wonder if this means that I'm leaking
memory when I use these components for unbounded strings.  

Anyone have any pointers to more extensive docs for these?


Al



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

* Re: Problem with Gnat.Dynamic_Tables
  2002-07-12  7:01       ` achrist
@ 2002-07-12 14:23         ` Mats Weber
  2002-07-12 16:56           ` achrist
  0 siblings, 1 reply; 10+ messages in thread
From: Mats Weber @ 2002-07-12 14:23 UTC (permalink / raw)


achrist@easystreet.com wrote:

> http://lglwww.epfl.ch/Team/MW/mw_components.html
> This doesn't say anything about initialization, but it does say that
> there is no finalization.  I wonder if this means that I'm leaking
> memory when I use these components for unbounded strings.  
> 
> Anyone have any pointers to more extensive docs for these?

Controlled types will work with my components because a new object is 
created every time you insert into the table. and that is probably true 
of all my structure components, even though they were developped in Ada 83.

GNAT.Dynamic_Table does not work because it preallocates an array for 
storing its stuff, thus avoiding one allocation per insertion into the 
table.

You should describe your problem (what do you need to store into the 
table, what is the distribution of values of your index/key type ?) so 
that we can give you a better hint for choosing the right component.




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

* Re: Problem with Gnat.Dynamic_Tables
  2002-07-12 14:23         ` Mats Weber
@ 2002-07-12 16:56           ` achrist
  2002-07-12 22:52             ` Mats Weber
  0 siblings, 1 reply; 10+ messages in thread
From: achrist @ 2002-07-12 16:56 UTC (permalink / raw)


Mats Weber wrote:
> 
> 
> You should describe your problem (what do you need to store into the
> table, what is the distribution of values of your index/key type ?) so
> that we can give you a better hint for choosing the right component.

I wanted a variable-length array of variable-length (Unbounded) strings, 
with random access and update based on an integer (Natural) index.
This works fine with your generic Dynamic_Array, thanks very much.  If
I'm not leaking memory or going to have a crash at some point because of
not initializing, then I'm happy with the solution.  I see that it
works like a Map, which means, I suppose, that there is some search 
involved in simply accessing an element of the array, but this is not a
noticeable problem, given all the other inefficiencies in my code, which
hardly add up to anything noticeable in toto.

Thanks very much.  


Al



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

* Re: Problem with Gnat.Dynamic_Tables
  2002-07-12 16:56           ` achrist
@ 2002-07-12 22:52             ` Mats Weber
  2002-07-12 23:02               ` achrist
  0 siblings, 1 reply; 10+ messages in thread
From: Mats Weber @ 2002-07-12 22:52 UTC (permalink / raw)


achrist@easystreet.com wrote in message news:<3D2F0A3D.8DEF335F@easystreet.com>...

> [...] I see that it
> works like a Map, which means, I suppose, that there is some search 
> involved in simply accessing an element of the array, but this is not a
> noticeable problem, given all the other inefficiencies in my code, which
> hardly add up to anything noticeable in toto.

Dynamic_Array is implemented using AVL trees. So there is searching
involved for each access and the complexity is O(Log N). But it is a
far better choice than an array implementation if your key values are
sparse.



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

* Re: Problem with Gnat.Dynamic_Tables
  2002-07-12 22:52             ` Mats Weber
@ 2002-07-12 23:02               ` achrist
  0 siblings, 0 replies; 10+ messages in thread
From: achrist @ 2002-07-12 23:02 UTC (permalink / raw)


Mats Weber wrote:
> 
> achrist@easystreet.com wrote in message news:<3D2F0A3D.8DEF335F@easystreet.com>...
> 
> > [...] I see that it
> > works like a Map, which means, I suppose, that there is some search
> > involved in simply accessing an element of the array, but this is not a
> > noticeable problem, given all the other inefficiencies in my code, which
> > hardly add up to anything noticeable in toto.
> 
> Dynamic_Array is implemented using AVL trees. So there is searching
> involved for each access and the complexity is O(Log N). But it is a
> far better choice than an array implementation if your key values are
> sparse.

No, my key values are not sparse, I've got all the keys from 0 to N, 
where N might get up to 10^5 or so, less than 10^4 for the app at hand.
Your dynamic_array was the first best thing I saw available free off the 
shelf for a variable N array of unbounded_string components.  As a 
believer in not reinventing anything that's already been reinvented,  I 
sure don't want to write any code when there's something already that 
works. And I do mean "any code".  

Any other freely available package do this better?


Al



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

end of thread, other threads:[~2002-07-12 23:02 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-09 23:04 Problem with Gnat.Dynamic_Tables achrist
2002-07-11  0:19 ` achrist
2002-07-11 15:13 ` Robert Dewar
2002-07-11 20:35   ` achrist
2002-07-12  3:42     ` Robert Dewar
2002-07-12  7:01       ` achrist
2002-07-12 14:23         ` Mats Weber
2002-07-12 16:56           ` achrist
2002-07-12 22:52             ` Mats Weber
2002-07-12 23:02               ` achrist

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