* Question Exception with Ada.Containers.Generic_Constrained_Array_Sort
@ 2012-08-13 20:05 awdorrin
2012-08-13 20:44 ` awdorrin
2012-08-14 12:39 ` Marc C
0 siblings, 2 replies; 7+ messages in thread
From: awdorrin @ 2012-08-13 20:05 UTC (permalink / raw)
I had this code working, but today I started getting 'length check failed' exceptions.
I'm trying to implement a Generic_Constrained_Array_Sort to sort a slice of a constrained array.
Now I'm not sure if it is possible to pass a slice of the array into the constrained generic sort procedure.
Its possible I broke something somewhere else in the code that was related to this, but I haven't been able to track it down yet.
Here is what I'm trying to do:
procedure DXSort is new Ada.Containers.Generic_Constrained_Array_Sort(
Index_Type => Device_Index_Type,
Element_Type => Device_Object_Type,
Array_Type => Device_Array_Type,
"<" => DevName_Less_Than);
With the following declarations:
type Node_Rec_Type is private;
type Node_Ptr_Type is access Node_Rec_Type;
subtype Device_Object_Type is Node_Ptr_Type;
subtype Device_Index_Type is Int32 range 1 .. 128;
type Device_Array_Type is array(Device_Index_Type) of Device_Object_Type;
The comparison function is:
function DevName_Less_Than( L,R : Device_Object_Type) return Boolean is
begin
if GetDeviceName(L) < GetDeviceName(R)
then
return TRUE;
else
return FALSE;
end if;
end DevName_Less_Than;
I make the call to the DXSort procedure with the following call:
DXSort( DevList.DeviceList(1..DevList.Num_Devices) );
Num_Devices is always less than 128.
DevList is a record defined as:
type DevListType is record
Num_Devices : Int32;
DeviceList : Device_Array_Type;
end record;
I thought that the issue might have to do with having 'Num_Devices' of type 'Int32' in the record, but defining the 'Device_Index_Type' subtype, but experimenting with changing the type still left me with 'length check failed'.
Any suggestions would be greatly appreciated! :)
-Al
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Question Exception with Ada.Containers.Generic_Constrained_Array_Sort
2012-08-13 20:05 Question Exception with Ada.Containers.Generic_Constrained_Array_Sort awdorrin
@ 2012-08-13 20:44 ` awdorrin
2012-08-13 21:25 ` Adam Beneschan
2012-08-14 12:39 ` Marc C
1 sibling, 1 reply; 7+ messages in thread
From: awdorrin @ 2012-08-13 20:44 UTC (permalink / raw)
I managed to find a way to get this to work, but I am really not sure I'm doing this the right way. :)
First, I changed from using Generic_Constrained_Array_Sort to the unconstrained version.
Second, I changed my declarations like this:
type Device_Array_Type is array( Device_Index_Type range <> ) of Device_Object_Type; // make this unconstrained
type DeviceListType is record
Num_Devices : Device_Index_Type;
DeviceList : Device_Array_Type(Device_Index_Type); // limit range 1..128
end record;
Code appears to do what I want it to, but I'm really not sure this is the right solution.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Question Exception with Ada.Containers.Generic_Constrained_Array_Sort
2012-08-13 20:44 ` awdorrin
@ 2012-08-13 21:25 ` Adam Beneschan
2012-08-14 12:22 ` awdorrin
0 siblings, 1 reply; 7+ messages in thread
From: Adam Beneschan @ 2012-08-13 21:25 UTC (permalink / raw)
On Monday, August 13, 2012 1:44:19 PM UTC-7, awdorrin wrote:
> I managed to find a way to get this to work, but I am really not sure I'm doing this the right way. :)
>
>
>
> First, I changed from using Generic_Constrained_Array_Sort to the unconstrained version.
>
>
>
> Second, I changed my declarations like this:
>
>
>
> type Device_Array_Type is array( Device_Index_Type range <> ) of Device_Object_Type; // make this unconstrained
>
>
>
> type DeviceListType is record
>
> Num_Devices : Device_Index_Type;
>
> DeviceList : Device_Array_Type(Device_Index_Type); // limit range 1..128
>
> end record;
>
>
>
> Code appears to do what I want it to, but I'm really not sure this is the right solution.
Well, Generic_Constrained_Array_Sort is declared like this:
generic
type Index_Type is (<>);
type Element_Type is private;
type Array_Type is array (Index_Type) of Element_Type;
with function "<" (Left, Right : Element_Type)
return Boolean is <>;
procedure Ada.Containers.Generic_Constrained_Array_Sort
(Container : in out Array_Type);
In your first example, the actual for Array_Type is Device_Array_Type, which is defined as an array with bounds 1..128. So the generic instantiation declares a function DXSort which takes an array parameter of type Device_Array_Type, a constrained array with bounds 1..128. You're passing it a parameter with fewer elements than that, so you'll get a Constraint_Error when you try to pass the parameter.
Since you don't know beforehand what size array you're passing to your sort, you need to create a sort function that takes an unconstrained array parameter. Which is what you did in your second attempt.
So, yes, you did find the right solution. But if you still want to have a "Device_Array_Type" that is constrained, you can do that with a subtype:
type Device_Array_Type_Unc is array (Device_Index_Type range <>)
of Device_Object_Type;
subtype Device_Array_Type is Device_Array_Type_Unc(Device_Index_Type);
Now you can still say
DeviceList: Device_Array_Type;
Just make sure you use the *unconstrained* version (Device_Array_Type_Unc) when instantiating Generic_Array_Sort.
-- Adam
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Question Exception with Ada.Containers.Generic_Constrained_Array_Sort
2012-08-13 21:25 ` Adam Beneschan
@ 2012-08-14 12:22 ` awdorrin
0 siblings, 0 replies; 7+ messages in thread
From: awdorrin @ 2012-08-14 12:22 UTC (permalink / raw)
Thanks for confirming! :)
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Question Exception with Ada.Containers.Generic_Constrained_Array_Sort
2012-08-13 20:05 Question Exception with Ada.Containers.Generic_Constrained_Array_Sort awdorrin
2012-08-13 20:44 ` awdorrin
@ 2012-08-14 12:39 ` Marc C
2012-08-15 7:11 ` Martin
1 sibling, 1 reply; 7+ messages in thread
From: Marc C @ 2012-08-14 12:39 UTC (permalink / raw)
On Monday, August 13, 2012 3:05:51 PM UTC-5, awdorrin wrote:
> The comparison function is:
>
> function DevName_Less_Than( L,R : Device_Object_Type) return Boolean is
> begin
> if GetDeviceName(L) < GetDeviceName(R)
> then
> return TRUE;
> else
> return FALSE;
> end if;
> end DevName_Less_Than;
Just a pet peeve of mine... :-)
function DevName_Less_Than( L,R : Device_Object_Type) return Boolean is
begin
return GetDeviceName(L) < GetDeviceName(R);
end DevName_Less_Than;
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Question Exception with Ada.Containers.Generic_Constrained_Array_Sort
2012-08-14 12:39 ` Marc C
@ 2012-08-15 7:11 ` Martin
2012-08-15 16:02 ` Georg Bauhaus
0 siblings, 1 reply; 7+ messages in thread
From: Martin @ 2012-08-15 7:11 UTC (permalink / raw)
On Tuesday, August 14, 2012 1:39:29 PM UTC+1, Marc C wrote:
> On Monday, August 13, 2012 3:05:51 PM UTC-5, awdorrin wrote:
>
>
>
> > The comparison function is:
>
> >
>
> > function DevName_Less_Than( L,R : Device_Object_Type) return Boolean is
>
> > begin
>
> > if GetDeviceName(L) < GetDeviceName(R)
>
> > then
>
> > return TRUE;
>
> > else
>
> > return FALSE;
>
> > end if;
>
> > end DevName_Less_Than;
>
>
>
> Just a pet peeve of mine... :-)
>
>
>
> function DevName_Less_Than( L,R : Device_Object_Type) return Boolean is
>
> begin
>
> return GetDeviceName(L) < GetDeviceName(R);
>
> end DevName_Less_Than;
+1
-- Martin
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Question Exception with Ada.Containers.Generic_Constrained_Array_Sort
2012-08-15 7:11 ` Martin
@ 2012-08-15 16:02 ` Georg Bauhaus
0 siblings, 0 replies; 7+ messages in thread
From: Georg Bauhaus @ 2012-08-15 16:02 UTC (permalink / raw)
On 15.08.12 09:11, Martin wrote:
> On Tuesday, August 14, 2012 1:39:29 PM UTC+1, Marc C wrote:
>> On Monday, August 13, 2012 3:05:51 PM UTC-5, awdorrin wrote:
>>
>>
>>
>>> The comparison function is:
>>
>>>
>>
>>> function DevName_Less_Than( L,R : Device_Object_Type) return Boolean is
>>
>>> begin
>>
>>> if GetDeviceName(L) < GetDeviceName(R)
>>
>>> then
>>
>>> return TRUE;
>>
>>> else
>>
>>> return FALSE;
>>
>>> end if;
>>
>>> end DevName_Less_Than;
>>
>>
>>
>> Just a pet peeve of mine... :-)
>>
>>
>>
>> function DevName_Less_Than( L,R : Device_Object_Type) return Boolean is
>>
>> begin
>>
>> return GetDeviceName(L) < GetDeviceName(R);
>>
>> end DevName_Less_Than;
>
> +1
Rather impractical for development use! OK, I cannot normally use
a debugger, which explains the habit.
function DevName_Less_Than( L,R : Device_Object_Type) return Boolean is
Ls_Name : Device_Object_Type renames GetDeviceName(L);
Rs_Name : Device_Object_Type renames GetDeviceName(R);
Result : Boolean;
begin
if Ls_Name < Rs_Name then
pragma Debug (D (623, "got < for device names!"));
Result := TRUE;
else
Result := FALSE;
end if;
pragma Debug (D (323, "L < R is " & Boolean'Image (Result)));
return Result;
end DevName_Less_Than;
Substitute "aspect oriented programming" for pragma Debug ;->
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2012-08-17 21:08 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-08-13 20:05 Question Exception with Ada.Containers.Generic_Constrained_Array_Sort awdorrin
2012-08-13 20:44 ` awdorrin
2012-08-13 21:25 ` Adam Beneschan
2012-08-14 12:22 ` awdorrin
2012-08-14 12:39 ` Marc C
2012-08-15 7:11 ` Martin
2012-08-15 16:02 ` Georg Bauhaus
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox