mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-01 03:24:24 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			63 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			63 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
| /**
 | |
|  * File: simple_hash.dart
 | |
|  * Created Time: 2023-06-25
 | |
|  * Author: liuyuxin (gvenusleo@gmail.com)
 | |
|  */
 | |
| 
 | |
| /* 加法哈希 */
 | |
| int addHash(String key) {
 | |
|   int hash = 0;
 | |
|   final int MODULUS = 1000000007;
 | |
|   for (int i = 0; i < key.length; i++) {
 | |
|     hash = (hash + key.codeUnitAt(i)) % MODULUS;
 | |
|   }
 | |
|   return hash;
 | |
| }
 | |
| 
 | |
| /* 乘法哈希 */
 | |
| int mulHash(String key) {
 | |
|   int hash = 0;
 | |
|   final int MODULUS = 1000000007;
 | |
|   for (int i = 0; i < key.length; i++) {
 | |
|     hash = (31 * hash + key.codeUnitAt(i)) % MODULUS;
 | |
|   }
 | |
|   return hash;
 | |
| }
 | |
| 
 | |
| /* 异或哈希 */
 | |
| int xorHash(String key) {
 | |
|   int hash = 0;
 | |
|   final int MODULUS = 1000000007;
 | |
|   for (int i = 0; i < key.length; i++) {
 | |
|     hash ^= key.codeUnitAt(i);
 | |
|   }
 | |
|   return hash & MODULUS;
 | |
| }
 | |
| 
 | |
| /* 旋转哈希 */
 | |
| int rotHash(String key) {
 | |
|   int hash = 0;
 | |
|   final int MODULUS = 1000000007;
 | |
|   for (int i = 0; i < key.length; i++) {
 | |
|     hash = ((hash << 4) ^ (hash >> 28) ^ key.codeUnitAt(i)) % MODULUS;
 | |
|   }
 | |
|   return hash;
 | |
| }
 | |
| 
 | |
| /* Dirver Code */
 | |
| void main() {
 | |
|   String key = "Hello 算法";
 | |
| 
 | |
|   int hash = addHash(key);
 | |
|   print("加法哈希值为 $hash");
 | |
| 
 | |
|   hash = mulHash(key);
 | |
|   print("乘法哈希值为 $hash");
 | |
| 
 | |
|   hash = xorHash(key);
 | |
|   print("异或哈希值为 $hash");
 | |
| 
 | |
|   hash = rotHash(key);
 | |
|   print("旋转哈希值为 $hash");
 | |
| }
 | 
