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!cyclone1.gnilink.net!gnilink.net!wn14feed!worldnet.att.net!207.217.77.102!elnk-nf2-pas!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!newsread1.news.pas.earthlink.net.POSTED!a6202946!not-for-mail From: Jeffrey Carter Organization: jrcarter commercial-at acm [period | full stop] org User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.4) Gecko/20030624 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: [newbie] simple(?) data structures References: <2j1e30Fsrg8vU1@uni-berlin.de> <878yesgvw5.fsf@insalien.org> <2j1v28Fs989jU1@uni-berlin.de> In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Date: Sun, 13 Jun 2004 18:24:07 GMT NNTP-Posting-Host: 63.184.104.160 X-Complaints-To: abuse@earthlink.net X-Trace: newsread1.news.pas.earthlink.net 1087151047 63.184.104.160 (Sun, 13 Jun 2004 11:24:07 PDT) NNTP-Posting-Date: Sun, 13 Jun 2004 11:24:07 PDT Xref: g2news1.google.com comp.lang.ada:1450 Date: 2004-06-13T18:24:07+00:00 List-Id: Stephen Leake wrote: > For a run-time determined size, the answer in Ada is the same as in > C++; you need to allocate the board object after reading in the size. > However, there are two ways to do this in Ada. The most powerful is > with access types: As in C++, access types require manual memory management, a significant source of errors. Unlike C++, you don't need to use access types to achieve this in Ada. > The other way is to declare the board in a local block: This is another way, but not very flexible. Go boards are typically 9x9, 13x13, or 19x19. Unless you're interested in playing around with unusual-size boards, you can achieve what you want by making all boards 19x19, and simply setting a field that indicates the actual size of the board in use. Such a board can be passed around, and its size changed dynamically. 19x19 is 361 bytes, which is unlikely to be considered "too big" on any system where the program is likely to run. Even if space is a consideration, each position only requires 2 bits, so that could be reduced to 91 bytes by packing. Finally, a discriminated record with a default discriminant can (and should) be used to implement such a representation. Objects will be large enough to hold the maximum size, but the compiler will enforce that only the part of the board required may be accessed, and that the board be square. Expanding on my earlier example: package Go is type Intersection_Content is (Empty, Black, White); -- stones are not empty; intersections are type Board_Size is (Nine, Thirteen, Nineteen); type Intersection_Num is range 1 .. 19; type Board is limited private; Invalid_Board : exception; Invalid_Intersection : exception; procedure Create (Go_Board : out Board; Size : in Board_Size); function Size (Go_Board : Board) return Board_Size; -- Raises Invalid_Board if Go_Board has not been created. function Contents (Go_Board : Board; Row : Intersection_Num; Column : Intersection_Num) return Intersection_Content; -- Raises Invalid_Board if Go_Board has not been created. -- Raises Invalid_Intersection if Row or Column are not valid for -- Size (Go_Board). -- Possibly other operations, such as Set. private -- Go type Board_Array is array (Intersection_Num range <>, Intersection_Num range <>) of Intersection_Content; -- 1st dimension is Row; 2nd, Column type Board (Size : Intersection_Num := 1) is -- 1 => not created Content : Board_Array (1 .. Size, 1 .. Size) := (1 .. Size => (1 .. Size => Empty) ); end record; end Go; Create could then be procedure Create (Go_Board : out Board; Size : in Board_Size) is -- null; begin -- Create case Size is when Nine => Go_Board := (Size => 9, Content => (1 .. 9 => (1 .. 9 => Empty) ) ); when Thirteen => Go_Board := (Size => 13, Content => (1 .. 13 => (1 .. 13 => Empty) ) ); when Nineteen => Go_Board := (Size => 19, Content => (1 .. 19 => (1 .. 19 => Empty) ) ); end case; end Create; This could be relaxed to make Board non-limited, if assignment of boards is required, and to allow the board size to be numeric rather than an enumeration, if unusual-size boards are wanted. -- Jeff Carter "You tiny-brained wipers of other people's bottoms!" Monty Python & the Holy Grail 18