Update StringId.cs

This commit is contained in:
ALEXTANG
2022-08-08 19:21:25 +08:00
parent fb679832b3
commit 0ca119be70

View File

@@ -4,29 +4,27 @@ namespace TEngine
{ {
public class StringId public class StringId
{ {
private static Dictionary<string, int> m_eventTypeHashMap = new Dictionary<string, int>(); private static readonly Dictionary<string, int> eventTypeHashMap = new Dictionary<string, int>();
private static Dictionary<int, string> m_eventHashToStringMap = new Dictionary<int, string>(); private static readonly Dictionary<int, string> eventHashToStringMap = new Dictionary<int, string>();
private static int m_currentId = 0; private static int _currentId = 0;
public static int StringToHash(string val) public static int StringToHash(string val)
{ {
int hashId; if (eventTypeHashMap.TryGetValue(val, out var hashId))
if (m_eventTypeHashMap.TryGetValue(val, out hashId))
{ {
return hashId; return hashId;
} }
hashId = ++m_currentId; hashId = ++_currentId;
m_eventTypeHashMap[val] = hashId; eventTypeHashMap[val] = hashId;
m_eventHashToStringMap[hashId] = val; eventHashToStringMap[hashId] = val;
return hashId; return hashId;
} }
public static string HashToString(int hash) public static string HashToString(int hash)
{ {
string value; if (eventHashToStringMap.TryGetValue(hash, out var value))
if (m_eventHashToStringMap.TryGetValue(hash, out value))
{ {
return value; return value;
} }