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.
This commit is contained in:
Jim Nelson 2013-09-03 14:53:32 -07:00
parent 3abbb95c98
commit ecd6fe3701
2 changed files with 10 additions and 25 deletions

View file

@ -66,21 +66,16 @@ public abstract class Geary.MessageData.IntMessageData : AbstractMessageData,
Gee.Hashable<IntMessageData> {
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() {

View file

@ -106,32 +106,22 @@ public Gee.MultiMap<V, K> reverse_multi_map<K, V>(Gee.MultiMap<K, V> 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;