Spinning Topp Logo BlackTopp Studios
inc
hashedstring.h
Go to the documentation of this file.
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_h
41 #define _hashedstring_h
42 
43 #include "datatypes.h"
44 #include "XML/xml.h"
45 
46 /// @file
47 /// @brief This implemenation of
48 
49 namespace Mezzanine
50 {
51 
52 #ifndef HASHED_STRING_32_SEED
53 /// @def HASHED_STRING_32_SEED
54 /// @brief A value used to tweak the hash function for maximum randomness.
55 #define HASHED_STRING_32_SEED 1
56 #endif
57 
58  ///////////////////////////////////////////////////////////////////////////////
59  /// @brief An identifier that compares as fast as an integer but has a string.
60  /// @details This computes a MurmurA(x86 32bit) hash on creation. This is used
61  /// in all comparisons, this means that in excedingly rare situations that
62  /// hash collisions could cause silent failure. @n @n
63  /// Internally programming la
64  ///////////////////////////////////////
66  {
67  public:
68  ///////////////////////////////////////////////////////////////////////////////
69  // Utility
70 
71  /// @brief Compute a MurmurA aka MurmurX86-32bit has of a string and return it.
72  /// @param ToBeHashed The string to hash
73  /// @return A 32 bit value approximating unique, which represents the passed string.
74  static Int32 Murmur32bit(const String& ToBeHashed);
75 
76  protected:
77  ///////////////////////////////////////////////////////////////////////////////
78  // Data
79 
80  /// The Actual string to store.
82  /// @brief The computed hash.
84 
85  public:
86  ///////////////////////////////////////////////////////////////////////////////
87  // Creation
88 
89  /// @brief NonComputing Constructor
90  /// @details Skip the hashing overhead and just do the work for h
92  /// @brief String Constructor
93  /// @param StartingString The String to work with
94  /// @details Compute the hash of the String and hang onto both for future use.
95  HashedString32(const String& StartingString);
96  /// @brief Precomputed Constructor
97  /// @param StartingString The String to work with
98  /// @param PrecomputedHash The value
99  /// @details Does not Compute the hash of the String, Just trusts whatever it is given.
100  HashedString32(const String& StartingString, Int32 PrecomputedHash);
101  /// @brief Copy constructor
102  /// @param StartingString The other hashed string
103  HashedString32(const HashedString32& StartingString);
104 
105  ///////////////////////////////////////////////////////////////////////////////
106  // Comparisons
107 
108  /// @brief Compare The hash of this and another string for equality.
109  /// @param Other The other string to compare this one too.
110  /// @return True if the hashes are the same.
111  /// @note It is imcredibly unlikely, but this could return true in the event of a hash collision.
112  bool operator==(const HashedString32& Other) const;
113  /// @brief Compare The hash of this and another string for inequality.
114  /// @param Other The other string to compare this one too.
115  /// @return True if the hashes are different.
116  bool operator!=(const HashedString32& Other) const;
117  /// @brief Provide a semantically useless, but quite fast sorting order for std containers based on only hashes.
118  /// @param Other The other string to compare this one too.
119  /// @return true If the Hash of this is smaller than the hash of the other
120  bool operator<(const HashedString32& Other) const;
121  /// @brief Provide a semantically useless, but quite fast sorting order for std containers based on only hashes.
122  /// @param Other The other string to compare this one too.
123  /// @return true If the Hash of this is larger than the hash of the other
124  bool operator>(const HashedString32& Other) const;
125 
126 
127  ///////////////////////////////////////////////////////////////////////////////
128  // Data Modifiers/observers
129 
130  /// @brief Get the 32 bit as an Int32.
131  /// @return an Int32 with the hash in it.
132  Int32 GetHash() const;
133  /// @brief Get the string that was used to calculate the hash.
134  /// @return A Mezzanine::String
135  String GetString() const;
136  /// @brief Set a new string and recalculate its hash.
137  /// @param NewString The data to hash and store for later use.
138  void SetString(const String& NewString);
139 
140  ///////////////////////////////////////////////////////////////////////////////
141  // Serialization
142 
143  /// @brief Convert this class to an XML::Node ready for serialization
144  /// @param CurrentRoot The point in the XML hierarchy that all this HashedString32 should be appended to.
145  void ProtoSerialize(XML::Node& CurrentRoot) const;
146  /// @brief Take the data stored in an XML and overwrite this instance of this object with it
147  /// @param OneNode and XML::Node containing the data.
148  void ProtoDeSerialize(const XML::Node& OneNode);
149  /// @brief Get the name of the the XML tag this class will leave behind as its instances are serialized.
150  /// @return A string containing "HashedString32"
151  static String GetSerializableName();
152  }; // HashedString32
153 
154  ///////////////////////////////////////////////////////////////////////////////
155  // Class External << Operators for streaming or assignment
156 
157  /// @brief Send a HashedString32 down a stream serialized.
158  /// @param stream The std::ostream to send it down.
159  /// @param x The HashedString32 to Send
160  std::ostream& MEZZ_LIB operator << (std::ostream& stream, const Mezzanine::HashedString32& x);
161 }//Mezzanine
162 
163 
164 #endif
int32_t Int32
An 32-bit integer.
Definition: datatypes.h:124
All the definitions for datatypes as well as some basic conversion functions are defined here...
Int32 Hash
The computed hash.
Definition: hashedstring.h:83
std::ostream & operator<<(std::ostream &stream, const Mezzanine::HashedString32 &x)
Send a HashedString32 down a stream serialized.
An identifier that compares as fast as an integer but has a string.
Definition: hashedstring.h:65
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
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