From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,aa7f494bf30adbc7 X-Google-Attributes: gid103376,public Path: g2news1.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!t-online.de!newsfeed1.ip.tiscali.net!tiscali!transit1.news.tiscali.nl!dreader2.news.tiscali.nl!not-for-mail Newsgroups: comp.lang.ada Subject: Re: [newbie] simple(?) data structures References: <2j1e30Fsrg8vU1@uni-berlin.de> <878yesgvw5.fsf@insalien.org> <2j1v28Fs989jU1@uni-berlin.de> From: Ludovic Brenta Date: Sun, 13 Jun 2004 13:56:21 +0200 Message-ID: <87zn77fz8a.fsf@insalien.org> User-Agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux) Cancel-Lock: sha1:8wDiBupE1pKbmOQ4YluXS+Cu3vI= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Organization: Tiscali bv NNTP-Posting-Date: 13 Jun 2004 13:55:41 CEST NNTP-Posting-Host: 83.134.237.205 X-Trace: 1087127741 dreader2.news.tiscali.nl 41751 83.134.237.205:33797 X-Complaints-To: abuse@tiscali.nl Xref: g2news1.google.com comp.lang.ada:1445 Date: 2004-06-13T13:55:41+02:00 List-Id: Marius Amado Alves writes: >> The thing that makes the definition of the data structure that >> complicated is that I want to choose the size of the Go_Board at >> runtime and I want to pass that Go_Board to other subprograms. Is >> that possible with packages? >> -- function New_Board(The_Size : Integer) return Go_Board is >> -- package The_Board is new Go_Board (Size => The_Size); >> -- begin >> -- return The_Board; >> -- end New_Board; > > Ah, packages as first class citizens... unfortunately not Ada :-( My generic package is elaborated at run time; this answers half of your question. But you cannot pass the package around, as Marius pointed out. To do this, you could declare a type in the generic package, and pass instances of that type around: generic Size : in Positive; package Go_Board is type Width is range 1 .. Size; type Height is range 1 .. Size; type Stone is (Empty, Black, White); type Board is private; function Get_Stone (B : in Board; X : in Width; Y : in Height) return Stone; procedure Set_Stone (B : in out Board; X : in Width; Y : in Height; S : in Stone); procedure Copy (From : in Board; To : out Board); -- copy boards private type Board is array (Width, Height) of Stone; end Go_Board; The potential problem with passing Boards around is that the subprograms accepting Boards as parameters would also have to be generic. Ada makes it possible to pass a package as a generic parameter. Moreover, you can specify that the package must be an instance of some other generic package: generic with package The_Board is new Go_Board; -- accept board of any one size, but not of all possible sizes procedure Fill_Empty (B : in out The_Board.Board); Then you would instanciate like this: with Go_Board; with Fill_Empty; procedure Foo is package Six_Board is new Go_Board (Size => 6); procedure Fill_Six_Board is new Fill_Board (The_Board => Six_Board); B : Six_Board.Board; begin Fill_Six_Board (B); end Foo; Of course, all these subprograms could be in the package Go_Board (like procedure Copy above), so you wouldn't have to instanciate them separately. Now, if you want one subprogram to accept a Board parameter of unknown size (in a more object-oriented way), you will need to give up static range checks, and replace them with dynamic ones: package Go_Board is type Stone is (Empty, Black, White); type Board is private; function New_Board (Size : in Positive) return Board; function Width (B : in Board) return Positive; function Height (B : in Board) return Positive; function Get_Stone (B : in Board; X, Y : in Positive) -- no static range check return Stone; procedure Set_Stone (B : in out Board; X, Y : in Positive; -- no static range check S : in Stone); private type Board_Array is array (Positive range <>, Positive range <>) of Stone; type Board is access Board_Array; end Go_Board; procedure Fill_Empty (B : in out Go_Board.Board) is use Go_Board; begin for X in 1 .. Width (B) loop for Y in 1 .. Height (B) loop Set_Stone (B, X, Y, Empty); end loop; end loop; Set_Stone (B, Height (B) + 1, 1, Empty); -- Constraint_Error at run time: dynamic check, not static end Fill_Empty; HTH -- Ludovic Brenta.