diff --git a/.vs/deemon-v141.sln b/.vs/deemon-v141.sln index d8d8f041d..5d5890add 100644 --- a/.vs/deemon-v141.sln +++ b/.vs/deemon-v141.sln @@ -395,6 +395,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{2360741E-F ..\util\test\deemon-sequence-each-contains.dee = ..\util\test\deemon-sequence-each-contains.dee ..\util\test\deemon-sequence-each.dee = ..\util\test\deemon-sequence-each.dee ..\util\test\deemon-sequence-enumerate.dee = ..\util\test\deemon-sequence-enumerate.dee + ..\util\test\deemon-sequence-math.dee = ..\util\test\deemon-sequence-math.dee ..\util\test\deemon-sequence-reversed.dee = ..\util\test\deemon-sequence-reversed.dee ..\util\test\deemon-sequence-segments.dee = ..\util\test\deemon-sequence-segments.dee ..\util\test\deemon-sequence-sorted.dee = ..\util\test\deemon-sequence-sorted.dee diff --git a/.vs/deemon-v141.vcxproj b/.vs/deemon-v141.vcxproj index 5c9664f08..6f4138d21 100644 --- a/.vs/deemon-v141.vcxproj +++ b/.vs/deemon-v141.vcxproj @@ -276,7 +276,6 @@ - @@ -465,11 +464,8 @@ - - - diff --git a/.vs/deemon-v141.vcxproj.filters b/.vs/deemon-v141.vcxproj.filters index 53bcac3a4..ca8632af3 100644 --- a/.vs/deemon-v141.vcxproj.filters +++ b/.vs/deemon-v141.vcxproj.filters @@ -801,9 +801,6 @@ src\objects\seq - - src\objects\seq - src\objects\seq @@ -1364,21 +1361,12 @@ src\objects\seq - - src\objects\seq - src\objects\seq src\objects\seq - - src\objects - - - src\objects - src\objects diff --git a/.vs/deemon-v142.sln b/.vs/deemon-v142.sln index 3697125cb..b35553255 100644 --- a/.vs/deemon-v142.sln +++ b/.vs/deemon-v142.sln @@ -395,6 +395,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{2360741E-F ..\util\test\deemon-sequence-each-contains.dee = ..\util\test\deemon-sequence-each-contains.dee ..\util\test\deemon-sequence-each.dee = ..\util\test\deemon-sequence-each.dee ..\util\test\deemon-sequence-enumerate.dee = ..\util\test\deemon-sequence-enumerate.dee + ..\util\test\deemon-sequence-math.dee = ..\util\test\deemon-sequence-math.dee ..\util\test\deemon-sequence-reversed.dee = ..\util\test\deemon-sequence-reversed.dee ..\util\test\deemon-sequence-segments.dee = ..\util\test\deemon-sequence-segments.dee ..\util\test\deemon-sequence-sorted.dee = ..\util\test\deemon-sequence-sorted.dee diff --git a/.vs/deemon-v142.vcxproj b/.vs/deemon-v142.vcxproj index ce9e2cea8..c47f0fdc4 100644 --- a/.vs/deemon-v142.vcxproj +++ b/.vs/deemon-v142.vcxproj @@ -276,7 +276,6 @@ - @@ -465,11 +464,8 @@ - - - diff --git a/.vs/deemon-v142.vcxproj.filters b/.vs/deemon-v142.vcxproj.filters index 53bcac3a4..ca8632af3 100644 --- a/.vs/deemon-v142.vcxproj.filters +++ b/.vs/deemon-v142.vcxproj.filters @@ -801,9 +801,6 @@ src\objects\seq - - src\objects\seq - src\objects\seq @@ -1364,21 +1361,12 @@ src\objects\seq - - src\objects\seq - src\objects\seq src\objects\seq - - src\objects - - - src\objects - src\objects diff --git a/include/deemon/seq.h b/include/deemon/seq.h index e7ed6c382..7236bbc19 100644 --- a/include/deemon/seq.h +++ b/include/deemon/seq.h @@ -50,146 +50,12 @@ DECL_BEGIN -/* TODO: Once `CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS' is done, re-write this documentation! */ -/* NOTE: There are no `DeeSeq_Check()' macros because they wouldn't make sense. - * Being derived from `DeeSeq_Type' is _NOT_ mandatory when writing a - * sequence class. The only thing that it does do is allow usercode - * to safely query whether or not an object implements all of the standard - * sequence functions. - * Instead, a sequence object `ob' should be - * detected using `DeeType_IsSequence(Dee_TYPE(ob))'. - * The following things are required from sub-class of `Sequence': +/* The following things are required from sub-class of `Sequence': * - Must either implement `tp_iter' or `tp_sizeob' + `tp_getitem' - * The following things are implemented by `Sequence': - * - Abstraction that automatically defines the following operators: - * - tp_iter - * - tp_sizeob - * - tp_contains - * - tp_getitem (for integer-index argument only) - * - tp_getrange (for integer-index argument only) - * - tp_eq (Lexicographic element-wise compare with other iterables) - * - tp_ne (...) - * - tp_lo - * - tp_le - * - tp_gr - * - tp_ge - * - tp_add (Concat 2 sequences) - * - tp_mul (Repeat a sequence some number of times) - * - tp_or (Repeat the left sequence, then only yield elements from right not found in the left one as well) - * - tp_and (Convert the left sequence into a set, then only yield elements from the right found in that set) - * - tp_repr (Surrounded by `{ ... }', print a comma-separated list of all elements) - * - tp_bool (Indicates if the sequence is non-empty) - * - Abstraction that automatically defines the following getsets: - * - `isempty: bool' (Read-only; same as `tp_bool', but negated) - * - `isnonempty: bool' (Read-only; same as `tp_bool') - * - `length: int' (Read-only; same as `tp_sizeob') - * - `first: Object' (Read-write; same as `tp_getitem(0)' / `tp_setitem(0)') - * - `last: Object' (Read-write; same as `tp_getitem(length - 1)' / `tp_setitem(length - 1)') - * - `each: Sequence' (Read-only; Proxy sequence for construction expressions to-be applied to each element) - * - `ids: {int...}' (Read-only; Proxy sequence for object IDs for elements) - * - `types: {Type...}' (Read-only; Proxy sequence for element types) - * - `classes: {Type...}' (Read-only; Proxy sequence for element classes) - * - `isfrozen: bool' (Read-only; returns true if the sequence is frozen) - * - `frozen: Sequence' (Read-only; returns a frozen copy of the sequence) - * - Abstraction that automatically defines the following class getsets: - * - `Iterator: Type' - * Evaluates to the internally used iterator type when `DeeObject_Iter()' would - * return it. Otherwise, accessing this field raises an `Error.AttributeError'. - * The intention here is that a sub-class defining its own iterator should override - * this field in order to return its own type. - * - Abstraction that automatically defines the following methods: - * - `front(): Object' - * - `back(): Object' - * - `reduce(combine: Callable, init?): Object' - * - Call `combine()' on two consecutive objects, reusing the result as - * the first argument in the next call; return the final sum-style value. - * NOTE: When the sequence is empty, return `none' - * - `filter(keep: Callable): Sequence' - * - Same as `(for (local x: this) if (keep(x)) x)' - * - `sum(): Object' - * - Same as `reduce((a, b) -> a + b);' or `this + ...' - * - Preferred way to concat sequences containing strings: - * - `print ["foo", "bar", "foobar"].sum(); // "foobarfoobar"' - * - `any(): bool' - * - Same as `reduce((a, b) -> a || b, false);', but stop on the first `true'. - * - Same as `this || ...' - * - `all(): bool' - * - Same as `reduce((a, b) -> a && b, true);', but stop on the first `false'. - * - Same as `this && ...' - * - `parity(): bool' - * - Same as `((#this.filter(x -> !!x)) % 2) != 0' - * - `min(key: Callable = none): Object' - * - `max(key: Callable = none): Object' - * - `count(ob: Object, key: Callable = none): int' - * - `locate(ob: Object, key: Callable = none): Object' - * - `rlocate(ob: Object, key: Callable = none): Object' - * - `locateall(ob: Object, key: Callable = none): Sequence' - * - `transform(callable transformation): Sequence' - * - Invoke `transformation()' on all items and return a sequence of all the results. - * - Same as `(for (local x: this) transformation(x));' - * - `contains(ob: Object, key: Callable = none): bool' - * - Same as the `tp_contains' operator, but allows for a key function to be used. - * - `partition(ob: Object, key: Callable = none): (Sequence, (ob), Sequence)' - * - `rpartition(ob: Object, key: Callable = none): (Sequence, (ob), Sequence)' - * - `startswith(ob: Object, key: Callable = none): bool' - * - `endswith(ob: Object, key: Callable = none): bool' - * - `find(ob: Object, key: Callable = none): int' - * - `rfind(ob: Object, key: Callable = none): int' - * - `index(ob: Object, key: Callable = none): int' - * - `rindex(ob: Object, key: Callable = none): int' - * - `join(items: Sequence): Sequence' - * - `strip(ob: Object, key: Callable = none): Sequence' - * - `lstrip(ob: Object, key: Callable = none): Sequence' - * - `rstrip(ob: Object, key: Callable = none): Sequence' - * - `split(sep: Object, key: Callable = none): Sequence' - * - `reversed(): Sequence' - * - `sorted(key: Callable = none): Sequence' - * - `segments(segsize: int): Sequence' - * - `countseq(seq: Sequence, key: Callable = none): int' - * - `containsseq(seq: Sequence, key: Callable = none): bool' - * - `partitionseq(seq: Sequence, key: Callable = none): (Sequence, Sequence, Sequence)' - * - `rpartitionseq(seq: Sequence, key: Callable = none): (Sequence, Sequence, Sequence)' - * - `startswithseq(seq: Sequence, key: Callable = none): bool' - * - `endswithseq(seq: Sequence, key: Callable = none): bool' - * - `findseq(seq: Sequence, key: Callable = none): int' - * - `rfindseq(seq: Sequence, key: Callable = none): int' - * - `indexseq(seq: Sequence, key: Callable = none): int' - * - `rindexseq(seq: Sequence, key: Callable = none): int' - * - `stripseq(items: Sequence, key: Callable = none): Sequence' - * - `lstripseq(items: Sequence, key: Callable = none): Sequence' - * - `rstripseq(items: Sequence, key: Callable = none): Sequence' - * - `splitseq(sep_seq: Sequence, key: Callable = none): Sequence' - * - ... // More exist; please consult http://localhost:8080/modules/deemon/Sequence - * Some operations (Such as `tp_add') will create instances of special objects - * that will only start invoking underlying operators when worked with: - * >> function foo() { - * >> print "In foo()"; - * >> yield 10; - * >> } - * >> function bar() { - * >> print "In bar()"; - * >> yield 20; - * >> } - * >> - * >> local combine = foo()+bar(); // Create a merged-sequence object - * >> // At this point, neither function has started executing, yet. - * >> for (local x: combine) { - * >> print x; // "In foo()", "10", "In bar()", "20" - * >> } - * Other operators/functions may also invoke iteration more than once. - * Which operators/functions do this is intentionally - * not revealed and is subject to change in the future. - * Code expecting certain types of sequences (or mutable sequence for that) - * should always perform an explicit cast to the desired sequence type: - * >> local a = [5, 10, 20, 30, 5]; - * >> local b = a.strip(5); - * >> for (local x: b) - * >> print x; // 10 20 30 - * >> print type b; // Undefined and subject to change - * >> b = [b...]; // Explicit cast to list; same as `b = list(b);' - * HINT: Instantiating `seq' as-is will yield `Dee_EmptySeq', - * which in return is re-used internally as a placeholder - * to represent an empty, general-purpose sequence. */ + * Many things are implemented by `Sequence'. + * For a full list, see http://localhost:8080/modules/deemon/Sequence + * + * HINT: Instantiating `Sequence' as-is will return a copy of `Dee_EmptySeq' */ DDATDEF DeeTypeObject DeeSeq_Type; /* `Sequence from deemon' */ #define DeeSeq_Check(ob) DeeObject_Implements(ob, &DeeSeq_Type) @@ -468,6 +334,12 @@ struct Dee_type_nsi { ; }; +/* Lookup the closest NSI descriptor for `tp', or return `NULL' + * if the top-most type implementing any sequence operator doesn't + * expose NSI functionality. */ +DFUNDEF WUNUSED NONNULL((1)) struct Dee_type_nsi const *DCALL +DeeType_NSI(DeeTypeObject *__restrict tp); /* !!! DEPRECATED !!! */ + /*****************************************************************************************/ /* */ @@ -527,12 +399,6 @@ DFUNDEF ATTR_CONST WUNUSED size_t DCALL DeeSeqRange_DoClamp_n(Dee_ssize_t start, size_t size); -/* Lookup the closest NSI descriptor for `tp', or return `NULL' - * if the top-most type implementing any sequence operator doesn't - * expose NSI functionality. */ -DFUNDEF WUNUSED NONNULL((1)) struct Dee_type_nsi const *DCALL -DeeType_NSI(DeeTypeObject *__restrict tp); - /* Create new range sequence objects. */ DFUNDEF WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL DeeRange_New(DeeObject *begin, DeeObject *end, DeeObject *step); @@ -548,27 +414,6 @@ DFUNDEF WUNUSED NONNULL((1)) DREF DeeObject *DCALL DeeSeq_Min(DeeObject *self); DFUNDEF WUNUSED NONNULL((1)) DREF DeeObject *DCALL DeeSeq_Max(DeeObject *self); -#ifdef CONFIG_BUILDING_DEEMON -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS - -/* @return: == -2: An error occurred. - * @return: == -1: `self < some_object' - * @return: == 0: Objects compare as equal - * @return: == 1: `self > some_object' */ -INTDEF WUNUSED NONNULL((1)) int DCALL DeeSeq_CompareIV(DeeObject *lhs, DeeObject *const *rhsv, size_t rhsc); /* ITERATOR <=> VECTOR */ -INTDEF WUNUSED NONNULL((1, 2)) int DCALL DeeSeq_CompareII(DeeObject *lhs, DeeObject *rhs); /* ITERATOR <=> ITERATOR */ -INTDEF WUNUSED NONNULL((1, 2)) int DCALL DeeSeq_CompareIS(DeeObject *lhs, DeeObject *rhs); /* ITERATOR <=> SEQUENCE */ - -/* @return: == -1: An error occurred. - * @return: == 0: Sequences differ - * @return: == 1: Sequences are equal */ -INTDEF WUNUSED NONNULL((1)) int DCALL DeeSeq_EqIV(DeeObject *lhs, DeeObject *const *rhsv, size_t rhsc); /* ITERATOR == VECTOR */ -INTDEF WUNUSED NONNULL((1, 2)) int DCALL DeeSeq_EqII(DeeObject *lhs, DeeObject *rhs); /* ITERATOR == ITERATOR */ -INTDEF WUNUSED NONNULL((1, 2)) int DCALL DeeSeq_EqIS(DeeObject *lhs, DeeObject *rhs); /* ITERATOR == SEQUENCE */ -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ -#endif /* CONFIG_BUILDING_DEEMON */ - - /* Possible return values for `DeeType_GetSeqClass()' */ #define Dee_SEQCLASS_UNKNOWN 0 /* Never returned by `DeeType_GetSeqClass()' (used internally) */ #define Dee_SEQCLASS_NONE 1 /* Type does not inherit from "Sequence" */ diff --git a/include/deemon/set.h b/include/deemon/set.h index 9928a7b4f..778b031cb 100644 --- a/include/deemon/set.h +++ b/include/deemon/set.h @@ -118,21 +118,6 @@ DDATDEF DeeObject DeeSet_UniversalInstance; #endif /* !GUARD_DEEMON_OBJECTS_SEQ_DEFAULT_SETS_C */ #define Dee_UniversalSet (&DeeSet_UniversalInstance) - -#ifdef CONFIG_BUILDING_DEEMON -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS -INTDEF WUNUSED NONNULL((1)) DREF DeeObject *DCALL DeeSet_Invert(DeeObject *__restrict self); -INTDEF WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL DeeSet_OperatorAdd(DeeObject *lhs, DeeObject *rhs); -INTDEF WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL DeeSet_OperatorSub(DeeObject *lhs, DeeObject *rhs); -INTDEF WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL DeeSet_OperatorAnd(DeeObject *lhs, DeeObject *rhs); -INTDEF WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL DeeSet_OperatorXor(DeeObject *lhs, DeeObject *rhs); -INTDEF WUNUSED NONNULL((1, 2)) int DCALL DeeSet_IsSubSet(DeeObject *lhs, DeeObject *rhs); -INTDEF WUNUSED NONNULL((1, 2)) int DCALL DeeSet_IsTrueSubSet(DeeObject *lhs, DeeObject *rhs); -INTDEF WUNUSED NONNULL((1, 2)) int DCALL DeeSet_IsSameSet(DeeObject *lhs, DeeObject *rhs); -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ -INTDEF WUNUSED NONNULL((1, 2)) int DCALL DeeSet_IsDisjoint(DeeObject *lhs, DeeObject *rhs); -#endif /* CONFIG_BUILDING_DEEMON */ - DECL_END #endif /* !GUARD_DEEMON_SET_H */ diff --git a/lib/rt/builtins.dee b/lib/rt/builtins.dee index 9d51be1ed..5c67fa09e 100644 --- a/lib/rt/builtins.dee +++ b/lib/rt/builtins.dee @@ -455,7 +455,7 @@ local __objects = deemon.List { /* 0x0189 */ deemon.Sequence.contains, /* 0x018a */ net.socket, /* 0x018b */ deemon.hasattr, - /* 0x018c */ none, /* TODO: SeqSubRangeIterator_Type (CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS) */ + /* 0x018c */ rt.IterWithSizeAndGetItem, /* SeqSubRangeIterator_Type (lots of other types would match here also, but we can only set one) */ /* 0x018d */ none, /* DeeSocketFile_Type -- Sockets no longer implement the File protocol */ /* 0x018e */ none, /* DeeSocketFileTo_Type -- Sockets no longer implement the File protocol */ /* 0x018f */ rt.StringIterator, /* DeeWideStringIterator_Type */ diff --git a/src/deemon/objects/map.c b/src/deemon/objects/map.c index 798c0ccc1..eb6b984e7 100644 --- a/src/deemon/objects/map.c +++ b/src/deemon/objects/map.c @@ -65,311 +65,6 @@ map_byhash(DeeObject *self, size_t argc, return NULL; } -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -map_get(DeeObject *self, size_t argc, DeeObject *const *argv) { - DREF DeeObject *result; - DeeObject *key, *def = Dee_None; - if (DeeArg_Unpack(argc, argv, "o|o:get", &key, &def)) - goto err; - result = DeeObject_TryGetItem(self, key); - if (result == ITER_DONE) { - result = def; - Dee_Incref(def); - } - return result; -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -map_setdefault(DeeObject *self, size_t argc, DeeObject *const *argv) { - struct type_nsi const *nsi; - DREF DeeObject *result; - DeeObject *key, *value = Dee_None; - if (DeeArg_Unpack(argc, argv, "o|o:setdefault", &key, &value)) - goto err; - nsi = DeeType_NSI(Dee_TYPE(self)); - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_MAP && nsi->nsi_maplike.nsi_setdefault) - return (*nsi->nsi_maplike.nsi_setdefault)(self, key, value); - - /* Fallback: lookup key and override if not already present (thread-unsafe) */ - result = DeeObject_TryGetItem(self, key); - if (result == ITER_DONE) { - if unlikely(DeeObject_SetItem(self, key, value)) - goto err; - result = value; - Dee_Incref(value); - } - return result; -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -map_pop(DeeObject *self, size_t argc, DeeObject *const *argv) { - DREF DeeObject *result; - DeeObject *key, *def = NULL; - if (DeeArg_Unpack(argc, argv, "o|o:pop", &key, &def)) - goto err; - result = DeeObject_TryGetItem(self, key); - if (result == ITER_DONE) { - /* Not present -> use default */ - result = def; - if unlikely(!result) { - err_unknown_key((DeeObject *)self, key); - } else { - Dee_Incref(result); - } - } else if (result != NULL) { - /* Remove `key' from `self' */ - if unlikely(DeeObject_DelItem(self, key)) - goto err_r; - } - return result; -err_r: - Dee_Decref(result); -err: - return NULL; -} - - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -map_popitem(DeeObject *self, size_t argc, DeeObject *const *argv) { - int temp; - DREF DeeObject *result, *iter, *key; - if (DeeArg_Unpack(argc, argv, ":popitem")) - goto err; - iter = DeeObject_Iter(self); - if unlikely(!iter) - goto err; - result = DeeObject_IterNext(iter); - Dee_Decref_likely(iter); - if unlikely(!result) - goto err; - if (result == ITER_DONE) - goto err_empty; - key = DeeObject_GetItemIndex(result, 0); - if unlikely(!key) - goto err_r; - temp = DeeObject_DelItem(self, key); - Dee_Decref(key); - if unlikely(temp) - goto err_r; - return result; -err_empty: - err_empty_sequence(self); - goto err; -err_r: - Dee_Decref(result); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -map_clear(DeeObject *self, size_t argc, DeeObject *const *argv) { - if (DeeArg_Unpack(argc, argv, ":clear")) - goto err; - for (;;) { - int temp; - DREF DeeObject *item, *iter, *key; - iter = DeeObject_Iter(self); - if unlikely(!iter) - goto err; - item = DeeObject_IterNext(iter); - Dee_Decref_likely(iter); - if unlikely(!item) - goto err; - if (item == ITER_DONE) - break; - key = DeeObject_GetItemIndex(item, 0); - Dee_Decref(item); - if unlikely(!key) - goto err; - temp = DeeObject_DelItem(self, key); - Dee_Decref(key); - if unlikely(temp) - goto err; - } - return_none; -err: - return NULL; -} - - -#if __SIZEOF_SIZE_T__ == __SIZEOF_INT__ -#define map_update_callback (*(Dee_foreach_pair_t)&DeeObject_SetItem) -#else /* __SIZEOF_SIZE_T__ == __SIZEOF_INT__ */ -PRIVATE WUNUSED NONNULL((2, 3)) Dee_ssize_t DCALL -map_update_callback(void *arg, DeeObject *key, DeeObject *value) { - return DeeObject_SetItem((DeeObject *)arg, key, value); -} -#endif /* __SIZEOF_SIZE_T__ != __SIZEOF_INT__ */ - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -map_update(DeeObject *self, size_t argc, DeeObject *const *argv) { - DeeObject *seq; - if (DeeArg_Unpack(argc, argv, "o:update", &seq)) - goto err; - if (self != seq) { - if unlikely(DeeObject_ForeachPair(seq, &map_update_callback, self)) - goto err; - } - return_none; -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -map_setold(DeeObject *self, size_t argc, DeeObject *const *argv) { - struct type_nsi const *nsi; - DREF DeeObject *old_value; - DeeObject *key, *value; - if (DeeArg_Unpack(argc, argv, "oo:setold", &key, &value)) - goto err; - nsi = DeeType_NSI(Dee_TYPE(self)); - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_MAP && nsi->nsi_maplike.nsi_updateold) { - int error; - error = (*nsi->nsi_maplike.nsi_updateold)(self, key, value, NULL); - if unlikely(error < 0) - goto err; - return_bool_(error > 0); - } - - /* Fallback: must use thread-unsafe (multi-step) operations. */ - old_value = DeeObject_TryGetItem(self, key); - if (ITER_ISOK(old_value)) { - Dee_Decref(old_value); - if unlikely(DeeObject_SetItem(self, key, value)) - goto err; - return_true; - } - if unlikely(!old_value) - goto err; - return_false; -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -map_setnew(DeeObject *self, size_t argc, DeeObject *const *argv) { - struct type_nsi const *nsi; - DREF DeeObject *old_value; - DeeObject *key, *value; - if (DeeArg_Unpack(argc, argv, "oo:setnew", &key, &value)) - goto err; - nsi = DeeType_NSI(Dee_TYPE(self)); - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_MAP && nsi->nsi_maplike.nsi_insertnew) { - int error; - error = (*nsi->nsi_maplike.nsi_insertnew)(self, key, value, NULL); - if unlikely(error < 0) - goto err; - return_bool_(error == 0); - } - - /* Fallback: must use thread-unsafe (multi-step) operations. */ - old_value = DeeObject_TryGetItem(self, key); - if (ITER_ISOK(old_value)) { - Dee_Decref(old_value); - return_false; - } - if unlikely(!old_value) - goto err; - if unlikely(DeeObject_SetItem(self, key, value)) - goto err; - return_false; -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -map_setold_ex(DeeObject *self, size_t argc, DeeObject *const *argv) { - struct type_nsi const *nsi; - DREF DeeObject *old_value, *result; - DeeObject *key, *value; - if (DeeArg_Unpack(argc, argv, "oo:setold_ex", &key, &value)) - goto err; - nsi = DeeType_NSI(Dee_TYPE(self)); - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_MAP && nsi->nsi_maplike.nsi_updateold) { - int error; - error = (*nsi->nsi_maplike.nsi_updateold)(self, key, value, &old_value); - if unlikely(error < 0) - goto err; - if (error == 1) { - result = DeeTuple_Pack(2, Dee_True, old_value); - Dee_Decref_unlikely(old_value); - } else { - result = DeeTuple_Pack(2, Dee_False, Dee_None); - } - return result; - } - - /* Fallback: must use thread-unsafe (multi-step) operations. */ - old_value = DeeObject_TryGetItem(self, key); - if (ITER_ISOK(old_value)) { - if unlikely(DeeObject_SetItem(self, key, value)) { - Dee_Decref(old_value); - goto err; - } - result = DeeTuple_Pack(2, Dee_True, old_value); - Dee_Decref_unlikely(old_value); - return result; - } - if unlikely(!old_value) - goto err; - return DeeTuple_Pack(2, Dee_False, Dee_None); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -map_setnew_ex(DeeObject *self, size_t argc, DeeObject *const *argv) { - struct type_nsi const *nsi; - DREF DeeObject *result, *old_value; - DeeObject *key, *value; - if (DeeArg_Unpack(argc, argv, "oo:setnew_ex", &key, &value)) - goto err; - nsi = DeeType_NSI(Dee_TYPE(self)); - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_MAP && nsi->nsi_maplike.nsi_insertnew) { - int error; - error = (*nsi->nsi_maplike.nsi_insertnew)(self, key, value, &old_value); - if unlikely(error < 0) - goto err; - if (error == 0) { - result = DeeTuple_Pack(2, Dee_True, Dee_None); - } else { - result = DeeTuple_Pack(2, Dee_False, old_value); - Dee_Decref_unlikely(old_value); - } - return result; - } - - /* Fallback: must use thread-unsafe (multi-step) operations. */ - old_value = DeeObject_TryGetItem(self, key); - if (ITER_ISOK(old_value)) { - result = DeeTuple_Pack(2, Dee_False, old_value); - Dee_Decref_unlikely(old_value); - return result; - } - if unlikely(!old_value) - goto err; - if unlikely(DeeObject_SetItem(self, key, value)) - goto err; - return DeeTuple_Pack(2, Dee_True, Dee_None); -err: - return NULL; -} - -#ifndef CONFIG_NO_DEEMON_100_COMPAT -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -map_insert_all(DeeObject *self, size_t argc, DeeObject *const *argv) { - return DeeObject_CallAttrString(self, "update", argc, argv); -} -#endif /* !CONFIG_NO_DEEMON_100_COMPAT */ -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - - - DOC_DEF(map_get_doc, "(key,def=!N)->\n" @@ -397,1467 +92,175 @@ DOC_DEF(map_popitem_doc, DOC_DEF(map_update_doc, "(items:?S?T2?O?O)\n" "Iterate @items and unpack each element into 2 others, " - /**/ "using them as key and value to insert into @this ?."); -DOC_DEF(map_setold_doc, - "(key,value)->?Dbool\n" - "#r{Indicative of @value having been assigned to @key}" - "Assign @value to @key, only succeeding when @key already existed to begin with"); -DOC_DEF(map_setnew_doc, - "(key,value)->?Dbool\n" - "#r{Indicative of @value having been assigned to @key}" - "Assign @value to @key, only succeeding when @key didn't exist before"); -DOC_DEF(map_setold_ex_doc, - "(key,value)->?T2?Dbool?O\n" - "#r{A pair of values (new-value-was-assigned, old-value-or-none)}" - "Same as ?#setold but also return the previously assigned value"); -DOC_DEF(map_setnew_ex_doc, - "(key,value)->?T2?Dbool?O\n" - "#r{A pair of values (new-value-was-assigned, old-value-or-none)}" - "Same as ?#setnew but return the previously assigned value on failure"); - -INTDEF struct type_method tpconst map_methods[]; -INTERN_TPCONST struct type_method tpconst map_methods[] = { -#ifdef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS - /* Default operations for all mappings. */ - TYPE_METHOD(STR_get, &default_map_get, DOC_GET(map_get_doc)), - TYPE_KWMETHOD("byhash", &map_byhash, DOC_GET(map_byhash_doc)), - - /* Default operations for modifiable mappings. */ - TYPE_METHOD(STR_setold, &default_map_setold, DOC_GET(map_setold_doc)), - TYPE_METHOD(STR_setold_ex, &default_map_setold_ex, DOC_GET(map_setold_ex_doc)), - TYPE_METHOD(STR_setnew, &default_map_setnew, DOC_GET(map_setnew_doc)), - TYPE_METHOD(STR_setnew_ex, &default_map_setnew_ex, DOC_GET(map_setnew_ex_doc)), - TYPE_METHOD(STR_setdefault, &default_map_setdefault, DOC_GET(map_setdefault_doc)), - TYPE_METHOD(STR_update, &default_map_update, DOC_GET(map_update_doc)), - TYPE_METHOD(STR_remove, &default_map_remove, "(key)->?Dbool"), - TYPE_METHOD(STR_removekeys, &default_map_removekeys, "(keys:?S?O)"), - TYPE_METHOD(STR_pop, &default_map_pop, DOC_GET(map_pop_doc)), - TYPE_METHOD(STR_popitem, &default_map_popitem, DOC_GET(map_popitem_doc)), - - TYPE_METHOD("__contains__", &default_map___contains__, - "(item)->?Dbool\n" - "Alias for ${item in (this as Mapping)}"), - TYPE_METHOD("__getitem__", &default_map___getitem__, - "(key)->\n" - "Alias for ${(this as Mapping)[key]}"), - TYPE_METHOD("__delitem__", &default_map___delitem__, - "(key)\n" - "Alias for ${del (this as Mapping)[key]}"), - TYPE_METHOD("__setitem__", &default_map___setitem__, - "(key,value)\n" - "Alias for ${(this as Mapping)[key] = value}"), - TYPE_METHOD("__enumerate__", &default_map___enumerate__, - "(cb)->\n" - "Alias for ${(this as Mapping).enumerate(cb)}"), - TYPE_KWMETHOD("__enumerate_index__", &default_map___enumerate_index__, - "(cb,start=!0,end:?Dint=!A!Dint!PSIZE_MAX)->\n" - "Alias for ${Mapping.enumerate(this, cb, start, end)}"), - TYPE_METHOD("__iterkeys__", &default_map___iterkeys__, - "->?DIterator\n" - "Alias for: ${(this as Mapping).iterkeys}"), - TYPE_METHOD("__bounditem__", &default_map___bounditem__, - "(key,allow_missing=!t)->?Dbool\n" - "Alias for ${deemon.bounditem(this as Mapping, key, allow_missing)}"), - TYPE_METHOD("__hasitem__", &default_map___hasitem__, - "(key)->?Dbool\n" - "Alias for ${deemon.hasitem(this as Mapping, key)}"), - TYPE_METHOD("__getitem_index__", &default_map___getitem_index__, - "(key:?Dint)->\n" - "Alias for ${(this as Mapping)[key]}"), - TYPE_METHOD("__delitem_index__", &default_map___delitem_index__, - "(key:?Dint)\n" - "Alias for ${del (this as Mapping)[key]}"), - TYPE_METHOD("__setitem_index__", &default_map___setitem_index__, - "(key:?Dint,value)\n" - "Alias for ${(this as Mapping)[key] = value}"), - TYPE_METHOD("__bounditem_index__", &default_map___bounditem_index__, - "(key:?Dint,allow_missing=!t)->?Dbool\n" - "Alias for ${deemon.bounditem(this as Mapping, key, allow_missing)}"), - TYPE_METHOD("__hasitem_index__", &default_map___hasitem_index__, - "(key:?Dint)->?Dbool\n" - "Alias for ${deemon.hasitem(this as Mapping, key)}"), - TYPE_METHOD("__trygetitem__", &default_map___trygetitem__, - "(key,def=!N)->\n" - "Alias for ${(this as Mapping).get(key, def)"), - TYPE_METHOD("__trygetitem_index__", &default_map___trygetitem_index__, - "(key:?Dint,def=!N)->\n" - "Alias for ${(this as Mapping).get(key, def)"), - TYPE_METHOD("__trygetitem_string__", &default_map___trygetitem_string__, - "(key:?X2?DBytes?Dstring,def=!N)->\n" - "Alias for ${(this as Mapping).get(key, def)"), - TYPE_METHOD("__getitem_string__", &default_map___getitem_string__, - "(key:?X2?DBytes?Dstring)->\n" - "Alias for ${(this as Mapping)[key]}"), - TYPE_METHOD("__delitem_string__", &default_map___delitem_string__, - "(key:?X2?DBytes?Dstring)\n" - "Alias for ${del (this as Mapping)[key]}"), - TYPE_METHOD("__setitem_string__", &default_map___setitem_string__, - "(key:?X2?DBytes?Dstring,value)\n" - "Alias for ${(this as Mapping)[key] = value}"), - TYPE_METHOD("__bounditem_string__", &default_map___bounditem_string__, - "(key:?X2?DBytes?Dstring,allow_missing=!t)->?Dbool\n" - "Alias for ${deemon.bounditem(this as Mapping, key, allow_missing)}"), - TYPE_METHOD("__hasitem_string__", &default_map___hasitem_string__, - "(key:?X2?DBytes?Dstring)->?Dbool\n" - "Alias for ${deemon.hasitem(this as Mapping, key)}"), - TYPE_METHOD("__hash__", &default_map___hash__, - "->?Dint\n" - "Alias for ${(this as Mapping).operator hash()}"), - TYPE_METHOD("__compare_eq__", &default_map___compare_eq__, - "(rhs:?S?O)->?Dbool\n" - "Alias for ${(this as Mapping).operator == (rhs)}"), - TYPE_METHOD("__trycompare_eq__", &default_map___trycompare_eq__, - "(rhs:?S?O)->?Dbool\n" - "Alias for ${deemon.equals(this as Mapping, rhs)}"), - TYPE_METHOD("__eq__", &default_map___eq__, - "(rhs:?S?O)->?Dbool\n" - "Alias for ${(this as Mapping) == (rhs)}"), - TYPE_METHOD("__ne__", &default_map___ne__, - "(rhs:?S?O)->?Dbool\n" - "Alias for ${(this as Mapping) != (rhs)}"), - TYPE_METHOD("__lo__", &default_map___lo__, - "(rhs:?S?O)->?Dbool\n" - "Alias for ${(this as Mapping) < (rhs)}"), - TYPE_METHOD("__le__", &default_map___le__, - "(rhs:?S?O)->?Dbool\n" - "Alias for ${(this as Mapping) <= (rhs)}"), - TYPE_METHOD("__gr__", &default_map___gr__, - "(rhs:?S?O)->?Dbool\n" - "Alias for ${(this as Mapping) > (rhs)}"), - TYPE_METHOD("__ge__", &default_map___ge__, - "(rhs:?S?O)->?Dbool\n" - "Alias for ${(this as Mapping) >= (rhs)}"), - - TYPE_METHOD("__add__", &default_map___add__, - "(rhs:?S?O)->?DMapping\n" - "Alias for ${(this as Mapping) + rhs}"), - TYPE_METHOD("__sub__", &default_map___sub__, - "(rhs:?S?O)->?DMapping\n" - "Alias for ${(this as Mapping) - rhs}"), - TYPE_METHOD("__and__", &default_map___and__, - "(rhs:?S?O)->?DMapping\n" - "Alias for ${(this as Mapping) & rhs}"), - TYPE_METHOD("__xor__", &default_map___xor__, - "(rhs:?S?O)->?DMapping\n" - "Alias for ${(this as Mapping) ^ rhs}"), - TYPE_METHOD("__inplace_add__", &default_map___inplace_add__, - "(rhs:?S?O)->?.\n" - "Alias for ${(this as Mapping) += rhs}"), - TYPE_METHOD("__inplace_sub__", &default_map___inplace_sub__, - "(rhs:?S?O)->?.\n" - "Alias for ${(this as Mapping) -= rhs}"), - TYPE_METHOD("__inplace_and__", &default_map___inplace_and__, - "(rhs:?S?O)->?.\n" - "Alias for ${(this as Mapping) &= rhs}"), - TYPE_METHOD("__inplace_xor__", &default_map___inplace_xor__, - "(rhs:?S?O)->?.\n" - "Alias for ${(this as Mapping) ^= rhs}"), - TYPE_METHOD("__or__", &default_map___or__, - "(rhs:?S?O)->?DMapping\n" - "Alias for ?#__and__"), - TYPE_METHOD("__inplace_or__", &default_map___inplace_or__, - "(rhs:?S?O)->?DMapping\n" - "Alias for ?#__inplace_and__"), - - /* Old function names. */ -#ifndef CONFIG_NO_DEEMON_100_COMPAT - TYPE_METHOD("insert_all", &default_map_update, - "(items:?S?T2?O?O)\n" - "A deprecated alias for ?#update"), -#endif /* !CONFIG_NO_DEEMON_100_COMPAT */ -#else /* CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - /* Default operations for all mappings. */ - TYPE_METHOD(STR_get, &map_get, DOC_GET(map_get_doc)), - TYPE_KWMETHOD("byhash", &map_byhash, DOC_GET(map_byhash_doc)), - - /* Default operations for modifiable mappings. */ - TYPE_METHOD("setdefault", &map_setdefault, DOC_GET(map_setdefault_doc)), - TYPE_METHOD("pop", &map_pop, DOC_GET(map_pop_doc)), - TYPE_METHOD("popitem", &map_popitem, DOC_GET(map_popitem_doc)), - TYPE_METHOD("clear", &map_clear, - "()\n" - "Clear all values from @this ?."), - TYPE_METHOD("update", &map_update, DOC_GET(map_update_doc)), - TYPE_METHOD("setold", &map_setold, DOC_GET(map_setold_doc)), - TYPE_METHOD("setnew", &map_setnew, DOC_GET(map_setnew_doc)), - TYPE_METHOD("setold_ex", &map_setold_ex, DOC_GET(map_setold_ex_doc)), - TYPE_METHOD("setnew_ex", &map_setnew_ex, DOC_GET(map_setnew_ex_doc)), - - /* Old function names. */ -#ifndef CONFIG_NO_DEEMON_100_COMPAT - TYPE_METHOD("insert_all", &map_insert_all, - "(items:?S?T2?O?O)\n" - "A deprecated alias for ?#update"), -#endif /* !CONFIG_NO_DEEMON_100_COMPAT */ -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - TYPE_METHOD_END -}; - - - -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS -typedef struct { - OBJECT_HEAD - DREF DeeObject *mpi_iter; /* [1..1][const] The iterator for enumerating `mpi_map'. */ - DREF DeeObject *mpi_map; /* [1..1][const] The mapping object for which this is a proxy. */ - struct type_nsi const *mpi_nsi; /* [0..1][const][->nsi_class == TYPE_SEQX_CLASS_MAP] If available, the NSI interface of `mpi_map'. */ -} MapProxyIterator; - -PRIVATE WUNUSED NONNULL((1)) int DCALL -proxy_iterator_ctor(MapProxyIterator *__restrict self) { - self->mpi_iter = DeeObject_Iter(Dee_EmptyMapping); - if unlikely(!self->mpi_iter) - goto err; - self->mpi_map = Dee_EmptyMapping; - Dee_Incref(self->mpi_map); - self->mpi_nsi = NULL; - return 0; -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -proxy_iterator_init(MapProxyIterator *__restrict self, - size_t argc, DeeObject *const *argv) { - if (DeeArg_Unpack(argc, argv, "o:_mappingproxy.Iterator", &self->mpi_map)) - goto err; - self->mpi_iter = DeeObject_Iter(self->mpi_map); - if unlikely(!self->mpi_iter) - goto err; - Dee_Incref(self->mpi_map); - self->mpi_nsi = DeeType_NSI(Dee_TYPE(self->mpi_map)); - if (self->mpi_nsi && self->mpi_nsi->nsi_class != TYPE_SEQX_CLASS_MAP) - self->mpi_nsi = NULL; - return 0; -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1, 2)) int DCALL -proxy_iterator_copy(MapProxyIterator *__restrict self, - MapProxyIterator *__restrict other) { - self->mpi_iter = DeeObject_Copy(other->mpi_iter); - if unlikely(!self->mpi_iter) - goto err; - self->mpi_map = other->mpi_map; - Dee_Incref(self->mpi_map); - self->mpi_nsi = other->mpi_nsi; - return 0; -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1, 2)) int DCALL -proxy_iterator_deepcopy(MapProxyIterator *__restrict self, - MapProxyIterator *__restrict other) { - self->mpi_iter = DeeObject_DeepCopy(other->mpi_iter); - if unlikely(!self->mpi_iter) - goto err; - self->mpi_map = DeeObject_DeepCopy(other->mpi_map); - if unlikely(!self->mpi_iter) - goto err_iter; - self->mpi_nsi = other->mpi_nsi; - return 0; -err_iter: - Dee_Decref(self->mpi_iter); -err: - return -1; -} - -PRIVATE NONNULL((1)) void DCALL -proxy_iterator_fini(MapProxyIterator *__restrict self) { - Dee_Decref(self->mpi_iter); - Dee_Decref(self->mpi_map); -} - -PRIVATE NONNULL((1, 2)) void DCALL -proxy_iterator_visit(MapProxyIterator *__restrict self, dvisit_t proc, void *arg) { - Dee_Visit(self->mpi_iter); - Dee_Visit(self->mpi_map); -} - -PRIVATE NONNULL((1)) int DCALL -proxy_iterator_bool(MapProxyIterator *__restrict self) { - return DeeObject_Bool(self->mpi_iter); -} - -PRIVATE struct type_member tpconst proxy_iterator_members[] = { - TYPE_MEMBER_FIELD_DOC(STR_seq, STRUCT_OBJECT, offsetof(MapProxyIterator, mpi_map), "->?DMapping"), - TYPE_MEMBER_FIELD_DOC("__iter__", STRUCT_OBJECT, offsetof(MapProxyIterator, mpi_iter), "->?DIterator"), - TYPE_MEMBER_END -}; - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -proxy_iterator_next_key(MapProxyIterator *__restrict self) { - DREF DeeObject *pair; - DREF DeeObject *key_and_value[2]; - int error; - /* Optimize using NSI */ - if (self->mpi_nsi && self->mpi_nsi->nsi_maplike.nsi_nextkey) - return (*self->mpi_nsi->nsi_maplike.nsi_nextkey)(self->mpi_iter); - pair = DeeObject_IterNext(self->mpi_iter); - if (pair == ITER_DONE) - return ITER_DONE; - if unlikely(!pair) - goto err; - error = DeeObject_Unpack(pair, 2, key_and_value); - Dee_Decref(pair); - if unlikely(error) - goto err; - Dee_Decref(key_and_value[1]); - return key_and_value[0]; -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -proxy_iterator_next_value(MapProxyIterator *__restrict self) { - DREF DeeObject *pair; - DREF DeeObject *key_and_value[2]; - int error; - /* Optimize using NSI */ - if (self->mpi_nsi && self->mpi_nsi->nsi_maplike.nsi_nextvalue) - return (*self->mpi_nsi->nsi_maplike.nsi_nextvalue)(self->mpi_iter); - pair = DeeObject_IterNext(self->mpi_iter); - if (pair == ITER_DONE) - return ITER_DONE; - if unlikely(!pair) - goto err; - error = DeeObject_Unpack(pair, 2, key_and_value); - Dee_Decref(pair); - if unlikely(error) - goto err; - Dee_Decref(key_and_value[0]); - return key_and_value[1]; -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -proxy_iterator_next_item(MapProxyIterator *__restrict self) { - return DeeObject_IterNext(self->mpi_iter); -} - - -PRIVATE DeeTypeObject DeeMappingProxyIterator_Type = { - OBJECT_HEAD_INIT(&DeeType_Type), - /* .tp_name = */ "_MappingProxyIterator", - /* .tp_doc = */ NULL, - /* .tp_flags = */ TP_FNORMAL, - /* .tp_weakrefs = */ 0, - /* .tp_features = */ TF_NONE, - /* .tp_base = */ &DeeIterator_Type, - /* .tp_init = */ { - { - /* .tp_alloc = */ { - /* .tp_ctor = */ (dfunptr_t)&proxy_iterator_ctor, - /* .tp_copy_ctor = */ (dfunptr_t)&proxy_iterator_copy, - /* .tp_deep_ctor = */ (dfunptr_t)&proxy_iterator_deepcopy, - /* .tp_any_ctor = */ (dfunptr_t)&proxy_iterator_init, - TYPE_FIXED_ALLOCATOR(MapProxyIterator) - } - }, - /* .tp_dtor = */ (void (DCALL *)(DeeObject *__restrict))&proxy_iterator_fini, - /* .tp_assign = */ NULL, - /* .tp_move_assign = */ NULL - }, - /* .tp_cast = */ { - /* .tp_str = */ NULL, - /* .tp_repr = */ NULL, - /* .tp_bool = */ (int (DCALL *)(DeeObject *__restrict))&proxy_iterator_bool - }, - /* .tp_call = */ NULL, - /* .tp_visit = */ (void (DCALL *)(DeeObject *__restrict, dvisit_t, void *))&proxy_iterator_visit, - /* .tp_gc = */ NULL, - /* .tp_math = */ NULL, - /* .tp_cmp = */ NULL, - /* .tp_seq = */ NULL, - /* .tp_iter_next = */ NULL, - /* .tp_iterator = */ NULL, - /* .tp_attr = */ NULL, - /* .tp_with = */ NULL, - /* .tp_buffer = */ NULL, - /* .tp_methods = */ NULL, - /* .tp_getsets = */ NULL, - /* .tp_members = */ proxy_iterator_members, - /* .tp_class_methods = */ NULL, - /* .tp_class_getsets = */ NULL, - /* .tp_class_members = */ NULL -}; - -PRIVATE DeeTypeObject DeeMappingKeysIterator_Type = { - OBJECT_HEAD_INIT(&DeeType_Type), - /* .tp_name = */ "_MappingKeysIterator", - /* .tp_doc = */ NULL, - /* .tp_flags = */ TP_FNORMAL, - /* .tp_weakrefs = */ 0, - /* .tp_features = */ TF_NONE, - /* .tp_base = */ &DeeMappingProxyIterator_Type, - /* .tp_init = */ { - { - /* .tp_alloc = */ { - /* .tp_ctor = */ (dfunptr_t)&proxy_iterator_ctor, - /* .tp_copy_ctor = */ (dfunptr_t)&proxy_iterator_copy, - /* .tp_deep_ctor = */ (dfunptr_t)&proxy_iterator_deepcopy, - /* .tp_any_ctor = */ (dfunptr_t)&proxy_iterator_init, - TYPE_FIXED_ALLOCATOR(MapProxyIterator) - } - }, - /* .tp_dtor = */ NULL, - /* .tp_assign = */ NULL, - /* .tp_move_assign = */ NULL - }, - /* .tp_cast = */ { - /* .tp_str = */ NULL, - /* .tp_repr = */ NULL, - /* .tp_bool = */ NULL - }, - /* .tp_call = */ NULL, - /* .tp_visit = */ NULL, - /* .tp_gc = */ NULL, - /* .tp_math = */ NULL, - /* .tp_cmp = */ NULL, - /* .tp_seq = */ NULL, - /* .tp_iter_next = */ (DREF DeeObject *(DCALL *)(DeeObject *__restrict))&proxy_iterator_next_key, - /* .tp_iterator = */ NULL, - /* .tp_attr = */ NULL, - /* .tp_with = */ NULL, - /* .tp_buffer = */ NULL, - /* .tp_methods = */ NULL, - /* .tp_getsets = */ NULL, - /* .tp_members = */ proxy_iterator_members, - /* .tp_class_methods = */ NULL, - /* .tp_class_getsets = */ NULL, - /* .tp_class_members = */ NULL -}; - -PRIVATE DeeTypeObject DeeMappingValuesIterator_Type = { - OBJECT_HEAD_INIT(&DeeType_Type), - /* .tp_name = */ "_MappingValuesIterator", - /* .tp_doc = */ DOC("next->?T2?O?O"), - /* .tp_flags = */ TP_FNORMAL, - /* .tp_weakrefs = */ 0, - /* .tp_features = */ TF_NONE, - /* .tp_base = */ &DeeMappingProxyIterator_Type, - /* .tp_init = */ { - { - /* .tp_alloc = */ { - /* .tp_ctor = */ (dfunptr_t)&proxy_iterator_ctor, - /* .tp_copy_ctor = */ (dfunptr_t)&proxy_iterator_copy, - /* .tp_deep_ctor = */ (dfunptr_t)&proxy_iterator_deepcopy, - /* .tp_any_ctor = */ (dfunptr_t)&proxy_iterator_init, - TYPE_FIXED_ALLOCATOR(MapProxyIterator) - } - }, - /* .tp_dtor = */ NULL, - /* .tp_assign = */ NULL, - /* .tp_move_assign = */ NULL - }, - /* .tp_cast = */ { - /* .tp_str = */ NULL, - /* .tp_repr = */ NULL, - /* .tp_bool = */ NULL - }, - /* .tp_call = */ NULL, - /* .tp_visit = */ NULL, - /* .tp_gc = */ NULL, - /* .tp_math = */ NULL, - /* .tp_cmp = */ NULL, - /* .tp_seq = */ NULL, - /* .tp_iter_next = */ (DREF DeeObject *(DCALL *)(DeeObject *__restrict))&proxy_iterator_next_value, - /* .tp_iterator = */ NULL, - /* .tp_attr = */ NULL, - /* .tp_with = */ NULL, - /* .tp_buffer = */ NULL, - /* .tp_methods = */ NULL, - /* .tp_getsets = */ NULL, - /* .tp_members = */ proxy_iterator_members, - /* .tp_class_methods = */ NULL, - /* .tp_class_getsets = */ NULL, - /* .tp_class_members = */ NULL -}; - -PRIVATE DeeTypeObject DeeMappingItemsIterator_Type = { - OBJECT_HEAD_INIT(&DeeType_Type), - /* .tp_name = */ "_MappingItemsIterator", - /* .tp_doc = */ DOC("next->?T2?O?O"), - /* .tp_flags = */ TP_FNORMAL, - /* .tp_weakrefs = */ 0, - /* .tp_features = */ TF_NONE, - /* .tp_base = */ &DeeMappingProxyIterator_Type, - /* .tp_init = */ { - { - /* .tp_alloc = */ { - /* .tp_ctor = */ (dfunptr_t)&proxy_iterator_ctor, - /* .tp_copy_ctor = */ (dfunptr_t)&proxy_iterator_copy, - /* .tp_deep_ctor = */ (dfunptr_t)&proxy_iterator_deepcopy, - /* .tp_any_ctor = */ (dfunptr_t)&proxy_iterator_init, - TYPE_FIXED_ALLOCATOR(MapProxyIterator) - } - }, - /* .tp_dtor = */ NULL, - /* .tp_assign = */ NULL, - /* .tp_move_assign = */ NULL - }, - /* .tp_cast = */ { - /* .tp_str = */ NULL, - /* .tp_repr = */ NULL, - /* .tp_bool = */ NULL - }, - /* .tp_call = */ NULL, - /* .tp_visit = */ NULL, - /* .tp_gc = */ NULL, - /* .tp_math = */ NULL, - /* .tp_cmp = */ NULL, - /* .tp_seq = */ NULL, - /* .tp_iter_next = */ (DREF DeeObject *(DCALL *)(DeeObject *__restrict))&proxy_iterator_next_item, - /* .tp_iterator = */ NULL, - /* .tp_attr = */ NULL, - /* .tp_with = */ NULL, - /* .tp_buffer = */ NULL, - /* .tp_methods = */ NULL, - /* .tp_getsets = */ NULL, - /* .tp_members = */ proxy_iterator_members, - /* .tp_class_methods = */ NULL, - /* .tp_class_getsets = */ NULL, - /* .tp_class_members = */ NULL -}; - - - - - - -typedef struct { - OBJECT_HEAD - DREF DeeObject *mp_map; /* [1..1][const] The mapping object for which this is a proxy. */ -} MapProxy; - -PRIVATE WUNUSED NONNULL((1)) int DCALL -proxy_ctor(MapProxy *__restrict self) { - self->mp_map = Dee_EmptyMapping; - Dee_Incref(self->mp_map); - return 0; -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -proxy_init(MapProxy *__restrict self, size_t argc, - DeeObject *const *argv) { - self->mp_map = Dee_EmptyMapping; - if (DeeArg_Unpack(argc, argv, "|o:_MappingProxy", &self->mp_map)) - goto err; - Dee_Incref(self->mp_map); - return 0; -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1, 2)) int DCALL -proxy_copy(MapProxy *__restrict self, - MapProxy *__restrict other) { - self->mp_map = other->mp_map; - Dee_Incref(self->mp_map); - return 0; -} - -PRIVATE WUNUSED NONNULL((1, 2)) int DCALL -proxy_deepcopy(MapProxy *__restrict self, - MapProxy *__restrict other) { - self->mp_map = DeeObject_DeepCopy(other->mp_map); - if unlikely(!self->mp_map) - goto err; - return 0; -err: - return -1; -} - -PRIVATE NONNULL((1)) void DCALL -proxy_fini(MapProxy *__restrict self) { - Dee_Decref(self->mp_map); -} - -PRIVATE NONNULL((1, 2)) void DCALL -proxy_visit(MapProxy *__restrict self, dvisit_t proc, void *arg) { - Dee_Visit(self->mp_map); -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -proxy_sizeob(MapProxy *__restrict self) { - return DeeObject_SizeOb(self->mp_map); -} - -PRIVATE WUNUSED NONNULL((1)) size_t DCALL -proxy_size(MapProxy *__restrict self) { - return DeeObject_Size(self->mp_map); -} - -PRIVATE WUNUSED NONNULL((1)) size_t DCALL -proxy_size_fast(MapProxy *__restrict self) { - return DeeObject_SizeFast(self->mp_map); -} - -PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -proxy_contains_key(MapProxy *self, DeeObject *key) { - return DeeObject_Contains(self->mp_map, key); -} - -PRIVATE WUNUSED NONNULL((1, 2)) DREF MapProxyIterator *DCALL -proxy_iterself(MapProxy *__restrict self, DeeTypeObject *__restrict result_type) { - DREF MapProxyIterator *result; - result = DeeObject_MALLOC(MapProxyIterator); - if unlikely(!result) - goto done; - result->mpi_iter = DeeObject_Iter(self->mp_map); - if unlikely(!result->mpi_iter) - goto err_r; - result->mpi_nsi = DeeType_NSI(Dee_TYPE(self->mp_map)); - if (result->mpi_nsi && result->mpi_nsi->nsi_class != TYPE_SEQX_CLASS_MAP) - result->mpi_nsi = NULL; - result->mpi_map = self->mp_map; - Dee_Incref(result->mpi_map); - DeeObject_Init(result, result_type); -done: - return result; -err_r: - DeeObject_FREE(result); - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF MapProxyIterator *DCALL -proxy_iterself_keys(MapProxy *__restrict self) { - return proxy_iterself(self, &DeeMappingKeysIterator_Type); -} - -PRIVATE WUNUSED NONNULL((1)) DREF MapProxyIterator *DCALL -proxy_iterself_values(MapProxy *__restrict self) { - return proxy_iterself(self, &DeeMappingValuesIterator_Type); -} - -PRIVATE WUNUSED NONNULL((1)) DREF MapProxyIterator *DCALL -proxy_iterself_items(MapProxy *__restrict self) { - return proxy_iterself(self, &DeeMappingItemsIterator_Type); -} - - -PRIVATE struct type_seq proxykeys_seq = { - /* .tp_iter = */ (DREF DeeObject *(DCALL *)(DeeObject *__restrict))&proxy_iterself_keys, - /* .tp_sizeob = */ (DREF DeeObject *(DCALL *)(DeeObject *__restrict))&proxy_sizeob, - /* .tp_contains = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))&proxy_contains_key, - /* .tp_getitem = */ NULL, - /* .tp_delitem = */ NULL, - /* .tp_setitem = */ NULL, - /* .tp_getrange = */ NULL, - /* .tp_delrange = */ NULL, - /* .tp_setrange = */ NULL, - /* .tp_nsi = */ NULL, - /* .tp_foreach = */ NULL, /* TODO: implement using DeeObject_Enumerate() */ - /* .tp_foreach_pair = */ NULL, - /* .tp_enumerate = */ NULL, - /* .tp_enumerate_index = */ NULL, - /* .tp_iterkeys = */ NULL, - /* .tp_bounditem = */ NULL, - /* .tp_hasitem = */ NULL, - /* .tp_size = */ (size_t (DCALL *)(DeeObject *__restrict))&proxy_size, - /* .tp_size_fast = */ (size_t (DCALL *)(DeeObject *__restrict))&proxy_size_fast, - /* .tp_getitem_index = */ NULL, - /* .tp_getitem_index_fast = */ NULL, - /* .tp_delitem_index = */ NULL, - /* .tp_setitem_index = */ NULL, - /* .tp_bounditem_index = */ NULL, - /* .tp_hasitem_index = */ NULL, - /* .tp_getrange_index = */ NULL, - /* .tp_delrange_index = */ NULL, - /* .tp_setrange_index = */ NULL, - /* .tp_getrange_index_n = */ NULL, - /* .tp_delrange_index_n = */ NULL, - /* .tp_setrange_index_n = */ NULL, - /* .tp_trygetitem = */ NULL, - /* .tp_trygetitem_index = */ NULL, - /* .tp_trygetitem_string_hash = */ NULL, - /* .tp_getitem_string_hash = */ NULL, - /* .tp_delitem_string_hash = */ NULL, - /* .tp_setitem_string_hash = */ NULL, - /* .tp_bounditem_string_hash = */ NULL, - /* .tp_hasitem_string_hash = */ NULL, - /* .tp_trygetitem_string_len_hash = */ NULL, - /* .tp_getitem_string_len_hash = */ NULL, - /* .tp_delitem_string_len_hash = */ NULL, - /* .tp_setitem_string_len_hash = */ NULL, - /* .tp_bounditem_string_len_hash = */ NULL, - /* .tp_hasitem_string_len_hash = */ NULL, -}; - -PRIVATE struct type_seq proxyvalues_seq = { - /* .tp_iter = */ (DREF DeeObject *(DCALL *)(DeeObject *__restrict))&proxy_iterself_values, - /* .tp_sizeob = */ (DREF DeeObject *(DCALL *)(DeeObject *__restrict))&proxy_sizeob, - /* .tp_contains = */ NULL, - /* .tp_getitem = */ NULL, - /* .tp_delitem = */ NULL, - /* .tp_setitem = */ NULL, - /* .tp_getrange = */ NULL, - /* .tp_delrange = */ NULL, - /* .tp_setrange = */ NULL, - /* .tp_nsi = */ NULL, - /* .tp_foreach = */ NULL, - /* .tp_foreach_pair = */ NULL, - /* .tp_enumerate = */ NULL, - /* .tp_enumerate_index = */ NULL, - /* .tp_iterkeys = */ NULL, - /* .tp_bounditem = */ NULL, - /* .tp_hasitem = */ NULL, - /* .tp_size = */ (size_t (DCALL *)(DeeObject *__restrict))&proxy_size, - /* .tp_size_fast = */ (size_t (DCALL *)(DeeObject *__restrict))&proxy_size_fast, - /* .tp_getitem_index = */ NULL, - /* .tp_getitem_index_fast = */ NULL, - /* .tp_delitem_index = */ NULL, - /* .tp_setitem_index = */ NULL, - /* .tp_bounditem_index = */ NULL, - /* .tp_hasitem_index = */ NULL, - /* .tp_getrange_index = */ NULL, - /* .tp_delrange_index = */ NULL, - /* .tp_setrange_index = */ NULL, - /* .tp_getrange_index_n = */ NULL, - /* .tp_delrange_index_n = */ NULL, - /* .tp_setrange_index_n = */ NULL, - /* .tp_trygetitem = */ NULL, - /* .tp_trygetitem_index = */ NULL, - /* .tp_trygetitem_string_hash = */ NULL, - /* .tp_getitem_string_hash = */ NULL, - /* .tp_delitem_string_hash = */ NULL, - /* .tp_setitem_string_hash = */ NULL, - /* .tp_bounditem_string_hash = */ NULL, - /* .tp_hasitem_string_hash = */ NULL, - /* .tp_trygetitem_string_len_hash = */ NULL, - /* .tp_getitem_string_len_hash = */ NULL, - /* .tp_delitem_string_len_hash = */ NULL, - /* .tp_setitem_string_len_hash = */ NULL, - /* .tp_bounditem_string_len_hash = */ NULL, - /* .tp_hasitem_string_len_hash = */ NULL, -}; - -PRIVATE struct type_seq proxyitems_seq = { - /* .tp_iter = */ (DREF DeeObject *(DCALL *)(DeeObject *__restrict))&proxy_iterself_items, - /* .tp_sizeob = */ (DREF DeeObject *(DCALL *)(DeeObject *__restrict))&proxy_sizeob, - /* .tp_contains = */ NULL, - /* .tp_getitem = */ NULL, - /* .tp_delitem = */ NULL, - /* .tp_setitem = */ NULL, - /* .tp_getrange = */ NULL, - /* .tp_delrange = */ NULL, - /* .tp_setrange = */ NULL, - /* .tp_nsi = */ NULL, - /* .tp_foreach = */ NULL, - /* .tp_foreach_pair = */ NULL, - /* .tp_enumerate = */ NULL, - /* .tp_enumerate_index = */ NULL, - /* .tp_iterkeys = */ NULL, - /* .tp_bounditem = */ NULL, - /* .tp_hasitem = */ NULL, - /* .tp_size = */ (size_t (DCALL *)(DeeObject *__restrict))&proxy_size, - /* .tp_size_fast = */ (size_t (DCALL *)(DeeObject *__restrict))&proxy_size_fast, - /* .tp_getitem_index = */ NULL, - /* .tp_getitem_index_fast = */ NULL, - /* .tp_delitem_index = */ NULL, - /* .tp_setitem_index = */ NULL, - /* .tp_bounditem_index = */ NULL, - /* .tp_hasitem_index = */ NULL, - /* .tp_getrange_index = */ NULL, - /* .tp_delrange_index = */ NULL, - /* .tp_setrange_index = */ NULL, - /* .tp_getrange_index_n = */ NULL, - /* .tp_delrange_index_n = */ NULL, - /* .tp_setrange_index_n = */ NULL, - /* .tp_trygetitem = */ NULL, - /* .tp_trygetitem_index = */ NULL, - /* .tp_trygetitem_string_hash = */ NULL, - /* .tp_getitem_string_hash = */ NULL, - /* .tp_delitem_string_hash = */ NULL, - /* .tp_setitem_string_hash = */ NULL, - /* .tp_bounditem_string_hash = */ NULL, - /* .tp_hasitem_string_hash = */ NULL, - /* .tp_trygetitem_string_len_hash = */ NULL, - /* .tp_getitem_string_len_hash = */ NULL, - /* .tp_delitem_string_len_hash = */ NULL, - /* .tp_setitem_string_len_hash = */ NULL, - /* .tp_bounditem_string_len_hash = */ NULL, - /* .tp_hasitem_string_len_hash = */ NULL, -}; - -PRIVATE struct type_member tpconst proxykeys_class_members[] = { - TYPE_MEMBER_CONST(STR_Iterator, &DeeMappingKeysIterator_Type), - TYPE_MEMBER_END -}; - -PRIVATE struct type_member tpconst proxyvalues_class_members[] = { - TYPE_MEMBER_CONST(STR_Iterator, &DeeMappingValuesIterator_Type), - TYPE_MEMBER_END -}; - -PRIVATE struct type_member tpconst proxyitems_class_members[] = { - TYPE_MEMBER_CONST(STR_Iterator, &DeeMappingItemsIterator_Type), - TYPE_MEMBER_END -}; - -PRIVATE struct type_member tpconst proxy_class_members[] = { - TYPE_MEMBER_CONST(STR_Iterator, &DeeMappingProxyIterator_Type), - TYPE_MEMBER_END -}; - - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -proxyitems_byhash(MapProxy *self, size_t argc, - DeeObject *const *argv, DeeObject *kw) { - DeeObject *template_; - if (DeeArg_UnpackKw(argc, argv, kw, kwlist__template, "o:byhash", &template_)) - goto err; - /* Invoke byhash() on the underlying mapping. */ - return DeeObject_CallAttrStringf(self->mp_map, "byhash", PCKuSIZ, - DeeObject_Hash(template_)); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -proxykeys_byhash(MapProxy *self, size_t argc, - DeeObject *const *argv, DeeObject *kw) { - DREF DeeObject *result; - result = proxyitems_byhash(self, argc, argv, kw); - if likely(result) { - if (DeeTuple_Check(result)) { - if (DeeTuple_SIZE(result) == 0) - goto done; - if (DeeTuple_SIZE(result) == 1) { - DREF DeeObject *key_and_value[2]; - /* Unpack the pointed-to pair */ - if (DeeObject_Unpack(DeeTuple_GET(result, 0), 2, key_and_value)) - goto err_r; - Dee_Decref(result); - Dee_Decref(key_and_value[1]); - /* Return the pair's first element (which is the key) */ - result = (DREF DeeObject *)DeeTuple_NewUninitialized(1); - if unlikely(!result) { - Dee_Decref(key_and_value[0]); - goto err; - } - DeeTuple_SET(result, 0, key_and_value[0]); - goto done; - } - } -#ifndef __OPTIMIZE_SIZE__ - else if (Dee_TYPE(result) == &MapHashFilter_Type && - ((HashFilter *)result)->f_seq == self->mp_map) { - /* The mapping doesn't implement a proper byhash() function. - * In this case, we might as well just return a regular, old - * sequence hash filter object for ourself (the mapping's key - * view), rather than going through the each-wrapper below. */ - HashFilter *filter; - filter = (HashFilter *)result; - if unlikely(DeeObject_IsShared(filter)) { - /* The filter is being shared, so we must create a new filter. */ - Dee_hash_t hash; - hash = filter->f_hash; - Dee_Decref_unlikely(filter); - result = DeeSeq_HashFilter((DeeObject *)self, hash); - } else { - /* The filter isn't being shared. -> Can modify in-place */ - Dee_DecrefNokill(&MapHashFilter_Type); - Dee_DecrefNokill(filter->f_seq); - Dee_Incref(&SeqHashFilter_Type); - Dee_Incref(self); - filter->f_seq = (DREF DeeObject *)self; - filter->ob_type = &SeqHashFilter_Type; - } - goto done; - } -#endif /* !__OPTIMIZE_SIZE__ */ - - /* Fallback: byhash() didn't return a tuple, or that tuple's length is >= 2 - * In this case, we return the equivalent of `return byhash(template).each[0] as Sequence' */ - { - DREF SeqEachOperator *each; - each = SeqEachOperator_MALLOC(1); - if unlikely(!each) - goto err_r; - each->so_opname = OPERATOR_GETITEM; - each->so_opargc = 1; - each->se_seq = result; /* inherit reference */ - each->so_opargv[0] = DeeInt_Zero; - Dee_Incref(DeeInt_Zero); - DeeObject_Init(each, &SeqEachOperator_Type); - /* Wrap the each-operator in a super-view for sequences, thus preventing - * the caller from accidentally extending the each-expression any further. */ - result = DeeSuper_New(&DeeSeq_Type, (DeeObject *)each); - Dee_Decref_unlikely(each); - } - } -done: - return result; -err_r: - Dee_Decref(result); -err: - return NULL; -} - - -DOC_REF(seq_byhash_doc); - -INTDEF struct type_method tpconst proxykeys_methods[]; -INTERN_TPCONST struct type_method tpconst proxykeys_methods[] = { - TYPE_KWMETHOD("byhash", &proxykeys_byhash, DOC_GET(seq_byhash_doc)), - TYPE_METHOD_END -}; - -INTDEF struct type_method tpconst proxyitems_methods[]; -INTERN_TPCONST struct type_method tpconst proxyitems_methods[] = { - TYPE_KWMETHOD("byhash", &proxyitems_byhash, DOC_GET(map_byhash_doc)), - TYPE_METHOD_END -}; - - - -/* Base class for proxy views for mapping sequences. */ -PRIVATE DeeTypeObject DeeMappingProxy_Type = { - OBJECT_HEAD_INIT(&DeeType_Type), - /* .tp_name = */ "_MappingProxy", - /* .tp_doc = */ DOC("()\n" - "(map:?DMapping)"), - /* .tp_flags = */ TP_FNORMAL, - /* .tp_weakrefs = */ 0, - /* .tp_features = */ TF_NONE, - /* .tp_base = */ &DeeSeq_Type, - /* .tp_init = */ { - { - /* .tp_alloc = */ { - /* .tp_ctor = */ (dfunptr_t)&proxy_ctor, - /* .tp_copy_ctor = */ (dfunptr_t)&proxy_copy, - /* .tp_deep_ctor = */ (dfunptr_t)&proxy_deepcopy, - /* .tp_any_ctor = */ (dfunptr_t)&proxy_init, - TYPE_FIXED_ALLOCATOR(MapProxy) - } - }, - /* .tp_dtor = */ (void (DCALL *)(DeeObject *__restrict))&proxy_fini, - /* .tp_assign = */ NULL, - /* .tp_move_assign = */ NULL - }, - /* .tp_cast = */ { - /* .tp_str = */ NULL, - /* .tp_repr = */ NULL, - /* .tp_bool = */ NULL - }, - /* .tp_call = */ NULL, - /* .tp_visit = */ (void (DCALL *)(DeeObject *__restrict, dvisit_t, void *))&proxy_visit, - /* .tp_gc = */ NULL, - /* .tp_math = */ NULL, - /* .tp_cmp = */ NULL, - /* .tp_seq = */ &proxyitems_seq, - /* .tp_iter_next = */ NULL, - /* .tp_iterator = */ NULL, - /* .tp_attr = */ NULL, - /* .tp_with = */ NULL, - /* .tp_buffer = */ NULL, - /* .tp_methods = */ NULL, - /* .tp_getsets = */ NULL, - /* .tp_members = */ NULL, - /* .tp_class_methods = */ NULL, - /* .tp_class_getsets = */ NULL, - /* .tp_class_members = */ proxy_class_members, -}; - -PRIVATE DeeTypeObject *tpconst mapping_keys_mro[] = { - &DeeSet_Type, - &DeeMappingProxy_Type, - &DeeSeq_Type, - &DeeObject_Type, - NULL -}; - -PRIVATE DeeTypeObject DeeMappingKeys_Type = { - OBJECT_HEAD_INIT(&DeeType_Type), - /* .tp_name = */ "_MappingKeys", - /* .tp_doc = */ DOC("()\n" - "(map:?DMapping)"), - /* .tp_flags = */ TP_FNORMAL, - /* .tp_weakrefs = */ 0, - /* .tp_features = */ TF_NONE, - /* .tp_base = */ &DeeMappingProxy_Type, - /* .tp_init = */ { - { - /* .tp_alloc = */ { - /* .tp_ctor = */ (dfunptr_t)&proxy_ctor, - /* .tp_copy_ctor = */ (dfunptr_t)&proxy_copy, - /* .tp_deep_ctor = */ (dfunptr_t)&proxy_deepcopy, - /* .tp_any_ctor = */ (dfunptr_t)&proxy_init, - TYPE_FIXED_ALLOCATOR(MapProxy) - } - }, - /* .tp_dtor = */ NULL, - /* .tp_assign = */ NULL, - /* .tp_move_assign = */ NULL - }, - /* .tp_cast = */ { - /* .tp_str = */ NULL, - /* .tp_repr = */ NULL, - /* .tp_bool = */ NULL - }, - /* .tp_call = */ NULL, - /* .tp_visit = */ NULL, - /* .tp_gc = */ NULL, - /* .tp_math = */ NULL, - /* .tp_cmp = */ NULL, - /* .tp_seq = */ &proxykeys_seq, - /* .tp_iter_next = */ NULL, - /* .tp_iterator = */ NULL, - /* .tp_attr = */ NULL, - /* .tp_with = */ NULL, - /* .tp_buffer = */ NULL, - /* .tp_methods = */ proxykeys_methods, - /* .tp_getsets = */ NULL, - /* .tp_members = */ NULL, - /* .tp_class_methods = */ NULL, - /* .tp_class_getsets = */ NULL, - /* .tp_class_members = */ proxykeys_class_members, - /* .tp_call_kw = */ NULL, - /* .tp_mro = */ mapping_keys_mro -}; - -PRIVATE DeeTypeObject DeeMappingValues_Type = { - OBJECT_HEAD_INIT(&DeeType_Type), - /* .tp_name = */ "_MappingValues", - /* .tp_doc = */ DOC("()\n" - "(map:?DMapping)"), - /* .tp_flags = */ TP_FNORMAL, - /* .tp_weakrefs = */ 0, - /* .tp_features = */ TF_NONE, - /* .tp_base = */ &DeeMappingProxy_Type, - /* .tp_init = */ { - { - /* .tp_alloc = */ { - /* .tp_ctor = */ (dfunptr_t)&proxy_ctor, - /* .tp_copy_ctor = */ (dfunptr_t)&proxy_copy, - /* .tp_deep_ctor = */ (dfunptr_t)&proxy_deepcopy, - /* .tp_any_ctor = */ (dfunptr_t)&proxy_init, - TYPE_FIXED_ALLOCATOR(MapProxy) - } - }, - /* .tp_dtor = */ NULL, - /* .tp_assign = */ NULL, - /* .tp_move_assign = */ NULL - }, - /* .tp_cast = */ { - /* .tp_str = */ NULL, - /* .tp_repr = */ NULL, - /* .tp_bool = */ NULL - }, - /* .tp_call = */ NULL, - /* .tp_visit = */ NULL, - /* .tp_gc = */ NULL, - /* .tp_math = */ NULL, - /* .tp_cmp = */ NULL, - /* .tp_seq = */ &proxyvalues_seq, - /* .tp_iter_next = */ NULL, - /* .tp_iterator = */ NULL, - /* .tp_attr = */ NULL, - /* .tp_with = */ NULL, - /* .tp_buffer = */ NULL, - /* .tp_methods = */ NULL, - /* .tp_getsets = */ NULL, - /* .tp_members = */ NULL, - /* .tp_class_methods = */ NULL, - /* .tp_class_getsets = */ NULL, - /* .tp_class_members = */ proxyvalues_class_members -}; - -PRIVATE DeeTypeObject DeeMappingItems_Type = { - OBJECT_HEAD_INIT(&DeeType_Type), - /* .tp_name = */ "_MappingItems", - /* .tp_doc = */ DOC("()\n" - "(map:?DMapping)"), - /* .tp_flags = */ TP_FNORMAL, - /* .tp_weakrefs = */ 0, - /* .tp_features = */ TF_NONE, - /* .tp_base = */ &DeeMappingProxy_Type, - /* .tp_init = */ { - { - /* .tp_alloc = */ { - /* .tp_ctor = */ (dfunptr_t)&proxy_ctor, - /* .tp_copy_ctor = */ (dfunptr_t)&proxy_copy, - /* .tp_deep_ctor = */ (dfunptr_t)&proxy_deepcopy, - /* .tp_any_ctor = */ (dfunptr_t)&proxy_init, - TYPE_FIXED_ALLOCATOR(MapProxy) - } - }, - /* .tp_dtor = */ NULL, - /* .tp_assign = */ NULL, - /* .tp_move_assign = */ NULL - }, - /* .tp_cast = */ { - /* .tp_str = */ NULL, - /* .tp_repr = */ NULL, - /* .tp_bool = */ NULL - }, - /* .tp_call = */ NULL, - /* .tp_visit = */ NULL, - /* .tp_gc = */ NULL, - /* .tp_math = */ NULL, - /* .tp_cmp = */ NULL, - /* .tp_seq = */ &proxyitems_seq, - /* .tp_iter_next = */ NULL, - /* .tp_iterator = */ NULL, - /* .tp_attr = */ NULL, - /* .tp_with = */ NULL, - /* .tp_buffer = */ NULL, - /* .tp_methods = */ proxyitems_methods, - /* .tp_getsets = */ NULL, - /* .tp_members = */ NULL, - /* .tp_class_methods = */ NULL, - /* .tp_class_getsets = */ NULL, - /* .tp_class_members = */ proxyitems_class_members, -}; - - - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -map_iterself(DeeObject *__restrict self) { - if unlikely(Dee_TYPE(self) == &DeeMapping_Type) { - /* Special case: Create an empty iterator. - * >> This can happen when someone tries to iterate a symbolic empty-mapping object. */ - return_empty_iterator; - } - err_unimplemented_operator(Dee_TYPE(self), OPERATOR_ITER); - return NULL; -} - -PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -map_contains(DeeObject *self, DeeObject *key) { - DREF DeeObject *value; - value = DeeObject_TryGetItem(self, key); - if (!ITER_ISOK(value)) { - if (value == ITER_DONE) - return_false; - return NULL; - } - Dee_Decref(value); - return_true; -} - -PRIVATE WUNUSED NONNULL((1)) size_t DCALL -map_nsi_getsize(DeeObject *__restrict self) { - size_t result = 0; - DREF DeeObject *iter, *item; - DeeTypeObject *tp_self = Dee_TYPE(self); - - /* Check if a sub-class is overriding `operator iter'. If - * not, then the mapping is empty for all we're concerned */ - if (tp_self->tp_seq->tp_iter == &map_iterself) - goto done; - - /* Very inefficient: iterate the mapping and count items. */ - iter = DeeObject_Iter(self); - if unlikely(!iter) - goto err; - while (ITER_ISOK(item = DeeObject_IterNext(iter))) { - Dee_Decref(item); - if (DeeThread_CheckInterrupt()) - goto err_iter; - ++result; - } - if unlikely(!item) - goto err_iter; - Dee_Decref(iter); -done: - return result; -err_iter: - Dee_Decref(iter); -err: - return (size_t)-1; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -map_size(DeeObject *__restrict self) { - size_t result = map_nsi_getsize(self); - if unlikely(result == (size_t)-1) - goto err; - return DeeInt_NewSize(result); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -map_getitem(DeeObject *self, DeeObject *key) { - DREF DeeObject *iter, *item; - DREF DeeObject *item_key_and_value[2]; - dhash_t key_hash; - DeeTypeObject *tp_self = Dee_TYPE(self); - - /* Check if a sub-class is overriding `operator iter'. If - * not, then the mapping is empty for all we're concerned */ - if (tp_self->tp_seq->tp_iter == &map_iterself) { - err_unknown_key(self, key); - goto err; - } - - /* Very inefficient: iterate the mapping to search for a matching key-item pair. */ - key_hash = DeeObject_Hash(key); - iter = DeeObject_Iter(self); - if unlikely(!iter) - goto err; - while (ITER_ISOK(item = DeeObject_IterNext(iter))) { - int unpack_error, temp; - unpack_error = DeeObject_Unpack(item, 2, item_key_and_value); - Dee_Decref(item); - if unlikely(unpack_error) - goto err_iter; - - /* Check if this is the key we're looking for. */ - if (DeeObject_Hash(item_key_and_value[0]) != key_hash) { - Dee_Decref(item_key_and_value[0]); - } else { - temp = DeeObject_TryCompareEq(key, item_key_and_value[0]); - Dee_Decref(item_key_and_value[0]); - if unlikely(temp == Dee_COMPARE_ERR) { - Dee_Decref(item_key_and_value[1]); - goto err_iter; - } - if (temp == 0) { - /* Found it! */ - Dee_Decref(iter); - return item_key_and_value[1]; - } - } - Dee_Decref(item_key_and_value[1]); - if (DeeThread_CheckInterrupt()) - goto err_iter; - } - if (item == ITER_DONE) - err_unknown_key(self, key); -err_iter: - Dee_Decref(iter); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -map_getitem_def(DeeObject *self, DeeObject *key, DeeObject *defl) { - DREF DeeObject *iter, *item; - DREF DeeObject *item_key_and_value[2]; - dhash_t key_hash; - DeeTypeObject *tp_self = Dee_TYPE(self); - - /* Check if a sub-class is overriding `operator iter'. If - * not, then the mapping is empty for all we're concerned */ - if (tp_self->tp_seq->tp_iter == &map_iterself) - goto return_defl; - - /* Very inefficient: iterate the mapping to search for a matching key-item pair. */ - key_hash = DeeObject_Hash(key); - iter = DeeObject_Iter(self); - if unlikely(!iter) - goto err; - while (ITER_ISOK(item = DeeObject_IterNext(iter))) { - int unpack_error, temp; - unpack_error = DeeObject_Unpack(item, 2, item_key_and_value); - Dee_Decref(item); - if unlikely(unpack_error) - goto err_iter; - - /* Check if this is the key we're looking for. */ - if (DeeObject_Hash(item_key_and_value[0]) != key_hash) { - Dee_Decref(item_key_and_value[0]); - } else { - temp = DeeObject_TryCompareEq(key, item_key_and_value[0]); - Dee_Decref(item_key_and_value[0]); - if unlikely(temp == Dee_COMPARE_ERR) { - Dee_Decref(item_key_and_value[1]); - goto err_iter; - } - if (temp == 0) { - /* Found it! */ - Dee_Decref(iter); - return item_key_and_value[1]; - } - } - Dee_Decref(item_key_and_value[1]); - if (DeeThread_CheckInterrupt()) - goto err_iter; - } - if (!item) - goto err_iter; - Dee_Decref(iter); -return_defl: - if (defl != ITER_DONE) - Dee_Incref(defl); - return defl; -err_iter: - Dee_Decref(iter); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1, 2, 3)) DREF DeeObject *DCALL -map_getrange(DeeObject *self, - DeeObject *UNUSED(begin), - DeeObject *UNUSED(end)) { - /* Override the getrange operator of `sequence' as not-implemented. */ - err_unimplemented_operator(Dee_TYPE(self), OPERATOR_GETRANGE); - return NULL; -} - -PRIVATE struct type_nsi tpconst map_nsi = { - /* .nsi_class = */ TYPE_SEQX_CLASS_MAP, - /* .nsi_flags = */ TYPE_SEQX_FNORMAL, - { - /* .nsi_maplike = */ { - /* .nsi_getsize = */ (dfunptr_t)&map_nsi_getsize, - /* .nsi_nextkey = */ (dfunptr_t)NULL, - /* .nsi_nextvalue = */ (dfunptr_t)NULL, - /* .nsi_getdefault = */ (dfunptr_t)&map_getitem_def - } - } -}; - -PRIVATE struct type_seq DeeMap_OperatorSeq = { - /* .tp_iter = */ &map_iterself, - /* .tp_sizeob = */ &map_size, - /* .tp_contains = */ &map_contains, - /* .tp_getitem = */ &map_getitem, - /* .tp_delitem = */ NULL, - /* .tp_setitem = */ NULL, - /* .tp_getrange = */ &map_getrange, - /* .tp_delrange = */ NULL, - /* .tp_setrange = */ NULL, - /* .tp_nsi = */ &map_nsi -}; - -PRIVATE WUNUSED NONNULL((1, 2)) int DCALL -map_eq_impl(DeeObject *__restrict self, - DeeObject *__restrict other) { - size_t pair_count = 0, other_size; - int temp; - DREF DeeObject *iter, *elem, *pair[2]; - DREF DeeObject *other_value; - /* Check of all keys from `self' have the same value within `other' */ - iter = DeeObject_Iter(self); - if unlikely(!iter) - goto err; - while (ITER_ISOK(elem = DeeObject_IterNext(iter))) { - if (DeeObject_Unpack(elem, 2, pair)) - goto err_elem; - Dee_Decref(elem); - other_value = DeeObject_TryGetItem(other, pair[0]); - Dee_Decref(pair[0]); - if (!ITER_ISOK(other_value)) { - Dee_Decref(pair[1]); - Dee_Decref(iter); - if unlikely(!other_value) - goto err; - return 0; /* Key wasn't provided by `other' */ - } - temp = DeeObject_TryCompareEq(pair[1], other_value); - Dee_Decref(other_value); - Dee_Decref(pair[1]); - if (temp != 0) { - if unlikely(temp == Dee_COMPARE_ERR) - goto err_iter; - Dee_Decref(iter); - return 0; - } - /* Track the number of pairs found in `self' */ - ++pair_count; - } - if unlikely(!elem) - goto err_iter; - Dee_Decref(iter); - /* Make sure that `other' has the same size as `self' */ - other_size = DeeObject_Size(other); - if unlikely(other_size == (size_t)-1) - goto err; - return pair_count == other_size; -err_elem: - Dee_Decref(elem); -err_iter: - Dee_Decref(iter); -err: - return -1; -} - + /**/ "using them as key and value to insert into @this ?."); +DOC_DEF(map_setold_doc, + "(key,value)->?Dbool\n" + "#r{Indicative of @value having been assigned to @key}" + "Assign @value to @key, only succeeding when @key already existed to begin with"); +DOC_DEF(map_setnew_doc, + "(key,value)->?Dbool\n" + "#r{Indicative of @value having been assigned to @key}" + "Assign @value to @key, only succeeding when @key didn't exist before"); +DOC_DEF(map_setold_ex_doc, + "(key,value)->?T2?Dbool?O\n" + "#r{A pair of values (new-value-was-assigned, old-value-or-none)}" + "Same as ?#setold but also return the previously assigned value"); +DOC_DEF(map_setnew_ex_doc, + "(key,value)->?T2?Dbool?O\n" + "#r{A pair of values (new-value-was-assigned, old-value-or-none)}" + "Same as ?#setnew but return the previously assigned value on failure"); -PRIVATE WUNUSED NONNULL((1)) dhash_t DCALL -DeeMap_OperatorHash(DeeObject *__restrict self) { - dhash_t result = DEE_HASHOF_EMPTY_SEQUENCE; - DREF DeeObject *iter, *elem; - iter = DeeObject_Iter(self); - if unlikely(!iter) - goto err; - while (ITER_ISOK(elem = DeeObject_IterNext(iter))) { - /* Note how we don't use `Dee_HashCombine()' here! - * That become order doesn't matter for mappings. */ - result ^= DeeObject_Hash(elem); - Dee_Decref(elem); - } - Dee_Decref(iter); - if unlikely(!elem) - goto err; - return result; -err: - DeeError_Print("Unhandled error in `Mapping.operator hash'\n", - ERROR_PRINT_DOHANDLE); - return DeeObject_HashGeneric(self); -} +INTDEF struct type_method tpconst map_methods[]; +INTERN_TPCONST struct type_method tpconst map_methods[] = { + /* Default operations for all mappings. */ + TYPE_METHOD(STR_get, &default_map_get, DOC_GET(map_get_doc)), + TYPE_KWMETHOD("byhash", &map_byhash, DOC_GET(map_byhash_doc)), -PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -DeeMap_OperatorEq(DeeObject *self, DeeObject *other) { - int error = map_eq_impl(self, other); - if unlikely(error < 0) - goto err; - return_bool_(error); -err: - return NULL; -} + /* Default operations for modifiable mappings. */ + TYPE_METHOD(STR_setold, &default_map_setold, DOC_GET(map_setold_doc)), + TYPE_METHOD(STR_setold_ex, &default_map_setold_ex, DOC_GET(map_setold_ex_doc)), + TYPE_METHOD(STR_setnew, &default_map_setnew, DOC_GET(map_setnew_doc)), + TYPE_METHOD(STR_setnew_ex, &default_map_setnew_ex, DOC_GET(map_setnew_ex_doc)), + TYPE_METHOD(STR_setdefault, &default_map_setdefault, DOC_GET(map_setdefault_doc)), + TYPE_METHOD(STR_update, &default_map_update, DOC_GET(map_update_doc)), + TYPE_METHOD(STR_remove, &default_map_remove, "(key)->?Dbool"), + TYPE_METHOD(STR_removekeys, &default_map_removekeys, "(keys:?S?O)"), + TYPE_METHOD(STR_pop, &default_map_pop, DOC_GET(map_pop_doc)), + TYPE_METHOD(STR_popitem, &default_map_popitem, DOC_GET(map_popitem_doc)), -PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -DeeMap_OperatorNe(DeeObject *self, DeeObject *other) { - int error = map_eq_impl(self, other); - if unlikely(error < 0) - goto err; - return_bool_(!error); -err: - return NULL; -} + TYPE_METHOD("__contains__", &default_map___contains__, + "(item)->?Dbool\n" + "Alias for ${item in (this as Mapping)}"), + TYPE_METHOD("__getitem__", &default_map___getitem__, + "(key)->\n" + "Alias for ${(this as Mapping)[key]}"), + TYPE_METHOD("__delitem__", &default_map___delitem__, + "(key)\n" + "Alias for ${del (this as Mapping)[key]}"), + TYPE_METHOD("__setitem__", &default_map___setitem__, + "(key,value)\n" + "Alias for ${(this as Mapping)[key] = value}"), + TYPE_METHOD("__enumerate__", &default_map___enumerate__, + "(cb)->\n" + "Alias for ${(this as Mapping).enumerate(cb)}"), + TYPE_KWMETHOD("__enumerate_index__", &default_map___enumerate_index__, + "(cb,start=!0,end:?Dint=!A!Dint!PSIZE_MAX)->\n" + "Alias for ${Mapping.enumerate(this, cb, start, end)}"), + TYPE_METHOD("__iterkeys__", &default_map___iterkeys__, + "->?DIterator\n" + "Alias for: ${(this as Mapping).iterkeys}"), + TYPE_METHOD("__bounditem__", &default_map___bounditem__, + "(key,allow_missing=!t)->?Dbool\n" + "Alias for ${deemon.bounditem(this as Mapping, key, allow_missing)}"), + TYPE_METHOD("__hasitem__", &default_map___hasitem__, + "(key)->?Dbool\n" + "Alias for ${deemon.hasitem(this as Mapping, key)}"), + TYPE_METHOD("__getitem_index__", &default_map___getitem_index__, + "(key:?Dint)->\n" + "Alias for ${(this as Mapping)[key]}"), + TYPE_METHOD("__delitem_index__", &default_map___delitem_index__, + "(key:?Dint)\n" + "Alias for ${del (this as Mapping)[key]}"), + TYPE_METHOD("__setitem_index__", &default_map___setitem_index__, + "(key:?Dint,value)\n" + "Alias for ${(this as Mapping)[key] = value}"), + TYPE_METHOD("__bounditem_index__", &default_map___bounditem_index__, + "(key:?Dint,allow_missing=!t)->?Dbool\n" + "Alias for ${deemon.bounditem(this as Mapping, key, allow_missing)}"), + TYPE_METHOD("__hasitem_index__", &default_map___hasitem_index__, + "(key:?Dint)->?Dbool\n" + "Alias for ${deemon.hasitem(this as Mapping, key)}"), + TYPE_METHOD("__trygetitem__", &default_map___trygetitem__, + "(key,def=!N)->\n" + "Alias for ${(this as Mapping).get(key, def)"), + TYPE_METHOD("__trygetitem_index__", &default_map___trygetitem_index__, + "(key:?Dint,def=!N)->\n" + "Alias for ${(this as Mapping).get(key, def)"), + TYPE_METHOD("__trygetitem_string__", &default_map___trygetitem_string__, + "(key:?X2?DBytes?Dstring,def=!N)->\n" + "Alias for ${(this as Mapping).get(key, def)"), + TYPE_METHOD("__getitem_string__", &default_map___getitem_string__, + "(key:?X2?DBytes?Dstring)->\n" + "Alias for ${(this as Mapping)[key]}"), + TYPE_METHOD("__delitem_string__", &default_map___delitem_string__, + "(key:?X2?DBytes?Dstring)\n" + "Alias for ${del (this as Mapping)[key]}"), + TYPE_METHOD("__setitem_string__", &default_map___setitem_string__, + "(key:?X2?DBytes?Dstring,value)\n" + "Alias for ${(this as Mapping)[key] = value}"), + TYPE_METHOD("__bounditem_string__", &default_map___bounditem_string__, + "(key:?X2?DBytes?Dstring,allow_missing=!t)->?Dbool\n" + "Alias for ${deemon.bounditem(this as Mapping, key, allow_missing)}"), + TYPE_METHOD("__hasitem_string__", &default_map___hasitem_string__, + "(key:?X2?DBytes?Dstring)->?Dbool\n" + "Alias for ${deemon.hasitem(this as Mapping, key)}"), + TYPE_METHOD("__hash__", &default_map___hash__, + "->?Dint\n" + "Alias for ${(this as Mapping).operator hash()}"), + TYPE_METHOD("__compare_eq__", &default_map___compare_eq__, + "(rhs:?S?O)->?Dbool\n" + "Alias for ${(this as Mapping).operator == (rhs)}"), + TYPE_METHOD("__trycompare_eq__", &default_map___trycompare_eq__, + "(rhs:?S?O)->?Dbool\n" + "Alias for ${deemon.equals(this as Mapping, rhs)}"), + TYPE_METHOD("__eq__", &default_map___eq__, + "(rhs:?S?O)->?Dbool\n" + "Alias for ${(this as Mapping) == (rhs)}"), + TYPE_METHOD("__ne__", &default_map___ne__, + "(rhs:?S?O)->?Dbool\n" + "Alias for ${(this as Mapping) != (rhs)}"), + TYPE_METHOD("__lo__", &default_map___lo__, + "(rhs:?S?O)->?Dbool\n" + "Alias for ${(this as Mapping) < (rhs)}"), + TYPE_METHOD("__le__", &default_map___le__, + "(rhs:?S?O)->?Dbool\n" + "Alias for ${(this as Mapping) <= (rhs)}"), + TYPE_METHOD("__gr__", &default_map___gr__, + "(rhs:?S?O)->?Dbool\n" + "Alias for ${(this as Mapping) > (rhs)}"), + TYPE_METHOD("__ge__", &default_map___ge__, + "(rhs:?S?O)->?Dbool\n" + "Alias for ${(this as Mapping) >= (rhs)}"), -PRIVATE struct type_cmp DeeMap_OperatorCmp = { - /* .tp_hash = */ &DeeMap_OperatorHash, - /* .tp_compare_eq = */ NULL, - /* .tp_compare = */ NULL, - /* .tp_trycompare_eq = */ NULL, - /* .tp_eq = */ &DeeMap_OperatorEq, - /* .tp_ne = */ &DeeMap_OperatorNe, - /* .tp_lo = */ NULL, - /* .tp_le = */ NULL, - /* .tp_gr = */ NULL, - /* .tp_ge = */ NULL, -}; + TYPE_METHOD("__add__", &default_map___add__, + "(rhs:?S?O)->?DMapping\n" + "Alias for ${(this as Mapping) + rhs}"), + TYPE_METHOD("__sub__", &default_map___sub__, + "(rhs:?S?O)->?DMapping\n" + "Alias for ${(this as Mapping) - rhs}"), + TYPE_METHOD("__and__", &default_map___and__, + "(rhs:?S?O)->?DMapping\n" + "Alias for ${(this as Mapping) & rhs}"), + TYPE_METHOD("__xor__", &default_map___xor__, + "(rhs:?S?O)->?DMapping\n" + "Alias for ${(this as Mapping) ^ rhs}"), + TYPE_METHOD("__inplace_add__", &default_map___inplace_add__, + "(rhs:?S?O)->?.\n" + "Alias for ${(this as Mapping) += rhs}"), + TYPE_METHOD("__inplace_sub__", &default_map___inplace_sub__, + "(rhs:?S?O)->?.\n" + "Alias for ${(this as Mapping) -= rhs}"), + TYPE_METHOD("__inplace_and__", &default_map___inplace_and__, + "(rhs:?S?O)->?.\n" + "Alias for ${(this as Mapping) &= rhs}"), + TYPE_METHOD("__inplace_xor__", &default_map___inplace_xor__, + "(rhs:?S?O)->?.\n" + "Alias for ${(this as Mapping) ^= rhs}"), + TYPE_METHOD("__or__", &default_map___or__, + "(rhs:?S?O)->?DMapping\n" + "Alias for ?#__and__"), + TYPE_METHOD("__inplace_or__", &default_map___inplace_or__, + "(rhs:?S?O)->?DMapping\n" + "Alias for ?#__inplace_and__"), -PRIVATE struct type_math DeeMap_OperatorMath = { - /* .tp_int32 = */ NULL, - /* .tp_int64 = */ NULL, - /* .tp_double = */ NULL, - /* .tp_int = */ NULL, - /* .tp_inv = */ NULL, - /* .tp_pos = */ NULL, - /* .tp_neg = */ NULL, - /* .tp_add = */ NULL, /* TODO: &DeeMap_Union */ - /* .tp_sub = */ NULL, /* TODO: &DeeMap_Difference */ - /* .tp_mul = */ NULL, - /* .tp_div = */ NULL, - /* .tp_mod = */ NULL, - /* .tp_shl = */ NULL, - /* .tp_shr = */ NULL, - /* .tp_and = */ NULL, /* TODO: &DeeMap_Intersection */ - /* .tp_or = */ NULL, /* TODO: &DeeMap_Union */ - /* .tp_xor = */ NULL, /* TODO: &DeeMap_SymmetricDifference */ - /* .tp_pow = */ NULL + /* Old function names. */ +#ifndef CONFIG_NO_DEEMON_100_COMPAT + TYPE_METHOD("insert_all", &default_map_update, + "(items:?S?T2?O?O)\n" + "A deprecated alias for ?#update"), +#endif /* !CONFIG_NO_DEEMON_100_COMPAT */ + TYPE_METHOD_END }; -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ @@ -1922,195 +325,12 @@ map_printrepr(DeeObject *__restrict self, goto err; } -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS -PRIVATE WUNUSED NONNULL((1, 2)) DREF MapProxy *DCALL -map_new_proxy(DeeObject *__restrict self, DeeTypeObject *__restrict result_type) { - DREF MapProxy *result; - result = DeeObject_MALLOC(MapProxy); - if unlikely(!result) - goto done; - result->mp_map = self; - Dee_Incref(self); - DeeObject_Init(result, result_type); -done: - return result; -} - -PRIVATE WUNUSED NONNULL((1, 2)) DREF MapProxyIterator *DCALL -map_new_proxyiter(DeeObject *__restrict self, DeeTypeObject *__restrict result_type) { - DREF MapProxyIterator *result; - result = DeeObject_MALLOC(MapProxyIterator); - if unlikely(!result) - goto done; - result->mpi_iter = DeeObject_Iter(self); - if unlikely(!result->mpi_iter) - goto err_r; - result->mpi_map = self; - Dee_Incref(self); - if (result_type != &DeeMappingItemsIterator_Type) { - /* Search for an NSI descriptor defined by the mapping type. */ - result->mpi_nsi = DeeType_NSI(Dee_TYPE(self)); - if (result->mpi_nsi && result->mpi_nsi->nsi_class != TYPE_SEQX_CLASS_MAP) - result->mpi_nsi = NULL; - } - DeeObject_Init(result, result_type); -done: - return result; -err_r: - DeeObject_FREE(result); - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF MapProxy *DCALL -map_keys(DeeObject *__restrict self) { - return map_new_proxy(self, &DeeMappingKeys_Type); -} - -PRIVATE WUNUSED NONNULL((1)) DREF MapProxy *DCALL -map_values(DeeObject *__restrict self) { - return map_new_proxy(self, &DeeMappingValues_Type); -} - -PRIVATE WUNUSED NONNULL((1)) DREF MapProxy *DCALL -map_items(DeeObject *__restrict self) { - return map_new_proxy(self, &DeeMappingItems_Type); -} - -PRIVATE WUNUSED NONNULL((1)) DREF MapProxyIterator *DCALL -map_iterkeys(DeeObject *__restrict self) { - return map_new_proxyiter(self, &DeeMappingKeysIterator_Type); -} - -PRIVATE WUNUSED NONNULL((1)) DREF MapProxyIterator *DCALL -map_itervalues(DeeObject *__restrict self) { - return map_new_proxyiter(self, &DeeMappingValuesIterator_Type); -} - -PRIVATE WUNUSED NONNULL((1)) DREF MapProxyIterator *DCALL -map_iteritems(DeeObject *__restrict self) { - return map_new_proxyiter(self, &DeeMappingItemsIterator_Type); -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -DeeMap_GetFirst(DeeObject *__restrict self) { - DREF DeeObject *iter, *result; - iter = DeeObject_Iter(self); - if unlikely(!iter) - goto err; - result = DeeObject_IterNext(iter); - Dee_Decref(iter); - if (result == ITER_DONE) - goto err_empty; - return result; -err_empty: - err_empty_sequence(self); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -DeeMap_DelFirst(DeeObject *__restrict self) { - DREF DeeObject *iter, *key; - int result; - iter = DeeObject_Iter(self); - if unlikely(!iter) - goto err; - key = DeeObject_IterNextKey(iter); - Dee_Decref(iter); - if (!ITER_ISOK(key)) { - if unlikely(!key) - goto err; - goto err_empty; - } - result = DeeObject_DelItem(self, key); - Dee_Decref(key); - return result; -err_empty: - err_empty_sequence(self); -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -DeeMap_GetLast(DeeObject *__restrict self) { - DREF DeeObject *iter, *result, *next; - iter = DeeObject_Iter(self); - if unlikely(!iter) - goto err; - result = DeeObject_IterNext(iter); - if (!ITER_ISOK(result)) { - Dee_Decref(iter); - if (result == ITER_DONE) - goto err_empty; - goto err; - } - for (;;) { - next = DeeObject_IterNext(iter); - if (!ITER_ISOK(next)) - break; - Dee_Decref(result); - result = next; - if (DeeThread_CheckInterrupt()) - goto err_result; - } - Dee_Decref(iter); - if unlikely(!next) - Dee_Clear(result); - return result; -err_empty: - err_empty_sequence(self); -err: - return NULL; -err_result: - Dee_Decref(result); - goto err; -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -DeeMap_DelLast(DeeObject *__restrict self) { - DREF DeeObject *iter, *key, *next; - int result; - iter = DeeObject_Iter(self); - if unlikely(!iter) - goto err; - key = DeeObject_IterNextKey(iter); - if (!ITER_ISOK(key)) { - Dee_Decref(iter); - if (key == ITER_DONE) - goto err_empty; - goto err; - } - for (;;) { - next = DeeObject_IterNextKey(iter); - if (!ITER_ISOK(next)) - break; - Dee_Decref(key); - key = next; - } - Dee_Decref(iter); - if unlikely(!next) { - Dee_Decref(key); - goto err; - } - result = DeeObject_DelItem(self, key); - Dee_Decref(key); - return result; -err_empty: - err_empty_sequence(self); -err: - return -1; -} -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - -#ifdef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL map_items(DeeObject *self) { return DeeSuper_New(&DeeSeq_Type, self); } -#endif /* CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ PRIVATE struct type_getset tpconst map_getsets[] = { -#ifdef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS TYPE_GETTER("keys", &default_map_keys, "->?#Keys\n" "Returns a ?DSequence that can be enumerated to view only the keys of @this ?."), @@ -2132,34 +352,6 @@ PRIVATE struct type_getset tpconst map_getsets[] = { TYPE_GETTER("iteritems", &DeeObject_Iter, "->?#Iterator\n" "Returns an iterator for ?#{items}. Same as ${this.operator iter()}"), -#else /* CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - TYPE_GETTER("keys", &map_keys, - "->?#Keys\n" - "Returns a ?DSequence that can be enumerated to view only the keys of @this ?."), - TYPE_GETTER("values", &map_values, - "->?#Values\n" - "Returns a ?DSequence that can be enumerated to view only the values of @this ?."), - TYPE_GETTER("items", &map_items, - "->?#Items\n" - "Returns a ?DSequence that can be enumerated to view the key-item " - /**/ "pairs as 2-element sequences, the same way they could be viewed " - /**/ "if @this ?. itself was being iterated\n" - "Note however that the returned ?DSequence is pure, meaning that it " - /**/ "implements a index-based getitem and getrange operators, the " - /**/ "same way one would expect of any regular object implementing " - /**/ "the sequence protocol"), - TYPE_GETTER("iterkeys", &map_iterkeys, - "->?AIterator?#Keys\n" - "Returns an iterator for ?#{keys}. Same as ${this.keys.operator iter()}"), - TYPE_GETTER("itervalues", &map_itervalues, - "->?AIterator?#Values\n" - "Returns an iterator for ?#{values}. Same as ${this.values.operator iter()}"), - TYPE_GETTER("iteritems", &map_iteritems, - "->?AIterator?#Items\n" - "Returns an iterator for ?#{items}. Same as ${this.items.operator iter()}"), - TYPE_GETSET_NODOC(STR_first, &DeeMap_GetFirst, &DeeMap_DelFirst, NULL), - TYPE_GETSET_NODOC(STR_last, &DeeMap_GetLast, &DeeMap_DelLast, NULL), -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ TYPE_GETTER("byattr", &MapByAttr_New, "->?Ert:MappingByAttr\n" "Construct a wrapper for @this mapping that behaves like a generic class object, " @@ -2193,7 +385,6 @@ map_Iterator_get(DeeTypeObject *__restrict self) { return NULL; } -#ifdef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS PRIVATE WUNUSED NONNULL((1)) DREF DeeTypeObject *DCALL map_Keys_get(DeeTypeObject *__restrict self) { DREF DeeTypeObject *result = &DeeSet_Type; @@ -2240,7 +431,6 @@ map_IterValues_get(DeeTypeObject *__restrict self) { } return_reference_(result); } -#endif /* CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ @@ -2299,7 +489,6 @@ PRIVATE struct type_getset tpconst map_class_getsets[] = { TYPE_GETTER("Frozen", &map_Frozen_get, "->?DType\n" "Returns the type of sequence returned by the ?#i:frozen property"), -#ifdef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS TYPE_GETTER("Keys", &map_Keys_get, "->?DType\n" "Returns the type of sequence returned by the ?#i:keys property"), @@ -2312,37 +501,9 @@ PRIVATE struct type_getset tpconst map_class_getsets[] = { TYPE_GETTER("IterValues", &map_IterValues_get, "->?DType\n" "Returns the type of sequence returned by the ?#i:itervalues property"), -#endif /* CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ TYPE_GETSET_END }; -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS -PRIVATE struct type_member tpconst map_class_members[] = { - TYPE_MEMBER_CONST_DOC("Proxy", &DeeMappingProxy_Type, - "->?DType\n" - "The common base-class of ?#Keys, ?#Values and ?#Items"), - TYPE_MEMBER_CONST_DOC("Keys", &DeeMappingKeys_Type, - "->?DType\n" - "The return type of the ?#keys member function"), - TYPE_MEMBER_CONST_DOC("Values", &DeeMappingValues_Type, - "->?DType\n" - "The return type of the ?#values member function"), - TYPE_MEMBER_CONST_DOC("Items", &DeeMappingItems_Type, - "->?DType\n" - "The return type of the ?#items member function"), - - /* TODO: KeyType->?DType - * When this type of ?. only allows key of a certain ?DType, - * this class attribute is overwritten with that ?DType. Else, - * it simply evaluates to ?O - * TODO: ValueType->?DType - * When this type of ?. only allows values of a certain ?DType, - * this class attribute is overwritten with that ?DType. Else, - * it simply evaluates to ?O */ - TYPE_MEMBER_END -}; -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - INTDEF int DCALL none_i1(void *UNUSED(a)); INTDEF int DCALL none_i2(void *UNUSED(a), void *UNUSED(b)); @@ -2388,11 +549,7 @@ PUBLIC DeeTypeObject DeeMapping_Type = { "Returns an iterator for enumerating key-value pairs as 2-elements sequences"), /* .tp_flags = */ TP_FNORMAL | TP_FABSTRACT | TP_FNAMEOBJECT, /* Generic base class type. */ /* .tp_weakrefs = */ 0, -#ifdef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS /* .tp_features = */ TF_NONE | (Dee_SEQCLASS_MAP << Dee_TF_SEQCLASS_SHFT), -#else /* CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - /* .tp_features = */ TF_NONE, -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ /* .tp_base = */ &DeeSeq_Type, /* .tp_init = */ { { @@ -2431,11 +588,7 @@ PUBLIC DeeTypeObject DeeMapping_Type = { /* .tp_members = */ NULL, /* .tp_class_methods = */ NULL, /* .tp_class_getsets = */ map_class_getsets, -#ifdef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS /* .tp_class_members = */ NULL, -#else /* CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - /* .tp_class_members = */ map_class_members, -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ /* .tp_call_kw = */ NULL, /* .tp_mro = */ NULL, /* .tp_operators = */ map_operators, diff --git a/src/deemon/objects/seq.c b/src/deemon/objects/seq.c index c652cee63..aec8d970f 100644 --- a/src/deemon/objects/seq.c +++ b/src/deemon/objects/seq.c @@ -60,7 +60,6 @@ #include "seq/repeat.h" #include "seq/segments.h" #include "seq/simpleproxy.h" -#include "seq/subrange.h" #include "seq/svec.h" #include "seq_functions.h" @@ -168,2058 +167,6 @@ DeeType_NSI(DeeTypeObject *__restrict tp) { return NULL; } -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS -INTDEF DeeTypeObject DeeGenericIterator_Type; -INTDEF DeeTypeObject DeeNsiIterator_Type; -INTDEF DeeTypeObject DeeFastNsiIterator_Type; - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seq_size(DeeObject *__restrict self) { - size_t result; - result = DeeSeq_Size(self); - if unlikely(result == (size_t)-1) - goto err; - return DeeInt_NewSize((size_t)result); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -seq_tpcontains(DeeObject *self, DeeObject *item) { - return DeeSeq_DefaultContainsWithForeachDefault(self, item); -} - -PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -seq_getitem(DeeObject *self, DeeObject *index) { - size_t i; - if (DeeObject_AsSize(index, &i)) - goto err; - return DeeSeq_GetItem(self, i); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1, 2, 3)) DREF DeeObject *DCALL -seq_getrange(DeeObject *self, DeeObject *start, DeeObject *end) { - Dee_ssize_t i_begin, i_end; - if (DeeObject_AsSSize(start, &i_begin)) - goto err; - if (DeeNone_Check(end)) { - if unlikely(i_begin < 0) { - size_t seq_len = DeeObject_Size(self); - if unlikely(seq_len == (size_t)-1) - goto err; - if (i_begin < 0) { - i_begin += seq_len; - if unlikely(i_begin < 0) { - if unlikely(seq_len == 0) - goto empty_range; - i_begin = (Dee_ssize_t)do_fix_negative_range_index(i_begin, seq_len); - } - } - } - return DeeSeq_GetRangeN(self, (size_t)i_begin); - } - if (DeeObject_AsSSize(end, &i_end)) - goto err; - if unlikely(i_begin < 0 || i_end < 0) { - size_t seq_len = DeeObject_Size(self); - if unlikely(seq_len == (size_t)-1) - goto err; - if (i_begin < 0) { - i_begin += seq_len; - if unlikely(i_begin < 0) { - if unlikely(seq_len == 0) - goto empty_range; - i_begin = (Dee_ssize_t)do_fix_negative_range_index(i_begin, seq_len); - } - } - if (i_end < 0) { - i_end += seq_len; - if unlikely(i_end < 0) { - if unlikely(seq_len == 0) - goto empty_range; - i_end = (Dee_ssize_t)do_fix_negative_range_index(i_end, seq_len); - } - } - } - return DeeSeq_GetRange(self, - (size_t)i_begin, - (size_t)i_end); -empty_range: - return_empty_seq; -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seq_nsi_getrange(DeeObject *__restrict self, Dee_ssize_t i_begin, Dee_ssize_t i_end) { - if unlikely(i_begin < 0 || i_end < 0) { - size_t seq_len; - seq_len = DeeObject_Size(self); - if unlikely(seq_len == (size_t)-1) - goto err; - if (i_begin < 0) { - i_begin += seq_len; - if unlikely(i_begin < 0) { - if unlikely(seq_len == 0) - goto empty_range; - i_begin = (Dee_ssize_t)do_fix_negative_range_index(i_begin, seq_len); - } - } - if (i_end < 0) { - i_end += seq_len; - if unlikely(i_end < 0) { - if unlikely(seq_len == 0) - goto empty_range; - i_end = (Dee_ssize_t)do_fix_negative_range_index(i_end, seq_len); - } - } - } - return DeeSeq_GetRange(self, - (size_t)i_begin, - (size_t)i_end); -empty_range: - return_empty_seq; -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seq_nsi_getrange_n(DeeObject *__restrict self, Dee_ssize_t i_begin) { - if unlikely(i_begin < 0) { - size_t seq_len; - seq_len = DeeObject_Size(self); - if unlikely(seq_len == (size_t)-1) - goto err; - if (i_begin < 0) { - i_begin += seq_len; - if unlikely(i_begin < 0) { - if unlikely(seq_len == 0) - goto empty_range; - i_begin = (Dee_ssize_t)do_fix_negative_range_index(i_begin, seq_len); - } - } - } - return DeeSeq_GetRangeN(self, (size_t)i_begin); -empty_range: - return_empty_seq; -err: - return NULL; -} - -typedef struct { - OBJECT_HEAD - DREF DeeObject *(DCALL *si_getitem)(DeeObject *self, DeeObject *index); - DREF DeeObject *si_seq; /* [1..1][const] The Sequence being iterated. */ - DREF DeeObject *si_size; /* [1..1][const] The size of the Sequence. */ - DREF DeeObject *si_index; /* [1..1][lock(si_lock)] The current index (`int' object). */ -#ifndef CONFIG_NO_THREADS - Dee_atomic_rwlock_t si_lock; /* Lock for accessing `si_index' */ -#endif /* !CONFIG_NO_THREADS */ -} SeqIterator; - -#define SeqIterator_LockReading(self) Dee_atomic_rwlock_reading(&(self)->si_lock) -#define SeqIterator_LockWriting(self) Dee_atomic_rwlock_writing(&(self)->si_lock) -#define SeqIterator_LockTryRead(self) Dee_atomic_rwlock_tryread(&(self)->si_lock) -#define SeqIterator_LockTryWrite(self) Dee_atomic_rwlock_trywrite(&(self)->si_lock) -#define SeqIterator_LockCanRead(self) Dee_atomic_rwlock_canread(&(self)->si_lock) -#define SeqIterator_LockCanWrite(self) Dee_atomic_rwlock_canwrite(&(self)->si_lock) -#define SeqIterator_LockWaitRead(self) Dee_atomic_rwlock_waitread(&(self)->si_lock) -#define SeqIterator_LockWaitWrite(self) Dee_atomic_rwlock_waitwrite(&(self)->si_lock) -#define SeqIterator_LockRead(self) Dee_atomic_rwlock_read(&(self)->si_lock) -#define SeqIterator_LockWrite(self) Dee_atomic_rwlock_write(&(self)->si_lock) -#define SeqIterator_LockTryUpgrade(self) Dee_atomic_rwlock_tryupgrade(&(self)->si_lock) -#define SeqIterator_LockUpgrade(self) Dee_atomic_rwlock_upgrade(&(self)->si_lock) -#define SeqIterator_LockDowngrade(self) Dee_atomic_rwlock_downgrade(&(self)->si_lock) -#define SeqIterator_LockEndWrite(self) Dee_atomic_rwlock_endwrite(&(self)->si_lock) -#define SeqIterator_LockEndRead(self) Dee_atomic_rwlock_endread(&(self)->si_lock) -#define SeqIterator_LockEnd(self) Dee_atomic_rwlock_end(&(self)->si_lock) - -PRIVATE /*WUNUSED*/ NONNULL((1)) int DCALL -seqiterator_ctor(SeqIterator *__restrict self) { - self->si_getitem = &seq_getitem; - self->si_seq = Dee_EmptySeq; - self->si_index = DeeInt_Zero; - self->si_size = DeeInt_Zero; - Dee_atomic_rwlock_init(&self->si_lock); - Dee_Incref(Dee_EmptySeq); - Dee_Incref_n(DeeInt_Zero, 2); - return 0; -} - -PRIVATE WUNUSED NONNULL((1, 2)) int DCALL -seqiterator_copy(SeqIterator *__restrict self, - SeqIterator *__restrict other) { - self->si_getitem = other->si_getitem; - self->si_seq = other->si_seq; - self->si_size = other->si_size; - Dee_Incref(self->si_seq); - Dee_Incref(self->si_size); - Dee_atomic_rwlock_init(&self->si_lock); - SeqIterator_LockRead(other); - self->si_index = other->si_index; - Dee_Incref(self->si_index); - SeqIterator_LockEndRead(other); - return 0; -} - -PRIVATE NONNULL((1)) void DCALL -seqiterator_fini(SeqIterator *__restrict self) { - Dee_Decref(self->si_seq); - Dee_Decref(self->si_size); - Dee_Decref(self->si_index); -} - -PRIVATE NONNULL((1, 2)) void DCALL -seqiterator_visit(SeqIterator *__restrict self, dvisit_t proc, void *arg) { - Dee_Visit(self->si_seq); - Dee_Visit(self->si_size); - SeqIterator_LockRead(self); - Dee_Visit(self->si_index); - SeqIterator_LockEndRead(self); -} - -PRIVATE WUNUSED NONNULL((1)) Dee_ssize_t DCALL -seqiterator_printrepr(SeqIterator *__restrict self, - dformatprinter printer, void *arg) { - Dee_ssize_t result; - DREF DeeObject *index_ob; - SeqIterator_LockRead(self); - index_ob = self->si_index; - Dee_Incref(index_ob); - SeqIterator_LockEndRead(self); - result = DeeFormat_Printf(printer, arg, - "rt.GenericIterator(%r, %r /* of %r */)", - self->si_seq, index_ob, self->si_size); - Dee_Decref(index_ob); - return result; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seqiterator_next(SeqIterator *__restrict self) { - DREF DeeObject *old_index; - int error; - DREF DeeObject *result, *new_index; -again: - SeqIterator_LockRead(self); - old_index = self->si_index; - Dee_Incref(old_index); - SeqIterator_LockEndRead(self); - /* Check if the Iterator has been exhausted. */ - error = DeeObject_CmpGeAsBool(old_index, self->si_size); - if unlikely(error < 0) - goto err_old_index; - if (error) - goto eof_old_index; - /* Check if the index has changed during the comparison. */ - if unlikely(old_index != atomic_read(&self->si_index)) - goto old_index_again; - /* Lookup the item that's going to be returned. */ - result = (*self->si_getitem)(self->si_seq, old_index); - if unlikely(!result) { - if (!DeeError_Catch(&DeeError_UnboundItem)) - goto err_old_index; - /* Unbound item (just skip it!). */ - new_index = old_index; - Dee_Incref(new_index); - for (;;) { - if (DeeObject_Inc(&new_index)) - goto err_new_index; - error = DeeObject_CmpGeAsBool(new_index, self->si_size); - if unlikely(error < 0) - goto err_new_index; - if (error) - goto eof_new_index; - /* Check if the index has changed during the comparison. */ - if unlikely(old_index != atomic_read(&self->si_index)) - goto new_index_again; - result = (*self->si_getitem)(self->si_seq, new_index); - if likely(result) - goto set_new_index_plus; - if (!DeeError_Catch(&DeeError_UnboundItem)) - goto err_new_index; - } - } - /* Increment the index. */ - new_index = old_index; - Dee_Incref(new_index); -set_new_index_plus: - if (DeeObject_Inc(&new_index)) { - Dee_Decref(result); - goto err_new_index; - } - SeqIterator_LockWrite(self); - COMPILER_READ_BARRIER(); - if (old_index != self->si_index) { - /* The index was changed in the mean time. */ - SeqIterator_LockEndWrite(self); - Dee_Decref(result); - old_index = new_index; -old_index_again: - Dee_Decref(old_index); - goto again; - } - self->si_index = new_index; /* Override reference. */ - SeqIterator_LockEndWrite(self); - /* Drop the old-index reference we've inherited - * when overriding `self->si_index' */ - Dee_Decref(old_index); -end: - /* Drop our temporary reference from the old index. */ - Dee_Decref(old_index); - return result; -new_index_again: - Dee_Decref(new_index); - goto old_index_again; -eof_new_index: - Dee_Decref(new_index); -eof_old_index: - result = ITER_DONE; - goto end; -err_new_index: - Dee_Decref(new_index); -err_old_index: - result = NULL; - goto end; -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -seqiterator_init(SeqIterator *__restrict self, size_t argc, DeeObject *const *argv) { - DeeTypeObject *tp_iter; - self->si_index = DeeInt_Zero; - if (DeeArg_Unpack(argc, argv, "o|o:GenericIterator", &self->si_seq, &self->si_index)) - goto err; - if (DeeObject_AssertTypeExact(self->si_index, &DeeInt_Type)) - goto err; - tp_iter = Dee_TYPE(self->si_seq); - if (!tp_iter->tp_seq || !tp_iter->tp_seq->tp_getitem) { - if (!DeeType_InheritGetItem(tp_iter)) - goto err_not_implemented; - } - ASSERT(tp_iter->tp_seq); - ASSERT(tp_iter->tp_seq->tp_getitem); - self->si_getitem = tp_iter->tp_seq->tp_getitem; - self->si_size = DeeObject_SizeOb(self->si_seq); - if unlikely(!self->si_size) - goto err; - Dee_atomic_rwlock_init(&self->si_lock); - Dee_Incref(self->si_seq); - Dee_Incref(self->si_index); - return 0; -err_not_implemented: - err_unimplemented_operator(tp_iter, OPERATOR_GETITEM); -err: - return -1; -} - - -#define DEFINE_SEQITERATOR_COMPARE(name, cmp_name) \ - PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL \ - name(SeqIterator *self, SeqIterator *other) { \ - DREF DeeObject *lindex, *rindex, *result; \ - if (DeeObject_AssertType(other, &DeeGenericIterator_Type)) \ - goto err; \ - SeqIterator_LockRead(self); \ - lindex = self->si_index; \ - Dee_Incref(lindex); \ - SeqIterator_LockEndRead(self); \ - SeqIterator_LockRead(other); \ - rindex = other->si_index; \ - Dee_Incref(rindex); \ - SeqIterator_LockEndRead(other); \ - result = cmp_name(lindex, rindex); \ - Dee_Decref(rindex); \ - Dee_Decref(lindex); \ - return result; \ - err: \ - return NULL; \ - } -DEFINE_SEQITERATOR_COMPARE(seqiterator_eq, DeeObject_CmpEq) -DEFINE_SEQITERATOR_COMPARE(seqiterator_ne, DeeObject_CmpNe) -DEFINE_SEQITERATOR_COMPARE(seqiterator_lo, DeeObject_CmpLo) -DEFINE_SEQITERATOR_COMPARE(seqiterator_le, DeeObject_CmpLe) -DEFINE_SEQITERATOR_COMPARE(seqiterator_gr, DeeObject_CmpGr) -DEFINE_SEQITERATOR_COMPARE(seqiterator_ge, DeeObject_CmpGe) -#undef DEFINE_SEQITERATOR_COMPARE - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seqiterator_nii_getseq(SeqIterator *__restrict self) { - return_reference_(self->si_seq); -} - -PRIVATE WUNUSED NONNULL((1)) size_t DCALL -seqiterator_nii_getindex(SeqIterator *__restrict self) { - size_t result; - DREF DeeObject *index; - SeqIterator_LockRead(self); - index = self->si_index; - Dee_Incref(index); - SeqIterator_LockEndRead(self); - if unlikely(DeeObject_AsSize(index, &result)) - goto err_index; - if unlikely(result == (size_t)-1) - goto err_index_overflow; - Dee_Decref_unlikely(index); - return result; -err_index_overflow: - err_integer_overflow(index, sizeof(size_t) * 8, true); -err_index: - Dee_Decref_unlikely(index); - return (size_t)-1; -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -seqiterator_nii_setindex(SeqIterator *__restrict self, size_t new_index) { - DREF DeeObject *old_index; - DREF DeeObject *index = DeeInt_NewSize(new_index); - if unlikely(!index) - goto err; - SeqIterator_LockWrite(self); - old_index = self->si_index; - self->si_index = index; - SeqIterator_LockEndWrite(self); - Dee_Decref(old_index); - return 0; -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -seqiterator_nii_rewind(SeqIterator *__restrict self) { - DREF DeeObject *old_index; - Dee_Incref(DeeInt_Zero); - SeqIterator_LockWrite(self); - old_index = self->si_index; - self->si_index = DeeInt_Zero; - SeqIterator_LockEndWrite(self); - Dee_Decref(old_index); - return 0; -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -seqiterator_nii_revert(SeqIterator *__restrict self, size_t step) { - int temp; - size_t old_index; - size_t new_index; - DREF DeeObject *new_index_ob; - DREF DeeObject *old_index_ob; -again_read_index: - SeqIterator_LockRead(self); - old_index_ob = self->si_index; - Dee_Incref(old_index_ob); - SeqIterator_LockEndRead(self); - temp = DeeObject_AsSize(old_index_ob, &old_index); - Dee_Decref_unlikely(old_index_ob); - if unlikely(temp) - goto err; - if (OVERFLOW_USUB(old_index, step, &new_index)) { - new_index = 0; - new_index_ob = DeeInt_Zero; - Dee_Incref(DeeInt_Zero); - } else { - new_index_ob = DeeInt_NewSize(new_index); - if unlikely(!new_index_ob) - goto err; - } - SeqIterator_LockWrite(self); - if unlikely(old_index_ob != self->si_index) { - SeqIterator_LockEndWrite(self); - Dee_Decref(new_index_ob); - goto again_read_index; - } - self->si_index = new_index_ob; /* Inherit reference (x2) */ - SeqIterator_LockEndWrite(self); - Dee_Decref(old_index_ob); - return new_index == 0 ? 1 : 2; -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -seqiterator_nii_advance(SeqIterator *__restrict self, size_t step) { - int temp; - size_t old_index; - size_t new_index; - size_t size; - DREF DeeObject *new_index_ob; - DREF DeeObject *old_index_ob; -again_read_index: - SeqIterator_LockRead(self); - old_index_ob = self->si_index; - Dee_Incref(old_index_ob); - SeqIterator_LockEndRead(self); - temp = DeeObject_AsSize(old_index_ob, &old_index); - Dee_Decref_unlikely(old_index_ob); - if unlikely(temp) - goto err; - if (OVERFLOW_UADD(old_index, step, &new_index)) - goto err_overflow; - size = DeeObject_Size(self->si_size); - if unlikely(size == (size_t)-1) - goto err; - if (new_index > size) - new_index = size; - new_index_ob = DeeInt_NewSize(new_index); - if unlikely(!new_index_ob) - goto err; - SeqIterator_LockWrite(self); - if unlikely(old_index_ob != self->si_index) { - SeqIterator_LockEndWrite(self); - Dee_Decref(new_index_ob); - goto again_read_index; - } - self->si_index = new_index_ob; /* Inherit reference (x2) */ - SeqIterator_LockEndWrite(self); - Dee_Decref(old_index_ob); - return new_index >= size ? 1 : 2; -err_overflow: - err_integer_overflow_i(sizeof(size_t) * 8, true); -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -seqiterator_nii_prev(SeqIterator *__restrict self) { - int temp; - size_t old_index; - DREF DeeObject *new_index_ob; - DREF DeeObject *old_index_ob; -again_read_index: - SeqIterator_LockRead(self); - old_index_ob = self->si_index; - Dee_Incref(old_index_ob); - SeqIterator_LockEndRead(self); - temp = DeeObject_AsSize(old_index_ob, &old_index); - Dee_Decref_unlikely(old_index_ob); - if unlikely(temp) - goto err; - if (old_index == 0) - return 1; - new_index_ob = DeeInt_NewSize(old_index - 1); - if unlikely(!new_index_ob) - goto err; - SeqIterator_LockWrite(self); - if unlikely(old_index_ob != self->si_index) { - SeqIterator_LockEndWrite(self); - Dee_Decref(new_index_ob); - goto again_read_index; - } - self->si_index = new_index_ob; /* Inherit reference (x2) */ - SeqIterator_LockEndWrite(self); - Dee_Decref(old_index_ob); - return 0; -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -seqiterator_nii_next(SeqIterator *__restrict self) { - int temp; - size_t old_index; - size_t size; - DREF DeeObject *new_index_ob; - DREF DeeObject *old_index_ob; -again_read_index: - SeqIterator_LockRead(self); - old_index_ob = self->si_index; - Dee_Incref(old_index_ob); - SeqIterator_LockEndRead(self); - temp = DeeObject_AsSize(old_index_ob, &old_index); - Dee_Decref_unlikely(old_index_ob); - if unlikely(temp) - goto err; - size = DeeObject_Size(self->si_size); - if unlikely(size == (size_t)-1) - goto err; - if (old_index >= size) - return 1; - new_index_ob = DeeInt_NewSize(old_index + 1); - if unlikely(!new_index_ob) - goto err; - SeqIterator_LockWrite(self); - if unlikely(old_index_ob != self->si_index) { - SeqIterator_LockEndWrite(self); - Dee_Decref(new_index_ob); - goto again_read_index; - } - self->si_index = new_index_ob; /* Inherit reference (x2) */ - SeqIterator_LockEndWrite(self); - Dee_Decref(old_index_ob); - return 0; -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -seqiterator_nii_hasprev(SeqIterator *__restrict self) { - int temp; - size_t index; - DREF DeeObject *index_ob; - SeqIterator_LockRead(self); - index_ob = self->si_index; - Dee_Incref(index_ob); - SeqIterator_LockEndRead(self); - temp = DeeObject_AsSize(index_ob, &index); - Dee_Decref_unlikely(index_ob); - if unlikely(temp) - goto err; - return index != 0; -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seqiterator_nii_peek(SeqIterator *__restrict self) { - int error; - DREF DeeObject *index; - DREF DeeObject *result; -again: - SeqIterator_LockRead(self); - index = self->si_index; - Dee_Incref(index); - SeqIterator_LockEndRead(self); - /* Check if the Iterator has been exhausted. */ - error = DeeObject_CmpGeAsBool(index, self->si_size); - if unlikely(error < 0) - goto err_index; - if (error) - goto eof_index; - /* Check if the index has changed during the comparison. */ - if unlikely(index != atomic_read(&self->si_index)) - goto decref_index_and_again; - /* Lookup the item that's going to be returned. */ - result = (*self->si_getitem)(self->si_seq, index); - if unlikely(!result) { - if (!DeeError_Catch(&DeeError_UnboundItem)) - goto err_index; - /* Unbound item (just skip it!). */ - for (;;) { - if (DeeObject_Inc(&index)) - goto err_index; - error = DeeObject_CmpGeAsBool(index, self->si_size); - if unlikely(error < 0) - goto err_index; - if (error) - goto eof_index; - /* Check if the index has changed during the comparison. */ - if unlikely(index != atomic_read(&self->si_index)) - goto decref_index_and_again; - result = (*self->si_getitem)(self->si_seq, index); - if likely(result) - break; - if (!DeeError_Catch(&DeeError_UnboundItem)) - goto err_index; - } - } - Dee_Decref(index); - return result; -decref_index_and_again: - Dee_Decref(index); - goto again; -eof_index: - Dee_Decref(index); - return ITER_DONE; -err_index: - Dee_Decref(index); -/*err:*/ - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seqiterator_index_get(SeqIterator *__restrict self) { - DREF DeeObject *result; - SeqIterator_LockRead(self); - result = self->si_index; - Dee_Incref(result); - SeqIterator_LockEndRead(self); - return result; -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -seqiterator_index_del(SeqIterator *__restrict self) { - DREF DeeObject *old_ob; - Dee_Incref(DeeInt_Zero); - SeqIterator_LockWrite(self); - old_ob = self->si_index; - self->si_index = DeeInt_Zero; - SeqIterator_LockEndWrite(self); - Dee_Decref(old_ob); - return 0; -} - -PRIVATE WUNUSED NONNULL((1, 2)) int DCALL -seqiterator_index_set(SeqIterator *self, DeeObject *new_index) { - DREF DeeObject *old_ob; - if (DeeObject_AssertTypeExact(new_index, &DeeInt_Type)) - goto err; - Dee_Incref(new_index); - SeqIterator_LockWrite(self); - old_ob = self->si_index; - self->si_index = new_index; - SeqIterator_LockEndWrite(self); - Dee_Decref(old_ob); - return 0; -err: - return -1; -} - -PRIVATE struct type_getset tpconst seqiterator_getsets[] = { - TYPE_GETSET_F(STR_index, - &seqiterator_index_get, - &seqiterator_index_del, - &seqiterator_index_set, - METHOD_FNOREFESCAPE, - "->?Dint"), - TYPE_GETSET_END -}; - -PRIVATE struct type_member tpconst seqiterator_members[] = { - TYPE_MEMBER_FIELD_DOC(STR_seq, STRUCT_OBJECT, offsetof(SeqIterator, si_seq), "->?DSequence"), - TYPE_MEMBER_FIELD_DOC("__size__", STRUCT_OBJECT, offsetof(SeqIterator, si_size), "->?X2?DInt?O"), - TYPE_MEMBER_END -}; - -PRIVATE struct type_nii tpconst seqiterator_nii = { - /* .nii_class = */ TYPE_ITERX_CLASS_BIDIRECTIONAL, - /* .nii_flags = */ TYPE_ITERX_FNORMAL, - { - /* .nii_common = */ { - /* .nii_getseq = */ (dfunptr_t)&seqiterator_nii_getseq, - /* .nii_getindex = */ (dfunptr_t)&seqiterator_nii_getindex, - /* .nii_setindex = */ (dfunptr_t)&seqiterator_nii_setindex, - /* .nii_rewind = */ (dfunptr_t)&seqiterator_nii_rewind, - /* .nii_revert = */ (dfunptr_t)&seqiterator_nii_revert, - /* .nii_advance = */ (dfunptr_t)&seqiterator_nii_advance, - /* .nii_prev = */ (dfunptr_t)&seqiterator_nii_prev, - /* .nii_next = */ (dfunptr_t)&seqiterator_nii_next, - /* .nii_hasprev = */ (dfunptr_t)&seqiterator_nii_hasprev, - /* .nii_peek = */ (dfunptr_t)&seqiterator_nii_peek, - } - } -}; - -PRIVATE struct type_cmp seqiterator_cmp = { - /* .tp_hash = */ NULL, - /* .tp_compare_eq = */ NULL, - /* .tp_compare = */ NULL, - /* .tp_trycompare_eq = */ NULL, - /* .tp_eq = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))&seqiterator_eq, - /* .tp_ne = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))&seqiterator_ne, - /* .tp_lo = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))&seqiterator_lo, - /* .tp_le = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))&seqiterator_le, - /* .tp_gr = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))&seqiterator_gr, - /* .tp_ge = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))&seqiterator_ge, - /* .tp_nii = */ &seqiterator_nii -}; - -INTERN DeeTypeObject DeeGenericIterator_Type = { - OBJECT_HEAD_INIT(&DeeType_Type), - /* .tp_name = */ "_GenericIterator", - /* .tp_doc = */ DOC("(seq:?DSequence,index=!0)"), - /* .tp_flags = */ TP_FNORMAL, - /* .tp_weakrefs = */ 0, - /* .tp_features = */ TF_NONE, - /* .tp_base = */ &DeeIterator_Type, - /* .tp_init = */ { - { - /* .tp_alloc = */ { - /* .tp_ctor = */ (dfunptr_t)&seqiterator_ctor, - /* .tp_copy_ctor = */ (dfunptr_t)&seqiterator_copy, - /* .tp_deep_ctor = */ (dfunptr_t)NULL, - /* .tp_any_ctor = */ (dfunptr_t)&seqiterator_init, - TYPE_FIXED_ALLOCATOR(SeqIterator) - } - }, - /* .tp_dtor = */ (void (DCALL *)(DeeObject *__restrict))&seqiterator_fini, - /* .tp_assign = */ NULL, - /* .tp_move_assign = */ NULL - }, - /* .tp_cast = */ { - /* .tp_str = */ NULL, - /* .tp_repr = */ NULL, - /* .tp_bool = */ NULL, - /* .tp_print = */ NULL, - /* .tp_printrepr = */ (Dee_ssize_t (DCALL *)(DeeObject *__restrict, dformatprinter, void *))&seqiterator_printrepr - }, - /* .tp_call = */ NULL, - /* .tp_visit = */ (void (DCALL *)(DeeObject *__restrict, dvisit_t, void *))&seqiterator_visit, - /* .tp_gc = */ NULL, - /* .tp_math = */ NULL, - /* .tp_cmp = */ &seqiterator_cmp, - /* .tp_seq = */ NULL, - /* .tp_iter_next = */ (DREF DeeObject *(DCALL *)(DeeObject *__restrict))&seqiterator_next, - /* .tp_iterator = */ NULL, - /* .tp_attr = */ NULL, - /* .tp_with = */ NULL, - /* .tp_buffer = */ NULL, - /* .tp_methods = */ NULL, - /* .tp_getsets = */ seqiterator_getsets, - /* .tp_members = */ seqiterator_members, - /* .tp_class_methods = */ NULL, - /* .tp_class_getsets = */ NULL, - /* .tp_class_members = */ NULL -}; - - -typedef struct { - OBJECT_HEAD - /* [1..1][const] Either the `nsi_getitem' or `nsi_getitem_fast' callback of a sequence. */ - DREF DeeObject *(DCALL *ni_getitem)(DeeObject *__restrict self, size_t index); - DREF DeeObject *ni_seq; /* [1..1][const] The Sequence being iterated. */ - size_t ni_size; /* [1..1][const] The size of the Sequence. */ - size_t ni_index; /* [1..1][lock(ATOMIC)] Index of next item to enumerate. */ -} NsiIterator; - -PRIVATE /*WUNUSED*/ NONNULL((1)) int DCALL -nsiiterator_ctor(NsiIterator *__restrict self) { - /* Don't assign `ni_getitem()' because "ni_size" is 0, meaning - * there is no valid index with which to call the operator. */ - /*self->ni_getitem = &DeeSeq_GetItem;*/ - DBG_memset(&self->ni_getitem, 0xcc, sizeof(self->ni_getitem)); - self->ni_seq = Dee_EmptySeq; - self->ni_index = 0; - self->ni_size = 0; - Dee_Incref(Dee_EmptySeq); - return 0; -} - -PRIVATE WUNUSED NONNULL((1, 2)) int DCALL -nsiiterator_copy(NsiIterator *__restrict self, - NsiIterator *__restrict other) { - self->ni_getitem = other->ni_getitem; - self->ni_seq = other->ni_seq; - self->ni_size = other->ni_size; - self->ni_index = atomic_read(&other->ni_index); - Dee_Incref(self->ni_seq); - return 0; -} - -PRIVATE NONNULL((1)) void DCALL -nsiiterator_fini(NsiIterator *__restrict self) { - Dee_Decref(self->ni_seq); -} - -PRIVATE NONNULL((1, 2)) void DCALL -nsiiterator_visit(NsiIterator *__restrict self, dvisit_t proc, void *arg) { - Dee_Visit(self->ni_seq); -} - -PRIVATE WUNUSED NONNULL((1)) Dee_ssize_t DCALL -nsiiterator_printrepr(NsiIterator *__restrict self, - dformatprinter printer, void *arg) { - return DeeFormat_Printf(printer, arg, - "rt.NsiIterator(%r, %" PRFuSIZ " /* of %" PRFuSIZ " */)", - self->ni_seq, atomic_read(&self->ni_index), self->ni_size); -} - -PRIVATE WUNUSED NONNULL((1)) Dee_ssize_t DCALL -fastnsiiterator_printrepr(NsiIterator *__restrict self, - dformatprinter printer, void *arg) { - return DeeFormat_Printf(printer, arg, - "rt.FastNsiIterator(%r, %" PRFuSIZ " /* of %" PRFuSIZ " */)", - self->ni_seq, atomic_read(&self->ni_index), self->ni_size); -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -nsiiterator_next(NsiIterator *__restrict self) { - DREF DeeObject *result; - size_t old_index, new_index; -again: - old_index = atomic_read(&self->ni_index); - new_index = old_index; - for (;;) { - if (new_index >= self->ni_size) - return ITER_DONE; - result = (*self->ni_getitem)(self->ni_seq, new_index); - ++new_index; - if (result) - break; - if (!DeeError_Catch(&DeeError_UnboundItem)) - goto err; - } - if (!atomic_cmpxch_or_write(&self->ni_index, old_index, new_index)) { - Dee_Decref(result); - goto again; - } - return result; -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -fastnsiiterator_next(NsiIterator *__restrict self) { - DREF DeeObject *result; - size_t old_index, new_index; -again: - old_index = atomic_read(&self->ni_index); - new_index = old_index; - for (;;) { - if (new_index >= self->ni_size) - return ITER_DONE; - result = (*self->ni_getitem)(self->ni_seq, new_index); - ++new_index; - if (result) - break; - /*if (!DeeError_Catch(&DeeError_UnboundItem)) - goto err;*/ - } - if (!atomic_cmpxch_or_write(&self->ni_index, old_index, new_index)) { - Dee_Decref(result); - goto again; - } - return result; -/* -err: - return NULL;*/ -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -nsiiterator_init(NsiIterator *__restrict self, size_t argc, DeeObject *const *argv) { - DeeTypeMRO mro; - DeeTypeObject *tp_iter; - self->ni_index = 0; - if (DeeArg_Unpack(argc, argv, "o|" UNPuSIZ ":NsiIterator", &self->ni_seq, &self->ni_index)) - goto err; - tp_iter = Dee_TYPE(self->ni_seq); - tp_iter = DeeTypeMRO_Init(&mro, tp_iter); - for (;;) { - if unlikely(tp_iter == &DeeSeq_Type || tp_iter == NULL) - goto err_not_implemented; - if (tp_iter->tp_seq && - tp_iter->tp_seq->tp_nsi && - tp_iter->tp_seq->tp_nsi->nsi_class == TYPE_SEQX_CLASS_SEQ && - tp_iter->tp_seq->tp_nsi->nsi_seqlike.nsi_getitem) - break; - tp_iter = DeeTypeMRO_Next(&mro, tp_iter); - } - self->ni_getitem = tp_iter->tp_seq->tp_nsi->nsi_seqlike.nsi_getitem; - self->ni_size = (*tp_iter->tp_seq->tp_nsi->nsi_seqlike.nsi_getsize)(self->ni_seq); - if unlikely(self->ni_size == (size_t)-1) - goto err; - Dee_Incref(self->ni_seq); - return 0; -err_not_implemented: - err_unimplemented_operator(tp_iter, OPERATOR_GETITEM); -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -fastnsiiterator_init(NsiIterator *__restrict self, size_t argc, DeeObject *const *argv) { - DeeTypeMRO mro; - DeeTypeObject *tp_iter; - self->ni_index = 0; - if (DeeArg_Unpack(argc, argv, "o|" UNPuSIZ ":FastNsiIterator", &self->ni_seq, &self->ni_index)) - goto err; - tp_iter = Dee_TYPE(self->ni_seq); - tp_iter = DeeTypeMRO_Init(&mro, tp_iter); - for (;;) { - if unlikely(tp_iter == &DeeSeq_Type || tp_iter == NULL) - goto err_not_implemented; - if (tp_iter->tp_seq && - tp_iter->tp_seq->tp_nsi && - tp_iter->tp_seq->tp_nsi->nsi_class == TYPE_SEQX_CLASS_SEQ && - tp_iter->tp_seq->tp_nsi->nsi_seqlike.nsi_getitem_fast) - break; - tp_iter = DeeTypeMRO_Next(&mro, tp_iter); - } - self->ni_getitem = tp_iter->tp_seq->tp_nsi->nsi_seqlike.nsi_getitem_fast; - self->ni_size = (*tp_iter->tp_seq->tp_nsi->nsi_seqlike.nsi_getsize)(self->ni_seq); - if unlikely(self->ni_size == (size_t)-1) - goto err; - Dee_Incref(self->ni_seq); - return 0; -err_not_implemented: - err_unimplemented_operator(tp_iter, OPERATOR_GETITEM); -err: - return -1; -} - - -#define DEFINE_SEQITERATOR_COMPARE(name, cmp) \ - PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL \ - name(NsiIterator *self, NsiIterator *other) { \ - if (DeeObject_AssertType(other, Dee_TYPE(self))) \ - goto err; \ - if (self->ni_seq != other->ni_seq) \ - return_bool(self->ni_seq cmp other->ni_seq); \ - return_bool(self->ni_index cmp other->ni_index); \ - err: \ - return NULL; \ - } -DEFINE_SEQITERATOR_COMPARE(nsiiterator_eq, ==) -DEFINE_SEQITERATOR_COMPARE(nsiiterator_ne, !=) -DEFINE_SEQITERATOR_COMPARE(nsiiterator_lo, <) -DEFINE_SEQITERATOR_COMPARE(nsiiterator_le, <=) -DEFINE_SEQITERATOR_COMPARE(nsiiterator_gr, >) -DEFINE_SEQITERATOR_COMPARE(nsiiterator_ge, >=) -#undef DEFINE_SEQITERATOR_COMPARE - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -nsiiterator_nii_getseq(NsiIterator *__restrict self) { - return_reference_(self->ni_seq); -} - -PRIVATE WUNUSED NONNULL((1)) size_t DCALL -nsiiterator_nii_getindex(NsiIterator *__restrict self) { - return atomic_read(&self->ni_index); -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -nsiiterator_nii_setindex(NsiIterator *__restrict self, size_t new_index) { - atomic_write(&self->ni_index, new_index); - return 0; -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -nsiiterator_nii_rewind(NsiIterator *__restrict self) { - atomic_write(&self->ni_index, 0); - return 0; -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -nsiiterator_nii_revert(NsiIterator *__restrict self, size_t step) { - size_t old_index, new_index; - do { - old_index = atomic_read(&self->ni_index); - if (OVERFLOW_USUB(old_index, step, &new_index)) - new_index = 0; - } while (!atomic_cmpxch_or_write(&self->ni_index, old_index, new_index)); - return new_index == 0 ? 1 : 2; -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -nsiiterator_nii_advance(NsiIterator *__restrict self, size_t step) { - size_t old_index, new_index; - do { - old_index = atomic_read(&self->ni_index); - if (OVERFLOW_UADD(old_index, step, &new_index)) - new_index = (size_t)-1; - if (new_index > self->ni_size) - new_index = self->ni_size; - } while (!atomic_cmpxch_or_write(&self->ni_index, old_index, new_index)); - return new_index >= self->ni_size ? 1 : 2; -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -nsiiterator_nii_prev(NsiIterator *__restrict self) { - size_t old_index; - do { - old_index = atomic_read(&self->ni_index); - if (old_index == 0) - return 1; - } while (!atomic_cmpxch_or_write(&self->ni_index, old_index, old_index - 1)); - return 0; -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -nsiiterator_nii_next(NsiIterator *__restrict self) { - size_t old_index; - do { - old_index = atomic_read(&self->ni_index); - if (old_index >= self->ni_size) - return 1; - } while (!atomic_cmpxch_or_write(&self->ni_index, old_index, old_index + 1)); - return 0; -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -nsiiterator_nii_hasprev(NsiIterator *__restrict self) { - return atomic_read(&self->ni_index) > 0 ? 1 : 0; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -nsiiterator_nii_peek(NsiIterator *__restrict self) { - DREF DeeObject *result; - size_t old_index, new_index; - old_index = atomic_read(&self->ni_index); - new_index = old_index; - for (;;) { - if (new_index >= self->ni_size) - return ITER_DONE; - result = (*self->ni_getitem)(self->ni_seq, new_index); - ++new_index; - if (result) - break; - if (!DeeError_Catch(&DeeError_UnboundItem)) - goto err; - } - return result; -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -fastnsiiterator_nii_peek(NsiIterator *__restrict self) { - DREF DeeObject *result; - size_t old_index, new_index; - old_index = atomic_read(&self->ni_index); - new_index = old_index; - for (;;) { - if (new_index >= self->ni_size) - return ITER_DONE; - result = (*self->ni_getitem)(self->ni_seq, new_index); - ++new_index; - if (result) - break; - /*if (!DeeError_Catch(&DeeError_UnboundItem)) - goto err;*/ - } - return result; -/* -err: - return NULL;*/ -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -nsiiterator_index_get(NsiIterator *__restrict self) { - size_t index = atomic_read(&self->ni_index); - return DeeInt_NewSize(index); -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -nsiiterator_index_del(NsiIterator *__restrict self) { - atomic_write(&self->ni_index, 0); - return 0; -} - -PRIVATE WUNUSED NONNULL((1, 2)) int DCALL -nsiiterator_index_set(NsiIterator *self, DeeObject *new_index) { - size_t index; - if (DeeObject_AsSize(new_index, &index)) - goto err; - if (index > self->ni_size) - index = self->ni_size; - atomic_write(&self->ni_index, index); - return 0; -err: - return -1; -} - -PRIVATE struct type_getset tpconst nsiiterator_getsets[] = { - TYPE_GETSET_F(STR_index, - &nsiiterator_index_get, - &nsiiterator_index_del, - &nsiiterator_index_set, - METHOD_FNOREFESCAPE, - "->?Dint"), - TYPE_GETSET_END -}; - -PRIVATE struct type_member tpconst nsiiterator_members[] = { - TYPE_MEMBER_FIELD_DOC(STR_seq, STRUCT_OBJECT, offsetof(NsiIterator, ni_seq), "->?DSequence"), - TYPE_MEMBER_FIELD("__size__", STRUCT_CONST | STRUCT_SIZE_T, offsetof(NsiIterator, ni_size)), - TYPE_MEMBER_END -}; - -PRIVATE struct type_nii tpconst nsiiterator_nii = { - /* .nii_class = */ TYPE_ITERX_CLASS_BIDIRECTIONAL, - /* .nii_flags = */ TYPE_ITERX_FNORMAL, - { - /* .nii_common = */ { - /* .nii_getseq = */ (dfunptr_t)&nsiiterator_nii_getseq, - /* .nii_getindex = */ (dfunptr_t)&nsiiterator_nii_getindex, - /* .nii_setindex = */ (dfunptr_t)&nsiiterator_nii_setindex, - /* .nii_rewind = */ (dfunptr_t)&nsiiterator_nii_rewind, - /* .nii_revert = */ (dfunptr_t)&nsiiterator_nii_revert, - /* .nii_advance = */ (dfunptr_t)&nsiiterator_nii_advance, - /* .nii_prev = */ (dfunptr_t)&nsiiterator_nii_prev, - /* .nii_next = */ (dfunptr_t)&nsiiterator_nii_next, - /* .nii_hasprev = */ (dfunptr_t)&nsiiterator_nii_hasprev, - /* .nii_peek = */ (dfunptr_t)&nsiiterator_nii_peek, - } - } -}; - -PRIVATE struct type_nii tpconst fastnsiiterator_nii = { - /* .nii_class = */ TYPE_ITERX_CLASS_BIDIRECTIONAL, - /* .nii_flags = */ TYPE_ITERX_FNORMAL, - { - /* .nii_common = */ { - /* .nii_getseq = */ (dfunptr_t)&nsiiterator_nii_getseq, - /* .nii_getindex = */ (dfunptr_t)&nsiiterator_nii_getindex, - /* .nii_setindex = */ (dfunptr_t)&nsiiterator_nii_setindex, - /* .nii_rewind = */ (dfunptr_t)&nsiiterator_nii_rewind, - /* .nii_revert = */ (dfunptr_t)&nsiiterator_nii_revert, - /* .nii_advance = */ (dfunptr_t)&nsiiterator_nii_advance, - /* .nii_prev = */ (dfunptr_t)&nsiiterator_nii_prev, - /* .nii_next = */ (dfunptr_t)&nsiiterator_nii_next, - /* .nii_hasprev = */ (dfunptr_t)&nsiiterator_nii_hasprev, - /* .nii_peek = */ (dfunptr_t)&fastnsiiterator_nii_peek, - } - } -}; - -PRIVATE struct type_cmp nsiiterator_cmp = { - /* .tp_hash = */ NULL, - /* .tp_compare_eq = */ NULL, - /* .tp_compare = */ NULL, - /* .tp_trycompare_eq = */ NULL, - /* .tp_eq = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))&nsiiterator_eq, - /* .tp_ne = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))&nsiiterator_ne, - /* .tp_lo = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))&nsiiterator_lo, - /* .tp_le = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))&nsiiterator_le, - /* .tp_gr = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))&nsiiterator_gr, - /* .tp_ge = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))&nsiiterator_ge, - /* .tp_nii = */ &nsiiterator_nii -}; - -PRIVATE struct type_cmp fastnsiiterator_cmp = { - /* .tp_hash = */ NULL, - /* .tp_compare_eq = */ NULL, - /* .tp_compare = */ NULL, - /* .tp_trycompare_eq = */ NULL, - /* .tp_eq = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))&nsiiterator_eq, - /* .tp_ne = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))&nsiiterator_ne, - /* .tp_lo = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))&nsiiterator_lo, - /* .tp_le = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))&nsiiterator_le, - /* .tp_gr = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))&nsiiterator_gr, - /* .tp_ge = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))&nsiiterator_ge, - /* .tp_nii = */ &fastnsiiterator_nii -}; - -INTERN DeeTypeObject DeeNsiIterator_Type = { - OBJECT_HEAD_INIT(&DeeType_Type), - /* .tp_name = */ "_NsiIterator", - /* .tp_doc = */ DOC("(seq:?DSequence,index=!0)"), - /* .tp_flags = */ TP_FNORMAL, - /* .tp_weakrefs = */ 0, - /* .tp_features = */ TF_NONE, - /* .tp_base = */ &DeeIterator_Type, - /* .tp_init = */ { - { - /* .tp_alloc = */ { - /* .tp_ctor = */ (dfunptr_t)&nsiiterator_ctor, - /* .tp_copy_ctor = */ (dfunptr_t)&nsiiterator_copy, - /* .tp_deep_ctor = */ (dfunptr_t)NULL, - /* .tp_any_ctor = */ (dfunptr_t)&nsiiterator_init, - TYPE_FIXED_ALLOCATOR(NsiIterator) - } - }, - /* .tp_dtor = */ (void (DCALL *)(DeeObject *__restrict))&nsiiterator_fini, - /* .tp_assign = */ NULL, - /* .tp_move_assign = */ NULL - }, - /* .tp_cast = */ { - /* .tp_str = */ NULL, - /* .tp_repr = */ NULL, - /* .tp_bool = */ NULL, - /* .tp_print = */ NULL, - /* .tp_printrepr = */ (Dee_ssize_t (DCALL *)(DeeObject *__restrict, dformatprinter, void *))&nsiiterator_printrepr - }, - /* .tp_call = */ NULL, - /* .tp_visit = */ (void (DCALL *)(DeeObject *__restrict, dvisit_t, void *))&nsiiterator_visit, - /* .tp_gc = */ NULL, - /* .tp_math = */ NULL, - /* .tp_cmp = */ &nsiiterator_cmp, - /* .tp_seq = */ NULL, - /* .tp_iter_next = */ (DREF DeeObject *(DCALL *)(DeeObject *__restrict))&nsiiterator_next, - /* .tp_iterator = */ NULL, - /* .tp_attr = */ NULL, - /* .tp_with = */ NULL, - /* .tp_buffer = */ NULL, - /* .tp_methods = */ NULL, - /* .tp_getsets = */ nsiiterator_getsets, - /* .tp_members = */ nsiiterator_members, - /* .tp_class_methods = */ NULL, - /* .tp_class_getsets = */ NULL, - /* .tp_class_members = */ NULL -}; - -INTERN DeeTypeObject DeeFastNsiIterator_Type = { - OBJECT_HEAD_INIT(&DeeType_Type), - /* .tp_name = */ "_FastNsiIterator", - /* .tp_doc = */ DOC("(seq:?DSequence,index=!0)"), - /* .tp_flags = */ TP_FNORMAL, - /* .tp_weakrefs = */ 0, - /* .tp_features = */ TF_NONE, - /* .tp_base = */ &DeeNsiIterator_Type, - /* .tp_init = */ { - { - /* .tp_alloc = */ { - /* .tp_ctor = */ (dfunptr_t)&nsiiterator_ctor, - /* .tp_copy_ctor = */ (dfunptr_t)&nsiiterator_copy, - /* .tp_deep_ctor = */ (dfunptr_t)NULL, - /* .tp_any_ctor = */ (dfunptr_t)&fastnsiiterator_init, - TYPE_FIXED_ALLOCATOR(NsiIterator) - } - }, - /* .tp_dtor = */ NULL, - /* .tp_assign = */ NULL, - /* .tp_move_assign = */ NULL - }, - /* .tp_cast = */ { - /* .tp_str = */ NULL, - /* .tp_repr = */ NULL, - /* .tp_bool = */ NULL, - /* .tp_print = */ NULL, - /* .tp_printrepr = */ (Dee_ssize_t (DCALL *)(DeeObject *__restrict, dformatprinter, void *))&fastnsiiterator_printrepr - }, - /* .tp_call = */ NULL, - /* .tp_visit = */ NULL, - /* .tp_gc = */ NULL, - /* .tp_math = */ NULL, - /* .tp_cmp = */ &fastnsiiterator_cmp, - /* .tp_seq = */ NULL, - /* .tp_iter_next = */ (DREF DeeObject *(DCALL *)(DeeObject *__restrict))&fastnsiiterator_next, - /* .tp_iterator = */ NULL, - /* .tp_attr = */ NULL, - /* .tp_with = */ NULL, - /* .tp_buffer = */ NULL, - /* .tp_methods = */ NULL, - /* .tp_getsets = */ NULL, - /* .tp_members = */ NULL, - /* .tp_class_methods = */ NULL, - /* .tp_class_getsets = */ NULL, - /* .tp_class_members = */ NULL -}; - - -PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -seq_Titerself_with_SeqIterator(DeeTypeObject *tp_self, - DeeObject *__restrict self) { - DREF SeqIterator *result; - result = DeeObject_MALLOC(SeqIterator); - if unlikely(!result) - goto err; - - /* Save the getitem operator. */ - ASSERT(tp_self->tp_seq); - ASSERT(tp_self->tp_seq->tp_getitem); - ASSERT(tp_self->tp_seq->tp_sizeob); - result->si_getitem = tp_self->tp_seq->tp_getitem; - result->si_size = (*tp_self->tp_seq->tp_sizeob)(self); - if unlikely(!result->si_size) - goto err_r; - - /* Assign the initial Iterator index. */ - result->si_index = DeeInt_Zero; - Dee_Incref(DeeInt_Zero); - - /* Save a reference to the associated Sequence. */ - result->si_seq = self; - Dee_Incref(self); - Dee_atomic_rwlock_init(&result->si_lock); - DeeObject_Init(result, &DeeGenericIterator_Type); - return (DREF DeeObject *)result; -err_r: - DeeObject_FREE(result); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seq_iterself_with_SeqIterator(DeeObject *__restrict self) { - return seq_Titerself_with_SeqIterator(Dee_TYPE(self), self); -} - -PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -seq_Titerself_with_NsiIterator(DeeTypeObject *tp_self, - DeeObject *__restrict self) { - DREF NsiIterator *result; - struct type_nsi const *nsi; - result = DeeObject_MALLOC(NsiIterator); - if unlikely(!result) - goto err; - - /* Save the getitem operator. */ - ASSERT(tp_self->tp_seq); - nsi = tp_self->tp_seq->tp_nsi; - ASSERT(nsi); - ASSERT(nsi->nsi_class == Dee_TYPE_SEQX_CLASS_SEQ); - ASSERT(nsi->nsi_seqlike.nsi_getitem); - result->ni_getitem = nsi->nsi_seqlike.nsi_getitem; - result->ni_size = (*nsi->nsi_seqlike.nsi_getsize)(self); - if unlikely(result->ni_size == (size_t)-1) - goto err_r; - - result->ni_index = 0; - result->ni_seq = self; - Dee_Incref(self); - DeeObject_Init(result, &DeeNsiIterator_Type); - return (DREF DeeObject *)result; -err_r: - DeeObject_FREE(result); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seq_iterself_with_NsiIterator(DeeObject *__restrict self) { - return seq_Titerself_with_NsiIterator(Dee_TYPE(self), self); -} - -PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -seq_Titerself_with_FastNsiIterator(DeeTypeObject *tp_self, - DeeObject *__restrict self) { - DREF NsiIterator *result; - struct type_nsi const *nsi; - result = DeeObject_MALLOC(NsiIterator); - if unlikely(!result) - goto err; - - /* Save the getitem operator. */ - ASSERT(tp_self->tp_seq); - nsi = tp_self->tp_seq->tp_nsi; - ASSERT(nsi); - ASSERT(nsi->nsi_class == Dee_TYPE_SEQX_CLASS_SEQ); - ASSERT(nsi->nsi_seqlike.nsi_getitem_fast); - result->ni_getitem = nsi->nsi_seqlike.nsi_getitem_fast; - result->ni_size = (*nsi->nsi_seqlike.nsi_getsize)(self); - if unlikely(result->ni_size == (size_t)-1) - goto err_r; - - result->ni_index = 0; - result->ni_seq = self; - Dee_Incref(self); - DeeObject_Init(result, &DeeFastNsiIterator_Type); - return (DREF DeeObject *)result; -err_r: - DeeObject_FREE(result); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seq_iterself_with_FastNsiIterator(DeeObject *__restrict self) { - return seq_Titerself_with_FastNsiIterator(Dee_TYPE(self), self); -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seq_iterself(DeeObject *__restrict self) { - int found = 0; - DeeTypeObject *tp_iter; - DeeTypeObject *tp_self = Dee_TYPE(self); - if unlikely(tp_self == &DeeSeq_Type) { - /* Special case: Create an empty Iterator. - * >> This can happen when someone tries to iterate a symbolic empty-Sequence object. */ - return_empty_iterator; - } - - /* Check if we're able to implement "operator iter()" with the help of other operators. */ - tp_iter = tp_self; - DeeType_mro_foreach_start(tp_iter) { - struct type_seq *seq = tp_iter->tp_seq; - if (seq) { - if unlikely(seq->tp_iter && seq->tp_iter != &seq_iterself) - return (*seq->tp_iter)(self); - - /* Check if there are NSI operators with which we can implement an iterator. */ - if (seq->tp_nsi && - seq->tp_nsi->nsi_class == TYPE_SEQX_CLASS_SEQ && - DeeType_HasPrivateNSI(tp_iter)) { - if (!tp_self->tp_seq || !tp_self->tp_seq->tp_nsi) - DeeType_InheritNSI(tp_self); - ASSERT(tp_self->tp_seq); - ASSERT(tp_self->tp_seq->tp_nsi); - if likely(tp_self->tp_seq->tp_nsi == seq->tp_nsi) { - if (seq->tp_nsi->nsi_seqlike.nsi_getitem_fast) { - if likely(DeeType_Implements(tp_self, &DeeSeq_Type)) - tp_self->tp_seq->tp_iter = &seq_iterself_with_FastNsiIterator; - return seq_Titerself_with_FastNsiIterator(tp_iter, self); - } else if (seq->tp_nsi->nsi_seqlike.nsi_getitem) { - if likely(DeeType_Implements(tp_self, &DeeSeq_Type)) - tp_self->tp_seq->tp_iter = &seq_iterself_with_NsiIterator; - return seq_Titerself_with_NsiIterator(tp_iter, self); - } - } - } - - /* Check for deemon operators with which we can implement an iterator. */ - if (seq->tp_sizeob && DeeType_HasPrivateOperator(tp_iter, OPERATOR_SIZE)) - found |= 1; - if (seq->tp_getitem && DeeType_HasPrivateOperator(tp_iter, OPERATOR_GETITEM)) - found |= 2; - if (found == (1 | 2)) { - if (!tp_self->tp_seq || !tp_self->tp_seq->tp_getitem) - DeeType_InheritGetItem(tp_self); - if (!tp_self->tp_seq || !tp_self->tp_seq->tp_sizeob) - DeeType_InheritSize(tp_self); - ASSERT(tp_self->tp_seq); - ASSERT(tp_self->tp_seq->tp_getitem); - ASSERT(tp_self->tp_seq->tp_sizeob); - if likely(DeeType_Implements(tp_self, &DeeSeq_Type)) - tp_self->tp_seq->tp_iter = &seq_iterself_with_SeqIterator; - return seq_Titerself_with_SeqIterator(tp_iter, self); - } - } - } - DeeType_mro_foreach_end(tp_iter); -/*not_a_seq:*/ - err_unimplemented_operator(tp_self, OPERATOR_ITER); -/*err:*/ - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeTypeObject *DCALL -seqtype_get_Iterator(DeeTypeObject *__restrict self) { - int found = 0; - struct type_seq *seq; - DeeTypeObject *iter; - if unlikely(self == &DeeSeq_Type) { - /* Special case: Create an empty Iterator. - * >> This can happen when someone tries to iterate a symbolic empty-Sequence object. */ - return_reference_(&DeeGenericIterator_Type); - } - - seq = self->tp_seq; - if (seq && seq->tp_iter) { - if (seq->tp_iter == &seq_iterself_with_FastNsiIterator) - return_reference_(&DeeFastNsiIterator_Type); - if (seq->tp_iter == &seq_iterself_with_NsiIterator) - return_reference_(&DeeNsiIterator_Type); - if (seq->tp_iter == &seq_iterself_with_SeqIterator) - return_reference_(&DeeGenericIterator_Type); - goto not_a_seq; - } - - /* Check if we're able to implement "operator iter()" with the help of other operators. */ - iter = self; - DeeType_mro_foreach_start(iter) { - seq = iter->tp_seq; - if (seq) { - if unlikely(seq->tp_iter && seq->tp_iter != &seq_iterself) - goto not_a_seq; - - /* Check if there are NSI operators with which we can implement an iterator. */ - if (seq->tp_nsi && - seq->tp_nsi->nsi_class == TYPE_SEQX_CLASS_SEQ && - DeeType_HasPrivateNSI(iter)) { - if (!self->tp_seq || !self->tp_seq->tp_nsi) - DeeType_InheritNSI(self); - if (self->tp_seq && self->tp_seq->tp_nsi) { - if likely(self->tp_seq->tp_nsi == seq->tp_nsi) { - if (seq->tp_nsi->nsi_seqlike.nsi_getitem_fast) { - if likely(DeeType_Implements(self, &DeeSeq_Type)) - self->tp_seq->tp_iter = &seq_iterself_with_FastNsiIterator; - return_reference_(&DeeFastNsiIterator_Type); - } else if (seq->tp_nsi->nsi_seqlike.nsi_getitem) { - if likely(DeeType_Implements(self, &DeeSeq_Type)) - self->tp_seq->tp_iter = &seq_iterself_with_NsiIterator; - return_reference_(&DeeNsiIterator_Type); - } - } - } - } - - /* Check for deemon operators with which we can implement an iterator. */ - if (seq->tp_sizeob && DeeType_HasPrivateOperator(iter, OPERATOR_SIZE)) - found |= 1; - if (seq->tp_getitem && DeeType_HasPrivateOperator(iter, OPERATOR_GETITEM)) - found |= 2; - if (found == (1 | 2)) { - if (!self->tp_seq || !self->tp_seq->tp_getitem) - DeeType_InheritGetItem(self); - if (!self->tp_seq || !self->tp_seq->tp_sizeob) - DeeType_InheritSize(self); - ASSERT(self->tp_seq); - ASSERT(self->tp_seq->tp_getitem); - ASSERT(self->tp_seq->tp_sizeob); - if likely(DeeType_Implements(self, &DeeSeq_Type)) - self->tp_seq->tp_iter = &seq_iterself_with_SeqIterator; - return_reference_(&DeeGenericIterator_Type); - } - } - } - DeeType_mro_foreach_end(iter); -not_a_seq: - err_unimplemented_operator(self, OPERATOR_ITER); -/*err:*/ - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -seq_nsi_delrange(DeeObject *self, Dee_ssize_t i_begin, Dee_ssize_t i_end) { - if unlikely(i_begin < 0 || i_end < 0) { - size_t seq_len = DeeObject_Size(self); - if unlikely(seq_len == (size_t)-1) - goto err; - if (i_begin < 0) { - i_begin += seq_len; - if unlikely(i_begin < 0) { - if unlikely(seq_len == 0) - goto empty_range; - i_begin = (Dee_ssize_t)do_fix_negative_range_index(i_begin, seq_len); - } - } - if (i_end < 0) { - i_end += seq_len; - if unlikely(i_end < 0) { - if unlikely(seq_len == 0) - goto empty_range; - i_end = (Dee_ssize_t)do_fix_negative_range_index(i_end, seq_len); - } - } - } - return DeeSeq_DelRange(self, - (size_t)i_begin, - (size_t)i_end); -empty_range: - return 0; -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1, 4)) int DCALL -seq_nsi_setrange(DeeObject *self, Dee_ssize_t i_begin, Dee_ssize_t i_end, - DeeObject *values) { - if unlikely(i_begin < 0 || i_end < 0) { - size_t seq_len = DeeObject_Size(self); - if unlikely(seq_len == (size_t)-1) - goto err; - if (i_begin < 0) { - i_begin += seq_len; - if unlikely(i_begin < 0) { - if unlikely(seq_len == 0) - goto empty_range; - i_begin = (Dee_ssize_t)do_fix_negative_range_index(i_begin, seq_len); - } - } - if (i_end < 0) { - i_end += seq_len; - if unlikely(i_end < 0) { - if unlikely(seq_len == 0) - goto empty_range; - i_end = (Dee_ssize_t)do_fix_negative_range_index(i_end, seq_len); - } - } - } -do_setrange: - return DeeSeq_SetRange(self, - (size_t)i_begin, - (size_t)i_end, - values); -empty_range: - i_begin = 0; - i_end = 0; - goto do_setrange; -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -seq_nsi_delrange_n(DeeObject *self, Dee_ssize_t i_begin) { - if unlikely(i_begin < 0) { - size_t seq_len = DeeObject_Size(self); - if unlikely(seq_len == (size_t)-1) - goto err; - i_begin += seq_len; - if unlikely(i_begin < 0) { - if unlikely(seq_len == 0) - goto empty_range; - i_begin = (Dee_ssize_t)do_fix_negative_range_index(i_begin, seq_len); - } - } - return DeeSeq_DelRangeN(self, (size_t)i_begin); -empty_range: - return 0; -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1, 3)) int DCALL -seq_nsi_setrange_n(DeeObject *self, Dee_ssize_t i_begin, - DeeObject *values) { - if unlikely(i_begin < 0) { - size_t seq_len = DeeObject_Size(self); - if unlikely(seq_len == (size_t)-1) - goto err; - i_begin += seq_len; - if unlikely(i_begin < 0) { - if unlikely(seq_len == 0) - goto empty_range; - i_begin = (Dee_ssize_t)do_fix_negative_range_index(i_begin, seq_len); - } - } -do_setrange: - return DeeSeq_SetRangeN(self, (size_t)i_begin, values); -empty_range: - i_begin = 0; - goto do_setrange; -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1, 2, 3)) int DCALL -seq_delrange(DeeObject *self, DeeObject *start, DeeObject *end) { - Dee_ssize_t i_begin, i_end; - if (DeeObject_AsSSize(start, &i_begin)) - goto err; - if (DeeNone_Check(end)) { - if unlikely(i_begin < 0) { - size_t seq_len = DeeObject_Size(self); - if unlikely(seq_len == (size_t)-1) - goto err; - i_begin += seq_len; - if unlikely(i_begin < 0) { - if unlikely(seq_len == 0) - goto empty_range; - i_begin = (Dee_ssize_t)do_fix_negative_range_index(i_begin, seq_len); - } - } - return DeeSeq_DelRangeN(self, (size_t)i_begin); - } - if (DeeObject_AsSSize(end, &i_end)) - goto err; - if unlikely(i_begin < 0 || i_end < 0) { - size_t seq_len = DeeObject_Size(self); - if unlikely(seq_len == (size_t)-1) - goto err; - if (i_begin < 0) { - i_begin += seq_len; - if unlikely(i_begin < 0) { - if unlikely(seq_len == 0) - goto empty_range; - i_begin = (Dee_ssize_t)do_fix_negative_range_index(i_begin, seq_len); - } - } - if (i_end < 0) { - i_end += seq_len; - if unlikely(i_end < 0) { - if unlikely(seq_len == 0) - goto empty_range; - i_end = (Dee_ssize_t)do_fix_negative_range_index(i_end, seq_len); - } - } - } - return DeeSeq_DelRange(self, - (size_t)i_begin, - (size_t)i_end); -empty_range: - return 0; -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1, 2, 3, 4)) int DCALL -seq_setrange(DeeObject *self, DeeObject *start, - DeeObject *end, DeeObject *values) { - Dee_ssize_t start_index, end_index; - if (DeeObject_AsSSize(start, &start_index)) - goto err; - if (DeeNone_Check(end)) - return seq_nsi_setrange_n(self, start_index, values); - if (DeeObject_AsSSize(end, &end_index)) - goto err; - return seq_nsi_setrange(self, start_index, end_index, values); -err: - return -1; -} - - -PRIVATE struct type_nsi tpconst seq_nsi = { - /* .nsi_class = */ TYPE_SEQX_CLASS_SEQ, - /* .nsi_flags = */ TYPE_SEQX_FNORMAL, - { - /* .nsi_seqlike = */ { - /* .nsi_getsize = */ (dfunptr_t)&DeeSeq_Size, - /* .nsi_getsize_fast = */ (dfunptr_t)NULL, - /* .nsi_getitem = */ (dfunptr_t)&DeeSeq_GetItem, - /* .nsi_delitem = */ (dfunptr_t)&DeeSeq_DelItem, - /* .nsi_setitem = */ (dfunptr_t)&DeeSeq_SetItem, - /* .nsi_getitem_fast = */ (dfunptr_t)NULL, - /* .nsi_getrange = */ (dfunptr_t)&seq_nsi_getrange, - /* .nsi_getrange_n = */ (dfunptr_t)&seq_nsi_getrange_n, - /* .nsi_delrange = */ (dfunptr_t)&seq_nsi_delrange, - /* .nsi_delrange_n = */ (dfunptr_t)&seq_nsi_delrange_n, - /* .nsi_setrange = */ (dfunptr_t)&seq_nsi_setrange, - /* .nsi_setrange_n = */ (dfunptr_t)&seq_nsi_setrange_n, - } - } -}; - -PRIVATE WUNUSED NONNULL((1, 2)) int DCALL -seq_delitem(DeeObject *self, DeeObject *index) { - size_t i; - if (DeeObject_AsSize(index, &i)) - goto err; - return DeeSeq_DelItem(self, i); -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1, 2, 3)) int DCALL -seq_setitem(DeeObject *self, DeeObject *index, DeeObject *value) { - size_t i; - if (DeeObject_AsSize(index, &i)) - goto err; - return DeeSeq_SetItem(self, i, value); -err: - return -1; -} - -PRIVATE struct type_seq DeeSeq_OperatorSeq = { - /* .tp_iter = */ &seq_iterself, - /* .tp_sizeob = */ &seq_size, - /* .tp_contains = */ &seq_tpcontains, - /* .tp_getitem = */ &seq_getitem, - /* .tp_delitem = */ &seq_delitem, - /* .tp_setitem = */ &seq_setitem, - /* .tp_getrange = */ &seq_getrange, - /* .tp_delrange = */ &seq_delrange, - /* .tp_setrange = */ &seq_setrange, - /* .tp_nsi = */ &seq_nsi, -}; - -PRIVATE WUNUSED NONNULL((1)) bool DCALL -sequence_should_use_getitem(DeeTypeObject *__restrict self) { - DeeTypeObject *iter, *base; - DeeTypeMRO mro; - int found; - if (self == &DeeSeq_Type) - return false; - if (DeeType_Implements(self, &DeeMapping_Type)) - return false; - iter = self; - found = 0; - DeeTypeMRO_Init(&mro, iter); - do { - struct type_seq *seq; - base = DeeTypeMRO_Next(&mro, iter); - if ((seq = iter->tp_seq) != NULL) { - if (seq->tp_getitem && seq->tp_getitem != &seq_getitem && - (!base || !base->tp_seq || seq->tp_getitem != base->tp_seq->tp_getitem)) - found |= 1; - if (seq->tp_sizeob && seq->tp_sizeob != &seq_size && - (!base || !base->tp_seq || seq->tp_sizeob != base->tp_seq->tp_sizeob)) - found |= 2; - if (found == (1 | 2)) - return true; - if (seq->tp_iter && - (!base || !base->tp_seq || seq->tp_iter != base->tp_seq->tp_iter)) - break; - } - } while (base && (iter = base) != &DeeSeq_Type); - return false; -} - -INTERN WUNUSED NONNULL((1, 2)) Dee_ssize_t DCALL -default_seq_printrepr(DeeObject *__restrict self, dformatprinter printer, void *arg) { -#define DO(err, expr) \ - do { \ - if unlikely((temp = (expr)) < 0) \ - goto err; \ - result += temp; \ - } __WHILE0 - Dee_ssize_t temp, result = 0; - bool is_first; - DREF DeeObject *iterator, *elem; - if (sequence_should_use_getitem(Dee_TYPE(self))) { - size_t i, size; - size = DeeObject_Size(self); - if unlikely(size == (size_t)-1) { - /* If the size operator isn't actually implemented, try to use iterators instead! */ - if (DeeError_Catch(&DeeError_NotImplemented)) - goto do_try_iterators; - goto err_m1; - } - if (!size) { - DO(err, DeeFormat_PRINT(printer, arg, "{ }")); - } else { - DO(err, DeeFormat_PRINT(printer, arg, "{ ")); - for (i = 0; i < size; ++i) { - if (i != 0) - DO(err, DeeFormat_PRINT(printer, arg, ", ")); - elem = DeeObject_GetItemIndex(self, i); - if likely(elem) { - DO(err_elem, DeeFormat_PrintObjectRepr(printer, arg, elem)); - Dee_Decref(elem); - } else if (DeeError_Catch(&DeeError_UnboundItem)) { - DO(err, DeeFormat_PRINT(printer, arg, "")); - } else if (DeeError_Catch(&DeeError_IndexError)) { - /* Assume that the Sequence got re-sized while we were iterating it. */ - if unlikely(!i) { - DO(err, DeeFormat_PRINT(printer, arg, "}")); - goto done; - } - break; - } else { - goto err_m1; - } - } - DO(err, DeeFormat_PRINT(printer, arg, " }")); - } - } else { -do_try_iterators: - iterator = DeeObject_Iter(self); - if unlikely(!iterator) - goto err_m1; - DO(err_iterator, DeeFormat_PRINT(printer, arg, "{ ")); - is_first = true; - while (ITER_ISOK(elem = DeeObject_IterNext(iterator))) { - if (!is_first) - DO(err_iterator_elem, DeeFormat_PRINT(printer, arg, ", ")); - DO(err_iterator_elem, DeeFormat_PrintObjectRepr(printer, arg, elem)); - Dee_Decref(elem); - is_first = false; - if (DeeThread_CheckInterrupt()) - goto err_m1_iterator; - } - if unlikely(!elem) - goto err_m1_iterator; - Dee_Decref(iterator); - if (is_first) { - DO(err, DeeFormat_PRINT(printer, arg, "}")); - } else { - DO(err, DeeFormat_PRINT(printer, arg, " }")); - } - } -done: - return result; -err_elem: - Dee_Decref(elem); - goto err; -err_iterator_elem: - Dee_Decref(elem); -err_iterator: - Dee_Decref(iterator); -err: - return temp; -err_m1_iterator: - temp = -1; - goto err_iterator; -err_m1: - temp = -1; - goto err; -#undef DO -} - -PRIVATE WUNUSED NONNULL((1, 2)) int DCALL -DeeSeq_Eq(DeeObject *lhs, DeeObject *rhs) { - int result; - DREF DeeObject *lhs_iter; - size_t lhs_size; - if ((lhs_size = DeeFastSeq_GetSize_deprecated(lhs)) != DEE_FASTSEQ_NOTFAST_DEPRECATED) - return DeeSeq_EqFS(lhs, lhs_size, rhs); - lhs_iter = DeeObject_Iter(lhs); - if unlikely(!lhs_iter) - return -1; - result = DeeSeq_EqIS(lhs_iter, rhs); - Dee_Decref(lhs_iter); - return result; -} - -PRIVATE WUNUSED NONNULL((1, 2)) int DCALL -DeeSeq_Compare(DeeObject *lhs, DeeObject *rhs) { - int result; - DREF DeeObject *lhs_iter; - size_t lhs_size; - if ((lhs_size = DeeFastSeq_GetSize_deprecated(lhs)) != DEE_FASTSEQ_NOTFAST_DEPRECATED) - return DeeSeq_CompareFS(lhs, lhs_size, rhs); - lhs_iter = DeeObject_Iter(lhs); - if unlikely(!lhs_iter) - return Dee_COMPARE_ERR; - result = DeeSeq_CompareIS(lhs_iter, rhs); - Dee_Decref(lhs_iter); - return result; -} - -PRIVATE WUNUSED NONNULL((1)) Dee_hash_t DCALL -seq_hash(DeeObject *__restrict self) { - Dee_hash_t result; - DREF DeeObject *iter, *elem; - iter = DeeObject_Iter(self); - if unlikely(!iter) - goto err; - elem = DeeObject_IterNext(iter); - if (!ITER_ISOK(elem)) { - Dee_Decref(iter); - if (!elem) - goto err; - return 0; /* Empty sequence hash */ - } - result = DeeObject_Hash(elem); - Dee_Decref(elem); - while (ITER_ISOK(elem = DeeObject_IterNext(iter))) { - result = Dee_HashCombine(result, DeeObject_Hash(elem)); - Dee_Decref(elem); - } - Dee_Decref(iter); - if unlikely(!elem) - goto err; - return result; -err: - DeeError_Print("Unhandled error in `Set.operator hash'\n", - ERROR_PRINT_DOHANDLE); - return DeeObject_HashGeneric(self); -} - -PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -seq_eq(DeeObject *self, DeeObject *other) { - int result = DeeSeq_Eq(self, other); - if unlikely(result < 0) - goto err; - return_bool_(result); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -seq_ne(DeeObject *self, DeeObject *other) { - int result = DeeSeq_Eq(self, other); - if unlikely(result < 0) - goto err; - return_bool_(!result); -err: - return NULL; -} - -INTERN WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -seq_lo(DeeObject *self, DeeObject *other) { - int result = DeeSeq_Compare(self, other); - if unlikely(result == Dee_COMPARE_ERR) - goto err; - return_bool_(result < 0); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -seq_le(DeeObject *self, DeeObject *other) { - int result = DeeSeq_Compare(self, other); - if unlikely(result == Dee_COMPARE_ERR) - goto err; - return_bool_(result <= 0); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -seq_gr(DeeObject *self, DeeObject *other) { - int result = DeeSeq_Compare(self, other); - if unlikely(result == Dee_COMPARE_ERR) - goto err; - return_bool_(result > 0); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -seq_ge(DeeObject *self, DeeObject *other) { - int result = DeeSeq_Compare(self, other); - if unlikely(result == Dee_COMPARE_ERR) - goto err; - return_bool_(result >= 0); -err: - return NULL; -} - -PRIVATE struct type_cmp DeeSeq_OperatorCmp = { - /* .tp_hash = */ &seq_hash, - /* .tp_compare_eq = */ NULL, - /* .tp_compare = */ NULL, - /* .tp_trycompare_eq = */ NULL, - /* .tp_eq = */ &seq_eq, - /* .tp_ne = */ &seq_ne, - /* .tp_lo = */ &seq_lo, - /* .tp_le = */ &seq_le, - /* .tp_gr = */ &seq_gr, - /* .tp_ge = */ &seq_ge -}; - -#else /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - PRIVATE WUNUSED NONNULL((1)) DREF DeeTypeObject *DCALL seqtype_get_Iterator(DeeTypeObject *__restrict self) { DeeTypeObject *result = &DeeIterator_Type; @@ -2318,7 +265,6 @@ default_seq_printrepr(DeeObject *__restrict self, Dee_formatprinter_t printer, v return temp; #undef DO } -#endif /* CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL @@ -2367,7 +313,6 @@ PRIVATE struct type_math seq_math = { /* .tp_pow = */ NULL, /* .tp_inc = */ NULL, /* .tp_dec = */ NULL, -#ifdef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS /* .tp_inplace_add = */ &DeeSeq_OperatorInplaceAdd, /* .tp_inplace_sub = */ NULL, /* .tp_inplace_mul = */ &DeeSeq_OperatorInplaceMul, @@ -2379,19 +324,6 @@ PRIVATE struct type_math seq_math = { /* .tp_inplace_or = */ NULL, /* .tp_inplace_xor = */ NULL, /* .tp_inplace_pow = */ NULL, -#else /* CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - /* .tp_inplace_add = */ &DeeSeq_InplaceExtend, - /* .tp_inplace_sub = */ NULL, - /* .tp_inplace_mul = */ &DeeSeq_InplaceRepeat, - /* .tp_inplace_div = */ NULL, - /* .tp_inplace_mod = */ NULL, - /* .tp_inplace_shl = */ NULL, - /* .tp_inplace_shr = */ NULL, - /* .tp_inplace_and = */ NULL, - /* .tp_inplace_or = */ NULL, - /* .tp_inplace_xor = */ NULL, - /* .tp_inplace_pow = */ NULL, -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ }; @@ -2566,262 +498,6 @@ seq_map(DeeObject *self, size_t argc, DeeObject *const *argv) { return NULL; } -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS -struct sequence_find_data { - DeeObject *sfd_elem; /* [1..1] The element to find */ - size_t sfd_start; /* Search start index */ - size_t sfd_end; /* Search end index */ - DeeObject *sfd_key; /* [0..1] Search key */ -}; - -#ifdef __OPTIMIZE_SIZE__ -#define get_sequence_find_args(name, argc, argv, result) \ - get_sequence_find_args_kw(name, argc, argv, NULL, result) -#else /* __OPTIMIZE_SIZE__ */ -PRIVATE WUNUSED NONNULL((1, 4)) int DCALL -get_sequence_find_args(char const *__restrict name, - size_t argc, DeeObject *const *argv, - struct sequence_find_data *__restrict result) { - switch (argc) { - - case 1: - result->sfd_elem = argv[0]; - result->sfd_key = NULL; - result->sfd_start = 0; - result->sfd_end = (size_t)-1; - break; - - case 2: - if (DeeInt_Check(argv[1])) { - if (DeeObject_AsSSize(argv[1], (Dee_ssize_t *)&result->sfd_start)) - goto err; - result->sfd_key = NULL; - } else { - result->sfd_key = argv[1]; - result->sfd_start = 0; - if (DeeNone_Check(result->sfd_key)) - result->sfd_key = NULL; - } - result->sfd_elem = argv[0]; - result->sfd_end = (size_t)-1; - break; - - case 3: - if (DeeObject_AsSSize(argv[1], (Dee_ssize_t *)&result->sfd_start)) - goto err; - if (DeeInt_Check(argv[2])) { - if (DeeObject_AsSSize(argv[2], (Dee_ssize_t *)&result->sfd_end)) - goto err; - result->sfd_key = NULL; - } else { - result->sfd_key = argv[2]; - result->sfd_end = (size_t)-1; - if (DeeNone_Check(result->sfd_key)) - result->sfd_key = NULL; - } - result->sfd_elem = argv[0]; - break; - - case 4: - if (DeeObject_AsSSize(argv[1], (Dee_ssize_t *)&result->sfd_start)) - goto err; - if (DeeObject_AsSSize(argv[2], (Dee_ssize_t *)&result->sfd_end)) - goto err; - result->sfd_elem = argv[0]; - result->sfd_key = argv[3]; - if (DeeNone_Check(result->sfd_key)) - result->sfd_key = NULL; - break; - - default: - err_invalid_argc(name, argc, 1, 4); -err: - return -1; - } - return 0; -} -#endif /* !__OPTIMIZE_SIZE__ */ - -/*[[[deemon -import define_Dee_HashStr from rt.gen.hash; -print define_Dee_HashStr("elem"); -print define_Dee_HashStr("start"); -print define_Dee_HashStr("end"); -print define_Dee_HashStr("key"); -print define_Dee_HashStr("defl"); -]]]*/ -#define Dee_HashStr__elem _Dee_HashSelectC(0x1aacf22d, 0x705652c4aed9308a) -#define Dee_HashStr__start _Dee_HashSelectC(0xa2ed6890, 0x80b621ce3c3982d5) -#define Dee_HashStr__end _Dee_HashSelectC(0x37fb4a05, 0x6de935c204dc3d01) -#define Dee_HashStr__key _Dee_HashSelectC(0xe29c6a44, 0x612dd31212e90587) -#define Dee_HashStr__defl _Dee_HashSelectC(0x4353f18, 0x655c26b85fe0c07b) -/*[[[end]]]*/ - -/* (elem,key:?DCallable=!N) - * (elem,start:?Dint,key:?DCallable=!N) - * (elem,start:?Dint,end:?Dint,key:?DCallable=!N) */ -PRIVATE WUNUSED NONNULL((1, 5)) int DCALL -get_sequence_find_args_kw(char const *__restrict name, - size_t argc, DeeObject *const *argv, DeeObject *kw, - struct sequence_find_data *__restrict result) { - DREF DeeObject *temp; - DeeKwArgs kwargs; -#ifndef __OPTIMIZE_SIZE__ - if (!kw) /* Fastpass */ - return get_sequence_find_args(name, argc, argv, result); -#endif /* !__OPTIMIZE_SIZE__ */ - if (DeeKwArgs_Init(&kwargs, &argc, argv, kw)) - goto err; - switch (argc) { - - case 0: - if unlikely((result->sfd_elem = DeeKwArgs_GetItemNRStringHash(&kwargs, "elem", Dee_HashStr__elem)) == NULL) - goto err; -check_kw_start_end_key: - if unlikely((temp = DeeKwArgs_GetItemNRStringHashDef(&kwargs, "start", Dee_HashStr__start, DeeInt_Zero)) == NULL) - goto err; - if (DeeObject_AsSSize(temp, (Dee_ssize_t *)&result->sfd_start)) - goto err; -check_kw_end_key: - if unlikely((temp = DeeKwArgs_GetItemNRStringHashDef(&kwargs, "end", Dee_HashStr__end, DeeInt_MinusOne)) == NULL) - goto err; - if (DeeObject_AsSSize(temp, (Dee_ssize_t *)&result->sfd_end)) - goto err; -/*check_kw_key:*/ - if unlikely((result->sfd_key = DeeKwArgs_GetItemNRStringHashDef(&kwargs, "key", Dee_HashStr__key, Dee_None)) == NULL) - goto err; - if (DeeNone_Check(result->sfd_key)) - result->sfd_key = NULL; - break; - - case 1: - result->sfd_elem = argv[0]; - goto check_kw_start_end_key; - - case 2: - result->sfd_elem = argv[0]; - if (DeeInt_Check(argv[1])) { - if (DeeObject_AsSSize(argv[1], (Dee_ssize_t *)&result->sfd_start)) - goto err; - goto check_kw_end_key; - } - result->sfd_key = argv[1]; - result->sfd_start = 0; - if (DeeNone_Check(result->sfd_key)) - result->sfd_key = NULL; -check_kw_end: - if unlikely((temp = DeeKwArgs_GetItemNRStringHashDef(&kwargs, "end", Dee_HashStr__end, DeeInt_MinusOne)) == NULL) - goto err; - if (DeeObject_AsSSize(temp, (Dee_ssize_t *)&result->sfd_end)) - goto err; - break; - - case 3: - result->sfd_elem = argv[0]; - if (DeeObject_AsSSize(argv[1], (Dee_ssize_t *)&result->sfd_start)) - goto err; - if (DeeInt_Check(argv[2])) { - if (DeeObject_AsSSize(argv[2], (Dee_ssize_t *)&result->sfd_end)) - goto err; - goto check_kw_end_key; - } - result->sfd_key = argv[2]; - if (DeeNone_Check(result->sfd_key)) - result->sfd_key = NULL; - goto check_kw_end; - - case 4: - if (DeeObject_AsSSize(argv[1], (Dee_ssize_t *)&result->sfd_start)) - goto err; - if (DeeObject_AsSSize(argv[2], (Dee_ssize_t *)&result->sfd_end)) - goto err; - result->sfd_elem = argv[0]; - result->sfd_key = argv[3]; - if (DeeNone_Check(result->sfd_key)) - result->sfd_key = NULL; - break; - - default: - err_invalid_argc(name, argc, 1, 4); - goto err; - } - return DeeKwArgs_Done(&kwargs, argc, name); -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seq_reversed(DeeObject *self, size_t argc, DeeObject *const *argv, DeeObject *kw) { - DREF DeeObject *result; - size_t start = 0, end = (size_t)-1; - if (DeeArg_UnpackKw(argc, argv, kw, kwlist__start_end, - "|" UNPuSIZ UNPuSIZ ":reversed", - &start, &end)) - goto err; - result = DeeList_FromSequence(self); - if unlikely(!result) - goto err; - if (end > DeeList_SIZE((DeeListObject *)result)) - end = DeeList_SIZE((DeeListObject *)result); - if (start > end) - start = end; - if (start > 0 || end < DeeList_SIZE((DeeListObject *)result)) { - /* Super hacky, but OK since this code will be removed anyways */ - DREF DeeObject *result2; - result2 = DeeObject_GetRangeIndex(result, (Dee_ssize_t)start, (Dee_ssize_t)end); - Dee_Decref(result); - if unlikely(!result2) - goto err; - result = DeeList_FromSequence(result2); - Dee_Decref(result2); - if unlikely(!result) - goto err; - } - DeeList_Reverse(result, 0, (size_t)-1); - return result; -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seq_sorted(DeeObject *self, size_t argc, - DeeObject *const *argv, DeeObject *kw) { - DREF DeeObject *result; - size_t start = 0, end = (size_t)-1; - DeeObject *key = Dee_None; - if (DeeArg_UnpackKw(argc, argv, kw, kwlist__start_end_key, - "|" UNPuSIZ UNPuSIZ "o:sorted", - &start, &end, &key)) - goto err; - result = DeeList_FromSequence(self); - if unlikely(!result) - goto err; - if (end > DeeList_SIZE((DeeListObject *)result)) - end = DeeList_SIZE((DeeListObject *)result); - if (start > end) - start = end; - if (start > 0 || end < DeeList_SIZE((DeeListObject *)result)) { - /* Super hacky, but OK since this code will be removed anyways */ - DREF DeeObject *result2; - result2 = DeeObject_GetRangeIndex(result, (Dee_ssize_t)start, (Dee_ssize_t)end); - Dee_Decref(result); - if unlikely(!result2) - goto err; - result = DeeList_FromSequence(result2); - Dee_Decref(result2); - if unlikely(!result) - goto err; - } - if unlikely(DeeList_Sort(result, 0, (size_t)-1, key)) - goto err_r; - return result; -err_r: - Dee_Decref(result); -err: - return NULL; -} -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL seq_segments(DeeObject *self, size_t argc, DeeObject *const *argv) { size_t segsize; @@ -2893,313 +569,6 @@ seq_permutations(DeeObject *self, size_t argc, DeeObject *const *argv) { } -/* Mutable-Sequence functions */ -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seq_insert(DeeObject *self, size_t argc, - DeeObject *const *argv, DeeObject *kw) { - size_t index; - DeeObject *item; - if (DeeArg_UnpackKw(argc, argv, kw, kwlist__index_item, UNPuSIZ "o:insert", &index, &item)) - goto err; - if (DeeSeq_Insert(self, index, item)) - goto err; - return_none; -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seq_insertall(DeeObject *self, size_t argc, - DeeObject *const *argv, DeeObject *kw) { - size_t index; - DeeObject *items; - if (DeeArg_UnpackKw(argc, argv, kw, kwlist__index_items, - UNPuSIZ "o:insertall", &index, &items)) - goto err; - if (DeeSeq_InsertAll(self, index, items)) - goto err; - return_none; -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seq_erase(DeeObject *self, size_t argc, - DeeObject *const *argv, DeeObject *kw) { - size_t index, count = 1; - if (DeeArg_UnpackKw(argc, argv, kw, kwlist__index_count, - UNPuSIZ "|" UNPuSIZ ":erase", - &index, &count)) - goto err; - if unlikely(DeeSeq_Erase(self, index, count) == (size_t)-1) - goto err; - return_none; -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seq_xchitem(DeeObject *self, size_t argc, - DeeObject *const *argv, DeeObject *kw) { - size_t index; - DeeObject *value; - if (DeeArg_UnpackKw(argc, argv, kw, kwlist__index_value, - UNPuSIZ "o:xch", &index, &value)) - goto err; - return DeeSeq_XchItem(self, index, value); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seq_pop(DeeObject *self, size_t argc, - DeeObject *const *argv, DeeObject *kw) { - Dee_ssize_t index = -1; - if (DeeArg_UnpackKw(argc, argv, kw, kwlist__index, "|" UNPdSIZ ":pop", &index)) - goto err; - return DeeSeq_PopItem(self, index); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seq_popfront(DeeObject *self, size_t argc, DeeObject *const *argv) { - DeeObject *args[1], *result; - if (DeeArg_Unpack(argc, argv, ":popfront")) - goto err; - args[0] = DeeInt_Zero; - result = DeeObject_CallAttr(self, (DeeObject *)&str_pop, 1, args); - if unlikely(!result && DeeError_Catch(&DeeError_IndexError)) - err_empty_sequence(self); - return result; -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seq_popback(DeeObject *self, size_t argc, DeeObject *const *argv) { - DeeObject *args[1], *result; - if (DeeArg_Unpack(argc, argv, ":popback")) - goto err; - args[0] = DeeInt_MinusOne; - result = DeeObject_CallAttr(self, (DeeObject *)&str_pop, 1, args); - if unlikely(!result && DeeError_Catch(&DeeError_IndexError)) - err_empty_sequence(self); - return result; -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seq_pushfront(DeeObject *self, size_t argc, DeeObject *const *argv) { - DeeObject *args[2]; - if (DeeArg_Unpack(argc, argv, "o:pushfront", &args[1])) - goto err; - args[0] = DeeInt_Zero; - return DeeObject_CallAttr(self, (DeeObject *)&str_insert, 2, args); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seq_append(DeeObject *self, size_t argc, DeeObject *const *argv) { - DeeObject *obj; - if (DeeArg_Unpack(argc, argv, "o:append", &obj)) - goto err; - if (DeeSeq_Append(self, obj)) - goto err; - return_none; -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seq_extend(DeeObject *self, size_t argc, DeeObject *const *argv) { - DeeObject *values; - if (DeeArg_Unpack(argc, argv, "o:extend", &values)) - goto err; - if (DeeSeq_Extend(self, values)) - goto err; - return_none; -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seq_remove(DeeObject *self, size_t argc, DeeObject *const *argv, DeeObject *kw) { - int result; - struct sequence_find_data data; - if (get_sequence_find_args_kw(STR_remove, argc, argv, kw, &data)) - goto err; - result = DeeSeq_Remove(self, data.sfd_start, data.sfd_end, data.sfd_elem, data.sfd_key); - if unlikely(result < 0) - goto err; - return_bool_(result); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seq_rremove(DeeObject *self, size_t argc, DeeObject *const *argv, DeeObject *kw) { - int result; - struct sequence_find_data data; - if (get_sequence_find_args_kw(STR_rremove, argc, argv, kw, &data)) - goto err; - result = DeeSeq_RRemove(self, data.sfd_start, data.sfd_end, data.sfd_elem, data.sfd_key); - if unlikely(result < 0) - goto err; - return_bool_(result); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seq_removeall(DeeObject *self, size_t argc, DeeObject *const *argv, DeeObject *kw) { - size_t result; - struct sequence_find_data data; - if (get_sequence_find_args_kw(STR_removeall, argc, argv, kw, &data)) - goto err; - result = DeeSeq_RemoveAll(self, data.sfd_start, data.sfd_end, data.sfd_elem, data.sfd_key); - if unlikely(result == (size_t)-1) - goto err; - return DeeInt_NewSize(result); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seq_removeif(DeeObject *self, size_t argc, - DeeObject *const *argv, DeeObject *kw) { - DeeObject *should; - size_t result, start = 0, end = (size_t)-1, max = (size_t)-1; - if (DeeArg_UnpackKw(argc, argv, kw, kwlist__should_start_end_max, - "o|" UNPdSIZ UNPdSIZ UNPdSIZ ":removeif", - &should, &start, &end, &max)) - goto err; - (void)max; /* Only supported in new API */ - result = DeeSeq_RemoveIf(self, start, end, should); - if unlikely(result == (size_t)-1) - goto err; - return DeeInt_NewSize(result); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seq_clear(DeeObject *self, size_t argc, DeeObject *const *argv) { - if (DeeArg_Unpack(argc, argv, ":clear")) - goto err; - if (DeeObject_SetRange(self, Dee_None, Dee_None, Dee_None)) - goto err; - return_none; -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seq_fill(DeeObject *self, size_t argc, - DeeObject *const *argv, DeeObject *kw) { - size_t start = 0, end = (size_t)-1, result; - DeeObject *filler = Dee_None; - if (DeeArg_UnpackKw(argc, argv, kw, kwlist__start_end_filler, - "|" UNPdSIZ UNPdSIZ "o:fill", - &start, &end, &filler)) - goto err; - result = DeeSeq_Fill(self, start, end, filler); - if unlikely(result == (size_t)-1) - goto err; - return DeeInt_NewSize(result); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seq_resize(DeeObject *self, size_t argc, - DeeObject *const *argv, DeeObject *kw) { - DREF DeeObject *result; - size_t size, oldsize; - DeeObject *filler = Dee_None; - if (DeeArg_UnpackKw(argc, argv, kw, kwlist__size_filler, - UNPuSIZ "|o:resize", &size, &filler)) - goto err; - if (!size) { - result = DeeObject_CallAttr(self, (DeeObject *)&str_clear, 0, NULL); - } else { - oldsize = DeeObject_Size(self); - if unlikely(oldsize == (size_t)-1) - goto err; - if (size < oldsize) { - result = DeeObject_CallAttrf(self, (DeeObject *)&str_erase, - PCKuSIZ "o", size, DeeInt_MinusOne); - } else if (size > oldsize) { - DREF DeeObject *seq_extension; - seq_extension = DeeSeq_RepeatItem(filler, size - oldsize); - if unlikely(!seq_extension) - goto err; - result = DeeObject_CallAttr(self, (DeeObject *)&str_extend, 1, &seq_extension); - Dee_Decref(seq_extension); - } else { - goto do_return_none; - } - } - if unlikely(result != Dee_None) { - if unlikely(!result) - goto err; - Dee_Decref(result); -do_return_none: - result = Dee_None; - Dee_Incref(Dee_None); - } - return result; -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seq_reverse(DeeObject *self, size_t argc, DeeObject *const *argv, DeeObject *kw) { - DREF DeeObject *reversed; - (void)kw; - if (DeeArg_Unpack(argc, argv, ":reverse")) - goto err; - reversed = DeeList_FromSequence(self); - if unlikely(!reversed) - goto err; - DeeList_Reverse(reversed, 0, (size_t)-1); - if unlikely(DeeObject_Assign(self, reversed)) - goto err_reversed; - Dee_Decref(reversed); - return_none; -err_reversed: - Dee_Decref(reversed); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -seq_sort(DeeObject *self, size_t argc, - DeeObject *const *argv, DeeObject *kw) { - DREF DeeObject *sorted; - DeeObject *key = Dee_None; - if (DeeArg_UnpackKw(argc, argv, kw, kwlist__key, "|o:sort", &key)) - goto err; - sorted = DeeList_FromSequence(self); - if unlikely(!sorted) - goto err; - if unlikely(DeeList_Sort(sorted, 0, (size_t)-1, key)) - goto err_sorted; - if unlikely(DeeObject_Assign(self, sorted)) - goto err_sorted; - Dee_Decref(sorted); - return_none; -err_sorted: - Dee_Decref(sorted); -err: - return NULL; -} -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - - PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL seq_index(DeeObject *self, size_t argc, DeeObject *const *argv, DeeObject *kw) { DeeObject *item, *key = Dee_None; @@ -3381,11 +750,7 @@ seq_blocateall(DeeObject *self, size_t argc, DeeObject *const *argv, DeeObject * ? DeeSeq_InvokeBRangeWithKey(self, item, start, end, key, result_range) : DeeSeq_InvokeBRange(self, item, start, end, result_range)) goto err; -#ifdef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS return DeeSeq_OperatorGetRangeIndex(self, result_range[0], result_range[1]); -#else /* CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - return DeeSeq_GetRange(self, result_range[0], result_range[1]); -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ err: return NULL; } @@ -3410,31 +775,6 @@ seq_binsert(DeeObject *self, size_t argc, DeeObject *const *argv, DeeObject *kw) -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS -#define default_seq_erase seq_erase -#define default_seq_insert seq_insert -#define default_seq_insertall seq_insertall -#define default_seq_pushfront seq_pushfront -#define default_seq_append seq_append -#define default_seq_extend seq_extend -#define default_seq_xchitem seq_xchitem -#define default_seq_clear seq_clear -#define default_seq_pop seq_pop -#define default_seq_popfront seq_popfront -#define default_seq_popback seq_popback -#define default_seq_remove seq_remove -#define default_seq_rremove seq_rremove -#define default_seq_removeall seq_removeall -#define default_seq_removeif seq_removeif -#define default_seq_resize seq_resize -#define default_seq_fill seq_fill -#define default_seq_reverse seq_reverse -#define default_seq_reversed seq_reversed -#define default_seq_sort seq_sort -#define default_seq_sorted seq_sorted -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - - #define DOC_throws_ValueError_if_empty \ "#tValueError{The specified range is empty}" #define DOC_throws_ValueError_if_not_found \ @@ -4465,7 +1805,6 @@ INTERN_TPCONST struct type_method tpconst seq_methods[] = { * * TODO: The compiler should also be able to automatically * optimize the second version into the first. */ -#ifdef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS TYPE_METHOD("__bool__", &default_seq___bool__, "->?Dbool\n" "Alias for ${!!(this as Sequence)}"), @@ -4614,7 +1953,6 @@ INTERN_TPCONST struct type_method tpconst seq_methods[] = { TYPE_METHOD("__inplace_mul__", &default_seq___inplace_mul__, "(factor:?Dint)->?.\n" "Alias for ${(this as Sequence) *= factor}"), -#endif /* CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ /* Old function names/deprecated functions. */ TYPE_METHOD("transform", &seq_map, @@ -5620,11 +2958,7 @@ PUBLIC DeeTypeObject DeeSeq_Type = { "#B{²}: Default implementation provided if sub-class matches requirements"), /* .tp_flags = */ TP_FNORMAL | TP_FABSTRACT | TP_FNAMEOBJECT, /* Generic base class type. */ /* .tp_weakrefs = */ 0, -#ifdef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS /* .tp_features = */ TF_NONE | (Dee_SEQCLASS_SEQ << Dee_TF_SEQCLASS_SHFT), -#else /* CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - /* .tp_features = */ TF_NONE, -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ /* .tp_base = */ &DeeObject_Type, /* .tp_init = */ { { diff --git a/src/deemon/objects/seq/default-api.c b/src/deemon/objects/seq/default-api.c index f8da225ef..b4b1c2152 100644 --- a/src/deemon/objects/seq/default-api.c +++ b/src/deemon/objects/seq/default-api.c @@ -43,10 +43,6 @@ /**/ #include "default-enumerate.h" -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS -#include "../seq_functions.h" -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - DECL_BEGIN PRIVATE ATTR_PURE WUNUSED NONNULL((1, 2)) Dee_tsc_seq_trygetfirst_t DCALL DeeType_RequireSeqTryGetFirst_private_uncached(DeeTypeObject *orig_type, DeeTypeObject *self); diff --git a/src/deemon/objects/seq/default-sets.c b/src/deemon/objects/seq/default-sets.c index aa965e82e..8a10a71b2 100644 --- a/src/deemon/objects/seq/default-sets.c +++ b/src/deemon/objects/seq/default-sets.c @@ -153,21 +153,8 @@ invset_contains(SetInversion *self, DeeObject *key) { return NULL; } -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -invset_iterself(SetInversion *__restrict self) { - /* Sorry, but it's impossible to enumerate a set containing (almost) everything */ - err_unimplemented_operator(Dee_TYPE(self), OPERATOR_ITER); - return NULL; -} -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - PRIVATE struct type_seq invset_seq = { -#ifdef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS /* .tp_iter = */ NULL, -#else /* CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - /* .tp_iter = */ (DREF DeeObject *(DCALL *)(DeeObject *__restrict))&invset_iterself, -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ /* .tp_sizeob = */ NULL, /* .tp_contains = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))&invset_contains, /* .tp_getitem = */ NULL, @@ -1793,188 +1780,6 @@ INTERN DeeTypeObject SetDifference_Type = { - - - - -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS -INTERN WUNUSED NONNULL((1)) DREF DeeObject *DCALL -DeeSet_Invert(DeeObject *__restrict self) { - DREF SetInversion *result; - /* Just re-return the original set. */ - if (SetInversion_CheckExact(self)) - return_reference(SetInversion_GetSet(self)); - - /* Construct a new inverse-set wrapper. */ - result = DeeObject_MALLOC(SetInversion); - if unlikely(!result) - goto done; - DeeObject_Init(result, &SetInversion_Type); - result->si_set = self; - Dee_Incref(self); -done: - return (DREF DeeObject *)result; -} - - -INTERN WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -DeeSet_OperatorAdd(DeeObject *lhs, DeeObject *rhs) { - DREF DeeObject *result, *temp; - if (SetInversion_CheckExact(lhs)) { - SetInversion *lhs_inv = (SetInversion *)lhs; - if (SetInversion_CheckExact(rhs)) { - SetInversion *rhs_inv = (SetInversion *)rhs; - - /* Special case: `~a | ~b' --> `~(a & b)' */ - temp = DeeSet_OperatorAnd(lhs_inv->si_set, - rhs_inv->si_set); - if unlikely(!temp) - goto err; - result = DeeSet_Invert(temp); - Dee_Decref(temp); - goto done; - } - - /* Special case: `~a | b' --> `~(a & ~b)' */ - if unlikely((rhs = DeeSet_Invert(rhs)) == NULL) - goto err; - temp = DeeSet_OperatorAnd(lhs_inv->si_set, rhs); - Dee_Decref(rhs); - if unlikely(!temp) - goto err; - result = DeeSet_Invert(temp); - Dee_Decref(temp); - goto done; - } - if (SetInversion_CheckExact(rhs)) { - /* Special case: `a | ~b' --> `~(b & ~a)' */ - if unlikely((lhs = DeeSet_Invert(lhs)) == NULL) - goto err; - temp = DeeSet_OperatorAnd(SetInversion_GetSet(rhs), lhs); - Dee_Decref(lhs); - if unlikely(!temp) - goto err; - result = DeeSet_Invert(temp); - Dee_Decref(temp); - goto done; - } - if (DeeSet_CheckEmpty(lhs)) - return_reference_(rhs); - if (DeeSet_CheckEmpty(rhs)) - return_reference_(lhs); - - /* Construct a set-union wrapper. */ - result = (DREF DeeObject *)DeeObject_MALLOC(SetUnion); - if unlikely(!result) - goto done; - ((SetUnion *)result)->su_a = lhs; - ((SetUnion *)result)->su_b = rhs; - Dee_Incref(lhs); - Dee_Incref(rhs); - DeeObject_Init(result, &SetUnion_Type); -done: - return result; -err: - return NULL; -} - -INTERN WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -DeeSet_OperatorAnd(DeeObject *lhs, DeeObject *rhs) { - DREF DeeObject *result, *temp; - if (SetInversion_CheckExact(lhs)) { - if (SetInversion_CheckExact(rhs)) { - /* Special case: `~a & ~b' -> `~(a | b)' */ - temp = DeeSet_OperatorAnd(SetInversion_GetSet(lhs), - SetInversion_GetSet(rhs)); - if unlikely(!temp) - goto err; - result = DeeSet_Invert(temp); - Dee_Decref(temp); - goto done; - } - - /* Special case: `~a & b' -> `b & ~a' */ - temp = lhs; - lhs = rhs; - rhs = temp; - } - if (DeeSet_CheckEmpty(lhs) || DeeSet_CheckEmpty(rhs)) - return_reference_(Dee_EmptySet); - - /* Construct a set-intersection wrapper. */ - result = (DREF DeeObject *)DeeObject_MALLOC(SetIntersection); - if unlikely(!result) - goto done; - ASSERT(!SetInversion_CheckExact(lhs)); - ((SetIntersection *)result)->si_a = lhs; - ((SetIntersection *)result)->si_b = rhs; - Dee_Incref(lhs); - Dee_Incref(rhs); - DeeObject_Init(result, &SetIntersection_Type); -done: - return result; -err: - return NULL; -} - -INTERN WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -DeeSet_OperatorSub(DeeObject *lhs, DeeObject *rhs) { - DREF DeeObject *result, *temp; - if (SetInversion_CheckExact(lhs)) { - /* Special case: `~a - b' -> `~(a | b)' */ - temp = DeeSet_OperatorAdd(SetInversion_GetSet(lhs), rhs); - if unlikely(!temp) - goto err; - result = DeeSet_Invert(temp); - Dee_Decref(temp); - goto done; - } - if (DeeSet_CheckEmpty(lhs)) - return_reference_(Dee_EmptySet); - if (DeeSet_CheckEmpty(rhs)) - return_reference_(lhs); - - /* Construct a set-difference wrapper. */ - result = (DREF DeeObject *)DeeObject_MALLOC(SetDifference); - if unlikely(!result) - goto done; - ASSERT(!SetInversion_CheckExact(lhs)); - ((SetDifference *)result)->sd_a = lhs; - ((SetDifference *)result)->sd_b = rhs; - Dee_Incref(lhs); - Dee_Incref(rhs); - DeeObject_Init(result, &SetDifference_Type); -done: - return result; -err: - return NULL; -} - -INTERN WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -DeeSet_OperatorXor(DeeObject *lhs, DeeObject *rhs) { - DREF SetSymmetricDifference *result; - if (DeeSet_CheckEmpty(lhs)) - return_reference_(rhs); - if (DeeSet_CheckEmpty(rhs)) - return_reference_(lhs); - - /* Construct a set-symmetric-difference wrapper. */ - result = DeeObject_MALLOC(SetSymmetricDifference); - if unlikely(!result) - goto done; - ASSERT(!SetInversion_CheckExact(lhs)); - result->ssd_a = lhs; - result->ssd_b = rhs; - Dee_Incref(lhs); - Dee_Incref(rhs); - DeeObject_Init(result, &SetSymmetricDifference_Type); -done: - return (DREF DeeObject *)result; -} -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - - - #define SET_CONTAINSANY_FOREACH_LHS__FOUND SSIZE_MIN PRIVATE WUNUSED NONNULL((2)) Dee_ssize_t DCALL set_containsany_foreach_lhs_cb(void *arg, DeeObject *key) { diff --git a/src/deemon/objects/seq/fastseq.c b/src/deemon/objects/seq/fastseq.c index fe4da6648..dc997aaf9 100644 --- a/src/deemon/objects/seq/fastseq.c +++ b/src/deemon/objects/seq/fastseq.c @@ -38,7 +38,6 @@ #include "../../runtime/runtime_error.h" #include "range.h" -#include "subrange.h" #include "svec.h" DECL_BEGIN @@ -208,7 +207,6 @@ foreach_seq_as_heap_vector_cb(void *arg, DeeObject *__restrict elem) { PUBLIC WUNUSED NONNULL((1, 2)) /*owned(Dee_Free)*/ DREF DeeObject **DCALL DeeSeq_AsHeapVector(DeeObject *__restrict self, /*[out]*/ size_t *__restrict p_length) { -#ifdef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS DeeTypeObject *tp_self = Dee_TYPE(self); struct foreach_seq_as_heap_vector_data data; if unlikely(!(likely(tp_self->tp_seq && tp_self->tp_seq->tp_foreach) || @@ -281,65 +279,6 @@ DeeSeq_AsHeapVector(DeeObject *__restrict self, Dee_Free(data.sahvd_vector); err: return NULL; -#else /* CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - struct foreach_seq_as_heap_vector_data data; - data.sahvd_size = DeeFastSeq_GetSize_deprecated(self); - if (data.sahvd_size != DEE_FASTSEQ_NOTFAST_DEPRECATED) { - size_t i; - /* Optimization for fast-sequence-compatible objects. */ - *p_length = data.sahvd_size; - data.sahvd_vector = Dee_objectlist_elemv_malloc(data.sahvd_size); - if unlikely(!data.sahvd_vector) - goto err; - if (DeeTuple_Check(self)) { - /* Further optimization: tuple can be memcpy'd */ - ASSERT(data.sahvd_size == DeeTuple_SIZE(self)); - return Dee_Movrefv(data.sahvd_vector, DeeTuple_ELEM(self), data.sahvd_size); - } - for (i = 0; i < data.sahvd_size; ++i) { - DREF DeeObject *elem; - elem = DeeFastSeq_GetItem_deprecated(self, i); - if unlikely(!elem) { - data.sahvd_size = i; - goto err_data; - } - data.sahvd_vector[i] = elem; - } - return data.sahvd_vector; - } - - /* Use `DeeObject_Foreach()' */ - data.sahvd_size = 0; - data.sahvd_alloc = 16; - data.sahvd_vector = Dee_objectlist_elemv_trymalloc(16); - if unlikely(!data.sahvd_vector) { - data.sahvd_alloc = 1; - data.sahvd_vector = Dee_objectlist_elemv_malloc(1); - if unlikely(!data.sahvd_vector) - goto err; - } - if unlikely(DeeObject_Foreach(self, &foreach_seq_as_heap_vector_cb, &data)) - goto err_data; - ASSERT(data.sahvd_size <= data.sahvd_alloc); - - /* Free unused memory. */ - if (data.sahvd_size < data.sahvd_alloc) { - DREF DeeObject **new_vector; - new_vector = Dee_objectlist_elemv_tryrealloc(data.sahvd_vector, - data.sahvd_size); - if likely(new_vector) - data.sahvd_vector = new_vector; - } - - /* Save the resulting length. */ - *p_length = data.sahvd_size; - return data.sahvd_vector; -err_data: - Dee_Decrefv(data.sahvd_vector, data.sahvd_size); - Dee_Free(data.sahvd_vector); -err: - return NULL; -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ } #ifdef Dee_MallocUsableSize @@ -368,7 +307,6 @@ DeeSeq_AsHeapVectorWithAlloc(DeeObject *__restrict self, /*[out]*/ size_t *__restrict p_allocated) #endif /* !Dee_MallocUsableSize */ { -#ifdef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS DeeTypeObject *tp_self = Dee_TYPE(self); struct foreach_seq_as_heap_vector_data data; if unlikely(!(likely(tp_self->tp_seq && tp_self->tp_seq->tp_foreach) || @@ -431,62 +369,6 @@ DeeSeq_AsHeapVectorWithAlloc(DeeObject *__restrict self, Dee_Free(data.sahvd_vector); err: return NULL; -#else /* CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - struct foreach_seq_as_heap_vector_data data; - data.sahvd_size = DeeFastSeq_GetSize_deprecated(self); - if (data.sahvd_size != DEE_FASTSEQ_NOTFAST_DEPRECATED) { - size_t i; - /* Optimization for fast-sequence-compatible objects. */ -#ifndef Dee_MallocUsableSize - *p_allocated = data.sahvd_size; -#endif /* !Dee_MallocUsableSize */ - *p_length = data.sahvd_size; - data.sahvd_vector = Dee_objectlist_elemv_malloc(data.sahvd_size); - if unlikely(!data.sahvd_vector) - goto err; - if (DeeTuple_Check(self)) { - /* Further optimization: tuple can be memcpy'd */ - ASSERT(data.sahvd_size == DeeTuple_SIZE(self)); - return Dee_Movrefv(data.sahvd_vector, DeeTuple_ELEM(self), data.sahvd_size); - } - for (i = 0; i < data.sahvd_size; ++i) { - DREF DeeObject *elem; - elem = DeeFastSeq_GetItem_deprecated(self, i); - if unlikely(!elem) { - data.sahvd_size = i; - goto err_data; - } - data.sahvd_vector[i] = elem; - } - return data.sahvd_vector; - } - - /* Use `DeeObject_Foreach()' */ - data.sahvd_size = 0; - data.sahvd_alloc = 16; - data.sahvd_vector = Dee_objectlist_elemv_trymalloc(16); - if unlikely(!data.sahvd_vector) { - data.sahvd_alloc = 1; - data.sahvd_vector = Dee_objectlist_elemv_malloc(1); - if unlikely(!data.sahvd_vector) - goto err; - } - if unlikely(DeeObject_Foreach(self, &foreach_seq_as_heap_vector_cb, &data)) - goto err_data; - ASSERT(data.sahvd_size <= data.sahvd_alloc); - - /* Save the resulting length, and allocation. */ -#ifndef Dee_MallocUsableSize - *p_allocated = data.sahvd_alloc; -#endif /* !Dee_MallocUsableSize */ - *p_length = data.sahvd_size; - return data.sahvd_vector; -err_data: - Dee_Decrefv(data.sahvd_vector, data.sahvd_size); - Dee_Free(data.sahvd_vector); -err: - return NULL; -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ } @@ -536,7 +418,6 @@ DeeSeq_AsHeapVectorWithAllocReuse(DeeObject *__restrict self, /*in-out*/ size_t *__restrict p_allocated) #endif /* !Dee_MallocUsableSize */ { -#ifdef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS DeeTypeObject *tp_self = Dee_TYPE(self); struct foreach_seq_as_heap_vector_data data; if unlikely(!(likely(tp_self->tp_seq && tp_self->tp_seq->tp_foreach) || @@ -605,67 +486,6 @@ DeeSeq_AsHeapVectorWithAllocReuse(DeeObject *__restrict self, goto done; err: return (size_t)-1; -#else /* CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - Dee_ssize_t error; - struct foreach_seq_as_heap_vector_data data; - data.sahvd_vector = *p_vector; -#ifdef Dee_MallocUsableSize - data.sahvd_alloc = Dee_objectlist_elemv_usable_size(data.sahvd_vector); -#else /* Dee_MallocUsableSize */ - data.sahvd_alloc = *p_allocated; -#endif /* !Dee_MallocUsableSize */ - ASSERT(!data.sahvd_alloc || data.sahvd_vector); - - data.sahvd_size = DeeFastSeq_GetSize_deprecated(self); - if (data.sahvd_size != DEE_FASTSEQ_NOTFAST_DEPRECATED) { - /* Fast sequence optimizations. */ - if (data.sahvd_size > data.sahvd_alloc) { - DREF DeeObject **new_vector; - new_vector = Dee_objectlist_elemv_realloc(data.sahvd_vector, - data.sahvd_size); - if unlikely(!new_vector) - goto err; - data.sahvd_vector = new_vector; - *p_vector = new_vector; -#ifndef Dee_MallocUsableSize - *p_allocated = data.sahvd_size; -#endif /* !Dee_MallocUsableSize */ - } - if (DeeTuple_Check(self)) { - /* Further optimization: tuple can be memcpy'd */ - ASSERT(data.sahvd_size == DeeTuple_SIZE(self)); - Dee_Movrefv(data.sahvd_vector, DeeTuple_ELEM(self), data.sahvd_size); - } else { - size_t i; - for (i = 0; i < data.sahvd_size; ++i) { - DREF DeeObject *elem; - elem = DeeFastSeq_GetItem_deprecated(self, i); - if unlikely(!elem) { - data.sahvd_size = i; - goto err_writeback_data; - } - data.sahvd_vector[i] = elem; /* Inherit reference. */ - } - } - return data.sahvd_size; - } - - /* Use `DeeObject_Foreach()' */ - data.sahvd_size = 0; - error = DeeObject_Foreach(self, &foreach_seq_as_heap_vector_cb, &data); - ASSERT(data.sahvd_size <= data.sahvd_alloc); - *p_vector = data.sahvd_vector; -#ifndef Dee_MallocUsableSize - *p_allocated = data.sahvd_alloc; -#endif /* !Dee_MallocUsableSize */ - if unlikely(error) - goto err_writeback_data; - return data.sahvd_size; -err_writeback_data: - Dee_Decrefv(data.sahvd_vector, data.sahvd_size); -err: - return (size_t)-1; -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ } @@ -707,7 +527,6 @@ DeeSeq_AsHeapVectorWithAllocReuseOffset(DeeObject *__restrict self, /*in*/ size_t offset) #endif /* !Dee_MallocUsableSize */ { -#ifdef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS DeeTypeObject *tp_self = Dee_TYPE(self); struct foreach_seq_as_heap_vector_data data; if unlikely(!(likely(tp_self->tp_seq && tp_self->tp_seq->tp_foreach) || @@ -791,73 +610,6 @@ DeeSeq_AsHeapVectorWithAllocReuseOffset(DeeObject *__restrict self, goto done; err: return (size_t)-1; -#else /* CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - Dee_ssize_t error; - struct foreach_seq_as_heap_vector_data data; - data.sahvd_vector = *p_vector; -#ifdef Dee_MallocUsableSize - data.sahvd_alloc = Dee_objectlist_elemv_usable_size(data.sahvd_vector); -#else /* Dee_MallocUsableSize */ - data.sahvd_alloc = *p_allocated; -#endif /* !Dee_MallocUsableSize */ - ASSERT(data.sahvd_alloc >= offset); - ASSERT(!data.sahvd_alloc || data.sahvd_vector); - - data.sahvd_size = DeeFastSeq_GetSize_deprecated(self); - if (data.sahvd_size != DEE_FASTSEQ_NOTFAST_DEPRECATED) { - /* Fast sequence optimizations. */ - if (data.sahvd_size > (data.sahvd_alloc - offset)) { - DeeObject **new_vector; - data.sahvd_alloc = offset + data.sahvd_size; - new_vector = (DeeObject **)Dee_Reallocc(data.sahvd_vector, - data.sahvd_alloc, - sizeof(DeeObject *)); - if unlikely(!new_vector) - goto err; - data.sahvd_vector = new_vector; - *p_vector = new_vector; -#ifndef Dee_MallocUsableSize - *p_allocated = data.sahvd_alloc; -#endif /* !Dee_MallocUsableSize */ - } - if (DeeTuple_Check(self)) { - /* Further optimization: tuple can be memcpy'd */ - ASSERT(data.sahvd_size == DeeTuple_SIZE(self)); - Dee_Movrefv(data.sahvd_vector + offset, DeeTuple_ELEM(self), data.sahvd_size); - } else { - size_t i; - for (i = 0; i < data.sahvd_size; ++i) { - DREF DeeObject *elem; - elem = DeeFastSeq_GetItem_deprecated(self, i); - if unlikely(!elem) { - data.sahvd_size = offset + i; - goto err_data; - } - data.sahvd_vector[offset + i] = elem; /* Inherit reference. */ - } - } - return data.sahvd_size; - } - - /* Use `DeeObject_Foreach()' */ - data.sahvd_size = offset; - ASSERT(data.sahvd_size <= data.sahvd_alloc); - error = DeeObject_Foreach(self, &foreach_seq_as_heap_vector_cb, &data); - ASSERT(data.sahvd_size <= data.sahvd_alloc); - ASSERT(data.sahvd_size >= offset); - *p_vector = data.sahvd_vector; -#ifndef Dee_MallocUsableSize - *p_allocated = data.sahvd_alloc; -#endif /* !Dee_MallocUsableSize */ - if unlikely(error) - goto err_data; - return data.sahvd_size - offset; -err_data: - Dee_Decrefv(data.sahvd_vector + offset, - data.sahvd_size - offset); -err: - return (size_t)-1; -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ } DECL_END diff --git a/src/deemon/objects/seq/subrange.c b/src/deemon/objects/seq/subrange.c deleted file mode 100644 index 1a276f717..000000000 --- a/src/deemon/objects/seq/subrange.c +++ /dev/null @@ -1,833 +0,0 @@ -/* Copyright (c) 2018-2024 Griefer@Work * - * * - * This software is provided 'as-is', without any express or implied * - * warranty. In no event will the authors be held liable for any damages * - * arising from the use of this software. * - * * - * Permission is granted to anyone to use this software for any purpose, * - * including commercial applications, and to alter it and redistribute it * - * freely, subject to the following restrictions: * - * * - * 1. The origin of this software must not be misrepresented; you must not * - * claim that you wrote the original software. If you use this software * - * in a product, an acknowledgement (see the following) in the product * - * documentation is required: * - * Portions Copyright (c) 2018-2024 Griefer@Work * - * 2. Altered source versions must be plainly marked as such, and must not be * - * misrepresented as being the original software. * - * 3. This notice may not be removed or altered from any source distribution. * - */ -#ifndef GUARD_DEEMON_OBJECTS_SEQ_SUBRANGE_C -#define GUARD_DEEMON_OBJECTS_SEQ_SUBRANGE_C 1 - -#include "subrange.h" - -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "../../runtime/runtime_error.h" -#include "../../runtime/strings.h" -#include "../seq_functions.h" - -DECL_BEGIN - -PRIVATE WUNUSED NONNULL((1)) int DCALL -subrangeiterator_init(SubRangeIterator *__restrict self, - size_t argc, DeeObject *const *argv) { - if (DeeArg_Unpack(argc, argv, "o|" UNPuSIZ UNPuSIZ ":_SubRangeIterator", - &self->sr_iter, - &self->sr_start, - &self->sr_size)) - goto err; - Dee_Incref(self->sr_iter); - return 0; -err: - return -1; -} - -PRIVATE NONNULL((1)) void DCALL -subrangeiterator_fini(SubRangeIterator *__restrict self) { - Dee_Decref(self->sr_iter); -} - -PRIVATE NONNULL((1, 2)) void DCALL -subrangeiterator_visit(SubRangeIterator *__restrict self, - dvisit_t proc, void *arg) { - Dee_Visit(self->sr_iter); -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -subrangeiterator_next(SubRangeIterator *__restrict self) { - size_t oldval; - /* Consume one from the max-iteration size. */ - do { - oldval = atomic_read(&self->sr_size); - if (!oldval) - return ITER_DONE; - } while (!atomic_cmpxch_weak_or_write(&self->sr_size, oldval, oldval - 1)); - return DeeObject_IterNext(self->sr_iter); -} - -INTDEF DeeTypeObject SeqSubRangeIterator_Type; - -PRIVATE WUNUSED NONNULL((1)) Dee_hash_t DCALL -subrangeiterator_hash(SubRangeIterator *self) { - return READ_SIZE(self); -} - -PRIVATE WUNUSED NONNULL((1, 2)) int DCALL -subrangeiterator_compare(SubRangeIterator *self, SubRangeIterator *other) { - size_t lhs_size, rhs_size; - if (DeeObject_AssertTypeExact(other, &SeqSubRangeIterator_Type)) - goto err; - lhs_size = READ_SIZE(self); - rhs_size = READ_SIZE(other); - return Dee_Compare(rhs_size, lhs_size); /* Yes: reverse order (because sizes decrease) */ -err: - return Dee_COMPARE_ERR; -} - -PRIVATE struct type_cmp subrangeiterator_cmp = { - /* .tp_hash = */ (Dee_hash_t (DCALL *)(DeeObject *))&subrangeiterator_hash, - /* .tp_compare_eq = */ NULL, - /* .tp_compare = */ (int (DCALL *)(DeeObject *, DeeObject *))&subrangeiterator_compare, -}; - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -subrangeiterator_seq_get(SubRangeIterator *__restrict self) { - DREF DeeObject *result; - DREF DeeObject *base_seq; - size_t end; - base_seq = DeeObject_GetAttr(self->sr_iter, (DeeObject *)&str_seq); - if unlikely(!base_seq) - goto err; - if (OVERFLOW_UADD(self->sr_start, self->sr_size, &end)) { - result = DeeSeq_GetRangeN(base_seq, self->sr_start); - } else { - result = DeeSeq_GetRange(base_seq, self->sr_start, end); - } - Dee_Decref(base_seq); - return result; -err: - return NULL; -} - -PRIVATE struct type_getset tpconst subrangeiterator_getsets[] = { - TYPE_GETTER_F(STR_seq, &subrangeiterator_seq_get, METHOD_FNOREFESCAPE, "->?X2?Ert:SeqSubRange?Ert:SeqSubRangeN"), - TYPE_GETSET_END -}; - -PRIVATE struct type_member tpconst subrangeiterator_members[] = { - TYPE_MEMBER_FIELD_DOC("__iter__", STRUCT_OBJECT, offsetof(SubRangeIterator, sr_iter), "->?DIterator"), - TYPE_MEMBER_FIELD("__start__", STRUCT_SIZE_T, offsetof(SubRangeIterator, sr_start)), - TYPE_MEMBER_FIELD("__len__", STRUCT_SIZE_T, offsetof(SubRangeIterator, sr_size)), - TYPE_MEMBER_END -}; - -PRIVATE WUNUSED NONNULL((1)) int DCALL -subrangeiterator_ctor(SubRangeIterator *__restrict self) { - self->sr_start = 0; - self->sr_size = 0; - self->sr_iter = Dee_None; - Dee_Incref(Dee_None); - return 0; -} - -PRIVATE WUNUSED NONNULL((1, 2)) int DCALL -subrangeiterator_copy(SubRangeIterator *__restrict self, - SubRangeIterator *__restrict other) { - self->sr_start = other->sr_start; - self->sr_size = READ_SIZE(other); - self->sr_iter = DeeObject_Copy(other->sr_iter); - if unlikely(!self->sr_iter) - goto err; - return 0; -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1, 2)) int DCALL -subrangeiterator_deep(SubRangeIterator *__restrict self, - SubRangeIterator *__restrict other) { - self->sr_start = other->sr_start; - self->sr_size = READ_SIZE(other); - self->sr_iter = DeeObject_DeepCopy(other->sr_iter); - if unlikely(!self->sr_iter) - goto err; - return 0; -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -subrangeiterator_bool(SubRangeIterator *__restrict self) { - if (!READ_SIZE(self)) - return 0; - return DeeObject_Bool(self->sr_iter); -} - -INTERN DeeTypeObject SeqSubRangeIterator_Type = { - OBJECT_HEAD_INIT(&DeeType_Type), - /* .tp_name = */ "_SeqSubRangeIterator", - /* .tp_doc = */ NULL, - /* .tp_flags = */ TP_FNORMAL | TP_FFINAL, - /* .tp_weakrefs = */ 0, - /* .tp_features = */ TF_NONE, - /* .tp_base = */ &DeeIterator_Type, - /* .tp_init = */ { - { - /* .tp_alloc = */ { - /* .tp_ctor = */ (dfunptr_t)&subrangeiterator_ctor, - /* .tp_copy_ctor = */ (dfunptr_t)&subrangeiterator_copy, - /* .tp_deep_ctor = */ (dfunptr_t)&subrangeiterator_deep, - /* .tp_any_ctor = */ (dfunptr_t)&subrangeiterator_init, - TYPE_FIXED_ALLOCATOR(SubRangeIterator) - } - }, - /* .tp_dtor = */ (void (DCALL *)(DeeObject *__restrict))&subrangeiterator_fini, - /* .tp_assign = */ NULL, - /* .tp_move_assign = */ NULL - }, - /* .tp_cast = */ { - /* .tp_str = */ NULL, - /* .tp_repr = */ NULL, - /* .tp_bool = */ (int (DCALL *)(DeeObject *__restrict))&subrangeiterator_bool - }, - /* .tp_call = */ NULL, - /* .tp_visit = */ (void (DCALL *)(DeeObject *__restrict, dvisit_t, void *))&subrangeiterator_visit, - /* .tp_gc = */ NULL, - /* .tp_math = */ NULL, - /* .tp_cmp = */ &subrangeiterator_cmp, - /* .tp_seq = */ NULL, - /* .tp_iter_next = */ (DREF DeeObject *(DCALL *)(DeeObject *__restrict))&subrangeiterator_next, - /* .tp_iterator = */ NULL, - /* .tp_attr = */ NULL, - /* .tp_with = */ NULL, - /* .tp_buffer = */ NULL, - /* .tp_methods = */ NULL, - /* .tp_getsets = */ subrangeiterator_getsets, - /* .tp_members = */ subrangeiterator_members, - /* .tp_class_methods = */ NULL, - /* .tp_class_getsets = */ NULL, - /* .tp_class_members = */ NULL -}; - - - - -PRIVATE NONNULL((1)) void DCALL -subrange_fini(SubRange *__restrict self) { - Dee_Decref(self->sr_seq); -} - -PRIVATE NONNULL((1, 2)) void DCALL -subrange_visit(SubRange *__restrict self, dvisit_t proc, void *arg) { - Dee_Visit(self->sr_seq); -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -subrange_size(SubRange *__restrict self) { - size_t inner_size = DeeObject_Size(self->sr_seq); - if unlikely(inner_size == (size_t)-1) - goto err; - if (self->sr_start >= inner_size) - return_reference_(DeeInt_Zero); - inner_size -= self->sr_start; - if (inner_size > self->sr_size) - inner_size = self->sr_size; - return DeeInt_NewSize(inner_size); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -subrange_iter(SubRange *__restrict self) { - DREF SubRangeIterator *result; - DREF DeeObject *iterator; - size_t begin_index; - iterator = DeeObject_Iter(self->sr_seq); - if unlikely(!iterator) - goto err; - result = DeeObject_MALLOC(SubRangeIterator); - if unlikely(!result) - goto err_iterator; - begin_index = self->sr_start; - result->sr_start = begin_index; - result->sr_size = self->sr_size; - result->sr_iter = iterator; /* Inherit reference. */ - /* Discard leading items. */ - while (begin_index--) { - DREF DeeObject *discard; - discard = DeeObject_IterNext(iterator); - if unlikely(!ITER_ISOK(discard)) { - if (!discard) - goto err_iterator_r; - /* Empty iterator (the base iterator was exhausted during the discard-phase) */ - result->sr_size = 0; - break; - } - Dee_Decref(discard); - if (DeeThread_CheckInterrupt()) - goto err_iterator_r; - } - DeeObject_Init(result, &SeqSubRangeIterator_Type); - return (DREF DeeObject *)result; -err_iterator_r: - DeeObject_FREE(result); -err_iterator: - Dee_Decref(iterator); -err: - return NULL; -} - -PRIVATE struct type_member tpconst subrange_members[] = { - TYPE_MEMBER_FIELD_DOC("__seq__", STRUCT_OBJECT, offsetof(SubRange, sr_seq), "->?DSequence"), - TYPE_MEMBER_FIELD("__start__", STRUCT_CONST | STRUCT_SIZE_T, offsetof(SubRange, sr_start)), - TYPE_MEMBER_FIELD("__len__", STRUCT_CONST | STRUCT_SIZE_T, offsetof(SubRange, sr_size)), - TYPE_MEMBER_END -}; - - -PRIVATE WUNUSED NONNULL((1)) DREF SubRange *DCALL -subrange_get_frozen(SubRange *__restrict self) { - DREF DeeObject *inner_frozen; - DREF SubRange *result; - inner_frozen = DeeObject_GetAttr(self->sr_seq, (DeeObject *)&str_seq); - if unlikely(!inner_frozen) - goto err; - if (inner_frozen == self->sr_seq) { - Dee_DecrefNokill(inner_frozen); - return_reference_(self); - } - result = DeeObject_MALLOC(SubRange); - if unlikely(!result) - goto err_inner; - result->sr_seq = inner_frozen; - result->sr_start = self->sr_start; - result->sr_size = self->sr_size; - DeeObject_Init(result, &SeqSubRange_Type); - return result; -err_inner: - Dee_Decref(inner_frozen); -err: - return NULL; -} - -PRIVATE struct type_getset tpconst subrange_getsets[] = { - TYPE_GETTER("frozen", &subrange_get_frozen, "->?."), - TYPE_GETSET_END -}; - -PRIVATE struct type_member tpconst subrange_class_members[] = { - TYPE_MEMBER_CONST(STR_Iterator, &SeqSubRangeIterator_Type), - TYPE_MEMBER_CONST("Frozen", &SeqSubRange_Type), - TYPE_MEMBER_END -}; - -PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -subrange_getitem(SubRange *self, DeeObject *index_ob) { - size_t index; - if (DeeObject_AsSize(index_ob, &index)) - goto err; - return DeeObject_GetItemIndex(self->sr_seq, self->sr_start + index); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) size_t DCALL -subrange_nsi_getsize(SubRange *__restrict self) { - size_t inner_size = DeeObject_Size(self->sr_seq); - if unlikely(inner_size != (size_t)-1) { - if (self->sr_start >= inner_size) - return 0; - inner_size -= self->sr_start; - if (inner_size > self->sr_size) - inner_size = self->sr_size; - } - return inner_size; -} - -PRIVATE WUNUSED NONNULL((1)) size_t DCALL -subrange_nsi_getsize_fast(SubRange *__restrict self) { - size_t inner_size = DeeFastSeq_GetSize_deprecated(self->sr_seq); - if unlikely(inner_size != (size_t)-1) { - if (self->sr_start >= inner_size) - return 0; - inner_size -= self->sr_start; - if (inner_size > self->sr_size) - inner_size = self->sr_size; - } - return inner_size; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -subrange_nsi_getitem(SubRange *__restrict self, size_t index) { - return DeeObject_GetItemIndex(self->sr_seq, - self->sr_start + index); -} - -PRIVATE struct type_nsi tpconst subrange_nsi = { - /* .nsi_class = */ TYPE_SEQX_CLASS_SEQ, - /* .nsi_flags = */ TYPE_SEQX_FNORMAL, - { - /* .nsi_seqlike = */ { - /* .nsi_getsize = */ (dfunptr_t)&subrange_nsi_getsize, - /* .nsi_getsize_fast = */ (dfunptr_t)&subrange_nsi_getsize_fast, - /* .nsi_getitem = */ (dfunptr_t)&subrange_nsi_getitem, - } - } -}; - -PRIVATE struct type_seq subrange_seq = { - /* .tp_iter = */ (DREF DeeObject *(DCALL *)(DeeObject *__restrict))&subrange_iter, - /* .tp_sizeob = */ (DREF DeeObject *(DCALL *)(DeeObject *__restrict))&subrange_size, - /* .tp_contains = */ NULL, - /* .tp_getitem = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))&subrange_getitem, - /* .tp_delitem = */ NULL, - /* .tp_setitem = */ NULL, - /* .tp_getrange = */ NULL, - /* .tp_delrange = */ NULL, - /* .tp_setrange = */ NULL, - /* .tp_nsi = */ &subrange_nsi -}; - - -PRIVATE WUNUSED NONNULL((1)) int DCALL -subrange_ctor(SubRange *__restrict self) { - self->sr_seq = Dee_EmptySeq; - self->sr_start = 0; - self->sr_size = 0; - Dee_Incref(Dee_EmptySeq); - return 0; -} - -PRIVATE WUNUSED NONNULL((1, 2)) int DCALL -subrange_copy(SubRange *__restrict self, - SubRange *__restrict other) { - self->sr_seq = other->sr_seq; - self->sr_start = other->sr_start; - self->sr_size = other->sr_size; - Dee_Incref(self->sr_seq); - return 0; -} - -PRIVATE WUNUSED NONNULL((1, 2)) int DCALL -subrange_deep(SubRange *__restrict self, - SubRange *__restrict other) { - self->sr_seq = DeeObject_DeepCopy(other->sr_seq); - if unlikely(!self->sr_seq) - goto err; - self->sr_start = other->sr_start; - self->sr_size = other->sr_size; - return 0; -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -subrange_init(SubRange *__restrict self, size_t argc, - DeeObject *const *argv) { - size_t end = (size_t)-1; - self->sr_start = 0; - if (DeeArg_Unpack(argc, argv, "o|" UNPuSIZ UNPuSIZ ":_SeqSubRange", - &self->sr_seq, &self->sr_start, &end)) - goto err; - if (end < self->sr_start) - end = self->sr_start; - self->sr_size = (size_t)(end - self->sr_start); - Dee_Incref(self->sr_seq); - return 0; -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -subrange_bool(SubRange *__restrict self) { - size_t seqsize; - if unlikely(!self->sr_size) - return 0; - seqsize = DeeObject_Size(self->sr_seq); - if unlikely(seqsize == (size_t)-1) - goto err; - return self->sr_start < seqsize; -err: - return -1; -} - - -INTERN DeeTypeObject SeqSubRange_Type = { - OBJECT_HEAD_INIT(&DeeType_Type), - /* .tp_name = */ "_SeqSubRange", - /* .tp_doc = */ DOC("(seq:?DSequence,start=!0,end:?Dint=!A!Dint!PSIZE_MAX)"), - /* .tp_flags = */ TP_FNORMAL | TP_FFINAL, - /* .tp_weakrefs = */ 0, - /* .tp_features = */ TF_NONE, - /* .tp_base = */ &DeeSeq_Type, - /* .tp_init = */ { - { - /* .tp_alloc = */ { - /* .tp_ctor = */ (dfunptr_t)&subrange_ctor, - /* .tp_copy_ctor = */ (dfunptr_t)&subrange_copy, - /* .tp_deep_ctor = */ (dfunptr_t)&subrange_deep, - /* .tp_any_ctor = */ (dfunptr_t)&subrange_init, - TYPE_FIXED_ALLOCATOR(SubRange) - } - }, - /* .tp_dtor = */ (void (DCALL *)(DeeObject *__restrict))&subrange_fini, - /* .tp_assign = */ NULL, - /* .tp_move_assign = */ NULL - }, - /* .tp_cast = */ { - /* .tp_str = */ NULL, - /* .tp_repr = */ NULL, - /* .tp_bool = */ (int (DCALL *)(DeeObject *__restrict))&subrange_bool - }, - /* .tp_call = */ NULL, - /* .tp_visit = */ (void (DCALL *)(DeeObject *__restrict, dvisit_t, void *))&subrange_visit, - /* .tp_gc = */ NULL, - /* .tp_math = */ NULL, - /* .tp_cmp = */ NULL, - /* .tp_seq = */ &subrange_seq, - /* .tp_iter_next = */ NULL, - /* .tp_iterator = */ NULL, - /* .tp_attr = */ NULL, - /* .tp_with = */ NULL, - /* .tp_buffer = */ NULL, - /* .tp_methods = */ NULL, - /* .tp_getsets = */ subrange_getsets, - /* .tp_members = */ subrange_members, - /* .tp_class_methods = */ NULL, - /* .tp_class_getsets = */ NULL, - /* .tp_class_members = */ subrange_class_members -}; - -INTERN WUNUSED NONNULL((1)) DREF DeeObject *DCALL -DeeSeq_GetRange(DeeObject *__restrict self, - size_t begin, size_t end) { - DREF SubRange *result; - if unlikely(begin >= end) - return_reference_(Dee_EmptySeq); - - /* Create a sub-range sequence. */ - result = DeeObject_MALLOC(SubRange); - if unlikely(!result) - goto done; - if (DeeObject_InstanceOfExact(self, &SeqSubRange_Type)) { - SubRange *me = (SubRange *)self; - /* Special handling for recursion. */ - Dee_Incref(me->sr_seq); - result->sr_seq = me->sr_seq; - result->sr_start = begin + me->sr_start; - result->sr_size = MIN((size_t)(end - begin), me->sr_size); - } else { - Dee_Incref(self); - result->sr_seq = self; - result->sr_start = begin; - result->sr_size = (size_t)(end - begin); - } - DeeObject_Init(result, &SeqSubRange_Type); -done: - return (DREF DeeObject *)result; -} - -INTERN WUNUSED NONNULL((1)) DREF DeeObject *DCALL -DeeSeq_GetRangeN(DeeObject *__restrict self, - size_t begin) { - DREF SubRangeN *result; - if (!begin) - return_reference_(self); - /* Create a sub-range sequence. */ - result = DeeObject_MALLOC(SubRangeN); - if unlikely(!result) - goto done; - if (DeeObject_InstanceOfExact(self, &SeqSubRangeN_Type)) { - SubRangeN *me = (SubRangeN *)self; - /* Special handling for recursion. */ - Dee_Incref(me->sr_seq); - result->sr_seq = me->sr_seq; - result->sr_start = begin + me->sr_start; - } else { - Dee_Incref(self); - result->sr_seq = self; - result->sr_start = begin; - } - DeeObject_Init(result, &SeqSubRangeN_Type); -done: - return (DREF DeeObject *)result; -} - - - - -STATIC_ASSERT(offsetof(SubRangeN, sr_seq) == - offsetof(SubRange, sr_seq)); -#define subrangen_fini subrange_fini -#define subrangen_visit subrange_visit - - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -subrangen_iter(SubRangeN *__restrict self) { - DREF DeeObject *result, *elem; - size_t offset; - result = DeeObject_Iter(self->sr_seq); - if unlikely(!result) - goto done; - offset = self->sr_start; - while (offset--) { - elem = DeeObject_IterNext(result); - if (!ITER_ISOK(elem)) { - if (!elem) - goto err; - break; /* End of sequence. */ - } - Dee_Decref(elem); - if (DeeThread_CheckInterrupt()) - goto err; - } -done: - return result; -err: - Dee_Decref(result); - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) size_t DCALL -subrangen_nsi_getsize(SubRangeN *__restrict self) { - size_t result; - result = DeeObject_Size(self->sr_seq); - if likely(result != (size_t)-1) { - if (result <= self->sr_start) { - result = 0; - } else { - result -= self->sr_start; - } - } - return result; -} - -PRIVATE WUNUSED NONNULL((1)) size_t DCALL -subrangen_nsi_getsize_fast(SubRangeN *__restrict self) { - size_t result; - result = DeeFastSeq_GetSize_deprecated(self->sr_seq); - if likely(result != (size_t)-1) { - if (result <= self->sr_start) { - result = 0; - } else { - result -= self->sr_start; - } - } - return result; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -subrangen_size(SubRangeN *__restrict self) { - size_t result = subrangen_nsi_getsize(self); - if unlikely(result == (size_t)-1) - goto err; - return DeeInt_NewSize(result); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -subrangen_getitem(SubRangeN *self, - DeeObject *index_ob) { - size_t index; - if (DeeObject_AsSize(index_ob, &index)) - goto err; - return DeeObject_GetItemIndex(self->sr_seq, - self->sr_start + index); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -subrangen_nsi_getitem(SubRangeN *__restrict self, size_t index) { - return DeeObject_GetItemIndex(self->sr_seq, - self->sr_start + index); -} - -PRIVATE struct type_nsi tpconst subrangen_nsi = { - /* .nsi_class = */ TYPE_SEQX_CLASS_SEQ, - /* .nsi_flags = */ TYPE_SEQX_FNORMAL, - { - /* .nsi_seqlike = */ { - /* .nsi_getsize = */ (dfunptr_t)&subrangen_nsi_getsize, - /* .nsi_getsize_fast = */ (dfunptr_t)&subrangen_nsi_getsize_fast, - /* .nsi_getitem = */ (dfunptr_t)&subrangen_nsi_getitem, - } - } -}; - -PRIVATE struct type_seq subrangen_seq = { - /* .tp_iter = */ (DREF DeeObject *(DCALL *)(DeeObject *__restrict))&subrangen_iter, - /* .tp_sizeob = */ (DREF DeeObject *(DCALL *)(DeeObject *__restrict))&subrangen_size, - /* .tp_contains = */ NULL, - /* .tp_getitem = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))&subrangen_getitem, - /* .tp_delitem = */ NULL, - /* .tp_setitem = */ NULL, - /* .tp_getrange = */ NULL, - /* .tp_delrange = */ NULL, - /* .tp_setrange = */ NULL, - /* .tp_nsi = */ &subrangen_nsi -}; - -PRIVATE struct type_member tpconst subrangen_members[] = { - TYPE_MEMBER_FIELD_DOC("__seq__", STRUCT_OBJECT, offsetof(SubRangeN, sr_seq), "->?DSequence"), - TYPE_MEMBER_FIELD("__start__", STRUCT_SIZE_T, offsetof(SubRangeN, sr_start)), - TYPE_MEMBER_END -}; - -PRIVATE WUNUSED NONNULL((1)) DREF SubRangeN *DCALL -subrangen_get_frozen(SubRangeN *__restrict self) { - DREF DeeObject *inner_frozen; - DREF SubRangeN *result; - inner_frozen = DeeObject_GetAttr(self->sr_seq, (DeeObject *)&str_seq); - if unlikely(!inner_frozen) - goto err; - if (inner_frozen == self->sr_seq) { - Dee_DecrefNokill(inner_frozen); - return_reference_(self); - } - result = DeeObject_MALLOC(SubRangeN); - if unlikely(!result) - goto err_inner; - result->sr_seq = inner_frozen; - result->sr_start = self->sr_start; - DeeObject_Init(result, &SeqSubRangeN_Type); - return result; -err_inner: - Dee_Decref(inner_frozen); -err: - return NULL; -} - -PRIVATE struct type_getset tpconst subrangen_getsets[] = { - TYPE_GETTER("frozen", &subrangen_get_frozen, "->?."), - TYPE_GETSET_END -}; - -PRIVATE struct type_member tpconst subrangen_class_members[] = { - TYPE_MEMBER_CONST(STR_Iterator, &DeeIterator_Type), - TYPE_MEMBER_CONST("Frozen", &DeeIterator_Type), - TYPE_MEMBER_END -}; - -PRIVATE WUNUSED NONNULL((1)) int DCALL -subrangen_ctor(SubRangeN *__restrict self) { - self->sr_seq = Dee_EmptySeq; - self->sr_start = 0; - Dee_Incref(Dee_EmptySeq); - return 0; -} - -PRIVATE WUNUSED NONNULL((1, 2)) int DCALL -subrangen_copy(SubRangeN *__restrict self, - SubRangeN *__restrict other) { - self->sr_seq = other->sr_seq; - self->sr_start = other->sr_start; - Dee_Incref(self->sr_seq); - return 0; -} - -PRIVATE WUNUSED NONNULL((1, 2)) int DCALL -subrangen_deep(SubRangeN *__restrict self, - SubRangeN *__restrict other) { - self->sr_seq = DeeObject_DeepCopy(other->sr_seq); - if unlikely(!self->sr_seq) - goto err; - self->sr_start = other->sr_start; - return 0; -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -subrangen_init(SubRangeN *__restrict self, size_t argc, - DeeObject *const *argv) { - self->sr_start = 0; - if (DeeArg_Unpack(argc, argv, "o|" UNPuSIZ ":_SeqSubRangeN", - &self->sr_seq, &self->sr_start)) - goto err; - Dee_Incref(self->sr_seq); - return 0; -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1)) int DCALL -subrangen_bool(SubRangeN *__restrict self) { - size_t seqsize = DeeObject_Size(self->sr_seq); - if unlikely(seqsize == (size_t)-1) - goto err; - return self->sr_start < seqsize; -err: - return -1; -} - - -INTERN DeeTypeObject SeqSubRangeN_Type = { - OBJECT_HEAD_INIT(&DeeType_Type), - /* .tp_name = */ "_SeqSubRangeN", - /* .tp_doc = */ DOC("(seq:?DSequence,start=!0)"), - /* .tp_flags = */ TP_FNORMAL | TP_FFINAL, - /* .tp_weakrefs = */ 0, - /* .tp_features = */ TF_NONE, - /* .tp_base = */ &DeeSeq_Type, - /* .tp_init = */ { - { - /* .tp_alloc = */ { - /* .tp_ctor = */ (dfunptr_t)&subrangen_ctor, - /* .tp_copy_ctor = */ (dfunptr_t)&subrangen_copy, - /* .tp_deep_ctor = */ (dfunptr_t)&subrangen_deep, - /* .tp_any_ctor = */ (dfunptr_t)&subrangen_init, - TYPE_FIXED_ALLOCATOR(SubRangeN) - } - }, - /* .tp_dtor = */ (void (DCALL *)(DeeObject *__restrict))&subrangen_fini, - /* .tp_assign = */ NULL, - /* .tp_move_assign = */ NULL - }, - /* .tp_cast = */ { - /* .tp_str = */ NULL, - /* .tp_repr = */ NULL, - /* .tp_bool = */ (int (DCALL *)(DeeObject *__restrict))&subrangen_bool - }, - /* .tp_call = */ NULL, - /* .tp_visit = */ (void (DCALL *)(DeeObject *__restrict, dvisit_t, void *))&subrangen_visit, - /* .tp_gc = */ NULL, - /* .tp_math = */ NULL, - /* .tp_cmp = */ NULL, - /* .tp_seq = */ &subrangen_seq, - /* .tp_iter_next = */ NULL, - /* .tp_iterator = */ NULL, - /* .tp_attr = */ NULL, - /* .tp_with = */ NULL, - /* .tp_buffer = */ NULL, - /* .tp_methods = */ NULL, - /* .tp_getsets = */ subrangen_getsets, - /* .tp_members = */ subrangen_members, - /* .tp_class_methods = */ NULL, - /* .tp_class_getsets = */ NULL, - /* .tp_class_members = */ subrangen_class_members -}; - - -DECL_END -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - -#endif /* !GUARD_DEEMON_OBJECTS_SEQ_SUBRANGE_C */ diff --git a/src/deemon/objects/seq/subrange.h b/src/deemon/objects/seq/subrange.h deleted file mode 100644 index a5f26aa62..000000000 --- a/src/deemon/objects/seq/subrange.h +++ /dev/null @@ -1,61 +0,0 @@ -/* Copyright (c) 2018-2024 Griefer@Work * - * * - * This software is provided 'as-is', without any express or implied * - * warranty. In no event will the authors be held liable for any damages * - * arising from the use of this software. * - * * - * Permission is granted to anyone to use this software for any purpose, * - * including commercial applications, and to alter it and redistribute it * - * freely, subject to the following restrictions: * - * * - * 1. The origin of this software must not be misrepresented; you must not * - * claim that you wrote the original software. If you use this software * - * in a product, an acknowledgement (see the following) in the product * - * documentation is required: * - * Portions Copyright (c) 2018-2024 Griefer@Work * - * 2. Altered source versions must be plainly marked as such, and must not be * - * misrepresented as being the original software. * - * 3. This notice may not be removed or altered from any source distribution. * - */ -#ifndef GUARD_DEEMON_OBJECTS_SEQ_SUBRANGE_H -#define GUARD_DEEMON_OBJECTS_SEQ_SUBRANGE_H 1 - -#include - -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS -#include - -DECL_BEGIN - -typedef struct { - OBJECT_HEAD - DREF DeeObject *sr_iter; /* [1..1][const] Underlying iterator. */ - size_t sr_size; /* Max remaining number of items to-be yielded. */ - size_t sr_start; /* Starting index of the original sub-range. */ -} SubRangeIterator; -#define READ_SIZE(x) atomic_read(&(x)->sr_size) - -typedef struct { - OBJECT_HEAD - DREF DeeObject *sr_seq; /* [1..1][const] Underlying sequence. */ - size_t sr_start; /* [const] Amount of items discarded at the beginning. */ - size_t sr_size; /* [const] Max amount of items yielded. */ -} SubRange; - -typedef struct { - OBJECT_HEAD - DREF DeeObject *sr_seq; /* [1..1][const] Underlying sequence. */ - size_t sr_start; /* [const] Amount of items discarded at the beginning. */ -} SubRangeN; - -INTDEF DeeTypeObject SeqSubRangeIterator_Type; -INTDEF DeeTypeObject SeqSubRange_Type; -INTDEF DeeTypeObject SeqSubRangeN_Type; - -INTDEF WUNUSED NONNULL((1)) DREF DeeObject *DCALL DeeSeq_GetRange(DeeObject *__restrict self, size_t begin, size_t end); -INTDEF WUNUSED NONNULL((1)) DREF DeeObject *DCALL DeeSeq_GetRangeN(DeeObject *__restrict self, size_t begin); - -DECL_END -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - -#endif /* !GUARD_DEEMON_OBJECTS_SEQ_SUBRANGE_H */ diff --git a/src/deemon/objects/seq_functions.c b/src/deemon/objects/seq_functions.c deleted file mode 100644 index 4356ace3e..000000000 --- a/src/deemon/objects/seq_functions.c +++ /dev/null @@ -1,392 +0,0 @@ -/* Copyright (c) 2018-2024 Griefer@Work * - * * - * This software is provided 'as-is', without any express or implied * - * warranty. In no event will the authors be held liable for any damages * - * arising from the use of this software. * - * * - * Permission is granted to anyone to use this software for any purpose, * - * including commercial applications, and to alter it and redistribute it * - * freely, subject to the following restrictions: * - * * - * 1. The origin of this software must not be misrepresented; you must not * - * claim that you wrote the original software. If you use this software * - * in a product, an acknowledgement (see the following) in the product * - * documentation is required: * - * Portions Copyright (c) 2018-2024 Griefer@Work * - * 2. Altered source versions must be plainly marked as such, and must not be * - * misrepresented as being the original software. * - * 3. This notice may not be removed or altered from any source distribution. * - */ -#ifndef GUARD_DEEMON_OBJECTS_SEQ_FUNCTIONS_C -#define GUARD_DEEMON_OBJECTS_SEQ_FUNCTIONS_C 1 - -#include "seq_functions.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include "../runtime/runtime_error.h" -#include "seq/default-api.h" - -DECL_BEGIN - -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS -/* NOTE: Technically, all of these functions can be used on any type of object, - * but all objects derived from `DeeSeq_Type' automatically implement - * all of them as member functions. - * With that in mind, any type implementing the `tp_seq' interface - * with the intention of behaving as an Iterable, should probably - * be derived from `DeeSeq_Type' as this allows usercode to query - * for a general purpose sequence by writing `x is Sequence from deemon' */ -INTERN WUNUSED NONNULL((1)) size_t DCALL DeeSeq_Size(DeeObject *__restrict self) { - DREF DeeObject *iter, *elem; - size_t result = 0; - /* Count the number of elements, given an iterator. */ - iter = DeeObject_Iter(self); - if unlikely(!iter) { - if (DeeError_Catch(&DeeError_NotImplemented)) - err_unimplemented_operator(Dee_TYPE(self), OPERATOR_SIZE); - goto err; - } - while (ITER_ISOK(elem = DeeObject_IterNext(iter))) { - Dee_Decref(elem); - if unlikely(result == (size_t)-2) { - err_integer_overflow_i(sizeof(size_t) * 8, true); - goto err; - } - ++result; - if (DeeThread_CheckInterrupt()) - goto err; - } - Dee_Decref(iter); - if unlikely(!elem) - goto err; - return result; -err: - return (size_t)-1; -} - -PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -iterator_get_nth(DeeObject *__restrict self, - DeeObject *__restrict sequence, - size_t index) { - DREF DeeObject *elem; - size_t current_index = 0; - while (ITER_ISOK(elem = DeeObject_IterNext(self))) { - if (current_index == (size_t)index) - return elem; - Dee_Decref(elem); - ++current_index; - if (DeeThread_CheckInterrupt()) - goto err; - } - if unlikely(!elem) - goto err; - err_index_out_of_bounds(sequence, index, current_index); -err: - return NULL; -} - -INTERN WUNUSED NONNULL((1)) DREF DeeObject *DCALL -DeeSeq_GetItem(DeeObject *__restrict self, size_t index) { - DeeTypeObject *tp_self; - DREF DeeObject *result; - DeeTypeMRO mro; - ASSERT_OBJECT(self); - tp_self = Dee_TYPE(self); - if unlikely(tp_self == &DeeSeq_Type) { - err_index_out_of_bounds(self, index, 0); - goto err; - } - DeeTypeMRO_Init(&mro, tp_self); - for (;;) { - struct type_seq *seq; - if ((seq = tp_self->tp_seq) != NULL) { - struct type_nsi const *nsi; - if ((nsi = seq->tp_nsi) != NULL && - nsi->nsi_class == TYPE_SEQX_CLASS_SEQ && - is_noninherited_nsi(tp_self, seq, nsi)) { - if (nsi->nsi_seqlike.nsi_getitem) - return (*nsi->nsi_seqlike.nsi_getitem)(self, index); - if (nsi->nsi_seqlike.nsi_getitem_fast) { - size_t mylen = (*nsi->nsi_common.nsi_getsize)(self); - if unlikely(mylen == (size_t)-1) - goto err; - if unlikely(index >= mylen) { - err_index_out_of_bounds(self, index, mylen); - goto err; - } - result = (*nsi->nsi_seqlike.nsi_getitem_fast)(self, index); - if unlikely(!result) - err_unbound_index(self, index); - return result; - } - if (nsi->nsi_seqlike.nsi_getrange) { - result = (*nsi->nsi_seqlike.nsi_getrange)(self, index, index + 1); - if unlikely(!result) - goto err; - goto return_result_first; - } - } - if (has_noninherited_getitem(tp_self, seq)) { - DREF DeeObject *index_ob; - index_ob = DeeInt_NewSize(index); - if unlikely(!index_ob) - goto err; - result = (*seq->tp_getitem)(self, index_ob); - Dee_Decref(index_ob); - return result; - } - if (has_noninherited_getrange(tp_self, seq)) { - DREF DeeObject *real_result; - DREF DeeObject *index_ob, *index_plus1_ob; - index_ob = DeeInt_NewSize(index); - if unlikely(!index_ob) - goto err; - index_plus1_ob = DeeInt_NewSize(index + 1); - if unlikely(!index_plus1_ob) { - Dee_Decref(index_ob); - goto err; - } - result = (*seq->tp_getrange)(self, index_ob, index_plus1_ob); - Dee_Decref(index_plus1_ob); - Dee_Decref(index_ob); - if unlikely(!result) - goto err; -return_result_first: - real_result = default_seq_getfirst(result); - Dee_Decref(result); - if unlikely(!real_result) { - /* Translate the empty-sequence error into an index-out-of-bounds */ - if (DeeError_Catch(&DeeError_ValueError)) { - size_t mylen = DeeObject_Size(self); - if unlikely(mylen == (size_t)-1) - goto err; - err_index_out_of_bounds(self, index, mylen); - } - } - return real_result; - } - if (seq->tp_iter) { - DREF DeeObject *iterator; - iterator = (*seq->tp_iter)(self); - if unlikely(!iterator) - goto err; - result = iterator_get_nth(iterator, self, index); - Dee_Decref(iterator); - return result; - } - } - if ((tp_self = DeeTypeMRO_Next(&mro, tp_self)) == NULL) - break; - if (tp_self == &DeeSeq_Type) - break; - } - err_unimplemented_operator3(Dee_TYPE(self), - OPERATOR_GETITEM, - OPERATOR_GETRANGE, - OPERATOR_ITER); -err: - return NULL; -} - -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - - - - - -/************************************************************************/ -/* Sequence compare */ -/************************************************************************/ -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS - -/* @return: == Dee_COMPARE_ERR: An error occurred. - * @return: == -1: `self < some_object' - * @return: == 0: Objects compare as equal - * @return: == 1: `self > some_object' */ -INTERN WUNUSED NONNULL((1)) int DCALL -DeeSeq_CompareIV(DeeObject *lhs, - DeeObject *const *rhsv, size_t rhsc) { - size_t i; - DREF DeeObject *lhs_item; - for (i = 0; i < rhsc; ++i) { - int diff; - lhs_item = DeeObject_IterNext(lhs); - if unlikely(!ITER_ISOK(lhs_item)) { - if (lhs_item == ITER_DONE) - return -1; /* COUNT(LHS) < COUNT(RHS) */ - return Dee_COMPARE_ERR; - } - diff = DeeObject_Compare(lhs_item, rhsv[i]); - Dee_Decref(lhs_item); - if (diff != 0) - return diff; - } - lhs_item = DeeObject_IterNext(lhs); - if (!ITER_ISOK(lhs_item)) { - if (lhs_item == ITER_DONE) - return 0; /* COUNT(LHS) == COUNT(RHS) */ - return Dee_COMPARE_ERR; - } - Dee_Decref(lhs_item); - return 1; /* COUNT(LHS) > COUNT(RHS) */ -} - -INTERN WUNUSED NONNULL((1, 2)) int DCALL -DeeSeq_CompareII(DeeObject *lhs, - DeeObject *rhs) { - for (;;) { - int diff; - DREF DeeObject *lhs_item; - DREF DeeObject *rhs_item; - lhs_item = DeeObject_IterNext(lhs); - if unlikely(!ITER_ISOK(lhs_item)) { - if unlikely(lhs_item != ITER_DONE) - return Dee_COMPARE_ERR; - rhs_item = DeeObject_IterNext(rhs); - if (!ITER_ISOK(rhs_item)) { - if unlikely(rhs_item != ITER_DONE) - return Dee_COMPARE_ERR; - return 0; /* COUNT(LHS) == COUNT(RHS) */ - } - Dee_Decref(rhs_item); - return -1; /* COUNT(LHS) < COUNT(RHS) */ - } - rhs_item = DeeObject_IterNext(rhs); - if unlikely(!ITER_ISOK(rhs_item)) { - Dee_Decref(lhs_item); - if unlikely(rhs_item != ITER_DONE) - return Dee_COMPARE_ERR; - return 1; /* COUNT(LHS) > COUNT(RHS) */ - } - diff = DeeObject_Compare(lhs_item, rhs_item); - Dee_Decref(rhs_item); - Dee_Decref(lhs_item); - if (diff != 0) - return diff; - } -} - -INTERN WUNUSED NONNULL((1, 2)) int DCALL -DeeSeq_CompareIS(DeeObject *lhs, - DeeObject *rhs) { - int result; - size_t rhs_size; - if (DeeTuple_Check(rhs)) { - result = DeeSeq_CompareIV(lhs, DeeTuple_ELEM(rhs), DeeTuple_SIZE(rhs)); - } else { - DREF DeeObject *rhs_iter; - rhs_iter = DeeObject_Iter(rhs); - if unlikely(!rhs_iter) - return Dee_COMPARE_ERR; - result = DeeSeq_CompareII(lhs, rhs_iter); - Dee_Decref(rhs_iter); - } - return result; -} - - - -/* @return: == -1: An error occurred. - * @return: == 0: Sequences differ - * @return: == 1: Sequences are equal */ -INTERN WUNUSED NONNULL((1)) int DCALL -DeeSeq_EqIV(DeeObject *lhs, - DeeObject *const *rhsv, size_t rhsc) { - size_t i; - DREF DeeObject *lhs_item; - for (i = 0; i < rhsc; ++i) { - int diff; - lhs_item = DeeObject_IterNext(lhs); - if unlikely(!ITER_ISOK(lhs_item)) { - if (lhs_item == ITER_DONE) - return 0; /* COUNT(LHS) < COUNT(RHS) */ - return -1; - } - diff = DeeObject_TryCmpEqAsBool(lhs_item, rhsv[i]); - Dee_Decref(lhs_item); - if (diff <= 0) - return diff; - } - lhs_item = DeeObject_IterNext(lhs); - if (!ITER_ISOK(lhs_item)) { - if (lhs_item == ITER_DONE) - return 1; /* COUNT(LHS) == COUNT(RHS) */ - return -1; - } - Dee_Decref(lhs_item); - return 0; /* COUNT(LHS) > COUNT(RHS) */ -} - -INTERN WUNUSED NONNULL((1, 2)) int DCALL -DeeSeq_EqII(DeeObject *lhs, DeeObject *rhs) { - for (;;) { - int diff; - DREF DeeObject *lhs_item; - DREF DeeObject *rhs_item; - lhs_item = DeeObject_IterNext(lhs); - if unlikely(!ITER_ISOK(lhs_item)) { - if unlikely(lhs_item != ITER_DONE) - return -1; - rhs_item = DeeObject_IterNext(rhs); - if (!ITER_ISOK(rhs_item)) { - if unlikely(rhs_item != ITER_DONE) - return -1; - return 1; /* COUNT(LHS) == COUNT(RHS) */ - } - Dee_Decref(rhs_item); - return 0; /* COUNT(LHS) < COUNT(RHS) */ - } - rhs_item = DeeObject_IterNext(rhs); - if unlikely(!ITER_ISOK(rhs_item)) { - Dee_Decref(lhs_item); - if unlikely(rhs_item != ITER_DONE) - return -1; - return 0; /* COUNT(LHS) > COUNT(RHS) */ - } - diff = DeeObject_TryCmpEqAsBool(lhs_item, rhs_item); - Dee_Decref(rhs_item); - Dee_Decref(lhs_item); - if (diff <= 0) - return diff; - } -} - -INTERN WUNUSED NONNULL((1, 2)) int DCALL -DeeSeq_EqIS(DeeObject *lhs, DeeObject *rhs) { - int result; - size_t rhs_size; - if (DeeTuple_Check(rhs)) { - result = DeeSeq_EqIV(lhs, DeeTuple_ELEM(rhs), DeeTuple_SIZE(rhs)); - } else { - DREF DeeObject *rhs_iter; - rhs_iter = DeeObject_Iter(rhs); - if unlikely(!rhs_iter) { - if (DeeError_Catch(&DeeError_NotImplemented)) - return 0; - return -1; - } - result = DeeSeq_EqII(lhs, rhs_iter); - Dee_Decref(rhs_iter); - } - return result; -} -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - -DECL_END - -#endif /* !GUARD_DEEMON_OBJECTS_SEQ_FUNCTIONS_C */ diff --git a/src/deemon/objects/seq_functions.h b/src/deemon/objects/seq_functions.h index 1433caf66..8699256ad 100644 --- a/src/deemon/objects/seq_functions.h +++ b/src/deemon/objects/seq_functions.h @@ -59,39 +59,6 @@ DECL_BEGIN #define has_noninherited_size(tp, seq) has_noninherited_seqfield(tp, seq, tp_sizeob) #define has_noninherited_bool(tp) has_noninherited_field(tp, tp_cast.tp_bool) -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS -/* Mutable-sequence API */ -INTDEF WUNUSED NONNULL((1)) int DCALL DeeSeq_DelItem(DeeObject *__restrict self, size_t index); -INTDEF WUNUSED NONNULL((1, 3)) int DCALL DeeSeq_SetItem(DeeObject *self, size_t index, DeeObject *value); -INTDEF WUNUSED NONNULL((1, 3)) DREF DeeObject *DCALL DeeSeq_XchItem(DeeObject *self, size_t index, DeeObject *value); -INTDEF WUNUSED NONNULL((1)) int DCALL DeeSeq_DelRange(DeeObject *__restrict self, size_t start, size_t end); -INTDEF WUNUSED NONNULL((1, 4)) int DCALL DeeSeq_SetRange(DeeObject *self, size_t start, size_t end, DeeObject *values); -INTDEF WUNUSED NONNULL((1)) int DCALL DeeSeq_DelRangeN(DeeObject *__restrict self, size_t start); -INTDEF WUNUSED NONNULL((1, 3)) int DCALL DeeSeq_SetRangeN(DeeObject *self, size_t start, DeeObject *values); -INTDEF WUNUSED NONNULL((1, 3)) int DCALL DeeSeq_Insert(DeeObject *self, size_t index, DeeObject *value); -INTDEF WUNUSED NONNULL((1, 3)) int DCALL DeeSeq_InsertAll(DeeObject *self, size_t index, DeeObject *values); -INTDEF WUNUSED NONNULL((1, 2)) int DCALL DeeSeq_Append(DeeObject *self, DeeObject *value); -INTDEF WUNUSED NONNULL((1, 2)) int DCALL DeeSeq_Extend(DeeObject *self, DeeObject *values); -INTDEF WUNUSED NONNULL((1, 2)) int DCALL DeeSeq_InplaceExtend(DREF DeeObject **__restrict p_self, DeeObject *values); -INTDEF WUNUSED NONNULL((1, 2)) int DCALL DeeSeq_InplaceRepeat(DREF DeeObject **__restrict p_self, DeeObject *count); -INTDEF WUNUSED NONNULL((1)) size_t DCALL DeeSeq_Erase(DeeObject *__restrict self, size_t index, size_t count); -INTDEF WUNUSED NONNULL((1)) DREF DeeObject *DCALL DeeSeq_PopItem(DeeObject *__restrict self, Dee_ssize_t index); -INTDEF WUNUSED NONNULL((1, 4)) int DCALL DeeSeq_Remove(DeeObject *self, size_t start, size_t end, DeeObject *elem, DeeObject *key); -INTDEF WUNUSED NONNULL((1, 4)) int DCALL DeeSeq_RRemove(DeeObject *self, size_t start, size_t end, DeeObject *elem, DeeObject *key); -INTDEF WUNUSED NONNULL((1, 4)) size_t DCALL DeeSeq_RemoveAll(DeeObject *self, size_t start, size_t end, DeeObject *elem, DeeObject *key); -INTDEF WUNUSED NONNULL((1, 4)) size_t DCALL DeeSeq_RemoveIf(DeeObject *self, size_t start, size_t end, DeeObject *should); -INTDEF WUNUSED NONNULL((1, 4)) size_t DCALL DeeSeq_Fill(DeeObject *self, size_t start, size_t end, DeeObject *value); - -/* NOTE: Technically, all of these functions can be used on any type of object, - * but all objects derived from `DeeSeq_Type' automatically implement - * all of them as member functions. - * With that in mind, any type implementing the `tp_seq' interface - * with the intention of behaving as an Iterable, should probably - * be derived from `DeeSeq_Type' as this allows usercode to query - * for a general purpose sequence by writing `x is Sequence from deemon' */ -INTDEF WUNUSED NONNULL((1)) size_t DCALL DeeSeq_Size(DeeObject *__restrict self); -INTDEF WUNUSED NONNULL((1)) DREF DeeObject *DCALL DeeSeq_GetItem(DeeObject *__restrict self, size_t index); -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ /* TODO: All of the following also needs to go eventually... */ diff --git a/src/deemon/objects/seq_mutable.c b/src/deemon/objects/seq_mutable.c deleted file mode 100644 index 0a1cb89d9..000000000 --- a/src/deemon/objects/seq_mutable.c +++ /dev/null @@ -1,3639 +0,0 @@ -/* Copyright (c) 2018-2024 Griefer@Work * - * * - * This software is provided 'as-is', without any express or implied * - * warranty. In no event will the authors be held liable for any damages * - * arising from the use of this software. * - * * - * Permission is granted to anyone to use this software for any purpose, * - * including commercial applications, and to alter it and redistribute it * - * freely, subject to the following restrictions: * - * * - * 1. The origin of this software must not be misrepresented; you must not * - * claim that you wrote the original software. If you use this software * - * in a product, an acknowledgement (see the following) in the product * - * documentation is required: * - * Portions Copyright (c) 2018-2024 Griefer@Work * - * 2. Altered source versions must be plainly marked as such, and must not be * - * misrepresented as being the original software. * - * 3. This notice may not be removed or altered from any source distribution. * - */ -#ifndef GUARD_DEEMON_OBJECTS_SEQ_MUTABLE_C -#define GUARD_DEEMON_OBJECTS_SEQ_MUTABLE_C 1 - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "../runtime/runtime_error.h" -#include "../runtime/strings.h" -#include "seq/concat.h" -#include "seq/default-api.h" -#include "seq/repeat.h" -#include "seq/svec.h" -#include "seq_functions.h" - -#undef SSIZE_MAX -#include -#define SSIZE_MAX __SSIZE_MAX__ - -DECL_BEGIN - -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS -INTDEF WUNUSED NONNULL((1, 2, 3)) DREF DeeObject *DCALL -get_generic_attribute(DeeTypeObject *tp_self, DeeObject *self, DeeObject *name); - -/* @return: 0: Call was OK. - * @return: 1: No such attribute. - * @return: -1: Error. */ -PRIVATE WUNUSED NONNULL((1, 2, 3, 5)) DREF DeeObject *DCALL -vcall_generic_attribute(DeeTypeObject *tp_self, DeeObject *self, - char const *name, dhash_t hash, - char const *format, va_list args) { - DREF DeeObject *result; - ASSERT_OBJECT(tp_self); - ASSERT_OBJECT(self); - ASSERT(DeeType_Check(tp_self)); - ASSERT(DeeObject_InstanceOf(self, tp_self)); - if (DeeType_IsClass(tp_self)) { - struct class_attribute *member; - if ((member = DeeType_QueryAttributeStringHash(tp_self, tp_self, name, hash)) != NULL) { - struct class_desc *desc = DeeClass_DESC(tp_self); - return DeeInstance_VCallAttributef(desc, DeeInstance_DESC(desc, self), - self, member, format, args); - } - result = ITER_DONE; - } else { - result = ITER_DONE; - if (tp_self->tp_methods && - (result = DeeType_VCallMethodAttrStringHashf(tp_self, tp_self, self, name, hash, format, args)) != ITER_DONE) - goto done; - if (tp_self->tp_getsets && - (result = DeeType_GetGetSetAttrStringHash(tp_self, tp_self, self, name, hash)) != ITER_DONE) - goto done_call; - if (tp_self->tp_members && - (result = DeeType_GetMemberAttrStringHash(tp_self, tp_self, self, name, hash)) != ITER_DONE) - goto done_call; - } -done: - return result; -done_call: - if likely(result) { - DREF DeeObject *real_result; - real_result = DeeObject_VCallf(result, format, args); - Dee_Decref(result); - result = real_result; - } - return result; -} - -PRIVATE WUNUSED NONNULL((1, 2, 3, 5)) DREF DeeObject * -call_generic_attribute(DeeTypeObject *tp_self, DeeObject *self, - char const *name, dhash_t hash, - char const *format, ...) { - DREF DeeObject *result; - va_list args; - va_start(args, format); - result = vcall_generic_attribute(tp_self, self, name, hash, format, args); - va_end(args); - return result; -} - -PRIVATE WUNUSED NONNULL((1, 2, 3, 4)) DREF DeeObject * -call_generic_attribute_in_range(DeeTypeObject *tp_limit, DeeObject *self, - DeeObject *name, char const *format, - ...) { - DREF DeeObject *result; - DeeTypeObject *iter = Dee_TYPE(self); - va_list args; - DeeTypeMRO mro; - DeeTypeMRO_Init(&mro, iter); - for (;;) { - if (iter->tp_attr) { - if (iter->tp_attr->tp_getattr) { - DREF DeeObject *func; - if (iter->tp_attr->tp_getattr == &instance_getattr) { - func = instance_tgetattr(iter, self, name); - } else { - func = (*iter->tp_attr->tp_getattr)(self, name); - } - if unlikely(!func) - goto err; - va_start(args, format); - result = DeeObject_VCallf(func, format, args); - va_end(args); - Dee_Decref(func); - return result; - } - break; - } else { - /* Try to invoke a generic attribute. */ - va_start(args, format); - result = vcall_generic_attribute(iter, - self, - DeeString_STR(name), - DeeString_Hash(name), - format, - args); - va_end(args); - if (result != ITER_DONE) - return result; - } - if (iter == tp_limit) - break; - iter = DeeTypeMRO_Next(&mro, iter); - } - return ITER_DONE; -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1, 2, 3)) DREF DeeObject * -call_generic_attribute_anywhere(DeeObject *self, DeeObject *name, - char const *format, ...) { - DREF DeeObject *result; - DeeTypeObject *iter = Dee_TYPE(self); - va_list args; - DeeTypeMRO mro; - DeeTypeMRO_Init(&mro, iter); - for (;;) { - if (iter->tp_attr) { - if (iter->tp_attr->tp_getattr) { - DREF DeeObject *func; - if (iter->tp_attr->tp_getattr == &instance_getattr) { - func = instance_tgetattr(iter, self, name); - } else { - func = (*iter->tp_attr->tp_getattr)(self, name); - } - if unlikely(!func) - goto err; - va_start(args, format); - result = DeeObject_VCallf(func, format, args); - va_end(args); - Dee_Decref(func); - return result; - } - break; - } else { - /* Try to invoke a generic attribute. */ - va_start(args, format); - result = vcall_generic_attribute(iter, - self, - DeeString_STR(name), - DeeString_Hash(name), - format, - args); - va_end(args); - if (result != ITER_DONE) - return result; - } - iter = DeeTypeMRO_Next(&mro, iter); - if (!iter) - break; - } - return ITER_DONE; -err: - return NULL; -} - - -INTERN WUNUSED NONNULL((1)) int DCALL -DeeSeq_DelItem(DeeObject *__restrict self, size_t index) { - int result; - DeeTypeObject *tp_self = Dee_TYPE(self); - bool found_attributes = false; - DeeTypeMRO mro; - DeeTypeMRO_Init(&mro, tp_self); - while (tp_self != &DeeSeq_Type) { - struct type_seq *seq = tp_self->tp_seq; - if (seq) { - struct type_nsi const *nsi = seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ && - is_noninherited_nsi(tp_self, seq, nsi)) { - /* Check for NSI-optimized variants */ - if (nsi->nsi_seqlike.nsi_delitem) - return (*nsi->nsi_seqlike.nsi_delitem)(self, index); - if (nsi->nsi_seqlike.nsi_setrange) { - size_t my_length = (*nsi->nsi_seqlike.nsi_getsize)(self); - if unlikely(my_length == (size_t)-1) - goto err; - if unlikely(index >= my_length) - return err_index_out_of_bounds(self, index, my_length); - return (*nsi->nsi_seqlike.nsi_setrange)(self, - index, - index + 1, - Dee_None); - } - } - if (has_noninherited_delitem(tp_self, seq)) { - /* Try to invoke the native delitem operator. */ - DREF DeeObject *index_ob; - index_ob = DeeInt_NewSize(index); - if unlikely(!index_ob) - goto err; - result = (*seq->tp_delitem)(self, index_ob); - Dee_Decref(index_ob); - return result; - } - } - if (!found_attributes) { - if (tp_self->tp_attr) { - if (tp_self->tp_attr->tp_getattr) { - DREF DeeObject *erase_function, *erase_result; - if (tp_self->tp_attr->tp_getattr == &instance_getattr) { - erase_function = instance_tgetattr(tp_self, self, (DeeObject *)&str_erase); - } else { - erase_function = (*tp_self->tp_attr->tp_getattr)(self, (DeeObject *)&str_erase); - } - if unlikely(!erase_function) - goto err_bad_attribute; - /* Found an erase() function. -> Now call it! */ - erase_result = DeeObject_Callf(erase_function, PCKuSIZ "u", index, 1u); - Dee_Decref(erase_function); - if unlikely(!erase_result) - goto err_bad_attribute; - Dee_Decref(erase_result); - return 0; - } -did_find_attributes: - found_attributes = true; - } else { - /* Try to invoke a generic attribute. */ - DREF DeeObject *callback_result; - callback_result = call_generic_attribute(tp_self, self, STR_erase, - DeeString_Hash((DeeObject *)&str_erase), - PCKuSIZ "u", index, 1u); - if (callback_result != ITER_DONE) { - if unlikely(!callback_result) - goto err_bad_attribute; - /* Invocation was successful */ - Dee_Decref(callback_result); - return 0; - } - } - } - if (seq && - (has_noninherited_delrange(tp_self, seq) || - has_noninherited_setrange(tp_self, seq))) { - /* Try to implement delitem using delrange or setrange. */ - DREF DeeObject *start_index, *end_index; - size_t mylen; - if (seq->tp_nsi && - is_noninherited_nsi(tp_self, seq, seq->tp_nsi)) { - mylen = (*seq->tp_nsi->nsi_common.nsi_getsize)(self); - } else { - mylen = DeeObject_Size(self); - } - if unlikely(mylen == (size_t)-1) - goto err; - start_index = DeeInt_NewSize(index); - if unlikely(!start_index) - goto err; - end_index = DeeInt_NewSize(index + 1); - if unlikely(!end_index) { - Dee_Decref(start_index); - goto err; - } - if (has_noninherited_delrange(tp_self, seq)) { - result = (*seq->tp_delrange)(self, start_index, end_index); - } else { - result = (*seq->tp_setrange)(self, start_index, end_index, Dee_None); - } - Dee_Decref(end_index); - Dee_Decref(start_index); - return result; - } - /*if (seq) - break;*/ - if ((tp_self = DeeTypeMRO_Next(&mro, tp_self)) == NULL) - break; - } - return err_fixedlength_sequence(self); -err_bad_attribute: - if (DeeError_Catch(&DeeError_NotImplemented) || - DeeError_Catch(&DeeError_AttributeError)) - goto did_find_attributes; -err: - return -1; -} - -INTERN WUNUSED NONNULL((1, 3)) int DCALL -DeeSeq_SetItem(DeeObject *self, size_t index, DeeObject *value) { - int result; - DeeTypeObject *tp_self = Dee_TYPE(self); - DeeTypeMRO mro; - DeeTypeMRO_Init(&mro, tp_self); - while (tp_self != &DeeSeq_Type) { - struct type_seq *seq = tp_self->tp_seq; - if (seq) { - struct type_nsi const *nsi = seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ && - is_noninherited_nsi(tp_self, seq, nsi)) { - /* Check for NSI-optimized variants */ - if (nsi->nsi_seqlike.nsi_setitem) - return (*nsi->nsi_seqlike.nsi_setitem)(self, index, value); - if (nsi->nsi_seqlike.nsi_setrange) { - size_t my_length = (*nsi->nsi_seqlike.nsi_getsize)(self); - DREF DeeObject *value_seq; - if unlikely(my_length == (size_t)-1) - goto err; - if unlikely(index >= my_length) - return err_index_out_of_bounds(self, index, my_length); - value_seq = DeeTuple_Pack(1, value); - if unlikely(!value_seq) - goto err; - Dee_Incref(value); - result = (*nsi->nsi_seqlike.nsi_setrange)(self, - index, - index + 1, - value_seq); - Dee_Decref(value_seq); - return result; - } - } - if (has_noninherited_setitem(tp_self, seq)) { - /* Try to invoke the native delitem operator. */ - DREF DeeObject *index_ob; - index_ob = DeeInt_NewSize(index); - if unlikely(!index_ob) - goto err; - result = (*seq->tp_setitem)(self, index_ob, value); - Dee_Decref(index_ob); - return result; - } - if (has_noninherited_setrange(tp_self, seq)) { - /* Try to implement setitem using setrange. */ - DREF DeeObject *start_index, *end_index; - DREF DeeObject *value_seq; - size_t mylen; - mylen = nsi ? (*nsi->nsi_common.nsi_getsize)(self) : DeeObject_Size(self); - if unlikely(mylen == (size_t)-1) - goto err; - start_index = DeeInt_NewSize(index); - if unlikely(!start_index) - goto err; - end_index = DeeInt_NewSize(index + 1); - if unlikely(!end_index) { -err_start_index: - Dee_Decref(start_index); - goto err; - } - value_seq = DeeTuple_Pack(1, value); - if unlikely(!value_seq) { - Dee_Decref(end_index); - goto err_start_index; - } - result = (*seq->tp_setrange)(self, start_index, end_index, value_seq); - Dee_Decref(value_seq); - Dee_Decref(end_index); - Dee_Decref(start_index); - return result; - } - } - if ((tp_self = DeeTypeMRO_Next(&mro, tp_self)) == NULL) - break; - } - return err_immutable_sequence(self); -err: - return -1; -} - -INTERN WUNUSED NONNULL((1, 3)) DREF DeeObject *DCALL -DeeSeq_XchItem(DeeObject *self, size_t index, DeeObject *value) { - DREF DeeObject *result; - int error; - DeeTypeObject *tp_self = Dee_TYPE(self); - DeeTypeMRO mro; - DeeTypeMRO_Init(&mro, tp_self); - while (tp_self != &DeeSeq_Type) { - struct type_seq *seq = tp_self->tp_seq; - if (seq) { - struct type_nsi const *nsi = seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ && - is_noninherited_nsi(tp_self, seq, nsi)) { - /* Check for NSI-optimized variants */ - if (nsi->nsi_seqlike.nsi_getitem) { - if (nsi->nsi_seqlike.nsi_setitem) { - result = (*nsi->nsi_seqlike.nsi_getitem)(self, index); - if unlikely(!result) - goto err; - error = (*nsi->nsi_seqlike.nsi_setitem)(self, index, value); - if unlikely(error) - goto err_r; - return result; - } - if (nsi->nsi_seqlike.nsi_setrange) { - size_t my_length = (*nsi->nsi_seqlike.nsi_getsize)(self); - DREF DeeObject *value_seq; - if unlikely(my_length == (size_t)-1) - goto err; - if unlikely(index >= my_length) { - err_index_out_of_bounds(self, index, my_length); - goto err; - } - result = (*nsi->nsi_seqlike.nsi_getitem)(self, index); - if unlikely(!result) - goto err; - value_seq = DeeTuple_Pack(1, value); - if unlikely(!value_seq) - goto err_r; - error = (*nsi->nsi_seqlike.nsi_setrange)(self, - index, - index + 1, - value_seq); - Dee_Decref(value_seq); - if unlikely(error) - goto err_r; - return result; - } - } - if (nsi->nsi_seqlike.nsi_getrange) { - if (nsi->nsi_seqlike.nsi_setitem) { - DREF DeeObject *real_result; - result = (*nsi->nsi_seqlike.nsi_getrange)(self, index, index + 1); - if unlikely(!result) - goto err; - error = (*nsi->nsi_seqlike.nsi_setitem)(self, index, value); - if unlikely(error) - goto err_r; -return_result_first: - real_result = default_seq_getfirst(result); - Dee_Decref(result); - if unlikely(!real_result) { - /* Translate the empty-sequence error into an index-out-of-bounds */ - if (DeeError_Catch(&DeeError_ValueError)) { - size_t mylen = DeeObject_Size(self); - if unlikely(mylen == (size_t)-1) - goto err; - err_index_out_of_bounds(self, index, mylen); - } - } - return real_result; - } - if (nsi->nsi_seqlike.nsi_setrange) { - size_t my_length = (*nsi->nsi_seqlike.nsi_getsize)(self); - DREF DeeObject *value_seq; - if unlikely(my_length == (size_t)-1) - goto err; - if unlikely(index >= my_length) { - err_index_out_of_bounds(self, index, my_length); - goto err; - } - result = (*nsi->nsi_seqlike.nsi_getrange)(self, index, index + 1); - if unlikely(!result) - goto err; - value_seq = DeeTuple_Pack(1, value); - if unlikely(!value_seq) - goto err_r; - error = (*nsi->nsi_seqlike.nsi_setrange)(self, - index, - index + 1, - value_seq); - Dee_Decref(value_seq); - if unlikely(error) - goto err_r; - goto return_result_first; - } - } - } - if (has_noninherited_setitem(tp_self, seq)) { - DREF DeeObject *index_ob; - index_ob = DeeInt_NewSize(index); - if unlikely(!index_ob) - goto err; - result = DeeObject_GetItem(self, index_ob); - if unlikely(!result) { - Dee_Decref(index_ob); - goto err; - } - error = (*seq->tp_setitem)(self, index_ob, value); - Dee_Decref(index_ob); - if unlikely(error) - goto err_r; - return result; - } - if (has_noninherited_setrange(tp_self, seq)) { - DREF DeeObject *index_ob, *index_plus1_ob; - index_ob = DeeInt_NewSize(index); - if unlikely(!index_ob) - goto err; - result = DeeObject_GetItem(self, index_ob); - if unlikely(!result) { - Dee_Decref(index_ob); - goto err; - } - index_plus1_ob = DeeInt_NewSize(index + 1); - if unlikely(!index_plus1_ob) { - Dee_Decref(index_ob); - goto err_r; - } - error = (*seq->tp_setrange)(self, index_ob, index_plus1_ob, value); - Dee_Decref(index_plus1_ob); - Dee_Decref(index_ob); - if unlikely(error) - goto err_r; - return result; - } - } - if ((tp_self = DeeTypeMRO_Next(&mro, tp_self)) == NULL) - break; - } - err_immutable_sequence(self); -err: - return NULL; -err_r: - Dee_Decref(result); - goto err; -} - -INTERN WUNUSED NONNULL((1)) int DCALL -DeeSeq_DelRange(DeeObject *__restrict self, size_t start, size_t end) { - int result; - DeeTypeObject *tp_self = Dee_TYPE(self); - DeeTypeMRO mro; - DeeTypeMRO_Init(&mro, tp_self); - while (tp_self != &DeeSeq_Type) { - struct type_seq *seq = tp_self->tp_seq; - if (seq) { - struct type_nsi const *nsi = seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ && - is_noninherited_nsi(tp_self, seq, nsi)) { - /* Check for NSI-optimized variants */ - if (nsi->nsi_seqlike.nsi_delrange) - return (*nsi->nsi_seqlike.nsi_delrange)(self, start, end); - if (nsi->nsi_seqlike.nsi_delitem) { - size_t mylen; - mylen = (*nsi->nsi_seqlike.nsi_getsize)(self); - if unlikely(mylen == (size_t)-1) - goto err; - if (end > mylen) - end = mylen; - while (end > start) { - --end; - if ((*nsi->nsi_seqlike.nsi_delitem)(self, end)) - goto err; - } - return 0; - } - } - if (has_noninherited_delrange(tp_self, seq)) { - /* Try to implement delitem using delrange. */ - DREF DeeObject *start_index, *end_index; - start_index = DeeInt_NewSize(start); - if unlikely(!start_index) - goto err; - end_index = DeeInt_NewSize(end); - if unlikely(!end_index) { - Dee_Decref(start_index); - goto err; - } - result = (*seq->tp_delrange)(self, start_index, end_index); - Dee_Decref(end_index); - Dee_Decref(start_index); - return result; - } - { - size_t mylen; - DREF DeeObject *erase_result; - mylen = nsi ? (*nsi->nsi_common.nsi_getsize)(self) : DeeObject_Size(self); - if unlikely(mylen == (size_t)-1) - goto err; - /* Search for a type implementing an `erase()' function. */ - if (start > mylen) - start = mylen; - if (end < start) - end = start; - end -= start; - erase_result = call_generic_attribute_in_range(tp_self, - self, - (DeeObject *)&str_erase, - PCKuSIZ PCKuSIZ, - start, - end); - if (erase_result != ITER_DONE) { - if unlikely(!erase_result) - goto err_bad_attribute; - Dee_Decref(erase_result); - return 0; - } - /* Last chance: Use `operator del[]' to erase each item individually. */ - if (has_noninherited_delitem(tp_self, seq)) { - while (end--) { - DREF DeeObject *index_ob; - index_ob = DeeInt_NewSize(start + end); - if unlikely(!index_ob) - goto err; - result = (*seq->tp_delitem)(self, index_ob); - Dee_Decref(index_ob); - if unlikely(result) - goto err; - } - return 0; - } - } - } - if ((tp_self = DeeTypeMRO_Next(&mro, tp_self)) == NULL) - break; - } -is_immutable: - return err_immutable_sequence(self); -err_bad_attribute: - if (DeeError_Catch(&DeeError_NotImplemented) || - DeeError_Catch(&DeeError_AttributeError)) - goto is_immutable; -err: - return -1; -} - -INTERN WUNUSED NONNULL((1)) int DCALL -DeeSeq_DelRangeN(DeeObject *__restrict self, size_t start) { - int result; - DeeTypeObject *tp_self = Dee_TYPE(self); - DeeTypeMRO mro; - DeeTypeMRO_Init(&mro, tp_self); - while (tp_self != &DeeSeq_Type) { - struct type_seq *seq = tp_self->tp_seq; - if (start == 0) { - DREF DeeObject *callback_result; - callback_result = call_generic_attribute(tp_self, self, STR_clear, - DeeString_Hash((DeeObject *)&str_clear), - ""); - if (callback_result != ITER_DONE) { - if unlikely(!callback_result) - goto err_bad_attribute; - Dee_Decref(callback_result); - return 0; - } - } - if (seq) { - struct type_nsi const *nsi = seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ && - is_noninherited_nsi(tp_self, seq, nsi)) { - /* Check for NSI-optimized variants */ - if (nsi->nsi_seqlike.nsi_delrange_n) - return (*nsi->nsi_seqlike.nsi_delrange_n)(self, start); - if (nsi->nsi_seqlike.nsi_delrange) - return (*nsi->nsi_seqlike.nsi_delrange)(self, start, SSIZE_MAX); - if (nsi->nsi_seqlike.nsi_delitem) { - size_t mylen; - mylen = (*nsi->nsi_seqlike.nsi_getsize)(self); - if unlikely(mylen == (size_t)-1) - goto err; - while (mylen > start) { - --mylen; - if ((*nsi->nsi_seqlike.nsi_delitem)(self, mylen)) - goto err; - } - return 0; - } - } - if (has_noninherited_delrange(tp_self, seq)) { - /* Try to implement delitem using delrange. */ - DREF DeeObject *start_index; - start_index = DeeInt_NewSize(start); - if unlikely(!start_index) - goto err; - result = (*seq->tp_delrange)(self, start_index, Dee_None); - Dee_Decref(start_index); - return result; - } - { - size_t mylen; - DREF DeeObject *erase_result; - mylen = nsi ? (*nsi->nsi_common.nsi_getsize)(self) : DeeObject_Size(self); - if unlikely(mylen == (size_t)-1) - goto err; - /* Search for a type implementing an `erase()' function. */ - if (start > mylen) - start = mylen; - mylen -= start; - erase_result = call_generic_attribute_in_range(tp_self, - self, - (DeeObject *)&str_erase, - PCKuSIZ PCKuSIZ, - start, - mylen); - if (erase_result != ITER_DONE) { - if unlikely(!erase_result) - goto err_bad_attribute; - Dee_Decref(erase_result); - return 0; - } - /* Last chance: Use `operator del[]' to erase each item individually. */ - if (has_noninherited_delitem(tp_self, seq)) { - while (mylen--) { - DREF DeeObject *index_ob; - index_ob = DeeInt_NewSize(start + mylen); - if unlikely(!index_ob) - goto err; - result = (*seq->tp_delitem)(self, index_ob); - Dee_Decref(index_ob); - if unlikely(result) - goto err; - } - return 0; - } - } - } - if ((tp_self = DeeTypeMRO_Next(&mro, tp_self)) == NULL) - break; - } -is_immutable: - return err_immutable_sequence(self); -err_bad_attribute: - if (DeeError_Catch(&DeeError_NotImplemented) || - DeeError_Catch(&DeeError_AttributeError)) - goto is_immutable; -err: - return -1; -} - - -INTDEF WUNUSED NONNULL((1)) DREF DeeObject *DCALL IteratorPending_For(DeeObject *__restrict self); - -INTERN WUNUSED NONNULL((1, 4)) int DCALL -DeeSeq_SetRange(DeeObject *self, size_t start, size_t end, - DeeObject *values) { - int result; - DeeTypeObject *tp_self = Dee_TYPE(self); - DREF DeeObject *values_iterator; - DREF DeeObject *callback_result; - int values_is_empty; - DeeTypeMRO mro; - if (start >= end) { - /* Use insertall() */ - size_t mylen = DeeObject_Size(self); - if unlikely(mylen == (size_t)-1) - goto err; - if (start > mylen) - start = mylen; - callback_result = call_generic_attribute_anywhere(self, - (DeeObject *)&str_insertall, - PCKuSIZ "o", - start, - values); - if (!ITER_ISOK(callback_result)) { - if (!callback_result) { - if (DeeError_Catch(&DeeError_NotImplemented) || - DeeError_Catch(&DeeError_AttributeError)) { - size_t values_len; - values_len = DeeObject_Size(values); - if unlikely(values_len == (size_t)-1) - goto err; - if (values_len == 0) - return 0; - goto not_resizable; - } - goto err; - } - goto is_immutable; - } - Dee_Decref(callback_result); - return 0; - } - values_is_empty = -1; - DeeTypeMRO_Init(&mro, tp_self); - while (tp_self != &DeeSeq_Type) { - struct type_seq *seq = tp_self->tp_seq; - if (seq) { - struct type_nsi const *nsi = seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ && - is_noninherited_nsi(tp_self, seq, nsi)) { - if (nsi->nsi_seqlike.nsi_setrange) - return (*nsi->nsi_seqlike.nsi_setrange)(self, start, end, values); - } - if (has_noninherited_setrange(tp_self, seq)) { - DREF DeeObject *start_index, *end_index; - start_index = DeeInt_NewSize(start); - if unlikely(!start_index) - goto err; - end_index = DeeInt_NewSize(end); - if unlikely(!end_index) { - Dee_Decref(start_index); - goto err; - } - result = (*seq->tp_setrange)(self, start_index, end_index, values); - Dee_Decref(end_index); - Dee_Decref(start_index); - return result; - } - if (has_noninherited_setitem(tp_self, seq)) { - DREF DeeObject *future; - size_t mylen; - mylen = DeeObject_Size(self); - if unlikely(mylen == (size_t)-1) - goto err; - if (start > mylen) - start = mylen; - if (end < start) - end = start; - values_iterator = DeeObject_Iter(values); - if unlikely(!values_iterator) - goto err; - /* Override existing / Delete trailing */ - while (start < end) { - DREF DeeObject *index_ob, *elem; - elem = DeeObject_IterNext(values_iterator); - if (!ITER_ISOK(elem)) { - if unlikely(!elem) - goto err_valiter; - /* Erase all the remaining indices. */ - Dee_Decref(values_iterator); - callback_result = call_generic_attribute_in_range(tp_self, - self, - (DeeObject *)&str_erase, - PCKuSIZ PCKuSIZ, - start, - end - start); - if (!ITER_ISOK(callback_result)) { - if unlikely(!callback_result) - goto err_attr; - goto not_resizable; - } - Dee_Decref(callback_result); - return 0; - } - index_ob = DeeInt_NewSize(start); - if unlikely(!index_ob) { - Dee_Decref(elem); - goto err_valiter; - } - result = (*seq->tp_setitem)(self, index_ob, elem); - Dee_Decref(index_ob); - Dee_Decref(elem); - if unlikely(result) - goto err; - ++start; - if (DeeThread_CheckInterrupt()) - goto err; - } - ASSERT(start == end); - /* Insert the remainder. */ - future = IteratorPending_For(values_iterator); - if unlikely(!future) - goto err_valiter; - callback_result = call_generic_attribute_in_range(tp_self, - self, - (DeeObject *)&str_insertall, - PCKuSIZ "o", - start, - future); - Dee_Decref(future); - if (!ITER_ISOK(callback_result)) { - DREF DeeObject *trailing; - if unlikely(!callback_result) - goto err_valiter; - trailing = DeeObject_IterNext(values_iterator); - Dee_Decref(values_iterator); - if (trailing == ITER_DONE) - return 0; /* Empty input iterator -> the caller didn't actually want to re-size the */ - if (!trailing) - goto err; - Dee_Decref(trailing); - goto not_resizable; - } - Dee_Decref(callback_result); - return 0; - } - } - if (values_is_empty < 0) { - size_t temp = DeeObject_Size(values); - if unlikely(temp == (size_t)-1) - goto err; - values_is_empty = temp == 0; - } - if (values_is_empty) { - /* Empty values! */ - if (seq) { - struct type_nsi const *nsi = seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ && - is_noninherited_nsi(tp_self, seq, nsi)) { - ASSERT(!nsi->nsi_seqlike.nsi_setrange); - if (nsi->nsi_seqlike.nsi_delitem) { - do { - --end; - if unlikely((*nsi->nsi_seqlike.nsi_delitem)(self, end)) - goto err; - } while (end > start); - return 0; - } - } - if (has_noninherited_delrange(tp_self, seq)) { - DREF DeeObject *start_ob, *end_ob; - start_ob = DeeInt_NewSize(start); - if unlikely(!start_ob) - goto err; - end_ob = DeeInt_NewSize(end); - if unlikely(!end_ob) { - Dee_Decref(start_ob); - goto err; - } - result = (*seq->tp_delrange)(self, start_ob, end_ob); - Dee_Decref(end_ob); - Dee_Decref(start_ob); - return result; - } - if (has_noninherited_delitem(tp_self, seq)) { - do { - DREF DeeObject *index_ob; - --end; - index_ob = DeeInt_NewSize(end); - if unlikely(!index_ob) - goto err; - result = (*seq->tp_delitem)(self, index_ob); - Dee_Decref(index_ob); - if unlikely(result) - break; - } while (end > start); - return result; - } - } - } - if ((tp_self = DeeTypeMRO_Next(&mro, tp_self)) == NULL) - break; - } -is_immutable: - return err_immutable_sequence(self); -not_resizable: - return err_fixedlength_sequence(self); -err_attr: - if (DeeError_Catch(&DeeError_NotImplemented) || - DeeError_Catch(&DeeError_AttributeError)) - goto is_immutable; - goto err; -err_valiter: - Dee_Decref(values_iterator); -err: - return -1; -} - -INTERN WUNUSED NONNULL((1, 3)) int DCALL -DeeSeq_SetRangeN(DeeObject *self, size_t start, - DeeObject *values) { - int result; - DeeTypeObject *tp_self = Dee_TYPE(self); - DREF DeeObject *values_iterator; - DREF DeeObject *callback_result; - size_t mylen = DeeObject_Size(self); - DeeTypeMRO mro; - if unlikely(mylen == (size_t)-1) - goto err; - if (start >= mylen) { - /* Use insertall() */ - callback_result = call_generic_attribute_anywhere(self, - (DeeObject *)&str_insertall, - PCKuSIZ "o", - mylen, - values); - if (!ITER_ISOK(callback_result)) { - if (!callback_result) { - if (DeeError_Catch(&DeeError_NotImplemented) || - DeeError_Catch(&DeeError_AttributeError)) { - size_t values_len; - values_len = DeeObject_Size(values); - if unlikely(values_len == (size_t)-1) - goto err; - if (values_len == 0) - return 0; - goto not_resizable; - } - goto err; - } - goto is_immutable; - } - Dee_Decref(callback_result); - return 0; - } - DeeTypeMRO_Init(&mro, tp_self); - while (tp_self != &DeeSeq_Type) { - struct type_seq *seq = tp_self->tp_seq; - if (seq) { - struct type_nsi const *nsi = seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ && - is_noninherited_nsi(tp_self, seq, nsi)) { - if (nsi->nsi_seqlike.nsi_setrange_n) - return (*nsi->nsi_seqlike.nsi_setrange_n)(self, start, values); - if (nsi->nsi_seqlike.nsi_setrange) - return (*nsi->nsi_seqlike.nsi_setrange)(self, start, mylen, values); - } - if (has_noninherited_setrange(tp_self, seq)) { - DREF DeeObject *start_index; - start_index = DeeInt_NewSize(start); - if unlikely(!start_index) - goto err; - result = (*seq->tp_setrange)(self, start_index, Dee_None, values); - Dee_Decref(start_index); - return result; - } - if (has_noninherited_setitem(tp_self, seq)) { - DREF DeeObject *future; - values_iterator = DeeObject_Iter(values); - if unlikely(!values_iterator) - goto err; - /* Override existing / Delete trailing */ - while (start < mylen) { - DREF DeeObject *index_ob, *elem; - elem = DeeObject_IterNext(values_iterator); - if (!ITER_ISOK(elem)) { - if unlikely(!elem) - goto err_valiter; - /* Erase all the remaining indices. */ - Dee_Decref(values_iterator); - callback_result = call_generic_attribute_in_range(tp_self, - self, - (DeeObject *)&str_erase, - PCKuSIZ PCKuSIZ, - start, - mylen - start); - if (!ITER_ISOK(callback_result)) { - if unlikely(!callback_result) - goto err_attr; - goto not_resizable; - } - Dee_Decref(callback_result); - return 0; - } - index_ob = DeeInt_NewSize(start); - if unlikely(!index_ob) { - Dee_Decref(elem); - goto err_valiter; - } - result = (*seq->tp_setitem)(self, index_ob, elem); - Dee_Decref(index_ob); - Dee_Decref(elem); - if unlikely(result) - goto err; - ++start; - if (DeeThread_CheckInterrupt()) - goto err; - } - ASSERT(start == mylen); - /* Insert the remainder. */ - future = IteratorPending_For(values_iterator); - if unlikely(!future) - goto err_valiter; - callback_result = call_generic_attribute_in_range(tp_self, - self, - (DeeObject *)&str_insertall, - PCKuSIZ "o", - start, - future); - Dee_Decref(future); - if (!ITER_ISOK(callback_result)) { - DREF DeeObject *trailing; - if unlikely(!callback_result) - goto err_valiter; - trailing = DeeObject_IterNext(values_iterator); - Dee_Decref(values_iterator); - if (trailing == ITER_DONE) - return 0; /* Empty input iterator -> the caller didn't actually want to re-size the */ - if (!trailing) - goto err; - Dee_Decref(trailing); - goto not_resizable; - } - Dee_Decref(callback_result); - return 0; - } - } - if ((tp_self = DeeTypeMRO_Next(&mro, tp_self)) == NULL) - break; - } -is_immutable: - return err_immutable_sequence(self); -not_resizable: - return err_fixedlength_sequence(self); -err_attr: - if (DeeError_Catch(&DeeError_NotImplemented) || - DeeError_Catch(&DeeError_AttributeError)) - goto is_immutable; - goto err; -err_valiter: - Dee_Decref(values_iterator); -err: - return -1; -} - - - -INTERN WUNUSED NONNULL((1, 3)) int DCALL -DeeSeq_Insert(DeeObject *self, size_t index, - DeeObject *value) { - int result; - DeeTypeObject *tp_self = Dee_TYPE(self); - DeeTypeMRO mro; - DeeTypeMRO_Init(&mro, tp_self); - while (tp_self != &DeeSeq_Type) { - struct type_seq *seq = tp_self->tp_seq; - if (seq) { - struct type_nsi const *nsi = seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ && - is_noninherited_nsi(tp_self, seq, nsi)) { - if (nsi->nsi_seqlike.nsi_setrange) { - DREF DeeObject *value_seq; - value_seq = DeeTuple_Pack(1, value); - if unlikely(!value_seq) - goto err; - if ((dssize_t)index < 0) - index = SSIZE_MAX; - result = (*nsi->nsi_seqlike.nsi_setrange)(self, index, index, (DeeObject *)value_seq); - Dee_Decref(value_seq); - return result; - } - } - } - if ((dssize_t)index < 0) { - /* Use append / extend. */ - DREF DeeObject *callback_result; - callback_result = call_generic_attribute(tp_self, self, STR_append, - DeeString_Hash((DeeObject *)&str_append), - "o", value); - if (callback_result == ITER_DONE) { - callback_result = call_generic_attribute(tp_self, self, STR_extend, - DeeString_Hash((DeeObject *)&str_extend), - "(o)", value); - } - if (callback_result != ITER_DONE) { - if unlikely(!callback_result) - goto err_attr; - Dee_Decref(callback_result); - return 0; - } - } - { - DREF DeeObject *callback_result; - callback_result = call_generic_attribute(tp_self, self, STR_insertall, - DeeString_Hash((DeeObject *)&str_insertall), - PCKuSIZ "(o)", index, value); - if (callback_result != ITER_DONE) { - if unlikely(!callback_result) - goto err_attr; - Dee_Decref(callback_result); - return 0; - } - } - if (seq && has_noninherited_setrange(tp_self, seq)) { - DREF DeeObject *index_ob, *value_seq; - if ((dssize_t)index < 0) - index = SSIZE_MAX; - index_ob = DeeInt_NewSize(index); - if unlikely(!index_ob) - goto err; - value_seq = DeeTuple_Pack(1, value); - if unlikely(!value_seq) { - Dee_Decref(index_ob); - goto err; - } - result = (*seq->tp_setrange)(self, index_ob, index_ob, (DeeObject *)value_seq); - Dee_Decref(value_seq); - Dee_Decref(index_ob); - return result; - } - if (tp_self->tp_init.tp_assign && (dssize_t)index < 0) { - DREF DeeObject *items, *concat; - items = DeeTuple_Pack(1, value); - if unlikely(!items) - goto err; - concat = DeeSeq_Concat(self, items); - Dee_Decref(items); - if unlikely(!concat) - goto err; - result = (*tp_self->tp_init.tp_assign)(self, concat); - Dee_Decref(concat); - return result; - } - if ((tp_self = DeeTypeMRO_Next(&mro, tp_self)) == NULL) - break; - } -not_resizable: - return err_fixedlength_sequence(self); -err_attr: - if (DeeError_Catch(&DeeError_NotImplemented) || - DeeError_Catch(&DeeError_AttributeError)) - goto not_resizable; - goto err; -err: - return -1; -} - -INTERN WUNUSED NONNULL((1, 3)) int DCALL -DeeSeq_InsertAll(DeeObject *self, size_t index, - DeeObject *values) { - int result; - DeeTypeObject *tp_self = Dee_TYPE(self); - DeeTypeMRO mro; - DeeTypeMRO_Init(&mro, tp_self); - while (tp_self != &DeeSeq_Type) { - struct type_seq *seq = tp_self->tp_seq; - if (seq) { - struct type_nsi const *nsi = seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ && - is_noninherited_nsi(tp_self, seq, nsi)) { - if (nsi->nsi_seqlike.nsi_setrange) { - if ((dssize_t)index < 0) - index = SSIZE_MAX; - return (*nsi->nsi_seqlike.nsi_setrange)(self, index, index, values); - } - } - } - if ((dssize_t)index < 0) { - /* Try to call `extend()' */ - DREF DeeObject *callback_result; - callback_result = call_generic_attribute(tp_self, self, STR_extend, - DeeString_Hash((DeeObject *)&str_extend), - "o", values); - if (callback_result != ITER_DONE) { - if unlikely(!callback_result) - goto err_attr; - Dee_Decref(callback_result); - return 0; - } - } - if (seq && has_noninherited_setrange(tp_self, seq)) { - DREF DeeObject *index_ob; - if ((dssize_t)index < 0) - index = SSIZE_MAX; - index_ob = DeeInt_NewSize(index); - if unlikely(!index_ob) - goto err; - result = (*seq->tp_setrange)(self, index_ob, index_ob, values); - Dee_Decref(index_ob); - return result; - } - if ((dssize_t)index < 0) { - DREF DeeObject *append_function; - append_function = get_generic_attribute(tp_self, - self, - (DeeObject *)&str_append); - if (append_function != ITER_DONE) { - size_t i, fast_size; - DREF DeeObject *iterator, *elem; - if unlikely(!append_function) - goto err_attr; - /* Use the append function to append everything */ - fast_size = DeeFastSeq_GetSize_deprecated(values); - if (fast_size != DEE_FASTSEQ_NOTFAST_DEPRECATED) { - for (i = 0; i < fast_size; ++i) { - DREF DeeObject *callback_result; - elem = DeeFastSeq_GetItem_deprecated(values, i); - if unlikely(!elem) - goto err_append_function; - callback_result = DeeObject_Call(append_function, 1, &elem); - Dee_Decref(elem); - if unlikely(!callback_result) - goto err_append_function; - Dee_Decref(callback_result); - } - Dee_Decref(append_function); - return 0; - } - iterator = DeeObject_Iter(values); - if unlikely(!iterator) - goto err_append_function; - while (ITER_ISOK(elem = DeeObject_IterNext(iterator))) { - DREF DeeObject *callback_result; - callback_result = DeeObject_Call(append_function, 1, &elem); - Dee_Decref(elem); - if unlikely(!callback_result) - goto err_append_function_iterator; - Dee_Decref(callback_result); - if (DeeThread_CheckInterrupt()) - goto err_append_function_iterator; - } - if unlikely(!elem) - goto err_append_function_iterator; - Dee_Decref(iterator); - Dee_Decref(append_function); - return 0; -err_append_function_iterator: - Dee_Decref(iterator); -err_append_function: - Dee_Decref(append_function); - goto err; - } - } - { - DREF DeeObject *insert_function; - insert_function = get_generic_attribute(tp_self, - self, - (DeeObject *)&str_insert); - if (insert_function != ITER_DONE) { - size_t i, fast_size; - DREF DeeObject *iterator, *elem; - if unlikely(!insert_function) - goto err_attr; - if (index > SSIZE_MAX) - index = SSIZE_MAX; - /* Use the insert function to insert everything */ - fast_size = DeeFastSeq_GetSize_deprecated(values); - if (fast_size != DEE_FASTSEQ_NOTFAST_DEPRECATED) { - for (i = 0; i < fast_size; ++i) { - DREF DeeObject *callback_result; - elem = DeeFastSeq_GetItem_deprecated(values, i); - if unlikely(!elem) - goto err_insert_function; - callback_result = DeeObject_Callf(insert_function, PCKuSIZ "o", index, elem); - Dee_Decref(elem); - if unlikely(!callback_result) - goto err_insert_function; - Dee_Decref(callback_result); - if (index < SSIZE_MAX) - ++index; - } - Dee_Decref(insert_function); - return 0; - } - iterator = DeeObject_Iter(values); - if unlikely(!iterator) - goto err_insert_function; - while (ITER_ISOK(elem = DeeObject_IterNext(iterator))) { - DREF DeeObject *callback_result; - callback_result = DeeObject_Callf(insert_function, PCKuSIZ "o", index, elem); - Dee_Decref(elem); - if unlikely(!callback_result) - goto err_insert_function_iterator; - Dee_Decref(callback_result); - if (index < SSIZE_MAX) - ++index; - } - if unlikely(!elem) - goto err_insert_function_iterator; - Dee_Decref(iterator); - Dee_Decref(insert_function); - return 0; -err_insert_function_iterator: - Dee_Decref(iterator); -err_insert_function: - Dee_Decref(insert_function); - goto err; - } - } - if (tp_self->tp_init.tp_assign && (dssize_t)index < 0) { - DREF DeeObject *concat; - concat = DeeSeq_Concat(self, values); - if unlikely(!concat) - goto err; - result = (*tp_self->tp_init.tp_assign)(self, concat); - Dee_Decref(concat); - return result; - } - if ((tp_self = DeeTypeMRO_Next(&mro, tp_self)) == NULL) - break; - } -not_resizable: - return err_fixedlength_sequence(self); -err_attr: - if (DeeError_Catch(&DeeError_NotImplemented) || - DeeError_Catch(&DeeError_AttributeError)) - goto not_resizable; - goto err; -err: - return -1; -} - - -INTERN WUNUSED NONNULL((1, 2)) int DCALL -DeeSeq_Append(DeeObject *self, DeeObject *value) { - int result; - DeeTypeObject *tp_self = Dee_TYPE(self); - DeeTypeMRO mro; - DeeTypeMRO_Init(&mro, tp_self); - while (tp_self != &DeeSeq_Type) { - struct type_seq *seq = tp_self->tp_seq; - if (seq) { - struct type_nsi const *nsi = seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ && - is_noninherited_nsi(tp_self, seq, nsi)) { - if (nsi->nsi_seqlike.nsi_setrange) { - DREF DeeObject *value_seq; - value_seq = DeeTuple_Pack(1, value); - if unlikely(!value_seq) - goto err; - result = (*nsi->nsi_seqlike.nsi_setrange)(self, SSIZE_MAX, SSIZE_MAX, (DeeObject *)value_seq); - Dee_Decref(value_seq); - return result; - } - } - } - { - /* Use extend. */ - DREF DeeObject *callback_result; - callback_result = call_generic_attribute(tp_self, self, STR_extend, - DeeString_Hash((DeeObject *)&str_extend), - "(o)", value); - if (callback_result == ITER_DONE) { - callback_result = call_generic_attribute(tp_self, self, STR_insert, - DeeString_Hash((DeeObject *)&str_insert), - PCKuSIZ "o", (size_t)SSIZE_MAX, value); - if (callback_result == ITER_DONE) { - callback_result = call_generic_attribute(tp_self, self, STR_insertall, - DeeString_Hash((DeeObject *)&str_insertall), - PCKuSIZ "(o)", (size_t)SSIZE_MAX, value); - } - } - if (callback_result != ITER_DONE) { - if unlikely(!callback_result) - goto err_attr; - Dee_Decref(callback_result); - return 0; - } - } - if (seq && has_noninherited_setrange(tp_self, seq)) { - DREF DeeObject *index_ob, *value_seq; - index_ob = DeeInt_NewSize(SSIZE_MAX); - if unlikely(!index_ob) - goto err; - value_seq = DeeTuple_Pack(1, value); - if unlikely(!value_seq) { - Dee_Decref(index_ob); - goto err; - } - result = (*seq->tp_setrange)(self, index_ob, index_ob, (DeeObject *)value_seq); - Dee_Decref(value_seq); - Dee_Decref(index_ob); - return result; - } - if (tp_self->tp_init.tp_assign) { - DREF DeeObject *items, *concat; - items = DeeTuple_Pack(1, value); - if unlikely(!items) - goto err; - concat = DeeSeq_Concat(self, items); - Dee_Decref(items); - if unlikely(!concat) - goto err; - result = (*tp_self->tp_init.tp_assign)(self, concat); - Dee_Decref(concat); - return result; - } - if ((tp_self = DeeTypeMRO_Next(&mro, tp_self)) == NULL) - break; - } -not_resizable: - return err_fixedlength_sequence(self); -err_attr: - if (DeeError_Catch(&DeeError_NotImplemented) || - DeeError_Catch(&DeeError_AttributeError)) - goto not_resizable; - goto err; -err: - return -1; -} - -INTERN WUNUSED NONNULL((1, 2)) int DCALL -DeeSeq_Extend(DeeObject *self, DeeObject *values) { - int result; - DeeTypeObject *tp_self = Dee_TYPE(self); - DeeTypeMRO mro; - DeeTypeMRO_Init(&mro, tp_self); - while (tp_self != &DeeSeq_Type) { - struct type_seq *seq = tp_self->tp_seq; - if (seq) { - struct type_nsi const *nsi = seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ && - is_noninherited_nsi(tp_self, seq, nsi)) { - if (nsi->nsi_seqlike.nsi_setrange) - return (*nsi->nsi_seqlike.nsi_setrange)(self, SSIZE_MAX, SSIZE_MAX, values); - } - } - { - /* Try to call `insertall()' */ - DREF DeeObject *callback_result; - callback_result = call_generic_attribute(tp_self, self, STR_insertall, - DeeString_Hash((DeeObject *)&str_insertall), - PCKdSIZ "o", (dssize_t)SSIZE_MAX, values); - if (callback_result != ITER_DONE) { - if unlikely(!callback_result) - goto err_attr; - Dee_Decref(callback_result); - return 0; - } - } - if (seq && has_noninherited_setrange(tp_self, seq)) { - DREF DeeObject *index_ob; - index_ob = DeeInt_NewSize(SSIZE_MAX); - if unlikely(!index_ob) - goto err; - result = (*seq->tp_setrange)(self, index_ob, index_ob, values); - Dee_Decref(index_ob); - return result; - } - { - DREF DeeObject *append_function; - append_function = get_generic_attribute(tp_self, - self, - (DeeObject *)&str_append); - if (append_function != ITER_DONE) { - size_t i, fast_size; - DREF DeeObject *iterator, *elem; - if unlikely(!append_function) - goto err_attr; - /* Use the append function to append everything */ - fast_size = DeeFastSeq_GetSize_deprecated(values); - if (fast_size != DEE_FASTSEQ_NOTFAST_DEPRECATED) { - for (i = 0; i < fast_size; ++i) { - DREF DeeObject *callback_result; - elem = DeeFastSeq_GetItem_deprecated(values, i); - if unlikely(!elem) - goto err_append_function; - callback_result = DeeObject_Call(append_function, 1, &elem); - Dee_Decref(elem); - if unlikely(!callback_result) - goto err_append_function; - Dee_Decref(callback_result); - } - Dee_Decref(append_function); - return 0; - } - iterator = DeeObject_Iter(values); - if unlikely(!iterator) - goto err_append_function; - while (ITER_ISOK(elem = DeeObject_IterNext(iterator))) { - DREF DeeObject *callback_result; - callback_result = DeeObject_Call(append_function, 1, &elem); - Dee_Decref(elem); - if unlikely(!callback_result) - goto err_append_function_iterator; - Dee_Decref(callback_result); - if (DeeThread_CheckInterrupt()) - goto err_append_function_iterator; - } - if unlikely(!elem) - goto err_append_function_iterator; - Dee_Decref(iterator); - Dee_Decref(append_function); - return 0; -err_append_function_iterator: - Dee_Decref(iterator); -err_append_function: - Dee_Decref(append_function); - goto err; - } - } - { - DREF DeeObject *insert_function; - insert_function = get_generic_attribute(tp_self, - self, - (DeeObject *)&str_insert); - if (insert_function != ITER_DONE) { - size_t i, fast_size; - DREF DeeObject *iterator, *elem; - if unlikely(!insert_function) - goto err_attr; - /* Use the insert function to insert everything */ - fast_size = DeeFastSeq_GetSize_deprecated(values); - if (fast_size != DEE_FASTSEQ_NOTFAST_DEPRECATED) { - for (i = 0; i < fast_size; ++i) { - DREF DeeObject *callback_result; - elem = DeeFastSeq_GetItem_deprecated(values, i); - if unlikely(!elem) - goto err_insert_function; - callback_result = DeeObject_Callf(insert_function, PCKuSIZ "o", (size_t)SSIZE_MAX, elem); - Dee_Decref(elem); - if unlikely(!callback_result) - goto err_insert_function; - Dee_Decref(callback_result); - } - Dee_Decref(insert_function); - return 0; - } - iterator = DeeObject_Iter(values); - if unlikely(!iterator) - goto err_insert_function; - while (ITER_ISOK(elem = DeeObject_IterNext(iterator))) { - DREF DeeObject *callback_result; - callback_result = DeeObject_Callf(insert_function, PCKuSIZ "o", (size_t)SSIZE_MAX, elem); - Dee_Decref(elem); - if unlikely(!callback_result) - goto err_insert_function_iterator; - Dee_Decref(callback_result); - if (DeeThread_CheckInterrupt()) - goto err_insert_function_iterator; - } - if unlikely(!elem) - goto err_insert_function_iterator; - Dee_Decref(iterator); - Dee_Decref(insert_function); - return 0; -err_insert_function_iterator: - Dee_Decref(iterator); -err_insert_function: - Dee_Decref(insert_function); - goto err; - } - } - if (tp_self->tp_init.tp_assign) { - DREF DeeObject *concat; - concat = DeeSeq_Concat(self, values); - if unlikely(!concat) - goto err; - result = (*tp_self->tp_init.tp_assign)(self, concat); - Dee_Decref(concat); - return result; - } - if ((tp_self = DeeTypeMRO_Next(&mro, tp_self)) == NULL) - break; - } -not_resizable: - return err_fixedlength_sequence(self); -err_attr: - if (DeeError_Catch(&DeeError_NotImplemented) || - DeeError_Catch(&DeeError_AttributeError)) - goto not_resizable; - goto err; -err: - return -1; -} - - -INTERN WUNUSED NONNULL((1, 2)) int DCALL -DeeSeq_InplaceExtend(DREF DeeObject **__restrict p_self, - DeeObject *values) { - DeeObject *self = *p_self; - int result; - DeeTypeObject *tp_self = Dee_TYPE(self); - DREF DeeObject *new_self; - DeeTypeMRO mro; - DeeTypeMRO_Init(&mro, tp_self); - while (tp_self != &DeeSeq_Type) { - struct type_seq *seq = tp_self->tp_seq; - if (seq) { - struct type_nsi const *nsi = seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ && - is_noninherited_nsi(tp_self, seq, nsi)) { - if (nsi->nsi_seqlike.nsi_setrange) - return (*nsi->nsi_seqlike.nsi_setrange)(self, SSIZE_MAX, SSIZE_MAX, values); - } - } - { - DREF DeeObject *callback_result; - /* Try to call `extend()' */ - callback_result = call_generic_attribute(tp_self, self, STR_extend, - DeeString_Hash((DeeObject *)&str_extend), - "o", values); - if (callback_result == ITER_DONE) { - /* Try to call `insertall()' */ - callback_result = call_generic_attribute(tp_self, self, STR_insertall, - DeeString_Hash((DeeObject *)&str_insertall), - PCKdSIZ "o", (dssize_t)SSIZE_MAX, values); - } - if (callback_result != ITER_DONE) { - if unlikely(!callback_result) - goto err_attr; - Dee_Decref(callback_result); - return 0; - } - } - if (seq && has_noninherited_setrange(tp_self, seq)) { - DREF DeeObject *index_ob; - index_ob = DeeInt_NewSize(SSIZE_MAX); - if unlikely(!index_ob) - goto err; - result = (*seq->tp_setrange)(self, index_ob, index_ob, values); - Dee_Decref(index_ob); - return result; - } - { - DREF DeeObject *append_function; - append_function = get_generic_attribute(tp_self, - self, - (DeeObject *)&str_append); - if (append_function != ITER_DONE) { - size_t i, fast_size; - DREF DeeObject *iterator, *elem; - if unlikely(!append_function) - goto err_attr; - /* Use the append function to append everything */ - fast_size = DeeFastSeq_GetSize_deprecated(values); - if (fast_size != DEE_FASTSEQ_NOTFAST_DEPRECATED) { - for (i = 0; i < fast_size; ++i) { - DREF DeeObject *callback_result; - elem = DeeFastSeq_GetItem_deprecated(values, i); - if unlikely(!elem) - goto err_append_function; - callback_result = DeeObject_Call(append_function, 1, &elem); - Dee_Decref(elem); - if unlikely(!callback_result) - goto err_append_function; - Dee_Decref(callback_result); - } - Dee_Decref(append_function); - return 0; - } - iterator = DeeObject_Iter(values); - if unlikely(!iterator) - goto err_append_function; - while (ITER_ISOK(elem = DeeObject_IterNext(iterator))) { - DREF DeeObject *callback_result; - callback_result = DeeObject_Call(append_function, 1, &elem); - Dee_Decref(elem); - if unlikely(!callback_result) - goto err_append_function_iterator; - Dee_Decref(callback_result); - if (DeeThread_CheckInterrupt()) - goto err_append_function_iterator; - } - if unlikely(!elem) - goto err_append_function_iterator; - Dee_Decref(iterator); - Dee_Decref(append_function); - return 0; -err_append_function_iterator: - Dee_Decref(iterator); -err_append_function: - Dee_Decref(append_function); - goto err; - } - } - { - DREF DeeObject *insert_function; - insert_function = get_generic_attribute(tp_self, - self, - (DeeObject *)&str_insert); - if (insert_function != ITER_DONE) { - size_t i, fast_size; - DREF DeeObject *iterator, *elem; - if unlikely(!insert_function) - goto err_attr; - /* Use the insert function to insert everything */ - fast_size = DeeFastSeq_GetSize_deprecated(values); - if (fast_size != DEE_FASTSEQ_NOTFAST_DEPRECATED) { - for (i = 0; i < fast_size; ++i) { - DREF DeeObject *callback_result; - elem = DeeFastSeq_GetItem_deprecated(values, i); - if unlikely(!elem) - goto err_insert_function; - callback_result = DeeObject_Callf(insert_function, PCKuSIZ "o", (size_t)SSIZE_MAX, elem); - Dee_Decref(elem); - if unlikely(!callback_result) - goto err_insert_function; - Dee_Decref(callback_result); - } - Dee_Decref(insert_function); - return 0; - } - iterator = DeeObject_Iter(values); - if unlikely(!iterator) - goto err_insert_function; - while (ITER_ISOK(elem = DeeObject_IterNext(iterator))) { - DREF DeeObject *callback_result; - callback_result = DeeObject_Callf(insert_function, PCKuSIZ "o", (size_t)SSIZE_MAX, elem); - Dee_Decref(elem); - if unlikely(!callback_result) - goto err_insert_function_iterator; - Dee_Decref(callback_result); - if (DeeThread_CheckInterrupt()) - goto err_insert_function_iterator; - } - if unlikely(!elem) - goto err_insert_function_iterator; - Dee_Decref(iterator); - Dee_Decref(insert_function); - return 0; -err_insert_function_iterator: - Dee_Decref(iterator); -err_insert_function: - Dee_Decref(insert_function); - goto err; - } - } - if (tp_self->tp_init.tp_assign) { - new_self = DeeSeq_Concat(self, values); - if unlikely(!new_self) - goto err; - result = (*tp_self->tp_init.tp_assign)(self, new_self); - Dee_Decref(new_self); - return result; - } - if (tp_self->tp_math && tp_self->tp_math->tp_add) { - new_self = (*tp_self->tp_math->tp_add)(self, values); - if unlikely(!new_self) - goto err; - Dee_Decref(self); - *p_self = new_self; - return 0; - } - if ((tp_self = DeeTypeMRO_Next(&mro, tp_self)) == NULL) - break; - } -not_resizable: - new_self = DeeSeq_Concat(self, values); -set_new_self: - if unlikely(!new_self) - goto err; - Dee_Decref(self); - *p_self = new_self; - return 0; -err_attr: - if (DeeError_Catch(&DeeError_NotImplemented) || - DeeError_Catch(&DeeError_AttributeError)) { - if (tp_self == &DeeSeq_Type) - goto not_resizable; - new_self = DeeObject_Add(self, values); - goto set_new_self; - } -err: - return -1; -} - -INTERN WUNUSED NONNULL((1, 2)) int DCALL -DeeSeq_InplaceRepeat(DREF DeeObject **__restrict p_self, - DeeObject *count) { - DeeObject *self = *p_self; - int result; - DeeTypeObject *tp_self = Dee_TYPE(self); - DREF DeeObject *new_self; - size_t integer_count; - DeeTypeMRO mro; - DeeTypeMRO_Init(&mro, tp_self); - while (tp_self != &DeeSeq_Type) { - if (tp_self->tp_init.tp_assign) { - if (DeeObject_AsSize(count, &integer_count)) - goto err; - if (integer_count == 1) - return 0; - new_self = DeeSeq_Repeat(self, integer_count); - if unlikely(!new_self) - goto err; - result = (*tp_self->tp_init.tp_assign)(self, new_self); - Dee_Decref(new_self); - return result; - } - if (tp_self->tp_seq && - has_noninherited_setrange(tp_self, tp_self->tp_seq)) { - if (DeeObject_AsSize(count, &integer_count)) - goto err; - if (integer_count == 1) - return 0; - new_self = DeeSeq_Repeat(self, integer_count); - if unlikely(!new_self) - goto err; - result = (*tp_self->tp_seq->tp_setrange)(self, Dee_None, Dee_None, new_self); - Dee_Decref(new_self); - return result; - } - if (tp_self->tp_math && tp_self->tp_math->tp_mul) { - new_self = (*tp_self->tp_math->tp_mul)(self, count); - if unlikely(!new_self) - goto err; - Dee_Decref(self); - *p_self = new_self; - return 0; - } - if ((tp_self = DeeTypeMRO_Next(&mro, tp_self)) == NULL) - break; - } - if (DeeObject_AsSize(count, &integer_count)) - goto err; - new_self = DeeSeq_Repeat(self, integer_count); - if unlikely(!new_self) - goto err; - Dee_Decref(self); - *p_self = new_self; - return 0; -err: - return -1; -} - - - -INTERN WUNUSED NONNULL((1)) size_t DCALL -DeeSeq_Erase(DeeObject *__restrict self, - size_t index, size_t count) { - int error; - DeeTypeObject *tp_self = Dee_TYPE(self); - DeeTypeMRO mro; - DeeTypeMRO_Init(&mro, tp_self); - while (tp_self != &DeeSeq_Type) { - struct type_seq *seq = tp_self->tp_seq; - if (seq) { - struct type_nsi const *nsi = seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ && - is_noninherited_nsi(tp_self, seq, nsi)) { - /* Check for NSI-optimized variants */ - if (nsi->nsi_seqlike.nsi_delrange) { - size_t mylen = (*nsi->nsi_seqlike.nsi_getsize)(self); - if unlikely(mylen == (size_t)-1) - goto err; - if (index >= mylen) { - index = mylen; - count = 0; - } else if (index + count > mylen) { - count = mylen - index; - } - if ((*nsi->nsi_seqlike.nsi_delrange)(self, index, index + count)) - goto err; - return count; - } - if (nsi->nsi_seqlike.nsi_delitem) { - size_t i, mylen = (*nsi->nsi_seqlike.nsi_getsize)(self); - if unlikely(mylen == (size_t)-1) - goto err; - if (index >= mylen) { - index = mylen; - count = 0; - } else if (index + count > mylen) { - count = mylen - index; - } - if (count) { - i = index + count; - do { - --i; - if ((*nsi->nsi_seqlike.nsi_delitem)(self, i)) - goto err; - } while (i > index); - } - return count; - } - } - if (has_noninherited_delrange(tp_self, seq)) { - DREF DeeObject *start_index, *end_index; - size_t mylen = DeeObject_Size(self); - if unlikely(mylen == (size_t)-1) - goto err; - if (index >= mylen) { - index = mylen; - count = 0; - } else if (index + count > mylen) { - count = mylen - index; - } - start_index = DeeInt_NewSize(index); - if unlikely(!start_index) - goto err; - end_index = DeeInt_NewSize(index + count); - if unlikely(!end_index) { - Dee_Decref(start_index); - goto err; - } - error = (*seq->tp_delrange)(self, start_index, end_index); - Dee_Decref(end_index); - Dee_Decref(start_index); - if unlikely(error) - goto err; - return count; - } - if (has_noninherited_delitem(tp_self, seq)) { - size_t mylen = DeeObject_Size(self); - if unlikely(mylen == (size_t)-1) - goto err; - if (index >= mylen) { - index = mylen; - count = 0; - } else if (index + count > mylen) { - count = mylen - index; - } - if (count) { - size_t i = index + count; - do { - DREF DeeObject *index_ob; - --i; - index_ob = DeeInt_NewSize(i); - if unlikely(!index_ob) - goto err; - error = (*seq->tp_delitem)(self, index_ob); - Dee_Decref(index_ob); - if unlikely(error) - goto err; - } while (i > index); - } - return count; - } - } - if ((tp_self = DeeTypeMRO_Next(&mro, tp_self)) == NULL) - break; - } - err_fixedlength_sequence(self); -err: - return (size_t)-1; -} - -INTERN WUNUSED NONNULL((1)) DREF DeeObject *DCALL -DeeSeq_PopItem(DeeObject *__restrict self, dssize_t index) { - DREF DeeObject *result; - int error; - DeeTypeObject *tp_self = Dee_TYPE(self); - DeeTypeMRO mro; - DeeTypeMRO_Init(&mro, tp_self); - while (tp_self != &DeeSeq_Type) { - struct type_seq *seq = tp_self->tp_seq; - if (seq) { - struct type_nsi const *nsi = seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ && - is_noninherited_nsi(tp_self, seq, nsi)) { - /* Check for NSI-optimized variants */ - if (nsi->nsi_seqlike.nsi_delitem) { - if (nsi->nsi_seqlike.nsi_getitem) { - if (index < 0) { - size_t mylen = (*nsi->nsi_seqlike.nsi_getsize)(self); - if unlikely(mylen == (size_t)-1) - goto err; - index += mylen; - } - result = (*nsi->nsi_seqlike.nsi_getitem)(self, (size_t)index); - if unlikely(!result) - goto err; - if unlikely((*nsi->nsi_seqlike.nsi_delitem)(self, (size_t)index)) - goto err_r; - return result; - } - if (nsi->nsi_seqlike.nsi_getitem_fast) { - size_t mylen = (*nsi->nsi_seqlike.nsi_getsize)(self); - if unlikely(mylen == (size_t)-1) - goto err; - if (index < 0) { - index += mylen; - } else if unlikely((size_t)index >= mylen) { - err_index_out_of_bounds(self, (size_t)index, mylen); - goto err; - } - result = (*nsi->nsi_seqlike.nsi_getitem_fast)(self, (size_t)index); - if unlikely(!result) { - err_unbound_index(self, (size_t)index); - goto err; - } - if unlikely((*nsi->nsi_seqlike.nsi_delitem)(self, (size_t)index)) - goto err_r; - return result; - } - } - if (nsi->nsi_seqlike.nsi_delrange) { - if (nsi->nsi_seqlike.nsi_getitem) { - if (index < 0) { - size_t mylen = (*nsi->nsi_seqlike.nsi_getsize)(self); - if unlikely(mylen == (size_t)-1) - goto err; - index += mylen; - } - result = (*nsi->nsi_seqlike.nsi_getitem)(self, (size_t)index); - if unlikely(!result) - goto err; - if unlikely((*nsi->nsi_seqlike.nsi_delrange)(self, (size_t)index, (size_t)index + 1)) - goto err_r; - return result; - } - if (nsi->nsi_seqlike.nsi_getitem_fast) { - size_t mylen = (*nsi->nsi_seqlike.nsi_getsize)(self); - if unlikely(mylen == (size_t)-1) - goto err; - if (index < 0) { - index += mylen; - } else if unlikely((size_t)index >= mylen) { - err_index_out_of_bounds(self, (size_t)index, mylen); - goto err; - } - result = (*nsi->nsi_seqlike.nsi_getitem_fast)(self, (size_t)index); - if unlikely(!result) { - err_unbound_index(self, (size_t)index); - goto err; - } - if unlikely((*nsi->nsi_seqlike.nsi_delrange)(self, (size_t)index, (size_t)index + 1)) - goto err_r; - return result; - } - } - } - if (has_noninherited_delitem(tp_self, seq)) { - DREF DeeObject *index_ob; - if (index < 0) { - size_t mylen = DeeObject_Size(self); - if unlikely(mylen == (size_t)-1) - goto err; - index += mylen; - } - index_ob = DeeInt_NewSize((size_t)index); - if unlikely(!index_ob) - goto err; - result = DeeObject_GetItem(self, index_ob); - if unlikely(!result) { - Dee_Decref(index_ob); - goto err; - } - error = (*seq->tp_delitem)(self, index_ob); - Dee_Decref(index_ob); - if unlikely(error) - goto err_r; - return result; - } - if (has_noninherited_delrange(tp_self, seq)) { - DREF DeeObject *index_ob, *index_plus1_ob; - if (index < 0) { - size_t mylen = DeeObject_Size(self); - if unlikely(mylen == (size_t)-1) - goto err; - index += mylen; - } - index_ob = DeeInt_NewSize((size_t)index); - if unlikely(!index_ob) - goto err; - result = DeeObject_GetItem(self, index_ob); - if unlikely(!result) { - Dee_Decref(index_ob); - goto err; - } - index_plus1_ob = DeeInt_NewSize((size_t)index + 1); - if unlikely(!index_plus1_ob) { - Dee_Decref(index_ob); - goto err_r; - } - error = (*seq->tp_delrange)(self, index_ob, index_plus1_ob); - Dee_Decref(index_plus1_ob); - Dee_Decref(index_ob); - if unlikely(error) - goto err_r; - return result; - } - } - if ((tp_self = DeeTypeMRO_Next(&mro, tp_self)) == NULL) - break; - } - err_fixedlength_sequence(self); -err: - return NULL; -err_r: - Dee_Decref(result); - goto err; -} - -INTERN WUNUSED NONNULL((1, 4)) int DCALL -DeeSeq_Remove(DeeObject *self, size_t start, size_t end, - DeeObject *elem, DeeObject *key) { - DREF DeeObject *keyed_search_item, *index_ob; - DREF DeeObject *item, *callback_result, *erase_func; - int result; - DeeTypeObject *tp_self = Dee_TYPE(self); - DeeTypeMRO mro; - DeeTypeMRO_Init(&mro, tp_self); - while (tp_self != &DeeSeq_Type) { - struct type_seq *seq = tp_self->tp_seq; - if (seq) { - struct type_nsi const *nsi = seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ && - is_noninherited_nsi(tp_self, seq, nsi)) { - if (nsi->nsi_seqlike.nsi_getitem && - (nsi->nsi_seqlike.nsi_delitem || - nsi->nsi_seqlike.nsi_delrange)) { - size_t i, mylen = (*nsi->nsi_seqlike.nsi_getsize)(self); - if (end > mylen) - end = mylen; - if (start >= end) { - } else if (key) { - keyed_search_item = DeeObject_Call(key, 1, (DeeObject **)&elem); - if unlikely(!keyed_search_item) - goto err; - for (i = start; i < end; ++i) { - item = (*nsi->nsi_seqlike.nsi_getitem)(self, i); - if unlikely(!item) - goto err_keyed_search_item; - result = DeeObject_TryCmpKeyEqAsBool(keyed_search_item, item, key); - Dee_Decref(item); - if (result != 0) { - Dee_Decref(keyed_search_item); - goto do_delete_i; - } - } - Dee_Decref(keyed_search_item); - } else { - for (i = start; i < end; ++i) { - item = (*nsi->nsi_seqlike.nsi_getitem)(self, i); - if unlikely(!item) - goto err; - result = DeeObject_TryCmpEqAsBool(elem, item); - Dee_Decref(item); - if (result != 0) { -do_delete_i: - if unlikely(result < 0) - goto err; - if (nsi->nsi_seqlike.nsi_delitem) { - if ((*nsi->nsi_seqlike.nsi_delitem)(self, i)) - goto err; - } else { - ASSERT(nsi->nsi_seqlike.nsi_delrange); - if ((*nsi->nsi_seqlike.nsi_delrange)(self, i, i + 1)) - goto err; - } - return 1; - } - } - } - return 0; - } - } - if (has_noninherited_delitem(tp_self, seq)) { - size_t i, mylen = DeeObject_Size(self); - if unlikely(mylen == (size_t)-1) - goto err; - if (end > mylen) - end = mylen; - if (start >= end) { - } else if (key) { - keyed_search_item = DeeObject_Call(key, 1, (DeeObject **)&elem); - if unlikely(!keyed_search_item) - goto err; - for (i = start; i < end; ++i) { - index_ob = DeeInt_NewSize(i); - if unlikely(!index_ob) - goto err_keyed_search_item; - item = DeeObject_GetItem(self, index_ob); - if unlikely(!item) { - Dee_Decref(index_ob); - goto err_keyed_search_item; - } - result = DeeObject_TryCmpKeyEqAsBool(keyed_search_item, item, key); - Dee_Decref(item); - if (result != 0) { - Dee_Decref(keyed_search_item); - goto do_tp_del_i; - } - Dee_Decref(index_ob); - } - Dee_Decref(keyed_search_item); - } else { - for (i = start; i < end; ++i) { - index_ob = DeeInt_NewSize(i); - if unlikely(!index_ob) - goto err; - item = DeeObject_GetItem(self, index_ob); - if unlikely(!item) { -err_index_ob_del: - Dee_Decref(index_ob); - goto err; - } - result = DeeObject_TryCmpEqAsBool(elem, item); - Dee_Decref(item); - if (result != 0) { -do_tp_del_i: - if unlikely(result < 0) - goto err_index_ob_del; - result = (*seq->tp_delitem)(self, index_ob); - if unlikely(result < 0) - goto err_index_ob_del; - Dee_Decref(index_ob); - return 1; - } - Dee_Decref(index_ob); - } - } - return 0; - } - } - erase_func = get_generic_attribute(tp_self, - self, - (DeeObject *)&str_erase); - if (erase_func != ITER_DONE) { - size_t i, mylen; - if unlikely(!erase_func) - goto err_attr; - mylen = DeeObject_Size(self); - if unlikely(mylen == (size_t)-1) - goto err_erase_func; - if (end > mylen) - end = mylen; - if (start >= end) { - } else if (key) { - keyed_search_item = DeeObject_Call(key, 1, (DeeObject **)&elem); - if unlikely(!keyed_search_item) - goto err_erase_func; - for (i = start; i < end; ++i) { - index_ob = DeeInt_NewSize(i); - if unlikely(!index_ob) - goto err_erase_func_keyed_search_item; - item = DeeObject_GetItem(self, index_ob); - if unlikely(!item) { - Dee_Decref(index_ob); - goto err_erase_func_keyed_search_item; - } - result = DeeObject_TryCmpKeyEqAsBool(keyed_search_item, item, key); - Dee_Decref(item); - if (result != 0) { - Dee_Decref(keyed_search_item); - goto do_erase_func_i; - } - Dee_Decref(index_ob); - } - Dee_Decref(keyed_search_item); - } else { - for (i = start; i < end; ++i) { - index_ob = DeeInt_NewSize(i); - if unlikely(!index_ob) - goto err_erase_func; - item = DeeObject_GetItem(self, index_ob); - if unlikely(!item) { -err_index_ob: - Dee_Decref(index_ob); - goto err_erase_func; - } - result = DeeObject_TryCmpEqAsBool(elem, item); - Dee_Decref(item); - if (result != 0) { -do_erase_func_i: - if unlikely(result < 0) - goto err_index_ob; - callback_result = DeeObject_CallPack(erase_func, 2, index_ob, DeeInt_One); - if unlikely(!callback_result) - goto err_index_ob; - Dee_Decref(callback_result); - Dee_Decref(index_ob); - Dee_Decref(erase_func); - return 1; - } - Dee_Decref(index_ob); - } - } - Dee_Decref(erase_func); - return 0; - } - if (seq && has_noninherited_delrange(tp_self, seq)) { - size_t i, mylen = DeeObject_Size(self); - if unlikely(mylen == (size_t)-1) - goto err; - if (end > mylen) - end = mylen; - if (start >= end) { - } else if (key) { - keyed_search_item = DeeObject_Call(key, 1, (DeeObject **)&elem); - if unlikely(!keyed_search_item) - goto err_erase_func; - for (i = start; i < end; ++i) { - index_ob = DeeInt_NewSize(i); - if unlikely(!index_ob) - goto err_keyed_search_item; - item = DeeObject_GetItem(self, index_ob); - if unlikely(!item) { - Dee_Decref(index_ob); - goto err_keyed_search_item; - } - result = DeeObject_TryCmpKeyEqAsBool(keyed_search_item, item, key); - Dee_Decref(item); - if (result != 0) { - Dee_Decref(keyed_search_item); - goto do_tp_range_del_i; - } - Dee_Decref(index_ob); - } - Dee_Decref(keyed_search_item); - } else { - for (i = start; i < end; ++i) { - index_ob = DeeInt_NewSize(i); - if unlikely(!index_ob) - goto err; - item = DeeObject_GetItem(self, index_ob); - if unlikely(!item) { -err_index_ob_delrange: - Dee_Decref(index_ob); - goto err; - } - result = DeeObject_TryCmpEqAsBool(elem, item); - Dee_Decref(item); - if (result != 0) { - DREF DeeObject *index_plus1_ob; -do_tp_range_del_i: - if unlikely(result < 0) - goto err_index_ob_delrange; - index_plus1_ob = DeeInt_NewSize(i + 1); - if unlikely(!index_plus1_ob) - goto err_index_ob_delrange; - result = (*seq->tp_delrange)(self, index_ob, index_plus1_ob); - Dee_Decref(index_plus1_ob); - if unlikely(result < 0) - goto err_index_ob_delrange; - Dee_Decref(index_ob); - return 1; - } - Dee_Decref(index_ob); - } - } - return 0; - } - if ((tp_self = DeeTypeMRO_Next(&mro, tp_self)) == NULL) - break; - } -is_immutable: - err_immutable_sequence(self); -err: - return -1; -err_keyed_search_item: - Dee_Decref(keyed_search_item); - goto err; -err_attr: - if (DeeError_Catch(&DeeError_NotImplemented) || - DeeError_Catch(&DeeError_AttributeError)) - goto is_immutable; - goto err; -err_erase_func_keyed_search_item: - Dee_Decref(keyed_search_item); -err_erase_func: - Dee_Decref(erase_func); - goto err; -} - -INTERN WUNUSED NONNULL((1, 4)) int DCALL -DeeSeq_RRemove(DeeObject *self, size_t start, size_t end, - DeeObject *elem, DeeObject *key) { - DREF DeeObject *keyed_search_item, *index_ob; - DREF DeeObject *item, *callback_result, *erase_func; - int result; - DeeTypeObject *tp_self = Dee_TYPE(self); - DeeTypeMRO mro; - DeeTypeMRO_Init(&mro, tp_self); - while (tp_self != &DeeSeq_Type) { - struct type_seq *seq = tp_self->tp_seq; - if (seq) { - struct type_nsi const *nsi = seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ && - is_noninherited_nsi(tp_self, seq, nsi)) { - if (nsi->nsi_seqlike.nsi_getitem && - (nsi->nsi_seqlike.nsi_delitem || - nsi->nsi_seqlike.nsi_delrange)) { - size_t i, mylen = (*nsi->nsi_seqlike.nsi_getsize)(self); - if (end > mylen) - end = mylen; - if (end > start) { - i = end; - if (key) { - keyed_search_item = DeeObject_Call(key, 1, (DeeObject **)&elem); - if unlikely(!keyed_search_item) - goto err; - do { - --i; - item = (*nsi->nsi_seqlike.nsi_getitem)(self, i); - if unlikely(!item) - goto err_keyed_search_item; - result = DeeObject_TryCmpKeyEqAsBool(keyed_search_item, item, key); - Dee_Decref(item); - if (result != 0) { - Dee_Decref(keyed_search_item); - goto do_delete_i; - } - } while (i > start); - Dee_Decref(keyed_search_item); - } else { - do { - --i; - item = (*nsi->nsi_seqlike.nsi_getitem)(self, i); - if unlikely(!item) - goto err; - result = DeeObject_TryCmpEqAsBool(elem, item); - Dee_Decref(item); - if (result != 0) { -do_delete_i: - if unlikely(result < 0) - goto err; - if (nsi->nsi_seqlike.nsi_delitem) { - if ((*nsi->nsi_seqlike.nsi_delitem)(self, i)) - goto err; - } else { - ASSERT(nsi->nsi_seqlike.nsi_delrange); - if ((*nsi->nsi_seqlike.nsi_delrange)(self, i, i + 1)) - goto err; - } - return 1; - } - } while (i > start); - } - } - return 0; - } - } - if (has_noninherited_delitem(tp_self, seq)) { - size_t i, mylen = DeeObject_Size(self); - if unlikely(mylen == (size_t)-1) - goto err; - if (end > mylen) - end = mylen; - if (end > start) { - i = end; - if (key) { - keyed_search_item = DeeObject_Call(key, 1, (DeeObject **)&elem); - if unlikely(!keyed_search_item) - goto err; - do { - --i; - index_ob = DeeInt_NewSize(i); - if unlikely(!index_ob) - goto err_keyed_search_item; - item = DeeObject_GetItem(self, index_ob); - if unlikely(!item) { - Dee_Decref(index_ob); - goto err_keyed_search_item; - } - result = DeeObject_TryCmpKeyEqAsBool(keyed_search_item, item, key); - Dee_Decref(item); - if (result != 0) { - Dee_Decref(keyed_search_item); - goto do_tp_del_i; - } - Dee_Decref(index_ob); - } while (i > start); - Dee_Decref(keyed_search_item); - } else { - do { - --i; - index_ob = DeeInt_NewSize(i); - if unlikely(!index_ob) - goto err; - item = DeeObject_GetItem(self, index_ob); - if unlikely(!item) { -err_index_ob_del: - Dee_Decref(index_ob); - goto err; - } - result = DeeObject_TryCmpEqAsBool(elem, item); - Dee_Decref(item); - if (result != 0) { -do_tp_del_i: - if unlikely(result < 0) - goto err_index_ob_del; - result = (*seq->tp_delitem)(self, index_ob); - if unlikely(result < 0) - goto err_index_ob_del; - Dee_Decref(index_ob); - return 1; - } - Dee_Decref(index_ob); - } while (i > start); - } - } - return 0; - } - } - erase_func = get_generic_attribute(tp_self, - self, - (DeeObject *)&str_erase); - if (erase_func != ITER_DONE) { - size_t i, mylen; - if unlikely(!erase_func) - goto err_attr; - mylen = DeeObject_Size(self); - if unlikely(mylen == (size_t)-1) - goto err_erase_func; - if (end > mylen) - end = mylen; - if (end > start) { - i = end; - if (key) { - keyed_search_item = DeeObject_Call(key, 1, (DeeObject **)&elem); - if unlikely(!keyed_search_item) - goto err; - do { - --i; - index_ob = DeeInt_NewSize(i); - if unlikely(!index_ob) - goto err_erase_func_keyed_search_item; - item = DeeObject_GetItem(self, index_ob); - if unlikely(!item) { - Dee_Decref(index_ob); - goto err_erase_func_keyed_search_item; - } - result = DeeObject_TryCmpKeyEqAsBool(keyed_search_item, item, key); - Dee_Decref(item); - if (result != 0) { - Dee_Decref(keyed_search_item); - goto do_erase_func_i; - } - Dee_Decref(index_ob); - } while (i > start); - Dee_Decref(keyed_search_item); - } else { - do { - --i; - index_ob = DeeInt_NewSize(i); - if unlikely(!index_ob) - goto err_erase_func; - item = DeeObject_GetItem(self, index_ob); - if unlikely(!item) { -err_index_ob: - Dee_Decref(index_ob); - goto err_erase_func; - } - result = DeeObject_TryCmpEqAsBool(elem, item); - Dee_Decref(item); - if (result != 0) { -do_erase_func_i: - if unlikely(result < 0) - goto err_index_ob; - callback_result = DeeObject_CallPack(erase_func, 2, index_ob, DeeInt_One); - if unlikely(!callback_result) - goto err_index_ob; - Dee_Decref(callback_result); - Dee_Decref(index_ob); - Dee_Decref(erase_func); - return 1; - } - Dee_Decref(index_ob); - } while (i > start); - } - } - Dee_Decref(erase_func); - return 0; - } - if (seq && has_noninherited_delrange(tp_self, seq)) { - size_t i, mylen = DeeObject_Size(self); - if unlikely(mylen == (size_t)-1) - goto err; - if (end > mylen) - end = mylen; - if (end > start) { - i = end; - if (key) { - keyed_search_item = DeeObject_Call(key, 1, (DeeObject **)&elem); - if unlikely(!keyed_search_item) - goto err; - do { - --i; - index_ob = DeeInt_NewSize(i); - if unlikely(!index_ob) - goto err_keyed_search_item; - item = DeeObject_GetItem(self, index_ob); - if unlikely(!item) { - Dee_Decref(index_ob); - goto err_keyed_search_item; - } - result = DeeObject_TryCmpKeyEqAsBool(keyed_search_item, item, key); - Dee_Decref(item); - if (result != 0) { - Dee_Decref(keyed_search_item); - goto do_tp_range_del_i; - } - Dee_Decref(index_ob); - } while (i > start); - Dee_Decref(keyed_search_item); - } else { - do { - --i; - index_ob = DeeInt_NewSize(i); - if unlikely(!index_ob) - goto err; - item = DeeObject_GetItem(self, index_ob); - if unlikely(!item) { -err_index_ob_delrange: - Dee_Decref(index_ob); - goto err; - } - result = DeeObject_TryCmpEqAsBool(elem, item); - Dee_Decref(item); - if (result != 0) { - DREF DeeObject *index_plus1_ob; -do_tp_range_del_i: - if unlikely(result < 0) - goto err_index_ob_delrange; - index_plus1_ob = DeeInt_NewSize(i + 1); - if unlikely(!index_plus1_ob) - goto err_index_ob_delrange; - result = (*seq->tp_delrange)(self, index_ob, index_plus1_ob); - Dee_Decref(index_plus1_ob); - if unlikely(result < 0) - goto err_index_ob_delrange; - Dee_Decref(index_ob); - return 1; - } - Dee_Decref(index_ob); - } while (i > start); - } - } - return 0; - } - if ((tp_self = DeeTypeMRO_Next(&mro, tp_self)) == NULL) - break; - } -is_immutable: - err_immutable_sequence(self); -err: - return -1; -err_keyed_search_item: - Dee_Decref(keyed_search_item); - goto err; -err_attr: - if (DeeError_Catch(&DeeError_NotImplemented) || - DeeError_Catch(&DeeError_AttributeError)) - goto is_immutable; - goto err; -err_erase_func_keyed_search_item: - Dee_Decref(keyed_search_item); -err_erase_func: - Dee_Decref(erase_func); - goto err; -} - -typedef struct { - OBJECT_HEAD - DREF DeeObject *ria_elem; /* [1..1][const] The element to compare against. */ - DREF DeeObject *ria_pred; /* [0..1][const] The optional key function. */ -} RemoveIfAllWrapper; - -PRIVATE NONNULL((1)) void DCALL -ria_fini(RemoveIfAllWrapper *__restrict self) { - Dee_Decref(self->ria_elem); - Dee_XDecref(self->ria_pred); -} - -PRIVATE NONNULL((1, 2)) void DCALL -ria_visit(RemoveIfAllWrapper *__restrict self, dvisit_t proc, void *arg) { - Dee_Visit(self->ria_elem); - Dee_XVisit(self->ria_pred); -} - -PRIVATE WUNUSED NONNULL((1, 3)) DREF DeeObject *DCALL -ria_call(RemoveIfAllWrapper *self, size_t argc, DeeObject *const *argv) { - DREF DeeObject *result, *key_elem; - if unlikely(argc != 1) { - err_invalid_argc("_SeqRemoveIfAllWrapper", argc, 1, 1); - goto err; - } - if (self->ria_pred) { - key_elem = DeeObject_Call(self->ria_pred, 1, argv); - if unlikely(!key_elem) - goto err; - result = DeeObject_CmpEq(self->ria_elem, key_elem); - Dee_Decref(key_elem); - } else { - result = DeeObject_CmpEq(self->ria_elem, argv[0]); - } - return result; -err: - return NULL; -} - -PRIVATE DeeTypeObject SeqRemoveIfAllWrapper_Type = { - OBJECT_HEAD_INIT(&DeeType_Type), - /* .tp_name = */ "_SeqRemoveIfAllWrapper", - /* .tp_doc = */ NULL, - /* .tp_flags = */ TP_FNORMAL | TP_FFINAL, - /* .tp_weakrefs = */ 0, - /* .tp_features = */ TF_NONE, - /* .tp_base = */ &DeeCallable_Type, - /* .tp_init = */ { - { - /* .tp_alloc = */ { - /* .tp_ctor = */ (dfunptr_t)NULL, - /* .tp_copy_ctor = */ (dfunptr_t)NULL, - /* .tp_deep_ctor = */ (dfunptr_t)NULL, - /* .tp_any_ctor = */ (dfunptr_t)NULL, TYPE_FIXED_ALLOCATOR(RemoveIfAllWrapper) - } - }, - /* .tp_dtor = */ (void (DCALL *)(DeeObject *__restrict))&ria_fini, - /* .tp_assign = */ NULL, - /* .tp_move_assign = */ NULL - }, - /* .tp_cast = */ { - /* .tp_str = */ NULL, - /* .tp_repr = */ NULL, - /* .tp_bool = */ NULL - }, - /* .tp_call = */ (DREF DeeObject *(DCALL *)(DeeObject *, size_t, DeeObject *const *))&ria_call, - /* .tp_visit = */ (void (DCALL *)(DeeObject *__restrict, dvisit_t, void *))&ria_visit, - /* .tp_gc = */ NULL, - /* .tp_math = */ NULL, - /* .tp_cmp = */ NULL, - /* .tp_seq = */ NULL, - /* .tp_iter_next = */ NULL, - /* .tp_iterator = */ NULL, - /* .tp_attr = */ NULL, - /* .tp_with = */ NULL, - /* .tp_buffer = */ NULL, - /* .tp_methods = */ NULL, - /* .tp_getsets = */ NULL, - /* .tp_members = */ NULL, - /* .tp_class_methods = */ NULL, - /* .tp_class_getsets = */ NULL, - /* .tp_class_members = */ NULL -}; - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -make_removeif_all_wrapper(DeeObject *elem, DeeObject *key) { - /* >> return x -> keyed_search_item == (key is none ? x : key(x)); - * So simple, yet sooo complex to implement in C... */ - DREF RemoveIfAllWrapper *result; - result = DeeObject_MALLOC(RemoveIfAllWrapper); - if unlikely(!result) - goto done; - if (key) { - result->ria_elem = DeeObject_Call(key, 1, (DeeObject **)&elem); - if unlikely(!result->ria_elem) - goto err_r; - result->ria_pred = key; - Dee_Incref(key); - } else { - result->ria_elem = elem; - result->ria_pred = NULL; - Dee_Incref(elem); - } - DeeObject_Init(result, &SeqRemoveIfAllWrapper_Type); -done: - return (DREF DeeObject *)result; -err_r: - DeeObject_FREE(result); - return NULL; -} - - - -INTERN WUNUSED NONNULL((1, 4)) size_t DCALL -DeeSeq_RemoveAll(DeeObject *self, size_t start, size_t end, - DeeObject *elem, DeeObject *key) { - DREF DeeObject *keyed_search_item, *index_ob; - DREF DeeObject *item, *callback_result, *erase_func; - int error; - size_t count = 0; - DeeTypeObject *tp_self = Dee_TYPE(self); - DeeTypeMRO mro; - DeeTypeMRO_Init(&mro, tp_self); - while (tp_self != &DeeSeq_Type) { - struct type_seq *seq = tp_self->tp_seq; - if (seq) { - struct type_nsi const *nsi = seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ && - is_noninherited_nsi(tp_self, seq, nsi)) { - if (nsi->nsi_seqlike.nsi_getitem && - (nsi->nsi_seqlike.nsi_delitem || - nsi->nsi_seqlike.nsi_delrange)) { - size_t i, mylen = (*nsi->nsi_seqlike.nsi_getsize)(self); - if (end > mylen) - end = mylen; - if (end > start) { - i = end; - if (key) { - keyed_search_item = DeeObject_Call(key, 1, (DeeObject **)&elem); - if unlikely(!keyed_search_item) - goto err; - do { - --i; - item = (*nsi->nsi_seqlike.nsi_getitem)(self, i); - if unlikely(!item) - goto err_keyed_search_item; - error = DeeObject_TryCmpKeyEqAsBool(keyed_search_item, item, key); - Dee_Decref(item); - if (error != 0) { - if unlikely(error < 0) - goto err_keyed_search_item; - if (nsi->nsi_seqlike.nsi_delitem) { - if ((*nsi->nsi_seqlike.nsi_delitem)(self, i)) - goto err_keyed_search_item; - } else { - ASSERT(nsi->nsi_seqlike.nsi_delrange); - if ((*nsi->nsi_seqlike.nsi_delrange)(self, i, i + 1)) - goto err_keyed_search_item; - } - if unlikely(count == (size_t)-2) - goto err_overflow_keyed_search_item; - ++count; - } - } while (i > start); - Dee_Decref(keyed_search_item); - } else { - do { - --i; - item = (*nsi->nsi_seqlike.nsi_getitem)(self, i); - if unlikely(!item) - goto err; - error = DeeObject_TryCmpEqAsBool(elem, item); - Dee_Decref(item); - if (error != 0) { - if unlikely(error < 0) - goto err; - if (nsi->nsi_seqlike.nsi_delitem) { - if ((*nsi->nsi_seqlike.nsi_delitem)(self, i)) - goto err; - } else { - ASSERT(nsi->nsi_seqlike.nsi_delrange); - if ((*nsi->nsi_seqlike.nsi_delrange)(self, i, i + 1)) - goto err; - } - if unlikely(count == (size_t)-2) - goto err_overflow; - ++count; - } - } while (i > start); - } - } - return count; - } - } - } - /* >> `self.removeif(x -> keyed_search_item == (key is none ? x : key(x)), start, end)' */ - erase_func = get_generic_attribute(tp_self, self, (DeeObject *)&str_removeif); - if (erase_func != ITER_DONE) { - if (end > start) { - DREF DeeObject *removeif_argv[3]; - removeif_argv[0] = make_removeif_all_wrapper(elem, key); - if unlikely(!removeif_argv[0]) - goto err; - removeif_argv[1] = DeeInt_NewSize(start); - if unlikely(!removeif_argv[1]) { -err_removeif_argv_0: - Dee_Decref(removeif_argv[0]); - goto err; - } - removeif_argv[2] = DeeInt_NewSize(end); - if unlikely(!removeif_argv[2]) { /*err_removeif_argv_1:*/ - Dee_Decref(removeif_argv[1]); - goto err_removeif_argv_0; - } - callback_result = DeeObject_Call(erase_func, 3, removeif_argv); - Dee_Decref(removeif_argv[2]); - Dee_Decref(removeif_argv[1]); - Dee_Decref(removeif_argv[0]); - if unlikely(!callback_result) - goto err; - error = DeeObject_AsSize(callback_result, &count); - Dee_Decref(callback_result); - if unlikely(error) - goto err; - } - return count; - } - erase_func = get_generic_attribute(tp_self, self, (DeeObject *)&str_rremove); - if (erase_func == ITER_DONE) - erase_func = get_generic_attribute(tp_self, self, (DeeObject *)&str_remove); - if (erase_func != ITER_DONE) { - if unlikely(!erase_func) - goto err_attr; - if (end > start) { - DREF DeeObject *remove_argv[4]; - size_t remove_argc; - remove_argv[0] = elem; - remove_argv[3] = key; - remove_argc = key ? 4 : 3; - remove_argv[1] = DeeInt_NewSize(start); - if unlikely(!remove_argv[1]) - goto err_erase_func; - while (end > start) { - remove_argv[2] = DeeInt_NewSize(end); - if unlikely(!remove_argv[2]) { -err_remove_argv: - Dee_Decref(remove_argv[1]); - goto err_erase_func; - } - callback_result = DeeObject_Call(erase_func, remove_argc, remove_argv); - Dee_Decref(remove_argv[2]); - if unlikely(!callback_result) - goto err_remove_argv; - error = DeeObject_BoolInherited(callback_result); - if (error <= 0) { - if unlikely(error < 0) - goto err_remove_argv; - break; - } - if unlikely(count == (size_t)-2) { - Dee_Decref(remove_argv[1]); - Dee_Decref(erase_func); - goto err_overflow; - } - ++count; - --end; - } - Dee_Decref(remove_argv[1]); - } - Dee_Decref(erase_func); - return count; - } - if (seq && has_noninherited_delitem(tp_self, seq)) { - size_t i, mylen = DeeObject_Size(self); - if unlikely(mylen == (size_t)-1) - goto err; - if (end > mylen) - end = mylen; - if (end > start) { - i = end; - if (key) { - keyed_search_item = DeeObject_Call(key, 1, (DeeObject **)&elem); - if unlikely(!keyed_search_item) - goto err; - do { - --i; - index_ob = DeeInt_NewSize(i); - if unlikely(!index_ob) - goto err_keyed_search_item; - item = DeeObject_GetItem(self, index_ob); - if unlikely(!item) { -err_index_ob_del_key: - Dee_Decref(index_ob); - goto err_keyed_search_item; - } - error = DeeObject_TryCmpKeyEqAsBool(keyed_search_item, item, key); - Dee_Decref(item); - if (error != 0) { - if unlikely(error < 0) - goto err_index_ob_del_key; - error = (*seq->tp_delitem)(self, index_ob); - if unlikely(error < 0) - goto err_index_ob_del_key; - if unlikely(count == (size_t)-2) { - Dee_Decref(index_ob); - goto err_overflow_keyed_search_item; - } - ++count; - } - Dee_Decref(index_ob); - } while (i > start); - Dee_Decref(keyed_search_item); - } else { - do { - --i; - index_ob = DeeInt_NewSize(i); - if unlikely(!index_ob) - goto err; - item = DeeObject_GetItem(self, index_ob); - if unlikely(!item) { -err_index_ob_del: - Dee_Decref(index_ob); - goto err; - } - error = DeeObject_TryCmpEqAsBool(elem, item); - Dee_Decref(item); - if (error != 0) { - if unlikely(error < 0) - goto err_index_ob_del; - error = (*seq->tp_delitem)(self, index_ob); - if unlikely(error < 0) - goto err_index_ob_del; - if unlikely(count == (size_t)-2) { - Dee_Decref(index_ob); - goto err_overflow; - } - ++count; - } - Dee_Decref(index_ob); - } while (i > start); - } - } - return count; - } - erase_func = get_generic_attribute(tp_self, self, (DeeObject *)&str_erase); - if (erase_func != ITER_DONE) { - size_t i, mylen; - if unlikely(!erase_func) - goto err_attr; - mylen = DeeObject_Size(self); - if unlikely(mylen == (size_t)-1) - goto err_erase_func; - if (end > mylen) - end = mylen; - if (end > start) { - i = end; - if (key) { - keyed_search_item = DeeObject_Call(key, 1, (DeeObject **)&elem); - if unlikely(!keyed_search_item) - goto err; - do { - --i; - index_ob = DeeInt_NewSize(i); - if unlikely(!index_ob) - goto err_erase_func_keyed_search_item; - item = DeeObject_GetItem(self, index_ob); - if unlikely(!item) { -err_index_ob_key: - Dee_Decref(index_ob); - goto err_erase_func_keyed_search_item; - } - error = DeeObject_TryCmpKeyEqAsBool(keyed_search_item, item, key); - Dee_Decref(item); - if (error != 0) { - if unlikely(error < 0) - goto err_index_ob_key; - callback_result = DeeObject_CallPack(erase_func, 2, index_ob, DeeInt_One); - if unlikely(!callback_result) - goto err_index_ob_key; - Dee_Decref(callback_result); - if unlikely(count == (size_t)-2) { - Dee_Decref(index_ob); - Dee_Decref(erase_func); - goto err_overflow_keyed_search_item; - } - ++count; - } - Dee_Decref(index_ob); - } while (i > start); - Dee_Decref(keyed_search_item); - } else { - do { - --i; - index_ob = DeeInt_NewSize(i); - if unlikely(!index_ob) - goto err_erase_func; - item = DeeObject_GetItem(self, index_ob); - if unlikely(!item) { -err_index_ob: - Dee_Decref(index_ob); - goto err_erase_func; - } - error = DeeObject_TryCmpEqAsBool(elem, item); - Dee_Decref(item); - if (error != 0) { - if unlikely(error < 0) - goto err_index_ob; - callback_result = DeeObject_CallPack(erase_func, 2, index_ob, DeeInt_One); - if unlikely(!callback_result) - goto err_index_ob; - Dee_Decref(callback_result); - if unlikely(count == (size_t)-2) { - Dee_Decref(index_ob); - Dee_Decref(erase_func); - goto err_overflow; - } - ++count; - } - Dee_Decref(index_ob); - } while (i > start); - } - } - Dee_Decref(erase_func); - return count; - } - if (seq && has_noninherited_delrange(tp_self, seq)) { - size_t i, mylen = DeeObject_Size(self); - if unlikely(mylen == (size_t)-1) - goto err; - if (end > mylen) - end = mylen; - if (end > start) { - i = end; - if (key) { - keyed_search_item = DeeObject_Call(key, 1, (DeeObject **)&elem); - if unlikely(!keyed_search_item) - goto err; - do { - --i; - index_ob = DeeInt_NewSize(i); - if unlikely(!index_ob) - goto err_keyed_search_item; - item = DeeObject_GetItem(self, index_ob); - if unlikely(!item) { -err_index_ob_delrange_key: - Dee_Decref(index_ob); - goto err_keyed_search_item; - } - error = DeeObject_TryCmpKeyEqAsBool(keyed_search_item, item, key); - Dee_Decref(item); - if (error != 0) { - DREF DeeObject *index_plus1_ob; - if unlikely(error < 0) - goto err_index_ob_delrange_key; - index_plus1_ob = DeeInt_NewSize(i + 1); - if unlikely(!index_plus1_ob) - goto err_index_ob_delrange_key; - error = (*seq->tp_delrange)(self, index_ob, index_plus1_ob); - Dee_Decref(index_plus1_ob); - if unlikely(error < 0) - goto err_index_ob_delrange_key; - if unlikely(count == (size_t)-2) { - Dee_Decref(index_ob); - goto err_overflow_keyed_search_item; - } - ++count; - } - Dee_Decref(index_ob); - } while (i > start); - Dee_Decref(keyed_search_item); - } else { - do { - --i; - index_ob = DeeInt_NewSize(i); - if unlikely(!index_ob) - goto err; - item = DeeObject_GetItem(self, index_ob); - if unlikely(!item) { -err_index_ob_delrange: - Dee_Decref(index_ob); - goto err; - } - error = DeeObject_TryCmpEqAsBool(elem, item); - Dee_Decref(item); - if (error != 0) { - DREF DeeObject *index_plus1_ob; - if unlikely(error < 0) - goto err_index_ob_delrange; - index_plus1_ob = DeeInt_NewSize(i + 1); - if unlikely(!index_plus1_ob) - goto err_index_ob_delrange; - error = (*seq->tp_delrange)(self, index_ob, index_plus1_ob); - Dee_Decref(index_plus1_ob); - if unlikely(error < 0) - goto err_index_ob_delrange; - if unlikely(count == (size_t)-2) { - Dee_Decref(index_ob); - goto err_overflow; - } - ++count; - } - Dee_Decref(index_ob); - } while (i > start); - } - } - return count; - } - if ((tp_self = DeeTypeMRO_Next(&mro, tp_self)) == NULL) - break; - } -is_immutable: - err_immutable_sequence(self); -err: - return (size_t)-1; -err_keyed_search_item: - Dee_Decref(keyed_search_item); - goto err; -err_attr: - if (DeeError_Catch(&DeeError_NotImplemented) || - DeeError_Catch(&DeeError_AttributeError)) - goto is_immutable; - goto err; -err_erase_func_keyed_search_item: - Dee_Decref(keyed_search_item); -err_erase_func: - Dee_Decref(erase_func); - goto err; -err_overflow_keyed_search_item: - Dee_Decref(keyed_search_item); -err_overflow: - err_integer_overflow_i(sizeof(size_t) * 8, true); - goto err; -} - - -INTERN WUNUSED NONNULL((1, 4)) size_t DCALL -DeeSeq_RemoveIf(DeeObject *self, size_t start, - size_t end, DeeObject *should) { - DREF DeeObject *item, *callback_result, *erase_func; - int error; - size_t count = 0; - DeeTypeObject *tp_self = Dee_TYPE(self); - DeeTypeMRO mro; - DeeTypeMRO_Init(&mro, tp_self); - while (tp_self != &DeeSeq_Type) { - struct type_seq *seq; - callback_result = call_generic_attribute(tp_self, self, STR_removeall, - DeeString_Hash((DeeObject *)&str_removeall), - PCKuSIZ PCKuSIZ "oo", start, end, Dee_True, should); - if (callback_result != ITER_DONE) { - error = DeeObject_AsSize(callback_result, &count); - Dee_Decref(callback_result); - if unlikely(error) - goto err; - return count; - } - seq = tp_self->tp_seq; - if (seq) { - struct type_nsi const *nsi = seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ && - is_noninherited_nsi(tp_self, seq, nsi)) { - if (nsi->nsi_seqlike.nsi_getitem && - (nsi->nsi_seqlike.nsi_delitem || - nsi->nsi_seqlike.nsi_delrange)) { - size_t i, mylen = (*nsi->nsi_seqlike.nsi_getsize)(self); - if (end > mylen) - end = mylen; - if (end > start) { - i = end; - do { - --i; - item = (*nsi->nsi_seqlike.nsi_getitem)(self, i); - if unlikely(!item) - goto err; - callback_result = DeeObject_Call(should, 1, &item); - if unlikely(!callback_result) - goto err_item; - error = DeeObject_BoolInherited(callback_result); - Dee_Decref(item); - if (error != 0) { - if unlikely(error < 0) - goto err; - if (nsi->nsi_seqlike.nsi_delitem) { - if ((*nsi->nsi_seqlike.nsi_delitem)(self, i)) - goto err; - } else { - ASSERT(nsi->nsi_seqlike.nsi_delrange); - if ((*nsi->nsi_seqlike.nsi_delrange)(self, i, i + 1)) - goto err; - } - if unlikely(count == (size_t)-2) - goto err_overflow; - ++count; - } - } while (i > start); - } - return count; - } - } - } - erase_func = get_generic_attribute(tp_self, self, (DeeObject *)&str_rremove); - if (erase_func == ITER_DONE) - erase_func = get_generic_attribute(tp_self, self, (DeeObject *)&str_remove); - if (erase_func != ITER_DONE) { - if unlikely(!erase_func) - goto err_attr; - if (end > start) { - DREF DeeObject *remove_argv[4]; - remove_argv[0] = Dee_True; - remove_argv[3] = should; - remove_argv[1] = DeeInt_NewSize(start); - if unlikely(!remove_argv[1]) - goto err_erase_func; - while (end > start) { - remove_argv[2] = DeeInt_NewSize(end); - if unlikely(!remove_argv[2]) { -err_remove_argv_wraper: - Dee_Decref(remove_argv[1]); - goto err_erase_func; - } - callback_result = DeeObject_Call(erase_func, 4, remove_argv); - Dee_Decref(remove_argv[2]); - if unlikely(!callback_result) - goto err_remove_argv_wraper; - error = DeeObject_BoolInherited(callback_result); - if (error <= 0) { - if unlikely(error < 0) - goto err_remove_argv_wraper; - break; - } - if unlikely(count == (size_t)-2) { - Dee_Decref(remove_argv[1]); - Dee_Decref(erase_func); - goto err_overflow; - } - ++count; - --end; - } - Dee_Decref(remove_argv[1]); - } - Dee_Decref(erase_func); - return count; - } - if (seq && has_noninherited_delitem(tp_self, seq)) { - size_t i, mylen = DeeObject_Size(self); - if unlikely(mylen == (size_t)-1) - goto err; - if (end > mylen) - end = mylen; - if (end > start) { - i = end; - do { - DREF DeeObject *index_ob; - --i; - index_ob = DeeInt_NewSize(i); - if unlikely(!index_ob) - goto err; - item = DeeObject_GetItem(self, index_ob); - if unlikely(!item) { -err_index_ob_del: - Dee_Decref(index_ob); - goto err; - } - callback_result = DeeObject_Call(should, 1, &item); - if unlikely(!callback_result) { - Dee_Decref(item); - goto err_index_ob_del; - } - error = DeeObject_BoolInherited(callback_result); - Dee_Decref(item); - if (error != 0) { - if unlikely(error < 0) - goto err_index_ob_del; - error = (*seq->tp_delitem)(self, index_ob); - if unlikely(error < 0) - goto err_index_ob_del; - if unlikely(count == (size_t)-2) { - Dee_Decref(index_ob); - goto err_overflow; - } - ++count; - } - Dee_Decref(index_ob); - } while (i > start); - } - return count; - } - erase_func = get_generic_attribute(tp_self, self, (DeeObject *)&str_erase); - if (erase_func != ITER_DONE) { - size_t i, mylen; - if unlikely(!erase_func) - goto err_attr; - mylen = DeeObject_Size(self); - if unlikely(mylen == (size_t)-1) - goto err_erase_func; - if (end > mylen) - end = mylen; - if (end > start) { - i = end; - do { - DREF DeeObject *index_ob; - --i; - index_ob = DeeInt_NewSize(i); - if unlikely(!index_ob) - goto err_erase_func; - item = DeeObject_GetItem(self, index_ob); - if unlikely(!item) { -err_index_ob: - Dee_Decref(index_ob); - goto err_erase_func; - } - callback_result = DeeObject_Call(should, 1, &item); - if unlikely(!callback_result) { - Dee_Decref(item); - goto err_index_ob; - } - error = DeeObject_BoolInherited(callback_result); - Dee_Decref(item); - if (error != 0) { - if unlikely(error < 0) - goto err_index_ob; - callback_result = DeeObject_CallPack(erase_func, 2, index_ob, DeeInt_One); - if unlikely(!callback_result) - goto err_index_ob; - Dee_Decref(callback_result); - if unlikely(count == (size_t)-2) { - Dee_Decref(index_ob); - Dee_Decref(erase_func); - goto err_overflow; - } - ++count; - } - Dee_Decref(index_ob); - } while (i > start); - } - Dee_Decref(erase_func); - return count; - } - if (seq && has_noninherited_delrange(tp_self, seq)) { - size_t i, mylen = DeeObject_Size(self); - if unlikely(mylen == (size_t)-1) - goto err; - if (end > mylen) - end = mylen; - if (end > start) { - i = end; - do { - DREF DeeObject *index_ob; - --i; - index_ob = DeeInt_NewSize(i); - if unlikely(!index_ob) - goto err; - item = DeeObject_GetItem(self, index_ob); - if unlikely(!item) { -err_index_ob_delrange: - Dee_Decref(index_ob); - goto err; - } - callback_result = DeeObject_Call(should, 1, &item); - if unlikely(!callback_result) { - Dee_Decref(item); - goto err_index_ob_delrange; - } - error = DeeObject_BoolInherited(callback_result); - Dee_Decref(item); - if (error != 0) { - DREF DeeObject *index_plus1_ob; - if unlikely(error < 0) - goto err_index_ob_delrange; - index_plus1_ob = DeeInt_NewSize(i + 1); - if unlikely(!index_plus1_ob) - goto err_index_ob_delrange; - error = (*seq->tp_delrange)(self, index_ob, index_plus1_ob); - Dee_Decref(index_plus1_ob); - if unlikely(error < 0) { - Dee_Decref(index_ob); - goto err_index_ob_delrange; - } - if unlikely(count == (size_t)-2) { - Dee_Decref(index_ob); - goto err_overflow; - } - ++count; - } - Dee_Decref(index_ob); - } while (i > start); - } - return count; - } - if ((tp_self = DeeTypeMRO_Next(&mro, tp_self)) == NULL) - break; - } -is_immutable: - err_immutable_sequence(self); -err: - return (size_t)-1; -err_attr: - if (DeeError_Catch(&DeeError_NotImplemented) || - DeeError_Catch(&DeeError_AttributeError)) - goto is_immutable; - goto err; -err_erase_func: - Dee_Decref(erase_func); - goto err; -err_item: - Dee_Decref(item); - goto err; -err_overflow: - err_integer_overflow_i(sizeof(size_t) * 8, true); - goto err; -} - -INTERN WUNUSED NONNULL((1, 4)) size_t DCALL -DeeSeq_Fill(DeeObject *self, size_t start, size_t end, - DeeObject *value) { - DREF DeeObject *temp; - size_t mylen, result; - int error; - DeeTypeObject *tp_self = Dee_TYPE(self); - DeeTypeMRO mro; - if (start >= end) - return 0; - mylen = DeeObject_Size(self); - if unlikely(mylen == (size_t)-1) - goto err; - if (start >= mylen) - return 0; - if (end > mylen) - end = mylen; - ASSERT(start < end); - result = end - start; - if unlikely(result == (size_t)-1) - goto err_overflow; - DeeTypeMRO_Init(&mro, tp_self); - while (tp_self != &DeeSeq_Type) { - struct type_seq *seq = tp_self->tp_seq; - if (seq) { - struct type_nsi const *nsi = seq->tp_nsi; - if (seq->tp_setrange) { - /* >> this[start:end] = sequence.repeat(value, end - start); */ - DREF DeeObject *repeated_value; - repeated_value = DeeSeq_RepeatItem(value, result); - if unlikely(!repeated_value) - goto err; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ && - nsi->nsi_seqlike.nsi_setrange && end <= SSIZE_MAX) { - error = (*nsi->nsi_seqlike.nsi_setrange)(self, (dssize_t)start, (dssize_t)end, repeated_value); - } else { - DREF DeeObject *start_ob; - DREF DeeObject *end_ob; - start_ob = DeeInt_NewSize(start); - if unlikely(!start_ob) { -err_repeated_value: - Dee_Decref(repeated_value); - goto err; - } - end_ob = DeeInt_NewSize(end); - if unlikely(!end_ob) { - Dee_Decref(start_ob); - goto err_repeated_value; - } - error = (*seq->tp_setrange)(self, start_ob, end_ob, repeated_value); - Dee_Decref(end_ob); - Dee_Decref(start_ob); - } - Dee_Decref(repeated_value); - if unlikely(error) - goto err; - return result; - } - if (seq->tp_setitem) { - /* >> for (local i = start; i < end; ++i) - * >> this[i] = value; */ - size_t i; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ && - nsi->nsi_seqlike.nsi_setitem) { - for (i = start; i < end; ++i) { - error = (*nsi->nsi_seqlike.nsi_setitem)(self, i, value); - if unlikely(error) - goto err; - } - } else { - for (i = start; i < end; ++i) { - temp = DeeInt_NewSize(i); - if unlikely(!temp) - goto err; - error = (*seq->tp_setitem)(self, temp, value); - Dee_Decref(temp); - if unlikely(error) - goto err; - } - } - return result; - } - break; - } - if ((tp_self = DeeTypeMRO_Next(&mro, tp_self)) == NULL) - break; - } - err_immutable_sequence(self); -err: - return (size_t)-1; -err_overflow: - err_integer_overflow_i(sizeof(size_t) * 8, true); - return (size_t)-1; -} -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - -DECL_END - -#endif /* !GUARD_DEEMON_OBJECTS_SEQ_MUTABLE_C */ diff --git a/src/deemon/objects/set.c b/src/deemon/objects/set.c index 31ae051ed..3a095b52f 100644 --- a/src/deemon/objects/set.c +++ b/src/deemon/objects/set.c @@ -45,451 +45,6 @@ DECL_BEGIN - -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS -/* Returns the number of items found in `lhs' if all of them appear in `rhs'. - * Otherwise, return `-1' if some of them don't appear in `rhs' - * Otherwise, return `-2' on error. */ -PRIVATE WUNUSED NONNULL((1, 2)) dssize_t DCALL -set_issubset_impl(DeeObject *lhs, DeeObject *rhs) { - dssize_t result = 0; - int temp; - DREF DeeObject *lhs_iter, *lhs_item; - lhs_iter = DeeObject_Iter(lhs); - if unlikely(!lhs_iter) - goto err; - /* TODO: Use DeeObject_Foreach() */ - while (ITER_ISOK(lhs_item = DeeObject_IterNext(lhs_iter))) { - /* Check if this item appears in `rhs' */ - temp = DeeObject_ContainsAsBool(rhs, lhs_item); - Dee_Decref(lhs_item); - if unlikely(temp < 0) - goto err_iter; - if unlikely(!temp) { - result = -1; - break; - } - ++result; /* Count the number of found items. */ - if (DeeThread_CheckInterrupt()) - goto err_iter; - } - Dee_Decref(lhs_iter); - if unlikely(!lhs_item) - result = -1; - return result; -err_iter: - Dee_Decref(lhs_iter); -err: - return -2; -} - -INTERN WUNUSED NONNULL((1, 2)) int DCALL -DeeSet_IsSubSet(DeeObject *lhs, DeeObject *rhs) { - if (SetInversion_CheckExact(lhs)) { - /* An inverse set can only ever be the sub-set of another inverse set. */ - if (!SetInversion_CheckExact(rhs)) - return 0; - /* ~lhs <= ~rhs <===> rhs <= lhs */ - return DeeSet_IsSubSet(SetInversion_GetSet(rhs), - SetInversion_GetSet(lhs)); - } else { - dssize_t result = set_issubset_impl(lhs, rhs); - return unlikely(result == -2) - ? -1 - : result < 0 - ? 0 - : 1; - } -} - -INTERN WUNUSED NONNULL((1, 2)) int DCALL -DeeSet_IsTrueSubSet(DeeObject *lhs, DeeObject *rhs) { - if (SetInversion_CheckExact(lhs)) { - /* An inverse set can only ever be the sub-set of another inverse set. */ - if (!SetInversion_CheckExact(rhs)) - return 0; - /* ~lhs < ~rhs <===> rhs < lhs */ - return DeeSet_IsTrueSubSet(SetInversion_GetSet(rhs), - SetInversion_GetSet(lhs)); - } else { - dssize_t result; - size_t rhs_size; - result = set_issubset_impl(lhs, rhs); - if unlikely(result == -2) - goto err; - if (result < 0) - return 0; - /* Check the size of `rhs' to make sure - * it contains more elements than `lhs' */ - if (SetInversion_CheckExact(rhs)) - return 1; /* Inverse sets have an infinite size. */ - rhs_size = DeeObject_Size(rhs); - if unlikely(rhs_size == (size_t)-1) - goto err; - return rhs_size > (size_t)result; - } -err: - return -1; -} - -INTERN WUNUSED NONNULL((1, 2)) int DCALL -DeeSet_IsSameSet(DeeObject *lhs, DeeObject *rhs) { - dssize_t result; - size_t rhs_size; - if (SetInversion_CheckExact(lhs)) { - /* An inverse set can never equal a non-inverse set. */ - if (!SetInversion_CheckExact(rhs)) - return 0; - lhs = SetInversion_GetSet(lhs); - rhs = SetInversion_GetSet(rhs); - } - if (SetInversion_CheckExact(rhs)) - return 0; /* A regular set can never match an inverse set. */ - result = set_issubset_impl(lhs, rhs); - if unlikely(result == -2) - goto err; - if (result < 0) - return 0; - /* Check the size of `rhs' to make sure - * it contains the same number of as `lhs' */ - rhs_size = DeeObject_Size(rhs); - if unlikely(rhs_size == (size_t)-1) - goto err; - return rhs_size == (size_t)result; -err: - return -1; -} - -INTERN WUNUSED NONNULL((1, 2)) int DCALL -DeeSet_IsDisjoint(DeeObject *lhs, DeeObject *rhs) { - DREF DeeObject *iter, *item; - int result = 1; - if (SetInversion_CheckExact(lhs)) { - /* 2 inverse sets can never be disjoint, because there's - * always an imaginary object that is shared by both. */ - if (SetInversion_CheckExact(rhs)) - return 0; - /* If all elements from `rhs' are black-listed, then - * our inverse set is disjoint from it, meaning that - * `rhs' is a subset of our black-list. */ - return DeeSet_IsSubSet(rhs, SetInversion_GetSet(lhs)); - } - - /* Verify that no items from `lhs' appear in `rhs' */ - /* TODO: Use DeeObject_Foreach() */ - iter = DeeObject_Iter(lhs); - if unlikely(!iter) - goto err; - while (ITER_ISOK(item = DeeObject_IterNext(iter))) { - /* Make sure that `rhs' doesn't contain this item. */ - int temp = DeeObject_ContainsAsBool(rhs, item); - Dee_Decref(item); - if unlikely(temp < 0) - goto err_iter; - if (temp) { - result = 0; - break; - } /* Rhs does contain this one... */ - if (DeeThread_CheckInterrupt()) - goto err_iter; - } - Dee_Decref(iter); - if unlikely(!item) - result = -1; - return result; -err_iter: - Dee_Decref(iter); -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1, 2)) int DCALL -DeeSet_OperatorInplaceSub(DeeObject **__restrict p_self, - DeeObject *items) { - DeeObject *self = *p_self; - size_t i, size; - DREF DeeObject *remove_func, *callback_result, *remove_args[1]; - remove_func = DeeObject_GetAttr(self, (DeeObject *)&str_remove); - if unlikely(!remove_func) - goto err; - size = DeeFastSeq_GetSize_deprecated(items); - if (size != DEE_FASTSEQ_NOTFAST_DEPRECATED) { - for (i = 0; i < size; ++i) { - remove_args[0] = DeeFastSeq_GetItem_deprecated(items, i); - if unlikely(!remove_args[0]) - goto err_func; - callback_result = DeeObject_Call(remove_func, 1, remove_args); - Dee_Decref(remove_args[0]); - if unlikely(!callback_result) - goto err_func; - Dee_Decref(callback_result); - } - } else { - items = DeeObject_Iter(items); - if unlikely(!items) - goto err_func; - while (ITER_ISOK(remove_args[0] = DeeObject_IterNext(items))) { - callback_result = DeeObject_Call(remove_func, 1, remove_args); - Dee_Decref(remove_args[0]); - if unlikely(!callback_result) - goto err_iter; - Dee_Decref(callback_result); - if (DeeThread_CheckInterrupt()) - goto err_iter; - } - if unlikely(!remove_args[0]) - goto err_iter; - Dee_Decref(items); - } - Dee_Decref(remove_func); - return 0; -err_iter: - Dee_Decref(items); -err_func: - Dee_Decref(remove_func); -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1, 2)) int DCALL -set_inplace_mul(DeeObject **__restrict p_self, - DeeObject *count) { - size_t count_integer; - if (DeeObject_AsSize(count, &count_integer)) - goto err; - if (count_integer == 0) { - DREF DeeObject *callback_result; - callback_result = DeeObject_CallAttr(*p_self, (DeeObject *)&str_clear, 0, NULL); - if unlikely(!callback_result) - goto err; - Dee_Decref(callback_result); - } - return 0; -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1, 2)) int DCALL -DeeSet_OperatorInplaceAnd(DeeObject **__restrict p_self, - DeeObject *items) { - DeeObject *self = *p_self; - int result; - DREF DeeObject *new_self; - new_self = DeeObject_And(self, items); - if unlikely(!new_self) - goto err; - result = DeeObject_Assign(self, new_self); - Dee_Decref(new_self); - return result; -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1, 2)) int DCALL -DeeSet_OperatorInplaceAdd(DeeObject **__restrict p_self, - DeeObject *items) { - DREF DeeObject *callback_result; - callback_result = DeeObject_CallAttr(*p_self, - (DeeObject *)&str_insertall, - 1, - (DeeObject **)&items); - if unlikely(!callback_result) - goto err; - Dee_Decref(callback_result); - return 0; -err: - return -1; -} - -PRIVATE WUNUSED NONNULL((1, 2)) int DCALL -DeeSet_OperatorInplaceXor(DeeObject **__restrict p_self, - DeeObject *items) { - DeeObject *self = *p_self; - int result; - DREF DeeObject *new_self; - new_self = DeeObject_Xor(self, items); - if unlikely(!new_self) - goto err; - result = DeeObject_Assign(self, new_self); - Dee_Decref(new_self); - return result; -err: - return -1; -} - - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -set_iterself(DeeObject *__restrict self) { - if unlikely(Dee_TYPE(self) == &DeeSet_Type) { - /* Special case: Create an empty iterator. - * >> This can happen when someone tries to iterate a symbolic empty-mapping object. */ - return_empty_iterator; - } - err_unimplemented_operator(Dee_TYPE(self), OPERATOR_ITER); - return NULL; -} - -PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -set_getitem(DeeObject *self, DeeObject *UNUSED(index)) { - err_unimplemented_operator(Dee_TYPE(self), OPERATOR_GETITEM); - return NULL; -} - -PRIVATE WUNUSED NONNULL((1, 2, 3)) DREF DeeObject *DCALL -set_getrange(DeeObject *self, DeeObject *UNUSED(start), DeeObject *UNUSED(end)) { - err_unimplemented_operator(Dee_TYPE(self), OPERATOR_GETRANGE); - return NULL; -} - -PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -set_tpcontains(DeeObject *self, DeeObject *UNUSED(key)) { - if unlikely(Dee_TYPE(self) == &DeeSet_Type) - return_false; - err_unimplemented_operator(Dee_TYPE(self), OPERATOR_CONTAINS); - return NULL; -} - -PRIVATE struct type_seq DeeSet_OperatorSeq = { - /* .tp_iter = */ &set_iterself, - /* .tp_sizeob = */ NULL, - /* .tp_contains = */ &set_tpcontains, - /* .tp_getitem = */ &set_getitem, - /* .tp_delitem = */ NULL, - /* .tp_setitem = */ NULL, - /* .tp_getrange = */ &set_getrange, - /* .tp_delrange = */ NULL, - /* .tp_setrange = */ NULL -}; - - - -PRIVATE WUNUSED NONNULL((1, 2)) int DCALL -DeeSet_OperatorCompareEq(DeeObject *self, DeeObject *some_object) { - /* TODO: DeeSet_DefaultCompareEqWithForeachDefault() */ - int result = DeeSet_IsSameSet(self, some_object); - if unlikely(result < 0) - return Dee_COMPARE_ERR; - return result ? 0 : 1; -} - -PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -DeeSet_OperatorEq(DeeObject *self, DeeObject *some_object) { - /* TODO: DeeSet_DefaultCompareEqWithForeachDefault() */ - int result = DeeSet_IsSameSet(self, some_object); - if unlikely(result < 0) - goto err; - return_bool_(result); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -DeeSet_OperatorNe(DeeObject *self, DeeObject *some_object) { - /* TODO: DeeSet_DefaultCompareEqWithForeachDefault() */ - int result = DeeSet_IsSameSet(self, some_object); - if unlikely(result < 0) - goto err; - return_bool_(!result); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -DeeSet_OperatorLo(DeeObject *self, DeeObject *some_object) { - /* TODO: DeeSet_DefaultLoWithForeachDefault() */ - int result; - result = DeeSet_IsTrueSubSet(self, some_object); - if unlikely(result < 0) - goto err; - return_bool_(result); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -DeeSet_OperatorLe(DeeObject *self, DeeObject *some_object) { - /* TODO: DeeSet_DefaultLeWithForeachDefault() */ - int result; - result = DeeSet_IsSubSet(self, some_object); - if unlikely(result < 0) - goto err; - return_bool_(result); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -DeeSet_OperatorGr(DeeObject *self, DeeObject *some_object) { - /* TODO: DeeSet_DefaultGrWithForeachDefault() */ - int result; - result = DeeSet_IsTrueSubSet(some_object, self); - if unlikely(result < 0) - goto err; - return_bool_(result); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL -DeeSet_OperatorGe(DeeObject *self, DeeObject *some_object) { - /* TODO: DeeSet_DefaultGeWithForeachDefault() */ - int result; - result = DeeSet_IsSubSet(self, some_object); - if unlikely(result < 0) - goto err; - return_bool_(result); -err: - return NULL; -} - -PRIVATE struct type_cmp DeeSet_OperatorCmp = { - /* .tp_hash = */ &set_hash, - /* .tp_compare_eq = */ &DeeSet_OperatorCompareEq, - /* .tp_compare = */ NULL, - /* .tp_trycompare_eq = */ NULL, - /* .tp_eq = */ &DeeSet_OperatorEq, - /* .tp_ne = */ &DeeSet_OperatorNe, - /* .tp_lo = */ &DeeSet_OperatorLo, - /* .tp_le = */ &DeeSet_OperatorLe, - /* .tp_gr = */ &DeeSet_OperatorGr, - /* .tp_ge = */ &DeeSet_OperatorGe, -}; - -PRIVATE struct type_math DeeSet_OperatorMath = { - /* .tp_int32 = */ NULL, - /* .tp_int64 = */ NULL, - /* .tp_double = */ NULL, - /* .tp_int = */ NULL, - /* .tp_inv = */ &DeeSet_OperatorInv, - /* .tp_pos = */ NULL, - /* .tp_neg = */ NULL, - /* .tp_add = */ &DeeSet_OperatorAdd, - /* .tp_sub = */ &DeeSet_OperatorSub, - /* .tp_mul = */ NULL, - /* .tp_div = */ NULL, - /* .tp_mod = */ NULL, - /* .tp_shl = */ NULL, - /* .tp_shr = */ NULL, - /* .tp_and = */ &DeeSet_OperatorAnd, - /* .tp_or = */ &DeeSet_OperatorAdd, - /* .tp_xor = */ &DeeSet_OperatorXor, - /* .tp_pow = */ NULL, - /* .tp_inc = */ NULL, - /* .tp_dec = */ NULL, - /* .tp_inplace_add = */ &DeeSet_OperatorInplaceAdd, - /* .tp_inplace_sub = */ &DeeSet_OperatorInplaceSub, - /* .tp_inplace_mul = */ &set_inplace_mul, - /* .tp_inplace_div = */ NULL, - /* .tp_inplace_mod = */ NULL, - /* .tp_inplace_shl = */ NULL, - /* .tp_inplace_shr = */ NULL, - /* .tp_inplace_and = */ &DeeSet_OperatorInplaceAnd, - /* .tp_inplace_or = */ &DeeSet_OperatorInplaceAdd, - /* .tp_inplace_xor = */ &DeeSet_OperatorInplaceXor, - /* .tp_inplace_pow = */ NULL, -}; -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL set_difference(DeeObject *self, size_t argc, DeeObject *const *argv) { DeeObject *other; @@ -531,9 +86,6 @@ set_symmetric_difference(DeeObject *self, size_t argc, DeeObject *const *argv) { } -#ifdef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS -INTDEF WUNUSED NONNULL((1)) int DCALL si_bool(SetIntersection *__restrict self); - PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL set_isdisjoint(DeeObject *self, size_t argc, DeeObject *const *argv) { int result; @@ -567,49 +119,6 @@ set_issuperset(DeeObject *self, size_t argc, DeeObject *const *argv) { err: return NULL; } -#else /* CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -set_isdisjoint(DeeObject *self, size_t argc, DeeObject *const *argv) { - DeeObject *other; - int result; - if (DeeArg_Unpack(argc, argv, "o:isdisjoint", &other)) - goto err; - result = DeeSet_IsDisjoint(self, other); - if unlikely(result < 0) - goto err; - return_bool_(result); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -set_issubset(DeeObject *self, size_t argc, DeeObject *const *argv) { - DeeObject *other; - int result; - if (DeeArg_Unpack(argc, argv, "o:issubset", &other)) - goto err; - result = DeeSet_IsSubSet(self, other); - if unlikely(result < 0) - goto err; - return_bool_(result); -err: - return NULL; -} - -PRIVATE WUNUSED NONNULL((1)) DREF DeeObject *DCALL -set_issuperset(DeeObject *self, size_t argc, DeeObject *const *argv) { - DeeObject *other; - int result; - if (DeeArg_Unpack(argc, argv, "o:issuperset", &other)) - goto err; - result = DeeSet_IsSubSet(other, self); - if unlikely(result < 0) - goto err; - return_bool_(result); -err: - return NULL; -} -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ PRIVATE struct type_method tpconst set_methods[] = { TYPE_METHOD("difference", &set_difference, @@ -658,7 +167,6 @@ PRIVATE struct type_method tpconst set_methods[] = { "Remove and return some random key from @this set. " /**/ "If the set is empty, return @def or throw :ValueError"), -#ifdef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS TYPE_METHOD("__hash__", &default_set___hash__, "->?Dint\n" "Alias for ${(this as Set).operator hash()}"), @@ -719,7 +227,6 @@ PRIVATE struct type_method tpconst set_methods[] = { TYPE_METHOD("__inplace_or__", &default_set___inplace_or__, "(rhs:?S?O)->?DSet\n" "Alias for ?#__inplace_and__"), -#endif /* CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ TYPE_METHOD_END }; @@ -907,11 +414,7 @@ PUBLIC DeeTypeObject DeeSet_Type = { "}"), /* .tp_flags = */ TP_FNORMAL | TP_FABSTRACT | TP_FNAMEOBJECT, /* Generic base class type. */ /* .tp_weakrefs = */ 0, -#ifdef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS /* .tp_features = */ TF_NONE | (Dee_SEQCLASS_SET << Dee_TF_SEQCLASS_SHFT), -#else /* CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - /* .tp_features = */ TF_NONE, -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ /* .tp_base = */ &DeeSeq_Type, /* .tp_init = */ { { diff --git a/src/deemon/runtime/operator-inherit.c b/src/deemon/runtime/operator-inherit.c index 77ed98dc7..e0618b5f8 100644 --- a/src/deemon/runtime/operator-inherit.c +++ b/src/deemon/runtime/operator-inherit.c @@ -163,8 +163,6 @@ Dee_type_seq_has_custom_tp_setitem_string_len_hash(struct type_seq const *__rest } /* Optimizations when inheriting certain operators. */ -#ifdef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS - typedef WUNUSED_T NONNULL_T((1)) int (DCALL *DeeType_tp_bool_t)(DeeObject *__restrict self); typedef WUNUSED_T NONNULL_T((1)) DREF DeeObject *(DCALL *DeeType_tp_iter_t)(DeeObject *__restrict self); typedef WUNUSED_T NONNULL_T((1)) DREF DeeObject *(DCALL *DeeType_tp_sizeob_t)(DeeObject *__restrict self); @@ -1061,82 +1059,11 @@ DeeType_Optimize_tp_inplace_xor(DeeTypeObject *__restrict dst, DeeType_tp_inplac return tp_inplace_xor; } -#else /* CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ -#define DeeType_Optimize_tp_bool(self, tp_bool) tp_bool -#define DeeType_Optimize_tp_iter(self, tp_iter) tp_iter -#define DeeType_Optimize_tp_sizeob(self, tp_sizeob) tp_sizeob -#define DeeType_Optimize_tp_contains(self, tp_contains) tp_contains -#define DeeType_Optimize_tp_getitem(self, tp_getitem) tp_getitem -#define DeeType_Optimize_tp_delitem(self, tp_delitem) tp_delitem -#define DeeType_Optimize_tp_setitem(self, tp_setitem) tp_setitem -#define DeeType_Optimize_tp_getrange(self, tp_getrange) tp_getrange -#define DeeType_Optimize_tp_delrange(self, tp_delrange) tp_delrange -#define DeeType_Optimize_tp_setrange(self, tp_setrange) tp_setrange -#define DeeType_Optimize_tp_foreach(self, tp_foreach) tp_foreach -#define DeeType_Optimize_tp_foreach_pair(self, tp_foreach_pair) tp_foreach_pair -#define DeeType_Optimize_tp_enumerate(self, tp_enumerate) tp_enumerate -#define DeeType_Optimize_tp_enumerate_index(self, tp_enumerate_index) tp_enumerate_index -//efine DeeType_Optimize_tp_iterkeys(self, tp_iterkeys) tp_iterkeys -#define DeeType_Optimize_tp_bounditem(self, tp_bounditem) tp_bounditem -#define DeeType_Optimize_tp_hasitem(self, tp_hasitem) tp_hasitem -#define DeeType_Optimize_tp_size(self, tp_size) tp_size -#define DeeType_Optimize_tp_size_fast(self, tp_size_fast) tp_size_fast -#define DeeType_Optimize_tp_getitem_index(self, tp_getitem_index) tp_getitem_index -#define DeeType_Optimize_tp_delitem_index(self, tp_delitem_index) tp_delitem_index -#define DeeType_Optimize_tp_setitem_index(self, tp_setitem_index) tp_setitem_index -#define DeeType_Optimize_tp_bounditem_index(self, tp_bounditem_index) tp_bounditem_index -#define DeeType_Optimize_tp_hasitem_index(self, tp_hasitem_index) tp_hasitem_index -#define DeeType_Optimize_tp_getrange_index(self, tp_getrange_index) tp_getrange_index -#define DeeType_Optimize_tp_delrange_index(self, tp_delrange_index) tp_delrange_index -#define DeeType_Optimize_tp_setrange_index(self, tp_setrange_index) tp_setrange_index -#define DeeType_Optimize_tp_getrange_index_n(self, tp_getrange_index_n) tp_getrange_index_n -#define DeeType_Optimize_tp_delrange_index_n(self, tp_delrange_index_n) tp_delrange_index_n -#define DeeType_Optimize_tp_setrange_index_n(self, tp_setrange_index_n) tp_setrange_index_n -#define DeeType_Optimize_tp_trygetitem(self, tp_trygetitem) tp_trygetitem -#define DeeType_Optimize_tp_trygetitem_index(self, tp_trygetitem_index) tp_trygetitem_index -#define DeeType_Optimize_tp_trygetitem_string_hash(self, tp_trygetitem_string_hash) tp_trygetitem_string_hash -#define DeeType_Optimize_tp_getitem_string_hash(self, tp_getitem_string_hash) tp_getitem_string_hash -#define DeeType_Optimize_tp_delitem_string_hash(self, tp_delitem_string_hash) tp_delitem_string_hash -#define DeeType_Optimize_tp_setitem_string_hash(self, tp_setitem_string_hash) tp_setitem_string_hash -#define DeeType_Optimize_tp_bounditem_string_hash(self, tp_bounditem_string_hash) tp_bounditem_string_hash -#define DeeType_Optimize_tp_hasitem_string_hash(self, tp_hasitem_string_hash) tp_hasitem_string_hash -#define DeeType_Optimize_tp_trygetitem_string_len_hash(self, tp_trygetitem_string_len_hash) tp_trygetitem_string_len_hash -#define DeeType_Optimize_tp_getitem_string_len_hash(self, tp_getitem_string_len_hash) tp_getitem_string_len_hash -#define DeeType_Optimize_tp_delitem_string_len_hash(self, tp_delitem_string_len_hash) tp_delitem_string_len_hash -#define DeeType_Optimize_tp_setitem_string_len_hash(self, tp_setitem_string_len_hash) tp_setitem_string_len_hash -#define DeeType_Optimize_tp_bounditem_string_len_hash(self, tp_bounditem_string_len_hash) tp_bounditem_string_len_hash -#define DeeType_Optimize_tp_hasitem_string_len_hash(self, tp_hasitem_string_len_hash) tp_hasitem_string_len_hash -#define DeeType_Optimize_tp_hash(self, tp_hash) tp_hash -#define DeeType_Optimize_tp_compare_eq(self, tp_compare_eq) tp_compare_eq -#define DeeType_Optimize_tp_compare(self, tp_compare) tp_compare -#define DeeType_Optimize_tp_trycompare_eq(self, tp_trycompare_eq) tp_trycompare_eq -#define DeeType_Optimize_tp_eq(self, tp_eq) tp_eq -#define DeeType_Optimize_tp_ne(self, tp_ne) tp_ne -#define DeeType_Optimize_tp_lo(self, tp_lo) tp_lo -#define DeeType_Optimize_tp_le(self, tp_le) tp_le -#define DeeType_Optimize_tp_gr(self, tp_gr) tp_gr -#define DeeType_Optimize_tp_ge(self, tp_ge) tp_ge -#define DeeType_Optimize_tp_cmp(dst, tp_cmp) tp_cmp -#define DeeType_Optimize_tp_inv(self, tp_inv) tp_inv -#define DeeType_Optimize_tp_add(self, tp_add) tp_add -#define DeeType_Optimize_tp_sub(self, tp_sub) tp_sub -#define DeeType_Optimize_tp_and(self, tp_and) tp_and -#define DeeType_Optimize_tp_or(self, tp_or) tp_or -#define DeeType_Optimize_tp_xor(self, tp_xor) tp_xor -#define DeeType_Optimize_tp_inplace_add(self, tp_inplace_add) tp_inplace_add -#define DeeType_Optimize_tp_inplace_sub(self, tp_inplace_sub) tp_inplace_sub -#define DeeType_Optimize_tp_inplace_mul(self, tp_inplace_mul) tp_inplace_mul -#define DeeType_Optimize_tp_inplace_and(self, tp_inplace_and) tp_inplace_and -#define DeeType_Optimize_tp_inplace_or(self, tp_inplace_or) tp_inplace_or -#define DeeType_Optimize_tp_inplace_xor(self, tp_inplace_xor) tp_inplace_xor -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - #define DeeType_Optimize_tp_asvector(self, tp_asvector) tp_asvector #define DeeType_Optimize_tp_asvector_nothrow(self, tp_asvector_nothrow) tp_asvector_nothrow #define DeeType_Optimize_tp_unpack(self, tp_unpack) tp_unpack #define DeeType_Optimize_tp_unpack_ub(self, tp_unpack_ub) tp_unpack_ub - #define DeeType_Optimize_tp_deepload(self, tp_deepload) tp_deepload #define DeeType_Optimize_tp_assign(self, tp_assign) tp_assign #define DeeType_Optimize_tp_move_assign(self, tp_move_assign) tp_move_assign diff --git a/src/deemon/runtime/operator.c b/src/deemon/runtime/operator.c index 802043ed6..6d15f4777 100644 --- a/src/deemon/runtime/operator.c +++ b/src/deemon/runtime/operator.c @@ -15422,43 +15422,11 @@ DEFINE_OPERATOR(size_t, SizeFast, (DeeObject *RESTRICT_IF_NOTYPE self)) { } DEFINE_OPERATOR(size_t, Size, (DeeObject *RESTRICT_IF_NOTYPE self)) { -#ifdef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS LOAD_TP_SELF; if likely(likely(tp_self->tp_seq && tp_self->tp_seq->tp_size) || unlikely(DeeType_InheritSize(tp_self) && tp_self->tp_seq->tp_size)) return DeeType_INVOKE_SIZE(tp_self, self); return (size_t)err_unimplemented_operator(tp_self, OPERATOR_SIZE); -#else /* CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - DREF DeeObject *sizeob; - size_t result; - LOAD_TP_SELF; - if likely((tp_self->tp_seq && tp_self->tp_seq->tp_sizeob) || - unlikely(DeeType_InheritSize(tp_self))) { - struct type_nsi const *nsi; - /* NSI optimizations. */ - nsi = tp_self->tp_seq->tp_nsi; - if (nsi) { - ASSERT(nsi->nsi_common.nsi_getsize); - return (*nsi->nsi_common.nsi_getsize)(self); - } - sizeob = DeeType_INVOKE_SIZEOB(tp_self, self); - if unlikely(!sizeob) - goto err; - if (DeeObject_AsSize(sizeob, &result)) - goto err_ob; - Dee_Decref(sizeob); - /* Deal with negative-one */ - if unlikely(result == (size_t)-1) - return err_integer_overflow_i(sizeof(size_t) * 8, true); - return result; - } - err_unimplemented_operator(tp_self, OPERATOR_SIZE); - return (size_t)-1; -err_ob: - Dee_Decref(sizeob); -err: - return (size_t)-1; -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ } @@ -15578,7 +15546,6 @@ DeeObject_ExtendInherited(/*inherit(on_success)*/ DREF DeeObject *self, size_t a #endif /* !DEFINE_TYPED_OPERATORS */ -#ifdef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS DEFINE_OPERATOR(DREF DeeObject *, GetRangeBeginIndex, (DeeObject *self, Dee_ssize_t start, DeeObject *end)) { Dee_ssize_t end_index; @@ -15809,325 +15776,6 @@ DEFINE_OPERATOR(int, SetRangeIndexN, return err_unimplemented_operator(tp_self, OPERATOR_SETRANGE); } -#else /* CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ -DEFINE_OPERATOR(DREF DeeObject *, GetRangeBeginIndex, - (DeeObject *self, Dee_ssize_t begin, DeeObject *end)) { - LOAD_TP_SELF; - ASSERT_OBJECT(end); - if likely(likely(tp_self->tp_seq && tp_self->tp_seq->tp_getrange) || - unlikely(DeeType_InheritGetRange(tp_self))) { - Dee_ssize_t end_index; - struct type_nsi const *nsi; - DREF DeeObject *begin_ob, *result; - /* NSI optimizations. */ - nsi = tp_self->tp_seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ) { - if (DeeNone_Check(end)) { - if (nsi->nsi_seqlike.nsi_getrange_n) - return (*nsi->nsi_seqlike.nsi_getrange_n)(self, begin); - } else if (nsi->nsi_seqlike.nsi_getrange) { - if (DeeObject_AsSSize(end, &end_index)) - goto err; - return (*nsi->nsi_seqlike.nsi_getrange)(self, begin, end_index); - } - } - begin_ob = DeeInt_NewSSize(begin); - if unlikely(!begin_ob) - goto err; - result = DeeType_INVOKE_GETRANGE(tp_self, self, begin_ob, end); - Dee_Decref(begin_ob); - return result; - } - err_unimplemented_operator(tp_self, OPERATOR_GETRANGE); -err: - return NULL; -} - -DEFINE_OPERATOR(DREF DeeObject *, GetRangeEndIndex, - (DeeObject *self, DeeObject *begin, Dee_ssize_t end)) { - LOAD_TP_SELF; - if likely(likely(tp_self->tp_seq && tp_self->tp_seq->tp_getrange) || - unlikely(DeeType_InheritGetRange(tp_self))) { - Dee_ssize_t begin_index; - DREF DeeObject *end_ob, *result; - struct type_nsi const *nsi; - /* NSI optimizations. */ - nsi = tp_self->tp_seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ) { - if (nsi->nsi_seqlike.nsi_getrange) { - if (DeeObject_AsSSize(begin, &begin_index)) - goto err; - return (*nsi->nsi_seqlike.nsi_getrange)(self, begin_index, end); - } - } - end_ob = DeeInt_NewSSize(end); - if unlikely(!end_ob) - goto err; - result = DeeType_INVOKE_GETRANGE(tp_self, self, begin, end_ob); - Dee_Decref(end_ob); - return result; - } - err_unimplemented_operator(tp_self, OPERATOR_GETRANGE); -err: - return NULL; -} - -DEFINE_OPERATOR(DREF DeeObject *, GetRangeIndex, - (DeeObject *RESTRICT_IF_NOTYPE self, Dee_ssize_t begin, Dee_ssize_t end)) { - LOAD_TP_SELF; - if likely(likely(tp_self->tp_seq && tp_self->tp_seq->tp_getrange) || - unlikely(DeeType_InheritGetRange(tp_self))) { - DREF DeeObject *begin_ob, *end_ob, *result; - struct type_nsi const *nsi; - /* NSI optimizations. */ - nsi = tp_self->tp_seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ) { - if (nsi->nsi_seqlike.nsi_getrange) - return (*nsi->nsi_seqlike.nsi_getrange)(self, begin, end); - } - begin_ob = DeeInt_NewSSize(begin); - if unlikely(!begin_ob) - goto err; - end_ob = DeeInt_NewSSize(end); - if unlikely(!end_ob) { - Dee_Decref(begin_ob); - goto err; - } - result = DeeType_INVOKE_GETRANGE(tp_self, self, begin_ob, end_ob); - Dee_Decref(end_ob); - Dee_Decref(begin_ob); - return result; - } - err_unimplemented_operator(tp_self, OPERATOR_GETRANGE); -err: - return NULL; -} - -DEFINE_OPERATOR(int, DelRangeBeginIndex, - (DeeObject *self, Dee_ssize_t begin, DeeObject *end)) { - LOAD_TP_SELF; - ASSERT_OBJECT(end); - if likely(likely(tp_self->tp_seq && tp_self->tp_seq->tp_delrange) || - unlikely(DeeType_InheritSetRange(tp_self))) { - int result; - DREF DeeObject *begin_ob; - struct type_nsi const *nsi; - /* NSI optimizations. */ - nsi = tp_self->tp_seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ) { - if (DeeNone_Check(end)) { - if (nsi->nsi_seqlike.nsi_delrange_n) - return (*nsi->nsi_seqlike.nsi_delrange_n)(self, begin); - } else if (nsi->nsi_seqlike.nsi_delrange) { - Dee_ssize_t end_index; - if (DeeObject_AsSSize(end, &end_index)) - goto err; - return (*nsi->nsi_seqlike.nsi_delrange)(self, begin, end_index); - } - } - begin_ob = DeeInt_NewSSize(begin); - if unlikely(!begin_ob) - goto err; - result = DeeType_INVOKE_DELRANGE(tp_self, self, begin_ob, end); - Dee_Decref(begin_ob); - return result; - } - err_unimplemented_operator(tp_self, OPERATOR_SETRANGE); -err: - return -1; -} - -DEFINE_OPERATOR(int, DelRangeEndIndex, - (DeeObject *self, DeeObject *begin, Dee_ssize_t end)) { - LOAD_TP_SELF; - ASSERT_OBJECT(begin); - if likely(likely(tp_self->tp_seq && tp_self->tp_seq->tp_delrange) || - unlikely(DeeType_InheritSetRange(tp_self))) { - int result; - DREF DeeObject *end_ob; - struct type_nsi const *nsi; - /* NSI optimizations. */ - nsi = tp_self->tp_seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ) { - if (nsi->nsi_seqlike.nsi_delrange) { - Dee_ssize_t start_index; - if (DeeObject_AsSSize(begin, &start_index)) - goto err; - return (*nsi->nsi_seqlike.nsi_delrange)(self, start_index, end); - } - } - end_ob = DeeInt_NewSSize(end); - if unlikely(!end_ob) - goto err; - result = DeeType_INVOKE_DELRANGE(tp_self, self, begin, end_ob); - Dee_Decref(end_ob); - return result; - } - err_unimplemented_operator(tp_self, OPERATOR_SETRANGE); -err: - return -1; -} - -DEFINE_OPERATOR(int, DelRangeIndex, - (DeeObject *self, Dee_ssize_t begin, Dee_ssize_t end)) { - LOAD_TP_SELF; - if likely(likely(tp_self->tp_seq && tp_self->tp_seq->tp_delrange) || - unlikely(DeeType_InheritSetRange(tp_self))) { - DREF DeeObject *begin_ob, *end_ob; - int result; - struct type_nsi const *nsi; - /* NSI optimizations. */ - nsi = tp_self->tp_seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ) { - if (nsi->nsi_seqlike.nsi_delrange) - return (*nsi->nsi_seqlike.nsi_delrange)(self, begin, end); - } - begin_ob = DeeInt_NewSSize(begin); - if unlikely(!begin_ob) - goto err; - end_ob = DeeInt_NewSSize(end); - if unlikely(!end_ob) { - Dee_Decref(begin_ob); - goto err; - } - result = DeeType_INVOKE_DELRANGE(tp_self, self, begin_ob, end_ob); - Dee_Decref(end_ob); - Dee_Decref(begin_ob); - return result; - } - err_unimplemented_operator(tp_self, OPERATOR_SETRANGE); -err: - return -1; -} - -DEFINE_OPERATOR(int, SetRangeBeginIndex, - (DeeObject *self, Dee_ssize_t begin, DeeObject *end, DeeObject *values)) { - LOAD_TP_SELF; - ASSERT_OBJECT(end); - ASSERT_OBJECT(values); - if likely(likely(tp_self->tp_seq && tp_self->tp_seq->tp_setrange) || - unlikely(DeeType_InheritSetRange(tp_self))) { - int result; - DREF DeeObject *begin_ob; - struct type_nsi const *nsi; - /* NSI optimizations. */ - nsi = tp_self->tp_seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ) { - if (DeeNone_Check(end)) { - if (nsi->nsi_seqlike.nsi_setrange_n) - return (*nsi->nsi_seqlike.nsi_setrange_n)(self, begin, values); - } else if (nsi->nsi_seqlike.nsi_setrange) { - Dee_ssize_t end_index; - if (DeeObject_AsSSize(end, &end_index)) - goto err; - return (*nsi->nsi_seqlike.nsi_setrange)(self, begin, end_index, values); - } - } - begin_ob = DeeInt_NewSSize(begin); - if unlikely(!begin_ob) - goto err; - result = DeeType_INVOKE_SETRANGE(tp_self, self, begin_ob, end, values); - Dee_Decref(begin_ob); - return result; - } - err_unimplemented_operator(tp_self, OPERATOR_SETRANGE); -err: - return -1; -} - -DEFINE_OPERATOR(int, SetRangeEndIndex, - (DeeObject *self, DeeObject *begin, Dee_ssize_t end, DeeObject *values)) { - LOAD_TP_SELF; - ASSERT_OBJECT(begin); - ASSERT_OBJECT(values); - if likely(likely(tp_self->tp_seq && tp_self->tp_seq->tp_setrange) || - unlikely(DeeType_InheritSetRange(tp_self))) { - int result; - DREF DeeObject *end_ob; - struct type_nsi const *nsi; - /* NSI optimizations. */ - nsi = tp_self->tp_seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ) { - if (nsi->nsi_seqlike.nsi_setrange) { - Dee_ssize_t start_index; - if (DeeObject_AsSSize(begin, &start_index)) - goto err; - return (*nsi->nsi_seqlike.nsi_setrange)(self, start_index, end, values); - } - } - end_ob = DeeInt_NewSSize(end); - if unlikely(!end_ob) - goto err; - result = DeeType_INVOKE_SETRANGE(tp_self, self, begin, end_ob, values); - Dee_Decref(end_ob); - return result; - } - err_unimplemented_operator(tp_self, OPERATOR_SETRANGE); -err: - return -1; -} - -DEFINE_OPERATOR(int, SetRangeIndex, - (DeeObject *self, Dee_ssize_t begin, Dee_ssize_t end, DeeObject *values)) { - LOAD_TP_SELF; - ASSERT_OBJECT(values); - if likely(likely(tp_self->tp_seq && tp_self->tp_seq->tp_setrange) || - unlikely(DeeType_InheritSetRange(tp_self))) { - DREF DeeObject *begin_ob, *end_ob; - int result; - struct type_nsi const *nsi; - /* NSI optimizations. */ - nsi = tp_self->tp_seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ) { - if (nsi->nsi_seqlike.nsi_setrange) - return (*nsi->nsi_seqlike.nsi_setrange)(self, begin, end, values); - } - begin_ob = DeeInt_NewSSize(begin); - if unlikely(!begin_ob) - goto err; - end_ob = DeeInt_NewSSize(end); - if unlikely(!end_ob) { - Dee_Decref(begin_ob); - goto err; - } - result = DeeType_INVOKE_SETRANGE(tp_self, self, begin_ob, end_ob, values); - Dee_Decref(end_ob); - Dee_Decref(begin_ob); - return result; - } - err_unimplemented_operator(tp_self, OPERATOR_SETRANGE); -err: - return -1; -} - - -DEFINE_OPERATOR(DREF DeeObject *, GetRangeIndexN, - (DeeObject *RESTRICT_IF_NOTYPE self, Dee_ssize_t begin)) { -#ifdef DEFINE_TYPED_OPERATORS - return DeeObject_TGetRangeBeginIndex(tp_self, self, begin, Dee_None); -#else /* DEFINE_TYPED_OPERATORS */ - return DeeObject_GetRangeBeginIndex(self, begin, Dee_None); -#endif /* !DEFINE_TYPED_OPERATORS */ -} - -DEFINE_OPERATOR(int, DelRangeIndexN, (DeeObject *RESTRICT_IF_NOTYPE self, Dee_ssize_t begin)) { -#ifdef DEFINE_TYPED_OPERATORS - return DeeObject_TDelRangeBeginIndex(tp_self, self, begin, Dee_None); -#else /* DEFINE_TYPED_OPERATORS */ - return DeeObject_DelRangeBeginIndex(self, begin, Dee_None); -#endif /* !DEFINE_TYPED_OPERATORS */ -} - -DEFINE_OPERATOR(int, SetRangeIndexN, (DeeObject *self, Dee_ssize_t begin, DeeObject *values)) { -#ifdef DEFINE_TYPED_OPERATORS - return DeeObject_TSetRangeBeginIndex(tp_self, self, begin, Dee_None, values); -#else /* DEFINE_TYPED_OPERATORS */ - return DeeObject_SetRangeBeginIndex(self, begin, Dee_None, values); -#endif /* !DEFINE_TYPED_OPERATORS */ -} -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - - DEFINE_OPERATOR(int, DelRange, (DeeObject *self, DeeObject *start, DeeObject *end)) { LOAD_TP_SELF; @@ -16149,7 +15797,6 @@ DEFINE_OPERATOR(int, SetRange, -#ifdef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS DEFINE_OPERATOR(int, BoundItem, (DeeObject *self, DeeObject *index)) { LOAD_TP_SELF; @@ -16350,661 +15997,6 @@ DEFINE_OPERATOR(int, SetItemStringLenHash, return err_unimplemented_operator(tp_self, OPERATOR_SETITEM); } -#else /* CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ -DEFINE_OPERATOR(int, BoundItem, (DeeObject *self, DeeObject *index)) { - DREF DeeObject *result; - size_t i; - LOAD_TP_SELF; - if likely(likely(tp_self->tp_seq && tp_self->tp_seq->tp_getitem) || - unlikely(DeeType_InheritGetItem(tp_self))) { - struct type_nsi const *nsi; - nsi = tp_self->tp_seq->tp_nsi; - if (nsi) { - if (nsi->nsi_class == TYPE_SEQX_CLASS_SEQ) { - if (nsi->nsi_seqlike.nsi_getitem_fast) { - size_t size; - if (DeeObject_AsSize(index, &i)) - goto err; - if (nsi->nsi_seqlike.nsi_getsize_fast) { - size = (*nsi->nsi_seqlike.nsi_getsize_fast)(self); - } else { - size = (*nsi->nsi_seqlike.nsi_getsize)(self); - if unlikely(size == (size_t)-1) - goto err; - } - if unlikely(i >= size) { - /* Bad index */ - return -2; - } - result = (*nsi->nsi_seqlike.nsi_getitem_fast)(self, i); - if (!result) - return 0; - Dee_Decref(result); - return 1; - } - if (nsi->nsi_seqlike.nsi_getitem) { - if (DeeObject_AsSize(index, &i)) - goto err; - result = (*nsi->nsi_seqlike.nsi_getitem)(self, i); - goto check_result; - } - } else if (nsi->nsi_class == TYPE_SEQX_CLASS_MAP) { - if (nsi->nsi_maplike.nsi_getdefault) { - result = (*nsi->nsi_maplike.nsi_getdefault)(self, index, ITER_DONE); - if (result == ITER_DONE) - return -2; - goto check_result; - } - /* We can't actually substitute with `operator contains', because - * if we did that, we may get a false positive for a valid key, but - * an unbound key none-the-less. */ - } - } - - /* Fallback: create an integer object and use it for indexing. */ - result = DeeType_INVOKE_GETITEM(tp_self, self, index); -check_result: - if (!result) { - if (DeeError_Catch(&DeeError_UnboundItem)) - return 0; - if (DeeError_Catch(&DeeError_IndexError) || - DeeError_Catch(&DeeError_KeyError)) - return -2; - goto err; - } - Dee_Decref(result); - return 1; - } - err_unimplemented_operator(tp_self, OPERATOR_GETITEM); -err: - return -1; -} - -DEFINE_OPERATOR(int, BoundItemIndex, - (DeeObject *RESTRICT_IF_NOTYPE self, size_t index)) { - DREF DeeObject *result, *index_ob; - LOAD_TP_SELF; - if likely(likely(tp_self->tp_seq && tp_self->tp_seq->tp_getitem) || - unlikely(DeeType_InheritGetItem(tp_self))) { - struct type_nsi const *nsi; - nsi = tp_self->tp_seq->tp_nsi; - if (nsi) { - if (nsi->nsi_class == TYPE_SEQX_CLASS_SEQ) { - if (nsi->nsi_seqlike.nsi_getitem_fast) { - size_t size; - if (nsi->nsi_seqlike.nsi_getsize_fast) { - size = (*nsi->nsi_seqlike.nsi_getsize_fast)(self); - } else { - size = (*nsi->nsi_seqlike.nsi_getsize)(self); - if unlikely(size == (size_t)-1) - goto err; - } - if unlikely(index >= size) - return -2; /* Bad index */ - result = (*nsi->nsi_seqlike.nsi_getitem_fast)(self, index); - if (!result) - return 0; - Dee_Decref(result); - return 1; - } - if (nsi->nsi_seqlike.nsi_getitem) { - result = (*nsi->nsi_seqlike.nsi_getitem)(self, index); - goto check_result; - } - } else if (nsi->nsi_class == TYPE_SEQX_CLASS_MAP) { - if (nsi->nsi_maplike.nsi_getdefault) { - index_ob = DeeInt_NewSize(index); - if unlikely(!index_ob) - goto err; - result = (*nsi->nsi_maplike.nsi_getdefault)(self, index_ob, ITER_DONE); - if (result == ITER_DONE) { - Dee_Decref(index_ob); - return -2; - } - Dee_Decref(index_ob); - goto check_result; - } - /* We can't actually substitute with `operator contains', because - * if we did that, we may get a false positive for a valid key, but - * an unbound key none-the-less. */ - } - } - - /* Fallback: create an integer object and use it for indexing. */ - index_ob = DeeInt_NewSize(index); - if unlikely(!index_ob) - goto err; - result = DeeType_INVOKE_GETITEM(tp_self, self, index_ob); - Dee_Decref(index_ob); -check_result: - if (!result) { - if (DeeError_Catch(&DeeError_UnboundItem)) - return 0; - if (DeeError_Catch(&DeeError_IndexError) || - DeeError_Catch(&DeeError_KeyError)) - return -2; - goto err; - } - Dee_Decref(result); - return 1; - } - err_unimplemented_operator(tp_self, OPERATOR_GETITEM); -err: - return -1; -} - -DEFINE_OPERATOR(int, BoundItemStringHash, (DeeObject *self, char const *key, dhash_t hash)) { - int result; - DeeObject *orig_self = self; - DREF DeeObject *keyob; - LOAD_TP_SELF; - - /* Optimization for specific types. */ -again: - if (tp_self == &DeeSuper_Type) { - tp_self = DeeSuper_TYPE(self); - self = DeeSuper_SELF(self); - goto again; - } - - /* Fallback: create a temporary string object and use it for indexing. */ - keyob = DeeString_NewWithHash(key, hash); - if unlikely(!keyob) - goto err; - result = DeeObject_BoundItem(orig_self, keyob); - Dee_Decref(keyob); - return result; -err: - return -1; -} - -DEFINE_OPERATOR(int, BoundItemStringLenHash, - (DeeObject *self, char const *key, size_t keylen, dhash_t hash)) { - int result; - DeeObject *orig_self = self; - DREF DeeObject *keyob; - LOAD_TP_SELF; - - /* Optimization for specific types. */ -again: - if (tp_self == &DeeSuper_Type) { - tp_self = DeeSuper_TYPE(self); - self = DeeSuper_SELF(self); - goto again; - } - - /* Fallback: create a temporary string object and use it for indexing. */ - keyob = DeeString_NewSizedWithHash(key, keylen, hash); - if unlikely(!keyob) - goto err; - result = DeeObject_BoundItem(orig_self, keyob); - Dee_Decref(keyob); - return result; -err: - return -1; -} - - -/* Check if a given item exists (`deemon.hasitem(self, index)') - * @return: 1: Does exists - * @return: 0: Doesn't exist - * @return: -1: Error. */ -DEFINE_OPERATOR(int, HasItem, (DeeObject *self, DeeObject *index)) { - DREF DeeObject *result; - size_t i; - LOAD_TP_SELF; - if likely(likely(tp_self->tp_seq && tp_self->tp_seq->tp_getitem) || - unlikely(DeeType_InheritGetItem(tp_self))) { - struct type_nsi const *nsi; - nsi = tp_self->tp_seq->tp_nsi; - if (nsi) { - if (nsi->nsi_class == TYPE_SEQX_CLASS_SEQ) { - size_t size; - if (DeeObject_AsSize(index, &i)) - goto err; - if (nsi->nsi_seqlike.nsi_getsize_fast) { - size = (*nsi->nsi_seqlike.nsi_getsize_fast)(self); - } else { - size = (*nsi->nsi_seqlike.nsi_getsize)(self); - if unlikely(size == (size_t)-1) - goto err; - } - return i < size; - } else if (nsi->nsi_class == TYPE_SEQX_CLASS_MAP) { - if (nsi->nsi_maplike.nsi_getdefault) { - result = (*nsi->nsi_maplike.nsi_getdefault)(self, index, ITER_DONE); - if (result == ITER_DONE) - return 0; - goto check_result; - } - /* We can't actually substitute with `operator contains', because - * if we did that, we may get a false positive for a valid key, but - * an unbound key none-the-less. */ - } - } - - /* Fallback: create an integer object and use it for indexing. */ - result = DeeType_INVOKE_GETITEM(tp_self, self, index); -check_result: - if (!result) { - if (DeeError_Catch(&DeeError_UnboundItem) || - DeeError_Catch(&DeeError_IndexError) || - DeeError_Catch(&DeeError_KeyError)) - return 0; - goto err; - } - Dee_Decref(result); - return 1; - } - err_unimplemented_operator(tp_self, OPERATOR_GETITEM); -err: - return -1; -} - -DEFINE_OPERATOR(int, HasItemIndex, - (DeeObject *RESTRICT_IF_NOTYPE self, size_t index)) { - DREF DeeObject *result, *index_ob; - LOAD_TP_SELF; - if likely(likely(tp_self->tp_seq && tp_self->tp_seq->tp_getitem) || - unlikely(DeeType_InheritGetItem(tp_self))) { - struct type_nsi const *nsi; - nsi = tp_self->tp_seq->tp_nsi; - if (nsi) { - if (nsi->nsi_class == TYPE_SEQX_CLASS_SEQ) { - size_t size; - if (nsi->nsi_seqlike.nsi_getsize_fast) { - size = (*nsi->nsi_seqlike.nsi_getsize_fast)(self); - } else { - size = (*nsi->nsi_seqlike.nsi_getsize)(self); - if unlikely(size == (size_t)-1) - goto err; - } - return index < size; - } else if (nsi->nsi_class == TYPE_SEQX_CLASS_MAP) { - if (nsi->nsi_maplike.nsi_getdefault) { - index_ob = DeeInt_NewSize(index); - if unlikely(!index_ob) - goto err; - result = (*nsi->nsi_maplike.nsi_getdefault)(self, index_ob, ITER_DONE); - Dee_Decref(index_ob); - if (result == ITER_DONE) - return 0; - goto check_result; - } - /* We can't actually substitute with `operator contains', because - * if we did that, we may get a false positive for a valid key, but - * an unbound key none-the-less. */ - } - } - - /* Fallback: create an integer object and use it for indexing. */ - index_ob = DeeInt_NewSize(index); - if unlikely(!index_ob) - goto err; - result = DeeType_INVOKE_GETITEM(tp_self, self, index_ob); - Dee_Decref(index_ob); -check_result: - if (!result) { - if (DeeError_Catch(&DeeError_UnboundItem) || - DeeError_Catch(&DeeError_IndexError) || - DeeError_Catch(&DeeError_KeyError)) - return 0; - goto err; - } - Dee_Decref(result); - return 1; - } - err_unimplemented_operator(tp_self, OPERATOR_GETITEM); -err: - return -1; -} - -DEFINE_OPERATOR(int, HasItemStringHash, (DeeObject *self, char const *key, dhash_t hash)) { - int result; - DeeObject *orig_self = self; - DREF DeeObject *keyob; - LOAD_TP_SELF; - - /* Optimization for specific types. */ -again: - if (tp_self == &DeeSuper_Type) { - tp_self = DeeSuper_TYPE(self); - self = DeeSuper_SELF(self); - goto again; - } - - /* Fallback: create a temporary string object and use it for indexing. */ - keyob = DeeString_NewWithHash(key, hash); - if unlikely(!keyob) - goto err; - result = DeeObject_HasItem(orig_self, keyob); - Dee_Decref(keyob); - return result; -err: - return -1; -} - -DEFINE_OPERATOR(int, HasItemStringLenHash, (DeeObject *self, char const *key, size_t keylen, dhash_t hash)) { - int result; - DeeObject *orig_self = self; - DREF DeeObject *keyob; - LOAD_TP_SELF; - - /* Optimization for specific types. */ -again: - if (tp_self == &DeeSuper_Type) { - tp_self = DeeSuper_TYPE(self); - self = DeeSuper_SELF(self); - goto again; - } - - /* Fallback: create a temporary string object and use it for indexing. */ - keyob = DeeString_NewSizedWithHash(key, keylen, hash); - if unlikely(!keyob) - goto err; - result = DeeObject_HasItem(orig_self, keyob); - Dee_Decref(keyob); - return result; -err: - return -1; -} - -DEFINE_OPERATOR(DREF DeeObject *, TryGetItem, (DeeObject *self, DeeObject *key)) { - DREF DeeObject *result; - LOAD_TP_SELF; - if likely(likely(tp_self->tp_seq && tp_self->tp_seq->tp_getitem) || - unlikely(DeeType_InheritGetItem(tp_self))) { - struct type_nsi const *nsi; - nsi = tp_self->tp_seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_MAP) { - if (nsi->nsi_maplike.nsi_getdefault) - return (*nsi->nsi_maplike.nsi_getdefault)(self, key, ITER_DONE); - } - result = DeeType_INVOKE_GETITEM(tp_self, self, key); - if unlikely(!result) { - if (DeeError_Catch(&DeeError_KeyError) || - DeeError_Catch(&DeeError_NotImplemented)) - return ITER_DONE; - } - return result; - } - return ITER_DONE; -} - -DEFINE_OPERATOR(DREF DeeObject *, TryGetItemIndex, - (DeeObject *RESTRICT_IF_NOTYPE self, size_t index)) { - DREF DeeObject *result; - DREF DeeObject *index_ob; - index_ob = DeeInt_NewSize(index); - if unlikely(!index_ob) - goto err; -#ifdef DEFINE_TYPED_OPERATORS - result = DeeObject_TTryGetItem(tp_self, self, index_ob); -#else /* DEFINE_TYPED_OPERATORS */ - result = DeeObject_TryGetItem(self, index_ob); -#endif /* !DEFINE_TYPED_OPERATORS */ - Dee_Decref(index_ob); - return result; -err: - return NULL; -} - -DEFINE_OPERATOR(DREF DeeObject *, TryGetItemStringHash, - (DeeObject *RESTRICT_IF_NOTYPE self, char const *key, Dee_hash_t hash)) { - DREF DeeObject *result, *keyob; - keyob = DeeString_NewWithHash(key, hash); - if unlikely(!keyob) - goto err; -#ifdef DEFINE_TYPED_OPERATORS - result = DeeObject_TTryGetItem(tp_self, self, keyob); -#else /* DEFINE_TYPED_OPERATORS */ - result = DeeObject_TryGetItem(self, keyob); -#endif /* !DEFINE_TYPED_OPERATORS */ - Dee_Decref(keyob); - return result; -err: - return NULL; -} - -DEFINE_OPERATOR(DREF DeeObject *, TryGetItemStringLenHash, - (DeeObject *RESTRICT_IF_NOTYPE self, char const *key, size_t keylen, Dee_hash_t hash)) { - DREF DeeObject *result, *keyob; - keyob = DeeString_NewSizedWithHash(key, keylen, hash); - if unlikely(!keyob) - goto err; -#ifdef DEFINE_TYPED_OPERATORS - result = DeeObject_TTryGetItem(tp_self, self, keyob); -#else /* DEFINE_TYPED_OPERATORS */ - result = DeeObject_TryGetItem(self, keyob); -#endif /* !DEFINE_TYPED_OPERATORS */ - Dee_Decref(keyob); - return result; -err: - return NULL; -} - -DEFINE_OPERATOR(DREF DeeObject *, GetItemIndex, - (DeeObject *RESTRICT_IF_NOTYPE self, size_t index)) { - DREF DeeObject *index_ob, *result; - LOAD_TP_SELF; - if likely(likely(tp_self->tp_seq && tp_self->tp_seq->tp_getitem) || - unlikely(DeeType_InheritGetItem(tp_self))) { - struct type_nsi const *nsi; - nsi = tp_self->tp_seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ) { - if (nsi->nsi_seqlike.nsi_getitem) - return (*nsi->nsi_seqlike.nsi_getitem)(self, index); - } - - /* Fallback: create an integer object and use it for indexing. */ - index_ob = DeeInt_NewSize(index); - if unlikely(!index_ob) - goto err; - result = DeeType_INVOKE_GETITEM(tp_self, self, index_ob); - Dee_Decref(index_ob); - return result; - } - err_unimplemented_operator(tp_self, OPERATOR_GETITEM); -err: - return NULL; -} - -DEFINE_OPERATOR(int, DelItemIndex, - (DeeObject *RESTRICT_IF_NOTYPE self, size_t index)) { - DREF DeeObject *index_ob; - int result; - LOAD_TP_SELF; - if likely(likely(tp_self->tp_seq && tp_self->tp_seq->tp_delitem) || - unlikely(DeeType_InheritDelItem(tp_self))) { - struct type_nsi const *nsi; - nsi = tp_self->tp_seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ) { - if (nsi->nsi_seqlike.nsi_delitem) - return (*nsi->nsi_seqlike.nsi_delitem)(self, index); - } - - /* Fallback: create an integer object and use it for indexing. */ - index_ob = DeeInt_NewSize(index); - if unlikely(!index_ob) - goto err; - result = DeeType_INVOKE_DELITEM(tp_self, self, index_ob); - Dee_Decref(index_ob); - return result; - } - err_unimplemented_operator(tp_self, OPERATOR_DELITEM); -err: - return -1; -} - -DEFINE_OPERATOR(int, SetItemIndex, (DeeObject *self, size_t index, DeeObject *value)) { - DREF DeeObject *index_ob; - int result; - LOAD_TP_SELF; - if likely(likely(tp_self->tp_seq && tp_self->tp_seq->tp_setitem) || - unlikely(DeeType_InheritSetItem(tp_self))) { - struct type_nsi const *nsi; - nsi = tp_self->tp_seq->tp_nsi; - if (nsi && nsi->nsi_class == TYPE_SEQX_CLASS_SEQ) { - if (nsi->nsi_seqlike.nsi_setitem) - return (*nsi->nsi_seqlike.nsi_setitem)(self, index, value); - } - - /* Fallback: create an integer object and use it for indexing. */ - index_ob = DeeInt_NewSize(index); - if unlikely(!index_ob) - goto err; - result = DeeType_INVOKE_SETITEM(tp_self, self, index_ob, value); - Dee_Decref(index_ob); - return result; - } - err_unimplemented_operator(tp_self, OPERATOR_DELITEM); -err: - return -1; -} - -DEFINE_OPERATOR(DREF DeeObject *, GetItemStringHash, (DeeObject *self, char const *key, dhash_t hash)) { - DREF DeeObject *keyob, *result; - LOAD_TP_SELF; - - /* Optimization for specific types. */ -again: - if (tp_self == &DeeDict_Type) { - return DeeDict_GetItemStringHash(self, key, hash); - } else if (tp_self == &DeeSuper_Type) { - tp_self = DeeSuper_TYPE(self); - self = DeeSuper_SELF(self); - goto again; - } - - /* Fallback: create a temporary string object and use it for indexing. */ - keyob = DeeString_NewWithHash(key, hash); - if unlikely(!keyob) - goto err; - result = DeeObject_TGetItem(tp_self, self, keyob); - Dee_Decref(keyob); - return result; -err: - return NULL; -} - -DEFINE_OPERATOR(DREF DeeObject *, GetItemStringLenHash, - (DeeObject *self, char const *key, size_t keylen, dhash_t hash)) { - DREF DeeObject *keyob, *result; - LOAD_TP_SELF; - - /* Optimization for specific types. */ -again: - if (tp_self == &DeeDict_Type) { - return DeeDict_GetItemStringLenHash(self, key, keylen, hash); - } else if (tp_self == &DeeSuper_Type) { - tp_self = DeeSuper_TYPE(self); - self = DeeSuper_SELF(self); - goto again; - } - - /* Fallback: create a temporary string object and use it for indexing. */ - keyob = DeeString_NewSizedWithHash(key, keylen, hash); - if unlikely(!keyob) - goto err; - result = DeeObject_TGetItem(tp_self, self, keyob); - Dee_Decref(keyob); - return result; -err: - return NULL; -} - -DEFINE_OPERATOR(int, DelItemStringHash, (DeeObject *self, char const *key, dhash_t hash)) { - DREF DeeObject *keyob; - int result; - LOAD_TP_SELF; - if (tp_self == &DeeDict_Type) - return DeeDict_DelItemStringHash(self, key, hash); - - /* Fallback: create a temporary string object and use it for indexing. */ - keyob = DeeString_NewWithHash(key, hash); - if unlikely(!keyob) - goto err; -#ifdef DEFINE_TYPED_OPERATORS - result = DeeObject_TDelItem(tp_self, self, keyob); -#else /* DEFINE_TYPED_OPERATORS */ - result = DeeObject_DelItem(self, keyob); -#endif /* !DEFINE_TYPED_OPERATORS */ - Dee_Decref(keyob); - return result; -err: - return -1; -} - -DEFINE_OPERATOR(int, DelItemStringLenHash, - (DeeObject *self, char const *key, size_t keylen, dhash_t hash)) { - DREF DeeObject *keyob; - int result; - LOAD_TP_SELF; - if (tp_self == &DeeDict_Type) - return DeeDict_DelItemStringLenHash(self, key, keylen, hash); - - /* Fallback: create a temporary string object and use it for indexing. */ - keyob = DeeString_NewSizedWithHash(key, keylen, hash); - if unlikely(!keyob) - goto err; -#ifdef DEFINE_TYPED_OPERATORS - result = DeeObject_TDelItem(tp_self, self, keyob); -#else /* DEFINE_TYPED_OPERATORS */ - result = DeeObject_DelItem(self, keyob); -#endif /* !DEFINE_TYPED_OPERATORS */ - Dee_Decref(keyob); - return result; -err: - return -1; -} - -DEFINE_OPERATOR(int, SetItemStringHash, - (DeeObject *self, char const *key, dhash_t hash, DeeObject *value)) { - DREF DeeObject *keyob; - int result; - LOAD_TP_SELF; - if (tp_self == &DeeDict_Type) - return DeeDict_SetItemStringHash(self, key, hash, value); - - /* Fallback: create a temporary string object and use it for indexing. */ - keyob = DeeString_NewWithHash(key, hash); - if unlikely(!keyob) - goto err; -#ifdef DEFINE_TYPED_OPERATORS - result = DeeObject_TSetItem(tp_self, self, keyob, value); -#else /* DEFINE_TYPED_OPERATORS */ - result = DeeObject_SetItem(self, keyob, value); -#endif /* !DEFINE_TYPED_OPERATORS */ - Dee_Decref(keyob); - return result; -err: - return -1; -} - -DEFINE_OPERATOR(int, SetItemStringLenHash, - (DeeObject *self, char const *key, size_t keylen, dhash_t hash, DeeObject *value)) { - DREF DeeObject *keyob; - int result; - LOAD_TP_SELF; - if (tp_self == &DeeDict_Type) - return DeeDict_SetItemStringLenHash(self, key, keylen, hash, value); - - /* Fallback: create a temporary string object and use it for indexing. */ - keyob = DeeString_NewSizedWithHash(key, keylen, hash); - if unlikely(!keyob) - goto err; -#ifdef DEFINE_TYPED_OPERATORS - result = DeeObject_TSetItem(tp_self, self, keyob, value); -#else /* DEFINE_TYPED_OPERATORS */ - result = DeeObject_SetItem(self, keyob, value); -#endif /* !DEFINE_TYPED_OPERATORS */ - Dee_Decref(keyob); - return result; -err: - return -1; -} -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - /* With-operator invocation: * >> with (my_object) { * >> ... diff --git a/src/dex/_hostasm/generator-vstack-ex.c b/src/dex/_hostasm/generator-vstack-ex.c index e4af425dd..5239bfae1 100644 --- a/src/dex/_hostasm/generator-vstack-ex.c +++ b/src/dex/_hostasm/generator-vstack-ex.c @@ -6239,40 +6239,6 @@ vassert_unpack_size(struct fungen *__restrict self, return -1; } -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS -/* self, index -> CHECKED(elem) */ -PRIVATE WUNUSED NONNULL((1, 2)) int DCALL -vcall_DeeFastSeq_GetItem(struct fungen *__restrict self, - struct Dee_type_nsi const *__restrict nsi) { - struct fg_branch branch; - ASSERT(nsi->nsi_class == TYPE_SEQX_CLASS_SEQ); - if (nsi->nsi_seqlike.nsi_getitem_fast) { - DO(fg_vdup_at(self, 2)); /* self, index, self */ - DO(fg_vdup_at(self, 2)); /* self, index, self, index */ - DO(fg_vcallapi(self, nsi->nsi_seqlike.nsi_getitem_fast, VCALL_CC_RAWINTPTR, 2)); /* self, index, UNCHECKED(elem) */ - DO(fg_vdup(self)); /* self, index, UNCHECKED(elem), UNCHECKED(elem) */ - DO(fg_vjz_enter_unlikely(self, &branch)); /* self, index, UNCHECKED(elem) */ - EDO(err_branch, fg_vpop(self)); /* self, index */ - EDO(err_branch, fg_vcallapi(self, &libhostasm_rt_err_unbound_index, VCALL_CC_EXCEPT, 2)); /* N/A */ - DO(fg_vjx_leave_noreturn(self, &branch)); /* self, index, elem */ - DO(fg_vrrot(self, 3)); /* elem, self, index */ - DO(fg_vpop(self)); /* elem, self */ - DO(fg_vpop(self)); /* elem */ - DO(fg_vdirect1(self)); /* elem */ - fg_vtop_direct_setref(self); - return 0; - } - ASSERT(nsi->nsi_seqlike.nsi_getitem); - return fg_vcallapi(self, nsi->nsi_seqlike.nsi_getitem, - VCALL_CC_OBJECT, 2); -err_branch: - fg_branch_fini(&branch); -err: - return -1; -} -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - - /* seq -> [elems...] */ INTERN WUNUSED NONNULL((1)) int DCALL fg_vopunpack(struct fungen *__restrict self, vstackaddr_t n) { @@ -6368,52 +6334,6 @@ fg_vopunpack(struct fungen *__restrict self, vstackaddr_t n) { /* TODO: Incref every element where the consumer needs it to be a reference */ /* TODO: Release the lock from the list (if the list isn't ONEREF) */ } - -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS - /* If "seqtype" implements "nsi_getsize_fast" (DeeFastSeq_GetSize_deprecated), - * inline the relevant code from `DeeObject_Unpack()' */ - if (seqtype->tp_seq && - seqtype->tp_seq->tp_nsi && - seqtype->tp_seq->tp_nsi->nsi_class == TYPE_SEQX_CLASS_SEQ && - seqtype->tp_seq->tp_nsi->nsi_seqlike.nsi_getsize_fast) { - struct Dee_type_nsi const *nsi = seqtype->tp_seq->tp_nsi; - ASSERT(nsi->nsi_seqlike.nsi_getitem || - nsi->nsi_seqlike.nsi_getitem_fast); - - /* NOTE: Here, we're *NOT* free to load in arbitrary order! - * If more than one element is unbound / causes an exception, - * then the actual error needs to be for the *first* (lowest) - * index where access causes a fault! */ - DO(fg_vdup(self)); /* seq, seq */ - DO(fg_vcallapi(self, nsi->nsi_seqlike.nsi_getsize_fast, - VCALL_CC_RAWINTPTR, 1)); /* seq, size */ - DO(vassert_unpack_size(self, n)); /* seq */ - for (i = 0; i < n; ++i) { - /* TODO: For locations that don't end up being used, simply push none: - * >> local a, none, b = foo.partition(",")...; - * - * ASM: - * >> push @foo - * >> push @"," - * >> callattr top, @"partition", #1 - * >> unpack pop, #3 // Don't actually need to load second value - * >> pop local @b - * >> pop // Second value is discarded without being used - * >> pop local @a - */ - if (i == n - 1) { - DO(fg_vlrot(self, n)); /* [items...], seq */ - } else { - DO(fg_vdup_at(self, i + 1)); /* seq, [items...], seq */ - } - DO(fg_vpush_immSIZ(self, i)); /* [seq], [items...], seq, i */ - DO(vcall_DeeFastSeq_GetItem(self, nsi)); /* [seq], [items...], elem */ - } /* [seq], [items...] */ - if (n == 0) - return fg_vpop(self); - return 0; - } -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ } } diff --git a/src/dex/rt/librt.c b/src/dex/rt/librt.c index 001243063..78e371a9f 100644 --- a/src/dex/rt/librt.c +++ b/src/dex/rt/librt.c @@ -93,12 +93,6 @@ print define_Dee_HashStr("Attribute"); print define_Dee_HashStr("AttributeTable"); print define_Dee_HashStr("ObjectTable"); print define_Dee_HashStr("OperatorTable"); -print "#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS"; -print define_Dee_HashStr("Proxy"); -print define_Dee_HashStr("Keys"); -print define_Dee_HashStr("Items"); -print define_Dee_HashStr("Values"); -print "#endif /" "* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS *" "/"; print define_Dee_HashStr("__args__"); print define_Dee_HashStr("__bases__"); print define_Dee_HashStr("__exports__"); @@ -191,12 +185,6 @@ print define_Dee_HashStr("__SeqReversedWithTryGetItemIndex__"); #define Dee_HashStr__AttributeTable _Dee_HashSelectC(0xb3fdb6eb, 0x2abf3f78af71dee0) #define Dee_HashStr__ObjectTable _Dee_HashSelectC(0xc5d943d2, 0x8cda148232a1cdb2) #define Dee_HashStr__OperatorTable _Dee_HashSelectC(0xee6c4bef, 0x7987fd5ae34b3d62) -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS -#define Dee_HashStr__Proxy _Dee_HashSelectC(0x1e08cc78, 0x57cf9ad5a4dae6) -#define Dee_HashStr__Keys _Dee_HashSelectC(0x276b259c, 0xaec3c5ef66bfaf3f) -#define Dee_HashStr__Items _Dee_HashSelectC(0x58117411, 0x947064e89df3b8bd) -#define Dee_HashStr__Values _Dee_HashSelectC(0x47fcf975, 0x323bdee84d34e652) -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ #define Dee_HashStr____args__ _Dee_HashSelectC(0x938e1f4c, 0x78969e2a67f8471d) #define Dee_HashStr____bases__ _Dee_HashSelectC(0xff4ac0d2, 0x56bdc053fa64e4c9) #define Dee_HashStr____exports__ _Dee_HashSelectC(0x1d7df2db, 0x304ed10433cd0d26) @@ -720,29 +708,6 @@ librt_get_DocKwdsIterator_f(size_t UNUSED(argc), DeeObject *const *UNUSED(argv)) return get_iterator_of(librt_get_DocKwds_impl_f()); } - -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS -PRIVATE WUNUSED DREF DeeObject *DCALL -librt_get_MappingProxy_impl_f(void) { - return DeeObject_GetAttrStringHash((DeeObject *)&DeeMapping_Type, STR_AND_HASH(Proxy)); -} - -PRIVATE WUNUSED DREF DeeObject *DCALL -librt_get_MappingKeys_impl_f(void) { - return DeeObject_GetAttrStringHash((DeeObject *)&DeeMapping_Type, STR_AND_HASH(Keys)); -} - -PRIVATE WUNUSED DREF DeeObject *DCALL -librt_get_MappingValues_impl_f(void) { - return DeeObject_GetAttrStringHash((DeeObject *)&DeeMapping_Type, STR_AND_HASH(Values)); -} - -PRIVATE WUNUSED DREF DeeObject *DCALL -librt_get_MappingItems_impl_f(void) { - return DeeObject_GetAttrStringHash((DeeObject *)&DeeMapping_Type, STR_AND_HASH(Items)); -} -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - PRIVATE WUNUSED DREF DeeObject *DCALL librt_get_MappingHashFilter_impl_f(void) { DeeObject *argv[] = { DeeInt_Zero }; @@ -754,48 +719,6 @@ librt_get_MappingByAttr_impl_f(void) { return get_type_of(DeeObject_GetAttrStringHash(Dee_EmptyMapping, STR_AND_HASH(byattr))); } -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS -PRIVATE WUNUSED DREF DeeObject *DCALL -librt_get_MappingProxy_f(size_t UNUSED(argc), DeeObject *const *UNUSED(argv)) { - return librt_get_MappingProxy_impl_f(); -} - -PRIVATE WUNUSED DREF DeeObject *DCALL -librt_get_MappingKeys_f(size_t UNUSED(argc), DeeObject *const *UNUSED(argv)) { - return librt_get_MappingKeys_impl_f(); -} - -PRIVATE WUNUSED DREF DeeObject *DCALL -librt_get_MappingValues_f(size_t UNUSED(argc), DeeObject *const *UNUSED(argv)) { - return librt_get_MappingValues_impl_f(); -} - -PRIVATE WUNUSED DREF DeeObject *DCALL -librt_get_MappingItems_f(size_t UNUSED(argc), DeeObject *const *UNUSED(argv)) { - return librt_get_MappingItems_impl_f(); -} - -PRIVATE WUNUSED DREF DeeObject *DCALL -librt_get_MappingProxyIterator_f(size_t UNUSED(argc), DeeObject *const *UNUSED(argv)) { - return get_iterator_of(librt_get_MappingProxy_impl_f()); -} - -PRIVATE WUNUSED DREF DeeObject *DCALL -librt_get_MappingKeysIterator_f(size_t UNUSED(argc), DeeObject *const *UNUSED(argv)) { - return get_iterator_of(librt_get_MappingKeys_impl_f()); -} - -PRIVATE WUNUSED DREF DeeObject *DCALL -librt_get_MappingValuesIterator_f(size_t UNUSED(argc), DeeObject *const *UNUSED(argv)) { - return get_iterator_of(librt_get_MappingValues_impl_f()); -} - -PRIVATE WUNUSED DREF DeeObject *DCALL -librt_get_MappingItemsIterator_f(size_t UNUSED(argc), DeeObject *const *UNUSED(argv)) { - return get_iterator_of(librt_get_MappingItems_impl_f()); -} -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - PRIVATE WUNUSED DREF DeeObject *DCALL librt_get_MappingHashFilter_f(size_t UNUSED(argc), DeeObject *const *UNUSED(argv)) { return librt_get_MappingHashFilter_impl_f(); @@ -982,28 +905,6 @@ librt_get_SeqLocatorIterator_f(size_t UNUSED(argc), DeeObject *const *UNUSED(arg return get_iterator_of(librt_get_SeqLocator_impl_f()); } -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS -LOCAL WUNUSED DREF DeeObject *DCALL -librt_get_SeqSubRange_impl_f(void) { - return get_type_of(DeeObject_GetRange(Dee_EmptySeq, DeeInt_Zero, DeeInt_One)); -} - -PRIVATE WUNUSED DREF DeeObject *DCALL -librt_get_SeqSubRange_f(size_t UNUSED(argc), DeeObject *const *UNUSED(argv)) { - return librt_get_SeqSubRange_impl_f(); -} - -PRIVATE WUNUSED DREF DeeObject *DCALL -librt_get_SeqSubRangeIterator_f(size_t UNUSED(argc), DeeObject *const *UNUSED(argv)) { - return get_iterator_of(librt_get_SeqSubRange_impl_f()); -} - -PRIVATE WUNUSED DREF DeeObject *DCALL -librt_get_SeqSubRangeN_f(size_t UNUSED(argc), DeeObject *const *UNUSED(argv)) { - return get_type_of(DeeObject_GetRange(Dee_EmptySeq, DeeInt_One, Dee_None)); -} -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ - LOCAL WUNUSED DREF DeeObject *DCALL librt_get_SeqMapped_impl_f(void) { DeeObject *argv[] = { Dee_None }; @@ -2351,11 +2252,6 @@ PRIVATE DEFINE_CMETHOD(librt_get_SeqHashFilter, &librt_get_SeqHashFilter_f, METH PRIVATE DEFINE_CMETHOD(librt_get_SeqHashFilterIterator, &librt_get_SeqHashFilterIterator_f, METHOD_FCONSTCALL); PRIVATE DEFINE_CMETHOD(librt_get_SeqLocator, &librt_get_SeqLocator_f, METHOD_FCONSTCALL); PRIVATE DEFINE_CMETHOD(librt_get_SeqLocatorIterator, &librt_get_SeqLocatorIterator_f, METHOD_FCONSTCALL); -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS -PRIVATE DEFINE_CMETHOD(librt_get_SeqSubRange, &librt_get_SeqSubRange_f, METHOD_FCONSTCALL); -PRIVATE DEFINE_CMETHOD(librt_get_SeqSubRangeIterator, &librt_get_SeqSubRangeIterator_f, METHOD_FCONSTCALL); -PRIVATE DEFINE_CMETHOD(librt_get_SeqSubRangeN, &librt_get_SeqSubRangeN_f, METHOD_FCONSTCALL); -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ PRIVATE DEFINE_CMETHOD(librt_get_SeqMapped, &librt_get_SeqMapped_f, METHOD_FCONSTCALL); PRIVATE DEFINE_CMETHOD(librt_get_SeqMappedIterator, &librt_get_SeqMappedIterator_f, METHOD_FCONSTCALL); PRIVATE DEFINE_CMETHOD(librt_get_SeqRange, &librt_get_SeqRange_f, METHOD_FCONSTCALL); @@ -2479,16 +2375,6 @@ PRIVATE DEFINE_CMETHOD(librt_get_Traceback_empty, &librt_get_Traceback_empty_f, PRIVATE DEFINE_CMETHOD(librt_get_Module_empty, &librt_get_Module_empty_f, METHOD_FCONSTCALL); PRIVATE DEFINE_CMETHOD(librt_get_DocKwds, &librt_get_DocKwds_f, METHOD_FCONSTCALL); PRIVATE DEFINE_CMETHOD(librt_get_DocKwdsIterator, &librt_get_DocKwdsIterator_f, METHOD_FCONSTCALL); -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS -PRIVATE DEFINE_CMETHOD(librt_get_MappingProxy, &librt_get_MappingProxy_f, METHOD_FCONSTCALL); -PRIVATE DEFINE_CMETHOD(librt_get_MappingKeys, &librt_get_MappingKeys_f, METHOD_FCONSTCALL); -PRIVATE DEFINE_CMETHOD(librt_get_MappingValues, &librt_get_MappingValues_f, METHOD_FCONSTCALL); -PRIVATE DEFINE_CMETHOD(librt_get_MappingItems, &librt_get_MappingItems_f, METHOD_FCONSTCALL); -PRIVATE DEFINE_CMETHOD(librt_get_MappingProxyIterator, &librt_get_MappingProxyIterator_f, METHOD_FCONSTCALL); -PRIVATE DEFINE_CMETHOD(librt_get_MappingKeysIterator, &librt_get_MappingKeysIterator_f, METHOD_FCONSTCALL); -PRIVATE DEFINE_CMETHOD(librt_get_MappingValuesIterator, &librt_get_MappingValuesIterator_f, METHOD_FCONSTCALL); -PRIVATE DEFINE_CMETHOD(librt_get_MappingItemsIterator, &librt_get_MappingItemsIterator_f, METHOD_FCONSTCALL); -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ PRIVATE DEFINE_CMETHOD(librt_get_MappingHashFilter, &librt_get_MappingHashFilter_f, METHOD_FCONSTCALL); PRIVATE DEFINE_CMETHOD(librt_get_MappingHashFilterIterator, &librt_get_MappingHashFilterIterator_f, METHOD_FCONSTCALL); PRIVATE DEFINE_CMETHOD(librt_get_MappingByAttr, &librt_get_MappingByAttr_f, METHOD_FCONSTCALL); @@ -2698,11 +2584,6 @@ PRIVATE struct dex_symbol symbols[] = { { "SeqHashFilterIterator", (DeeObject *)&librt_get_SeqHashFilterIterator, MODSYM_FREADONLY | MODSYM_FPROPERTY | MODSYM_FCONSTEXPR }, /* SeqHashFilterIterator_Type */ { "SeqLocator", (DeeObject *)&librt_get_SeqLocator, MODSYM_FREADONLY | MODSYM_FPROPERTY | MODSYM_FCONSTEXPR }, /* SeqLocator_Type */ { "SeqLocatorIterator", (DeeObject *)&librt_get_SeqLocatorIterator, MODSYM_FREADONLY | MODSYM_FPROPERTY | MODSYM_FCONSTEXPR }, /* SeqLocatorIterator_Type */ -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS - { "SeqSubRange", (DeeObject *)&librt_get_SeqSubRange, MODSYM_FREADONLY | MODSYM_FPROPERTY | MODSYM_FCONSTEXPR }, /* SeqSubRange_Type */ - { "SeqSubRangeIterator", (DeeObject *)&librt_get_SeqSubRangeIterator, MODSYM_FREADONLY | MODSYM_FPROPERTY | MODSYM_FCONSTEXPR }, /* SeqSubRangeIterator_Type */ - { "SeqSubRangeN", (DeeObject *)&librt_get_SeqSubRangeN, MODSYM_FREADONLY | MODSYM_FPROPERTY | MODSYM_FCONSTEXPR }, /* SeqSubRangeN_Type */ -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ { "SeqMapped", (DeeObject *)&librt_get_SeqMapped, MODSYM_FREADONLY | MODSYM_FPROPERTY | MODSYM_FCONSTEXPR }, /* SeqMapped_Type */ { "SeqMappedIterator", (DeeObject *)&librt_get_SeqMappedIterator, MODSYM_FREADONLY | MODSYM_FPROPERTY | MODSYM_FCONSTEXPR }, /* SeqMappedIterator_Type */ { "SeqRange", (DeeObject *)&librt_get_SeqRange, MODSYM_FREADONLY | MODSYM_FPROPERTY | MODSYM_FCONSTEXPR }, /* SeqRange_Type */ @@ -2805,27 +2686,6 @@ PRIVATE struct dex_symbol symbols[] = { { "MapDifferenceIterator", (DeeObject *)&librt_get_MapDifferenceIterator, MODSYM_FREADONLY | MODSYM_FPROPERTY | MODSYM_FCONSTEXPR }, /* MapDifferenceIterator_Type */ /* Internal types used to drive mapping proxies */ -#ifndef CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS - { "MappingProxy", (DeeObject *)&librt_get_MappingProxy, MODSYM_FREADONLY | MODSYM_FPROPERTY | MODSYM_FCONSTEXPR, - DOC("Base class for ?GMappingKeys, ?GMappingValues and ?GMappingItems") }, /* DeeMappingProxy_Type */ - { "MappingKeys", (DeeObject *)&librt_get_MappingKeys, MODSYM_FREADONLY | MODSYM_FPROPERTY | MODSYM_FCONSTEXPR, - DOC("General purpose, sequence proxy type for viewing the keys of an abstract mapping object\n" - "When not overwritten by the mapping type itself, this is the type of sequence that's returned " - /**/ "by :Mapping.keys") }, /* DeeMappingKeys_Type */ - { "MappingValues", (DeeObject *)&librt_get_MappingValues, MODSYM_FREADONLY | MODSYM_FPROPERTY | MODSYM_FCONSTEXPR, - DOC("General purpose, sequence proxy type for viewing the values of an abstract mapping object\n" - "When not overwritten by the mapping type itself, this is the type of sequence that's returned " - /**/ "by :Mapping.values") }, /* DeeMappingValues_Type */ - { "MappingItems", (DeeObject *)&librt_get_MappingItems, MODSYM_FREADONLY | MODSYM_FPROPERTY | MODSYM_FCONSTEXPR, - DOC("General purpose, sequence proxy type for viewing the items (key-value pairs) of an abstract mapping object\n" - "When not overwritten by the mapping type itself, this is the type of sequence that's returned " - /**/ "by :Mapping.items") }, /* DeeMappingItems_Type */ - { "MappingProxyIterator", (DeeObject *)&librt_get_MappingProxyIterator, MODSYM_FREADONLY | MODSYM_FPROPERTY | MODSYM_FCONSTEXPR, - DOC("Iterator class for ?GMappingProxy, and Base class for ?GMappingKeysIterator, ?GMappingValuesIterator and ?GMappingItemsIterator") }, /* DeeMappingProxyIterator_Type */ - { "MappingKeysIterator", (DeeObject *)&librt_get_MappingKeysIterator, MODSYM_FREADONLY | MODSYM_FPROPERTY | MODSYM_FCONSTEXPR }, /* DeeMappingKeysIterator_Type */ - { "MappingValuesIterator", (DeeObject *)&librt_get_MappingValuesIterator, MODSYM_FREADONLY | MODSYM_FPROPERTY | MODSYM_FCONSTEXPR }, /* DeeMappingValuesIterator_Type */ - { "MappingItemsIterator", (DeeObject *)&librt_get_MappingItemsIterator, MODSYM_FREADONLY | MODSYM_FPROPERTY | MODSYM_FCONSTEXPR }, /* DeeMappingProxyIterator_Type */ -#endif /* !CONFIG_EXPERIMENTAL_NEW_SEQUENCE_OPERATORS */ { "MappingHashFilter", (DeeObject *)&librt_get_MappingHashFilter, MODSYM_FREADONLY | MODSYM_FPROPERTY | MODSYM_FCONSTEXPR }, /* MapHashFilter_Type */ { "MappingHashFilterIterator", (DeeObject *)&librt_get_MappingHashFilterIterator, MODSYM_FREADONLY | MODSYM_FPROPERTY | MODSYM_FCONSTEXPR }, /* MapHashFilterIterator_Type */ { "MappingByAttr", (DeeObject *)&librt_get_MappingByAttr, MODSYM_FREADONLY | MODSYM_FPROPERTY | MODSYM_FCONSTEXPR }, /* MapByAttr_Type */