0

Before saying anything, please let me make it clear that this question is NOT language-specific but part of my work on an interpreter.

Let's say we have an enum of Value types. So a value can be:

SV, // stringValue
IV, // integerValue
AV, // arrayValue
etc, etc

then let's say we have a function F which takes one of the following combinations of arguments:

[
   [SV],
   [SV,IV],
   [AV]
]

Now, the function is called, we calculate the values passed, and get their types. Let's say we get [XV,YV].

The question is:

What is the most efficient way to check if the passed values are allowed?

(The original interpreter is written in Nim, so one could say we could lookup the value array in the array of accepted value arrays like: accepted.contains(passed) - but this is not efficient)


P.S. ^ That's how I'm currently doing it, although I've explored the option of using bitmasks too. However, I cannot seem how it would help, since order plays an important part too.

kiran Biradar
  • 12,700
  • 3
  • 19
  • 44
Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
  • is the likelihood of passing any combination (much) higher than another? – pmg Nov 08 '19 at 11:29
  • @pmg I guess, yes. – Dr.Kameleon Nov 08 '19 at 11:34
  • Well, then ... check each combination by itself starting with the most probable – pmg Nov 08 '19 at 11:35
  • I still dare to say that is question actually is language specific, with different languages giving you different possibilities to solve this problem. – Rene Nov 08 '19 at 11:36
  • @Rene Hmm, I regarded it as more of a conceptual issue, but I see your point. As I mentioned, I'm writting it in Nim (so, anything valid for C is valid for me too) – Dr.Kameleon Nov 08 '19 at 11:39
  • In C I would define a structure that can store all possible value combinations, verify the combinations when the values are set and then pass this structure (or a pointer to it) to your function. – Rene Nov 08 '19 at 11:56
  • In theory you could use some tree-structure to group all functions with the same first parameter or so. In practice just checking every possibility is probably the fastest, if the number of legal combinations is low. – Lukas-T Nov 08 '19 at 12:25

0 Answers0