comp.lang.ada
 help / color / mirror / Atom feed
* Character Type Definition
@ 1991-07-23 18:47 cis.ohio-state.edu!udecc.engr.udayton.edu!blackbird.afit.af.mil!galaxy.af
  0 siblings, 0 replies; 3+ messages in thread
From: cis.ohio-state.edu!udecc.engr.udayton.edu!blackbird.afit.af.mil!galaxy.af @ 1991-07-23 18:47 UTC (permalink / raw)


I am new to Ada and have been trying to find out how to do the following.
I would like to define a character type of disjoint ASCII characters so I
can perform a membership test.  For instance, 

subtype Math_Operators is character ('^','*','/','+','-'); -- syntax??

I am able to define the type but cannot do a membership test such as,

if My_Operator in Math_Operators then ...

This gives me an invalid comparison since they are not of the same type.
Is there a way to do the above (a case statement will work of course).
Any help would be appreciated.  Thanks in advance.


----------------------------------------------------------------------------- 
Rusty Baldwin **Your impressive title here**   rbaldwin@blackbird.afit.af.mil

Air Force Institute of Technology              Tel: (513)255-8989
AFIT/EN046
Wright-Patterson AFB, Ohio  45433
USA

Computers are useless.  They can only give you answers.  - Pablo Picasso
----------------------------------------------------------------------------

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

* Re: Character Type Definition
@ 1991-07-24 23:16 Robert I. Eachus
  0 siblings, 0 replies; 3+ messages in thread
From: Robert I. Eachus @ 1991-07-24 23:16 UTC (permalink / raw)


In article <rbaldwin.680294862@galaxy.afit.af.mil> news@afit.af.mil writes:

 > I am new to Ada and have been trying to find out how to do the following.
 > I would like to define a character type of disjoint ASCII characters so I
 > can perform a membership test...

   Since the set you need is not contiguous you need a Set construct.
If you don't have a generic Set package around ;-) all you need do is:

   type Character_Set is array (Character) of Boolean;
   pragma PACK(Character_Set);

   Now for your example the code is:

   Math_Operator: constant Character_Set := 
		('^' | '*' | '/' | '+' | '-' => True, others => False);
   ...

   if Math_Operator(My_Operator) then...

   Depending on the size and use of the sets you may or may not want
the pragma PACK.  While this is not quite as elegant as having a set
type in Ada, in was felt to be easy enough that adding sets was not
justified.

   Without actually benchmarking things in particular cases, it is
almost impossible to tell whether this, an if statement with "'^' or
'*' or '+'..." or the case statement will be faster.  However the set
method is in general the easiest to work with and the least error
prone.

   Where a set is contiguous in ASCII you may want to use subtypes.
If the code needs to be portable or will be around a long time, this
is not a good idea. The set model is ALWAYS the easiest to maintain.  

--

					Robert I. Eachus

with STANDARD_DISCLAIMER;
use  STANDARD_DISCLAIMER;
function MESSAGE (TEXT: in CLEVER_IDEAS) return BETTER_IDEAS is...

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

* Re: Character Type Definition
@ 1991-07-29  7:56 Robert I. Eachus
  0 siblings, 0 replies; 3+ messages in thread
From: Robert I. Eachus @ 1991-07-29  7:56 UTC (permalink / raw)


      I said:

>     Since the set you need is not contiguous you need a Set construct.
>  If you don't have a generic Set package around ;-) all you need do is:

>     type Character_Set is array (Character) of Boolean;
>     pragma PACK(Character_Set)

>     Now for your example the code is:

>     Math_Operator: constant Character_Set := 
>		   ('^' | '*' | '/' | '+' | '-' => True, others => False);
>     ...

>     if Math_Operator(My_Operator) then...

      Norm Cohen pointed out to me that 4.3.2(6) forbids the initial
value for Math_Operator unless it is qualified, in spite of the fact
that it is already constrained:
    
    Math_Operator: constant Character_Set := 
      Character_Set'('^' | '*' | '/' | '+' | '-' => True, others => False);

      If this makes no sense to you, I think that you will find
yourself with lots of company. However, I think it comes under the
heading of insufficiently broken to try to fix before Ada 9X.
--

					Robert I. Eachus

with STANDARD_DISCLAIMER;
use  STANDARD_DISCLAIMER;
function MESSAGE (TEXT: in CLEVER_IDEAS) return BETTER_IDEAS is...

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

end of thread, other threads:[~1991-07-29  7:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1991-07-29  7:56 Character Type Definition Robert I. Eachus
  -- strict thread matches above, loose matches on Subject: below --
1991-07-24 23:16 Robert I. Eachus
1991-07-23 18:47 cis.ohio-state.edu!udecc.engr.udayton.edu!blackbird.afit.af.mil!galaxy.af

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