From 8cbc628e4861db4bdc1f83394a3cead21bd2c4d1 Mon Sep 17 00:00:00 2001 From: Jeff Niu Date: Wed, 8 Nov 2017 21:41:37 -0800 Subject: [PATCH] Basic comparator type --- lib/wlib/stl/Comparator.h | 132 ++++++++++++++++++++++++++++++++++++++ lib/wlib/stl/Equal.h | 9 ++- 2 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 lib/wlib/stl/Comparator.h diff --git a/lib/wlib/stl/Comparator.h b/lib/wlib/stl/Comparator.h new file mode 100644 index 00000000..52b40a85 --- /dev/null +++ b/lib/wlib/stl/Comparator.h @@ -0,0 +1,132 @@ +#ifndef EMBEDDEDCPLUSPLUS_COMPARATOR_H +#define EMBEDDEDCPLUSPLUS_COMPARATOR_H + +#include + +#include "../WlibConfig.h" +#include "Equal.h" + +namespace wlp { + + int8_t strcmp(const char *s1, const char *s2) { + for (; *s1 == *s2; ++s1, ++s2) { + if (*s1 == 0) { + return 0; + } + } + return *s2 - *s1; + } + + template + struct Comparator { + bool __lt__(const T t1, const T t2) const { + return t1 < t2; + } + bool __le__(const T t1, const T t2) const { + return t1 <= t2; + } + bool __eq__(const T t1, const T t2) const { + return t1 == t2; + } + bool __ne__(const T t1, const T t2) const { + return t1 != t2; + } + bool __gt__(const T t1, const T t2) const { + return t1 > t2; + } + bool __ge__(const T t1, const T t2) const { + return t1 >= t2; + } + }; + + TEMPLATE_NULL + struct Comparator { + bool __lt__(const char *s1, const char *s2) { + return strcmp(s1, s2) < 0; + } + bool __le__(const char *s1, const char *s2) { + return strcmp(s1, s2) <= 0; + } + bool __eq__(const char *s1, const char *s2) { + return strcmp(s1, s2) == 0; + } + bool __ne__(const char *s1, const char *s2) { + return strcmp(s1, s2) != 0; + } + bool __gt__(const char *s1, const char *s2) { + return strcmp(s1, s2) > 0; + } + bool __ge__(const char *s1, const char *s2) { + return strcmp(s1, s2) >= 0; + } + }; + + TEMPLATE_NULL + struct Comparator { + bool __lt__(const char *s1, const char *s2) { + return strcmp(s1, s2) < 0; + } + bool __le__(const char *s1, const char *s2) { + return strcmp(s1, s2) <= 0; + } + bool __eq__(const char *s1, const char *s2) { + return strcmp(s1, s2) == 0; + } + bool __ne__(const char *s1, const char *s2) { + return strcmp(s1, s2) != 0; + } + bool __gt__(const char *s1, const char *s2) { + return strcmp(s1, s2) > 0; + } + bool __ge__(const char *s1, const char *s2) { + return strcmp(s1, s2) >= 0; + } + };ß + + template + struct Comparator> { + bool __lt__(const StaticString &s1, const StaticString &s2) { + return strcmp(s1.c_str(), s2.c_str()) < 0; + } + bool __le__(const StaticString &s1, const StaticString &s2) { + return strcmp(s1.c_str(), s2.c_str()) <= 0; + } + bool __eq__(const StaticString &s1, const StaticString &s2) { + return strcmp(s1.c_str(), s2.c_str()) == 0; + } + bool __ne__(const StaticString &s1, const StaticString &s2) { + return strcmp(s1.c_str(), s2.c_str()) != 0; + } + bool __gt__(const StaticString &s1, const StaticString &s2) { + return strcmp(s1.c_str(), s2.c_str()) > 0; + } + bool __ge__(const StaticString &s1, const StaticString &s2) { + return strcmp(s1.c_str(), s2.c_str()) >= 0; + } + }; + + template + struct Comparator> { + bool __lt__(const StaticString &s1, const StaticString &s2) { + return strcmp(s1.c_str(), s2.c_str()) < 0; + } + bool __le__(const StaticString &s1, const StaticString &s2) { + return strcmp(s1.c_str(), s2.c_str()) <= 0; + } + bool __eq__(const StaticString &s1, const StaticString &s2) { + return strcmp(s1.c_str(), s2.c_str()) == 0; + } + bool __ne__(const StaticString &s1, const StaticString &s2) { + return strcmp(s1.c_str(), s2.c_str()) != 0; + } + bool __gt__(const StaticString &s1, const StaticString &s2) { + return strcmp(s1.c_str(), s2.c_str()) > 0; + } + bool __ge__(const StaticString &s1, const StaticString &s2) { + return strcmp(s1.c_str(), s2.c_str()) >= 0; + } + }; + +} + +#endif //EMBEDDEDCPLUSPLUS_COMPARATOR_H diff --git a/lib/wlib/stl/Equal.h b/lib/wlib/stl/Equal.h index 1e716d7d..46bc857c 100644 --- a/lib/wlib/stl/Equal.h +++ b/lib/wlib/stl/Equal.h @@ -52,7 +52,14 @@ namespace wlp { } template - struct equals > { + struct equals> { + bool operator()(const StaticString &key1, const StaticString &key2) const { + return static_string_equals(key1, key2); + } + }; + + template + struct equals> { bool operator()(const StaticString &key1, const StaticString &key2) const { return static_string_equals(key1, key2); }