Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Implement hash, eq, neq #102

Merged
merged 3 commits into from
Mar 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions pygeos/test/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,29 @@ def test_adapt_ptr_raises():
point = pygeos.Geometry("POINT (2 2)")
with pytest.raises(AttributeError):
point._ptr += 1


@pytest.mark.parametrize("geom", all_types[:-1])
def test_hash_same_equal(geom):
assert hash(geom) == hash(pygeos.apply(geom, lambda x: x))


@pytest.mark.parametrize("geom", all_types[:-1])
def test_hash_same_not_equal(geom):
assert hash(geom) != hash(pygeos.apply(geom, lambda x: x + 1))


@pytest.mark.parametrize("geom", all_types[:-1])
def test_eq(geom):
assert geom == pygeos.apply(geom, lambda x: x)


@pytest.mark.parametrize("geom", all_types[:-1])
def test_neq(geom):
assert geom != pygeos.apply(geom, lambda x: x + 1)


@pytest.mark.parametrize("geom", all_types[:-1])
def test_set_unique(geom):
a = {geom, pygeos.apply(geom, lambda x: x)}
assert len(a) == 1
65 changes: 65 additions & 0 deletions src/pygeom.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,69 @@ static PyObject *GeometryObject_str(GeometryObject *self)
return GeometryObject_ToWKT(self, "%s");
}

static Py_hash_t GeometryObject_hash(GeometryObject *self)
{
void *context = geos_context[0];
unsigned char *wkb;
size_t size;
Py_hash_t x;

if (self->ptr == NULL) {
return -1;
}
GEOSWKBWriter *writer = GEOSWKBWriter_create_r(context);
jorisvandenbossche marked this conversation as resolved.
Show resolved Hide resolved
if (writer == NULL) {
return -1;
}

GEOSWKBWriter_setOutputDimension_r(context, writer, 3);
GEOSWKBWriter_setIncludeSRID_r(context, writer, 1);
wkb = GEOSWKBWriter_write_r(context, writer, self->ptr, &size);
GEOSWKBWriter_destroy_r(context, writer);
if (wkb == NULL) {
return -1;
}
x = PyHash_GetFuncDef()->hash(wkb, size);
if (x == -1) {
x = -2;
} else {
x ^= 374761393UL; // to make the result distinct from the actual WKB hash //
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a random number? Or does it come from somewhere?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a prime that I stole from some other hash function in the Python source. I guess it could be any number if you use it in a XOR like I did.

}
GEOSFree_r(context, wkb);
return x;
}

static PyObject *GeometryObject_richcompare(GeometryObject *self, PyObject *other, int op) {
PyObject *result = NULL;
if (Py_TYPE(self)->tp_richcompare != Py_TYPE(other)->tp_richcompare) {
result = Py_NotImplemented;
} else {
GeometryObject *other_geom = (GeometryObject *) other;
switch (op) {
case Py_LT:
result = Py_NotImplemented;
break;
case Py_LE:
result = Py_NotImplemented;
break;
case Py_EQ:
result = (GeometryObject_hash(self) == GeometryObject_hash(other_geom)) ? Py_True : Py_False;
break;
case Py_NE:
result = (GeometryObject_hash(self) != GeometryObject_hash(other_geom)) ? Py_True : Py_False;
break;
case Py_GT:
result = Py_NotImplemented;
break;
case Py_GE:
result = Py_NotImplemented;
break;
}
}
Py_XINCREF(result);
return result;
}

static PyObject *GeometryObject_FromWKT(PyTypeObject *type, PyObject *value)
{
void *context_handle = geos_context[0];
Expand Down Expand Up @@ -143,6 +206,8 @@ PyTypeObject GeometryType = {
.tp_members = GeometryObject_members,
.tp_methods = GeometryObject_methods,
.tp_repr = (reprfunc) GeometryObject_repr,
.tp_hash = (hashfunc) GeometryObject_hash,
.tp_richcompare = (richcmpfunc) GeometryObject_richcompare,
.tp_str = (reprfunc) GeometryObject_str,
};

Expand Down
2 changes: 1 addition & 1 deletion src/ufuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ static void from_wkt_func(char **args, npy_intp *dimensions,
GEOSWKTReader *reader;
const char *wkt;

/* Create the WKB reader */
/* Create the WKT reader */
reader = GEOSWKTReader_create_r(context_handle);
if (reader == NULL) {
return;
Expand Down