summaryrefslogtreecommitdiff
path: root/python/src/sa/float_list.pxi
blob: b98e444f5ffa84d347cfd0e3c5b463a3213d89d2 (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
# defines int arrays in C, with some convenience methods
# for reading arrays directly 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, strcpy, strlen

cdef class FloatList:

    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 = <float*> malloc(size*sizeof(float))
        memset(self.arr, 0, initial_len*sizeof(float))

    def __dealloc__(self):
        free(self.arr)

    def __getitem__(self, i):
        j = i
        if i<0: 
            j = self.len + i
        if j<0 or j>=self.len:
            raise IndexError("Requested index %d of %d-length FloatList" % (i, self.len))
        return self.arr[j]

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

    def __len__(self):
        return self.len

    def append(self, float val):
        if self.len == self.size:
            self.size = self.size + self.increment
            self.arr = <float*> realloc(self.arr, self.size*sizeof(float))
        self.arr[self.len] = val
        self.len = self.len + 1

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

    def write(self, char* filename):
        cdef FILE* f
        f = fopen(filename, "w")
        self.write_handle(f)
        fclose(f)

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

    def read(self, char* filename):
        cdef FILE* f
        f = fopen(filename, "r")
        self.read_handle(f)
        fclose(f)