-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
reversed.mojo
197 lines (151 loc) · 5.67 KB
/
reversed.mojo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# ===----------------------------------------------------------------------=== #
# Copyright (c) 2024, Modular Inc. All rights reserved.
#
# Licensed under the Apache License v2.0 with LLVM Exceptions:
# https://llvm.org/LICENSE.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===----------------------------------------------------------------------=== #
"""Provides the `reversed` function for reverse iteration over collections.
These are Mojo built-ins, so you don't need to import them.
"""
from .range import _StridedRange
from collections.list import _ListIter
from collections.dict import _DictKeyIter, _DictValueIter, _DictEntryIter
# ===----------------------------------------------------------------------=== #
# Reversible
# ===----------------------------------------------------------------------=== #
trait ReversibleRange:
"""
The `ReversibleRange` trait describes a range that can be reversed.
Any type that conforms to `ReversibleRange` works with the builtin
[`reversed()`](/mojo/stdlib/builtin/reversed.html) functions.
The `ReversibleRange` trait requires the type to define the `__reversed__()`
method.
**Note**: iterators are currently non-raising.
"""
# TODO: general `Reversible` trait that returns an iterator.
# iterators currently check __len__() instead of raising an exception
# so there is no ReversibleRaising trait yet.
fn __reversed__(self) -> _StridedRange:
"""Get a reversed iterator for the type.
**Note**: iterators are currently non-raising.
Returns:
The reversed iterator of the type.
"""
...
# ===----------------------------------------------------------------------=== #
# reversed
# ===----------------------------------------------------------------------=== #
fn reversed[T: ReversibleRange](value: T) -> _StridedRange:
"""Get a reversed iterator of the input range.
**Note**: iterators are currently non-raising.
Parameters:
T: The type conforming to ReversibleRange.
Args:
value: The range to get the reversed iterator of.
Returns:
The reversed iterator of the range.
"""
return value.__reversed__()
fn reversed[
T: CollectionElement
](
value: Reference[List[T], _, _],
) -> _ListIter[
T, value.is_mutable, value.lifetime, False
]:
"""Get a reversed iterator of the input list.
**Note**: iterators are currently non-raising.
Parameters:
T: The type of the elements in the list.
Args:
value: The list to get the reversed iterator of.
Returns:
The reversed iterator of the list.
"""
return value[].__reversed__()
fn reversed[
K: KeyElement,
V: CollectionElement,
](
value: Reference[Dict[K, V], _, _],
) -> _DictKeyIter[
K, V, value.is_mutable, value.lifetime, False
]:
"""Get a reversed iterator of the input dict.
**Note**: iterators are currently non-raising.
Parameters:
K: The type of the keys in the dict.
V: The type of the values in the dict.
Args:
value: The dict to get the reversed iterator of.
Returns:
The reversed iterator of the dict keys.
"""
return value[].__reversed__()
fn reversed[
mutability: Bool,
self_life: AnyLifetime[mutability].type,
K: KeyElement,
V: CollectionElement,
dict_mutability: Bool,
dict_lifetime: AnyLifetime[dict_mutability].type,
](
value: Reference[
_DictValueIter[K, V, dict_mutability, dict_lifetime],
mutability,
self_life,
]._mlir_type,
) -> _DictValueIter[K, V, dict_mutability, dict_lifetime, False]:
"""Get a reversed iterator of the input dict values.
**Note**: iterators are currently non-raising.
Parameters:
mutability: Whether the reference to the dict is mutable.
self_life: The lifetime of the dict.
K: The type of the keys in the dict.
V: The type of the values in the dict.
dict_mutability: Whether the reference to the dict values is mutable.
dict_lifetime: The lifetime of the dict values.
Args:
value: The dict values to get the reversed iterator of.
Returns:
The reversed iterator of the dict values.
"""
return Reference(value)[].__reversed__[mutability, self_life]()
fn reversed[
mutability: Bool,
self_life: AnyLifetime[mutability].type,
K: KeyElement,
V: CollectionElement,
dict_mutability: Bool,
dict_lifetime: AnyLifetime[dict_mutability].type,
](
value: Reference[
_DictEntryIter[K, V, dict_mutability, dict_lifetime],
mutability,
self_life,
]._mlir_type,
) -> _DictEntryIter[K, V, dict_mutability, dict_lifetime, False]:
"""Get a reversed iterator of the input dict items.
**Note**: iterators are currently non-raising.
Parameters:
mutability: Whether the reference to the dict is mutable.
self_life: The lifetime of the dict.
K: The type of the keys in the dict.
V: The type of the values in the dict.
dict_mutability: Whether the reference to the dict items is mutable.
dict_lifetime: The lifetime of the dict items.
Args:
value: The dict items to get the reversed iterator of.
Returns:
The reversed iterator of the dict items.
"""
var src = Reference(value)[].src
return _DictEntryIter[K, V, dict_mutability, dict_lifetime, False](
src[]._reserved, 0, src
)