[fpc-pascal] best method: multiple three state options in a decision tree

Lukasz Sokol el.es.cr at gmail.com
Thu Jan 23 11:01:40 CET 2014


On 22/01/14 21:23, waldo kitty wrote:
[...] 
>> - associate the procedures (their pointers) to jump to with the
>> valid bit masks in a new data type (- do you REALLY have that many
>> procedures ? Or are they somewhat repeating themselves ?)
> 
> it will be only two procedures IF i can do it like that... it should
> be easier to use one CASE procedure of the existing ~8000 lines for
> both (future) input selection of matching data records and for
> (current) output filtering of matching data records...
> 
> so what i was actually asking is if using a pointer is the proper
> item for passing the procedure to use and is @procA how i would
> specify that in the call to the procedure...
> 
>> - then just walk the array of [bit mask, proc pointer] records and
>> jump to pointers supplied if matching?
>> 
>> No ifs, no buts, treat the Options as a bitmask if such is its
>> purpose?
>> 
>> Just a thought,
> 
> i really am trying to visualize your suggested methods ;)
> 

Maybe Something like: (but this is with type TOptionMinMax = [mmNone, mmMin,mmMinMax, mmMax])


//then 
type
TOptionsArray = array[0..4] of TOptionMinMax;

var 
OptionsArray : TOptionsArray;

//then you process all the options and do some kind of score to them :


function JumpTo(AOptionsArray : TOptionsArray): TProcedure;
var Score : integer;

begin
for i := 0 to 4 do // walk the array one by one
  begin
    case AOptionsArray[i].Option of
      mmMin : ;   // 
      mmMinMax : ;// here gather the 'score' of the particular set of options
      mmMax : ;   // depending on 'i' and these 4 cases - if there is a well-defined formula detecting this
      else        // 
    end; // case
  end; // for

//then the actual detection of score to actual procedures

if Score < something1 then Result := AddressOf(Procedure1); end;  // pseudo-code ;)
if Score >= someting1 and Score < something2 then ... end;
...
...

end;

// converting the above to dynamic array of Options :

type
TOptionMinMax = [mmMin, mmMinMax, mmMax]; 

TOption = record
  OptionIndex : integer;
  OptionMinMax : TOptionMinMax;
end;

TOptionsArray = array of TOption;

function JumpTo(AOptionsArray : TOptionsArray): TProcedure;
var Score : integer;

begin
for i := 0 to SizeOf(AOptionsArray)-1 do
  begin
    case AOptionsArray[i].Option of
      mmMin : ;   // 
      mmMinMax : ;// here gather the 'score' of the particular set of options
      mmMax : ;   // depending on 'OptionIndex'(!) and these 3 cases - if there is a well-defined formula detecting this
                  // no mmNone because it's only to say the option is not in the set
    end; // case
  end; // for

//then the actual detection of score to procedure(s)

if Score < something1 then Result := AddressOf(Procedure1); end;  // pseudo-code ;)
if Score >= someting1 and Score < something2 then ... end;
...
...

end;

// score detection to procedures could be also automated:

type TJumpToScoreProcedure = record
   MinScore : integer;
   MaxScore : integer;
   JumpTo : TProcedure;
end;

// declare the above statically / or dynamically somewhere in an array:

const JumpToArray = [(MinScore : 10; MaxScore : 20 ; JumpTo : @Procedure1), ... ];

// and the actual detection in the JumpTo function above ^^^

for j  := 0 to SizeOf(JumpToArray)-1 do
  if (Score > JumpToArray[j].MinScore) and (Score < JumpToArray.MaxScore) then 
    Result := JumpToArray[j].JumpTo;



// usage in both cases would be:

@Procedure := JumpTo(OptionsArray);

if Assigned(Procedure) then Procedure(); // in case we did not get a valid pointer from all that or
there was an error

Is this what you meant ? 

-L.






More information about the fpc-pascal mailing list