Skip to content

Commit a65c0b1

Browse files
author
Release Manager
committed
gh-38936: use `sig_realloc` in method `OP_make_set` Method `OP_make_set` was introduced in #38692 in file `src/sage/groups/perm_gps/partn_ref/data_structures.pxd` to add the method `make_set` to `DisjointSet` (i.e., the ability to add a new element to a disjoint set). We improve the method by using `sig_realloc` rather than a combination of `sig_malloc` and `memcpy`. This requires some changes in methods `OP_new` and `OP_dealloc`. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [x] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - #12345: short description why this is a dependency --> <!-- - #34567: ... --> URL: #38936 Reported by: David Coudert Reviewer(s): David Coudert, gmou3
2 parents 6d24b05 + 52f8ec8 commit a65c0b1

File tree

3 files changed

+101
-55
lines changed

3 files changed

+101
-55
lines changed

src/sage/groups/perm_gps/partn_ref/data_structures.pxd

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ cdef inline int OP_copy_from_to(OrbitPartition *OP, OrbitPartition *OP2) noexcep
8181
- OP2.degree == OP.degree
8282
- OP2.num_cells == OP.num_cells
8383
"""
84-
memcpy(OP2.parent, OP.parent, 4*OP.degree * sizeof(int) )
84+
memcpy(OP2.parent, OP.parent, OP.degree * sizeof(int))
85+
memcpy(OP2.rank, OP.rank, OP.degree * sizeof(int))
86+
memcpy(OP2.mcr, OP.mcr, OP.degree * sizeof(int))
87+
memcpy(OP2.size, OP.size, OP.degree * sizeof(int))
8588

8689
cdef inline OrbitPartition *OP_copy(OrbitPartition *OP) noexcept:
8790
"""
@@ -138,38 +141,7 @@ cdef inline void OP_join(OrbitPartition *OP, int m, int n) noexcept:
138141
if m_root != n_root:
139142
OP.num_cells -= 1
140143

141-
142-
cdef inline void OP_make_set(OrbitPartition *OP) noexcept:
143-
cdef int i, n = OP.degree
144-
cdef int *new_parent, *new_rank, *new_mcr, *new_size
145-
146-
cdef int *int_array = <int *> sig_malloc(4*(n+1) * sizeof(int))
147-
if int_array is NULL:
148-
raise MemoryError("MemoryError allocating int_array in make_set method")
149-
150-
OP.degree = n + 1
151-
OP.num_cells = OP.num_cells + 1
152-
new_parent = int_array
153-
new_rank = int_array + (n + 1)
154-
new_mcr = int_array + (2*n + 2)
155-
new_size = int_array + (3 * n + 3)
156-
157-
memcpy(new_parent, OP.parent, n * sizeof(int))
158-
memcpy(new_rank, OP.rank, n * sizeof(int))
159-
memcpy(new_mcr, OP.mcr, n * sizeof(int))
160-
memcpy(new_size, OP.size, n * sizeof(int))
161-
162-
new_parent[n] = n
163-
new_rank[n] = 0
164-
new_mcr[n] = n
165-
new_size[n] = 1
166-
167-
sig_free(OP.parent)
168-
169-
OP.parent = new_parent
170-
OP.rank = new_rank
171-
OP.mcr = new_mcr
172-
OP.size = new_size
144+
cdef void OP_make_set(OrbitPartition *OP) noexcept
173145

174146
cdef inline int OP_merge_list_perm(OrbitPartition *OP, int *gamma) noexcept:
175147
"""

0 commit comments

Comments
 (0)