Line data Source code
1 : // ignore_for_file: binary-expression-operand-order 2 : 3 : /// 4 : /// 5 : /// 6 : abstract class Hashable { 7 : /// 8 : /// 9 : /// 10 2 : int hashIterable( 11 : Iterable<dynamic> iterable, 12 : // { int deep = 1, 13 : // bool debug = false,} 14 : ) { 15 2 : int iterated = iterable.fold( 16 : 0, 17 2 : (int h, dynamic i) { 18 : int hash; 19 2 : if (i is List) { 20 : // hash = hashIterable(i, deep: deep + 1, debug: debug); 21 0 : hash = hashIterable(i); 22 2 : } else if (i is Map) { 23 : // hash = hashIterable(i.values, deep: deep + 1, debug: debug); 24 0 : hash = hashIterable(i.values); 25 : } else if (i == null) { 26 : hash = 0; 27 : } else { 28 2 : hash = i.hashCode; 29 : } 30 : 31 : // int comb = combine(h, hash); 32 : // 33 : // if (debug) { 34 : // if (kDebugMode) { 35 : // print('${' ' * deep * 2}h: $h => ' 36 : // '(${i.runtimeType}) $i: $hash => comb: $comb'); 37 : // } 38 : // } 39 : // 40 : // return comb; 41 : 42 2 : return combine(h, hash); 43 : }, 44 : ); 45 : 46 : // int finished = finish(iterated); 47 : // 48 : // if (debug) { 49 : // if (kDebugMode) { 50 : // print('finish: $finished'); 51 : // } 52 : // } 53 : // 54 : // return finished; 55 2 : return finish(iterated); 56 : } 57 : 58 : /// 59 : /// 60 : /// 61 5 : int combine(int hash, int value) { 62 10 : hash = 0x1fffffff & (hash + value); 63 20 : hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10)); 64 : 65 10 : return hash ^ (hash >> 6); 66 : } 67 : 68 : /// 69 : /// 70 : /// 71 5 : int finish(int hash) { 72 20 : hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); 73 10 : hash = hash ^ (hash >> 11); 74 : 75 20 : return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); 76 : } 77 : }