mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-16 15:18:37 +08:00
* Add Russian docs site baseline * Add Russian localized codebase * Polish Russian code wording * Update ru code translation. * Update code translation and chapter covers. * Fix pythontutor extraction. * Add README and landing page. * placeholder of profiles * Use figures of English version * Remove chapter paperbook
63 lines
1.5 KiB
Dart
63 lines
1.5 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;
|
||
}
|
||
|
||
/* XOR-хеширование */
|
||
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 Algo";
|
||
|
||
int hash = addHash(key);
|
||
print("Хеш-сумма сложением = $hash");
|
||
|
||
hash = mulHash(key);
|
||
print("Хеш-сумма умножением = $hash");
|
||
|
||
hash = xorHash(key);
|
||
print("Хеш-сумма XOR = $hash");
|
||
|
||
hash = rotHash(key);
|
||
print("Хеш-сумма с циклическим сдвигом = $hash");
|
||
}
|