comp.lang.ada
 help / color / mirror / Atom feed
From: nickroberts@ukf.net (Nick Roberts)
Subject: Re: Two Dimensional Array
Date: Sat, 23 Feb 2002 13:36:05 GMT
Date: 2002-02-23T13:36:05+00:00	[thread overview]
Message-ID: <3c778671.310328464@news.cis.dfn.de> (raw)
In-Reply-To: 3c6e2709.601229@news.demon.co.uk

Brian, I can give you an idea that you may or may not find useful.

If you have an array that you would like to index in some arbitrary order,
e.g.:

Data Array
	Index: 23 14  3 10 27 13 12 20 21  2 19 ...
	Value:  0  0  0  0  0  1  1  1  1  1  0 ...

The 'classic' way to do this is with a 'mapping' array, e.g.:

Mapping Array
	Index: 23 14  3 10 27 13 12 20 21  2 19 ...
	Value:  1  2  3  4  5  6  7  8  9 10 11 ...

This array 'maps' 23 -> 1, 14 -> 2, 3 -> 3, 10 -> 4, and so on.

In Ada, the mapping array could be defined as follows:

   type Index_Type is range 1..50;

   Map: constant array (Index_Type) of Index_Type :=
           ( 23 =>  1, 14 =>  2,  3 =>  3, 10 =>  4, 27 =>  5,
             13 =>  6, 12 =>  6  20 =>  8, ........... );

You could then declare an array convenient for declaring the data:

   Data: constant array (Index_Type) of Bit :=
            (  1.. 5 => 0,  6..10 => 1, 11..15 => 0, 16..20 => 1,
              21..25 => 0, 26..30 => 0, 31..35 => 1, 36..40 => 1,
              41..45 => 1, 46..50 => 0 );

You could then construct the actual array you need using the map:

	Work: array (Index_Type) of Bit;

   procedure Initialize_Work is
	begin
		for i in Work'Range loop
			Work(i) := Data(Map(i));
      end loop;
   end;

The 'tricky' bit here is how the Map array is used to generate an index for
indexing the Data array [Data(Map(i))]. It is important that you study this
construct until you understand how it works.

After Initialize_Work is called, the array Work will contain the bits in
the places they really should be.

Hope this helps.

You might be interested that this particular kind of map is called a
'bijection'. A bijection is a mapping that is both an 'injection' and a
'surjection'. In simple terms, an injection is a mapping which never maps
to the same thing twice, and a surjection is a mapping that is complete.

In order to help ensure that you have not made a mistake, you may wish to
write procedures that check the Map array for being an injection and a
surjection. I'll leave how to code these tests as an exercise!

-- 
Nick Roberts



      reply	other threads:[~2002-02-23 13:36 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-02-15 12:30 Two Dimensional Array Brian A Crawford
2002-02-15 17:28 ` Jeffrey Carter
2002-02-15 18:15 ` Stephen Leake
2002-02-15 22:50   ` Brian A Crawford
2002-02-16  2:05     ` Mike Silva
2002-02-16  9:38       ` Brian A Crawford
2002-02-23 13:36         ` Nick Roberts [this message]
replies disabled

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