Skip to content

Commit 110679f

Browse files
committed
Refactor _VarArray initialization logic
Inlined the pointer creation logic in _VarArray's __cinit__ method, removing the inner create_ptr function. This simplifies the code and improves readability while maintaining the same functionality.
1 parent d72f951 commit 110679f

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

src/pyscipopt/scip.pxi

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2467,27 +2467,23 @@ cdef class _VarArray:
24672467
cdef int size
24682468

24692469
def __cinit__(self, object vars):
2470-
def create_ptr(vars: Union[list, tuple, MatrixVariable]):
2471-
cdef SCIP_VAR** ptr
2472-
ptr = <SCIP_VAR**> malloc(len(vars) * sizeof(SCIP_VAR*)) if len(vars) > 0 else NULL
2473-
for i, var in enumerate(vars):
2474-
if not isinstance(var, Variable):
2475-
raise TypeError(f"Expected Variable, got {type(var)}.")
2476-
ptr[i] = (<Variable>var).scip_var
2477-
return ptr
2478-
24792470
if isinstance(vars, Variable):
24802471
self.size = 1
2481-
self.ptr = create_ptr([vars])
2472+
vars = [vars]
24822473
elif isinstance(vars, (list, tuple)):
24832474
self.size = len(vars)
2484-
self.ptr = create_ptr(vars)
24852475
elif isinstance(vars, MatrixVariable):
24862476
self.size = vars.size
2487-
self.ptr = create_ptr(np.ravel(vars))
2477+
vars = np.ravel(vars)
24882478
else:
24892479
raise TypeError(f"Expected Variable or list of Variable, got {type(vars)}.")
24902480

2481+
self.ptr = <SCIP_VAR**> malloc(self.size * sizeof(SCIP_VAR*)) if self.size else NULL
2482+
for i, var in enumerate(vars):
2483+
if not isinstance(var, Variable):
2484+
raise TypeError(f"Expected Variable, got {type(var)}.")
2485+
self.ptr[i] = (<Variable>var).scip_var
2486+
24912487
def __dealloc__(self):
24922488
if self.ptr != NULL:
24932489
free(self.ptr)

0 commit comments

Comments
 (0)