@@ -1,7 +1,7 @@ /* - * Copyright (c) 2008 - 2009 + * Copyright (c) 2008 - 2010 * Jonathan Schleifer * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the @@ -11,10 +11,11 @@ #include "config.h" #import "OFNumber.h" #import "OFExceptions.h" +#import "OFMacros.h" #define RETURN_AS(t) \ switch (type) { \ case OF_NUMBER_CHAR: \ return (t)value.char_; \ @@ -143,11 +144,11 @@ break; \ default: \ @throw [OFInvalidFormatException newWithClass: isa]; \ } #define CALCULATE2(o, n) \ - switch ([n type]) { \ + switch (type) { \ case OF_NUMBER_CHAR: \ value.char_ o [n asChar]; \ break; \ case OF_NUMBER_SHORT: \ value.short_ o [n asShort]; \ @@ -756,11 +757,31 @@ } } - (uint32_t)hash { - return [self asUInt32]; + uint32_t hash; + size_t i; + + switch (type) { + case OF_NUMBER_FLOAT: + OF_HASH_INIT(hash); + for (i = 0; i < sizeof(float); i++) + OF_HASH_ADD(hash, ((char*)&value.float_)[i]); + OF_HASH_FINALIZE(hash); + + return hash; + case OF_NUMBER_DOUBLE: + OF_HASH_INIT(hash); + for (i = 0; i < sizeof(double); i++) + OF_HASH_ADD(hash, ((char*)&value.double_)[i]); + OF_HASH_FINALIZE(hash); + + return hash; + default: + return [self asUInt32]; + } } - add: (OFNumber*)num { CALCULATE2(+=, num)