Просмотр исходного кода

Moved StringHash to Math library.

Lasse Öörni 13 лет назад
Родитель
Сommit
bdb060bd7d

+ 6 - 6
Engine/Core/Variant.h

@@ -171,14 +171,14 @@ public:
     Variant(const StringHash& value) :
         type_(VAR_NONE)
     {
-        *this = (int)value.GetValue();
+        *this = (int)value.Value();
     }
     
     /// Construct from a short string hash (convert to integer.)
     Variant(const ShortStringHash& value) :
         type_(VAR_NONE)
     {
-        *this = (int)value.GetValue();
+        *this = (int)value.Value();
     }
     
     /// Construct from a bool.
@@ -356,7 +356,7 @@ public:
     Variant& operator = (const StringHash& rhs)
     {
         SetType(VAR_INT);
-        value_.int_ = (int)rhs.GetValue();
+        value_.int_ = (int)rhs.Value();
         return *this;
     }
     
@@ -364,7 +364,7 @@ public:
     Variant& operator = (const ShortStringHash& rhs)
     {
         SetType(VAR_INT);
-        value_.int_ = (int)rhs.GetValue();
+        value_.int_ = (int)rhs.Value();
         return *this;
     }
     
@@ -641,7 +641,7 @@ public:
     bool operator == (const StringHash& rhs) const
     {
         if (type_ == VAR_INT)
-            return (unsigned)value_.int_ == rhs.GetValue();
+            return (unsigned)value_.int_ == rhs.Value();
         else
             return false;
     }
@@ -650,7 +650,7 @@ public:
     bool operator == (const ShortStringHash& rhs) const
     {
         if (type_ == VAR_INT)
-            return (unsigned short)value_.int_ == rhs.GetValue();
+            return (unsigned short)value_.int_ == rhs.Value();
         else
             return false;
     }

+ 2 - 2
Engine/Engine/CoreAPI.cpp

@@ -105,7 +105,7 @@ static void RegisterStringHash(asIScriptEngine* engine)
     engine->RegisterObjectMethod("StringHash", "int opCmp(const StringHash&in) const", asFUNCTION(StringHashCmp), asCALL_CDECL_OBJFIRST);
     engine->RegisterObjectMethod("StringHash", "StringHash opAdd(const StringHash&in) const", asMETHOD(StringHash, operator +), asCALL_THISCALL);
     engine->RegisterObjectMethod("StringHash", "String ToString() const", asMETHOD(StringHash, ToString), asCALL_THISCALL);
-    engine->RegisterObjectMethod("StringHash", "uint get_value()", asMETHOD(StringHash, GetValue), asCALL_THISCALL);
+    engine->RegisterObjectMethod("StringHash", "uint get_value()", asMETHOD(StringHash, Value), asCALL_THISCALL);
     
     engine->RegisterObjectType("ShortStringHash", sizeof(ShortStringHash), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CAK);
     engine->RegisterObjectBehaviour("ShortStringHash", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructShortStringHash), asCALL_CDECL_OBJLAST);
@@ -117,7 +117,7 @@ static void RegisterStringHash(asIScriptEngine* engine)
     engine->RegisterObjectMethod("ShortStringHash", "bool opEquals(const ShortStringHash&in) const", asMETHOD(ShortStringHash, operator ==), asCALL_THISCALL);
     engine->RegisterObjectMethod("ShortStringHash", "int opCmp(const ShortStringHash&in) const", asFUNCTION(ShortStringHashCmp), asCALL_CDECL_OBJFIRST);
     engine->RegisterObjectMethod("ShortStringHash", "String ToString() const", asMETHOD(ShortStringHash, ToString), asCALL_THISCALL);
-    engine->RegisterObjectMethod("ShortStringHash", "uint16 get_value()", asMETHOD(ShortStringHash, GetValue), asCALL_THISCALL);
+    engine->RegisterObjectMethod("ShortStringHash", "uint16 get_value()", asMETHOD(ShortStringHash, Value), asCALL_THISCALL);
 }
 
 static void ConstructResourceRef(ResourceRef* ptr)

+ 2 - 2
Engine/IO/Serializer.cpp

@@ -166,12 +166,12 @@ bool Serializer::WriteFileID(const String& value)
 
 bool Serializer::WriteStringHash(const StringHash& value)
 {
-    return WriteUInt(value.GetValue());
+    return WriteUInt(value.Value());
 }
 
 bool Serializer::WriteShortStringHash(const ShortStringHash& value)
 {
-    return WriteUShort(value.GetValue());
+    return WriteUShort(value.Value());
 }
 
 bool Serializer::WriteBuffer(const PODVector<unsigned char>& value)

+ 90 - 0
Engine/Math/StringHash.cpp

@@ -0,0 +1,90 @@
+//
+// Urho3D Engine
+// Copyright (c) 2008-2012 Lasse Öörni
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#include "Precompiled.h"
+#include "MathDefs.h"
+#include "StringHash.h"
+
+#include <cstdio>
+
+#include "DebugNew.h"
+
+const StringHash StringHash::ZERO;
+const ShortStringHash ShortStringHash::ZERO;
+
+StringHash::StringHash(const char* str) :
+    value_(Calculate(str))
+{
+}
+
+StringHash::StringHash(const String& str) :
+    value_(Calculate(str.CString()))
+{
+}
+
+unsigned StringHash::Calculate(const char* str)
+{
+    unsigned hash = 0;
+    
+    if (!str)
+        return hash;
+    
+    while (*str)
+    {
+        // Perform the actual hashing as case-insensitive
+        char c = *str;
+        hash = SDBMHash(hash, tolower(c));
+        ++str;
+    }
+    
+    return hash;
+}
+
+String StringHash::ToString() const
+{
+    char tempBuffer[CONVERSION_BUFFER_LENGTH];
+    sprintf(tempBuffer, "%08X", value_);
+    return String(tempBuffer);
+}
+
+ShortStringHash::ShortStringHash(const char* str) :
+    value_(Calculate(str))
+{
+}
+
+ShortStringHash::ShortStringHash(const String& str) :
+    value_(Calculate(str.CString()))
+{
+}
+
+unsigned short ShortStringHash::Calculate(const char* str)
+{
+    return StringHash::Calculate(str);
+}
+
+String ShortStringHash::ToString() const
+{
+    char tempBuffer[CONVERSION_BUFFER_LENGTH];
+    sprintf(tempBuffer, "%04X", value_);
+    return String(tempBuffer);
+}

+ 190 - 0
Engine/Math/StringHash.h

@@ -0,0 +1,190 @@
+//
+// Urho3D Engine
+// Copyright (c) 2008-2012 Lasse Öörni
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#pragma once
+
+#include "Str.h"
+
+/// 32-bit hash value for a string.
+class StringHash
+{
+public:
+    /// Construct with zero value.
+    StringHash() :
+        value_(0)
+    {
+    }
+    
+    /// Copy-construct from another hash.
+    StringHash(const StringHash& rhs) :
+        value_(rhs.value_)
+    {
+    }
+    
+    /// Construct with an initial value.
+    explicit StringHash(unsigned value) :
+        value_(value)
+    {
+    }
+    
+    /// Construct from a C string case-insensitively.
+    explicit StringHash(const char* str);
+    
+    /// Construct from a string case-insensitively.
+    explicit StringHash(const String& str);
+    
+    /// Assign from another hash.
+    StringHash& operator = (const StringHash& rhs)
+    {
+        value_ = rhs.value_;
+        return *this;
+    }
+    
+    /// Add a hash.
+    StringHash operator + (const StringHash& rhs) const
+    {
+        StringHash ret;
+        ret.value_ = value_ + rhs.value_;
+        return ret;
+    }
+    
+    /// Add-assign a hash.
+    StringHash& operator += (const StringHash& rhs)
+    {
+        value_ += rhs.value_;
+        return *this;
+    }
+    
+    // Test for equality with another hash.
+    bool operator == (const StringHash& rhs) const { return value_ == rhs.value_; }
+    /// Test for inequality with another hash.
+    bool operator != (const StringHash& rhs) const { return value_ != rhs.value_; }
+    /// Test if less than another hash.
+    bool operator < (const StringHash& rhs) const { return value_ < rhs.value_; }
+    /// Test if greater than another hash.
+    bool operator > (const StringHash& rhs) const { return value_ > rhs.value_; }
+    /// Return true if nonzero hash value.
+    operator bool () const { return value_ != 0; }
+    /// Return hash value.
+    unsigned Value() const { return value_; }
+    /// Return as string.
+    String ToString() const;
+    /// Return hash value for HashSet & HashMap.
+    unsigned ToHash() const { return value_; }
+    
+    /// Calculate hash value case-insensitively from a C string.
+    static unsigned Calculate(const char* str);
+    
+    /// Zero hash.
+    static const StringHash ZERO;
+    
+private:
+    /// Hash value.
+    unsigned value_;
+};
+
+/// 16-bit hash value for a string.
+class ShortStringHash
+{
+public:
+    /// Construct with zero hash value.
+    ShortStringHash() :
+        value_(0)
+    {
+    }
+    
+    /// Copy-construct from another hash value.
+    ShortStringHash(const ShortStringHash& rhs) :
+        value_(rhs.value_)
+    {
+    }
+    
+    /// Copy-construct from another 32-bit hash value (ignore the high bits.)
+    explicit ShortStringHash(const StringHash& rhs) :
+        value_(rhs.Value())
+    {
+    }
+    
+    /// Construct with an initial value.
+    explicit ShortStringHash(unsigned short value) :
+        value_(value)
+    {
+    }
+
+    /// Construct from a C string case-insensitively.
+    explicit ShortStringHash(const char* str);
+    /// Construct from a string case-insensitively.
+    explicit ShortStringHash(const String& str);
+    
+    /// Assign from another hash.
+    ShortStringHash& operator = (const ShortStringHash& rhs)
+    {
+        value_ = rhs.value_;
+        return *this;
+    }
+    
+    /// Add a hash.
+    ShortStringHash operator + (const ShortStringHash& rhs) const
+    {
+        ShortStringHash ret;
+        ret.value_ = value_ + rhs.value_;
+        return ret;
+    }
+    
+    // Add-assign a hash.
+    ShortStringHash& operator += (const ShortStringHash& rhs)
+    {
+        value_ += rhs.value_;
+        return *this;
+    }
+    
+    /// Test for equality with another hash.
+    bool operator == (const ShortStringHash& rhs) const { return value_ == rhs.value_; }
+    /// Test for inequality with another hash.
+    bool operator != (const ShortStringHash& rhs) const { return value_ != rhs.value_; }
+    /// Test if less than another hash.
+    bool operator < (const ShortStringHash& rhs) const { return value_ < rhs.value_; }
+    /// Test if greater than another hash.
+    bool operator > (const ShortStringHash& rhs) const { return value_ > rhs.value_; }
+    /// Return true if nonzero hash value.
+    operator bool () const { return value_ != 0; }
+    /// Return hash value.
+    unsigned short Value() const { return value_; }
+    /// Return as string.
+    String ToString() const;
+    /// Return hash value for HashSet & HashMap.
+    unsigned ToHash() const { return value_; }
+    
+    /// Calculate hash value case-insensitively from a C string.
+    static unsigned short Calculate(const char* str);
+    
+    /// Zero hash.
+    static const ShortStringHash ZERO;
+    
+private:
+    /// Hash value.
+    unsigned short value_;
+};
+
+#define HASH(str) (StringHash(#str))
+#define SHORTHASH(str) (ShortStringHash(#str))

+ 1 - 1
Engine/Resource/XMLElement.cpp

@@ -304,7 +304,7 @@ bool XMLElement::SetVariantMap(const VariantMap& value)
         XMLElement variantElem = CreateChild("variant");
         if (!variantElem)
             return false;
-        variantElem.SetInt("hash", i->first_.GetValue());
+        variantElem.SetInt("hash", i->first_.Value());
         variantElem.SetVariant(i->second_);
     }