Skip to content

Commit 74df778

Browse files
committed
pyqueue: add internal queue data structure
1 parent 86efa7d commit 74df778

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

Include/cpython/pyqueue.h

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef Py_PYQUEUE_H
2+
#define Py_PYQUEUE_H
3+
/* Header excluded from the stable API */
4+
#ifndef Py_LIMITED_API
5+
6+
// See pycore_pyqueue.h for the implementation of the queue API.
7+
8+
struct _Py_queue_node {
9+
struct _Py_queue_node *next;
10+
};
11+
12+
struct _Py_queue_head {
13+
struct _Py_queue_node first;
14+
struct _Py_queue_node *tail;
15+
};
16+
17+
#endif /* !defined(Py_LIMITED_API) */
18+
#endif /* !Py_PYQUEUE_H */

Include/internal/pycore_pyqueue.h

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#ifndef Py_INTERNAL_PYQUEUE_H
2+
#define Py_INTERNAL_PYQUEUE_H
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
#ifndef Py_BUILD_CORE
8+
# error "this header requires Py_BUILD_CORE define"
9+
#endif
10+
11+
// Implementation of a queue that uses a singly linked list of
12+
// struct _Py_queue_node pointers. The queue is represented by a
13+
// struct _Py_queue_head which contains pointers to the first and
14+
// last node in the queue.
15+
16+
static inline void
17+
_Py_queue_init(struct _Py_queue_head *head)
18+
{
19+
head->first.next = head->tail = &head->first;
20+
}
21+
22+
static inline bool
23+
_Py_queue_is_empty(struct _Py_queue_head *head)
24+
{
25+
return head->first.next == &head->first;
26+
}
27+
28+
static inline void
29+
_Py_queue_enqeue(struct _Py_queue_head *head, struct _Py_queue_node *node)
30+
{
31+
node->next = &head->first;
32+
head->tail->next = node;
33+
head->tail = node;
34+
}
35+
36+
static inline struct _Py_queue_node *
37+
_Py_queue_dequeue(struct _Py_queue_head *head)
38+
{
39+
if (_Py_queue_is_empty(head)) {
40+
return NULL;
41+
}
42+
struct _Py_queue_node *node = head->first.next;
43+
head->first.next = node->next;
44+
if (node->next == &head->first) {
45+
head->tail = &head->first;
46+
}
47+
return node;
48+
}
49+
50+
#define _Py_queue_data(node, type, member) \
51+
(type*)((char*)node - offsetof(type, member))
52+
53+
#define _Py_queue_first(head, type, member) (_Py_queue_data(((head)->first.next), type, member))
54+
55+
#define _Py_queue_last(head, type, member) (_Py_queue_data(((head)->tail), type, member))
56+
57+
58+
#ifdef __cplusplus
59+
}
60+
#endif
61+
#endif // !Py_INTERNAL_PYQUEUE_H

Makefile.pre.in

+1
Original file line numberDiff line numberDiff line change
@@ -1706,6 +1706,7 @@ PYTHON_HEADERS= \
17061706
$(srcdir)/Include/internal/pycore_pylifecycle.h \
17071707
$(srcdir)/Include/internal/pycore_pymem.h \
17081708
$(srcdir)/Include/internal/pycore_pymem_init.h \
1709+
$(srcdir)/Include/internal/pycore_pyqueue.h \
17091710
$(srcdir)/Include/internal/pycore_pystate.h \
17101711
$(srcdir)/Include/internal/pycore_pythread.h \
17111712
$(srcdir)/Include/internal/pycore_range.h \

PCbuild/pythoncore.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@
251251
<ClInclude Include="..\Include\internal\pycore_pylifecycle.h" />
252252
<ClInclude Include="..\Include\internal\pycore_pymem.h" />
253253
<ClInclude Include="..\Include\internal\pycore_pymem_init.h" />
254+
<ClInclude Include="..\Include\internal\pycore_pyqueue.h" />
254255
<ClInclude Include="..\Include\internal\pycore_pystate.h" />
255256
<ClInclude Include="..\Include\internal\pycore_pythread.h" />
256257
<ClInclude Include="..\Include\internal\pycore_qsbr.h" />

PCbuild/pythoncore.vcxproj.filters

+3
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,9 @@
663663
<ClInclude Include="..\Include\internal\pycore_pymem_init.h">
664664
<Filter>Include\internal</Filter>
665665
</ClInclude>
666+
<ClInclude Include="..\Include\internal\pycore_pyqueue.h">
667+
<Filter>Include\internal</Filter>
668+
</ClInclude>
666669
<ClInclude Include="..\Include\internal\pycore_pystate.h">
667670
<Filter>Include\internal</Filter>
668671
</ClInclude>

0 commit comments

Comments
 (0)