Skip to content

Commit af6ec5a

Browse files
author
Release Manager
committed
gh-35776: cylint: no more xrange in matroids <!-- Please provide a concise, informative and self-explanatory title. --> <!-- Don't put issue numbers in the title. Put it in the Description below. --> <!-- For example, instead of "Fixes #12345", use "Add a new method to multiply two integers" --> ### 📚 Description getting rid of the uses of `xrange` in matroids also changing a few C-style loops into python-style loops <!-- Describe your changes here in detail. --> <!-- Why is this change required? What problem does it solve? --> <!-- If this PR resolves an open issue, please link to it here. For example "Fixes #12345". --> <!-- If your change requires a documentation PR, please link it appropriately. --> ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. It should be `[x]` not `[x ]`. --> - [x] The title is concise, informative, and self-explanatory. - [x] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation accordingly. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on - #12345: short description why this is a dependency - #34567: ... --> <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> URL: #35776 Reported by: Frédéric Chapoton Reviewer(s): Matthias Köppe
2 parents 8f46a63 + 1d06129 commit af6ec5a

File tree

9 files changed

+176
-180
lines changed

9 files changed

+176
-180
lines changed

src/sage/matroids/basis_exchange_matroid.pyx

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ cdef class BasisExchangeMatroid(Matroid):
176176
self._E = groundset
177177
self._idx = {}
178178
cdef long i
179-
for i in xrange(self._groundset_size):
179+
for i in range(self._groundset_size):
180180
self._idx[self._E[i]] = i
181181

182182
if basis is not None:
@@ -219,7 +219,7 @@ cdef class BasisExchangeMatroid(Matroid):
219219
self._groundset = frozenset(E)
220220

221221
self._idx = {}
222-
for i in xrange(self._groundset_size):
222+
for i in range(self._groundset_size):
223223
self._idx[self._E[i]] = i
224224

225225
if self._weak_partition_var:
@@ -1090,7 +1090,7 @@ cdef class BasisExchangeMatroid(Matroid):
10901090
bitset_free(loops)
10911091
bitset_free(loop)
10921092
bitset_free(active_rows)
1093-
for i in xrange(self.full_rank()):
1093+
for i in range(self.full_rank()):
10941094
bitset_free(comp[i])
10951095
sig_free(comp)
10961096
return res
@@ -1267,16 +1267,16 @@ cdef class BasisExchangeMatroid(Matroid):
12671267
flats = <bitset_t*>sig_malloc((self.full_rank() + 1) * sizeof(bitset_t))
12681268
todo = <bitset_t*>sig_malloc((self.full_rank() + 1) * sizeof(bitset_t))
12691269

1270-
for i in xrange(self.full_rank() + 1):
1270+
for i in range(self.full_rank() + 1):
12711271
bitset_init(flats[i], self._bitset_size)
12721272
bitset_init(todo[i], self._bitset_size)
1273-
f_vec = [0 for i in xrange(self.full_rank() + 1)]
1273+
f_vec = [0 for i in range(self.full_rank() + 1)]
12741274
i = 0
12751275
bitset_clear(todo[0])
12761276
self.__closure(flats[0], todo[0])
12771277
bitset_complement(todo[0], flats[0])
12781278
self._f_vector_rec(f_vec, flats, todo, 0, 0)
1279-
for i in xrange(self.full_rank() + 1):
1279+
for i in range(self.full_rank() + 1):
12801280
bitset_free(flats[i])
12811281
bitset_free(todo[i])
12821282
sig_free(flats)
@@ -1340,7 +1340,7 @@ cdef class BasisExchangeMatroid(Matroid):
13401340
flats = <bitset_t*>sig_malloc((r + 1) * sizeof(bitset_t))
13411341
todo = <bitset_t*>sig_malloc((r + 1) * sizeof(bitset_t))
13421342

1343-
for i in xrange(r + 1):
1343+
for i in range(r + 1):
13441344
bitset_init(flats[i], self._bitset_size)
13451345
bitset_init(todo[i], self._bitset_size)
13461346
Rflats = SetSystem(self._E)
@@ -1349,7 +1349,7 @@ cdef class BasisExchangeMatroid(Matroid):
13491349
self.__closure(flats[0], todo[0])
13501350
bitset_complement(todo[0], flats[0])
13511351
self._flats_rec(Rflats, r, flats, todo, 0, 0)
1352-
for i in xrange(r + 1):
1352+
for i in range(r + 1):
13531353
bitset_free(flats[i])
13541354
bitset_free(todo[i])
13551355
sig_free(flats)
@@ -1415,7 +1415,7 @@ cdef class BasisExchangeMatroid(Matroid):
14151415
coflats = <bitset_t*>sig_malloc((r + 1) * sizeof(bitset_t))
14161416
todo = <bitset_t*>sig_malloc((r + 1) * sizeof(bitset_t))
14171417

1418-
for i in xrange(r + 1):
1418+
for i in range(r + 1):
14191419
bitset_init(coflats[i], self._bitset_size)
14201420
bitset_init(todo[i], self._bitset_size)
14211421
Rcoflats = SetSystem(self._E)
@@ -1424,7 +1424,7 @@ cdef class BasisExchangeMatroid(Matroid):
14241424
self.__coclosure(coflats[0], todo[0])
14251425
bitset_complement(todo[0], coflats[0])
14261426
self._coflats_rec(Rcoflats, r, coflats, todo, 0, 0)
1427-
for i in xrange(r + 1):
1427+
for i in range(r + 1):
14281428
bitset_free(coflats[i])
14291429
bitset_free(todo[i])
14301430
sig_free(coflats)
@@ -1462,28 +1462,28 @@ cdef class BasisExchangeMatroid(Matroid):
14621462
flats = <bitset_t*>sig_malloc((k + 1) * sizeof(bitset_t))
14631463
todo = <bitset_t*>sig_malloc((k + 1) * sizeof(bitset_t))
14641464

1465-
for i in xrange(k + 1):
1465+
for i in range(k + 1):
14661466
bitset_init(flats[i], self._bitset_size)
14671467
bitset_init(todo[i], self._bitset_size)
1468-
f_inc = [[0 for e in range(self._groundset_size + 1)] for i in xrange(k + 1)]
1468+
f_inc = [[0 for e in range(self._groundset_size + 1)] for i in range(k + 1)]
14691469
i = 0
14701470
bitset_clear(todo[0])
14711471
self.__closure(flats[0], todo[0])
14721472
bitset_complement(todo[0], flats[0])
14731473
self._flat_element_inv_rec(f_inc, k, flats, todo, 0, 0)
1474-
for i in xrange(k + 1):
1474+
for i in range(k + 1):
14751475
bitset_free(flats[i])
14761476
bitset_free(todo[i])
14771477
sig_free(flats)
14781478
sig_free(todo)
14791479
fie = {}
14801480
for e in range(self._groundset_size):
1481-
t = tuple([f_inc[i][e] for i in xrange(k + 1)])
1481+
t = tuple([f_inc[i][e] for i in range(k + 1)])
14821482
if t in fie:
14831483
fie[t].add(self._E[e])
14841484
else:
14851485
fie[t] = set([self._E[e]])
1486-
f_vec = tuple([f_inc[i][self._groundset_size] for i in xrange(k + 1)])
1486+
f_vec = tuple([f_inc[i][self._groundset_size] for i in range(k + 1)])
14871487
return fie, f_vec
14881488

14891489
cdef _flat_element_inv_rec(self, object f_inc, long R, bitset_t* flats, bitset_t* todo, long elt, long i):
@@ -1806,7 +1806,7 @@ cdef class BasisExchangeMatroid(Matroid):
18061806
if self.__is_independent(self._input):
18071807
bitset_copy(self._input2, self._current_basis)
18081808
e = bitset_first(self._current_basis)
1809-
for e in xrange(self._groundset_size):
1809+
for e in range(self._groundset_size):
18101810
if not bitset_in(self._current_basis, e):
18111811
self.__fundamental_circuit(self._output, e)
18121812
if e > bitset_first(self._output):
@@ -1854,7 +1854,7 @@ cdef class BasisExchangeMatroid(Matroid):
18541854
if self.__is_independent(self._input):
18551855
bitset_copy(self._input2, self._current_basis)
18561856
e = bitset_first(self._current_basis)
1857-
for e in xrange(self._groundset_size):
1857+
for e in range(self._groundset_size):
18581858
if not bitset_in(self._current_basis, e):
18591859
self.__fundamental_circuit(self._output, e)
18601860
if e > bitset_first(self._output):
@@ -2162,7 +2162,7 @@ cdef class BasisExchangeMatroid(Matroid):
21622162
Bitpacked version of ``is_isomorphism``.
21632163
"""
21642164
cdef long i
2165-
morph = [other._idx[morphism[self._E[i]]] for i in xrange(len(self))]
2165+
morph = [other._idx[morphism[self._E[i]]] for i in range(len(self))]
21662166
bitset_clear(self._input)
21672167
bitset_set_first_n(self._input, self._matroid_rank)
21682168
repeat = True
@@ -2226,15 +2226,15 @@ cdef class BasisExchangeMatroid(Matroid):
22262226
if self.full_rank() != other.full_rank():
22272227
return None
22282228
if self.full_rank() == 0 or self.full_corank() == 0:
2229-
return {self.groundset_list()[i]: other.groundset_list()[i] for i in xrange(len(self))}
2229+
return {self.groundset_list()[i]: other.groundset_list()[i] for i in range(len(self))}
22302230

22312231
if self._weak_invariant() != other._weak_invariant():
22322232
return None
22332233
PS = self._weak_partition()
22342234
PO = other._weak_partition()
22352235
if len(PS) == len(self) and len(PO) == len(other):
22362236
morphism = {}
2237-
for i in xrange(len(self)):
2237+
for i in range(len(self)):
22382238
morphism[min(PS[i])] = min(PO[i])
22392239
if self.__is_isomorphism(other, morphism):
22402240
return morphism
@@ -2247,7 +2247,7 @@ cdef class BasisExchangeMatroid(Matroid):
22472247
PO = other._strong_partition()
22482248
if len(PS) == len(self) and len(PO) == len(other):
22492249
morphism = {}
2250-
for i in xrange(len(self)):
2250+
for i in range(len(self)):
22512251
morphism[min(PS[i])] = min(PO[i])
22522252
if self.__is_isomorphism(other, morphism):
22532253
return morphism
@@ -2258,7 +2258,7 @@ cdef class BasisExchangeMatroid(Matroid):
22582258
PHS = self._heuristic_partition()
22592259
PHO = other._heuristic_partition()
22602260
morphism = {}
2261-
for i in xrange(len(self)):
2261+
for i in range(len(self)):
22622262
morphism[min(PHS[i])] = min(PHO[i])
22632263
if self.__is_isomorphism(other, morphism):
22642264
return morphism
@@ -2327,7 +2327,7 @@ cdef class BasisExchangeMatroid(Matroid):
23272327
PO = other._weak_partition()
23282328
if len(PS) == len(self) and len(PO) == len(other):
23292329
morphism = {}
2330-
for i in xrange(len(self)):
2330+
for i in range(len(self)):
23312331
morphism[min(PS[i])] = min(PO[i])
23322332
return self.__is_isomorphism(other, morphism)
23332333

@@ -2337,15 +2337,15 @@ cdef class BasisExchangeMatroid(Matroid):
23372337
PO = other._strong_partition()
23382338
if len(PS) == len(self) and len(PO) == len(other):
23392339
morphism = {}
2340-
for i in xrange(len(self)):
2340+
for i in range(len(self)):
23412341
morphism[min(PS[i])] = min(PO[i])
23422342
return self.__is_isomorphism(other, morphism)
23432343

23442344
if self._heuristic_invariant() == other._heuristic_invariant():
23452345
PHS = self._heuristic_partition()
23462346
PHO = other._heuristic_partition()
23472347
morphism = {}
2348-
for i in xrange(len(self)):
2348+
for i in range(len(self)):
23492349
morphism[min(PHS[i])] = min(PHO[i])
23502350
if self.__is_isomorphism(other, morphism):
23512351
return True

src/sage/matroids/basis_matroid.pyx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ cdef class BasisMatroid(BasisExchangeMatroid):
178178
bitset_init(self._bb, bc)
179179
bitset_set_first_n(self._bb, bc)
180180
NB = M.nonbases()
181-
for i in xrange(len(NB)):
181+
for i in range(len(NB)):
182182
bitset_discard(self._bb, set_to_index(NB._subsets[i]))
183183

184184
bitset_init(self._b, (<BasisExchangeMatroid>M)._bitset_size)
@@ -364,7 +364,7 @@ cdef class BasisMatroid(BasisExchangeMatroid):
364364
cdef BasisMatroid D
365365
D = BasisMatroid(groundset=self._E, rank=self.full_corank())
366366
N = binom[self._groundset_size][self._matroid_rank]
367-
for i in xrange(N):
367+
for i in range(N):
368368
if not bitset_in(self._bb, i):
369369
bitset_discard(D._bb, N - i - 1)
370370
D.reset_current_basis()
@@ -684,16 +684,16 @@ cdef class BasisMatroid(BasisExchangeMatroid):
684684
cdef long i, j
685685
cdef list bc
686686
cdef dict bi
687-
bc = [0 for i in xrange(len(self))]
688-
for i in xrange(binom[self._groundset_size][self._matroid_rank]):
687+
bc = [0 for i in range(len(self))]
688+
for i in range(binom[self._groundset_size][self._matroid_rank]):
689689
if not bitset_in(self._bb, i):
690690
index_to_set(self._b, i, self._matroid_rank, self._groundset_size)
691691
j = bitset_first(self._b)
692692
while j >= 0:
693693
bc[j] += 1
694694
j = bitset_next(self._b, j + 1)
695695
bi = {}
696-
for e in xrange(len(self)):
696+
for e in range(len(self)):
697697
if bc[e] in bi:
698698
bi[bc[e]].append(e)
699699
else:
@@ -902,7 +902,7 @@ cdef class BasisMatroid(BasisExchangeMatroid):
902902
bitset_complement(bb_comp, self._bb)
903903

904904
bitset_init(b2, max(len(self), 1))
905-
morph = [(<BasisMatroid>other)._idx[morphism[self._E[i]]] for i in xrange(len(self))]
905+
morph = [(<BasisMatroid>other)._idx[morphism[self._E[i]]] for i in range(len(self))]
906906
i = bitset_first(bb_comp)
907907
while i != -1:
908908
index_to_set(self._b, i, self._matroid_rank, self._groundset_size)
@@ -993,7 +993,7 @@ cdef class BasisMatroid(BasisExchangeMatroid):
993993
if self.full_rank() != other.full_rank():
994994
return None
995995
if self.full_rank() == 0:
996-
return {self.groundset_list()[i]: other.groundset_list()[i] for i in xrange(len(self))}
996+
return {self.groundset_list()[i]: other.groundset_list()[i] for i in range(len(self))}
997997
if self.bases_count() != other.bases_count():
998998
return None
999999

@@ -1003,7 +1003,7 @@ cdef class BasisMatroid(BasisExchangeMatroid):
10031003
PO = other._bases_partition()
10041004
if len(PS) == len(self) and len(PO) == len(other):
10051005
morphism = {}
1006-
for i in xrange(len(self)):
1006+
for i in range(len(self)):
10071007
morphism[min(PS[i])] = min(PO[i])
10081008
if self._is_relaxation(other, morphism):
10091009
return morphism
@@ -1016,7 +1016,7 @@ cdef class BasisMatroid(BasisExchangeMatroid):
10161016
PO = other._bases_partition2()
10171017
if len(PS) == len(self) and len(PO) == len(other):
10181018
morphism = {}
1019-
for i in xrange(len(self)):
1019+
for i in range(len(self)):
10201020
morphism[min(PS[i])] = min(PO[i])
10211021
if self._is_relaxation(other, morphism):
10221022
return morphism
@@ -1027,7 +1027,7 @@ cdef class BasisMatroid(BasisExchangeMatroid):
10271027
PHS = self._bases_partition3()
10281028
PHO = other._bases_partition3()
10291029
morphism = {}
1030-
for i in xrange(len(self)):
1030+
for i in range(len(self)):
10311031
morphism[min(PHS[i])] = min(PHO[i])
10321032
if self._is_relaxation(other, morphism):
10331033
return morphism
@@ -1085,7 +1085,7 @@ cdef class BasisMatroid(BasisExchangeMatroid):
10851085
PO = other._bases_partition()
10861086
if len(PS) == len(self) and len(PO) == len(other):
10871087
morphism = {}
1088-
for i in xrange(len(self)):
1088+
for i in range(len(self)):
10891089
morphism[min(PS[i])] = min(PO[i])
10901090
return self._is_relaxation(other, morphism)
10911091

@@ -1095,15 +1095,15 @@ cdef class BasisMatroid(BasisExchangeMatroid):
10951095
PO = other._bases_partition2()
10961096
if len(PS) == len(self) and len(PO) == len(other):
10971097
morphism = {}
1098-
for i in xrange(len(self)):
1098+
for i in range(len(self)):
10991099
morphism[min(PS[i])] = min(PO[i])
11001100
return self._is_relaxation(other, morphism)
11011101

11021102
if self._bases_invariant3() == other._bases_invariant3():
11031103
PHS = self._bases_partition3()
11041104
PHO = other._bases_partition3()
11051105
morphism = {}
1106-
for i in xrange(len(self)):
1106+
for i in range(len(self)):
11071107
morphism[min(PHS[i])] = min(PHO[i])
11081108
if self._is_relaxation(other, morphism):
11091109
return True
@@ -1246,7 +1246,7 @@ cdef binom_init(long N, long K):
12461246
if binom[0][0] != 1:
12471247
binom[0][0] = 1
12481248
binom[0][1] = 0
1249-
for n in xrange(1, 2955):
1249+
for n in range(1, 2955):
12501250
bin = 1
12511251
k = 0
12521252
while bin < 2 ** 32 and k <= 32 and k <= n:

src/sage/matroids/extension.pyx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,8 @@ cdef class LinearSubclasses:
340340

341341
self._planes_on_line = [[] for l in range(self._hyperlines_count)]
342342
self._lines_on_plane = [[] for l in range(self._hyperplanes_count)]
343-
for l in xrange(self._hyperlines_count):
344-
for h in xrange(self._hyperplanes_count):
343+
for l in range(self._hyperlines_count):
344+
for h in range(self._hyperplanes_count):
345345
if self._hyperplanes[h] >= self._hyperlines[l]:
346346
self._lines_on_plane[h].append(l)
347347
self._planes_on_line[l].append(h)
@@ -353,10 +353,10 @@ cdef class LinearSubclasses:
353353

354354
if line_length is not None:
355355
self._line_length = line_length
356-
self._mandatory_lines = [l for l in xrange(self._hyperlines_count) if len(self._planes_on_line[l]) >= line_length]
356+
self._mandatory_lines = [l for l in range(self._hyperlines_count) if len(self._planes_on_line[l]) >= line_length]
357357

358358
if subsets is not None:
359-
for p in xrange(self._hyperplanes_count):
359+
for p in range(self._hyperplanes_count):
360360
H = self._hyperplanes[p]
361361
for S in subsets:
362362
if frozenset(S).issubset(H):
@@ -370,7 +370,7 @@ cdef class LinearSubclasses:
370370
if len(E) != len(E2) or len(F) != 1:
371371
raise ValueError("LinearSubclasses: the ground set of the splice matroid is not of the form E + e-f")
372372

373-
for p in xrange(self._hyperplanes_count):
373+
for p in range(self._hyperplanes_count):
374374
X = self._hyperplanes[p] - F
375375
if splice._rank(X) == splice.full_rank() - 1:
376376
if splice._rank(X | F2) == splice.full_rank() - 1:
@@ -490,7 +490,7 @@ cdef class MatroidExtensions(LinearSubclasses):
490490
BM = BasisMatroid(M)
491491
LinearSubclasses.__init__(self, BM, line_length=line_length, subsets=subsets, splice=splice)
492492
self._BX = BM._extension(e, [])
493-
self._BH = [BM._extension(e, [self._hyperplanes[i]]) for i in xrange(len(self._hyperplanes))]
493+
self._BH = [BM._extension(e, [self._hyperplanes[i]]) for i in range(len(self._hyperplanes))]
494494
if orderly:
495495
self._orderly = True
496496

0 commit comments

Comments
 (0)