Given integer I and an integer N which is power of 2, how does it work faster to calculate "I mod N"?
OpenJDK's java.util.HashMap.indexFor method gives us a best solution for it.
It simply calculates " I (bitwise AND) (N-1) ".
Reference
1. “HashMap.” Available: [Online] http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7u40-b43/java/util/HashMap.java#HashMap.indexFor%28int%2Cint%29
2. “HashMap implementation in Java. How does the bucket index calculation work?” Stackoverflow. Available: [Online] http://stackoverflow.com/questions/10879302/hashmap-implementation-in-java-how-does-the-bucket-index-calculation-work
沒有留言:
張貼留言