forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpycore_dynarray.h
More file actions
233 lines (207 loc) · 5.44 KB
/
Copy pathpycore_dynarray.h
File metadata and controls
233 lines (207 loc) · 5.44 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#ifndef Py_INTERNAL_DYNARRAY_H
#define Py_INTERNAL_DYNARRAY_H
#ifdef __cplusplus
extern "C" {
#endif
#include "Python.h" // Py_ssize_t
#ifndef Py_BUILD_CORE
# error "this header requires Py_BUILD_CORE define"
#endif
#define _PyDynArray_DEFAULT_SIZE 16
/*
* Deallocator for items on a _PyDynArray structure. A NULL pointer
* will never be given to the deallocator.
*/
typedef void (*_PyDynArray_Deallocator)(void *);
/*
* Internal only dynamic array for CPython.
*/
typedef struct {
/*
* The actual items in the dynamic array.
* Don't access this field publicly to get
* items--use _PyDynArray_GET_ITEM() instead.
*/
void **items;
/*
* The length of the actual items array allocation.
*/
Py_ssize_t capacity;
/*
* The number of items in the array.
* Don't use this field publicly--use _PyDynArray_LENGTH()
*/
Py_ssize_t length;
/*
* The deallocator, set by one of the initializer functions.
* This may be NULL.
*/
_PyDynArray_Deallocator deallocator;
} _PyDynArray;
static inline void
_PyDynArray_ASSERT_VALID(_PyDynArray *array)
{
assert(array != NULL);
assert(array->items != NULL);
}
static inline void
_PyDynArray_ASSERT_INDEX(_PyDynArray *array, Py_ssize_t index)
{
// Ensure the index is valid
assert(index >= 0);
assert(index < array->length);
}
/*
* Initialize a dynamic array with an initial size and deallocator.
*
* If the deallocator is NULL, then nothing happens to items upon
* removal and upon array clearing.
*
* Returns -1 upon failure, 0 otherwise.
*/
PyAPI_FUNC(int)
_PyDynArray_InitWithSize(_PyDynArray *array,
_PyDynArray_Deallocator deallocator,
Py_ssize_t initial);
/*
* Append to the array.
*
* Returns -1 upon failure, 0 otherwise.
* If this fails, the deallocator is not ran on the item.
*/
PyAPI_FUNC(int) _PyDynArray_Append(_PyDynArray *array, void *item);
/*
* Insert an item at the target index. The index
* must currently be a valid index in the array.
*
* Returns -1 upon failure, 0 otherwise.
* If this fails, the deallocator is not ran on the item.
*/
PyAPI_FUNC(int)
_PyDynArray_Insert(_PyDynArray *array, Py_ssize_t index, void *item);
/*
* Clear all the fields on the array.
*
* Note that this does *not* free the actual dynamic array
* structure--use _PyDynArray_Free() for that.
*
* It's safe to call _PyDynArray_Init() or InitWithSize() again
* on the array after calling this.
*/
PyAPI_FUNC(void) _PyDynArray_Clear(_PyDynArray *array);
/*
* Set a value at index in the array.
*
* If an item already exists at the target index, the deallocator
* is called on it, if the array has one set.
*
* This cannot fail.
*/
PyAPI_FUNC(void)
_PyDynArray_Set(_PyDynArray *array, Py_ssize_t index, void *item);
/*
* Remove the item at the index, and call the deallocator on it (if the array
* has one set).
*
* This cannot fail.
*/
PyAPI_FUNC(void)
_PyDynArray_Remove(_PyDynArray *array, Py_ssize_t index);
/*
* Remove the item at the index *without* deallocating it, and
* return the item.
*
* This cannot fail.
*/
PyAPI_FUNC(void *)
_PyDynArray_Pop(_PyDynArray *array, Py_ssize_t index);
/*
* Clear all the fields on a dynamic array, and then
* free the dynamic array structure itself.
*
* The array must have been created by _PyDynArray_New()
*/
static inline void
_PyDynArray_Free(_PyDynArray *array)
{
_PyDynArray_ASSERT_VALID(array);
_PyDynArray_Clear(array);
PyMem_RawFree(array);
}
/*
* Equivalent to _PyDynArray_InitWithSize() with a default size of 16.
*
* Returns -1 upon failure, 0 otherwise.
*/
static inline int
_PyDynArray_Init(_PyDynArray *array, _PyDynArray_Deallocator deallocator)
{
return _PyDynArray_InitWithSize(array, deallocator, _PyDynArray_DEFAULT_SIZE);
}
/*
* Allocate and create a new dynamic array on the heap.
*
* The returned pointer should be freed with _PyDynArray_Free()
* If this function fails, it returns NULL.
*/
static inline _PyDynArray *
_PyDynArray_NewWithSize(_PyDynArray_Deallocator deallocator, Py_ssize_t initial)
{
_PyDynArray *array = PyMem_RawMalloc(sizeof(_PyDynArray));
if (array == NULL)
{
return NULL;
}
if (_PyDynArray_InitWithSize(array, deallocator, initial) < 0)
{
PyMem_RawFree(array);
return NULL;
}
_PyDynArray_ASSERT_VALID(array); // Sanity check
return array;
}
/*
* Equivalent to _PyDynArray_NewWithSize() with a size of 16.
*
* The returned array must be freed with _PyDynArray_Free().
* Returns NULL on failure.
*/
static inline _PyDynArray *
_PyDynArray_New(_PyDynArray_Deallocator deallocator)
{
return _PyDynArray_NewWithSize(deallocator, _PyDynArray_DEFAULT_SIZE);
}
/*
* Get an item from the array. This cannot fail.
*
* If the index is not valid, this is undefined behavior.
*/
static inline void *
_PyDynArray_GET_ITEM(_PyDynArray *array, Py_ssize_t index)
{
_PyDynArray_ASSERT_VALID(array);
_PyDynArray_ASSERT_INDEX(array, index);
return array->items[index];
}
/*
* Get the length of the array. This cannot fail.
*/
static inline Py_ssize_t
_PyDynArray_LENGTH(_PyDynArray *array)
{
_PyDynArray_ASSERT_VALID(array);
return array->length;
}
/*
* Pop the item at the end the array.
* This function cannot fail.
*/
static inline void *
_PyDynArray_PopTop(_PyDynArray *array)
{
return _PyDynArray_Pop(array, _PyDynArray_LENGTH(array) - 1);
}
#ifdef __cplusplus
}
#endif
#endif /* !Py_INTERNAL_DYNARRAY_H */