-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyuba.c
More file actions
162 lines (142 loc) · 4.16 KB
/
pyuba.c
File metadata and controls
162 lines (142 loc) · 4.16 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <Python.h>
/*XXX: add a free function pl0x */
/* this macro is safe right?~?!?~? pls don't use with an incrementer */
#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
typedef struct {
int *array; /*For future circular buffer use*/
int *start;
int *end;
int numitems;
int size;
} pyuba_struct;
/* XXX: we need to check for index and boundary safety*/
static PyObject *
pyuba_getitem(PyObject *self, PyObject *args)
{
int index;
pyuba_struct *pyuba_str = NULL;
PyObject *str_holder;
if (PyArg_ParseTuple(args, "Oi", &str_holder, &index) == 0){
return NULL;
}
pyuba_str = PyCapsule_GetPointer(str_holder, "");
return Py_BuildValue("i", pyuba_str->array[index]);
}
/* XXX: check for ability to shrink buffer size */
static PyObject *
pyuba_remove(PyObject *self, PyObject *args)
{
int index, i;
PyObject *str_holder;
pyuba_struct *pyuba_str;
if (PyArg_ParseTuple(args, "Oi", &str_holder, &index) == 0) {
return NULL;
}
pyuba_str = PyCapsule_GetPointer(str_holder, "");
for (i = index; i < (pyuba_str->numitems -1); i++){
pyuba_str->array[i] = pyuba_str->array[i + 1];
}
pyuba_str->numitems--;
pyuba_str->end--;
return Py_None;
}
/*XXX: improve (aka implement lolz) circular buffering */
static PyObject *
pyuba_insert(PyObject *self, PyObject *args)
{
int elem, index, i;
pyuba_struct *pyuba_ptr;
PyObject *str_holder;
if (PyArg_ParseTuple(args, "Oii" , &str_holder, &index, &elem) == 0){
return NULL;
}
pyuba_ptr = PyCapsule_GetPointer(str_holder, "");
if (index <= pyuba_ptr->numitems && pyuba_ptr->numitems < pyuba_ptr->size) {
pyuba_ptr->numitems++;
for (i = pyuba_ptr->numitems - 1; i > index; i--){
pyuba_ptr->array[i] = pyuba_ptr->array[i-1];
}
pyuba_ptr->end++;
pyuba_ptr->array[index] = elem;
Py_INCREF(Py_None);
return Py_None;
} else {
/* We need to extend !!! */
int *newarray = malloc(sizeof(int) * MAX(index, 2*pyuba_ptr->numitems));
(void) memcpy(newarray, pyuba_ptr->array,
(sizeof(int) * (pyuba_ptr->numitems)));
(void) free(pyuba_ptr->array);
pyuba_ptr->array = newarray;
pyuba_ptr->start = newarray;
pyuba_ptr->size = MAX(index, 2*pyuba_ptr->numitems);
pyuba_ptr->numitems++;
pyuba_ptr->array[index] = elem;
Py_INCREF(Py_None);
return Py_None;
}
}
/* can probably just map this to an insert at end of list... */
static PyObject *
pyuba_append(PyObject *self, PyObject *args)
{
int elem;
PyObject *str_holder;
int *newarray;
pyuba_struct *pyuba_str;
if (PyArg_ParseTuple(args, "Oi", &str_holder, &elem) == 0) {
return NULL;
}
pyuba_str = PyCapsule_GetPointer(str_holder, "");
if (++(pyuba_str->numitems) == pyuba_str->size){
/* We need to double the array size before insertion */
newarray = malloc(2 * sizeof(int) * (pyuba_str->numitems));
(void) memcpy(newarray, pyuba_str->array,
(pyuba_str->numitems * sizeof(int)));
(void) free(pyuba_str->array);
pyuba_str->array = newarray;
pyuba_str->start = newarray;
pyuba_str->end = newarray + pyuba_str->numitems;
pyuba_str->size *= 2;
} else {
pyuba_str->end++;
}
pyuba_str->array[pyuba_str->numitems - 1] = elem;
Py_INCREF(Py_None);
return Py_None;
}
/* this one might actually be ok~!!!~~ */
static PyObject *
new_pyuba_method(PyObject *self, PyObject *args)
{
int size;
int i;
PyObject *retcapsule;
if (PyArg_ParseTuple(args, "i", &size) == 0) {
return NULL;
}
/* Prepare pyuba structure */
pyuba_struct *mystruct = malloc(sizeof(pyuba_struct));
mystruct->array = malloc(sizeof(int) * size);
for (i = 0; i < size; i++){
mystruct->array[i] = 0;
}
mystruct->size = size;
mystruct->numitems = 0;
mystruct->start = mystruct->array;
mystruct->end = mystruct->start;
retcapsule = PyCapsule_New(mystruct, "", NULL);
return retcapsule;
}
static PyMethodDef pyuba_methods[] = {
{"insert", pyuba_insert, METH_VARARGS},
{"remove", pyuba_remove, METH_VARARGS},
{"append", pyuba_append, METH_VARARGS},
{"new_pyuba", new_pyuba_method, METH_VARARGS},
{"getitem", pyuba_getitem, METH_VARARGS},
{NULL, NULL}
};
void
initpyuba()
{
(void) Py_InitModule("pyuba", pyuba_methods);
}