|
From: | Andreas Fischlin |
Subject: | Re: [Gm2] can only CAST objects of the same size |
Date: | Sun, 19 Jul 2009 13:22:33 +0200 |
User-agent: | Thunderbird 2.0.0.22 (Macintosh/20090605) |
Dear Vladimir, I'm not sure whether you have ever received an answer to your question. I just stumbled over it cleaning up my inbox. IMHO such problems should be simply solved by using auxiliary variables and making good use of assignment compatibility to stay away from casting as much as possible. You then get also perfectly compatible source code that can be comiled without problems with many compilers. I suggest you introduce two auxiliary variables to acchieve what you want: VAR theCh: CHAR; lOrd: LONGCARD; the assuming the _expression_ is correctly of type LONGCARD your code then becomes theCh := FetchChar(Pattern, Index); lOrd := ORD(theCh); PatternHash:=(PatternHash*Base+lOrd) MOD Prime; and all should be fine. The advantages being that (i) you avoid casting of function procedure results, often implementation, i.e. compiler, dependent and may or may not work and casting should be in general a last resort; (ii) all implementations I know of are happy to assign to lOrd the result from the ORD _expression_, even regardless as ORD is defined (INTEGER vs. CARDINAL, PIM1,2,3,4 issue). Should there be any problem with bad values of the ORD _expression_ (very unlikely in your case, since CHAR is AFAIK always treated as unsigned and a string array is typically never longer than LONGCARD), the assignment is even safe, since compilers will generate test code (unless such options would be turned off). Casting is by definition not safe, except for incompatible sizes, although some compilers do cast safely, i.e. make also range tests or intercept negative value assignments of a signed _expression_ to unsigned targets. Hope this helps. Regards, Andreas Vladimir Karpenko wrote: Hello, EveryBody, I am trying to count something with gm2 here. Code looks something like that: PatternHash:=(PatternHash*Base+LONGCARD(ORD(FetchChar(Pattern, Index)))) MOD Prime; and like that: TextHash:=(TextHash*Base+LONGCARD(ORD(FetchChar(Text, Index)))) MOD Prime; and like that: TextHash:=(TextHash+Base*Prime-LONGCARD(ORD(FetchChar(Text,Index)))*Power) MOD Prime; and like that TextHash:=(TextHash*Base+LONGCARD(ORD(FetchChar(Text, Index+PatternLength)))) MOD Prime; on every of the lines mentioned above i got some strange error: can only CAST objects of the same size FetchChar looks like that: PROCEDURE FetchChar(Source:String; Index: CARDINAL):CHAR; BEGIN IF Index>Length(Source) THEN RETURN "|"; ELSE RETURN Source[Index]; END; END FetchChar; And string: TYPE String= ARRAY [0..255] OF CHAR; What should i do? Where am i wrong? Thanks, Vladimir. _______________________________________________ gm2 mailing list address@hidden http://lists.nongnu.org/mailman/listinfo/gm2 --
________________________________________________________________________ ETH Zurich Prof. Dr. Andreas Fischlin Systems Ecology - Institute of Integrative Biology CHN E 21.1 Universitaetstrasse 16 8092 Zurich SWITZERLAND address@hidden +41 44 633-6090 phone Make it as simple as possible, but distrust it! |
andreas_fischlin.vcf
Description: Vcard
[Prev in Thread] | Current Thread | [Next in Thread] |