From ecd6fe37015c39b8b0f2293984fb67d49e482a97 Mon Sep 17 00:00:00 2001 From: Jim Nelson Date: Tue, 3 Sep 2013 14:53:32 -0700 Subject: [PATCH] Small fixes discovered while working on #7426 MessageIntData doesn't need to be so sophisticated about calculating a hash; slight optimization to the hash_memory() function. --- src/engine/common/common-message-data.vala | 7 +----- src/engine/util/util-collection.vala | 28 +++++++--------------- 2 files changed, 10 insertions(+), 25 deletions(-) diff --git a/src/engine/common/common-message-data.vala b/src/engine/common/common-message-data.vala index ea79fb4e..d716e2f7 100644 --- a/src/engine/common/common-message-data.vala +++ b/src/engine/common/common-message-data.vala @@ -66,21 +66,16 @@ public abstract class Geary.MessageData.IntMessageData : AbstractMessageData, Gee.Hashable { public int value { get; private set; } - private uint stored_hash = uint.MAX; - public IntMessageData(int value) { this.value = value; } public virtual bool equal_to(IntMessageData other) { - if (this == other) - return true; - return (value == other.value); } public virtual uint hash() { - return (stored_hash != uint.MAX) ? stored_hash : (stored_hash = int_hash(value)); + return value; } public override string to_string() { diff --git a/src/engine/util/util-collection.vala b/src/engine/util/util-collection.vala index 64e09580..042526f5 100644 --- a/src/engine/util/util-collection.vala +++ b/src/engine/util/util-collection.vala @@ -106,32 +106,22 @@ public Gee.MultiMap reverse_multi_map(Gee.MultiMap map) { /** * To be used by a Hashable's to_hash() method. */ -public static uint int64_hash(int64 value) { +public inline static uint int64_hash(int64 value) { return hash_memory(&value, sizeof(int64)); } /** - * To be used as a raw HashFunc where an int64 is being stored directly. - */ -public static uint bare_int64_hash(void *ptr) { - return hash_memory(ptr, sizeof(int64)); -} - -/** - * A HashFunc for DateTime. - */ -public static uint date_time_hash(void *a) { - return ((DateTime) a).hash(); -} - -/** - * A rotating-XOR hash that can be used to hash memory buffers of any size. Use only if - * equality is determined by memory contents. + * A rotating-XOR hash that can be used to hash memory buffers of any size. */ public static uint hash_memory(void *ptr, size_t bytes) { + if (bytes == 0) + return 0; + uint8 *u8 = (uint8 *) ptr; - uint hash = 0; - for (int ctr = 0; ctr < bytes; ctr++) + + // initialize hash to first byte value and then rotate-XOR from there + uint hash = *u8; + for (int ctr = 1; ctr < bytes; ctr++) hash = (hash << 4) ^ (hash >> 28) ^ (*u8++); return hash;