Instead of building a set twice for optional and required,
build a set for each while walking the options once.
Then take advantage of set being sorted meaning we don't
have to enforce the upper/lower order ourselves.
Just cleaned up the formatting on the later loops.
Combined the if conditions and used a single line if.
Depends on D123501
I'm surprised you change this from !empty() to size(). I personally think the former is more readable. Not an issue in practice, but I do believe the STL guarantees ::empty to be O(1) while technically ::size could be O(n), though I think in practice they're both O(1).