Skip to content

Commit 49ddcd5

Browse files
jbrockmendeljreback
authored andcommitted
simplify skiplist inclusion/cimport to be more cythonize-friendly (pandas-dev#18420)
1 parent 674fb96 commit 49ddcd5

File tree

5 files changed

+68
-45
lines changed

5 files changed

+68
-45
lines changed

pandas/_libs/skiplist.pxd

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# -*- coding: utf-8 -*-
2+
# cython: profile=False
3+
4+
from cython cimport Py_ssize_t
5+
6+
from numpy cimport double_t
7+
8+
9+
cdef extern from "src/skiplist.h":
10+
ctypedef struct node_t:
11+
node_t **next
12+
int *width
13+
double value
14+
int is_nil
15+
int levels
16+
int ref_count
17+
18+
ctypedef struct skiplist_t:
19+
node_t *head
20+
node_t **tmp_chain
21+
int *tmp_steps
22+
int size
23+
int maxlevels
24+
25+
skiplist_t* skiplist_init(int) nogil
26+
void skiplist_destroy(skiplist_t*) nogil
27+
double skiplist_get(skiplist_t*, int, int*) nogil
28+
int skiplist_insert(skiplist_t*, double) nogil
29+
int skiplist_remove(skiplist_t*, double) nogil
30+
31+
32+
# Note: Node is declared here so that IndexableSkiplist can be exposed;
33+
# Node itself not intended to be exposed.
34+
cdef class Node:
35+
cdef public:
36+
double_t value
37+
list next
38+
list width
39+
40+
41+
cdef class IndexableSkiplist:
42+
cdef:
43+
Py_ssize_t size, maxlevels
44+
Node head
45+
46+
cpdef get(self, Py_ssize_t i)
47+
cpdef insert(self, double value)
48+
cpdef remove(self, double value)

pandas/_libs/src/skiplist.pyx renamed to pandas/_libs/skiplist.pyx

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
# Cython version: Wes McKinney
88

9-
cdef extern from "math.h":
10-
double log(double x)
9+
from libc.math cimport log
1110

1211
# MSVC does not have log2!
1312

@@ -16,6 +15,7 @@ cdef double Log2(double x):
1615

1716
cimport numpy as np
1817
import numpy as np
18+
from numpy cimport double_t
1919

2020
from random import random
2121

@@ -25,10 +25,10 @@ np.import_array()
2525
# TODO: optimize this, make less messy
2626

2727
cdef class Node:
28-
cdef public:
29-
double_t value
30-
list next
31-
list width
28+
# cdef public:
29+
# double_t value
30+
# list next
31+
# list width
3232

3333
def __init__(self, double_t value, list next, list width):
3434
self.value = value
@@ -43,9 +43,9 @@ cdef class IndexableSkiplist:
4343
Sorted collection supporting O(lg n) insertion, removal, and
4444
lookup by rank.
4545
"""
46-
cdef:
47-
Py_ssize_t size, maxlevels
48-
Node head
46+
# cdef:
47+
# Py_ssize_t size, maxlevels
48+
# Node head
4949

5050
def __init__(self, expected_size=100):
5151
self.size = 0

pandas/_libs/src/skiplist.pxd

-22
This file was deleted.

pandas/_libs/window.pyx

+6-11
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ cimport util
1414

1515
from libc.stdlib cimport malloc, free
1616

17-
1817
from numpy cimport ndarray, double_t, int64_t, float64_t
1918

19+
from skiplist cimport (IndexableSkiplist,
20+
node_t, skiplist_t,
21+
skiplist_init, skiplist_destroy,
22+
skiplist_get, skiplist_insert, skiplist_remove)
23+
2024
cdef np.float32_t MINfloat32 = np.NINF
2125
cdef np.float64_t MINfloat64 = np.NINF
2226

@@ -30,19 +34,10 @@ cdef inline int int_min(int a, int b): return a if a <= b else b
3034

3135
from util cimport numeric
3236

33-
from skiplist cimport (
34-
skiplist_t,
35-
skiplist_init,
36-
skiplist_destroy,
37-
skiplist_get,
38-
skiplist_insert,
39-
skiplist_remove)
40-
4137
cdef extern from "../src/headers/math.h":
42-
double sqrt(double x) nogil
4338
int signbit(double) nogil
39+
double sqrt(double x) nogil
4440

45-
include "skiplist.pyx"
4641

4742
# Cython implementations of rolling sum, mean, variance, skewness,
4843
# other statistical moment functions

setup.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ class CheckSDist(sdist_class):
341341
'pandas/_libs/missing.pyx',
342342
'pandas/_libs/testing.pyx',
343343
'pandas/_libs/window.pyx',
344+
'pandas/_libs/skiplist.pyx',
344345
'pandas/_libs/sparse.pyx',
345346
'pandas/_libs/parsers.pyx',
346347
'pandas/_libs/tslibs/strptime.pyx',
@@ -544,6 +545,9 @@ def pxd(name):
544545
'_libs.reshape': {
545546
'pyxfile': '_libs/reshape',
546547
'depends': _pxi_dep['reshape']},
548+
'_libs.skiplist': {
549+
'pyxfile': '_libs/skiplist',
550+
'depends': ['pandas/_libs/src/skiplist.h']},
547551
'_libs.sparse': {
548552
'pyxfile': '_libs/sparse',
549553
'depends': _pxi_dep['sparse']},
@@ -629,9 +633,7 @@ def pxd(name):
629633
'pyxfile': '_libs/testing'},
630634
'_libs.window': {
631635
'pyxfile': '_libs/window',
632-
'pxdfiles': ['_libs/src/skiplist', '_libs/src/util'],
633-
'depends': ['pandas/_libs/src/skiplist.pyx',
634-
'pandas/_libs/src/skiplist.h']},
636+
'pxdfiles': ['_libs/skiplist', '_libs/src/util']},
635637
'io.sas._sas': {
636638
'pyxfile': 'io/sas/sas'}}
637639

0 commit comments

Comments
 (0)