Skip to content

Commit 4fa9aa8

Browse files
committed
Structures: StringView can now be used as an HashMap key.
1 parent 4adee1d commit 4fa9aa8

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

include/pulsar/structures/stringview.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ namespace Pulsar
4848
return m_Data[m_Start+idx];
4949
}
5050

51+
bool operator==(const StringView& other) const
52+
{
53+
if (Length() != other.Length())
54+
return false;
55+
for (size_t i = 0; i < other.Length(); i++) {
56+
if ((*this)[i] != other[i])
57+
return false;
58+
}
59+
return true;
60+
}
61+
62+
bool operator!=(const StringView& other) const { return !(*this == other); }
63+
5164
size_t Length() const { return Empty() ? 0 : m_End - m_Start; }
5265
bool Empty() const { return m_Start >= m_End; }
5366
size_t GetStart() const { return m_Start; }
@@ -61,4 +74,16 @@ namespace Pulsar
6174
};
6275
}
6376

77+
template<>
78+
struct std::hash<Pulsar::StringView>
79+
{
80+
size_t operator()(const Pulsar::StringView& strView) const
81+
{
82+
size_t hash = 0;
83+
for (size_t i = 0; i < strView.Length(); i++)
84+
hash += std::hash<char>{}(strView[i]);
85+
return hash;
86+
}
87+
};
88+
6489
#endif // _PULSAR_STRUCTURES_STRINGVIEW_H

0 commit comments

Comments
 (0)