comp.lang.ada
 help / color / mirror / Atom feed
* A question about private types
@ 2014-06-12 15:03 Mike H
  2014-06-12 15:21 ` Adam Beneschan
  2014-06-12 17:20 ` Jeffrey Carter
  0 siblings, 2 replies; 4+ messages in thread
From: Mike H @ 2014-06-12 15:03 UTC (permalink / 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 ;-)


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

end of thread, other threads:[~2014-06-12 18:11 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-06-12 15:03 A question about private types Mike H
2014-06-12 15:21 ` Adam Beneschan
2014-06-12 17:20 ` Jeffrey Carter
2014-06-12 18:11   ` Mike H

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