comp.lang.ada
 help / color / mirror / Atom feed
* Nested declares, constant size arrays and clarity
@ 2007-10-03 22:58 Mateusz Papiernik
  2007-10-03 23:49 ` Ludovic Brenta
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Mateusz Papiernik @ 2007-10-03 22:58 UTC (permalink / raw)


Hey!

I'm starting off with Ada95. I'm having some problems with clarity of my 
solution, which - I think - is not as good as I would like to.

I've got my root procedure in which I read some number from the standard 
input. Then I create an array of MyType with size read before.

procedure mxp is
   ... -- some record type declarations
   ... -- some *functions*
begin
   ... -- read size
   declare
     type MyTypeArray is array (1...sizegiven) of MyType;
     MyArray: MyTypeArray;
     ... -- some *functions* doing something with newly made array
   begin
     ... -- these function calls
   end;
end;

The problem is, I don't really like the idea of declaring some functions 
in the root procedure declaration, and some others in nested declare 
section. Due to that I've got function implementations in two places, 
which somehow feels awkward to me.

Is there any way to accomplish the same without that nesting _and_ 
without building external module/using OOP/pointers?

Maybe there is a way to define general array type of unknown size, then 
declare functions doing something with it in the topmost declaration 
section, and then only somehow initialise the array with given size?



Thanks for any advice!
-- 
Mateusz Papiernik, Maticomp Webdesign
Projektowanie i realizacja witryn WWW
mati@maticomp.net, http://www.maticomp.net
"One man can make a difference" - Wilton Knight



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

* Re: Nested declares, constant size arrays and clarity
  2007-10-03 22:58 Nested declares, constant size arrays and clarity Mateusz Papiernik
@ 2007-10-03 23:49 ` Ludovic Brenta
  2007-10-04  8:49   ` Mateusz Papiernik
  2007-10-04  0:20 ` Jeffrey R. Carter
  2007-10-04  0:23 ` anon
  2 siblings, 1 reply; 15+ messages in thread
From: Ludovic Brenta @ 2007-10-03 23:49 UTC (permalink / raw)


Mateusz Papiernik <mati@maticomp.net> writes:
> Hey!
>
> I'm starting off with Ada95. I'm having some problems with clarity of
> my solution, which - I think - is not as good as I would like to.
>
> I've got my root procedure in which I read some number from the
> standard input. Then I create an array of MyType with size read before.
>
> procedure mxp is
>   ... -- some record type declarations
>   ... -- some *functions*
> begin
>   ... -- read size
>   declare
>     type MyTypeArray is array (1...sizegiven) of MyType;
>     MyArray: MyTypeArray;
>     ... -- some *functions* doing something with newly made array
>   begin
>     ... -- these function calls
>   end;
> end;
>
> The problem is, I don't really like the idea of declaring some
> functions in the root procedure declaration, and some others in nested
> declare section. Due to that I've got function implementations in two
> places, which somehow feels awkward to me.
>
> Is there any way to accomplish the same without that nesting _and_
> without building external module/using OOP/pointers?
>
> Maybe there is a way to define general array type of unknown size,
> then declare functions doing something with it in the topmost
> declaration section, and then only somehow initialise the array with
> given size?

Yes, it is called an "unconstrained array type".  Here is how you
would do it:

procedure mxp is
   type My_Record_Type is record ... end record;
   type My_Array_Type is array (Positive range <>) of My_Record_Type;
   -- the type is unconstrained

   Size : constant Natural := Get_From_Keyboard;
   My_Array : My_Array_Type (1 .. Size); -- the array is constrained
begin
   -- Call the functions
end mxp;

Note: the array type uses Positive as the index type; the Size is a
Natural.  Positive and Natural are both subtypes of Integer and so are
compatible with one another:

subtype Positive is Integer range 1 .. Integer'Last;
subtype Natural  is Integer range 0 .. Integer'Last;

If the user types zero for the size, then My_Array will have the
bounds 1 .. 0.  THIS IS OKAY!!!  In that case, looping over the array
will simply do nothing.  You normally loop over an array like this:

   for K in My_Array'Range loop
      -- ...
   end loop;

PS. What I described above is exactly how the predefined type String
works; it is an unconstrained array of Character's.  Look at the
standard subprograms that operate on Strings for more inspiration.

HTH

-- 
Ludovic Brenta.



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

* Re: Nested declares, constant size arrays and clarity
  2007-10-03 22:58 Nested declares, constant size arrays and clarity Mateusz Papiernik
  2007-10-03 23:49 ` Ludovic Brenta
@ 2007-10-04  0:20 ` Jeffrey R. Carter
  2007-10-04  8:51   ` Mateusz Papiernik
  2007-10-04  0:23 ` anon
  2 siblings, 1 reply; 15+ messages in thread
From: Jeffrey R. Carter @ 2007-10-04  0:20 UTC (permalink / raw)


Mateusz Papiernik wrote:
> 
> I'm starting off with Ada95. I'm having some problems with clarity of my 
> solution, which - I think - is not as good as I would like to.

You're right. Most of them can be corrected by learning to use the 
underline in identifiers. My_Array is much clearer than Myarray. Uniform 
  capitalization will help, too: mxp, MyArray, sizegiven, ...

> The problem is, I don't really like the idea of declaring some functions 
> in the root procedure declaration, and some others in nested declare 
> section. Due to that I've got function implementations in two places, 
> which somehow feels awkward to me.

I don't see any problem with your approach; it seems clear and easy to 
understand to me. But the use of unconstrained array types and 
operations on them, as suggested elsewhere, may help give you something 
more to your taste.

-- 
Jeff Carter
"Spam! Spam! Spam! Spam! Spam! Spam! Spam! Spam!"
Monty Python's Flying Circus
53



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

* Re: Nested declares, constant size arrays and clarity
  2007-10-03 22:58 Nested declares, constant size arrays and clarity Mateusz Papiernik
  2007-10-03 23:49 ` Ludovic Brenta
  2007-10-04  0:20 ` Jeffrey R. Carter
@ 2007-10-04  0:23 ` anon
  2 siblings, 0 replies; 15+ messages in thread
From: anon @ 2007-10-04  0:23 UTC (permalink / raw)


There are basically two ways of dealing with your question.

The First is to create a function or procedure that contains the statements 

     type MyTypeArray is array (1...sizegiven) of MyType;
     MyArray: MyTypeArray;

and then place this and all other routines as children of MXP.  In this 
case, it is easier to use pointers.

The second way is to create a package for MyType. And place all 
funtions and  procedures that handles the internal working of 
MyType within that package. 

        You need need routines for:

            function:   Create_MyType_Array ( Size, ... )
            procedure:  Destroy_MyType_Array ( MyTypeArray )
            function:   Retrive_MyTypeArray ( Index, element ) 
            procedure:  Store_MyTypeArray ( Index, 
                                            element, value, ... ) 
            ...
            If needed:
              procedure:  Get ( MyTypeArray, ... )
              procedure:  Put ( MyTypeArray, ... )


Then the main routine could call these routines.




In <fe1748$it1$1@nemesis.news.tpi.pl>, Mateusz Papiernik <mati@maticomp.net> writes:
>Hey!
>
>I'm starting off with Ada95. I'm having some problems with clarity of my 
>solution, which - I think - is not as good as I would like to.
>
>I've got my root procedure in which I read some number from the standard 
>input. Then I create an array of MyType with size read before.
>
>procedure mxp is
>   ... -- some record type declarations
>   ... -- some *functions*
>begin
>   ... -- read size
>   declare
>     type MyTypeArray is array (1...sizegiven) of MyType;
>     MyArray: MyTypeArray;
>     ... -- some *functions* doing something with newly made array
>   begin
>     ... -- these function calls
>   end;
>end;
>
>The problem is, I don't really like the idea of declaring some functions 
>in the root procedure declaration, and some others in nested declare 
>section. Due to that I've got function implementations in two places, 
>which somehow feels awkward to me.
>
>Is there any way to accomplish the same without that nesting _and_ 
>without building external module/using OOP/pointers?
>
>Maybe there is a way to define general array type of unknown size, then 
>declare functions doing something with it in the topmost declaration 
>section, and then only somehow initialise the array with given size?
>
>
>
>Thanks for any advice!
>-- 
>Mateusz Papiernik, Maticomp Webdesign
>Projektowanie i realizacja witryn WWW
>mati@maticomp.net, http://www.maticomp.net
>"One man can make a difference" - Wilton Knight




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

* Re: Nested declares, constant size arrays and clarity
  2007-10-03 23:49 ` Ludovic Brenta
@ 2007-10-04  8:49   ` Mateusz Papiernik
  2007-10-04 11:33     ` Ludovic Brenta
  2007-10-05  4:54     ` Jeffrey R. Carter
  0 siblings, 2 replies; 15+ messages in thread
From: Mateusz Papiernik @ 2007-10-04  8:49 UTC (permalink / raw)


Ludovic Brenta pisze:
> Yes, it is called an "unconstrained array type".  Here is how you
> would do it:

Thanks! That's something I missed reading docs. Basically, that's 
exactly what I want to do.

> PS. What I described above is exactly how the predefined type String
> works; it is an unconstrained array of Character's.  Look at the
> standard subprograms that operate on Strings for more inspiration.

With strings I also have to work some way. As for now I'm using 
Unbounded_Strings, because easy implementations of functions like split 
(I'm parsing CSV file). Is it a good practice to use U_S, or should I 
fall back to normal strings and fight with implementation?


Thanks!
-- 
Mateusz Papiernik, Maticomp Webdesign
Projektowanie i realizacja witryn WWW
mati@maticomp.net, http://www.maticomp.net
"One man can make a difference" - Wilton Knight



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

* Re: Nested declares, constant size arrays and clarity
  2007-10-04  0:20 ` Jeffrey R. Carter
@ 2007-10-04  8:51   ` Mateusz Papiernik
  2007-10-05  2:45     ` Steve Whalen
  0 siblings, 1 reply; 15+ messages in thread
From: Mateusz Papiernik @ 2007-10-04  8:51 UTC (permalink / raw)


Jeffrey R. Carter pisze:
> You're right. Most of them can be corrected by learning to use the 
> underline in identifiers.

Well, sorry :) I did in the real code, and here I wrote the fragment "ad 
hoc" forgetting about it.

> My_Array is much clearer than Myarray. Uniform 
> capitalization will help, too: mxp, MyArray, sizegiven, ...

Yup, of course :) Thanks :)

> I don't see any problem with your approach; it seems clear and easy to 
> understand to me. But the use of unconstrained array types and 
> operations on them, as suggested elsewhere, may help give you something 
> more to your taste.

It's not that's difficult to understand - I just, after dealing with 
other languaes, am not yet used to defining functions in nested blocks. 
Which acctually may be a good aproach and logical. Ada in many places 
force me to change my mind schemes :)


Regards,
-- 
Mateusz Papiernik, Maticomp Webdesign
Projektowanie i realizacja witryn WWW
mati@maticomp.net, http://www.maticomp.net
"One man can make a difference" - Wilton Knight



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

* Re: Nested declares, constant size arrays and clarity
  2007-10-04  8:49   ` Mateusz Papiernik
@ 2007-10-04 11:33     ` Ludovic Brenta
  2007-10-04 12:45       ` Dmitry A. Kazakov
  2007-10-05  4:54     ` Jeffrey R. Carter
  1 sibling, 1 reply; 15+ messages in thread
From: Ludovic Brenta @ 2007-10-04 11:33 UTC (permalink / raw)


Mateusz Papiernik <mati@maticomp.net> writes:

> Ludovic Brenta pisze:
>> Yes, it is called an "unconstrained array type".  Here is how you
>> would do it:
>
> Thanks! That's something I missed reading docs. Basically, that's
> exactly what I want to do.
>
>> PS. What I described above is exactly how the predefined type String
>> works; it is an unconstrained array of Character's.  Look at the
>> standard subprograms that operate on Strings for more inspiration.
>
> With strings I also have to work some way. As for now I'm using
> Unbounded_Strings, because easy implementations of functions like
> split (I'm parsing CSV file). Is it a good practice to use U_S, or
> should I fall back to normal strings and fight with implementation?

I guess that depends on whether the input lines have a fixed length or
not.  The main disadvantage of Unbounded_Strings are that they are
much slower than regular Strings.  A good compromise might be to use
Bounded_Strings instead: you would get efficiency and flexibility at
the same time at the cost of perhaps consuming more memory than
strictly necessary.

-- 
Ludovic Brenta.



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

* Re: Nested declares, constant size arrays and clarity
  2007-10-04 11:33     ` Ludovic Brenta
@ 2007-10-04 12:45       ` Dmitry A. Kazakov
  2007-10-04 13:11         ` Mateusz Papiernik
  2007-10-05  4:58         ` Jeffrey R. Carter
  0 siblings, 2 replies; 15+ messages in thread
From: Dmitry A. Kazakov @ 2007-10-04 12:45 UTC (permalink / raw)


On Thu, 04 Oct 2007 13:33:10 +0200, Ludovic Brenta wrote:

> Mateusz Papiernik <mati@maticomp.net> writes:
> 
>> With strings I also have to work some way. As for now I'm using
>> Unbounded_Strings, because easy implementations of functions like
>> split (I'm parsing CSV file). Is it a good practice to use U_S, or
>> should I fall back to normal strings and fight with implementation?
> 
> I guess that depends on whether the input lines have a fixed length or
> not.  The main disadvantage of Unbounded_Strings are that they are
> much slower than regular Strings.

Others are lack of array interface, absence of slices, and inability to use
custom storage pools to allocate the body.

I never use Unbounded_String for parsing. The input line buffer can be
easily reused to accommodate varying strings. The right line boundary would
be not S'Last, but some variable <= S'Last. Alternatively a string slice
can be passed down to the parser. Something like that would be necessary to
do anyway to strip LF/CRs.

CSV formats are very easy to do with regular String, IMO much easier than
with Unbounded_String. You might take a look at

   http://www.dmitry-kazakov.de/ada/strings_edit.htm

No split or other forms of tokenizing is needed. Instead of that the fields
are scanned from left to right and the cursor position in the string) is
advanced to the position following the field. When it reaches the right
boundary, another line is read into the buffer, and the cursor is reset.
That's it.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: Nested declares, constant size arrays and clarity
  2007-10-04 12:45       ` Dmitry A. Kazakov
@ 2007-10-04 13:11         ` Mateusz Papiernik
  2007-10-05  5:00           ` Jeffrey R. Carter
  2007-10-05  4:58         ` Jeffrey R. Carter
  1 sibling, 1 reply; 15+ messages in thread
From: Mateusz Papiernik @ 2007-10-04 13:11 UTC (permalink / raw)


Dmitry A. Kazakov pisze:
> I never use Unbounded_String for parsing. The input line buffer can be
> easily reused to accommodate varying strings. The right line boundary would
> be not S'Last, but some variable <= S'Last. Alternatively a string slice
> can be passed down to the parser. Something like that would be necessary to
> do anyway to strip LF/CRs.

Thanks. I will play around with traditional strings to make my own 
simple CSV parser - a good lesson it will be :)

> CSV formats are very easy to do with regular String, IMO much easier than
> with Unbounded_String. You might take a look at
> 
>    http://www.dmitry-kazakov.de/ada/strings_edit.htm

Thanks. I may, in fact, not, due to regulations. I'm playing with ADA 
for my academic course. We've formally just started, but I'm "thinking 
ahead" and doing all future the tasks at home to learn better. We must 
not use external ADA libraries. That's why I'm playing with my own 
parser, which somehow felt right with Unbounded Strings.

The string format is known, its always three columns - but of unknown 
width (in some boundaries, of course, let's say - 100).

I used Unbounded Strings to slice my input by semicolon sign once, then 
slice the rest, and then slice the rest of the rest. I guess I can do 
the same with a simple loop over traditional string to check semicolon 
positions and then simply slice the string with known left/right 
boundaries. Back to code then... :)


Best wishes,
-- 
Mateusz Papiernik, Maticomp Webdesign
Projektowanie i realizacja witryn WWW
mati@maticomp.net, http://www.maticomp.net
"One man can make a difference" - Wilton Knight



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

* Re: Nested declares, constant size arrays and clarity
  2007-10-04  8:51   ` Mateusz Papiernik
@ 2007-10-05  2:45     ` Steve Whalen
  0 siblings, 0 replies; 15+ messages in thread
From: Steve Whalen @ 2007-10-05  2:45 UTC (permalink / raw)


On Oct 4, 1:51 am, Mateusz Papiernik <m...@maticomp.net> wrote:

> Ada in many places
> force me to change my mind schemes :)
>
> Regards,
> --
> Mateusz Papiernik, Maticomp Webdesign

Welcome to Ada.  In some ways Ada is like the "opposite" of what he
meant when Professor Edsger Dijkstra's said: "the teaching of BASIC
should be rated as a criminal offence: it mutilates the mind beyond
recovery.".

Ada makes you think harder about structure and representation, and
helps lead to a better understanding of the problem and the program.
Over time, I think that is a good thing and leads to better programs
and better programmers.

I haven't seen anyone mention the Ada 95 Quality and Style Guide ...
it has many good ideas to help you start to write better Ada
programs.  Not everyone agrees with every "guide" in it, but all the
sections are good things to think about, and will help make you a
better Ada programmer.

There is an online version of the style guide at:

http://www.adaic.org/docs/95style/html/contents.html

and other formats of the same guide are in the directory:

http://adaic.org/docs/95style/

Steve




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

* Re: Nested declares, constant size arrays and clarity
  2007-10-04  8:49   ` Mateusz Papiernik
  2007-10-04 11:33     ` Ludovic Brenta
@ 2007-10-05  4:54     ` Jeffrey R. Carter
  1 sibling, 0 replies; 15+ messages in thread
From: Jeffrey R. Carter @ 2007-10-05  4:54 UTC (permalink / raw)


Mateusz Papiernik wrote:
> 
> With strings I also have to work some way. As for now I'm using 
> Unbounded_Strings, because easy implementations of functions like split 
> (I'm parsing CSV file). Is it a good practice to use U_S, or should I 
> fall back to normal strings and fight with implementation?

When you understand String, you'll find the implementation works for 
you, and you don't have to fight it. There are very few places where you 
really need a variable-length string type; most things can be done with 
String and block statements.

But there's nothing wrong with using Unbounded_String, and sometimes a 
variable-length string type can result in shorter or more readable code.

-- 
Jeff Carter
"Why, the Mayflower was full of Fireflies, and a few
horseflies, too. The Fireflies were on the upper deck,
and the horseflies were on the Fireflies."
Duck Soup
95



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

* Re: Nested declares, constant size arrays and clarity
  2007-10-04 12:45       ` Dmitry A. Kazakov
  2007-10-04 13:11         ` Mateusz Papiernik
@ 2007-10-05  4:58         ` Jeffrey R. Carter
  2007-10-05  7:38           ` Dmitry A. Kazakov
  1 sibling, 1 reply; 15+ messages in thread
From: Jeffrey R. Carter @ 2007-10-05  4:58 UTC (permalink / raw)


Dmitry A. Kazakov wrote:
> 
> I never use Unbounded_String for parsing. The input line buffer can be
> easily reused to accommodate varying strings. The right line boundary would
> be not S'Last, but some variable <= S'Last. Alternatively a string slice
> can be passed down to the parser. Something like that would be necessary to
> do anyway to strip LF/CRs.

Except when you have a line longer than S'Length. Better is to use 
function Ada.Text_IO.Get_Line (Ada 07, but easily implemented in earlier 
versions; my seminal paper on the subject was in Ada 83 and published in 
  1989) and always have a string of exactly the right length.

-- 
Jeff Carter
"Why, the Mayflower was full of Fireflies, and a few
horseflies, too. The Fireflies were on the upper deck,
and the horseflies were on the Fireflies."
Duck Soup
95



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

* Re: Nested declares, constant size arrays and clarity
  2007-10-04 13:11         ` Mateusz Papiernik
@ 2007-10-05  5:00           ` Jeffrey R. Carter
  0 siblings, 0 replies; 15+ messages in thread
From: Jeffrey R. Carter @ 2007-10-05  5:00 UTC (permalink / raw)


Mateusz Papiernik wrote:
> 
> The string format is known, its always three columns - but of unknown 
> width (in some boundaries, of course, let's say - 100).

No boundaries can easily be accomodated. See function 
Ada.Text_IO.Get_Line if you're using Ada 07, or you should be able to 
easily write your own if you're not.

> I used Unbounded Strings to slice my input by semicolon sign once, then 
> slice the rest, and then slice the rest of the rest. I guess I can do 
> the same with a simple loop over traditional string to check semicolon 
> positions and then simply slice the string with known left/right 
> boundaries. Back to code then... :)

Rather than looping to find positions, you might want to look at 
Ada.Strings.Fixed.Index, which does it for you ...

-- 
Jeff Carter
"Why, the Mayflower was full of Fireflies, and a few
horseflies, too. The Fireflies were on the upper deck,
and the horseflies were on the Fireflies."
Duck Soup
95



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

* Re: Nested declares, constant size arrays and clarity
  2007-10-05  4:58         ` Jeffrey R. Carter
@ 2007-10-05  7:38           ` Dmitry A. Kazakov
  2007-10-05 17:08             ` Jeffrey R. Carter
  0 siblings, 1 reply; 15+ messages in thread
From: Dmitry A. Kazakov @ 2007-10-05  7:38 UTC (permalink / raw)


On Fri, 05 Oct 2007 04:58:06 GMT, Jeffrey R. Carter wrote:

> Except when you have a line longer than S'Length.

In that case I reallocate the buffer. However to set a hard upper margin
would be a good idea, in order to prevent somebody from feeding the parser
with DVD images.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: Nested declares, constant size arrays and clarity
  2007-10-05  7:38           ` Dmitry A. Kazakov
@ 2007-10-05 17:08             ` Jeffrey R. Carter
  0 siblings, 0 replies; 15+ messages in thread
From: Jeffrey R. Carter @ 2007-10-05 17:08 UTC (permalink / raw)


Dmitry A. Kazakov wrote:
> 
> In that case I reallocate the buffer. However to set a hard upper margin
> would be a good idea, in order to prevent somebody from feeding the parser
> with DVD images.

If you're offering parsing over a public network, that's probably a good 
idea, but otherwise I think the user who does so gets what he deserves. 
Besides, I'd expect a DVD image to contain some things that would be 
interpreted as line terminators ...

-- 
Jeff Carter
"People called Romanes, they go the house?"
Monty Python's Life of Brian
79



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

end of thread, other threads:[~2007-10-05 17:08 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-10-03 22:58 Nested declares, constant size arrays and clarity Mateusz Papiernik
2007-10-03 23:49 ` Ludovic Brenta
2007-10-04  8:49   ` Mateusz Papiernik
2007-10-04 11:33     ` Ludovic Brenta
2007-10-04 12:45       ` Dmitry A. Kazakov
2007-10-04 13:11         ` Mateusz Papiernik
2007-10-05  5:00           ` Jeffrey R. Carter
2007-10-05  4:58         ` Jeffrey R. Carter
2007-10-05  7:38           ` Dmitry A. Kazakov
2007-10-05 17:08             ` Jeffrey R. Carter
2007-10-05  4:54     ` Jeffrey R. Carter
2007-10-04  0:20 ` Jeffrey R. Carter
2007-10-04  8:51   ` Mateusz Papiernik
2007-10-05  2:45     ` Steve Whalen
2007-10-04  0:23 ` anon

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