[comp.lang.pascal] SET

PRDESENV%ORION.CPQD.ANSP.BR@uga.cc.uga.edu (11/26/90)

Hey,

        Does anybody know how can I build a SET with Real (or double)
elements in Turbo Pascal? I Only need a SET with 15 real numbers.

        Once SET requires subranges with no more than 256 itens I can not
make a set of word (my real numbers have only one place after
the decimal point, so I could multiply by 10, solving the problem, but
the range from the first to the last overcome 256).

        I've tried to make a TYPE with these numbers but Turbo gives an
error saying "Error 2: Identifier expected" .

        Any help has value. Thanks.

                                        Claudio

dmurdoch@watstat.waterloo.edu (Duncan Murdoch) (11/26/90)

In article <25092@adm.brl.mil> PRDESENV%ORION.CPQD.ANSP.BR@uga.cc.uga.edu writes:
>Hey,
>
>        Does anybody know how can I build a SET with Real (or double)
>elements in Turbo Pascal? I Only need a SET with 15 real numbers.

If you have version 5.5 or 6.0, you could get Object Professional from 
Turbopower Software.  It includes objects that can handle integer type sets
with more than 256 elements, and a general purpose set for strings, that
could easily be made to hold floating point values.  There's also a huge
number of other routines, mostly dealing with user interface objects.

I don't have their number handy, but they advertise regularly in PC programming
magazines.   You can also contact them by email; my alias line for them is 

    alias turbopower 76004.2611@compuserve.com

Duncan Murdoch

zhou@brazil.psych.purdue.edu (Albert Zhou) (11/27/90)

In article <25092@adm.brl.mil> PRDESENV%ORION.CPQD.ANSP.BR@uga.cc.uga.edu writes:
>Hey,
>
>        Does anybody know how can I build a SET with Real (or double)
>elements in Turbo Pascal? I Only need a SET with 15 real numbers.

	There is no directly way of doing it in TP. But you can map these
15 reals to 0..14 and subsequently define a set of byte [0..14].
>
>        Once SET requires subranges with no more than 256 itens I can not
>make a set of word (my real numbers have only one place after
>the decimal point, so I could multiply by 10, solving the problem, but
>the range from the first to the last overcome 256).
It looks like you need to do certain non-linear transform so that you can map
no more than 255 reals or integers into a set of byte (Multiplying by 10 is
a kind of transform or mapping if you haven't aware, very simple linear 
transform though).

>
>        I've tried to make a TYPE with these numbers but Turbo gives an
>error saying "Error 2: Identifier expected" .

	As I remember, you can make subrange out of BYTE, CHAR, INTEGER and 
WORD (They are all ordinal). But you cannot make subrange out of REAL.

CDCKAB%EMUVM1.BITNET@cunyvm.cuny.edu ( Karl Brendel) (11/27/90)

In article <9011260748.aa11950@VIM.BRL.MIL>
  PRDESENV%ORION.CPQD.ANSP.BR@uga.cc.uga.edu ( Claudio ) writes:

>Does anybody know how can I build a SET with Real (or double)
>elements in Turbo Pascal? I Only need a SET with 15 real numbers.
>
>Once SET requires subranges with no more than 256 itens I can not
>make a set of word (my real numbers have only one place after the
>decimal point, so I could multiply by 10, solving the problem, but
>the range from the first to the last overcome 256).

[deleted to end]

In article <9011260916.aa08727@VGR.BRL.MIL>
  ZCCBJSB%EB0UB011.BITNET@cunyvm.cuny.edu ( Josep Sau B. ) writes:

>If those real values are variable, forget it, you can't use the set
>structure.
>
>If those real values are constant, you may do thru with this trick:
>
>Declare a const array of real, and use the ordinal index type to
>build up the set to work with.
[deleted to end]

I just wanted to point out that Josep Sau B.'s method may be more
general than he admits: You should be able to declare a _variable_
array [1..max] of reals together with the value of the highest index
in use. Then add reals to the array as required, starting with element
1. If you delete an element from the set, compact the array and Dec
the value of the high index. Example:

const
  maxIndex = 255;
type
  RealSetArray = array[1..maxIndex]of real;
var
  RealSet : RealSetArray;
  i,hiIndex : byte;

procedure InitRealSetArray(var index : byte);
begin
  index := 0;
end;

function RealInSet
  (r : real; var rsa : RealSetArray;
   var i : byte; hiIndex : byte) : Boolean;
{Obviously for a large set you'd use a more sophisticated searching
routine than you see here--but this is about sets, not about
searching. <g>}
var
  ii : byte;
begin
  for ii := 1 to hiIndex do
    if r = rsa[ii] then
    begin
      RealInSet := True;
      i := ii;
      Exit;
    end;
  RealInSet := False;
end;

function AddRealToSet
  (r : real; var rsa : RealSetArray; var index : Byte) : Boolean;
var
  i : byte;
begin
  AddRealToSet := True;
  if not RealInSet(r,rsa,i,index) then
  begin
    if index = maxIndex then AddRealToSet := False
    else begin
      Inc(index);
      rsa[index] := r;
    end;
  end;
end;

procedure RemoveRealFromSet
  (r : real; var rsa : RealSetArray; var index : Byte);
var
  i : byte;
begin
  if RealInSet(r,rsa,i,index) then
  begin
    rsa[i] := rsa[index];
    Dec(index);
  end;
end;

begin
  InitRealSetArray(hiIndex);
  if not AddRealToSet(3.1415,RealSet,hiIndex) then Halt(1);
  if not AddRealToSet(2.7183,RealSet,hiIndex) then Halt(1);
  if not AddRealToSet(-1.5708,RealSet,hiIndex) then Halt(1);
  WriteLn('2.5 is in the set: ',RealInSet(2.5,RealSet,i,hiIndex));
  WriteLn('Pi is in the set: ',RealInSet(3.1415,RealSet,i,hiIndex));
  RemoveRealFromSet(3.1415,RealSet,hiIndex);
  if not AddRealToSet(2.5,RealSet,hiIndex) then Halt(1);
  WriteLn('Added 2.5, deleted 3.1415');
  WriteLn('2.5 is in the set: ',RealInSet(2.5,RealSet,i,hiIndex));
  WriteLn('Pi is in the set: ',RealInSet(3.1415,RealSet,i,hiIndex));
end.

Caveat about RealInSet: Anything which relies on comparing "real"
numbers for "exact equality" can prove flakey. You may want to Trunc
or Round your reals in some manner which leads to reliable
comparisons.
+-------------------------------------------------------------------------+
| Karl Brendel                                Centers for Disease Control |
| Internet: CDCKAB@EMUVM1.BITNET              Epidemiology Program Office |
| Bitnet: CDCKAB@EMUVM1                       Atlanta, GA, USA            |
| ILink/RIME: KARL BRENDEL                    phone 404/639-2709          |
| CIS  : 73307,3101                           fts       236-2709          |
| GEnie: K.BRENDEL                            Home of Epi Info 5.0        |
+-------------------------------------------------------------------------+

ANNEKE%HUTRUU54.BITNET@uga.cc.uga.edu ( Anneke Sicherer-Roetman) (11/30/90)

As far as I know a set always consists of types that exist in discrete
values, that is all simple types EXCEPT real. What you want is therefore
impossible. Anneke