Skip to content

Commit

Permalink
Implement new default compare operators.
Browse files Browse the repository at this point in the history
  • Loading branch information
GrieferAtWork committed May 4, 2024
1 parent 5df126a commit 5a0fbf2
Show file tree
Hide file tree
Showing 19 changed files with 5,033 additions and 445 deletions.
3 changes: 3 additions & 0 deletions .vs/deemon.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@
<ClInclude Include="..\src\deemon\objects\seq\bsearch-impl.c.inl" />
<ClInclude Include="..\src\deemon\objects\seq\bsearch.c.inl" />
<ClInclude Include="..\src\deemon\objects\seq\byattr.h" />
<ClInclude Include="..\src\deemon\objects\seq\default-compare-impl.c.inl" />
<ClInclude Include="..\src\deemon\objects\seq\default-compare.h" />
<ClInclude Include="..\src\deemon\objects\seq\default-iterators.h" />
<ClInclude Include="..\src\deemon\objects\seq\default-sequences.h" />
<ClInclude Include="..\src\deemon\objects\seq\each-fastpass.c.inl" />
Expand Down Expand Up @@ -419,6 +421,7 @@
<ClCompile Include="..\src\deemon\objects\seq\byattr.c" />
<ClCompile Include="..\src\deemon\objects\seq\combinations.c" />
<ClCompile Include="..\src\deemon\objects\seq\concat.c" />
<ClCompile Include="..\src\deemon\objects\seq\default-compare.c" />
<ClCompile Include="..\src\deemon\objects\seq\default-iterators.c" />
<ClCompile Include="..\src\deemon\objects\seq\default-sequences.c" />
<ClCompile Include="..\src\deemon\objects\seq\each.c" />
Expand Down
9 changes: 9 additions & 0 deletions .vs/deemon.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,12 @@
<ClInclude Include="..\src\deemon\objects\seq\byattr.h">
<Filter>src\objects\seq</Filter>
</ClInclude>
<ClInclude Include="..\src\deemon\objects\seq\default-compare-impl.c.inl">
<Filter>src\objects\seq</Filter>
</ClInclude>
<ClInclude Include="..\src\deemon\objects\seq\default-compare.h">
<Filter>src\objects\seq</Filter>
</ClInclude>
<ClInclude Include="..\src\deemon\objects\seq\default-iterators.h">
<Filter>src\objects\seq</Filter>
</ClInclude>
Expand Down Expand Up @@ -1226,6 +1232,9 @@
<ClCompile Include="..\src\deemon\objects\seq\concat.c">
<Filter>src\objects\seq</Filter>
</ClCompile>
<ClCompile Include="..\src\deemon\objects\seq\default-compare.c">
<Filter>src\objects\seq</Filter>
</ClCompile>
<ClCompile Include="..\src\deemon\objects\seq\default-iterators.c">
<Filter>src\objects\seq</Filter>
</ClCompile>
Expand Down
3 changes: 3 additions & 0 deletions cpp.hint
Original file line number Diff line number Diff line change
Expand Up @@ -1642,3 +1642,6 @@
#define _memval_foreach_obj(...) for(;;)
#define memval_foreach_obj(...) for(;;)
#define memval_foreach_obj_end

#define LOCAL_seq_compare__(x) seq_compare__##x
#define LOCAL_seq_docompare__(x) seq_docompare__##x
475 changes: 414 additions & 61 deletions include/deemon/class.h

Large diffs are not rendered by default.

34 changes: 20 additions & 14 deletions include/deemon/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -1905,19 +1905,20 @@ struct Dee_type_cmp {
* them to be ordered with other operators. */
struct Dee_type_nii Dee_tpconst *tp_nii; /* TODO: Deprecated */

/* Same as "tp_compare", but only needs to support equal/not-equal compare:
* @return: Dee_COMPARE_ERR: An error occurred.
* @return: -1: `lhs != rhs'
* @return: 0: `lhs == rhs'
* @return: 1: `lhs != rhs' */
WUNUSED_T NONNULL_T((1, 2)) int (DCALL *tp_compare_eq)(DeeObject *self, DeeObject *some_object);

/* Rich-compare operator that can be defined instead
* of `tp_eq', `tp_ne', `tp_lo', `tp_le', `tp_gr', `tp_ge'
* @return: Dee_COMPARE_ERR: An error occurred.
* @return: -1: `lhs < rhs'
* @return: 0: `lhs == rhs'
* @return: 1: `lhs > rhs' */
WUNUSED_T NONNULL_T((1, 2)) int (DCALL *tp_compare)(DeeObject *self, DeeObject *some_object);
/* Same as "tp_compare", but only needs to support equal/not-equal compare:
* @return: Dee_COMPARE_ERR: An error occurred.
* @return: -1: `lhs != rhs'
* @return: 0: `lhs == rhs'
* @return: 1: `lhs != rhs' */
WUNUSED_T NONNULL_T((1, 2)) int (DCALL *tp_compare_eq)(DeeObject *self, DeeObject *some_object);
};

typedef WUNUSED_T NONNULL_T((2)) Dee_ssize_t (DCALL *Dee_foreach_t)(void *arg, DeeObject *elem);
Expand Down Expand Up @@ -3898,8 +3899,7 @@ INTDEF NONNULL((1)) bool DCALL DeeType_InheritAnd(DeeTypeObject *__restrict self
INTDEF NONNULL((1)) bool DCALL DeeType_InheritOr(DeeTypeObject *__restrict self); /* tp_or, tp_inplace_or */
INTDEF NONNULL((1)) bool DCALL DeeType_InheritXor(DeeTypeObject *__restrict self); /* tp_xor, tp_inplace_xor */
INTDEF NONNULL((1)) bool DCALL DeeType_InheritPow(DeeTypeObject *__restrict self); /* tp_pow, tp_inplace_pow */
INTDEF NONNULL((1)) bool DCALL DeeType_InheritHash(DeeTypeObject *__restrict self); /* tp_hash, tp_eq, tp_ne (in order to inherit "tp_hash", must also inherit "tp_eq" and "tp_ne") */
INTDEF NONNULL((1)) bool DCALL DeeType_InheritCompare(DeeTypeObject *__restrict self); /* tp_eq, tp_ne, tp_lo, tp_le, tp_gr, tp_ge */
INTDEF NONNULL((1)) bool DCALL DeeType_InheritCompare(DeeTypeObject *__restrict self); /* tp_hash, tp_eq, tp_ne, tp_lo, tp_le, tp_gr, tp_ge, tp_compare_eq, tp_compare */
INTDEF NONNULL((1)) bool DCALL DeeType_InheritIterNext(DeeTypeObject *__restrict self); /* tp_iter_next */
INTDEF NONNULL((1)) bool DCALL DeeType_InheritIter(DeeTypeObject *__restrict self); /* tp_iter, tp_foreach, tp_foreach_pair */
INTDEF NONNULL((1)) bool DCALL DeeType_InheritSize(DeeTypeObject *__restrict self); /* tp_sizeob, tp_size */
Expand Down Expand Up @@ -3934,7 +3934,6 @@ INTDEF NONNULL((1)) bool DCALL DeeType_InheritBuffer(DeeTypeObject *__restrict s
#define DeeType_InheritOr(self) DeeType_InheritOperator(self, OPERATOR_OR)
#define DeeType_InheritXor(self) DeeType_InheritOperator(self, OPERATOR_XOR)
#define DeeType_InheritPow(self) DeeType_InheritOperator(self, OPERATOR_POW)
#define DeeType_InheritHash(self) DeeType_InheritOperator(self, OPERATOR_HASH)
#define DeeType_InheritCompare(self) DeeType_InheritOperator(self, OPERATOR_EQ)
#define DeeType_InheritIterNext(self) DeeType_InheritOperator(self, OPERATOR_ITERNEXT)
#define DeeType_InheritIter(self) DeeType_InheritOperator(self, OPERATOR_ITER)
Expand Down Expand Up @@ -4473,18 +4472,25 @@ DFUNDEF WUNUSED NONNULL((1, 2)) int (DCALL DeeObject_CompareLe)(DeeObject *self,
DFUNDEF WUNUSED NONNULL((1, 2)) int (DCALL DeeObject_CompareGr)(DeeObject *self, DeeObject *some_object);
DFUNDEF WUNUSED NONNULL((1, 2)) int (DCALL DeeObject_CompareGe)(DeeObject *self, DeeObject *some_object);

/* @return: == Dee_COMPARE_ERR: An error occurred.
* @return: == -1: `lhs < rhs'
/* @return: == -1: `lhs < rhs'
* @return: == 0: `lhs == rhs'
* @return: == 1: `lhs > rhs' */
* @return: == 1: `lhs > rhs'
* @return: == Dee_COMPARE_ERR: An error occurred. */
DFUNDEF WUNUSED NONNULL((1, 2)) int
(DCALL DeeObject_Compare)(DeeObject *lhs, DeeObject *rhs);

/* @return: == -1: `lhs != rhs'
* @return: == 0: `lhs == rhs'
* @return: == 1: `lhs != rhs'
* @return: == Dee_COMPARE_ERR: An error occurred. */
DFUNDEF WUNUSED NONNULL((1, 2)) int
(DCALL DeeObject_CompareForEquality)(DeeObject *lhs, DeeObject *rhs);

/* Compare a pre-keyed `lhs_keyed' with `rhs' using the given (optional) `key' function
* @return: == Dee_COMPARE_ERR: An error occurred.
* @return: == -1: `lhs_keyed < key(rhs)'
* @return: == 0: `lhs_keyed == key(rhs)'
* @return: == 1: `lhs_keyed > key(rhs)' */
* @return: == 1: `lhs_keyed > key(rhs)'
* @return: == Dee_COMPARE_ERR: An error occurred. */
DFUNDEF WUNUSED NONNULL((1, 2)) int
(DCALL DeeObject_CompareKey)(DeeObject *lhs_keyed,
DeeObject *rhs, /*nullable*/ DeeObject *key);
Expand Down
48 changes: 1 addition & 47 deletions src/deemon/objects/dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -2408,52 +2408,6 @@ dict_printrepr(Dict *__restrict self,
return temp;
}

PRIVATE WUNUSED NONNULL((1)) dhash_t DCALL
dict_hash(Dict *__restrict self) {
dhash_t result;
struct dict_item *iter, *end;
struct dict_item *vector;
size_t mask;
again:
result = DEE_HASHOF_EMPTY_SEQUENCE;
DeeDict_LockRead(self);
vector = self->d_elem;
mask = self->d_mask;
end = (iter = vector) + (mask + 1);
for (; iter < end; ++iter) {
DREF DeeObject *key, *value;
key = iter->di_key;
if (key == NULL || key == dummy)
continue;
value = iter->di_value;
Dee_Incref(key);
Dee_Incref(value);
DeeDict_LockEndRead(self);
result ^= Dee_HashCombine(DeeObject_Hash(key),
DeeObject_Hash(value));
Dee_Decref(value);
Dee_Decref(key);
DeeDict_LockRead(self);
if unlikely(self->d_elem != vector ||
self->d_mask != mask) {
DeeDict_LockEndRead(self);
goto again;
}
}
DeeDict_LockEndRead(self);
return result;
}

PRIVATE struct type_cmp dict_cmp = {
/* .tp_hash = */ (dhash_t (DCALL *)(DeeObject *__restrict))&dict_hash,
/* .tp_eq = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))NULL, // TODO: &dict_eq,
/* .tp_ne = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))NULL, // TODO: &dict_ne,
/* .tp_lo = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))NULL, // TODO: &dict_lo,
/* .tp_le = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))NULL, // TODO: &dict_le,
/* .tp_gr = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))NULL, // TODO: &dict_gr,
/* .tp_ge = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))NULL, // TODO: &dict_ge,
};

PRIVATE WUNUSED NONNULL((1)) int DCALL
dict_bool(Dict *__restrict self) {
return atomic_read(&self->d_used) != 0;
Expand Down Expand Up @@ -2834,7 +2788,7 @@ PUBLIC DeeTypeObject DeeDict_Type = {
/* .tp_visit = */ (void (DCALL *)(DeeObject *__restrict, dvisit_t, void *))&dict_visit,
/* .tp_gc = */ &dict_gc,
/* .tp_math = */ NULL,
/* .tp_cmp = */ &dict_cmp,
/* .tp_cmp = */ NULL, /* TODO: &dict_cmp */
/* .tp_seq = */ &dict_seq,
/* .tp_iter_next = */ NULL,
/* .tp_attr = */ NULL,
Expand Down
44 changes: 1 addition & 43 deletions src/deemon/objects/hashset.c
Original file line number Diff line number Diff line change
Expand Up @@ -1847,38 +1847,6 @@ hashset_printrepr(HashSet *__restrict self,
return temp;
}

PRIVATE WUNUSED NONNULL((1)) dhash_t DCALL
hashset_hash(HashSet *__restrict self) {
dhash_t result;
struct hashset_item *iter, *end;
struct hashset_item *vector;
size_t mask;
again:
result = DEE_HASHOF_EMPTY_SEQUENCE;
DeeHashSet_LockRead(self);
vector = self->hs_elem;
mask = self->hs_mask;
end = (iter = vector) + (mask + 1);
for (; iter < end; ++iter) {
DREF DeeObject *key;
key = iter->hsi_key;
if (key == NULL || key == dummy)
continue;
Dee_Incref(key);
DeeHashSet_LockEndRead(self);
result ^= DeeObject_Hash(key);
Dee_Decref(key);
DeeHashSet_LockRead(self);
if unlikely(self->hs_elem != vector ||
self->hs_mask != mask) {
DeeHashSet_LockEndRead(self);
goto again;
}
}
DeeHashSet_LockEndRead(self);
return result;
}

PRIVATE struct type_nsi tpconst hashset_nsi = {
/* .nsi_class = */ TYPE_SEQX_CLASS_SET,
/* .nsi_flags = */ TYPE_SEQX_FMUTABLE | TYPE_SEQX_FRESIZABLE,
Expand All @@ -1891,16 +1859,6 @@ PRIVATE struct type_nsi tpconst hashset_nsi = {
}
};

PRIVATE struct type_cmp hashset_cmp = {
/* .tp_hash = */ (dhash_t (DCALL *)(DeeObject *__restrict))&hashset_hash,
/* .tp_eq = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))NULL, // TODO: &hashset_eq,
/* .tp_ne = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))NULL, // TODO: &hashset_ne,
/* .tp_lo = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))NULL, // TODO: &hashset_lo,
/* .tp_le = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))NULL, // TODO: &hashset_le,
/* .tp_gr = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))NULL, // TODO: &hashset_gr,
/* .tp_ge = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))NULL, // TODO: &hashset_ge,
};

PRIVATE struct type_seq hashset_seq = {
/* .tp_iter = */ (DREF DeeObject *(DCALL *)(DeeObject *__restrict))&hashset_iter,
/* .tp_sizeob = */ NULL,
Expand Down Expand Up @@ -2256,7 +2214,7 @@ PUBLIC DeeTypeObject DeeHashSet_Type = {
/* .tp_visit = */ (void (DCALL *)(DeeObject *__restrict, dvisit_t, void *))&hashset_visit,
/* .tp_gc = */ &hashset_gc,
/* .tp_math = */ NULL,
/* .tp_cmp = */ &hashset_cmp,
/* .tp_cmp = */ NULL, /* TODO: &hashset_cmp */
/* .tp_seq = */ &hashset_seq,
/* .tp_iter_next = */ NULL,
/* .tp_attr = */ NULL,
Expand Down
30 changes: 1 addition & 29 deletions src/deemon/objects/rodict.c
Original file line number Diff line number Diff line change
Expand Up @@ -825,24 +825,6 @@ rodict_printrepr(RoDict *__restrict self,
return temp;
}

PRIVATE WUNUSED NONNULL((1)) Dee_hash_t DCALL
rodict_hash(RoDict *__restrict self) {
size_t i;
Dee_hash_t result = DEE_HASHOF_EMPTY_SEQUENCE;
for (i = 0; i <= self->rd_mask; ++i) {
if (!self->rd_elem[i].rdi_key)
continue;
/* Note that we still combine the hashes for the key and value,
* thus not only mirroring the behavior of hash of the item (that
* is the tuple `(key, value)', including the order between the
* key and value within the hash, so that swapping the key and
* value would produce a different hash) */
result ^= Dee_HashCombine(DeeObject_Hash(self->rd_elem[i].rdi_key),
DeeObject_Hash(self->rd_elem[i].rdi_value));
}
return result;
}

PRIVATE WUNUSED NONNULL((1)) size_t DCALL
rodict_size(RoDict *__restrict self) {
ASSERT(self->rd_size != (size_t)-1);
Expand Down Expand Up @@ -1016,16 +998,6 @@ PRIVATE struct type_nsi tpconst rodict_nsi = {
};


PRIVATE struct type_cmp rodict_cmp = {
/* .tp_hash = */ (Dee_hash_t (DCALL *)(DeeObject *__restrict))&rodict_hash,
/* .tp_eq = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))NULL, // TODO: &rodict_eq,
/* .tp_ne = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))NULL, // TODO: &rodict_ne,
/* .tp_lo = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))NULL, // TODO: &rodict_lo,
/* .tp_le = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))NULL, // TODO: &rodict_le,
/* .tp_gr = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))NULL, // TODO: &rodict_gr,
/* .tp_ge = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))NULL, // TODO: &rodict_ge,
};

PRIVATE struct type_seq rodict_seq = {
/* .tp_iter = */ (DREF DeeObject *(DCALL *)(DeeObject *__restrict))&rodict_iter,
/* .tp_sizeob = */ NULL,
Expand Down Expand Up @@ -1235,7 +1207,7 @@ PUBLIC DeeTypeObject DeeRoDict_Type = {
/* .tp_visit = */ (void (DCALL *)(DeeObject *__restrict, dvisit_t, void *))&rodict_visit,
/* .tp_gc = */ NULL,
/* .tp_math = */ NULL,
/* .tp_cmp = */ &rodict_cmp,
/* .tp_cmp = */ NULL, /* TODO: &rodict_cmp */
/* .tp_seq = */ &rodict_seq,
/* .tp_iter_next = */ NULL,
/* .tp_attr = */ NULL,
Expand Down
24 changes: 1 addition & 23 deletions src/deemon/objects/roset.c
Original file line number Diff line number Diff line change
Expand Up @@ -488,18 +488,6 @@ roset_printrepr(RoSet *__restrict self,
return temp;
}

PRIVATE WUNUSED NONNULL((1)) dhash_t DCALL
roset_hash(RoSet *__restrict self) {
size_t i;
dhash_t result = DEE_HASHOF_EMPTY_SEQUENCE;
for (i = 0; i <= self->rs_mask; ++i) {
if (!self->rs_elem[i].rsi_key)
continue;
result ^= DeeObject_Hash(self->rs_elem[i].rsi_key);
}
return result;
}

PRIVATE WUNUSED NONNULL((1, 2)) Dee_ssize_t DCALL
roset_foreach(RoSet *self, Dee_foreach_t proc, void *arg) {
Dee_ssize_t temp, result = 0;
Expand All @@ -518,16 +506,6 @@ roset_foreach(RoSet *self, Dee_foreach_t proc, void *arg) {
return temp;
}

PRIVATE struct type_cmp roset_cmp = {
/* .tp_hash = */ (dhash_t (DCALL *)(DeeObject *__restrict))&roset_hash,
/* .tp_eq = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))NULL, // TODO: &roset_eq,
/* .tp_ne = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))NULL, // TODO: &roset_ne,
/* .tp_lo = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))NULL, // TODO: &roset_lo,
/* .tp_le = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))NULL, // TODO: &roset_le,
/* .tp_gr = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))NULL, // TODO: &roset_gr,
/* .tp_ge = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))NULL, // TODO: &roset_ge,
};

PRIVATE struct type_seq roset_seq = {
/* .tp_iter = */ (DREF DeeObject *(DCALL *)(DeeObject *__restrict))&roset_iter,
/* .tp_sizeob = */ NULL,
Expand Down Expand Up @@ -711,7 +689,7 @@ PUBLIC DeeTypeObject DeeRoSet_Type = {
/* .tp_visit = */ (void (DCALL *)(DeeObject *__restrict, dvisit_t, void *))&roset_visit,
/* .tp_gc = */ NULL,
/* .tp_math = */ NULL,
/* .tp_cmp = */ &roset_cmp,
/* .tp_cmp = */ NULL, /* TODO: &roset_cmp */
/* .tp_seq = */ &roset_seq,
/* .tp_iter_next = */ NULL,
/* .tp_attr = */ NULL,
Expand Down
Loading

0 comments on commit 5a0fbf2

Please sign in to comment.