File: setlist.pxd

package info (click to toggle)
python-scipy 0.14.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 52,228 kB
  • ctags: 63,719
  • sloc: python: 112,726; fortran: 88,685; cpp: 86,979; ansic: 85,860; makefile: 530; sh: 236
file content (130 lines) | stat: -rw-r--r-- 3,087 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# -*- cython -*-
"""
List of sets of integers, low-level C implementation

Works similarly as

    setlist = [set() for j in range(n)]

but with integer values.

"""

cimport libc.stdlib
cimport numpy as np
import numpy as np

cdef struct setlist_t:
    size_t n
    size_t *sizes
    size_t *alloc_sizes
    int **sets

cdef inline int init(setlist_t *setlist, size_t n, size_t size_guess) except -1:
    """
    Initialise a list of `n` sets with a given guessed size
    """
    cdef int j

    setlist.n = n

    setlist.sets = <int**>libc.stdlib.malloc(sizeof(int*) * n)
    if setlist.sets == NULL:
        raise MemoryError

    setlist.sizes = <size_t*>libc.stdlib.malloc(sizeof(size_t) * n)
    if setlist.sizes == NULL:
        libc.stdlib.free(<void*>setlist.sets)
        raise MemoryError

    setlist.alloc_sizes = <size_t*>libc.stdlib.malloc(sizeof(size_t) * n)
    if setlist.alloc_sizes == NULL:
        libc.stdlib.free(<void*>setlist.sets)
        libc.stdlib.free(<void*>setlist.sizes)
        raise MemoryError

    for j in xrange(n):
        setlist.sizes[j] = 0
        setlist.alloc_sizes[j] = size_guess
        setlist.sets[j] = <int*>libc.stdlib.malloc(sizeof(int) * size_guess)

    return 0

cdef inline void free(setlist_t *setlist):
    """
    Free the set list
    """

    cdef int j
    for j in xrange(setlist.n):
        libc.stdlib.free(<void*>setlist.sets[j])
    libc.stdlib.free(<void*>setlist.sets)
    libc.stdlib.free(<void*>setlist.sizes)
    libc.stdlib.free(<void*>setlist.alloc_sizes)
    setlist.sets = NULL
    setlist.sizes = NULL
    setlist.alloc_sizes = NULL
    setlist.n = 0

cdef inline int add(setlist_t *setlist, int n, int value) nogil:
    """
    Add a value to set `n`
    """

    cdef size_t i, sz
    cdef int *p

    if n < 0 or n >= setlist.n:
        return 1

    for i in xrange(setlist.sizes[n]):
        if setlist.sets[n][i] == value:
            return 0

    if setlist.sizes[n] >= setlist.alloc_sizes[n]:
        sz = 2*setlist.alloc_sizes[n] + 1
        p = <int*>libc.stdlib.realloc(<void*>setlist.sets[n], sz * sizeof(int))
        if p == NULL:
            return -1
        setlist.sets[n] = p
        setlist.alloc_sizes[n] = sz

    setlist.sets[n][setlist.sizes[n]] = value
    setlist.sizes[n] += 1

    return 0

cdef inline object tocsr(setlist_t *setlist):
    """
    Convert list of sets to CSR format

    Integers for set `i` reside in data[indices[i]:indices[i+1]]

    Returns
    -------
    indices
        CSR indices
    data
        CSR data

    """
    cdef size_t i, j, pos
    cdef size_t total_size
    cdef np.ndarray[np.npy_int, ndim=1] indices, data

    total_size = 0
    for j in xrange(setlist.n):
        total_size += setlist.sizes[j]

    indices = np.empty((setlist.n+1,), dtype=np.intc)
    data = np.empty((total_size,), dtype=np.intc)

    pos = 0
    for i in xrange(setlist.n):
        indices[i] = pos
        for j in xrange(setlist.sizes[i]):
            data[pos] = setlist.sets[i][j]
            pos += 1
    indices[setlist.n] = pos

    return indices, data