Rust Tips
TL;DR
rustで覚えておきたいことのメモ
Order
真面目にOrderが見たいときはstd::cmp::Orderingを使うと見やすい。Stringの辞書順とかでも使える。Floatには使えない。
min, max of float
Ordが実装されていないので、std::cmp::maxとかはだめ。
cumsum
std::vecのbinary_search
普通に便利なんだけど、同じ値が含まれていた場合、matchそのものは値全部にできるのだが、返ってくる値は最大のindexを返す。なので、lower_boundがしたいときはsupersliceを使ったほうがよさそう。もしくは自分で書くか。
以下docsの引用。
If the value is found then Result::Ok is returned, containing the index of the matching element. If there are multiple matches, then any one of the matches could be returned. If the value is not found then Result::Err is returned, containing the index where a matching element could be inserted while maintaining sorted order.
雑にテストしてみると、atcoderのversion(1.42.0)では、iは4を返す。これはversionごとにも挙動が違うらしい。rustコミュニティでも議論はされているようなんだけど、いつ実装されるのかは不明。
なので、upper_boundしたければ下記の感じでできるはず。まあおとなしく自分で書いたほうがいい気がします。