-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.pyx
More file actions
54 lines (40 loc) · 1.3 KB
/
linked_list.pyx
File metadata and controls
54 lines (40 loc) · 1.3 KB
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
from cython.operator cimport dereference as dref
from libc.stdlib cimport malloc, free
from cpppython.hash import hash_any
cdef extern from "linked_list.h":
cdef cppclass LinkedList[T]:
LinkedList()
size_t add(T element)
size_t length()
T pop() except +
void remove(size_t i) except +
T get(size_t i) except +
cdef class PyLinkedList:
cdef LinkedList[long int] *thisptr
store = {}
def __cinit__(self):
self.thisptr = new LinkedList[long int]()
def __dealloc__(self):
del self.thisptr
def add(self, element):
cdef long int key = hash_any(element)
self.store[<object> key] = element
self.thisptr.add(key)
def length(self):
return <object> self.thisptr.length()
def pop(self):
cdef long int key = self.thisptr.pop()
retval = self.store[<object> key]
del self.store[<object> key]
return retval
def remove(self, i):
self.thisptr.remove(<size_t> i)
def get(self, i):
cdef long int key = self.thisptr.get(i)
return self.store[<object> key]
def __len__(self):
return <object> self.thisptr.length()
def __getitem__(self, i):
if i<0:
return self.get(len(self) + i)
return self.get(i)