@@ -780,6 +780,20 @@ def __contains__(self, x):
780
780
return False
781
781
return True
782
782
783
+ def unrank (self , x , rtn ):
784
+ ptr = 0
785
+ while True :
786
+ current_rank = self .rank (rtn )
787
+ if current_rank < x :
788
+ rtn [ptr + 1 ] = rtn [ptr ]
789
+ rtn [ptr ] = 0
790
+ ptr += 1
791
+ elif current_rank > x :
792
+ rtn [ptr ] -= 1
793
+ rtn [ptr - 1 ] += 1
794
+ else :
795
+ return self ._element_constructor_ (rtn )
796
+
783
797
784
798
class IntegerVectors_all (UniqueRepresentation , IntegerVectors ):
785
799
"""
@@ -945,22 +959,12 @@ def unrank(self, x):
945
959
sage: IntegerVectors(n=10).unrank(10)
946
960
[1, 9]
947
961
"""
948
- ptr = 0
949
962
rtn = [self .n ]
950
963
while self .rank (rtn ) < x :
951
964
rtn .append (0 )
952
965
rtn .pop ()
953
966
954
- while True :
955
- if self .rank (rtn ) < x :
956
- rtn [ptr + 1 ] = rtn [ptr ]
957
- rtn [ptr ] = 0
958
- ptr += 1
959
- elif self .rank (rtn ) > x :
960
- rtn [ptr ] -= 1
961
- rtn [ptr - 1 ] += 1
962
- else :
963
- return self ._element_constructor_ (rtn )
967
+ return IntegerVectors .unrank (self , x , rtn )
964
968
965
969
def cardinality (self ):
966
970
"""
@@ -1086,7 +1090,7 @@ def unrank(self, x):
1086
1090
1087
1091
INPUT:
1088
1092
1089
- - ``x`` -- an integer such that x < len( self) ``
1093
+ - ``x`` -- an integer such that x < self.cardinality( ) ``
1090
1094
1091
1095
EXAMPLES::
1092
1096
@@ -1098,22 +1102,14 @@ def unrank(self, x):
1098
1102
if self .k == 0 and x != 0 :
1099
1103
raise IndexError (f"Index { x } is out of range for the IntegerVector." )
1100
1104
else :
1101
- n , ptr = 0 , 0
1105
+ n = 0
1102
1106
rtn = [0 ]* self .k
1103
1107
while self .rank (rtn ) <= x :
1104
1108
n += 1
1105
- rtn [ptr ] = n
1106
- rtn [ptr ] -= 1
1107
- while True :
1108
- if self .rank (rtn ) < x :
1109
- rtn [ptr + 1 ] = rtn [ptr ]
1110
- rtn [ptr ] = 0
1111
- ptr += 1
1112
- elif self .rank (rtn ) > x :
1113
- rtn [ptr ] -= 1
1114
- rtn [ptr - 1 ] += 1
1115
- else :
1116
- return self ._element_constructor_ (rtn )
1109
+ rtn [0 ] = n
1110
+ rtn [0 ] -= 1
1111
+
1112
+ return IntegerVectors .unrank (self , x , rtn )
1117
1113
1118
1114
def cardinality (self ):
1119
1115
"""
@@ -1333,7 +1329,7 @@ def unrank(self, x):
1333
1329
1334
1330
INPUT:
1335
1331
1336
- - ``x`` -- an integer such that ``x < len( self)``
1332
+ - ``x`` -- an integer such that ``x < self.cardinality( )``
1337
1333
1338
1334
EXAMPLES::
1339
1335
@@ -1342,22 +1338,12 @@ def unrank(self, x):
1342
1338
sage: IntegerVectors(2,3).unrank(5)
1343
1339
[0, 0, 2]
1344
1340
"""
1345
- if x >= len ( self ):
1341
+ if x >= self . cardinality ( ):
1346
1342
raise IndexError (f"Index { x } is out of range for the IntegerVector." )
1347
1343
else :
1348
- ptr = 0
1349
1344
rtn = [0 ]* self .k
1350
- rtn [ptr ] = self .n
1351
- while True :
1352
- if self .rank (rtn ) < x :
1353
- rtn [ptr + 1 ] = rtn [ptr ]
1354
- rtn [ptr ] = 0
1355
- ptr += 1
1356
- elif self .rank (rtn ) > x :
1357
- rtn [ptr ] -= 1
1358
- rtn [ptr - 1 ] += 1
1359
- else :
1360
- return self ._element_constructor_ (rtn )
1345
+ rtn [0 ] = self .n
1346
+ return IntegerVectors .unrank (self , x , rtn )
1361
1347
1362
1348
def cardinality (self ):
1363
1349
"""
0 commit comments