diff --git a/uppsrc/Core/BiCont.h b/uppsrc/Core/BiCont.h index 9cbc1cd82..c25347b25 100644 --- a/uppsrc/Core/BiCont.h +++ b/uppsrc/Core/BiCont.h @@ -49,7 +49,7 @@ public: void Serialize(Stream& s); String ToString() const; - dword GetHashValue() const { return HashBySerialize(*this); } + hash_t GetHashValue() const { return HashBySerialize(*this); } template bool operator==(const B& b) const { return IsEqualRange(*this, b); } template bool operator!=(const B& b) const { return !operator==(b); } template int Compare(const B& b) const { return CompareRanges(*this, b); } @@ -128,7 +128,7 @@ public: void Serialize(Stream& s); String ToString() const; - dword GetHashValue() const { return HashBySerialize(*this); } + hash_t GetHashValue() const { return HashBySerialize(*this); } template bool operator==(const B& b) const { return IsEqualRange(*this, b); } template bool operator!=(const B& b) const { return !operator==(b); } template int Compare(const B& b) const { return CompareRanges(*this, b); } diff --git a/uppsrc/Core/Color.h b/uppsrc/Core/Color.h index d2f86131b..0344d28df 100644 --- a/uppsrc/Core/Color.h +++ b/uppsrc/Core/Color.h @@ -48,7 +48,7 @@ public: void SetNull() { color = 0xffffffff; } bool IsNullInstance() const { return color == 0xffffffff; } - unsigned GetHashValue() const { return color; } + hash_t GetHashValue() const { return color; } bool operator==(Color c) const { return color == c.color; } bool operator!=(Color c) const { return color != c.color; } @@ -91,7 +91,7 @@ inline Color StraightColor(RGBA rgba) { return Color(rgba.r, rgba.g, rgba.b); } typedef Color (*ColorF)(); -inline unsigned GetHashValue(Color c) { return c.GetHashValue(); } +inline hash_t GetHashValue(Color c) { return c.GetHashValue(); } inline Color Nvl(Color a, Color b) { return IsNull(a) ? b : a; } template<> diff --git a/uppsrc/Core/Complex.h b/uppsrc/Core/Complex.h index 2d47ad560..07505a384 100644 --- a/uppsrc/Core/Complex.h +++ b/uppsrc/Core/Complex.h @@ -26,7 +26,7 @@ struct Complex : std::complex }; template<> inline bool IsNull(const Complex& r) { return r.real() < DOUBLE_NULL_LIM || r.imag() < DOUBLE_NULL_LIM; } -template<> inline unsigned GetHashValue(const Complex& x) { return CombineHash(x.real(), x.imag()); } +template<> inline hash_t GetHashValue(const Complex& x) { return CombineHash(x.real(), x.imag()); } template<> inline String AsString(const std::complex& x) { return String().Cat() << "(" << x.real() << "," << x.imag() << ")"; } template<> inline String AsString(const Complex& x) { return AsString((const std::complex&)x); } diff --git a/uppsrc/Core/Format.cpp b/uppsrc/Core/Format.cpp index 59f3f7fc8..bb004f56c 100644 --- a/uppsrc/Core/Format.cpp +++ b/uppsrc/Core/Format.cpp @@ -511,7 +511,7 @@ struct FormId : Moveable { int type; }; -unsigned GetHashValue(const FormId& fid) +hash_t GetHashValue(const FormId& fid) { return CombineHash(fid.type, GetHashValue(fid.id)); } diff --git a/uppsrc/Core/Gtypes.h b/uppsrc/Core/Gtypes.h index 6b747702b..e7bf0ceac 100644 --- a/uppsrc/Core/Gtypes.h +++ b/uppsrc/Core/Gtypes.h @@ -56,7 +56,7 @@ struct Size_ : Moveable< Size_ > { friend T Squared(Size_ a) { return a.cx * a.cx + a.cy * a.cy; } friend double Length(Size_ a) { return hypot(a.cx, a.cy); } - unsigned GetHashValue() const { return CombineHash(cx, cy); } + hash_t GetHashValue() const { return CombineHash(cx, cy); } String ToString() const; @@ -167,7 +167,7 @@ struct Point_ : Moveable< Point_ > { friend Point_ Nvl(Point_ a, Point_ b) { return IsNull(a) ? b : a; } - unsigned GetHashValue() const { return CombineHash(x, y); } + hash_t GetHashValue() const { return CombineHash(x, y); } String ToString() const; @@ -357,7 +357,7 @@ struct Rect_ : Moveable< Rect_ > { friend const Rect_& Nvl(const Rect_& a, const Rect_& b) { return IsNull(a) ? b : a; } - unsigned GetHashValue() const { return CombineHash(left, top, right, bottom); } + hash_t GetHashValue() const { return CombineHash(left, top, right, bottom); } String ToString() const; diff --git a/uppsrc/Core/InVector.h b/uppsrc/Core/InVector.h index 920dce9ac..eb5da6ade 100644 --- a/uppsrc/Core/InVector.h +++ b/uppsrc/Core/InVector.h @@ -132,27 +132,27 @@ public: template int Find(const T& val, const L& less) const; - int Find(const T& val) const { return Find(val, StdLess()); } + int Find(const T& val) const { return Find(val, StdLess()); } - ConstIterator begin() const { ConstIterator it; SetBegin(it); return it; } - ConstIterator end() const { ConstIterator it; SetEnd(it); return it; } + ConstIterator begin() const { ConstIterator it; SetBegin(it); return it; } + ConstIterator end() const { ConstIterator it; SetEnd(it); return it; } - Iterator begin() { Iterator it; SetBegin(it); return it; } - Iterator end() { Iterator it; SetEnd(it); return it; } + Iterator begin() { Iterator it; SetBegin(it); return it; } + Iterator end() { Iterator it; SetEnd(it); return it; } InVector(); - InVector(InVector&& v) { Pick(pick(v)); } - void operator=(InVector&& v) { Pick(pick(v)); } + InVector(InVector&& v) { Pick(pick(v)); } + void operator=(InVector&& v) { Pick(pick(v)); } InVector(const InVector& v, int); - InVector(std::initializer_list init) { Init(); for(const auto& i : init) Add(i); } + InVector(std::initializer_list init) { Init(); for(const auto& i : init) Add(i); } void Swap(InVector& b); - void Serialize(Stream& s) { StreamContainer(s, *this); } + void Serialize(Stream& s) { StreamContainer(s, *this); } void Xmlize(XmlIO& xio, const char *itemtag = "item"); void Jsonize(JsonIO& jio); String ToString() const; - dword GetHashValue() const { return HashBySerialize(*this); } + hash_t GetHashValue() const { return HashBySerialize(*this); } template bool operator==(const B& b) const { return IsEqualRange(*this, b); } template bool operator!=(const B& b) const { return !operator==(b); } template int Compare(const B& b) const { return CompareRanges(*this, b); } @@ -380,7 +380,7 @@ public: void Xmlize(XmlIO& xio, const char *itemtag = "item"); void Jsonize(JsonIO& jio); String ToString() const; - dword GetHashValue() const { return HashBySerialize(*this); } + hash_t GetHashValue() const { return HashBySerialize(*this); } template bool operator==(const B& b) const { return IsEqualRange(*this, b); } template bool operator!=(const B& b) const { return !operator==(b); } template int Compare(const B& b) const { return CompareRanges(*this, b); } @@ -525,7 +525,8 @@ public: void Xmlize(XmlIO& xio, const char *itemtag = "key") { iv.Xmlize(xio, itemtag); } void Jsonize(JsonIO& jio) { iv.Jsonize(jio); } String ToString() const; - dword GetHashValue() const { return HashBySerialize(*this); } + hash_t GetHashValue() const { return HashBySerialize(*this); } + template bool operator==(const B& b) const { return IsEqualRange(*this, b); } template bool operator!=(const B& b) const { return !operator==(b); } template int Compare(const B& b) const { return CompareRanges(*this, b); } @@ -670,7 +671,7 @@ public: void Serialize(Stream& s); void Xmlize(XmlIO& xio); void Jsonize(JsonIO& jio); - dword GetHashValue() const { return HashBySerialize(*this); } + hash_t GetHashValue() const { return HashBySerialize(*this); } #endif const InVector& GetValues() const { return B::value.data; } @@ -754,7 +755,7 @@ public: void Serialize(Stream& s); void Xmlize(XmlIO& xio); void Jsonize(JsonIO& jio); - dword GetHashValue() const { return HashBySerialize(*this); } + hash_t GetHashValue() const { return HashBySerialize(*this); } #endif void Swap(SortedArrayMap& x); diff --git a/uppsrc/Core/Index.cpp b/uppsrc/Core/Index.cpp index f8c7edeeb..5ff7561e1 100644 --- a/uppsrc/Core/Index.cpp +++ b/uppsrc/Core/Index.cpp @@ -160,45 +160,4 @@ void IndexCommon::Sweep(int n) unlinked = -1; } -#ifdef CPU_UNALIGNED - -NOUBSAN // CPU supports unaligned memory access -unsigned memhash(const void *ptr, size_t count) -{ - unsigned hash = 1234567890U; - - const unsigned *ds = (unsigned *)ptr; - const unsigned *de = ds + (count >> 2); - while(ds < de) - hash = ((hash << 5) - hash) ^ *ds++; - - const byte *s = (byte *)ds; - const byte *e = s + (count & 3); - while(s < e) - hash = ((hash << 5) - hash) ^ *s++; - - return hash; -} - -#else - -unsigned memhash(const void *ptr, size_t count) -{ - unsigned hash = 1234567890U; - - const byte *s = (byte *)ptr; - const byte *e = s + count; - while(s < e) - hash = ((hash << 5) - hash) ^ *s++; - - return hash; -} - -#endif - -unsigned GetHashValue0(const double& d) -{ - return memhash(&d, sizeof(double)); -} - } \ No newline at end of file diff --git a/uppsrc/Core/Map.h b/uppsrc/Core/Map.h index 1a0c99251..f3d1aad50 100644 --- a/uppsrc/Core/Map.h +++ b/uppsrc/Core/Map.h @@ -147,7 +147,7 @@ public: void Xmlize(XmlIO& xio); void Jsonize(JsonIO& jio); String ToString() const; - dword GetHashValue() const { return HashBySerialize(*this); } + hash_t GetHashValue() const { return HashBySerialize(*this); } bool operator==(const AMap& b) const { ASSERT(!HasUnlinked()); return IsEqualMap(*this, b); } bool operator!=(const AMap& b) const { return !operator==(b); } int Compare(const AMap& b) const { ASSERT(!HasUnlinked()); return CompareMap(*this, b); } diff --git a/uppsrc/Core/Mem.h b/uppsrc/Core/Mem.h index 0e8bc7f2d..6d9742442 100644 --- a/uppsrc/Core/Mem.h +++ b/uppsrc/Core/Mem.h @@ -614,3 +614,39 @@ int inline_memcmp_aligned(const char *a, const char *b, size_t len) return memcmp(a, b, len); } #endif + +#ifdef CPU_UNALIGNED + +NOUBSAN // CPU supports unaligned memory access +inline hash_t memhash(const void *ptr, size_t count) +{ + unsigned hash = 1234567890U; + + const unsigned *ds = (unsigned *)ptr; + const unsigned *de = ds + (count >> 2); + while(ds < de) + hash = ((hash << 5) - hash) ^ *ds++; + + const byte *s = (byte *)ds; + const byte *e = s + (count & 3); + while(s < e) + hash = ((hash << 5) - hash) ^ *s++; + + return hash; +} + +#else + +inline hash_t memhash(const void *ptr, size_t count) +{ + unsigned hash = 1234567890U; + + const byte *s = (byte *)ptr; + const byte *e = s + count; + while(s < e) + hash = ((hash << 5) - hash) ^ *s++; + + return hash; +} + +#endif diff --git a/uppsrc/Core/Ops.h b/uppsrc/Core/Ops.h index 0f437e7f8..4ac0aa0cc 100644 --- a/uppsrc/Core/Ops.h +++ b/uppsrc/Core/Ops.h @@ -150,11 +150,18 @@ void EndianSwap(int *v, size_t count); void EndianSwap(int64 *v, size_t count); void EndianSwap(uint64 *v, size_t count); + +typedef dword hash_t; + inline dword FoldHash(dword h) { return SwapEndian32(0xa3613c16 * h); } +#define HASH64_CONST1 I64(0xf7c21089bee7c0a5) +#define HASH64_CONST2 I64(0xc85abc8da7534a4d) +#define HASH64_CONST3 I64(0x8642b0fe3e86671b) + force_inline int SignificantBits(dword x) { // basically log2(x) + 1 except that for 0 this is 0, number of significant bits of x diff --git a/uppsrc/Core/Other.h b/uppsrc/Core/Other.h index a966f2294..01308ea79 100644 --- a/uppsrc/Core/Other.h +++ b/uppsrc/Core/Other.h @@ -352,7 +352,7 @@ private: const void *type; bool operator==(const Key& b) const { return key == b.key && type == b.type; } - unsigned GetHashValue() const { return CombineHash(key, (uintptr_t)type); } + hash_t GetHashValue() const { return CombineHash(key, (uintptr_t)type); } }; Index key; diff --git a/uppsrc/Core/Speller.cpp b/uppsrc/Core/Speller.cpp index e9656a8a2..210f607fe 100644 --- a/uppsrc/Core/Speller.cpp +++ b/uppsrc/Core/Speller.cpp @@ -276,7 +276,7 @@ struct SpellKey : Moveable { int lang; WString wrd; - unsigned GetHashValue() const { return CombineHash(lang, wrd); } + hash_t GetHashValue() const { return CombineHash(lang, wrd); } bool operator==(const SpellKey& b) const { return lang == b.lang && wrd == b.wrd; } }; diff --git a/uppsrc/Core/String.cpp b/uppsrc/Core/String.cpp index 8153a8422..fc7b919d9 100644 --- a/uppsrc/Core/String.cpp +++ b/uppsrc/Core/String.cpp @@ -52,10 +52,10 @@ bool String0::LEq(const String0& s) const return l == s.GetCount() && inline_memeq8_aligned(begin(), s.begin(), l); } -unsigned String0::LHashValue() const +hash_t String0::LHashValue() const { int l = LLen(); - if(l < 15) { + if(l < 15) { // must be the same as small hash dword w[4]; w[0] = w[1] = w[2] = w[3] = 0; memcpy8((char *)w, ptr, l); diff --git a/uppsrc/Core/String.h b/uppsrc/Core/String.h index a21648d34..7539684e2 100644 --- a/uppsrc/Core/String.h +++ b/uppsrc/Core/String.h @@ -215,7 +215,7 @@ class String0 : Moveable { void LSet(const String0& s); void LFree(); void LCat(int c); - unsigned LHashValue() const; + hash_t LHashValue() const; void UnShare(); void SetSLen(int l); @@ -285,7 +285,7 @@ public: int Compare(const String0& s) const; - unsigned GetHashValue() const { + hash_t GetHashValue() const { return chr[KIND] ? LHashValue() : (unsigned)CombineHash(w[0], w[1], w[2], w[3]); } @@ -598,7 +598,7 @@ template<> inline String AsString(const String& s) { return s; } template<> -inline unsigned GetHashValue(const String& s) { return s.GetHashValue(); } +inline hash_t GetHashValue(const String& s) { return s.GetHashValue(); } int CompareNoCase(const String& a, const String& b, byte encoding = 0); int CompareNoCase(const String& a, const char *b, byte encoding = 0); @@ -755,7 +755,7 @@ public: int GetLength() const { return length; } int GetAlloc() const { return alloc; } - unsigned GetHashValue() const { return memhash(ptr, length * sizeof(wchar)); } + hash_t GetHashValue() const { return memhash(ptr, length * sizeof(wchar)); } bool IsEqual(const WString0& s) const { return s.length == length && memeq16(ptr, s.ptr, length); } int Compare(const WString0& s) const; @@ -898,7 +898,7 @@ inline bool IsNull(const WString& s) { return s.IsEmpty(); } //inline String AsString(const WString& s) { return s; } template<> -inline unsigned GetHashValue(const WString& s) { return memhash(~s, 2 * s.GetLength()); } +inline hash_t GetHashValue(const WString& s) { return memhash(~s, 2 * s.GetLength()); } WString TrimLeft(const WString& str); WString TrimRight(const WString& s); diff --git a/uppsrc/Core/TimeDate.h b/uppsrc/Core/TimeDate.h index 66e8028f9..315833346 100644 --- a/uppsrc/Core/TimeDate.h +++ b/uppsrc/Core/TimeDate.h @@ -29,7 +29,7 @@ struct Date : RelOps< Date, Moveable > { Date(int y, int m, int d) { day = d; month = m; year = y; } }; -inline unsigned GetHashValue(Date t) { +inline hash_t GetHashValue(Date t) { return 512 * t.year + 32 * t.month + t.day; } @@ -121,7 +121,7 @@ inline Time ToTime(const Date& d) { return Time(d.year, d.month, d.day); } -inline unsigned GetHashValue(Time t) { +inline hash_t GetHashValue(Time t) { return t.second + 32 * (t.minute + 32 * (t.hour + 16 * (t.day + 32 * (t.month + 8 * t.year)))); } diff --git a/uppsrc/Core/Topt.h b/uppsrc/Core/Topt.h index 74d8c0675..ca0c4e829 100644 --- a/uppsrc/Core/Topt.h +++ b/uppsrc/Core/Topt.h @@ -417,20 +417,24 @@ public: unsigned Pow2Bound(unsigned i); unsigned PrimeBound(unsigned i); -unsigned memhash(const void *ptr, size_t size); +hash_t memhash(const void *ptr, size_t size); template -inline unsigned GetHashValue(const T& x) { return x.GetHashValue(); } +inline hash_t GetHashValue(const T& x) { return x.GetHashValue(); } struct CombineHash { - unsigned hash; + hash_t hash; template CombineHash& Do(const T& x) { Put(GetHashValue(x)); return *this; } public: - CombineHash& Put(unsigned h) { hash = (0xacf34ce7 * hash) ^ h; return *this; } +#ifdef HASH64 + CombineHash& Put(hash_t h) { hash = (0xacf34ce7bcf34ce7 * hash) ^ h; return *this; } +#else + CombineHash& Put(hash_t h) { hash = (0xacf34ce7 * hash) ^ h; return *this; } +#endif - operator unsigned() const { return hash; } + operator hash_t() const { return hash; } CombineHash() { hash = 0; } template @@ -445,34 +449,33 @@ public: template CombineHash& operator<<(const T& x) { Do(x); return *this; } }; -template<> inline unsigned GetHashValue(const char& a) { return (unsigned)a; } -template<> inline unsigned GetHashValue(const signed char& a) { return (const unsigned)a; } -template<> inline unsigned GetHashValue(const unsigned char& a) { return (const unsigned)a; } -template<> inline unsigned GetHashValue(const short& a) { return (const unsigned)a; } -template<> inline unsigned GetHashValue(const unsigned short& a) { return (const unsigned)a; } -template<> inline unsigned GetHashValue(const int& a) { return (const unsigned)a; } -template<> inline unsigned GetHashValue(const unsigned int& a) { return (const unsigned)a; } -template<> inline unsigned GetHashValue(const long& a) { return (const unsigned)a; } -template<> inline unsigned GetHashValue(const unsigned long& a) { return (const unsigned)a; } -template<> inline unsigned GetHashValue(const bool& a) { return (const unsigned)a; } -template<> inline unsigned GetHashValue(const wchar_t& a) { return (const unsigned)a; } +template<> inline hash_t GetHashValue(const char& a) { return (hash_t)a; } +template<> inline hash_t GetHashValue(const signed char& a) { return (const hash_t)a; } +template<> inline hash_t GetHashValue(const unsigned char& a) { return (const hash_t)a; } +template<> inline hash_t GetHashValue(const short& a) { return (const hash_t)a; } +template<> inline hash_t GetHashValue(const unsigned short& a) { return (const hash_t)a; } +template<> inline hash_t GetHashValue(const int& a) { return (const hash_t)a; } +template<> inline hash_t GetHashValue(const unsigned int& a) { return (const hash_t)a; } +template<> inline hash_t GetHashValue(const long& a) { return (const hash_t)a; } +template<> inline hash_t GetHashValue(const unsigned long& a) { return (const hash_t)a; } +template<> inline hash_t GetHashValue(const bool& a) { return (const hash_t)a; } +template<> inline hash_t GetHashValue(const wchar_t& a) { return (const hash_t)a; } -template<> inline unsigned GetHashValue(const int64& a) { return CombineHash((unsigned)a, (unsigned)(a >> 32)); } -template<> inline unsigned GetHashValue(const uint64& a) { return GetHashValue((int64)a); } +// TODO: fix for CPU64 +template<> inline hash_t GetHashValue(const int64& a) { return CombineHash((hash_t)a, (hash_t)(a >> 32)); } +template<> inline hash_t GetHashValue(const uint64& a) { return GetHashValue((int64)a); } -unsigned GetHashValue0(const double& a); - -template<> inline unsigned GetHashValue(const double& a) { return GetHashValue0(a); } -template<> inline unsigned GetHashValue(const float& a) { double x = a; return GetHashValue0(x); } +template<> inline hash_t GetHashValue(const double& a) { return memhash(&a, sizeof(a)); } +//template<> inline hash_t GetHashValue(const float& a) { double memhash(&a, sizeof(a)); } #ifdef CPU_32 -inline unsigned GetPtrHashValue(const void *a) { return (int)a; } +inline hash_t GetPtrHashValue(const void *a) { return (int)a; } #else -inline unsigned GetPtrHashValue(const void *a) { return CombineHash((unsigned)(uintptr_t)a); } +inline hash_t GetPtrHashValue(const void *a) { return CombineHash((hash_t)(uintptr_t)a); } #endif template -inline unsigned GetHashValue(T *ptr) { return GetPtrHashValue(reinterpret_cast(ptr)); } +inline hash_t GetHashValue(T *ptr) { return GetPtrHashValue(reinterpret_cast(ptr)); } template struct Data_S_ : Moveable< Data_S_ > diff --git a/uppsrc/Core/Tuple.h b/uppsrc/Core/Tuple.h index ad4c2b04d..e33d43234 100644 --- a/uppsrc/Core/Tuple.h +++ b/uppsrc/Core/Tuple.h @@ -140,7 +140,7 @@ public: bool operator<(const Tuple& x) const { return Compare(x) < 0; } bool operator>(const Tuple& x) const { return Compare(x) > 0; } - unsigned GetHashValue() const { CombineHash h; Base::ToHash(h); return h; } + hash_t GetHashValue() const { CombineHash h; Base::ToHash(h); return h; } void Serialize(Stream& s) { Base::Serialize(s); } diff --git a/uppsrc/Core/Uuid.h b/uppsrc/Core/Uuid.h index f79f5c34e..66c8c7e68 100644 --- a/uppsrc/Core/Uuid.h +++ b/uppsrc/Core/Uuid.h @@ -12,7 +12,7 @@ struct Uuid : AssignValueTypeNo > { Uuid(const Nuller&) { SetNull(); } Uuid() {} - unsigned GetHashValue() const { return CombineHash(v[0], v[1]); } + hash_t GetHashValue() const { return CombineHash(v[0], v[1]); } String ToString() const; String ToStringWithDashes() const; diff --git a/uppsrc/Core/Value.cpp b/uppsrc/Core/Value.cpp index b75c3b134..dae8e1474 100644 --- a/uppsrc/Core/Value.cpp +++ b/uppsrc/Core/Value.cpp @@ -9,7 +9,7 @@ const Nuller Null; ValueTypeError::ValueTypeError(const String& text, const Value& src, int target) : Exc(text), src(src), target(target) {} -unsigned Value::GetOtherHashValue() const { +hash_t Value::GetOtherHashValue() const { if(IsNull()) return 0; byte st = data.GetSt(); @@ -265,7 +265,7 @@ struct SvoVoidFn { static void Serialize(void *p, Stream& s) {} static void Xmlize(void *p, XmlIO& xio) {} static void Jsonize(void *p, JsonIO& jio) {} - static unsigned GetHashValue(const void *p) { return 0; } + static hash_t GetHashValue(const void *p) { return 0; } static bool IsEqual(const void *p1, const void *p2) { return true; } static bool IsPolyEqual(const void *p, const Value& v) { return false; } static String AsString(const void *p) { return String(); } diff --git a/uppsrc/Core/Value.h b/uppsrc/Core/Value.h index b120c3faf..f630b5668 100644 --- a/uppsrc/Core/Value.h +++ b/uppsrc/Core/Value.h @@ -60,7 +60,7 @@ public: void Serialize(Stream& s) { NEVER(); } void Xmlize(XmlIO& xio) { NEVER(); } void Jsonize(JsonIO& jio) { NEVER(); } - unsigned GetHashValue() const { return 0; } + hash_t GetHashValue() const { return 0; } bool operator==(const T&) const { NEVER(); return false; } String ToString() const { return typeid(T).name(); } int Compare(const T&) const { NEVER(); return 0; } @@ -90,7 +90,7 @@ public: virtual void Serialize(Stream& s) {} virtual void Xmlize(XmlIO& xio) {} virtual void Jsonize(JsonIO& jio) {} - virtual unsigned GetHashValue() const { return 0; } + virtual hash_t GetHashValue() const { return 0; } virtual bool IsEqual(const Void *p) { return false; } virtual bool IsPolyEqual(const Value& v) { return false; } virtual String AsString() const { return ""; } @@ -108,7 +108,7 @@ public: void (*Serialize)(void *p, Stream& s); void (*Xmlize)(void *p, XmlIO& xio); void (*Jsonize)(void *p, JsonIO& jio); - unsigned (*GetHashValue)(const void *p); + hash_t (*GetHashValue)(const void *p); bool (*IsEqual)(const void *p1, const void *p2); bool (*IsPolyEqual)(const void *p, const Value& v); String (*AsString)(const void *p); @@ -162,7 +162,7 @@ protected: Date GetOtherDate() const; Time GetOtherTime() const; String GetOtherString() const; - unsigned GetOtherHashValue() const; + hash_t GetOtherHashValue() const; bool IsPolyEqual(const Value& v) const; @@ -253,7 +253,7 @@ public: void Xmlize(XmlIO& xio); void Jsonize(JsonIO& jio); - unsigned GetHashValue() const; + hash_t GetHashValue() const; Value& operator=(const Value& v); Value(const Value& v); @@ -326,7 +326,7 @@ inline bool IsPolyEqual(const T& x, const Value& v) { } template -inline unsigned ValueGetHashValue(const T& x) { +inline hash_t ValueGetHashValue(const T& x) { return UPP::GetHashValue(x); } diff --git a/uppsrc/Core/Value.hpp b/uppsrc/Core/Value.hpp index 439514409..f420b8b84 100644 --- a/uppsrc/Core/Value.hpp +++ b/uppsrc/Core/Value.hpp @@ -90,29 +90,29 @@ inline int PolyCompare(const Time& x, const Value& v) { } template<> -inline unsigned ValueGetHashValue(const bool& x) { +inline hash_t ValueGetHashValue(const bool& x) { return UPP::GetHashValue((int64)x); } template<> -inline unsigned ValueGetHashValue(const int& x) { +inline hash_t ValueGetHashValue(const int& x) { return UPP::GetHashValue((int64)x); } template<> -inline unsigned ValueGetHashValue(const double& x) { +inline hash_t ValueGetHashValue(const double& x) { if(x >= (double)INT64_MIN && x <= (double)INT64_MAX && (int64)x == x) - return UPP::GetHashValue((int64)x); + return UPP::GetHashValue((int64)x); // we want this to be equal to other number types return UPP::GetHashValue(x); } template<> -inline unsigned ValueGetHashValue(const Date& x) { +inline hash_t ValueGetHashValue(const Date& x) { return UPP::GetHashValue(ToTime(x)); } template<> -inline unsigned ValueGetHashValue(const WString& x) { +inline hash_t ValueGetHashValue(const WString& x) { return UPP::GetHashValue(x.ToString()); } @@ -142,7 +142,7 @@ public: virtual void Serialize(Stream& s) { s % this->v; } virtual void Xmlize(XmlIO& xio) { Upp::Xmlize(xio, this->v); } virtual void Jsonize(JsonIO& jio) { Upp::Jsonize(jio, this->v); } - virtual unsigned GetHashValue() const { return UPP::ValueGetHashValue(this->v); } + virtual hash_t GetHashValue() const { return UPP::ValueGetHashValue(this->v); } virtual bool IsEqual(const Value::Void *p) { ASSERT(dynamic_cast *>(p)); return static_cast *>(p)->Get() == this->v; } virtual bool IsPolyEqual(const Value& b) { return UPP::IsPolyEqual(this->v, b); } @@ -163,7 +163,7 @@ struct SvoFn { static void Serialize(void *p, Stream& s) { s % *(T*)p; } static void Xmlize(void *p, XmlIO& xio) { Upp::Xmlize(xio, *(T*)p); } static void Jsonize(void *p, JsonIO& jio) { Upp::Jsonize(jio, *(T*)p); } - static unsigned GetHashValue(const void *p) { return UPP::ValueGetHashValue(*(T*)p); } + static hash_t GetHashValue(const void *p) { return UPP::ValueGetHashValue(*(T*)p); } static bool IsEqual(const void *p1, const void *p2) { return *(T*)p1 == *(T*)p2; } static bool IsPolyEqual(const void *p, const Value& v) { return UPP::IsPolyEqual(*(T*)p, v); } static String AsString(const void *p) { return UPP::AsString(*(T*)p); } @@ -314,7 +314,7 @@ Value::Value(const T& x, VSMALL) } inline -unsigned Value::GetHashValue() const +hash_t Value::GetHashValue() const { return IsString() ? data.GetCount() ? data.GetHashValue() : 0 : GetOtherHashValue(); diff --git a/uppsrc/Core/ValueUtil.cpp b/uppsrc/Core/ValueUtil.cpp index 4bd83e099..0b699cb6c 100644 --- a/uppsrc/Core/ValueUtil.cpp +++ b/uppsrc/Core/ValueUtil.cpp @@ -46,7 +46,7 @@ void ValueArray::Data::Xmlize(XmlIO& io) Upp::Xmlize(io, data); } -unsigned ValueArray::Data::GetHashValue() const +hash_t ValueArray::Data::GetHashValue() const { CombineHash w(data.GetCount()); for(int i = 0; i < data.GetCount(); i++) @@ -333,7 +333,7 @@ void ValueMap::Data::Jsonize(JsonIO& jio) } } -unsigned ValueMap::Data::GetHashValue() const { +hash_t ValueMap::Data::GetHashValue() const { CombineHash w(key.GetCount()); for(int i = 0; i < key.GetCount(); i++) w.Put(key[i].GetHashValue()); diff --git a/uppsrc/Core/ValueUtil.h b/uppsrc/Core/ValueUtil.h index 155d0d44f..abb3ecb0e 100644 --- a/uppsrc/Core/ValueUtil.h +++ b/uppsrc/Core/ValueUtil.h @@ -77,7 +77,7 @@ class Id : Moveable { public: const String& ToString() const { return id; } - dword GetHashValue() const { return UPP::GetHashValue(id); } + hash_t GetHashValue() const { return UPP::GetHashValue(id); } bool IsNull() const { return UPP::IsNull(id); } operator const String&() const { return ToString(); } @@ -205,7 +205,7 @@ class ValueArray : public ValueTypeGetHashValue(); } + hash_t GetHashValue() const { return data->GetHashValue(); } void Serialize(Stream& s); void Jsonize(JsonIO& jio); void Xmlize(XmlIO& xio); @@ -307,7 +307,7 @@ class ValueMap : public ValueType >{ virtual void Serialize(Stream& s); virtual void Xmlize(XmlIO& xio); virtual void Jsonize(JsonIO& jio); - virtual unsigned GetHashValue() const; + virtual hash_t GetHashValue() const; virtual bool IsEqual(const Value::Void *p); virtual String AsString() const; virtual int Compare(const Value::Void *p); @@ -430,7 +430,7 @@ public: Value GetAndClear(const Value& key); - unsigned GetHashValue() const { return data->GetHashValue(); } + hash_t GetHashValue() const { return data->GetHashValue(); } void Serialize(Stream& s); void Jsonize(JsonIO& jio); void Xmlize(XmlIO& xio); diff --git a/uppsrc/Core/Vcont.h b/uppsrc/Core/Vcont.h index 9dbcc9235..53f7d958c 100644 --- a/uppsrc/Core/Vcont.h +++ b/uppsrc/Core/Vcont.h @@ -202,7 +202,7 @@ public: void Xmlize(XmlIO& xio, const char *itemtag = "item"); void Jsonize(JsonIO& jio); String ToString() const; - dword GetHashValue() const { return HashBySerialize(*this); } + hash_t GetHashValue() const { return HashBySerialize(*this); } template bool operator==(const B& b) const { return IsEqualRange(*this, b); } template bool operator!=(const B& b) const { return !operator==(b); } template int Compare(const B& b) const { return CompareRanges(*this, b); } @@ -352,7 +352,7 @@ public: void Xmlize(XmlIO& xio, const char *itemtag = "item"); void Jsonize(JsonIO& jio); String ToString() const; - dword GetHashValue() const { return HashBySerialize(*this); } + hash_t GetHashValue() const { return HashBySerialize(*this); } template bool operator==(const B& b) const { return IsEqualRange(*this, b); } template bool operator!=(const B& b) const { return !operator==(b); } diff --git a/uppsrc/Core/Vcont.hpp b/uppsrc/Core/Vcont.hpp index bd7aa6641..c3ff81bd8 100644 --- a/uppsrc/Core/Vcont.hpp +++ b/uppsrc/Core/Vcont.hpp @@ -22,7 +22,7 @@ void StreamContainer(Stream& s, T& cont) } template -dword HashBySerialize(const T& cont) +hash_t HashBySerialize(const T& cont) { TimeStop tm; xxHashStream xxh; diff --git a/uppsrc/Core/src.tpp/String_en-us.tpp b/uppsrc/Core/src.tpp/String_en-us.tpp index 14f005d05..4a5266f62 100644 --- a/uppsrc/Core/src.tpp/String_en-us.tpp +++ b/uppsrc/Core/src.tpp/String_en-us.tpp @@ -1,5 +1,4 @@ topic "String and WString"; -[2 $$0,0#00000000000000000000000000000000:Default] [i448;a25;kKO9;2 $$1,0#37138531426314131252341829483380:class] [l288;2 $$2,0#27521748481378242620020725143825:desc] [0 $$3,0#96390100711032703541132217272105:end] @@ -9,6 +8,7 @@ topic "String and WString"; [l288;i1121;b17;O9;~~~.1408;2 $$7,0#10431211400427159095818037425705:param] [i448;b42;O9;2 $$8,8#61672508125594000341940100500538:tparam] [b42;2 $$9,9#13035079074754324216151401829390:normal] +[2 $$0,0#00000000000000000000000000000000:Default] [{_}%EN-US [ {{10000@(113.42.0) [s0; [*@7;4 String]]}}&] [s3; &] @@ -409,7 +409,7 @@ grows exponentially, like with Vector or std`::vector).&] contain zero characters).&] [s3; &] [s4;%- &] -[s5;:WString0`:`:GetHashValue`(`)const:%- [@(0.0.255) unsigned]_[* GetHashValue]()_[@(0.0.255) c +[s5;:WString0`:`:GetHashValue`(`)const:%- [@(0.0.255) hash`_t]_[* GetHashValue]()_[@(0.0.255) c onst]&] [s2; Returns the hash value of the string.&] [s3;%- &] diff --git a/uppsrc/Core/t.cpp b/uppsrc/Core/t.cpp index 2904b833f..a41c31651 100644 --- a/uppsrc/Core/t.cpp +++ b/uppsrc/Core/t.cpp @@ -32,7 +32,7 @@ static bool sIdEq(const char *a, const char *b) struct CharS : Moveable { const char *s; - unsigned GetHashValue() const { return memhash(s, sIdLen(s)); } + hash_t GetHashValue() const { return memhash(s, sIdLen(s)); } bool operator==(const CharS& b) const { return sIdEq(s, b.s); } }; @@ -186,7 +186,7 @@ char *ZoneAlloc::Alloc(int sz) } template<> -inline unsigned GetHashValue(const char * const &s) +inline hash_t GetHashValue(const char * const &s) { return GetPtrHashValue(s); } diff --git a/uppsrc/CtrlCore/CtrlCore.h b/uppsrc/CtrlCore/CtrlCore.h index b50d3c595..29fe16d31 100644 --- a/uppsrc/CtrlCore/CtrlCore.h +++ b/uppsrc/CtrlCore/CtrlCore.h @@ -1371,9 +1371,9 @@ inline Ctrl *operator<<(Ctrl *parent, Ctrl& child) return parent; } -inline unsigned GetHashValue(Ctrl *x) +inline hash_t GetHashValue(Ctrl *x) { - return (unsigned)(intptr_t)x; + return (hash_t)(intptr_t)x; } String GetKeyDesc(dword key); diff --git a/uppsrc/CtrlCore/ImageX11.cpp b/uppsrc/CtrlCore/ImageX11.cpp index 0f71ac7d4..a10f1b1d9 100644 --- a/uppsrc/CtrlCore/ImageX11.cpp +++ b/uppsrc/CtrlCore/ImageX11.cpp @@ -125,7 +125,7 @@ inline int s255d16(int x) static XPicture sGetSolidFill(Color c) { - int q = GetHashValue(c) % (int)XRSolidFillCount; + int q = FoldHash(GetHashValue(c)) % (int)XRSolidFillCount; XRSolidFill& f = sFill[q]; if(f.color == c && f.picture) return f.picture; diff --git a/uppsrc/CtrlCore/Win32Gui.h b/uppsrc/CtrlCore/Win32Gui.h index 8f91ca3ff..6cf156c70 100644 --- a/uppsrc/CtrlCore/Win32Gui.h +++ b/uppsrc/CtrlCore/Win32Gui.h @@ -346,9 +346,9 @@ public: namespace Upp { -inline unsigned GetHashValue(const HWND& hwnd) +inline hash_t GetHashValue(const HWND& hwnd) { - return (unsigned)(intptr_t)hwnd; + return (hash_t)(intptr_t)hwnd; } } diff --git a/uppsrc/CtrlCore/Win32Wnd.cpp b/uppsrc/CtrlCore/Win32Wnd.cpp index 386dced0a..c981d171e 100644 --- a/uppsrc/CtrlCore/Win32Wnd.cpp +++ b/uppsrc/CtrlCore/Win32Wnd.cpp @@ -15,9 +15,9 @@ namespace Upp { #define ELOG(x) // RLOG(GetSysTime() << ": " << x) template<> -unsigned GetHashValue(const HWND& h) +hash_t GetHashValue(const HWND& h) { - return (unsigned)(intptr_t)h; + return (hash_t)(intptr_t)h; } bool Ctrl::GetMsg(MSG& msg) diff --git a/uppsrc/CtrlLib/ChWin32.cpp b/uppsrc/CtrlLib/ChWin32.cpp index 8592f5ed2..b90ba028a 100644 --- a/uppsrc/CtrlLib/ChWin32.cpp +++ b/uppsrc/CtrlLib/ChWin32.cpp @@ -52,7 +52,7 @@ struct XpElement : Moveable { return e.widget == widget && e.part == part && e.state == state; } - unsigned GetHashValue() const { return widget ^ part ^ state; } + hash_t GetHashValue() const { return widget ^ part ^ state; } XpElement() { contentm = whista = false; } }; diff --git a/uppsrc/Draw/Display.h b/uppsrc/Draw/Display.h index f9c5425f3..e2162f7b9 100644 --- a/uppsrc/Draw/Display.h +++ b/uppsrc/Draw/Display.h @@ -60,9 +60,9 @@ struct AttrText : public ValueType > { bool operator==(const AttrText& f) const; bool operator!=(const AttrText& f) const { return !operator==(f); } - dword GetHashValue() const { return CombineHash(text, font, ink, paper); } - bool IsNullInstance() const { return IsNull(text); } - void SetNull() { Init(); img = Null; text = Null; } + hash_t GetHashValue() const { return CombineHash(text, font, ink, paper); } + bool IsNullInstance() const { return IsNull(text); } + void SetNull() { Init(); img = Null; text = Null; } String ToString() const { return AsString(value); } int Compare(const AttrText& x) const { return value.Compare(x.value); } diff --git a/uppsrc/Draw/Draw.h b/uppsrc/Draw/Draw.h index 5f4bb93ef..11e7b08d1 100644 --- a/uppsrc/Draw/Draw.h +++ b/uppsrc/Draw/Draw.h @@ -193,9 +193,9 @@ public: v.width == f.v.width && v.height == f.v.height; } bool operator!=(Font f) const { return !operator==(f); } - dword GetHashValue() const { return CombineHash(v.width, v.flags, v.height, v.face); } - bool IsNullInstance() const { return v.face == 0xffff; } - void SetNull() { v.face = 0xffff; v.height = v.width = 0; v.flags = 0; } + hash_t GetHashValue() const { return CombineHash(v.width, v.flags, v.height, v.face); } + bool IsNullInstance() const { return v.face == 0xffff; } + void SetNull() { v.face = 0xffff; v.height = v.width = 0; v.flags = 0; } Font() { v.height = v.width = 0; v.face = v.flags = 0; } Font(int face, int height) { v.face = face; v.height = height; v.flags = 0; v.width = 0; } Font(const Nuller&) { SetNull(); } @@ -394,7 +394,7 @@ public: bool IsNullInstance() const { return cmd.IsEmpty(); } void SetNull() { size = Null; } bool operator==(const Painting& b) const { return cmd == b.cmd && data == b.data && size == b.size; } - unsigned GetHashValue() const { return CombineHash(cmd, data); } + hash_t GetHashValue() const { return CombineHash(cmd, data); } String ToString() const { return "painting " + AsString(size); } operator Value() const { return RichToValue(*this); } @@ -668,7 +668,7 @@ public: bool operator==(const Drawing& b) const { return val == b.val && data == b.data && size == b.size; } String ToString() const { return "drawing " + AsString(size); } - unsigned GetHashValue() const { return CombineHash(data, val); } + hash_t GetHashValue() const { return CombineHash(data, val); } operator Value() const { return RichToValue(*this); } Drawing(const Value& src) { *this = src.Get(); } diff --git a/uppsrc/Draw/Font.cpp b/uppsrc/Draw/Font.cpp index 2a6e31382..dddcc5d90 100644 --- a/uppsrc/Draw/Font.cpp +++ b/uppsrc/Draw/Font.cpp @@ -391,7 +391,7 @@ struct CharEntry { CharEntry fc_cache_global[4093]; -inline dword GlyphHash(Font font, int chr) +inline hash_t GlyphHash(Font font, int chr) { return FoldHash(CombineHash(font.GetHashValue(), chr)); } @@ -455,7 +455,7 @@ thread_local FontEntry fi_cache[63]; const CommonFontInfo& GetFontInfo(Font font) { font.RealizeStd(); - unsigned hash = FoldHash(font.GetHashValue()) % 63; + dword hash = FoldHash(font.GetHashValue()) % 63; FontEntry& e = fi_cache[hash]; if(e.font != font.AsInt64()) { Mutex::Lock __(sFontLock); diff --git a/uppsrc/Draw/Image.cpp b/uppsrc/Draw/Image.cpp index df442741d..f63e767a4 100644 --- a/uppsrc/Draw/Image.cpp +++ b/uppsrc/Draw/Image.cpp @@ -272,13 +272,14 @@ INITBLOCK { bool Image::operator==(const Image& img) const { + static_assert(sizeof(RGBA) == 4, "sizeof(RGBA)"); return IsSame(img) || GetSize() == img.GetSize() && GetHotSpot() == img.GetHotSpot() && Get2ndSpot() == img.Get2ndSpot() && GetDots() == img.GetDots() && GetResolution() == img.GetResolution() && - memcmp(~*this, ~img, GetLength() * sizeof(RGBA)) == 0; + memeq32(~*this, ~img, GetLength() * sizeof(RGBA)); } bool Image::operator!=(const Image& img) const @@ -286,7 +287,7 @@ bool Image::operator!=(const Image& img) const return !operator==(img); } -dword Image::GetHashValue() const +hash_t Image::GetHashValue() const { return memhash(~*this, GetLength() * sizeof(RGBA)); } diff --git a/uppsrc/Draw/Image.h b/uppsrc/Draw/Image.h index cfff29aea..141e60c09 100644 --- a/uppsrc/Draw/Image.h +++ b/uppsrc/Draw/Image.h @@ -200,7 +200,7 @@ public: bool operator==(const Image& img) const; bool operator!=(const Image& img) const; - dword GetHashValue() const; + hash_t GetHashValue() const; String ToString() const; void Serialize(Stream& s); diff --git a/uppsrc/Painter/RenderChar.cpp b/uppsrc/Painter/RenderChar.cpp index 00275e92c..d727fec68 100644 --- a/uppsrc/Painter/RenderChar.cpp +++ b/uppsrc/Painter/RenderChar.cpp @@ -67,7 +67,7 @@ struct GlyphKey { bool operator==(const GlyphKey& b) const { return fnt == b.fnt && chr == b.chr && tolerance == b.tolerance; } - unsigned GetHashValue() const { + hash_t GetHashValue() const { return CombineHash(fnt, chr, tolerance); } };