Top Banner
ADA ADA Data Structures Data Structures Subtype Subtype Enumeration type Enumeration type
22

ADA Data Structures

Jan 04, 2016

Download

Documents

berk-santana

ADA Data Structures. Subtype Enumeration type. SUBTYPE. Definition : A subset of the values associated with the original type. Example : SUBTYPE Natural IS Integer RANGE 0..Integer’Last; SUBTYPE Positive IS Integer RANGE 0..Integer’Last;. More Examples. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ADA Data Structures

ADAADAData StructuresData Structures

SubtypeSubtype

Enumeration typeEnumeration type

Page 2: ADA Data Structures

SUBTYPESUBTYPE

Definition :Definition :

A subset of the values associatedA subset of the values associated

with the original type.with the original type.

Example :Example :

SUBTYPE Natural IS Integer RANGE 0..Integer’Last;SUBTYPE Natural IS Integer RANGE 0..Integer’Last;

SUBTYPE Positive IS Integer RANGE SUBTYPE Positive IS Integer RANGE 0..Integer’Last;0..Integer’Last;

Page 3: ADA Data Structures

More ExamplesMore Examples

SUBTYPE SmallInt IS Integer RANGE -50. .50;SUBTYPE SmallInt IS Integer RANGE -50. .50;

SUBTYPE NonNegFloat IS Float RANGE 0.0 . . SUBTYPE NonNegFloat IS Float RANGE 0.0 . . Float’Last;Float’Last;

SUBTYPE CapLetter IS Character RANGE SUBTYPE CapLetter IS Character RANGE ‘A’. .’Z’;‘A’. .’Z’;

X, Y, Z : SmallInt;X, Y, Z : SmallInt;

NextChar : CapLetter;NextChar : CapLetter;

Hours_Worked : NonNegFloat;Hours_Worked : NonNegFloat;

Page 4: ADA Data Structures

Constraint ErrorConstraint Error

SUBTYPE SmallInt IS Integer RANGE -50. .50;SUBTYPE SmallInt IS Integer RANGE -50. .50;

X, Y, Z : SmallInt;X, Y, Z : SmallInt;

X := 26;X := 26;

Y := 28;Y := 28;

Z := X + Y;Z := X + Y;

Compilation Error ? Compilation Error ?

At Run Time, Error will be raised. Why ? At Run Time, Error will be raised. Why ?

Page 5: ADA Data Structures

Compatibility RulesCompatibility Rules for Types and Subtypesfor Types and Subtypes

Do not allow to mix the types of Do not allow to mix the types of operands operands

Examples:Examples:

v1 : Integer;v1 : Integer;

V2 : Float;V2 : Float;

V1+V2 leads to compilation errorV1+V2 leads to compilation error

Page 6: ADA Data Structures

More CompatibilityMore Compatibility

Example :Example :

SUBTYPE SmallInt IS Integer RANGE -SUBTYPE SmallInt IS Integer RANGE -50. .50;50. .50;

V1 : Integer;V1 : Integer;

V2 : SmallInt;V2 : SmallInt;

V1 + V2 is Compatible.V1 + V2 is Compatible.

Page 7: ADA Data Structures

More CompatibilityMore Compatibility

Two values are compatible if they Two values are compatible if they have have

the same type namethe same type name

or or

one value’s type is a subtype of one value’s type is a subtype of the other value’s type.the other value’s type.

Page 8: ADA Data Structures

Interesting ThingsInteresting Things

X : Integer;X : Integer;

Y : Natural;Y : Natural;

Think aboutThink about

X := Y;X := Y;

andand

Y := X;Y := X;

Page 9: ADA Data Structures

No Compilation ErrorNo Compilation Error

X := Y; is always valid.X := Y; is always valid.

But But

Y := X; is not always valid at execution Y := X; is not always valid at execution time.time.

When ?When ?

Page 10: ADA Data Structures

Ada Standard LibraryAda Standard Library

With Ada.Numerics;With Ada.Numerics;

ExampleExample

get the value of Pi asget the value of Pi as

Ada.Numerics.PiAda.Numerics.Pi

do not need to declare Pi as constant do not need to declare Pi as constant float ...float ...

Page 11: ADA Data Structures

Enumeration TypeEnumeration Type

Defined by a list of values taking Defined by a list of values taking the form of identifiers. the form of identifiers.

Useful in representing a fixed set Useful in representing a fixed set of values that are not numerical of values that are not numerical such as days of the week, class such as days of the week, class year (freshman, sophomore, junior, year (freshman, sophomore, junior, senior) , etc...senior) , etc...

Page 12: ADA Data Structures

Defining Enumeration Defining Enumeration TypeType

FormForm

TYPE enumeration-type TYPE enumeration-type ( identifier-list)( identifier-list)

Page 13: ADA Data Structures

ExamplesExamples

TYPE Days ISTYPE Days IS

(Monday, Tuesday, Wednesday, (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);Thursday, Friday, Saturday, Sunday);

DeclarationDeclaration

Some_Day : Days; Some_Day : Days;

Today : Days; Today : Days;

Valentine_Day : Days; Valentine_Day : Days;

Page 14: ADA Data Structures

More ExamplesMore Examples

Assignment StatementsAssignment Statements

Today := Friday; Today := Friday;

Some_Day := Monday;Some_Day := Monday;

Page 15: ADA Data Structures

Type Attributes and Type Attributes and OperationsOperations

Six important AttributesSix important Attributes First First LastLast Pos - - position for a given valuePos - - position for a given value Val - - value for a given positionVal - - value for a given position Pred - - predecessor of a given Pred - - predecessor of a given

value value Succ - - successor of a given valueSucc - - successor of a given value

Page 16: ADA Data Structures

Attribute QueryAttribute Query

Type’attribute-nameType’attribute-name

Type’attribute-name(value)Type’attribute-name(value)

ExampleExample

Days’FirstDays’First

Days’Succ(Friday)Days’Succ(Friday)

Days’Pos(Sunday)Days’Pos(Sunday)

Page 17: ADA Data Structures

Input/Output for Enumeration Input/Output for Enumeration TypesTypes

Within Ada.Text_IO,Within Ada.Text_IO,

a generic package called a generic package called “Enumeration_IO” must be used.“Enumeration_IO” must be used.

But it can not be used But it can not be used immediately.immediately.

Instances must be createdInstances must be created

Page 18: ADA Data Structures

FormatFormat

PACKAGE instance IS PACKAGE instance IS

NEW Ada.Text_IO.Enumeration_IO (Enum NEW Ada.Text_IO.Enumeration_IO (Enum => type);=> type);

Page 19: ADA Data Structures

Get ExamplesGet Examples

PACKAGE Day_IO IS NEW PACKAGE Day_IO IS NEW Ada.Text_IO.Enumeration ( Enum Ada.Text_IO.Enumeration ( Enum => Dats );=> Dats );

Day_IO.Get ( Item => Some_day);Day_IO.Get ( Item => Some_day);

Day_IO.Get ( Item => Day_IO.Get ( Item => Valentine_Day);Valentine_Day);

Page 20: ADA Data Structures

Put ExamplesPut Examples

Day_IO.PutDay_IO.Put

( Item => Some_Day, width => 5, Set => ( Item => Some_Day, width => 5, Set => Lower_case);Lower_case);

Page 21: ADA Data Structures

Example ProgramExample Program

with Enumeration Type with Enumeration Type

with Ada.Text_IO;Procedure Colors Is

Type English_Colors Is (white, black, red); Type French_Colors is (blanc, noir, rouge); Package Eng_Color_IO is New Ada.Text_IO.Enumeration_IO (Enum => English_Colors); Package Fr_Color_IO is New Ada.Text_IO.Enumeration_IO (Enum => French_Colors);

Page 22: ADA Data Structures

Eng_Color : English_Colors;Fr_Color : French_Colors;position : Natural;

Begin -- Ada.Text_IO.Put (Item =>" The first English color is "); Eng_Color_IO.Put(Item => English_Colors'First); Ada.Text_IO.New_Line; Ada.Text_IO.Put (Item => " Enter an English color : "); Eng_Color_IO.Get(Item => Eng_Color); position := English_Colors'Pos(Eng_Color); Fr_Color := French_Colors'Val(position); Ada.Text_IO.Put (Item => " The equavilent French Color is

"); Fr_Color_IO.Put (Item => Fr_Color, Set

=>Ada.Text_IO.Lower_Case); Ada.Text_IO.New_Line; end Colors;