hasher instances for 8-bit and 16-bit integral types
authorYedidya Feldblum <yfeldblum@fb.com>
Thu, 11 May 2017 05:41:55 +0000 (22:41 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Thu, 11 May 2017 05:50:36 +0000 (22:50 -0700)
Summary:
[Folly] `hasher` instances for 8-bit and 16-bit integral types.

Allowing the use of `Hash` with such types.

They are not necessarily the ideal algorithms for those widths, essentially forwarding to the 32-bit instances.

Reviewed By: luciang

Differential Revision: D5043094

fbshipit-source-id: 6ef96dfc8d1baf0a15b9bdd585b7c7672099e4f0

folly/Hash.h

index e64633d2c8f552fb0a053d46497038bea382efda..1df1ff67fc04eea30d50ab8deffb30ce43016b3c 100644 (file)
@@ -382,6 +382,38 @@ template<> struct hasher<uint32_t> {
   }
 };
 
+template<> struct hasher<int16_t> {
+  size_t operator()(int16_t key) const {
+    return hasher<int32_t>()(key); // as impl accident, sign-extends
+  }
+};
+
+template<> struct hasher<uint16_t> {
+  size_t operator()(uint16_t key) const {
+    return hasher<uint32_t>()(key);
+  }
+};
+
+template<> struct hasher<int8_t> {
+  size_t operator()(int8_t key) const {
+    return hasher<int32_t>()(key); // as impl accident, sign-extends
+  }
+};
+
+template<> struct hasher<uint8_t> {
+  size_t operator()(uint8_t key) const {
+    return hasher<uint32_t>()(key);
+  }
+};
+
+template<> struct hasher<char> {
+  using explicit_type =
+      std::conditional<std::is_signed<char>::value, int8_t, uint8_t>::type;
+  size_t operator()(char key) const {
+    return hasher<explicit_type>()(key); // as impl accident, sign-extends
+  }
+};
+
 template<> struct hasher<int64_t> {
   size_t operator()(int64_t key) const {
     return static_cast<size_t>(hash::twang_mix64(uint64_t(key)));