Skip to content

Commit 39b4912

Browse files
author
Release Manager
committed
gh-35073: refactor(34773): combinatorial polyhedron: move list of pairs to dedicated class ### πŸ“š Description `CombinatorialPolyhedron` currently contains a lot of code related to (possibly long) lists of pairs. This code can and should be moved elsewhere. This is a step towards better readability and allowing future changes to the code. This is a first step of #34773. Along the way I update my outdated email address. <!-- Describe your changes here in detail --> <!-- Why is this change required? What problem does it solve? --> <!-- If it resolves an open issue, please link to the issue here. For example "Closes #1337" --> ### πŸ“ Checklist <!-- Put an `x` in all the boxes that apply. --> <!-- If your change requires a documentation PR, please link it appropriately --> <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> - [x] I have made sure that the title is self-explanatory and the description concisely explains the PR. - [x] I have linked an issue or discussion. - [x] I have created tests covering the changes. - [x] I have updated the documentation accordingly. ### βŒ› Dependencies <!-- List all open pull requests that this PR logically depends on --> <!-- - #xyz: short description why this is a dependency - #abc: ... --> URL: #35073 Reported by: Jonathan Kliem Reviewer(s): Jonathan Kliem, Matthias KΓΆppe, Travis Scrimshaw
2 parents 6760c40 + 0a710d3 commit 39b4912

24 files changed

+233
-249
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
cimport cython
2+
3+
cdef struct pair_s:
4+
size_t first
5+
size_t second
6+
7+
@cython.final
8+
cdef class ListOfPairs:
9+
cdef pair_s** _lists
10+
cdef size_t length
11+
12+
cdef inline int enlarge(self) except -1
13+
cdef inline int add(self, size_t first, size_t second) except -1
14+
cdef inline pair_s* get(self, size_t index) except NULL
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
r"""
2+
A data structure to store lists of integer pairs of large size.
3+
"""
4+
5+
# ****************************************************************************
6+
# Copyright (C) 2022 Jonathan Kliem <[email protected]>
7+
#
8+
# This program is free software: you can redistribute it and/or modify
9+
# it under the terms of the GNU General Public License as published by
10+
# the Free Software Foundation, either version 2 of the License, or
11+
# (at your option) any later version.
12+
# https://www.gnu.org/licenses/
13+
# ****************************************************************************
14+
15+
from cysignals.memory cimport check_reallocarray, check_allocarray, sig_free
16+
from sage.rings.integer cimport smallInteger
17+
18+
# Should be a power of two.
19+
# Should be neither exposed nor modified.
20+
cdef size_t length_per_list = 16348
21+
22+
cdef class ListOfPairs:
23+
def __dealloc__(self):
24+
cdef size_t n_lists = self.length // length_per_list
25+
cdef size_t i
26+
for i in range(n_lists):
27+
sig_free(self._lists[i])
28+
sig_free(self._lists)
29+
30+
cdef inline int enlarge(self) except -1:
31+
"""
32+
Increase size of list by one.
33+
"""
34+
if self.length % length_per_list:
35+
self.length += 1
36+
return 0
37+
38+
cdef size_t n_lists = self.length // length_per_list
39+
self._lists = <pair_s**> check_reallocarray(self._lists, n_lists + 1, sizeof(pair_s*))
40+
self._lists[n_lists] = <pair_s*> check_allocarray(length_per_list, sizeof(pair_s))
41+
self.length += 1
42+
43+
cdef inline pair_s* get(self, size_t index) except NULL:
44+
"""
45+
Return a pointer to a pair of the list corresponding to the ``index``.
46+
"""
47+
if not (0 <= index < self.length):
48+
raise IndexError
49+
50+
cdef size_t list_index = index // length_per_list
51+
cdef size_t index_in_list = index - list_index * length_per_list
52+
53+
return &self._lists[list_index][index_in_list]
54+
55+
def __getitem__(self, size_t index):
56+
r"""
57+
Get item of specified index.
58+
59+
EXAMPLES::
60+
61+
sage: from sage.data_structures.list_of_pairs import ListOfPairs
62+
sage: l = ListOfPairs()
63+
sage: l[0] = [1, 5]
64+
sage: l[0]
65+
(1, 5)
66+
sage: l[1]
67+
Traceback (most recent call last):
68+
...
69+
IndexError
70+
sage: l[-1]
71+
Traceback (most recent call last):
72+
...
73+
OverflowError: can't convert negative value to size_t
74+
"""
75+
cdef pair_s* pair = self.get(index)
76+
return (smallInteger(pair.first), smallInteger(pair.second))
77+
78+
def __setitem__(self, size_t index, value):
79+
r"""
80+
Set item of specified index.
81+
82+
Allows increasing the size of the list by at most 1.
83+
84+
EXAMPLES::
85+
86+
sage: from sage.data_structures.list_of_pairs import ListOfPairs
87+
sage: l = ListOfPairs()
88+
sage: l[0] = (2, 1)
89+
sage: l[1] = (1, 2)
90+
sage: l[0]
91+
(2, 1)
92+
sage: l[1]
93+
(1, 2)
94+
sage: l[10] = (5, 3)
95+
Traceback (most recent call last):
96+
...
97+
IndexError
98+
sage: l[2] = 2
99+
Traceback (most recent call last):
100+
...
101+
TypeError: 'sage.rings.integer.Integer' object is not iterable
102+
"""
103+
cdef size_t first, second
104+
(first, second) = value
105+
106+
if index == self.length:
107+
self.add(first, second)
108+
return
109+
110+
cdef pair_s* pair_pt = self.get(index)
111+
pair_pt.first = first
112+
pair_pt.second = second
113+
114+
cdef inline int add(self, size_t first, size_t second) except -1:
115+
"""
116+
Add a pair to the list.
117+
"""
118+
self.enlarge()
119+
cdef pair_s* last = self.get(self.length - 1)
120+
last.first = first
121+
last.second = second

β€Žsrc/sage/data_structures/sparse_bitset.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This is a regular bitset to which we will add additional structure.
66
In particular some representation of which limbs even contain data.
77
"""
88
# ****************************************************************************
9-
# Copyright (C) 2020 Jonathan Kliem <jonathan.kliem@fu-berlin.de>
9+
# Copyright (C) 2020 Jonathan Kliem <jonathan.kliem@gmail.com>
1010
#
1111
# This program is free software: you can redistribute it and/or modify
1212
# it under the terms of the GNU General Public License as published by

β€Žsrc/sage/geometry/polyhedron/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# Copyright (C) 2019 Julian Ritter
2222
# Copyright (C) 2019-2020 Laith Rastanawi
2323
# Copyright (C) 2019-2020 Sophia Elia
24-
# Copyright (C) 2019-2021 Jonathan Kliem <jonathan.kliem@fu-berlin.de>
24+
# Copyright (C) 2019-2021 Jonathan Kliem <jonathan.kliem@gmail.com>
2525
#
2626
# This program is free software: you can redistribute it and/or modify
2727
# it under the terms of the GNU General Public License as published by

β€Žsrc/sage/geometry/polyhedron/base0.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# Copyright (C) 2019 Julian Ritter
2222
# Copyright (C) 2019-2020 Laith Rastanawi
2323
# Copyright (C) 2019-2020 Sophia Elia
24-
# Copyright (C) 2019-2021 Jonathan Kliem <jonathan.kliem@fu-berlin.de>
24+
# Copyright (C) 2019-2021 Jonathan Kliem <jonathan.kliem@gmail.com>
2525
#
2626
# This program is free software: you can redistribute it and/or modify
2727
# it under the terms of the GNU General Public License as published by

β€Žsrc/sage/geometry/polyhedron/base1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
# Copyright (C) 2019 Julian Ritter
2525
# Copyright (C) 2019-2020 Laith Rastanawi
2626
# Copyright (C) 2019-2020 Sophia Elia
27-
# Copyright (C) 2019-2021 Jonathan Kliem <jonathan.kliem@fu-berlin.de>
27+
# Copyright (C) 2019-2021 Jonathan Kliem <jonathan.kliem@gmail.com>
2828
#
2929
# This program is free software: you can redistribute it and/or modify
3030
# it under the terms of the GNU General Public License as published by

β€Žsrc/sage/geometry/polyhedron/base2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# Copyright (C) 2019 Julian Ritter
2222
# Copyright (C) 2019-2020 Laith Rastanawi
2323
# Copyright (C) 2019-2020 Sophia Elia
24-
# Copyright (C) 2019-2021 Jonathan Kliem <jonathan.kliem@fu-berlin.de>
24+
# Copyright (C) 2019-2021 Jonathan Kliem <jonathan.kliem@gmail.com>
2525
#
2626
# This program is free software: you can redistribute it and/or modify
2727
# it under the terms of the GNU General Public License as published by

β€Žsrc/sage/geometry/polyhedron/base3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
# Copyright (C) 2019 Julian Ritter
2424
# Copyright (C) 2019-2020 Laith Rastanawi
2525
# Copyright (C) 2019-2020 Sophia Elia
26-
# Copyright (C) 2019-2021 Jonathan Kliem <jonathan.kliem@fu-berlin.de>
26+
# Copyright (C) 2019-2021 Jonathan Kliem <jonathan.kliem@gmail.com>
2727
#
2828
# This program is free software: you can redistribute it and/or modify
2929
# it under the terms of the GNU General Public License as published by

β€Žsrc/sage/geometry/polyhedron/base4.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
# Copyright (C) 2019 Julian Ritter
2525
# Copyright (C) 2019-2020 Laith Rastanawi
2626
# Copyright (C) 2019-2020 Sophia Elia
27-
# Copyright (C) 2019-2021 Jonathan Kliem <jonathan.kliem@fu-berlin.de>
27+
# Copyright (C) 2019-2021 Jonathan Kliem <jonathan.kliem@gmail.com>
2828
#
2929
# This program is free software: you can redistribute it and/or modify
3030
# it under the terms of the GNU General Public License as published by

β€Žsrc/sage/geometry/polyhedron/base5.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
# Copyright (C) 2019 Julian Ritter
2424
# Copyright (C) 2019-2020 Laith Rastanawi
2525
# Copyright (C) 2019-2020 Sophia Elia
26-
# Copyright (C) 2019-2021 Jonathan Kliem <jonathan.kliem@fu-berlin.de>
26+
# Copyright (C) 2019-2021 Jonathan Kliem <jonathan.kliem@gmail.com>
2727
#
2828
# This program is free software: you can redistribute it and/or modify
2929
# it under the terms of the GNU General Public License as published by

0 commit comments

Comments
Β (0)