| 32 | | self.size = 1 |
| 33 | | self.current = 0 |
| 34 | | self.list = <void **>py.PyMem_Malloc(sizeof(void *)) |
| 35 | | self.mutex = py.PyThread_allocate_lock() |
| 36 | | |
| 37 | | cdef void lock(self) : |
| 38 | | py.PyThread_acquire_lock(self.mutex,1) |
| 39 | | |
| 40 | | cdef int trylock(self) : |
| 41 | | return py.PyThread_acquire_lock(self.mutex,0) |
| 42 | | |
| 43 | | cdef void unlock(self) : |
| 44 | | py.PyThread_release_lock(self.mutex) |
| 45 | | |
| 46 | | cdef void append(self, void *child) : |
| 47 | | if self.size == self.current : |
| 48 | | self.size = self.size * 2 |
| 49 | | self.list = <void **>py.PyMem_Realloc(self.list, |
| 50 | | sizeof(void *)*(self.size)) |
| 51 | | self.list[self.current] = <void *>child |
| 52 | | self.current = self.current + 1 |
| 53 | | |
| 54 | | cdef int index(self, void *child) : |
| 55 | | cdef int i |
| 56 | | for i from 0 <= i < self.current : |
| 57 | | if self.list[i] == child : |
| 58 | | return i |
| 59 | | return -1 |
| 60 | | |
| 61 | | cdef void swap(self, int first, int second) : |
| 62 | | cdef void *sibling |
| 63 | | sibling = self.list[first] |
| 64 | | self.list[first] = self.list[second] |
| 65 | | self.list[second] = sibling |
| 66 | | |
| 67 | | cdef void down(self, void *child) : |
| 68 | | cdef int index |
| 69 | | index = self.index(child) |
| 70 | | if index > 0 : |
| 71 | | self.swap(index, index-1) |
| 72 | | |
| 73 | | cdef void up(self, void *child) : |
| 74 | | cdef int index |
| 75 | | index = self.index(child) |
| 76 | | if index == -1 : |
| 77 | | return |
| 78 | | if index < self.current-1 : |
| 79 | | self.swap(index, index+1) |
| 80 | | |
| 81 | | cdef void top(self, void *child) : |
| 82 | | cdef int index |
| 83 | | index = self.index(child) |
| 84 | | if index == -1 : |
| 85 | | return |
| 86 | | if index < self.current-1 : |
| 87 | | self.swap(index, self.current-1) |
| 88 | | |
| 89 | | cdef void bottom(self, void *child) : |
| 90 | | cdef int index |
| 91 | | index = self.index(child) |
| 92 | | if index > 0 : |
| 93 | | self.swap(index, 0) |
| 94 | | |
| 95 | | cdef void remove(self, void *child) : |
| 96 | | cdef int i |
| 97 | | cdef int index |
| 98 | | index = self.index(child) |
| 99 | | if index == -1 : |
| 100 | | return |
| 101 | | for i from index <= i < (self.current-1) : |
| 102 | | self.list[i] = self.list[i+1] |
| 103 | | self.current = self.current - 1 |
| 104 | | |
| 105 | | cdef void empty(self) : |
| 106 | | py.PyMem_Free(self.list) |
| 107 | | self.size = 1 |
| 108 | | self.current = 0 |
| 109 | | self.list = <void **>py.PyMem_Malloc(sizeof(void *)) |
| | 32 | self._size = 1 |
| | 33 | self._current = 0 |
| | 34 | self._list = <void **>py.PyMem_Malloc(sizeof(void *)) |
| | 35 | self._mutex = py.PyThread_allocate_lock() |
| 112 | | py.PyMem_Free(self.list) |
| 113 | | py.PyThread_free_lock(self.mutex) |
| | 38 | py.PyMem_Free(self._list) |
| | 39 | py.PyThread_free_lock(self._mutex) |
| | 40 | |
| | 41 | cdef void _lock(self) : |
| | 42 | py.PyThread_acquire_lock(self._mutex, 1) |
| | 43 | |
| | 44 | cdef int _trylock(self) : |
| | 45 | return py.PyThread_acquire_lock(self._mutex, 0) |
| | 46 | |
| | 47 | cdef void _unlock(self) : |
| | 48 | py.PyThread_release_lock(self._mutex) |
| | 49 | |
| | 50 | cdef void _append(self, void* _child) : |
| | 51 | self._lock() |
| | 52 | if self._size == self._current : |
| | 53 | self._size = self._size * 2 |
| | 54 | self._list = <void **> py.PyMem_Realloc(self._list, |
| | 55 | sizeof(void *) * (self._size)) |
| | 56 | self._list[self._current] = <void *> _child |
| | 57 | self._current = self._current + 1 |
| | 58 | self._unlock() |
| | 59 | |
| | 60 | cdef int _index(self, void* _child) : |
| | 61 | cdef int _i |
| | 62 | self._lock() |
| | 63 | for _i from 0 <= _i < self._current : |
| | 64 | if self._list[_i] == _child : |
| | 65 | return _i |
| | 66 | return -1 |
| | 67 | self._unlock() |
| | 68 | |
| | 69 | cdef void _swap(self, int _first, int _second) : |
| | 70 | cdef void* _sibling |
| | 71 | _sibling = self._list[_first] |
| | 72 | self._list[_first] = self._list[_second] |
| | 73 | self._list[_second] = _sibling |
| | 74 | |
| | 75 | cdef void _down(self, void* _child) : |
| | 76 | cdef int _index |
| | 77 | _index = self._index(_child) |
| | 78 | if _index > 0 : |
| | 79 | self._swap(_index, _index - 1) |
| | 80 | |
| | 81 | cdef void _up(self, void* _child) : |
| | 82 | cdef int _index |
| | 83 | _index = self._index(_child) |
| | 84 | if _index == -1 : |
| | 85 | return |
| | 86 | if _index < self._current-1 : |
| | 87 | self._swap(_index, _index + 1) |
| | 88 | |
| | 89 | cdef void _top(self, void* _child) : |
| | 90 | cdef int _index |
| | 91 | _index = self._index(_child) |
| | 92 | if _index == -1 : |
| | 93 | return |
| | 94 | if _index < self._current - 1 : |
| | 95 | self._swap(_index, self._current - 1) |
| | 96 | |
| | 97 | cdef void _bottom(self, void* _child) : |
| | 98 | cdef int _index |
| | 99 | _index = self._index(_child) |
| | 100 | if _index > 0 : |
| | 101 | self._swap(_index, 0) |
| | 102 | |
| | 103 | cdef void _remove(self, void* _child) : |
| | 104 | cdef int _i, _index |
| | 105 | self._lock() |
| | 106 | _index = self._index(_child) |
| | 107 | if _index == -1 : |
| | 108 | return |
| | 109 | for _i from _index <= _i < (self._current - 1) : |
| | 110 | self._list[_i] = self._list[_i + 1] |
| | 111 | self._current = self._current - 1 |
| | 112 | self._unlock() |
| | 113 | |
| | 114 | cdef void _empty(self) : |
| | 115 | py.PyMem_Free(self._list) |
| | 116 | self._size = 1 |
| | 117 | self._current = 0 |
| | 118 | self._list = <void **>py.PyMem_Malloc(sizeof(void *)) |