Spinning Topp Logo BlackTopp Studios
inc
hashedstring.cpp
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _HashedString_cpp
41 #define _HashedString_cpp
42 
43 #include "hashedstring.h"
44 #include "Internal/murmurhash3.h.cpp"
45 #include "serialization.h"
46 #include "exception.h"
47 
48 namespace Mezzanine
49 {
51  {
52  Int32 Results = 0;
53  Internal::MurmurHash3_x86_32(ToBeHashed.data(), ToBeHashed.size(), HASHED_STRING_32_SEED, &Results );
54  return Results;
55  }
56 
58  Hash(0)
59  {}
60 
61  HashedString32::HashedString32(const String& StartingString) :
62  TheString(StartingString), Hash(Murmur32bit(StartingString))
63  {}
64 
65  HashedString32::HashedString32(const String& StartingString, Int32 PrecomputedHash) :
66  TheString(StartingString), Hash(PrecomputedHash)
67  {}
68 
70  TheString(StartingString.TheString), Hash(StartingString.Hash)
71  {}
72 
73  bool HashedString32::operator==(const HashedString32& Other) const
74  {
75  //if(Hash==Other.Hash)
76  // { return TheString == Other.TheString; }
77  //return false;
78  return Hash==Other.Hash;
79  }
80 
81  bool HashedString32::operator!=(const HashedString32& Other) const
82  {
83  //if(Hash!=Other.Hash)
84  // { return TheString != Other.TheString; }
85  //return false;
86  return Hash!=Other.Hash;
87  }
88 
89  bool HashedString32::operator<(const HashedString32& Other) const
90  {
91  //if(Hash==Other.Hash)
92  // { return TheString < Other.TheString; }
93  return Hash < Other.Hash;
94  }
95 
96  bool HashedString32::operator>(const HashedString32& Other) const
97  {
98  //if(Hash==Other.Hash)
99  // { return TheString > Other.TheString; }
100  return Hash > Other.Hash;
101  }
102 
103 
105  { return Hash; }
106 
108  { return TheString; }
109 
110  void HashedString32::SetString(const String& NewString)
111  {
112  TheString = NewString;
114  }
115 
116  void HashedString32::ProtoSerialize(XML::Node& CurrentRoot) const
117  {
118  Mezzanine::XML::Node Hash32Node = CurrentRoot.AppendChild(GetSerializableName());
119 
120  if(Hash32Node)
121  {
122  Mezzanine::XML::Attribute VersionAttr = Hash32Node.AppendAttribute("Version");
123  Mezzanine::XML::Attribute HashAttr = Hash32Node.AppendAttribute("Hash");
124  Mezzanine::XML::Attribute StringAttr = Hash32Node.AppendAttribute("String");
125 
126  if( VersionAttr && HashAttr && StringAttr )
127  {
128  if( VersionAttr.SetValue("1") && HashAttr.SetValue(ToString(Hash)) && StringAttr.SetValue(TheString))
129  {
130  return;
131  }else{
132  SerializeError("Create XML Attribute Values", GetSerializableName(),true);
133  }
134  }else{
135  SerializeError("Create XML Attributes", GetSerializableName(),true);
136  }
137  }else{
138  SerializeError("Create XML Serialization Node", GetSerializableName(),true);
139  }
140  }
141 
143  {
145  {
146  if(OneNode.GetAttribute("Version").AsInt() == 1)
147  {
148  Hash=OneNode.GetAttribute("Hash").AsInteger();
149  TheString=OneNode.GetAttribute("String").AsString();
150  }else{
151  MEZZ_EXCEPTION(ExceptionBase::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + GetSerializableName() + ": Not Version 1.");
152  }
153  }else{
154  MEZZ_EXCEPTION(ExceptionBase::II_IDENTITY_INVALID_EXCEPTION,"Attempting to deserialize a " + GetSerializableName() + ", found a " + String(OneNode.Name()) + ".");
155  }
156  }
157 
159  { return String("HashedString32"); }
160 
161  ///////////////////////////////////////////////////////////////////////////////
162  // Class External << Operators for streaming or assignment
163 
164  std::ostream& operator << (std::ostream& stream, const Mezzanine::HashedString32& x)
165  {
166  Serialize(stream,x);
167  return stream;
168  }
169 }//Mezzanine
170 
171 
172 
173 #endif
int32_t Int32
An 32-bit integer.
Definition: datatypes.h:124
#define HASHED_STRING_32_SEED
A value used to tweak the hash function for maximum randomness.
Definition: hashedstring.h:55
Attribute AppendAttribute(const Char8 *Name)
Creates an Attribute and puts it at the end of this Nodes attributes.
static String GetSerializableName()
Get the name of the the XML tag this class will leave behind as its instances are serialized...
A light-weight handle for manipulating attributes in DOM tree.
Definition: attribute.h:74
static Int32 Murmur32bit(const String &ToBeHashed)
Compute a MurmurA aka MurmurX86-32bit has of a string and return it.
String ToString(const T &Datum)
Converts whatever to a String as long as a streaming operator is available for it.
Definition: datatypes.h:242
bool operator==(const HashedString32 &Other) const
Compare The hash of this and another string for equality.
String GetString() const
Get the string that was used to calculate the hash.
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
std::ostream & Serialize(std::ostream &Stream, const T &Converted, const String &Indent=String(""))
Convert any class that supports serialization or has a serializer to a string of chars in a stream...
Thrown when a version is accessed/parsed/required and it cannot work correctly or is missing...
Definition: exception.h:112
const Char8 * AsString(const Char8 *def="") const
Attempts to convert the value of the attribute to a String and returns the results.
bool operator<(const HashedString32 &Other) const
Provide a semantically useless, but quite fast sorting order for std containers based on only hashes...
Int32 Hash
The computed hash.
Definition: hashedstring.h:83
void SetString(const String &NewString)
Set a new string and recalculate its hash.
void ProtoSerialize(XML::Node &CurrentRoot) const
Convert this class to an XML::Node ready for serialization.
This implements the exception hiearchy for Mezzanine.
Int32 GetHash() const
Get the 32 bit as an Int32.
This implemenation of.
std::ostream & operator<<(std::ostream &stream, const Mezzanine::HashedString32 &x)
Send a HashedString32 down a stream serialized.
The interface for serialization.
An identifier that compares as fast as an integer but has a string.
Definition: hashedstring.h:65
bool SetValue(const Char8 *rhs)
Set the value of this.
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
int AsInt(int def=0) const
Attempts to convert the value of the attribute to an int and returns the results. ...
Integer AsInteger(Integer def=0) const
Attempts to convert the value of the attribute to a Integer and returns the results.
bool operator!=(const HashedString32 &Other) const
Compare The hash of this and another string for inequality.
bool operator>(const HashedString32 &Other) const
Provide a semantically useless, but quite fast sorting order for std containers based on only hashes...
Thrown when the identity string wasn't valid at all.
Definition: exception.h:93
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
void ProtoDeSerialize(const XML::Node &OneNode)
Take the data stored in an XML and overwrite this instance of this object with it.
const Char8 * Name() const
ptrdiff_tGet the name of this Node.
HashedString32()
NonComputing Constructor.
void SerializeError(const String &FailedTo, const String &ClassName, Boole SOrD)
Simply does some string concatenation, then throws an Exception.
Node AppendChild(NodeType Type=NodeElement)
Creates a Node and makes it a child of this one.
String TheString
The Actual string to store.
Definition: hashedstring.h:81
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
Attribute GetAttribute(const Char8 *Name) const
Attempt to get an Attribute on this Node with a given name.