Question bout the @binarysearch opcode (Glulx)

In glulxcercise, there’s this line that the interpreter I’m writing can’t handle:

@binarysearch histogram 8 search_b 8 4 1 1 val;

If I understand correctly, it means it’s searching of key of size 8 (second value) with an offset of 1 (sixth value) in a struct of size 8 (fourth value).

But that would mean that the key goes beyond the end of the struct (because KeyOffset + KeySize > StructSize).

Am I misunderstanding something? And if so, how am I supposed to handle this situation?

1 Like

I rewrote my implementation, and it turns out it works if you consider that there are no structs and you compare directly the keys, that could in fact even overlap. (i.e don’t check bounds, consider NumStruct as NumKey, StructSize as DistanceBetweenKeys, with the first key at Start + KeyOffset.)

The information about structs is only needed when returning the index of the found struct at the end.

If that’s the intended way of doing, then the spec is a bit misleading, since it explicitely says:

Within each struct, there is a key value KeySize bytes long, starting at position KeyOffset (from the start of the structure.)

(Emphasis mine.)