-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitertools.py
More file actions
246 lines (208 loc) · 8.35 KB
/
Copy pathitertools.py
File metadata and controls
246 lines (208 loc) · 8.35 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
@namespace("itertools")
from Promethium import List
# A small, eager subset of Python's itertools module. CPython's itertools is
# built entirely around lazy generators; Promethium generators are outside
# the initial language slice (per PROMETHIUM_IMPLEMENTATION_PLAN.md), so
# every function here returns a fully-materialized `List` instead of an
# iterator — the same choice PromethiumBaseLibrary's own `range()` already
# makes for the same reason. Infinite generators (`count`, `cycle`,
# `repeat()` with no `times`) have no eager equivalent at all and are not
# attempted. Predicate-taking functions (`takewhile`, `dropwhile`,
# `filterfalse`, `starmap`) are also not attempted: there's no confirmed,
# tested way to type a callable/predicate parameter in this codebase (the
# one precedent, `DefaultDict`'s factory, is deliberately left untyped and
# only ever `.Invoke()`d, which isn't enough to build a generic predicate
# parameter on).
#
# Most functions here are fully generic over `T` — they only rearrange or
# copy elements, never compare or hash them, so they don't run into the
# "no `<`/`==` on unconstrained T" wall documented in `heapq.py`/`bisect.py`.
# Only `accumulate` needs arithmetic and is overloaded for `int`/`float`
# like `Builtins.py`'s own `sum()`.
#
# `permutations`/`combinations`/`combinations_with_replacement` compute
# purely over index arrays (`List[int]`) in a set of private, deliberately
# *non-generic* recursive helpers, then map the resulting index lists to `T`
# values in a separate, non-recursive pass. This two-phase split exists
# because of a real compiler limitation: a *generic* function calling
# itself recursively fails with "Generic parameter T for this method call
# could not fully be resolved" — confirmed by writing the natural one-phase
# generic-recursive version first and hitting that error on every recursive
# call site, then confirming the error disappears once the recursion is
# moved into non-generic helpers instead. This is a different limitation
# from the (now-fixed) generic-*class*-self-reference issue documented in
# `Counter.py` — that one was about a generic class referencing its own
# type in a method signature; this one is about a generic function calling
# itself by name, and remains unfixed as of this writing.
#
# `zip_longest` differs from CPython's signature: Python's single
# `fillvalue` (default `None`) can pad either side because Python is
# dynamically typed. With two independently-typed sequences (`T` and `U`),
# one shared fill value can't satisfy both slots' types statically, so this
# takes two fill values instead, one per side, matching `DefaultDict.get`'s
# already-proven `default: Value = None` pattern for a generic default.
def chain[T](a: List[T], b: List[T]) -> List[T]:
result: List[T] = a.copy()
result.extend(b)
return result
def chain[T](a: List[T], b: List[T], c: List[T]) -> List[T]:
result: List[T] = chain(a, b)
result.extend(c)
return result
def repeat[T](value: T, times: int) -> List[T]:
result: List[T] = List[T]()
count: int = 0
while count < times:
result.append(value)
count += 1
return result
def islice[T](values: List[T], stop: int) -> List[T]:
return islice(values, 0, stop)
def islice[T](values: List[T], start: int, stop: int) -> List[T]:
result: List[T] = List[T]()
index: int = start
limit: int = stop
if limit > len(values):
limit = len(values)
while index < limit:
result.append(values.__getitem__(index))
index += 1
return result
def compress[T](data: List[T], selectors: List[bool]) -> List[T]:
result: List[T] = List[T]()
limit: int = len(data)
if len(selectors) < limit:
limit = len(selectors)
index: int = 0
while index < limit:
if selectors.__getitem__(index):
result.append(data.__getitem__(index))
index += 1
return result
def accumulate(values: List[int]) -> List[int]:
result: List[int] = List[int]()
total: int = 0
index: int = 0
while index < len(values):
total += values.__getitem__(index)
result.append(total)
index += 1
return result
def accumulate(values: List[float]) -> List[float]:
result: List[float] = List[float]()
total: float = 0.0
index: int = 0
while index < len(values):
total += values.__getitem__(index)
result.append(total)
index += 1
return result
def product[T, U](a: List[T], b: List[U]) -> List[tuple[T, U]]:
result: List[tuple[T, U]] = List[tuple[T, U]]()
i: int = 0
while i < len(a):
left: T = a.__getitem__(i)
j: int = 0
while j < len(b):
result.append((left, b.__getitem__(j)))
j += 1
i += 1
return result
def _collectCombinations(n: int, r: int, start: int, current: List[int], result: List[List[int]]):
if len(current) == r:
result.append(current.copy())
return
index: int = start
while index < n:
current.append(index)
_collectCombinations(n, r, index + 1, current, result)
current.pop()
index += 1
def _indexCombinations(n: int, r: int) -> List[List[int]]:
result: List[List[int]] = List[List[int]]()
if r < 0 or r > n:
return result
current: List[int] = List[int]()
_collectCombinations(n, r, 0, current, result)
return result
def _collectCombinationsWithReplacement(n: int, r: int, start: int, current: List[int], result: List[List[int]]):
if len(current) == r:
result.append(current.copy())
return
index: int = start
while index < n:
current.append(index)
_collectCombinationsWithReplacement(n, r, index, current, result)
current.pop()
index += 1
def _indexCombinationsWithReplacement(n: int, r: int) -> List[List[int]]:
result: List[List[int]] = List[List[int]]()
if r < 0:
return result
if r > 0 and n == 0:
return result
current: List[int] = List[int]()
_collectCombinationsWithReplacement(n, r, 0, current, result)
return result
def _collectPermutations(n: int, r: int, used: List[bool], current: List[int], result: List[List[int]]):
if len(current) == r:
result.append(current.copy())
return
index: int = 0
while index < n:
if not used.__getitem__(index):
used[index] = True
current.append(index)
_collectPermutations(n, r, used, current, result)
current.pop()
used[index] = False
index += 1
def _indexPermutations(n: int, r: int) -> List[List[int]]:
result: List[List[int]] = List[List[int]]()
if r < 0 or r > n:
return result
used: List[bool] = List[bool]()
index: int = 0
while index < n:
used.append(False)
index += 1
current: List[int] = List[int]()
_collectPermutations(n, r, used, current, result)
return result
def _materialize[T](values: List[T], indexSets: List[List[int]]) -> List[List[T]]:
result: List[List[T]] = List[List[T]]()
i: int = 0
while i < len(indexSets):
indices: List[int] = indexSets.__getitem__(i)
combo: List[T] = List[T]()
k: int = 0
while k < len(indices):
combo.append(values.__getitem__(indices.__getitem__(k)))
k += 1
result.append(combo)
i += 1
return result
def permutations[T](values: List[T]) -> List[List[T]]:
return permutations(values, len(values))
def permutations[T](values: List[T], r: int) -> List[List[T]]:
return _materialize(values, _indexPermutations(len(values), r))
def combinations[T](values: List[T], r: int) -> List[List[T]]:
return _materialize(values, _indexCombinations(len(values), r))
def combinations_with_replacement[T](values: List[T], r: int) -> List[List[T]]:
return _materialize(values, _indexCombinationsWithReplacement(len(values), r))
def zip_longest[T, U](a: List[T], b: List[U], fillA: T = None, fillB: U = None) -> List[tuple[T, U]]:
result: List[tuple[T, U]] = List[tuple[T, U]]()
length: int = len(a)
if len(b) > length:
length = len(b)
index: int = 0
while index < length:
left: T = fillA
if index < len(a):
left = a.__getitem__(index)
right: U = fillB
if index < len(b):
right = b.__getitem__(index)
result.append((left, right))
index += 1
return result