comp.lang.ada
 help / color / mirror / Atom feed
From: Mike H <postmaster@ada-augusta.demon.co.uk>
Subject: A question about private types
Date: Thu, 12 Jun 2014 16:03:05 +0100
Date: 2014-06-12T16:03:05+01:00	[thread overview]
Message-ID: <$ql0lCCpEcmTFwCt@ada-augusta.demon.co.uk> (raw)

... Or, at lest, I expect the answer to be about private types.

My instinct is that the package below is vulnerable to erroneous use
because the "Address" component of "Cell_type" can be changed. I suspect
that this vulnerability could be removed if "Grid_index_type" is made
read only. I have attempted to make it a private type but it then
becomes non-discrete and can no longer be used as a parameter in
functions such as "Line_of".

Help please! And, preferably, in a form where the new source code is
sufficiently transparent to be understood when doing a "walk-through"
with a C++ speaker.

-- ==================================================
-- This package attempts to provide type-safe elementary procedures for
-- use in a problem domain in which a linear array of 81 records is to
be
-- mapped to, or from, 9 lines of 9, 9 columns of 9 or 9 blocks of 3x3
--
-----------------------------------------------------------------------
package Sudoku_types is

   type Status_type is (Predefined, Solved, Not_solved);
   subtype Cell_value is Character range '0'..'9';
   Space : constant Cell_value := '0';
   subtype Candidate is Cell_Value range '1'..'9';
   type Candidature is Array (Candidate) of Boolean;
   subtype Count_range is Integer range 0..9;

   type Cell_Data is
      record
         Status           : Status_type := Not_solved;
         Content         : Cell_value    := Space;
         Candidates    : Count_range := 9;
         Is_candidate  : Candidature  := (others => true);
      end record;

   type Grid_index_type is range 1 .. 81;

   -- to simplify mapping in either direction each cell contains its own
   -- (home) address within the grid
   type Cell_type is
      record
         Address : Grid_index_type;  -- ========== vulnerable?
         Data      : Cell_data;
      end record;
   type Grid_type is array (Grid_index_type) of Cell_type;

   -- Seed each cell with its own "home" address and set the data to
   -- default values
   procedure Initialise (Grid : in out Grid_type);

   -- ===================================================
   -- A line, column or block of the grid may be mapped onto a vector
   type Vector_index_range is range 0..9;
   subtype Vector_index  is Vector_index_range range 1..9;
   type Vector_type is array (Vector_index) of Cell_type;

   -- ===================================================
   -- Lines
   type Line_number is range 1..9;
   function Line_of (G : in Grid_index_type) return Line_number;
   function Line (L : in Line_number;
                  Grid : in Grid_type) return Vector_type;

   -- ===================================================
   -- Columns
   type Column_number is range 1..9;
   function Column_of (G : in Grid_index_type) return Column_number;
   --  etc., etc. ..........

   end Sudoku_types;

-- 
Knowledge is knowing a tomato is a fruit
Wisdom in knowing not to put it in the fruit salad.
Mike ;-)


             reply	other threads:[~2014-06-12 15:03 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-12 15:03 Mike H [this message]
2014-06-12 15:21 ` A question about private types Adam Beneschan
2014-06-12 17:20 ` Jeffrey Carter
2014-06-12 18:11   ` Mike H
replies disabled

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