A map that creates new keys for missing key access. Produces an incrementing sequence given a series of unique keys. This is similar to the compiler prefix_anon_map class although simpler. Inlines the approach taken by :class:`sqlalchemy.util.PopulateDict` which is otherwise usual
| 75 | |
| 76 | @cython.cclass |
| 77 | class anon_map(Dict[_AM_KEY, _AM_VALUE]): |
| 78 | """A map that creates new keys for missing key access. |
| 79 | |
| 80 | Produces an incrementing sequence given a series of unique keys. |
| 81 | |
| 82 | This is similar to the compiler prefix_anon_map class although simpler. |
| 83 | |
| 84 | Inlines the approach taken by :class:`sqlalchemy.util.PopulateDict` which |
| 85 | is otherwise usually used for this type of operation. |
| 86 | |
| 87 | """ |
| 88 | |
| 89 | if cython.compiled: |
| 90 | _index: cython.uint |
| 91 | |
| 92 | def __cinit__(self): # type: ignore[no-untyped-def] |
| 93 | self._index = 0 |
| 94 | |
| 95 | else: |
| 96 | _index: int = 0 # type: ignore[no-redef] |
| 97 | |
| 98 | @cython.cfunc |
| 99 | @cython.inline |
| 100 | def _add_missing(self: anon_map, key: _AM_KEY, /) -> int: |
| 101 | val: int = self._index |
| 102 | self._index += 1 |
| 103 | self_dict: dict = self # type: ignore[type-arg] |
| 104 | self_dict[key] = val |
| 105 | return val |
| 106 | |
| 107 | def get_anon(self: anon_map, obj: object, /) -> Tuple[int, bool]: |
| 108 | self_dict: dict = self # type: ignore[type-arg] |
| 109 | |
| 110 | idself: int = _get_id(obj) |
| 111 | if idself in self_dict: |
| 112 | return self_dict[idself], True |
| 113 | else: |
| 114 | return self._add_missing(idself), False |
| 115 | |
| 116 | if cython.compiled: |
| 117 | |
| 118 | def __getitem__(self: anon_map, key: _AM_KEY, /) -> _AM_VALUE: |
| 119 | self_dict: dict = self # type: ignore[type-arg] |
| 120 | |
| 121 | if key in self_dict: |
| 122 | return self_dict[key] # type: ignore[no-any-return] |
| 123 | else: |
| 124 | return self._add_missing(key) # type: ignore[no-any-return] |
| 125 | |
| 126 | def __missing__(self: anon_map, key: _AM_KEY, /) -> int: |
| 127 | return self._add_missing(key) # type: ignore[no-any-return] |
no outgoing calls
no test coverage detected