Adaptive Anderson mixing for electronic structure calculations