diff --git a/sklearn/cluster/_hierarchical.pyx b/sklearn/cluster/_hierarchical.pyx index b5b22000b36ec..069f119a2211f 100644 --- a/sklearn/cluster/_hierarchical.pyx +++ b/sklearn/cluster/_hierarchical.pyx @@ -18,6 +18,7 @@ from ..utils.fast_dict cimport IntFloatDict # C++ from cython.operator cimport dereference as deref, preincrement as inc from libcpp.map cimport map as cpp_map +from libc.math cimport fmax DTYPE = np.float64 ctypedef np.float64_t DTYPE_t @@ -25,10 +26,6 @@ ctypedef np.float64_t DTYPE_t ITYPE = np.intp ctypedef np.intp_t ITYPE_t -# Reimplementation for MSVC support -cdef inline double fmax(double a, double b): - return max(a, b) - ############################################################################### # Utilities for computing the ward momentum diff --git a/sklearn/neighbors/binary_tree.pxi b/sklearn/neighbors/binary_tree.pxi index 2eba04cdaabc7..b198016e258d8 100755 --- a/sklearn/neighbors/binary_tree.pxi +++ b/sklearn/neighbors/binary_tree.pxi @@ -145,6 +145,7 @@ cimport cython cimport numpy as np from libc.math cimport fabs, sqrt, exp, cos, pow, log, lgamma +from libc.math cimport fmin, fmax from libc.stdlib cimport calloc, malloc, free from libc.string cimport memcpy @@ -2610,12 +2611,6 @@ def nodeheap_sort(DTYPE_t[::1] vals): return np.asarray(vals_sorted), np.asarray(indices) -# Reimplementation for MSVC support -cdef inline double fmin(double a, double b): - return min(a, b) - -cdef inline double fmax(double a, double b) nogil: - return max(a, b) cdef inline DTYPE_t _total_node_weight(NodeData_t* node_data, DTYPE_t* sample_weight, diff --git a/sklearn/neighbors/quad_tree.pyx b/sklearn/neighbors/quad_tree.pyx index 9c22e5332801c..5f3454a5aa018 100644 --- a/sklearn/neighbors/quad_tree.pyx +++ b/sklearn/neighbors/quad_tree.pyx @@ -11,6 +11,7 @@ from cpython cimport Py_INCREF, PyObject, PyTypeObject from libc.stdlib cimport malloc, free from libc.string cimport memcpy from libc.stdio cimport printf +from libc.stdint cimport SIZE_MAX from ..tree._utils cimport safe_realloc, sizet_ptr_to_ndarray from ..utils import check_array @@ -29,11 +30,6 @@ cdef extern from "numpy/arrayobject.h": void* data, int flags, object obj) -# XXX using (size_t)(-1) is ugly, but SIZE_MAX is not available in C89 -# (i.e., older MSVC). -cdef SIZE_t DEFAULT = (-1) - - # Repeat struct definition for numpy CELL_DTYPE = np.dtype({ 'names': ['parent', 'children', 'cell_id', 'point_index', 'is_leaf', @@ -215,14 +211,14 @@ cdef class _QuadTree: Cell* child int i - # If the maximal capacity of the Tree have been reach, double the capacity + # If the maximal capacity of the Tree have been reached, double the capacity # We need to save the current cell id and the current point to retrieve them # in case the reallocation if self.cell_count + 1 > self.capacity: parent_id = cell.cell_id for i in range(self.n_dimensions): save_point[i] = point[i] - self._resize(DEFAULT) + self._resize(SIZE_MAX) cell = &self.cells[parent_id] point = save_point @@ -305,7 +301,7 @@ cdef class _QuadTree: cell.squared_max_width = 0 cell.cumulative_size = 0 for i in range(self.n_cells_per_cell): - cell.children[i] = DEFAULT + cell.children[i] = SIZE_MAX cdef void _init_root(self, DTYPE_t[3] min_bounds, DTYPE_t[3] max_bounds ) nogil: @@ -592,7 +588,7 @@ cdef class _QuadTree: with gil: raise MemoryError() - cdef int _resize_c(self, SIZE_t capacity=DEFAULT) nogil except -1: + cdef int _resize_c(self, SIZE_t capacity=SIZE_MAX) nogil except -1: """Guts of _resize Returns -1 in case of failure to allocate memory (and raise MemoryError) @@ -601,7 +597,7 @@ cdef class _QuadTree: if capacity == self.capacity and self.cells != NULL: return 0 - if capacity == DEFAULT: + if capacity == SIZE_MAX: if self.capacity == 0: capacity = 9 # default initial value to min else: diff --git a/sklearn/tree/_tree.pyx b/sklearn/tree/_tree.pyx index dd90611716f06..bbe2c8a796578 100644 --- a/sklearn/tree/_tree.pyx +++ b/sklearn/tree/_tree.pyx @@ -22,6 +22,7 @@ from libc.stdlib cimport free from libc.math cimport fabs from libc.string cimport memcpy from libc.string cimport memset +from libc.stdint cimport SIZE_MAX import numpy as np cimport numpy as np @@ -244,7 +245,7 @@ cdef class DepthFirstTreeBuilder(TreeBuilder): split.threshold, impurity, n_node_samples, weighted_n_node_samples) - if node_id == (-1): + if node_id == SIZE_MAX: rc = -1 break @@ -468,7 +469,7 @@ cdef class BestFirstTreeBuilder(TreeBuilder): is_left, is_leaf, split.feature, split.threshold, impurity, n_node_samples, weighted_n_node_samples) - if node_id == (-1): + if node_id == SIZE_MAX: return -1 # compute values also for split nodes (might become leafs later). @@ -691,9 +692,7 @@ cdef class Tree: with gil: raise MemoryError() - # XXX using (size_t)(-1) is ugly, but SIZE_MAX is not available in C89 - # (i.e., older MSVC). - cdef int _resize_c(self, SIZE_t capacity=(-1)) nogil except -1: + cdef int _resize_c(self, SIZE_t capacity=SIZE_MAX) nogil except -1: """Guts of _resize Returns -1 in case of failure to allocate memory (and raise MemoryError) @@ -702,7 +701,7 @@ cdef class Tree: if capacity == self.capacity and self.nodes != NULL: return 0 - if capacity == (-1): + if capacity == SIZE_MAX: if self.capacity == 0: capacity = 3 # default initial value else: @@ -738,7 +737,7 @@ cdef class Tree: if node_id >= self.capacity: if self._resize_c() != 0: - return (-1) + return SIZE_MAX cdef Node* node = &self.nodes[node_id] node.impurity = impurity @@ -1619,7 +1618,7 @@ cdef _build_pruned_tree( node.impurity, node.n_node_samples, node.weighted_n_node_samples) - if new_node_id == (-1): + if new_node_id == SIZE_MAX: rc = -1 break