Skip to content

Commit

Permalink
Use find() to implement remove(), if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
GrieferAtWork committed May 19, 2024
1 parent 493bcbc commit 64898e2
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 18 deletions.
54 changes: 40 additions & 14 deletions src/deemon/objects/seq/default-api-methods-require-impl.c.inl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
//#define DEFINE_DeeType_SeqCache_RequireFindWithKey
//#define DEFINE_DeeType_SeqCache_RequireRFind
//#define DEFINE_DeeType_SeqCache_RequireRFindWithKey
#define DEFINE_DeeType_SeqCache_RequireIndex
//#define DEFINE_DeeType_SeqCache_RequireIndex
//#define DEFINE_DeeType_SeqCache_RequireIndexWithKey
//#define DEFINE_DeeType_SeqCache_RequireRIndex
//#define DEFINE_DeeType_SeqCache_RequireRIndexWithKey
Expand All @@ -37,7 +37,7 @@
//#define DEFINE_DeeType_SeqCache_RequireClear
//#define DEFINE_DeeType_SeqCache_RequirePop
//#define DEFINE_DeeType_SeqCache_RequireRemove
//#define DEFINE_DeeType_SeqCache_RequireRemoveWithKey
#define DEFINE_DeeType_SeqCache_RequireRemoveWithKey
//#define DEFINE_DeeType_SeqCache_RequireRRemove
//#define DEFINE_DeeType_SeqCache_RequireRRemoveWithKey
//#define DEFINE_DeeType_SeqCache_RequireRemoveAll
Expand Down Expand Up @@ -644,10 +644,16 @@ LOCAL_DeeType_SeqCache_RequireFoo_private_uncached(DeeTypeObject *orig_type, Dee
tsc_removeif != &DeeSeq_DefaultRemoveAllWithError)
return &DeeSeq_DefaultRemoveWithTSCRemoveIf;
}
/* TODO: With find and delitem */
if (DeeType_HasPrivateOperator(self, OPERATOR_DELITEM) &&
DeeType_HasOperator(orig_type, OPERATOR_ITER) && orig_type->tp_seq->tp_enumerate_index)
return &DeeSeq_DefaultRemoveWithEnumerateIndexAndDelItemIndex;
if (DeeType_HasPrivateOperator(self, OPERATOR_DELITEM)) {
Dee_tsc_find_t tsc_find;
tsc_find = DeeType_SeqCache_RequireFind_private_uncached(orig_type, self);
if (tsc_find == &DeeSeq_DefaultFindWithEnumerateIndex)
return &DeeSeq_DefaultRemoveWithEnumerateIndexAndDelItemIndex;
if (tsc_find)
return &DeeSeq_DefaultRemoveWithTSCFindAndDelItemIndex;
if (DeeType_HasOperator(orig_type, OPERATOR_ITER) && orig_type->tp_seq->tp_enumerate_index)
return &DeeSeq_DefaultRemoveWithEnumerateIndexAndDelItemIndex;
}
#elif defined(DEFINE_DeeType_SeqCache_RequireRemoveWithKey)
{
Dee_tsc_removeall_with_key_t tsc_removeall_with_key;
Expand All @@ -663,23 +669,43 @@ LOCAL_DeeType_SeqCache_RequireFoo_private_uncached(DeeTypeObject *orig_type, Dee
tsc_removeif != &DeeSeq_DefaultRemoveAllWithError)
return &DeeSeq_DefaultRemoveWithKeyWithTSCRemoveIf;
}
/* TODO: With find and delitem */
if (DeeType_HasPrivateOperator(self, OPERATOR_DELITEM) &&
DeeType_HasOperator(orig_type, OPERATOR_ITER) && orig_type->tp_seq->tp_enumerate_index)
return &DeeSeq_DefaultRemoveWithKeyWithEnumerateIndexAndDelItemIndex;
if (DeeType_HasPrivateOperator(self, OPERATOR_DELITEM)) {
Dee_tsc_find_with_key_t tsc_find_with_key;
tsc_find_with_key = DeeType_SeqCache_RequireFindWithKey_private_uncached(orig_type, self);
if (tsc_find_with_key == &DeeSeq_DefaultFindWithKeyWithEnumerateIndex)
return &DeeSeq_DefaultRemoveWithKeyWithEnumerateIndexAndDelItemIndex;
if (tsc_find_with_key)
return &DeeSeq_DefaultRemoveWithKeyWithTSCFindWithKeyAndDelItemIndex;
if (DeeType_HasOperator(orig_type, OPERATOR_ITER) && orig_type->tp_seq->tp_enumerate_index)
return &DeeSeq_DefaultRemoveWithKeyWithEnumerateIndexAndDelItemIndex;
}
#elif defined(DEFINE_DeeType_SeqCache_RequireRRemove)
if (DeeType_HasPrivateOperator(self, OPERATOR_DELITEM)) {
Dee_tsc_rfind_t tsc_rfind;
tsc_rfind = DeeType_SeqCache_RequireRFind_private_uncached(orig_type, self);
if (tsc_rfind == &DeeSeq_DefaultRFindWithTSCEnumerateIndexReverse)
return &DeeSeq_DefaultRRemoveWithTSCEnumerateIndexReverseAndDelItemIndex;
if (tsc_rfind == &DeeSeq_DefaultRFindWithEnumerateIndex)
return &DeeSeq_DefaultRRemoveWithEnumerateIndexAndDelItemIndex;
if (tsc_rfind)
return &DeeSeq_DefaultRRemoveWithTSCRFindAndDelItemIndex;
if (DeeType_SeqCache_TryRequireEnumerateIndexReverse(orig_type))
return &DeeSeq_DefaultRRemoveWithTSCEnumerateIndexReverseAndDelItemIndex;
/* TODO: With rfind and delitem */
if (DeeType_HasOperator(orig_type, OPERATOR_ITER) && orig_type->tp_seq->tp_enumerate_index)
return &DeeSeq_DefaultRRemoveWithEnumerateIndexAndDelItemIndex;
}
#elif defined(DEFINE_DeeType_SeqCache_RequireRRemoveWithKey)
if (DeeType_HasPrivateOperator(self, OPERATOR_DELITEM)) {
if (DeeType_SeqCache_TryRequireEnumerateIndexReverse(self))
return &DeeSeq_DefaultRRemoveWithKeyWithEnumerateIndexReverseAndDelItemIndex;
/* TODO: With rfind and delitem */
Dee_tsc_rfind_with_key_t tsc_rfind_with_key;
tsc_rfind_with_key = DeeType_SeqCache_RequireRFindWithKey_private_uncached(orig_type, self);
if (tsc_rfind_with_key == &DeeSeq_DefaultRFindWithKeyWithTSCEnumerateIndexReverse)
return &DeeSeq_DefaultRRemoveWithKeyWithTSCEnumerateIndexReverseAndDelItemIndex;
if (tsc_rfind_with_key == &DeeSeq_DefaultRFindWithKeyWithEnumerateIndex)
return &DeeSeq_DefaultRRemoveWithKeyWithEnumerateIndexAndDelItemIndex;
if (tsc_rfind_with_key)
return &DeeSeq_DefaultRRemoveWithKeyWithTSCRFindWithKeyAndDelItemIndex;
if (DeeType_SeqCache_TryRequireEnumerateIndexReverse(orig_type))
return &DeeSeq_DefaultRRemoveWithKeyWithTSCEnumerateIndexReverseAndDelItemIndex;
if (DeeType_HasOperator(orig_type, OPERATOR_ITER) && orig_type->tp_seq->tp_enumerate_index)
return &DeeSeq_DefaultRRemoveWithKeyWithEnumerateIndexAndDelItemIndex;
}
Expand Down
74 changes: 71 additions & 3 deletions src/deemon/objects/seq/default-api-methods.c
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,23 @@ default_remove_with_enumerate_index_and_delitem_index_cb(void *arg, size_t index
return -1;
}

INTERN WUNUSED NONNULL((1, 2)) int DCALL
DeeSeq_DefaultRemoveWithTSCFindAndDelItemIndex(DeeObject *self, DeeObject *item,
size_t start, size_t end) {
int result;
size_t index = (*DeeType_SeqCache_RequireFind(Dee_TYPE(self)))(self, item, start, end);
if unlikely(index == (size_t)Dee_COMPARE_ERR)
goto err;
if (index == (size_t)-1)
return 0;
result = (*Dee_TYPE(self)->tp_seq->tp_delitem_index)(self, index);
if unlikely(result)
goto err;
return 1;
err:
return -1;
}

INTERN WUNUSED NONNULL((1, 2)) int DCALL
DeeSeq_DefaultRemoveWithEnumerateIndexAndDelItemIndex(DeeObject *self, DeeObject *item,
size_t start, size_t end) {
Expand Down Expand Up @@ -1187,6 +1204,23 @@ default_remove_with_key_with_enumerate_index_and_delitem_index_cb(void *arg, siz
return -1;
}

INTERN WUNUSED NONNULL((1, 2, 5)) int DCALL
DeeSeq_DefaultRemoveWithKeyWithTSCFindWithKeyAndDelItemIndex(DeeObject *self, DeeObject *item,
size_t start, size_t end, DeeObject *key) {
int result;
size_t index = (*DeeType_SeqCache_RequireFindWithKey(Dee_TYPE(self)))(self, item, start, end, key);
if unlikely(index == (size_t)Dee_COMPARE_ERR)
goto err;
if (index == (size_t)-1)
return 0;
result = (*Dee_TYPE(self)->tp_seq->tp_delitem_index)(self, index);
if unlikely(result)
goto err;
return 1;
err:
return -1;
}

INTERN WUNUSED NONNULL((1, 2, 5)) int DCALL
DeeSeq_DefaultRemoveWithKeyWithEnumerateIndexAndDelItemIndex(DeeObject *self, DeeObject *item,
size_t start, size_t end, DeeObject *key) {
Expand Down Expand Up @@ -1224,9 +1258,26 @@ DeeSeq_DefaultRemoveWithKeyWithError(DeeObject *self, DeeObject *item,
/************************************************************************/
/* rremove() */
/************************************************************************/
INTERN WUNUSED NONNULL((1, 2)) int DCALL
DeeSeq_DefaultRRemoveWithTSCRFindAndDelItemIndex(DeeObject *self, DeeObject *item,
size_t start, size_t end) {
int result;
size_t index = (*DeeType_SeqCache_RequireRFind(Dee_TYPE(self)))(self, item, start, end);
if unlikely(index == (size_t)Dee_COMPARE_ERR)
goto err;
if (index == (size_t)-1)
return 0;
result = (*Dee_TYPE(self)->tp_seq->tp_delitem_index)(self, index);
if unlikely(result)
goto err;
return 1;
err:
return -1;
}

INTERN WUNUSED NONNULL((1, 2)) int DCALL
DeeSeq_DefaultRRemoveWithTSCEnumerateIndexReverseAndDelItemIndex(DeeObject *self, DeeObject *item,
size_t start, size_t end) {
size_t start, size_t end) {
Dee_ssize_t foreach_status;
struct default_remove_with_enumerate_index_and_delitem_index_data data;
Dee_tsc_enumerate_index_reverse_t tsc_enumerate_index_reverse;
Expand Down Expand Up @@ -1277,8 +1328,25 @@ DeeSeq_DefaultRRemoveWithError(DeeObject *self, DeeObject *item,
/* rremove() (with key) */
/************************************************************************/
INTERN WUNUSED NONNULL((1, 2, 5)) int DCALL
DeeSeq_DefaultRRemoveWithKeyWithEnumerateIndexReverseAndDelItemIndex(DeeObject *self, DeeObject *item,
size_t start, size_t end, DeeObject *key) {
DeeSeq_DefaultRRemoveWithKeyWithTSCRFindWithKeyAndDelItemIndex(DeeObject *self, DeeObject *item,
size_t start, size_t end, DeeObject *key) {
int result;
size_t index = (*DeeType_SeqCache_RequireRFindWithKey(Dee_TYPE(self)))(self, item, start, end, key);
if unlikely(index == (size_t)Dee_COMPARE_ERR)
goto err;
if (index == (size_t)-1)
return 0;
result = (*Dee_TYPE(self)->tp_seq->tp_delitem_index)(self, index);
if unlikely(result)
goto err;
return 1;
err:
return -1;
}

INTERN WUNUSED NONNULL((1, 2, 5)) int DCALL
DeeSeq_DefaultRRemoveWithKeyWithTSCEnumerateIndexReverseAndDelItemIndex(DeeObject *self, DeeObject *item,
size_t start, size_t end, DeeObject *key) {
Dee_ssize_t foreach_status;
struct default_remove_with_key_with_enumerate_index_and_delitem_index_data data;
Dee_tsc_enumerate_index_reverse_t tsc_enumerate_index_reverse;
Expand Down
6 changes: 5 additions & 1 deletion src/deemon/objects/seq/default-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ INTDEF WUNUSED NONNULL((1, 2)) int DCALL DeeSeq_DefaultRemoveWithCallRemoveDataM
INTDEF WUNUSED NONNULL((1, 2)) int DCALL DeeSeq_DefaultRemoveWithCallRemoveDataKwMethod(DeeObject *self, DeeObject *item, size_t start, size_t end);
INTDEF WUNUSED NONNULL((1, 2)) int DCALL DeeSeq_DefaultRemoveWithTSCRemoveAll(DeeObject *self, DeeObject *item, size_t start, size_t end);
INTDEF WUNUSED NONNULL((1, 2)) int DCALL DeeSeq_DefaultRemoveWithTSCRemoveIf(DeeObject *self, DeeObject *item, size_t start, size_t end);
INTDEF WUNUSED NONNULL((1, 2)) int DCALL DeeSeq_DefaultRemoveWithTSCFindAndDelItemIndex(DeeObject *self, DeeObject *item, size_t start, size_t end);
INTDEF WUNUSED NONNULL((1, 2)) int DCALL DeeSeq_DefaultRemoveWithEnumerateIndexAndDelItemIndex(DeeObject *self, DeeObject *item, size_t start, size_t end);
INTDEF WUNUSED NONNULL((1, 2)) int DCALL DeeSeq_DefaultRemoveWithError(DeeObject *self, DeeObject *item, size_t start, size_t end);

Expand All @@ -517,13 +518,15 @@ INTDEF WUNUSED NONNULL((1, 2, 5)) int DCALL DeeSeq_DefaultRemoveWithKeyWithCallR
INTDEF WUNUSED NONNULL((1, 2, 5)) int DCALL DeeSeq_DefaultRemoveWithKeyWithCallRemoveDataKwMethod(DeeObject *self, DeeObject *item, size_t start, size_t end, DeeObject *key);
INTDEF WUNUSED NONNULL((1, 2, 5)) int DCALL DeeSeq_DefaultRemoveWithKeyWithTSCRemoveAllWithKey(DeeObject *self, DeeObject *item, size_t start, size_t end, DeeObject *key);
INTDEF WUNUSED NONNULL((1, 2, 5)) int DCALL DeeSeq_DefaultRemoveWithKeyWithTSCRemoveIf(DeeObject *self, DeeObject *item, size_t start, size_t end, DeeObject *key);
INTDEF WUNUSED NONNULL((1, 2, 5)) int DCALL DeeSeq_DefaultRemoveWithKeyWithTSCFindWithKeyAndDelItemIndex(DeeObject *self, DeeObject *item, size_t start, size_t end, DeeObject *key);
INTDEF WUNUSED NONNULL((1, 2, 5)) int DCALL DeeSeq_DefaultRemoveWithKeyWithEnumerateIndexAndDelItemIndex(DeeObject *self, DeeObject *item, size_t start, size_t end, DeeObject *key);
INTDEF WUNUSED NONNULL((1, 2, 5)) int DCALL DeeSeq_DefaultRemoveWithKeyWithError(DeeObject *self, DeeObject *item, size_t start, size_t end, DeeObject *key);

INTDEF WUNUSED NONNULL((1, 2)) int DCALL DeeSeq_DefaultRRemoveWithCallAttrRRemove(DeeObject *self, DeeObject *item, size_t start, size_t end);
INTDEF WUNUSED NONNULL((1, 2)) int DCALL DeeSeq_DefaultRRemoveWithCallRRemoveDataFunction(DeeObject *self, DeeObject *item, size_t start, size_t end);
INTDEF WUNUSED NONNULL((1, 2)) int DCALL DeeSeq_DefaultRRemoveWithCallRRemoveDataMethod(DeeObject *self, DeeObject *item, size_t start, size_t end);
INTDEF WUNUSED NONNULL((1, 2)) int DCALL DeeSeq_DefaultRRemoveWithCallRRemoveDataKwMethod(DeeObject *self, DeeObject *item, size_t start, size_t end);
INTDEF WUNUSED NONNULL((1, 2)) int DCALL DeeSeq_DefaultRRemoveWithTSCRFindAndDelItemIndex(DeeObject *self, DeeObject *item, size_t start, size_t end);
INTDEF WUNUSED NONNULL((1, 2)) int DCALL DeeSeq_DefaultRRemoveWithTSCEnumerateIndexReverseAndDelItemIndex(DeeObject *self, DeeObject *item, size_t start, size_t end);
INTDEF WUNUSED NONNULL((1, 2)) int DCALL DeeSeq_DefaultRRemoveWithEnumerateIndexAndDelItemIndex(DeeObject *self, DeeObject *item, size_t start, size_t end);
INTDEF WUNUSED NONNULL((1, 2)) int DCALL DeeSeq_DefaultRRemoveWithError(DeeObject *self, DeeObject *item, size_t start, size_t end);
Expand All @@ -532,7 +535,8 @@ INTDEF WUNUSED NONNULL((1, 2, 5)) int DCALL DeeSeq_DefaultRRemoveWithKeyWithCall
INTDEF WUNUSED NONNULL((1, 2, 5)) int DCALL DeeSeq_DefaultRRemoveWithKeyWithCallRRemoveDataFunction(DeeObject *self, DeeObject *item, size_t start, size_t end, DeeObject *key);
INTDEF WUNUSED NONNULL((1, 2, 5)) int DCALL DeeSeq_DefaultRRemoveWithKeyWithCallRRemoveDataMethod(DeeObject *self, DeeObject *item, size_t start, size_t end, DeeObject *key);
INTDEF WUNUSED NONNULL((1, 2, 5)) int DCALL DeeSeq_DefaultRRemoveWithKeyWithCallRRemoveDataKwMethod(DeeObject *self, DeeObject *item, size_t start, size_t end, DeeObject *key);
INTDEF WUNUSED NONNULL((1, 2, 5)) int DCALL DeeSeq_DefaultRRemoveWithKeyWithEnumerateIndexReverseAndDelItemIndex(DeeObject *self, DeeObject *item, size_t start, size_t end, DeeObject *key);
INTDEF WUNUSED NONNULL((1, 2, 5)) int DCALL DeeSeq_DefaultRRemoveWithKeyWithTSCRFindWithKeyAndDelItemIndex(DeeObject *self, DeeObject *item, size_t start, size_t end, DeeObject *key);
INTDEF WUNUSED NONNULL((1, 2, 5)) int DCALL DeeSeq_DefaultRRemoveWithKeyWithTSCEnumerateIndexReverseAndDelItemIndex(DeeObject *self, DeeObject *item, size_t start, size_t end, DeeObject *key);
INTDEF WUNUSED NONNULL((1, 2, 5)) int DCALL DeeSeq_DefaultRRemoveWithKeyWithEnumerateIndexAndDelItemIndex(DeeObject *self, DeeObject *item, size_t start, size_t end, DeeObject *key);
INTDEF WUNUSED NONNULL((1, 2, 5)) int DCALL DeeSeq_DefaultRRemoveWithKeyWithError(DeeObject *self, DeeObject *item, size_t start, size_t end, DeeObject *key);

Expand Down

0 comments on commit 64898e2

Please sign in to comment.