From b2b26f5511e78029b77381869cf3382b80541492 Mon Sep 17 00:00:00 2001
From: Andreas Kloeckner <inform@tiker.net>
Date: Thu, 18 Aug 2011 12:12:11 -0500
Subject: [PATCH] Make comparable CL objects hashable.

---
 doc/source/runtime.rst         | 6 ++++--
 src/wrapper/wrap_cl.hpp        | 5 +++--
 src/wrapper/wrap_cl_part_1.cpp | 6 ++++++
 src/wrapper/wrap_cl_part_2.cpp | 3 +++
 4 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/doc/source/runtime.rst b/doc/source/runtime.rst
index 7bcd5628..df764984 100644
--- a/doc/source/runtime.rst
+++ b/doc/source/runtime.rst
@@ -55,8 +55,10 @@ Constants
 Platforms, Devices and Contexts
 -------------------------------
 
-.. |comparable| replace:: Two instances of this class may be compared
-    using *"=="* and *"!="*.
+.. |comparable| replace:: Instances of this class are hashable, and two
+    instances of this class may be compared using *"=="* and *"!="*.
+    (Hashability was added in version 2011.2.)
+
 .. |buf-iface| replace:: must implement the Python buffer interface.
     (e.g. by being an :class:`numpy.ndarray`)
 .. |explain-waitfor| replace:: *wait_for*
diff --git a/src/wrapper/wrap_cl.hpp b/src/wrapper/wrap_cl.hpp
index 0b0d0dac..1a94c850 100644
--- a/src/wrapper/wrap_cl.hpp
+++ b/src/wrapper/wrap_cl.hpp
@@ -239,8 +239,9 @@
     bool operator==(cls const &other) const \
     { return data() == other.data(); } \
     bool operator!=(cls const &other) const \
-    { return data() != other.data(); }
-
+    { return data() != other.data(); } \
+    long hash() const \
+    { return (long) (intptr_t) data(); }
 // }}}
 
 
diff --git a/src/wrapper/wrap_cl_part_1.cpp b/src/wrapper/wrap_cl_part_1.cpp
index 6e83c951..08f71f56 100644
--- a/src/wrapper/wrap_cl_part_1.cpp
+++ b/src/wrapper/wrap_cl_part_1.cpp
@@ -24,6 +24,7 @@ void pyopencl_expose_part_1()
       .add_property("obj_ptr", &cls::obj_ptr)
       .def(py::self == py::self)
       .def(py::self != py::self)
+      .def("__hash__", &cls::hash)
       ;
   }
 
@@ -37,6 +38,7 @@ void pyopencl_expose_part_1()
       .add_property("obj_ptr", &cls::obj_ptr)
       .def(py::self == py::self)
       .def(py::self != py::self)
+      .def("__hash__", &cls::hash)
 #if defined(cl_ext_device_fission) && defined(PYOPENCL_USE_DEVICE_FISSION)
       .DEF_SIMPLE_METHOD(create_sub_devices)
 #endif
@@ -61,6 +63,7 @@ void pyopencl_expose_part_1()
       .add_property("obj_ptr", &cls::obj_ptr)
       .def(py::self == py::self)
       .def(py::self != py::self)
+      .def("__hash__", &cls::hash)
       ;
   }
 
@@ -82,6 +85,7 @@ void pyopencl_expose_part_1()
       .add_property("obj_ptr", &cls::obj_ptr)
       .def(py::self == py::self)
       .def(py::self != py::self)
+      .def("__hash__", &cls::hash)
       ;
   }
 
@@ -96,6 +100,7 @@ void pyopencl_expose_part_1()
       .add_property("obj_ptr", &cls::obj_ptr)
       .def(py::self == py::self)
       .def(py::self != py::self)
+      .def("__hash__", &cls::hash)
       ;
   }
 
@@ -129,6 +134,7 @@ void pyopencl_expose_part_1()
           (py::arg("shape"), py::arg("dtype"), py::arg("order")="C"))
       .def(py::self == py::self)
       .def(py::self != py::self)
+      .def("__hash__", &cls::hash)
       ;
   }
   {
diff --git a/src/wrapper/wrap_cl_part_2.cpp b/src/wrapper/wrap_cl_part_2.cpp
index 95f29883..210f9510 100644
--- a/src/wrapper/wrap_cl_part_2.cpp
+++ b/src/wrapper/wrap_cl_part_2.cpp
@@ -109,6 +109,7 @@ void pyopencl_expose_part_2()
       .add_property("obj_ptr", &cls::obj_ptr)
       .def(py::self == py::self)
       .def(py::self != py::self)
+      .def("__hash__", &cls::hash)
       ;
   }
 
@@ -133,6 +134,7 @@ void pyopencl_expose_part_2()
       .add_property("obj_ptr", &cls::obj_ptr)
       .def(py::self == py::self)
       .def(py::self != py::self)
+      .def("__hash__", &cls::hash)
       .def("all_kernels", create_kernels_in_program)
       ;
   }
@@ -149,6 +151,7 @@ void pyopencl_expose_part_2()
       .add_property("obj_ptr", &cls::obj_ptr)
       .def(py::self == py::self)
       .def(py::self != py::self)
+      .def("__hash__", &cls::hash)
       ;
   }
 
-- 
GitLab