summaryrefslogtreecommitdiff
path: root/python/src/sa/int_list.pxi
blob: 8ccf768621cb89e48f97e21f0d2b519309b87d81 (plain)
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
# defines int arrays in C, with some convenience methods
# for reading arrays as globs directly from disk.
# Adam Lopez <alopez@cs.umd.edu>

from libc.stdio cimport FILE, fopen, fread, fwrite, fclose
from libc.stdlib cimport malloc, realloc, free
from libc.string cimport memset, memcpy

cdef class IntList:

    def __cinit__(self, int size=0, int increment=1, int initial_len=0):
        if initial_len > size:
            size = initial_len
        self.size = size
        self.increment = increment
        self.len = initial_len
        self.arr = <int*> malloc(size*sizeof(int))
        memset(self.arr, 0, initial_len*sizeof(int))
        self.memory = self

    def __repr__(self):
        return 'IntList(%s)' % list(self)

    def index(self, val):
        cdef unsigned i
        for i in range(self.len):
            if self.arr[i] == val:
                return i
        raise ValueError('%s not in list' % val)

    def reset(self):
        self.len = 0

    def __dealloc__(self):
        if self.memory is self:
            free(self.arr)

    def __iter__(self):
        cdef int i
        for i in range(self.len):
            yield self.arr[i]

    def __getitem__(self, index):
        cdef int i, j, k
        if isinstance(index, int):
            j = index
            if j < 0: 
                j = self.len + j
            if j<0 or j>=self.len:
                raise IndexError("Requested index %d of %d-length IntList" % (index, self.len))
            return self.arr[j]
        elif isinstance(index, slice):
            i = index.start
            j = index.stop
            if i < 0:
                i = self.len + i
            if j < 0:
                j = self.len + j
            if i < 0 or i >= self.len or j < 0 or j > self.len:
                raise IndexError("Requested index %d:%d of %d-length IntList" % (index.start, index.stop, self.len))
            result = ()
            for k from i <= k < j:
                result = result + (self.arr[k],)
            return result
        else:
            raise TypeError("Illegal key type %s for IntList" % type(index))

    cdef void set(self, int i, int val):
        j = i
        if i<0:
            j = self.len + i
        if j<0 or j>=self.len:
            raise IndexError("Requested index %d of %d-length IntList" % (i, self.len))
        self.arr[j] = val
        
    def __setitem__(self, i, val):
        self.set(i, val)

    def __len__(self):
        return self.len

    def append(self, int val):
        self._append(val)

    cdef void _append(self, int val):
        if self.len == self.size:
            self.size = self.size + self.increment
            self.arr = <int*> realloc(self.arr, self.size*sizeof(int))
        self.arr[self.len] = val
        self.len = self.len + 1

    def extend(self, IntList other):
        self._extend_arr(other.arr, other.len)

    cdef void _extend_arr(self, int* other, int other_len):
        if self.size < self.len + other_len:
            self.size = self.len + other_len
            self.arr = <int*> realloc(self.arr, self.size*sizeof(int))
        memcpy(self.arr+self.len, other, other_len*sizeof(int))
        self.len = self.len + other_len

    cdef void _clear(self):
        free(self.arr)
        self.len = 0
        self.size = 0
        self.arr = <int*> malloc(0)

    cdef void write_handle(self, FILE* f):
        fwrite(&(self.len), sizeof(int), 1, f)
        fwrite(self.arr, sizeof(int), self.len, f)

    cdef void read_handle(self, FILE* f):
        free(self.arr)
        fread(&(self.len), sizeof(int), 1, f)
        self.arr = <int*> malloc(self.len * sizeof(int))
        self.size = self.len
        fread(self.arr, sizeof(int), self.len, f)

    cdef void read_mmaped(self, MemoryMap buf):
        free(self.arr)
        self.size = self.len = buf.read_int()
        self.arr = buf.read_int_array(self.len)
        self.memory = buf