[fpc-pascal] Sets as array index?

J├╝rgen Hestermann juergen.hestermann at gmx.de
Sat Oct 12 14:07:53 CEST 2013


I have a declaration like this:

type TagType       = (tag1,tag2,tag3,tag4,tag5);
         TagSetType = set of TagType;

I now want to store numbers for each of the 2^n possible set settings
[tag1,tag2,tag3,tag4,tag5]
[tag1,tag2,tag3,tag4         ]
[tag1,tag2,tag3         ,tag5]
.. and so on...

so I declared this array:

type TagSetArrayType = array[0..(1 shl (ord(High(TagSetTyp))+1))-1] of  integer;

My question:
Is it possible to retrieve the corresponding array index directly from a set variable?
For example, if I have

var TagSet     : TagSetType;
var TagArray  : TagSetArrayType;
var I                 : Integer;

I want to do something like this (which is not directly possible but maybe by some other way?):

I := TagArray[TagSet];
I := TagArray[[tag1,tag5]];
or
I := TagArray[ord(TagSet)];
I := TagArray[ord([tag1,tag5])];

I think that the compiler somehow needs to generate the bit mask for [tag1,tag5]
internally anyway so maybe it's available by some function to the programmer?





More information about the fpc-pascal mailing list