簡體   English   中英

在 SPARK Ada 中創建泛型約束數組類型

[英]Create generic constrained array type in SPARK Ada

我想制定一個程序來接受通用約束 arrays 即 ecgReadings 和 eegReadings:

Types declarations:
   subtype ecgReadingsSize is Natural range 1..3;
   subtype eegReadingsSize is Natural range 1..10;
   subtype eegReading is Natural range 0..1; -- eegRReading is 0 or 1
   subtype ecgReading is Natural range 2..600; -- Max heart rate 220

   
   type ecgReadings is array (ecgReadingsSize) of ecgReading;
   type eegReadings is array (eegReadingsSize) of eegReading;
   type eegPartialSums is array (eegReadingsSize) of Natural;

嘗試制定通用程序:

package iocontroller is

   
   generic 
      type ItemType is private;
      type Index_Type is (<>);  -- Any discrete type
      type Array_Type is array (Index_Type range <>) of ItemType;
  procedure startIO(inFileName : String; outFileName : String; List: 
  Array_Type);

測試通用產品

procedure Main is --change name

type MyArray is array (Natural range <>) of Natural;

procedure ecgIO is new startIO(
        ItemType => Natural,
        Index_Type => Natural,
        Array_Type => MyArray
    );

我認為您只需要約束Start_IO的泛型類型參數Array_Type (參見下面的示例)。

注意雖然這不是強制性的,但在這種情況下,您可能希望聲明類型而不是子類型,以防止從ECG_ReadingEEG_Reading的意外(隱式)轉換,即聲明

type ECG_Reading_Index is range 1 .. 3;
type ECG_Reading       is range 2 .. 600;

代替

subtype ECG_Reading_Index is Natural range 1 .. 3;
subtype ECG_Reading       is Natural range 2 .. 600;

EEG_Reading_IndexEEG_Reading類似。

但除此之外:

主文件

with IO_Controller;

procedure Main is
   
   subtype ECG_Reading_Index is Natural range 1 .. 3;
   subtype ECG_Reading       is Natural range 2 .. 600;
   type    ECG_Readings      is array (ECG_Reading_Index) of ECG_Reading;
   
   procedure Start_ECG_IO is new IO_Controller.Start_IO
     (Item_Type  => ECG_Reading,
      Index_Type => ECG_Reading_Index,
      Array_Type => ECG_Readings);   
   
   
   subtype EEG_Reading_Index is Natural range 1 .. 10;
   subtype EEG_Reading       is Natural range 0 .. 1;
   type    EEG_Readings      is array (EEG_Reading_Index) of EEG_Reading;
   
   procedure Start_EEG_IO is new IO_Controller.Start_IO
     (Item_Type  => EEG_Reading,
      Index_Type => EEG_Reading_Index,
      Array_Type => EEG_Readings);
   

   ECG : ECG_Readings := (others => <>);
   EEG : EEG_Readings := (others => <>);
   
begin
   Start_ECG_IO ("ecg_in", "ecg_out", ECG);
   Start_EEG_IO ("eeg_in", "eeg_out", EEG);
end Main;

io_controller.ads

package IO_Controller is   
   
   generic 
      type Item_Type is private;
      type Index_Type is (<>);
      type Array_Type is array (Index_Type) of Item_Type;   --  remove "range <>"
   procedure Start_IO
     (FileName_In  : String; 
      Filename_Out : String;
      List         : Array_Type);
 
end IO_Controller;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM