comp.lang.ada
 help / color / mirror / Atom feed
* multidimensional sort
@ 2020-03-17 12:31 Gilbert Gosseyn
  2020-03-17 12:56 ` Petter Fryklund
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Gilbert Gosseyn @ 2020-03-17 12:31 UTC (permalink / raw)


I want to sort an n-dim array by ascending dimensions and ascending elements as keys, in Ada. For example:
consider an array with n = 5 like:

((5, 8, 21, 37, 46), (5, 7, 12, 19, 26),
 (10, 12, 19, 33, 44), (7, 8, 34, 36, 38), (5, 11, 12, 27, 32), (10, 
  16, 30, 41, 45), (10, 13, 19, 40, 45), (10, 11, 18, 32, 41), (11, 
  28, 34, 37, 38))

which shall become

((5, 7, 12, 19, 26), (5, 8, 21, 37, 46), (5, 11, 12, 27, 32), (7, 8, 
  34, 36, 38), (10, 11, 18, 32, 41), (10, 12, 19, 33, 44), (10, 13, 
  19, 40, 45), (10, 16, 30, 41, 45), (11, 28, 34, 37, 38))

How to store, how to sort, and how to deallocate the original?

Thank you for your advice.

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

* Re: multidimensional sort
  2020-03-17 12:31 multidimensional sort Gilbert Gosseyn
@ 2020-03-17 12:56 ` Petter Fryklund
  2020-03-17 17:24   ` Simon Wright
  2020-03-17 16:08 ` Jeffrey R. Carter
  2020-03-17 17:10 ` Optikos
  2 siblings, 1 reply; 6+ messages in thread
From: Petter Fryklund @ 2020-03-17 12:56 UTC (permalink / raw)


Den tisdag 17 mars 2020 kl. 13:31:43 UTC+1 skrev Gilbert Gosseyn:
> I want to sort an n-dim array by ascending dimensions and ascending elements as keys, in Ada. For example:
> consider an array with n = 5 like:
> 
> ((5, 8, 21, 37, 46), (5, 7, 12, 19, 26),
>  (10, 12, 19, 33, 44), (7, 8, 34, 36, 38), (5, 11, 12, 27, 32), (10, 
>   16, 30, 41, 45), (10, 13, 19, 40, 45), (10, 11, 18, 32, 41), (11, 
>   28, 34, 37, 38))
> 
> which shall become
> 
> ((5, 7, 12, 19, 26), (5, 8, 21, 37, 46), (5, 11, 12, 27, 32), (7, 8, 
>   34, 36, 38), (10, 11, 18, 32, 41), (10, 12, 19, 33, 44), (10, 13, 
>   19, 40, 45), (10, 16, 30, 41, 45), (11, 28, 34, 37, 38))
> 
> How to store, how to sort, and how to deallocate the original?
> 
> Thank you for your advice.

This looks very much like homework. Have a look at Ada.Containers.Indefinate_Vectors and the generic procedure Generic_Sorting.


Regards,
Petter 

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

* Re: multidimensional sort
  2020-03-17 12:31 multidimensional sort Gilbert Gosseyn
  2020-03-17 12:56 ` Petter Fryklund
@ 2020-03-17 16:08 ` Jeffrey R. Carter
  2020-03-17 17:10 ` Optikos
  2 siblings, 0 replies; 6+ messages in thread
From: Jeffrey R. Carter @ 2020-03-17 16:08 UTC (permalink / raw)


On 3/17/20 1:31 PM, Gilbert Gosseyn wrote:
> I want to sort an n-dim array by ascending dimensions and ascending elements as keys, in Ada. For example:
> consider an array with n = 5 like:
> 
> ((5, 8, 21, 37, 46), (5, 7, 12, 19, 26),
>   (10, 12, 19, 33, 44), (7, 8, 34, 36, 38), (5, 11, 12, 27, 32), (10,
>    16, 30, 41, 45), (10, 13, 19, 40, 45), (10, 11, 18, 32, 41), (11,
>    28, 34, 37, 38))

This appears to be a 2-D array, not 5, or perhaps a 1-D array of 1-D arrays with 
5 components, so I'm not sure what you're asking in general. What would it look 
like for n = 4?

-- 
Jeff Carter
"Gentlemen, you can't fight in here. This is the War Room!"
Dr. Strangelove
30

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

* multidimensional sort
  2020-03-17 12:31 multidimensional sort Gilbert Gosseyn
  2020-03-17 12:56 ` Petter Fryklund
  2020-03-17 16:08 ` Jeffrey R. Carter
@ 2020-03-17 17:10 ` Optikos
  2 siblings, 0 replies; 6+ messages in thread
From: Optikos @ 2020-03-17 17:10 UTC (permalink / raw)


This StackOverflow answer should ba significant step to solving your problem:

https://stackoverflow.com/questions/17548979/defining-multiple-elements-of-a-multi-dimensional-array

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

* Re: multidimensional sort
  2020-03-17 12:56 ` Petter Fryklund
@ 2020-03-17 17:24   ` Simon Wright
  2020-03-17 20:16     ` Simon Wright
  0 siblings, 1 reply; 6+ messages in thread
From: Simon Wright @ 2020-03-17 17:24 UTC (permalink / raw)


Petter Fryklund <petter.fryklund@atero.se> writes:

> Den tisdag 17 mars 2020 kl. 13:31:43 UTC+1 skrev Gilbert Gosseyn:
>> I want to sort an n-dim array by ascending dimensions and ascending
>> elements as keys, in Ada. For example:
>> consider an array with n = 5 like:
>> 
>> ((5, 8, 21, 37, 46), (5, 7, 12, 19, 26),
>>  (10, 12, 19, 33, 44), (7, 8, 34, 36, 38), (5, 11, 12, 27, 32), (10, 
>>   16, 30, 41, 45), (10, 13, 19, 40, 45), (10, 11, 18, 32, 41), (11, 
>>   28, 34, 37, 38))
>> 
>> which shall become
>> 
>> ((5, 7, 12, 19, 26), (5, 8, 21, 37, 46), (5, 11, 12, 27, 32), (7, 8, 
>>   34, 36, 38), (10, 11, 18, 32, 41), (10, 12, 19, 33, 44), (10, 13, 
>>   19, 40, 45), (10, 16, 30, 41, 45), (11, 28, 34, 37, 38))
>> 
>> How to store, how to sort, and how to deallocate the original?
>> 
>> Thank you for your advice.
>
> This looks very much like homework. Have a look at
> Ada.Containers.Indefinate_Vectors and the generic procedure
> Generic_Sorting.

Unless OP has some unstated requirements, I'd think that just
Ada.Containers.Generic_Array_Sort would do the trick:

   type Arr is array (1 .. 5) of Positive;
   type Arrays is array (Positive range <>) of Arr;
   function "<" (L, R : Arr) return Boolean;

   procedure Sort is new Ada.Containers.Generic_Array_Sort
     (Index_Type   => Positive,
      Element_Type => Arr,
      Array_Type   => Arrays);

The trick is to get "<" right!

OP, what's wrong with

   Data : Arrays :=
     ((5, 8, 21, 37, 46),
      (5, 7, 12, 19, 26),
      ...
      (11, 28, 34, 37, 38));

which doesn't need deallocation at all.

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

* Re: multidimensional sort
  2020-03-17 17:24   ` Simon Wright
@ 2020-03-17 20:16     ` Simon Wright
  0 siblings, 0 replies; 6+ messages in thread
From: Simon Wright @ 2020-03-17 20:16 UTC (permalink / raw)


Simon Wright <simon@pushface.org> writes:

> Unless OP has some unstated requirements, I'd think that just
> Ada.Containers.Generic_Array_Sort would do the trick:

I should have said, ARM A.18.26(2),
http://www.ada-auth.org/standards/rm12_w_tc1/html/RM-A-18-26.html#p2

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

end of thread, other threads:[~2020-03-17 20:16 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-17 12:31 multidimensional sort Gilbert Gosseyn
2020-03-17 12:56 ` Petter Fryklund
2020-03-17 17:24   ` Simon Wright
2020-03-17 20:16     ` Simon Wright
2020-03-17 16:08 ` Jeffrey R. Carter
2020-03-17 17:10 ` Optikos

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