Closed loskutov closed 2 months ago
Hmm, there was a reason I didn't use bsearch
, will see if I can dig up any notes on that.
Considering this change...
OK, so using bsearch requires a using separate comparison function... The new code also uses size_t
(unsigned) so I can't start left negative...
That said, I'll look at simplifying this code since it isn't performance-critical...
The loop invariant is that
last
is at leastfirst + 2
, which means that neitherentities[first]
norentities[last]
are ever accessed, so it's safe to have them outside the array bounds. Another invariant is thatentities[first].name < name < entities[last].name
, so oncefirst + 1 == last
, it means there's no occurrence.Also,
bsearch
could be used, which might be a more straightforward approach.