• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2 
3  @File         PVRTStringHash.cpp
4 
5  @Title        String Hash
6 
7  @Version
8 
9  @Copyright    Copyright (c) Imagination Technologies Limited.
10 
11  @Platform     All
12 
13  @Description  Inherits from PVRTString to include PVRTHash functionality for
14                quick string compares.
15 
16 ******************************************************************************/
17 #include "PVRTGlobal.h"
18 #include "PVRTStringHash.h"
19 
20 /*!***********************************************************************
21 @Function			CPVRTString
22 @Input				_Ptr	A string
23 @Input				_Count	Length of _Ptr
24 @Description		Constructor
25 ************************************************************************/
CPVRTStringHash(const char * _Ptr,size_t _Count)26 CPVRTStringHash::CPVRTStringHash(const char* _Ptr, size_t _Count) :
27 	m_String(_Ptr, _Count)
28 {
29 	m_Hash = CPVRTHash::MakeHash(m_String);
30 }
31 
32 /*!***********************************************************************
33 @Function			CPVRTString
34 @Input				_Right	A string
35 @Description		Constructor
36 ************************************************************************/
CPVRTStringHash(const CPVRTString & _Right)37 CPVRTStringHash::CPVRTStringHash(const CPVRTString& _Right) :
38 	m_String(_Right)
39 {
40 	m_Hash = CPVRTHash::MakeHash(m_String);
41 }
42 
43 /*!***********************************************************************
44 @Function			CPVRTString
45 @Description		Constructor
46 ************************************************************************/
CPVRTStringHash()47 CPVRTStringHash::CPVRTStringHash()
48 {
49 }
50 
51 /*!***********************************************************************
52 @Function			append
53 @Input				_Ptr	A string
54 @Returns			Updated string
55 @Description		Appends a string
56 *************************************************************************/
append(const char * _Ptr)57 CPVRTStringHash& CPVRTStringHash::append(const char* _Ptr)
58 {
59 	m_String.append(_Ptr);
60 	m_Hash = CPVRTHash::MakeHash(m_String);
61 	return *this;
62 }
63 
64 /*!***********************************************************************
65 @Function			append
66 @Input				_Str	A string
67 @Returns			Updated string
68 @Description		Appends a string
69 *************************************************************************/
append(const CPVRTString & _Str)70 CPVRTStringHash& CPVRTStringHash::append(const CPVRTString& _Str)
71 {
72 	m_String.append(_Str);
73 	m_Hash = CPVRTHash::MakeHash(m_String);
74 	return *this;
75 }
76 
77 /*!***********************************************************************
78 @Function			assign
79 @Input				_Ptr A string
80 @Returns			Updated string
81 @Description		Assigns the string to the string _Ptr
82 *************************************************************************/
assign(const char * _Ptr)83 CPVRTStringHash& CPVRTStringHash::assign(const char* _Ptr)
84 {
85 	m_String.assign(_Ptr);
86 	m_Hash = CPVRTHash::MakeHash(m_String);
87 	return *this;
88 }
89 
90 /*!***********************************************************************
91 @Function			assign
92 @Input				_Str A string
93 @Returns			Updated string
94 @Description		Assigns the string to the string _Str
95 *************************************************************************/
assign(const CPVRTString & _Str)96 CPVRTStringHash& CPVRTStringHash::assign(const CPVRTString& _Str)
97 {
98 	m_String.assign(_Str);
99 	m_Hash = CPVRTHash::MakeHash(m_String);
100 	return *this;
101 }
102 
103 /*!***********************************************************************
104 @Function		==
105 @Input			_Str 	A string to compare with
106 @Returns		True if they match
107 @Description	== Operator
108 *************************************************************************/
operator ==(const CPVRTStringHash & _Str) const109 bool CPVRTStringHash::operator==(const CPVRTStringHash& _Str) const
110 {
111 	return (m_Hash == _Str.Hash());
112 }
113 
114 /*!***********************************************************************
115 @Function		==
116 @Input			Hash 	A hash to compare with
117 @Returns		True if they match
118 @Description	== Operator
119 *************************************************************************/
operator ==(const CPVRTHash & Hash) const120 bool CPVRTStringHash::operator==(const CPVRTHash& Hash) const
121 {
122 	return (m_Hash == Hash);
123 }
124 
125 /*!***********************************************************************
126 @Function		==
127 @Input			_Str 	A string to compare with
128 @Returns		True if they match
129 @Description	== Operator. This function performs a strcmp()
130 				as it's more efficient to strcmp than to hash the string
131 				for every comparison.
132 *************************************************************************/
operator ==(const char * _Str) const133 bool CPVRTStringHash::operator==(const char* _Str) const
134 {
135 	return (m_String.compare(_Str) == 0);
136 }
137 
138 /*!***********************************************************************
139 @Function		==
140 @Input			_Str 	A string to compare with
141 @Returns		True if they match
142 @Description	== Operator. This function performs a strcmp()
143 				as it's more efficient to strcmp than to hash the string
144 				for every comparison.
145 *************************************************************************/
operator ==(const CPVRTString & _Str) const146 bool CPVRTStringHash::operator==(const CPVRTString& _Str) const
147 {
148 	return (m_String.compare(_Str) == 0);
149 }
150 
151 /*!***********************************************************************
152 @Function			!=
153 @Input				_Str 	A string to compare with
154 @Returns			True if they don't match
155 @Description		!= Operator
156 *************************************************************************/
operator !=(const CPVRTStringHash & _Str) const157 bool CPVRTStringHash::operator!=(const CPVRTStringHash& _Str) const
158 {
159 	return (m_Hash != _Str.Hash());
160 }
161 
162 /*!***********************************************************************
163 @Function		!=
164 @Input			Hash 	A hash to compare with
165 @Returns		True if they match
166 @Description	!= Operator
167 *************************************************************************/
operator !=(const CPVRTHash & Hash) const168 bool CPVRTStringHash::operator!=(const CPVRTHash& Hash) const
169 {
170 	return (m_Hash != Hash);
171 }
172 
173 /*!***********************************************************************
174 @Function			String
175 @Returns			The original string
176 @Description		Returns the original, base string.
177 *************************************************************************/
String() const178 const CPVRTString& CPVRTStringHash::String() const
179 {
180 	return m_String;
181 }
182 
183 /*!***********************************************************************
184 @Function			Hash
185 @Returns			The hash
186 @Description		Returns the hash of the base string
187 *************************************************************************/
Hash() const188 const CPVRTHash& CPVRTStringHash::Hash() const
189 {
190 	return m_Hash;
191 }
192 
193 /*!***************************************************************************
194 @Function		c_str
195 @Return			The original string.
196 @Description	Returns the base string as a const char*.
197 *****************************************************************************/
c_str() const198 const char* CPVRTStringHash::c_str() const
199 {
200 	return m_String.c_str();
201 }
202 
203