aboutsummaryrefslogtreecommitdiff
path: root/include/StringView.hpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2019-02-20 22:35:24 +0100
committerdec05eba <dec05eba@protonmail.com>2020-07-06 07:39:33 +0200
commit95fa2a46519d0820548ab7855b32182ff81f0435 (patch)
treeff5333b61195a0d5019eb3d0aea05a1ff1ba7897 /include/StringView.hpp
parent29fa87b048c20a1e6e648cb874c6da1cb7f1766a (diff)
Change linux platform from 64 to x86_64, add sibs platforms command
Diffstat (limited to 'include/StringView.hpp')
-rw-r--r--include/StringView.hpp41
1 files changed, 39 insertions, 2 deletions
diff --git a/include/StringView.hpp b/include/StringView.hpp
index 4d87d8a..8ca685d 100644
--- a/include/StringView.hpp
+++ b/include/StringView.hpp
@@ -7,6 +7,7 @@
#include "env.hpp"
#include <cstring>
#include <unordered_map>
+#include <map>
#include <cassert>
namespace sibs
@@ -36,8 +37,7 @@ namespace sibs
bool equals(const StringView &other) const
{
- if(size != other.size) return false;
- return memcmp(data, other.data, size) == 0;
+ return size == other.size && memcmp(data, other.data, size) == 0;
}
size_t hash() const
@@ -54,6 +54,31 @@ namespace sibs
assert(index < size);
return data[index];
}
+
+ bool operator < (const StringView &other) const
+ {
+ return ((ssize)size - (ssize)other.size < 0) || strncmp(data, other.data, size) < 0;
+ }
+
+ bool operator > (const StringView &other) const
+ {
+ return !operator<(other);
+ }
+
+ size_t operator()() const
+ {
+ return hash();
+ }
+
+ bool operator()(const StringView &other) const
+ {
+ return equals(other);
+ }
+
+ bool operator == (const StringView &other) const
+ {
+ return equals(other);
+ }
const char *data;
usize size;
@@ -75,9 +100,21 @@ namespace sibs
return lhs.equals(rhs);
}
};
+
+ struct StringViewCompareSorted
+ {
+ public:
+ bool operator()(const StringView &lhs, const StringView &rhs) const
+ {
+ return lhs < rhs;
+ }
+ };
template <typename ValueType>
using StringViewMap = std::unordered_map<const StringView, ValueType, StringViewHash, StringViewCompare>;
+
+ // template <typename ValueType>
+ // using StringViewMapSorted = std::map<const StringView, ValueType, StringViewCompareSorted>;
}
#endif // SIBS_STRING_VIEW_HPP