py-doubly-linked-list 0.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of py-doubly-linked-list might be problematic. Click here for more details.
- py_doubly_linked_list-0.1.1/LICENSE +7 -0
- py_doubly_linked_list-0.1.1/PKG-INFO +105 -0
- py_doubly_linked_list-0.1.1/README.MD +78 -0
- py_doubly_linked_list-0.1.1/pyproject.toml +44 -0
- py_doubly_linked_list-0.1.1/setup.cfg +4 -0
- py_doubly_linked_list-0.1.1/src/doubly_linked_list.c +778 -0
- py_doubly_linked_list-0.1.1/src/py_doubly_linked_list/__init__.py +1 -0
- py_doubly_linked_list-0.1.1/src/py_doubly_linked_list/__init__.pyi +48 -0
- py_doubly_linked_list-0.1.1/src/py_doubly_linked_list.egg-info/PKG-INFO +105 -0
- py_doubly_linked_list-0.1.1/src/py_doubly_linked_list.egg-info/SOURCES.txt +11 -0
- py_doubly_linked_list-0.1.1/src/py_doubly_linked_list.egg-info/dependency_links.txt +1 -0
- py_doubly_linked_list-0.1.1/src/py_doubly_linked_list.egg-info/top_level.txt +1 -0
- py_doubly_linked_list-0.1.1/tests/test_build.py +30 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright (c) 2025 Joshua A. Morningstar
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: py-doubly-linked-list
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: A library adding doubly linked lists to python.
|
|
5
|
+
Author-email: Joshua Morningstar <joshuam18745@gmail.com>
|
|
6
|
+
Maintainer-email: Joshua Morningstar <joshuam18745@gmail.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
Project-URL: Documentation, https://github.com/JoshuaM176/PyDoublyLinkedList/blob/main/README.MD
|
|
9
|
+
Project-URL: Repository, https://github.com/JoshuaM176/PyDoublyLinkedList
|
|
10
|
+
Project-URL: Issues, https://github.com/JoshuaM176/PyDoublyLinkedList/issues
|
|
11
|
+
Project-URL: Changelog, https://github.com/JoshuaM176/PyDoublyLinkedList/blob/main/CHANGES
|
|
12
|
+
Keywords: doubly linked list,data structure
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
22
|
+
Requires-Python: >=3.9
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
License-File: README.MD
|
|
26
|
+
Dynamic: license-file
|
|
27
|
+
|
|
28
|
+
# py-doubly-linked-list
|
|
29
|
+
## Description
|
|
30
|
+
This library provides an implementation of a doubly linked list for python written with the c api. It is meant to be as close as possible to a regular list in its interface, meaning you can interact with it in almost all of the same ways you interact with a regular python list.
|
|
31
|
+
## Methods
|
|
32
|
+
- append
|
|
33
|
+
Append object to the end of the list. Set forward to false to append to the start.
|
|
34
|
+
```Python
|
|
35
|
+
doubly_linked_list.append(object: Any, forward: bool = True)
|
|
36
|
+
```
|
|
37
|
+
- clear
|
|
38
|
+
Remove all items from the list.
|
|
39
|
+
```Python
|
|
40
|
+
doubly_linked_list.clear()
|
|
41
|
+
```
|
|
42
|
+
- copy
|
|
43
|
+
Return a shallow copy of the list.
|
|
44
|
+
```Python
|
|
45
|
+
doubly_linked_list.copy()
|
|
46
|
+
```
|
|
47
|
+
- count
|
|
48
|
+
Return number of occurrences of value in the list.
|
|
49
|
+
```Python
|
|
50
|
+
doubly_linked_list.count(value: Any)
|
|
51
|
+
```
|
|
52
|
+
- extend
|
|
53
|
+
Extend list by appending elements from the iterable. Set forward to false to extend from the start.
|
|
54
|
+
```Python
|
|
55
|
+
doubly_linked_list.extend(iterable: Iterable, forward: bool = True)
|
|
56
|
+
```
|
|
57
|
+
- index
|
|
58
|
+
Return first index of value. Raises ValueError if the value is not present.
|
|
59
|
+
```Python
|
|
60
|
+
doubly_linked_list.index(value: Any, start: int, stop: int)
|
|
61
|
+
```
|
|
62
|
+
- insert
|
|
63
|
+
Insert object after index. Set forward to false to insert before index.
|
|
64
|
+
```Python
|
|
65
|
+
doubly_linked_list.insert(object: Any, index: int, forward: bool = True)
|
|
66
|
+
```
|
|
67
|
+
- pop
|
|
68
|
+
Remove and return item at index (default last). Raises IndexError if list is empty or index is out of range.
|
|
69
|
+
```Python
|
|
70
|
+
doubly_linked_list.pop(index: int = -1)
|
|
71
|
+
```
|
|
72
|
+
- remove
|
|
73
|
+
Remove first occurence of value. Raises ValueError if the value is not present.
|
|
74
|
+
```Python
|
|
75
|
+
doubly_linked_list.remove(value: Any)
|
|
76
|
+
```
|
|
77
|
+
- reverse
|
|
78
|
+
Reverse the order of the list.
|
|
79
|
+
```Python
|
|
80
|
+
doubly_linked_list.reverse()
|
|
81
|
+
```
|
|
82
|
+
- sort
|
|
83
|
+
In-place sort in ascending order, equal objects are not swapped. Key can be applied to values and the list will be sorted based on the result of applying the key. Reverse will reverse the sort order.
|
|
84
|
+
```Python
|
|
85
|
+
doubly_linked_list.sort(key: Callable = None, reverse: bool = False)
|
|
86
|
+
```
|
|
87
|
+
The DoublyLinkedList also supports:
|
|
88
|
+
- concatenation with other iterables
|
|
89
|
+
- in-place concatenation with other iterables
|
|
90
|
+
- indexing and assignment by index
|
|
91
|
+
- slicing
|
|
92
|
+
- iterating
|
|
93
|
+
|
|
94
|
+
Things that are not currently supported but might be in the future:
|
|
95
|
+
- assigning slices
|
|
96
|
+
- repeating, i.e list = [1] * 5 create a list with 1 repeated 5 times
|
|
97
|
+
|
|
98
|
+
# License
|
|
99
|
+
Copyright (c) 2025 Joshua A. Morningstar
|
|
100
|
+
|
|
101
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
102
|
+
|
|
103
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
104
|
+
|
|
105
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# py-doubly-linked-list
|
|
2
|
+
## Description
|
|
3
|
+
This library provides an implementation of a doubly linked list for python written with the c api. It is meant to be as close as possible to a regular list in its interface, meaning you can interact with it in almost all of the same ways you interact with a regular python list.
|
|
4
|
+
## Methods
|
|
5
|
+
- append
|
|
6
|
+
Append object to the end of the list. Set forward to false to append to the start.
|
|
7
|
+
```Python
|
|
8
|
+
doubly_linked_list.append(object: Any, forward: bool = True)
|
|
9
|
+
```
|
|
10
|
+
- clear
|
|
11
|
+
Remove all items from the list.
|
|
12
|
+
```Python
|
|
13
|
+
doubly_linked_list.clear()
|
|
14
|
+
```
|
|
15
|
+
- copy
|
|
16
|
+
Return a shallow copy of the list.
|
|
17
|
+
```Python
|
|
18
|
+
doubly_linked_list.copy()
|
|
19
|
+
```
|
|
20
|
+
- count
|
|
21
|
+
Return number of occurrences of value in the list.
|
|
22
|
+
```Python
|
|
23
|
+
doubly_linked_list.count(value: Any)
|
|
24
|
+
```
|
|
25
|
+
- extend
|
|
26
|
+
Extend list by appending elements from the iterable. Set forward to false to extend from the start.
|
|
27
|
+
```Python
|
|
28
|
+
doubly_linked_list.extend(iterable: Iterable, forward: bool = True)
|
|
29
|
+
```
|
|
30
|
+
- index
|
|
31
|
+
Return first index of value. Raises ValueError if the value is not present.
|
|
32
|
+
```Python
|
|
33
|
+
doubly_linked_list.index(value: Any, start: int, stop: int)
|
|
34
|
+
```
|
|
35
|
+
- insert
|
|
36
|
+
Insert object after index. Set forward to false to insert before index.
|
|
37
|
+
```Python
|
|
38
|
+
doubly_linked_list.insert(object: Any, index: int, forward: bool = True)
|
|
39
|
+
```
|
|
40
|
+
- pop
|
|
41
|
+
Remove and return item at index (default last). Raises IndexError if list is empty or index is out of range.
|
|
42
|
+
```Python
|
|
43
|
+
doubly_linked_list.pop(index: int = -1)
|
|
44
|
+
```
|
|
45
|
+
- remove
|
|
46
|
+
Remove first occurence of value. Raises ValueError if the value is not present.
|
|
47
|
+
```Python
|
|
48
|
+
doubly_linked_list.remove(value: Any)
|
|
49
|
+
```
|
|
50
|
+
- reverse
|
|
51
|
+
Reverse the order of the list.
|
|
52
|
+
```Python
|
|
53
|
+
doubly_linked_list.reverse()
|
|
54
|
+
```
|
|
55
|
+
- sort
|
|
56
|
+
In-place sort in ascending order, equal objects are not swapped. Key can be applied to values and the list will be sorted based on the result of applying the key. Reverse will reverse the sort order.
|
|
57
|
+
```Python
|
|
58
|
+
doubly_linked_list.sort(key: Callable = None, reverse: bool = False)
|
|
59
|
+
```
|
|
60
|
+
The DoublyLinkedList also supports:
|
|
61
|
+
- concatenation with other iterables
|
|
62
|
+
- in-place concatenation with other iterables
|
|
63
|
+
- indexing and assignment by index
|
|
64
|
+
- slicing
|
|
65
|
+
- iterating
|
|
66
|
+
|
|
67
|
+
Things that are not currently supported but might be in the future:
|
|
68
|
+
- assigning slices
|
|
69
|
+
- repeating, i.e list = [1] * 5 create a list with 1 repeated 5 times
|
|
70
|
+
|
|
71
|
+
# License
|
|
72
|
+
Copyright (c) 2025 Joshua A. Morningstar
|
|
73
|
+
|
|
74
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
75
|
+
|
|
76
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
77
|
+
|
|
78
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "py-doubly-linked-list"
|
|
7
|
+
version = "0.1.1"
|
|
8
|
+
description = "A library adding doubly linked lists to python."
|
|
9
|
+
readme = "README.MD"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
authors = [ {name = "Joshua Morningstar", email = "joshuam18745@gmail.com"} ]
|
|
12
|
+
maintainers = [ {name = "Joshua Morningstar", email = "joshuam18745@gmail.com"} ]
|
|
13
|
+
license = "MIT"
|
|
14
|
+
license-files = [
|
|
15
|
+
"LICENSE",
|
|
16
|
+
"README.MD"
|
|
17
|
+
]
|
|
18
|
+
classifiers = [
|
|
19
|
+
"Development Status :: 4 - Beta",
|
|
20
|
+
|
|
21
|
+
"Intended Audience :: Developers",
|
|
22
|
+
|
|
23
|
+
"Programming Language :: Python :: 3",
|
|
24
|
+
"Programming Language :: Python :: 3.9",
|
|
25
|
+
"Programming Language :: Python :: 3.10",
|
|
26
|
+
"Programming Language :: Python :: 3.11",
|
|
27
|
+
"Programming Language :: Python :: 3.12",
|
|
28
|
+
"Programming Language :: Python :: 3.13",
|
|
29
|
+
"Programming Language :: Python :: 3.14",
|
|
30
|
+
]
|
|
31
|
+
keywords = ["doubly linked list", "data structure"]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
[project.urls]
|
|
35
|
+
Documentation = "https://github.com/JoshuaM176/PyDoublyLinkedList/blob/main/README.MD"
|
|
36
|
+
Repository = "https://github.com/JoshuaM176/PyDoublyLinkedList"
|
|
37
|
+
Issues = "https://github.com/JoshuaM176/PyDoublyLinkedList/issues"
|
|
38
|
+
Changelog = "https://github.com/JoshuaM176/PyDoublyLinkedList/blob/main/CHANGES"
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
[[tool.setuptools.ext-modules]]
|
|
43
|
+
name = "py_doubly_linked_list.doubly_linked_list"
|
|
44
|
+
sources = ["src/doubly_linked_list.c"]
|
|
@@ -0,0 +1,778 @@
|
|
|
1
|
+
#define PY_SSIZE_T_CLEAN
|
|
2
|
+
#include <Python.h>
|
|
3
|
+
#include <structmember.h>
|
|
4
|
+
#include <string.h>
|
|
5
|
+
#include <stddef.h>
|
|
6
|
+
#include <math.h>
|
|
7
|
+
#if PY_MINOR_VERSION < 10
|
|
8
|
+
#define Py_IsNone(x) Py_Is((x), Py_None) // Define these so that we can use them on older versions
|
|
9
|
+
#define Py_Is(x,y) ((x) == (y))
|
|
10
|
+
static inline PyObject* Py_NewRef(PyObject *obj) {
|
|
11
|
+
Py_INCREF(obj);
|
|
12
|
+
return obj;
|
|
13
|
+
}
|
|
14
|
+
#endif
|
|
15
|
+
// - - - - - DoublyLinkedListNode - - - - - //
|
|
16
|
+
|
|
17
|
+
typedef struct {
|
|
18
|
+
PyObject_HEAD
|
|
19
|
+
PyObject* value;
|
|
20
|
+
PyObject* next;
|
|
21
|
+
PyObject* prev;
|
|
22
|
+
PyObject* key;
|
|
23
|
+
} DLLNode;
|
|
24
|
+
|
|
25
|
+
static PyTypeObject DLLNodeType;
|
|
26
|
+
|
|
27
|
+
// Initalization and Deallocation
|
|
28
|
+
|
|
29
|
+
static void
|
|
30
|
+
DLLNode_dealloc(PyObject *op)
|
|
31
|
+
{
|
|
32
|
+
DLLNode* self = (DLLNode* )op;
|
|
33
|
+
Py_XDECREF(self->next);
|
|
34
|
+
Py_XDECREF(self->value);
|
|
35
|
+
Py_TYPE(self)->tp_free(self);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
static PyObject *
|
|
39
|
+
DLLNode_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|
40
|
+
{
|
|
41
|
+
DLLNode *self;
|
|
42
|
+
self = (DLLNode *) type->tp_alloc(type, 0);
|
|
43
|
+
if (self != NULL) {
|
|
44
|
+
self->value = Py_NewRef(Py_None);
|
|
45
|
+
if (self->value == NULL) {
|
|
46
|
+
Py_DECREF(self);
|
|
47
|
+
return NULL;
|
|
48
|
+
}
|
|
49
|
+
self->next = Py_NewRef(Py_None);
|
|
50
|
+
if (self->next == NULL) {
|
|
51
|
+
Py_DECREF(self);
|
|
52
|
+
return NULL;
|
|
53
|
+
}
|
|
54
|
+
self->prev = Py_None;
|
|
55
|
+
if (self->prev == NULL) {
|
|
56
|
+
Py_DECREF(self);
|
|
57
|
+
return NULL;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return (PyObject *) self;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Getters and Setters
|
|
64
|
+
|
|
65
|
+
static int DLLNode_set(PyObject* op, PyObject* value, void* closure){
|
|
66
|
+
PyErr_SetString(PyExc_TypeError, "Default DLLNode attributes cannot be manually altered");
|
|
67
|
+
return -1;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
static PyObject* DLLNode_value_get(PyObject* op, void* closure){
|
|
71
|
+
DLLNode* self = (DLLNode* )op;
|
|
72
|
+
return Py_NewRef(self->value);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
static PyObject* DLLNode_next_get(PyObject* op, void* closure){
|
|
76
|
+
DLLNode* self = (DLLNode* )op;
|
|
77
|
+
return Py_NewRef(self->next);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
static PyObject* DLLNode_prev_get(PyObject* op, void* closure){
|
|
81
|
+
DLLNode* self = (DLLNode* )op;
|
|
82
|
+
return Py_NewRef(self->prev);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
static PyGetSetDef DLLNode_getsetters[] = {
|
|
86
|
+
{"value", DLLNode_value_get, DLLNode_set,
|
|
87
|
+
"value held by node", NULL},
|
|
88
|
+
{"next", DLLNode_next_get, DLLNode_set,
|
|
89
|
+
"next node", NULL},
|
|
90
|
+
{"prev", DLLNode_prev_get, DLLNode_set,
|
|
91
|
+
"prev node", NULL},
|
|
92
|
+
{NULL}
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// __Methods__
|
|
96
|
+
|
|
97
|
+
static PyObject* DLLNode_str(PyObject* op, PyObject* Py_UNUSED(dummy)){
|
|
98
|
+
DLLNode* self = (DLLNode* )op;
|
|
99
|
+
PyObject* rtn = PyUnicode_FromFormat("%S", self->value); if(!rtn) {return NULL;}
|
|
100
|
+
return rtn;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
//Type Definition
|
|
104
|
+
|
|
105
|
+
static PyTypeObject DLLNodeType = {
|
|
106
|
+
.ob_base = PyVarObject_HEAD_INIT(NULL, 0)
|
|
107
|
+
.tp_name = "py_doubly_linked_list.doubly_linked_list.DLLNode",
|
|
108
|
+
.tp_doc = PyDoc_STR("Node for doubly linked list"),
|
|
109
|
+
.tp_basicsize = sizeof(DLLNode),
|
|
110
|
+
.tp_itemsize = 0,
|
|
111
|
+
.tp_flags = Py_TPFLAGS_DEFAULT,
|
|
112
|
+
.tp_new = DLLNode_new,
|
|
113
|
+
.tp_dealloc = DLLNode_dealloc,
|
|
114
|
+
.tp_getset = DLLNode_getsetters,
|
|
115
|
+
.tp_str = (reprfunc)DLLNode_str
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
static int dllnode_module_exec(PyObject *m)
|
|
119
|
+
{
|
|
120
|
+
if (PyType_Ready(&DLLNodeType) < 0) {
|
|
121
|
+
return -1;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return 0;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// - - - - - DoublyLinkedList - - - - - //
|
|
128
|
+
|
|
129
|
+
typedef struct {
|
|
130
|
+
PyObject_HEAD
|
|
131
|
+
PyObject* head;
|
|
132
|
+
PyObject* tail;
|
|
133
|
+
PyObject* cursor;
|
|
134
|
+
Py_ssize_t cursor_pos;
|
|
135
|
+
Py_ssize_t length;
|
|
136
|
+
} DoublyLinkedList;
|
|
137
|
+
|
|
138
|
+
static PyTypeObject DoublyLinkedListType;
|
|
139
|
+
|
|
140
|
+
// Define internal helper methods
|
|
141
|
+
|
|
142
|
+
static int DoublyLinkedList_locate(PyObject*, Py_ssize_t);
|
|
143
|
+
static int DoublyLinkedList_cursor_insert(PyObject*, PyObject*, int);
|
|
144
|
+
static int DoublyLinkedList_append_iterator(PyObject*, PyObject*, int);
|
|
145
|
+
static int DoublyLinkedList_cursor_delete(PyObject*);
|
|
146
|
+
|
|
147
|
+
// Initialization and deallocation
|
|
148
|
+
|
|
149
|
+
static void
|
|
150
|
+
DoublyLinkedList_dealloc(PyObject *op)
|
|
151
|
+
{
|
|
152
|
+
DoublyLinkedList* self = (DoublyLinkedList* )op;
|
|
153
|
+
Py_XDECREF(self->head);
|
|
154
|
+
Py_XDECREF(self->tail);
|
|
155
|
+
Py_XDECREF(self->cursor);
|
|
156
|
+
Py_TYPE(self)->tp_free(self);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
static PyObject *
|
|
160
|
+
DoublyLinkedList_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|
161
|
+
{
|
|
162
|
+
DoublyLinkedList *self;
|
|
163
|
+
self = (DoublyLinkedList *) type->tp_alloc(type, 0);
|
|
164
|
+
if (self != NULL) {
|
|
165
|
+
self->head = Py_NewRef(Py_None);
|
|
166
|
+
if (self->head == NULL) {
|
|
167
|
+
Py_DECREF(self);
|
|
168
|
+
return NULL;
|
|
169
|
+
}
|
|
170
|
+
self->tail = Py_NewRef(Py_None);
|
|
171
|
+
if (self->tail == NULL) {
|
|
172
|
+
Py_DECREF(self);
|
|
173
|
+
return NULL;
|
|
174
|
+
}
|
|
175
|
+
self->cursor = Py_NewRef(Py_None);
|
|
176
|
+
if (self->cursor == NULL) {
|
|
177
|
+
Py_DECREF(self);
|
|
178
|
+
return NULL;
|
|
179
|
+
}
|
|
180
|
+
self->cursor_pos = 0;
|
|
181
|
+
self->length = 0;
|
|
182
|
+
}
|
|
183
|
+
return (PyObject *) self;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
static int
|
|
187
|
+
DoublyLinkedList_init(PyObject* op, PyObject *args)
|
|
188
|
+
{
|
|
189
|
+
DoublyLinkedList* self = (DoublyLinkedList* )op;
|
|
190
|
+
PyObject* iterable = NULL;
|
|
191
|
+
if (!PyArg_ParseTuple(args, "|O", &iterable))
|
|
192
|
+
return -1;
|
|
193
|
+
if(iterable){
|
|
194
|
+
if(DoublyLinkedList_append_iterator((PyObject*)self, iterable, 1)) {return -1;}
|
|
195
|
+
}
|
|
196
|
+
return 0;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Methods
|
|
200
|
+
|
|
201
|
+
static PyObject* DoublyLinkedList_insert(PyObject* op, PyObject* args, PyObject* kwds){
|
|
202
|
+
DoublyLinkedList* self = (DoublyLinkedList*) op;
|
|
203
|
+
static char* kwlist[] = {"object", "index", "forward", NULL};
|
|
204
|
+
PyObject* object = NULL;
|
|
205
|
+
Py_ssize_t index;
|
|
206
|
+
int forward = 1;
|
|
207
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "On|i", kwlist,
|
|
208
|
+
&object, &index, &forward))
|
|
209
|
+
return NULL;
|
|
210
|
+
if(DoublyLinkedList_locate((PyObject*)self, index)) {return NULL;}
|
|
211
|
+
if(DoublyLinkedList_cursor_insert((PyObject*)self, object, forward)) {return NULL;}
|
|
212
|
+
return Py_NewRef(Py_None);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
static PyObject* DoublyLinkedList_append(PyObject* op, PyObject* args, PyObject* kwds){
|
|
216
|
+
DoublyLinkedList* self = (DoublyLinkedList*) op;
|
|
217
|
+
static char* kwlist[] = {"object", "forward", NULL};
|
|
218
|
+
PyObject* object = NULL;
|
|
219
|
+
int forward = 1;
|
|
220
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i", kwlist,
|
|
221
|
+
&object, &forward))
|
|
222
|
+
return NULL;
|
|
223
|
+
if(forward) {Py_SETREF(self->cursor, Py_NewRef(self->tail));}
|
|
224
|
+
else {Py_SETREF(self->cursor, Py_NewRef(self->head));}
|
|
225
|
+
if(DoublyLinkedList_cursor_insert((PyObject*)self, object, forward)) {return NULL;}
|
|
226
|
+
return Py_NewRef(Py_None);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
static PyObject* DoublyLinkedList_index(PyObject* op, PyObject* args, PyObject* kwds){
|
|
230
|
+
DoublyLinkedList* self = (DoublyLinkedList* )op;
|
|
231
|
+
static char* kwlist[] = {"value", "start", "stop", NULL};
|
|
232
|
+
PyObject* value; Py_ssize_t start = 0; Py_ssize_t stop = self->length;
|
|
233
|
+
DLLNode* cursor;
|
|
234
|
+
if(!PyArg_ParseTupleAndKeywords(args, kwds, "O|nn", kwlist, &value, &start, &stop)) {return NULL;}
|
|
235
|
+
for(Py_ssize_t i=start; i<stop; i++){
|
|
236
|
+
if(DoublyLinkedList_locate((PyObject*)self, i)) {return NULL;}
|
|
237
|
+
cursor = (DLLNode*)self->cursor;
|
|
238
|
+
if(cursor->value == value) {
|
|
239
|
+
PyObject* rtn = PyLong_FromSsize_t(i); if(!rtn) {return NULL;}
|
|
240
|
+
return rtn;}
|
|
241
|
+
}
|
|
242
|
+
PyObject* err_value = PyObject_Str(value); if(!err_value) {return NULL;}
|
|
243
|
+
PyObject* err_format = PyUnicode_FromFormat("%U not in list", err_value); if(!err_format) {return NULL;}
|
|
244
|
+
PyErr_SetString(PyExc_ValueError, PyUnicode_AsUTF8(err_format));
|
|
245
|
+
Py_DECREF(err_value); Py_DECREF(err_format);
|
|
246
|
+
return NULL;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
static PyObject* DoublyLinkedList_pop(PyObject* op, PyObject* args, PyObject* kwds){
|
|
250
|
+
DoublyLinkedList* self = (DoublyLinkedList* ) op;
|
|
251
|
+
static char* kwlist[] = {"index", NULL};
|
|
252
|
+
Py_ssize_t index = self->length-1;
|
|
253
|
+
if(!PyArg_ParseTupleAndKeywords(args, kwds, "|n", kwlist, &index)) {return NULL;}
|
|
254
|
+
if(DoublyLinkedList_locate((PyObject*)self, index)) {return NULL;}
|
|
255
|
+
DLLNode* cursor = (DLLNode* )self->cursor;
|
|
256
|
+
PyObject* popped = Py_NewRef(cursor->value);
|
|
257
|
+
if(DoublyLinkedList_cursor_delete((PyObject*)self)) {return NULL;}
|
|
258
|
+
return popped;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
static PyObject* DoublyLinkedList_remove(PyObject* op, PyObject* args, PyObject* kwds){
|
|
262
|
+
DoublyLinkedList* self = (DoublyLinkedList* )op;
|
|
263
|
+
if(!DoublyLinkedList_index((PyObject*)self, args, kwds)) {return NULL;}
|
|
264
|
+
if(DoublyLinkedList_cursor_delete((PyObject*)self)) {return NULL;}
|
|
265
|
+
return Py_NewRef(Py_None);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
static PyObject* DoublyLinkedList_extend(PyObject* op, PyObject* args, PyObject* kwds){
|
|
269
|
+
DoublyLinkedList* self = (DoublyLinkedList* )op;
|
|
270
|
+
static char* kwlist[] = {"iterable", "forward", NULL};
|
|
271
|
+
PyObject* iterable;
|
|
272
|
+
int forward = 1;
|
|
273
|
+
if(!PyArg_ParseTupleAndKeywords(args, kwds, "O|i", kwlist, &iterable, &forward)) {return NULL;}
|
|
274
|
+
if(DoublyLinkedList_append_iterator((PyObject*)self, iterable, forward)) {return NULL;}
|
|
275
|
+
return Py_NewRef(Py_None);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
static PyObject* DoublyLinkedList_copy(PyObject* op){
|
|
279
|
+
DoublyLinkedList* copy = (DoublyLinkedList*)DoublyLinkedList_new(&DoublyLinkedListType, NULL, NULL); if(!copy) {return NULL;}
|
|
280
|
+
if(DoublyLinkedList_append_iterator((PyObject*)copy, op, 1)) {return NULL;}
|
|
281
|
+
return (PyObject*)copy;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
static PyObject* DoublyLinkedList_reverse(PyObject* op){
|
|
285
|
+
DoublyLinkedList* self = (DoublyLinkedList* )op;
|
|
286
|
+
Py_ssize_t middle = self->length / 2;
|
|
287
|
+
PyObject* temp;
|
|
288
|
+
DLLNode* node1 = (DLLNode*)self->head;
|
|
289
|
+
DLLNode* node2 = (DLLNode*)self->tail;
|
|
290
|
+
for(Py_ssize_t i = 0; i < middle; i++){
|
|
291
|
+
temp = (PyObject*)node1->value;
|
|
292
|
+
node1->value = node2->value;
|
|
293
|
+
node2->value = (PyObject*)temp;
|
|
294
|
+
node1 = (DLLNode*)node1->next;
|
|
295
|
+
node2 = (DLLNode*)node2->prev;
|
|
296
|
+
}
|
|
297
|
+
return Py_NewRef(Py_None);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
static PyObject* DoublyLinkedList_count(PyObject* op, PyObject* args, PyObject* kwds) {
|
|
301
|
+
DoublyLinkedList* self = (DoublyLinkedList* )op;
|
|
302
|
+
static char* kwlist[] = {"value", NULL};
|
|
303
|
+
PyObject* value;
|
|
304
|
+
if(!PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist, &value)) {return NULL;}
|
|
305
|
+
DLLNode* temp = (DLLNode*)self->head;
|
|
306
|
+
Py_ssize_t count = 0;
|
|
307
|
+
for(Py_ssize_t i = 0; i<self->length; i++){
|
|
308
|
+
if(temp->value==value) {count += 1;}
|
|
309
|
+
temp = (DLLNode*)temp->next;
|
|
310
|
+
}
|
|
311
|
+
PyObject* rtn = PyLong_FromSsize_t(count); if(!rtn) {return NULL;}
|
|
312
|
+
return rtn;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
static PyObject* DoublyLinkedList_clear_method(PyObject* op){
|
|
316
|
+
DoublyLinkedList* self = (DoublyLinkedList* )op;
|
|
317
|
+
if(self->length == 0){
|
|
318
|
+
return Py_NewRef(Py_None);
|
|
319
|
+
}
|
|
320
|
+
Py_DECREF(self->cursor); Py_DECREF(self->head); Py_DECREF(self->tail);
|
|
321
|
+
self->head = Py_NewRef(Py_None); self->tail = Py_NewRef(Py_None); self->cursor = Py_NewRef(Py_None);
|
|
322
|
+
self->length = 0; self->cursor_pos = 0;
|
|
323
|
+
return Py_NewRef(Py_None);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
//Helper method for sort, swaps two nodes that are next to each
|
|
327
|
+
static void swap(PyObject* op, PyObject* node1, PyObject* node2) {
|
|
328
|
+
DoublyLinkedList* self = (DoublyLinkedList*)op;
|
|
329
|
+
DLLNode* temp1 = (DLLNode*)node1;
|
|
330
|
+
DLLNode* temp2 = (DLLNode*)node2;
|
|
331
|
+
if(Py_IsNone(temp1->prev)) {
|
|
332
|
+
self->head = (PyObject*)temp2;
|
|
333
|
+
}
|
|
334
|
+
else{
|
|
335
|
+
((DLLNode*)(temp1->prev))->next = (PyObject*)temp2;
|
|
336
|
+
}
|
|
337
|
+
if(Py_IsNone(temp2->next)){
|
|
338
|
+
Py_SETREF(self->tail, (PyObject*)temp1);
|
|
339
|
+
}
|
|
340
|
+
else{
|
|
341
|
+
((DLLNode*)(temp2->next))->prev = (PyObject*)temp1;
|
|
342
|
+
}
|
|
343
|
+
temp1->next = temp2->next;
|
|
344
|
+
temp2->prev = temp1->prev;
|
|
345
|
+
temp2->next = (PyObject*)temp1;
|
|
346
|
+
temp1->prev = (PyObject*)temp2;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
static PyObject* DoublyLinkedList_sort(PyObject* op, PyObject* args, PyObject* kwds) {
|
|
350
|
+
DoublyLinkedList* self = (DoublyLinkedList* )op;
|
|
351
|
+
char* kwlist[] = {"key", "reverse", NULL};
|
|
352
|
+
PyObject* key = NULL; int reverse = 0;
|
|
353
|
+
if(!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi", kwlist, &key, &reverse)) { return NULL; }
|
|
354
|
+
int operator;
|
|
355
|
+
if(reverse) { operator = Py_GT; } else { operator = Py_LT; }
|
|
356
|
+
DLLNode* temp = (DLLNode*)self->head;
|
|
357
|
+
DLLNode* next = (DLLNode*)temp->next;
|
|
358
|
+
DLLNode* prev;
|
|
359
|
+
int comparison;
|
|
360
|
+
|
|
361
|
+
if(key) {
|
|
362
|
+
if(!PyCallable_Check(key)) {
|
|
363
|
+
PyErr_SetString(PyExc_TypeError, "Key must be a callable"); return NULL; }
|
|
364
|
+
for(int i = 0; i < self->length; i++) {
|
|
365
|
+
PyObject* value_key = PyObject_CallOneArg(key, temp->value);
|
|
366
|
+
if(!value_key) { return NULL; }
|
|
367
|
+
temp->key = value_key;
|
|
368
|
+
temp = (DLLNode*)temp->next;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
temp = (DLLNode*)self->head;
|
|
372
|
+
for(int i = 1; i < self->length; i++) {
|
|
373
|
+
temp = next;
|
|
374
|
+
next = (DLLNode*)temp->next;
|
|
375
|
+
for(int j = i; j >= 1; j--) {
|
|
376
|
+
prev = (DLLNode*)temp->prev;
|
|
377
|
+
comparison = PyObject_RichCompareBool(temp->key, prev->key, operator);
|
|
378
|
+
if(comparison == -1) { return NULL; }
|
|
379
|
+
if(comparison){swap((PyObject*)self, (PyObject*)prev, (PyObject*)temp);}
|
|
380
|
+
else { break; }
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
temp = (DLLNode*)self->head;
|
|
384
|
+
for(int i =0; i < self->length; i++) {
|
|
385
|
+
Py_DECREF(temp->key);
|
|
386
|
+
temp = (DLLNode*)temp->next;
|
|
387
|
+
}
|
|
388
|
+
self->cursor_pos = 0;
|
|
389
|
+
Py_SETREF(self->cursor, Py_NewRef(self->head));
|
|
390
|
+
return Py_NewRef(Py_None);
|
|
391
|
+
}
|
|
392
|
+
else{
|
|
393
|
+
for(int i = 1; i < self->length; i++) {
|
|
394
|
+
temp = next;
|
|
395
|
+
next = (DLLNode*)temp->next;
|
|
396
|
+
for(int j = i; j >= 1; j--) {
|
|
397
|
+
prev = (DLLNode*)temp->prev;
|
|
398
|
+
comparison = PyObject_RichCompareBool(temp->value, prev->value, operator);
|
|
399
|
+
if(comparison == -1) { return NULL; }
|
|
400
|
+
if(comparison){swap((PyObject*)self, (PyObject*)prev, (PyObject*)temp);}
|
|
401
|
+
else { break; }
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
self->cursor_pos = 0;
|
|
405
|
+
Py_SETREF(self->cursor, Py_NewRef(self->head));
|
|
406
|
+
return Py_NewRef(Py_None);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// Internal Methods
|
|
411
|
+
|
|
412
|
+
// Takes in DoublyLinkedList and index, locates node at that index and sets cursor to it
|
|
413
|
+
static int DoublyLinkedList_locate(PyObject* op, Py_ssize_t index){
|
|
414
|
+
DoublyLinkedList* self = (DoublyLinkedList* )op;
|
|
415
|
+
if(index < 0) {index = self->length + index;}
|
|
416
|
+
if(index >= self->length || index < 0){
|
|
417
|
+
PyErr_SetString(PyExc_IndexError, "Index out of bounds");
|
|
418
|
+
return -1;
|
|
419
|
+
}
|
|
420
|
+
DLLNode* search_node = (DLLNode*)self->cursor;
|
|
421
|
+
Py_ssize_t search_distance = index-self->cursor_pos;
|
|
422
|
+
const Py_ssize_t head_distance = index;
|
|
423
|
+
const Py_ssize_t tail_distance = index-(self->length-1);
|
|
424
|
+
if(abs(head_distance) < abs(search_distance)){
|
|
425
|
+
search_node = (DLLNode*)self->head;
|
|
426
|
+
search_distance = head_distance;
|
|
427
|
+
}
|
|
428
|
+
else if(abs(tail_distance) < abs(search_distance)){
|
|
429
|
+
search_node = (DLLNode*)self->tail;
|
|
430
|
+
search_distance = tail_distance;
|
|
431
|
+
}
|
|
432
|
+
if(search_distance>0){
|
|
433
|
+
for(Py_ssize_t i = 0; i<search_distance; i++){
|
|
434
|
+
search_node = (DLLNode*)search_node->next;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
else if(search_distance<0){
|
|
438
|
+
for(Py_ssize_t i=0; i>search_distance; i--){
|
|
439
|
+
search_node = (DLLNode*)search_node->prev;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
Py_SETREF(self->cursor, Py_NewRef((PyObject*)search_node));
|
|
443
|
+
self->cursor_pos = index;
|
|
444
|
+
return 0;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// Create a new node with value and inserts it forwards or backwards and sets cursor to it.
|
|
448
|
+
// We don't create a reference to the prev node since we can guarentee that as long as it is in the list it will have a reference from its prev anyway.
|
|
449
|
+
// This avoids cycles
|
|
450
|
+
static int DoublyLinkedList_cursor_insert(PyObject* op, PyObject* object, int forward) {
|
|
451
|
+
DoublyLinkedList* self = (DoublyLinkedList*)op;
|
|
452
|
+
self->length += 1;
|
|
453
|
+
DLLNode* node = (DLLNode*)DLLNode_new(&DLLNodeType, NULL, NULL); if(!node) {return -1;}
|
|
454
|
+
Py_SETREF(node->value, Py_NewRef(object));
|
|
455
|
+
if(Py_IsNone(self->cursor)){
|
|
456
|
+
Py_SETREF(self->head, (PyObject*)node);
|
|
457
|
+
Py_SETREF(self->tail, Py_NewRef((PyObject*)node));
|
|
458
|
+
Py_SETREF(self->cursor, Py_NewRef((PyObject*)node));
|
|
459
|
+
}
|
|
460
|
+
else{
|
|
461
|
+
DLLNode* cursor = (DLLNode*)self->cursor;
|
|
462
|
+
if(forward){
|
|
463
|
+
self->cursor_pos += 1;
|
|
464
|
+
if(Py_IsNone(cursor->next)){
|
|
465
|
+
node->prev = (PyObject*)cursor;
|
|
466
|
+
Py_SETREF(self->tail, Py_NewRef((PyObject*)node));
|
|
467
|
+
Py_SETREF(cursor->next, (PyObject*)node);
|
|
468
|
+
|
|
469
|
+
}
|
|
470
|
+
else{
|
|
471
|
+
DLLNode* temp = (DLLNode*)cursor->next;
|
|
472
|
+
node->prev = (PyObject*)cursor;
|
|
473
|
+
temp->prev = (PyObject*)node;
|
|
474
|
+
Py_SETREF(node->next, Py_NewRef((PyObject*)temp));
|
|
475
|
+
Py_SETREF(cursor->next, Py_NewRef((PyObject*)node));
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
else{
|
|
479
|
+
if(Py_IsNone(cursor->prev)){
|
|
480
|
+
cursor->prev = (PyObject*)node;
|
|
481
|
+
Py_SETREF(self->head, Py_NewRef((PyObject*)node));
|
|
482
|
+
Py_SETREF(node->next, Py_NewRef((PyObject*)cursor));
|
|
483
|
+
}
|
|
484
|
+
else{
|
|
485
|
+
DLLNode* temp = (DLLNode*)cursor->prev;
|
|
486
|
+
node->prev = (PyObject*)temp;
|
|
487
|
+
cursor->prev = (PyObject*)node;
|
|
488
|
+
Py_SETREF(temp->next, (PyObject*)node);
|
|
489
|
+
Py_SETREF(node->next, Py_NewRef((PyObject*)cursor));
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
Py_SETREF(self->cursor, Py_NewRef((PyObject*)node));
|
|
494
|
+
return 0;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
static int DoublyLinkedList_cursor_delete(PyObject* op){
|
|
498
|
+
DoublyLinkedList* self = (DoublyLinkedList* )op;
|
|
499
|
+
self->length -= 1;
|
|
500
|
+
DLLNode* cursor = (DLLNode*)self->cursor;
|
|
501
|
+
if(Py_IsNone(cursor->next)){
|
|
502
|
+
if(Py_IsNone(cursor->prev)){
|
|
503
|
+
Py_SETREF(self->head, Py_NewRef(Py_None));
|
|
504
|
+
Py_SETREF(self->tail, Py_NewRef(Py_None));
|
|
505
|
+
Py_SETREF(self->cursor, Py_NewRef(Py_None));
|
|
506
|
+
}
|
|
507
|
+
else{
|
|
508
|
+
Py_SETREF(self->tail, Py_NewRef(cursor->prev));
|
|
509
|
+
Py_SETREF(self->cursor, Py_NewRef(cursor->prev));
|
|
510
|
+
Py_SETREF(((DLLNode*)(cursor->prev))->next, Py_NewRef(cursor->next));
|
|
511
|
+
self->cursor_pos-=1;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
else{
|
|
515
|
+
((DLLNode*)(cursor->next))->prev = cursor->prev;
|
|
516
|
+
Py_SETREF(self->cursor, Py_NewRef(cursor->next));
|
|
517
|
+
if(Py_IsNone(cursor->prev)){
|
|
518
|
+
Py_SETREF(self->head, Py_NewRef(cursor->next));
|
|
519
|
+
}
|
|
520
|
+
else{
|
|
521
|
+
Py_SETREF(((DLLNode*)(cursor->prev))->next, Py_NewRef(cursor->next));
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
return 0;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
static int DoublyLinkedList_append_iterator(PyObject* op, PyObject* iterable, int forward){
|
|
528
|
+
DoublyLinkedList* self = (DoublyLinkedList* )op;
|
|
529
|
+
PyObject* iterator = PyObject_GetIter(iterable);
|
|
530
|
+
if(!iterator){
|
|
531
|
+
return -1;
|
|
532
|
+
}
|
|
533
|
+
if(forward) {Py_SETREF(self->cursor, Py_NewRef(self->tail));}
|
|
534
|
+
else {Py_SETREF(self->cursor, Py_NewRef(self->head));}
|
|
535
|
+
PyObject* item;
|
|
536
|
+
while((item = PyIter_Next(iterator)) != NULL){
|
|
537
|
+
if(DoublyLinkedList_cursor_insert((PyObject*)self, item, forward)) {return -1;}
|
|
538
|
+
Py_DECREF(item);
|
|
539
|
+
}
|
|
540
|
+
if(PyErr_Occurred()){
|
|
541
|
+
Py_XDECREF(iterator);
|
|
542
|
+
return -1;
|
|
543
|
+
|
|
544
|
+
}
|
|
545
|
+
Py_XDECREF(iterator);
|
|
546
|
+
return 0;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
// Mapping Methods
|
|
550
|
+
|
|
551
|
+
static PyObject* DoublyLinkedList_subscript(PyObject* op, PyObject* slice){
|
|
552
|
+
DoublyLinkedList* self = (DoublyLinkedList* )op;
|
|
553
|
+
if(PySlice_Check(slice)) {
|
|
554
|
+
Py_ssize_t start, stop, step;
|
|
555
|
+
DoublyLinkedList* list_slice = (DoublyLinkedList*)DoublyLinkedList_new(&DoublyLinkedListType, NULL, NULL); if(!list_slice) {return NULL;}
|
|
556
|
+
DLLNode* temp;
|
|
557
|
+
if (PySlice_Unpack(slice, &start, &stop, &step) == -1) {return NULL;}
|
|
558
|
+
if(start < 0) {start = self->length + start;}
|
|
559
|
+
if(stop < 0) {stop = self->length + stop;} if(stop > self->length) {stop = self->length;}
|
|
560
|
+
if(step > 0) {
|
|
561
|
+
for(Py_ssize_t i = start; i < stop; i+=step){
|
|
562
|
+
if(DoublyLinkedList_locate((PyObject*)self, i)) {return NULL;}
|
|
563
|
+
temp = (DLLNode*)self->cursor;
|
|
564
|
+
if(DoublyLinkedList_cursor_insert((PyObject*)list_slice, temp->value, 1)) {return NULL;}
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
if(step < 0) {
|
|
568
|
+
for(Py_ssize_t i = start; i > stop; i+=step){
|
|
569
|
+
if(DoublyLinkedList_locate((PyObject*)self, i)) {return NULL;}
|
|
570
|
+
temp = (DLLNode*)self->cursor;
|
|
571
|
+
if(DoublyLinkedList_cursor_insert((PyObject*)list_slice, temp->value, 1)) {return NULL;}
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
return (PyObject*)list_slice;
|
|
575
|
+
}
|
|
576
|
+
else if(PyLong_Check(slice)) {
|
|
577
|
+
Py_ssize_t index = PyLong_AsSsize_t(slice); if(index == -1 && PyErr_Occurred()) {return NULL;}
|
|
578
|
+
if(DoublyLinkedList_locate((PyObject*)self, index)) {return NULL;}
|
|
579
|
+
DLLNode* cursor = (DLLNode*)self->cursor;
|
|
580
|
+
return Py_NewRef(cursor->value);
|
|
581
|
+
}
|
|
582
|
+
else {PyErr_SetString(PyExc_TypeError, "Index must be an integer or slice"); return NULL;}
|
|
583
|
+
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
// Sequence Methods
|
|
587
|
+
|
|
588
|
+
static Py_ssize_t DoublyLinkedList_len(PyObject* op, PyObject* args, PyObject* kwds){
|
|
589
|
+
DoublyLinkedList* self = (DoublyLinkedList*)op;
|
|
590
|
+
return self->length;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
static PyObject* DoublyLinkedList_item(PyObject* op, Py_ssize_t index){
|
|
594
|
+
DoublyLinkedList* self = (DoublyLinkedList* )op;
|
|
595
|
+
if(DoublyLinkedList_locate((PyObject*)self, index)) {return NULL;}
|
|
596
|
+
DLLNode* cursor = (DLLNode*)self->cursor;
|
|
597
|
+
return Py_NewRef(cursor->value);
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
static int DoublyLinkedList_ass_item(PyObject* op, Py_ssize_t index, PyObject* value) {
|
|
601
|
+
DoublyLinkedList* self = (DoublyLinkedList*)op;
|
|
602
|
+
if(DoublyLinkedList_locate((PyObject*)self, index)) {return -1;}
|
|
603
|
+
if(!value){
|
|
604
|
+
if(DoublyLinkedList_cursor_delete((PyObject*)self)) {return -1;}
|
|
605
|
+
return 0;
|
|
606
|
+
}
|
|
607
|
+
DLLNode* cursor = (DLLNode*)self->cursor;
|
|
608
|
+
Py_SETREF(cursor->value, Py_NewRef(value));
|
|
609
|
+
return 0;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
static PyObject* DoublyLinkedList_concat(PyObject* op, PyObject* concat){
|
|
613
|
+
PyObject* new_list = DoublyLinkedList_new(&DoublyLinkedListType, NULL, NULL); if(new_list == NULL) {return NULL;}
|
|
614
|
+
if(DoublyLinkedList_append_iterator(new_list, op, 1)) {return NULL;}
|
|
615
|
+
if(DoublyLinkedList_append_iterator(new_list, concat, 1)) {return NULL;}
|
|
616
|
+
return new_list;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
static PyObject* DoublyLinkedList_inplace_concat(PyObject* op, PyObject* concat){
|
|
620
|
+
if(DoublyLinkedList_append_iterator(op, concat, 1)) {return NULL;}
|
|
621
|
+
return Py_NewRef(op);
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
static int DoublyLinkedList_contains(PyObject* op, PyObject* value){
|
|
625
|
+
DoublyLinkedList* self = (DoublyLinkedList*)op;
|
|
626
|
+
DLLNode* temp = (DLLNode*)self->head;
|
|
627
|
+
for(Py_ssize_t i = 0; i<self->length; i++){
|
|
628
|
+
if(temp->value==value) {return 1;}
|
|
629
|
+
temp = (DLLNode*)temp->next;
|
|
630
|
+
}
|
|
631
|
+
return 0;
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
// __Methods__
|
|
635
|
+
|
|
636
|
+
static PyObject* DoublyLinkedList_str(PyObject* op, PyObject* Py_UNUSED(dummy)){
|
|
637
|
+
DoublyLinkedList* self = (DoublyLinkedList*)op;
|
|
638
|
+
if(self->length == 0) {return PyUnicode_FromString("[]");}
|
|
639
|
+
PyObject* string = PyUnicode_FromString("["); if(!string) {return NULL;}
|
|
640
|
+
PyObject* new_string;
|
|
641
|
+
DLLNode* temp = (DLLNode*)self->head;
|
|
642
|
+
for(Py_ssize_t i = 1; i<self->length; i++){
|
|
643
|
+
PyObject* node_str = DLLNode_str((PyObject*)temp, NULL); if(!node_str) {return NULL;}
|
|
644
|
+
PyObject* format_node_str = PyUnicode_FromFormat("%U, ", node_str); if(!format_node_str) {return NULL;}
|
|
645
|
+
new_string = PyUnicode_Concat(string, format_node_str); if(!new_string) {return NULL;}
|
|
646
|
+
Py_DECREF(node_str); Py_DECREF(format_node_str); Py_DECREF(string);
|
|
647
|
+
string = new_string;
|
|
648
|
+
temp = (DLLNode*)temp->next;
|
|
649
|
+
}
|
|
650
|
+
new_string = PyUnicode_Concat(string, PyUnicode_FromFormat("%U]", DLLNode_str((PyObject*)temp, NULL)));
|
|
651
|
+
Py_DECREF(string);
|
|
652
|
+
string = new_string;
|
|
653
|
+
return string;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
static PyMethodDef DoublyLinkedList_methods[] = {
|
|
657
|
+
{"append", (PyCFunction)DoublyLinkedList_append, METH_VARARGS|METH_KEYWORDS,
|
|
658
|
+
"Append object to the end of the list. Set forward to false to append to the start."},
|
|
659
|
+
{"clear", (PyCFunction)DoublyLinkedList_clear_method, METH_NOARGS,
|
|
660
|
+
"Remove all items from the list."},
|
|
661
|
+
{"copy", (PyCFunction)DoublyLinkedList_copy, METH_NOARGS,
|
|
662
|
+
"Return a shallow copy of the list."},
|
|
663
|
+
{"count", (PyCFunction)DoublyLinkedList_count, METH_VARARGS|METH_KEYWORDS,
|
|
664
|
+
"Return number of occurrences of value in the list."},
|
|
665
|
+
{"extend", (PyCFunction)DoublyLinkedList_extend, METH_VARARGS|METH_KEYWORDS,
|
|
666
|
+
"Extend list by appending elements from the iterable. Set forward to false to extend from the start."},
|
|
667
|
+
{"index", (PyCFunction)DoublyLinkedList_index, METH_VARARGS|METH_KEYWORDS,
|
|
668
|
+
"Return first index of value.\nRaises ValueError if the value is not present."},
|
|
669
|
+
{"insert", (PyCFunction)DoublyLinkedList_insert, METH_VARARGS|METH_KEYWORDS,
|
|
670
|
+
"Insert object after index. Set forward to false to insert before index."},
|
|
671
|
+
{"pop", (PyCFunction)DoublyLinkedList_pop, METH_VARARGS|METH_KEYWORDS,
|
|
672
|
+
"Remove and return item at index (default last).\nRaises IndexError if list is empty or index is out of range."},
|
|
673
|
+
{"remove", (PyCFunction)DoublyLinkedList_remove, METH_VARARGS|METH_KEYWORDS,
|
|
674
|
+
"Remove first occurence of value.\nRaises ValueError if the value is not present."},
|
|
675
|
+
{"reverse", (PyCFunction)DoublyLinkedList_reverse, METH_NOARGS,
|
|
676
|
+
"Reverse the order of the list."},
|
|
677
|
+
{"sort", (PyCFunction)DoublyLinkedList_sort, METH_VARARGS|METH_KEYWORDS,
|
|
678
|
+
"In-place sort in ascending order, equal objects are not swapped. Key can be applied to values and the list will be sorted based on the result of applying the key. Reverse will reverse the sort order."},
|
|
679
|
+
{NULL, NULL, 0, NULL}
|
|
680
|
+
};
|
|
681
|
+
|
|
682
|
+
static PyMappingMethods DoublyLinkedList_map = {
|
|
683
|
+
.mp_subscript = DoublyLinkedList_subscript
|
|
684
|
+
};
|
|
685
|
+
|
|
686
|
+
static PySequenceMethods DoublyLinkedList_sequence = {
|
|
687
|
+
.sq_length = (lenfunc)DoublyLinkedList_len,
|
|
688
|
+
.sq_item = DoublyLinkedList_item,
|
|
689
|
+
.sq_ass_item = DoublyLinkedList_ass_item,
|
|
690
|
+
.sq_concat = DoublyLinkedList_concat,
|
|
691
|
+
.sq_inplace_concat = DoublyLinkedList_inplace_concat,
|
|
692
|
+
.sq_contains = DoublyLinkedList_contains
|
|
693
|
+
};
|
|
694
|
+
|
|
695
|
+
//Member Definition
|
|
696
|
+
|
|
697
|
+
#ifdef DEV
|
|
698
|
+
|
|
699
|
+
static PyMemberDef DoublyLinkedList_members[] = {
|
|
700
|
+
{"head", T_OBJECT_EX, offsetof(DoublyLinkedList, head), 0,
|
|
701
|
+
"Head node."},
|
|
702
|
+
{"tail", T_OBJECT_EX, offsetof(DoublyLinkedList, tail), 0,
|
|
703
|
+
"Tail node."},
|
|
704
|
+
{"cursor", T_OBJECT_EX, offsetof(DoublyLinkedList, cursor), 0,
|
|
705
|
+
"Cursor tracking most recently accessed node."},
|
|
706
|
+
{"cursor_pos", T_INT, offsetof(DoublyLinkedList, cursor_pos), 0,
|
|
707
|
+
"Index of cursor"},
|
|
708
|
+
{"length", T_INT, offsetof(DoublyLinkedList, length), 0,
|
|
709
|
+
"Length of list"},
|
|
710
|
+
{NULL}
|
|
711
|
+
};
|
|
712
|
+
|
|
713
|
+
#else
|
|
714
|
+
|
|
715
|
+
static PyMemberDef DoublyLinkedList_members[] = {{NULL}};
|
|
716
|
+
|
|
717
|
+
#endif
|
|
718
|
+
|
|
719
|
+
// Type Definition
|
|
720
|
+
|
|
721
|
+
static PyTypeObject DoublyLinkedListType = {
|
|
722
|
+
.ob_base = PyVarObject_HEAD_INIT(NULL, 0)
|
|
723
|
+
.tp_name = "py_doubly_linked_list.doubly_linked_list.DoublyLinkedList",
|
|
724
|
+
.tp_doc = PyDoc_STR("Node for doubly linked list"),
|
|
725
|
+
.tp_basicsize = sizeof(DoublyLinkedList),
|
|
726
|
+
.tp_itemsize = 0,
|
|
727
|
+
.tp_flags = Py_TPFLAGS_DEFAULT,
|
|
728
|
+
.tp_new = (newfunc)DoublyLinkedList_new,
|
|
729
|
+
.tp_init = (initproc)DoublyLinkedList_init,
|
|
730
|
+
.tp_dealloc = (destructor)DoublyLinkedList_dealloc,
|
|
731
|
+
.tp_str = (reprfunc)DoublyLinkedList_str,
|
|
732
|
+
.tp_methods = DoublyLinkedList_methods,
|
|
733
|
+
.tp_members = DoublyLinkedList_members,
|
|
734
|
+
.tp_as_sequence = &DoublyLinkedList_sequence,
|
|
735
|
+
.tp_as_mapping = &DoublyLinkedList_map
|
|
736
|
+
};
|
|
737
|
+
|
|
738
|
+
static int doubly_linked_list_module_exec(PyObject *m)
|
|
739
|
+
{
|
|
740
|
+
if (PyType_Ready(&DoublyLinkedListType) < 0) {return -1;}
|
|
741
|
+
Py_INCREF(&DoublyLinkedListType);
|
|
742
|
+
if (PyModule_AddObject(m, "DoublyLinkedList", (PyObject *) &DoublyLinkedListType) < 0) {
|
|
743
|
+
Py_DECREF(&DoublyLinkedListType);
|
|
744
|
+
Py_DECREF(m);
|
|
745
|
+
return -1;
|
|
746
|
+
}
|
|
747
|
+
return 0;
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
#if PY_MINOR_VERSION >= 12
|
|
751
|
+
|
|
752
|
+
static PyModuleDef_Slot py_doubly_linked_list_module_slots[] = {
|
|
753
|
+
{Py_mod_exec, dllnode_module_exec},
|
|
754
|
+
{Py_mod_exec, doubly_linked_list_module_exec},
|
|
755
|
+
{Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED},
|
|
756
|
+
{0, NULL}
|
|
757
|
+
};
|
|
758
|
+
|
|
759
|
+
#else
|
|
760
|
+
|
|
761
|
+
static PyModuleDef_Slot py_doubly_linked_list_module_slots[] = {
|
|
762
|
+
{Py_mod_exec, dllnode_module_exec},
|
|
763
|
+
{Py_mod_exec, doubly_linked_list_module_exec},
|
|
764
|
+
{0, NULL}
|
|
765
|
+
};
|
|
766
|
+
|
|
767
|
+
#endif
|
|
768
|
+
|
|
769
|
+
static struct PyModuleDef py_doubly_linked_list_module = {
|
|
770
|
+
PyModuleDef_HEAD_INIT,
|
|
771
|
+
"py_doubly_linked_list.doubly_linked_list",
|
|
772
|
+
"A library implementing a doubly linked list for python",
|
|
773
|
+
.m_slots = py_doubly_linked_list_module_slots
|
|
774
|
+
};
|
|
775
|
+
|
|
776
|
+
PyMODINIT_FUNC PyInit_doubly_linked_list(void) {
|
|
777
|
+
return PyModuleDef_Init(&py_doubly_linked_list_module);
|
|
778
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .doubly_linked_list import DoublyLinkedList
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
from typing import Iterable, Any, Self
|
|
2
|
+
from collections.abc import Callable
|
|
3
|
+
|
|
4
|
+
class DoublyLinkedList:
|
|
5
|
+
def __init__(self, Iterable: Iterable) -> None: ...
|
|
6
|
+
def append(self, object: Any, forward: bool = True)-> None:
|
|
7
|
+
"""Append object to the end of the list. Set forward to false to append to the start."""
|
|
8
|
+
...
|
|
9
|
+
def clear(self) -> None:
|
|
10
|
+
"""Remove all items from the list."""
|
|
11
|
+
...
|
|
12
|
+
def copy(self) -> DoublyLinkedList:
|
|
13
|
+
"""Return a shallow copy of the list."""
|
|
14
|
+
...
|
|
15
|
+
def count(self, value: Any) -> int:
|
|
16
|
+
"""Return number of occurrences of value in the list."""
|
|
17
|
+
...
|
|
18
|
+
def extend(self, iterable: Iterable, forward: bool = True) -> None:
|
|
19
|
+
"""Extend list by appending elements from the iterable. Set forward to false to extend from the start."""
|
|
20
|
+
...
|
|
21
|
+
def index(self, value: Any, start: int, stop: int) -> int:
|
|
22
|
+
"""Return first index of value.
|
|
23
|
+
Raises ValueError if the value is not present."""
|
|
24
|
+
...
|
|
25
|
+
def insert(self, object: Any, index: int, forward: bool = True) -> None:
|
|
26
|
+
"""Insert object after index. Set forward to false to insert before index."""
|
|
27
|
+
...
|
|
28
|
+
def pop(self, index: int = -1) -> Any:
|
|
29
|
+
"""Remove and return item at index (default last).
|
|
30
|
+
Raises IndexError if list is empty or index is out of range."""
|
|
31
|
+
...
|
|
32
|
+
def remove(self, value: Any) -> None:
|
|
33
|
+
"""Remove first occurence of value.
|
|
34
|
+
Raises ValueError if the value is not present."""
|
|
35
|
+
...
|
|
36
|
+
def reverse(self) -> None:
|
|
37
|
+
"""Reverse the order of the list."""
|
|
38
|
+
...
|
|
39
|
+
def sort(self: DoublyLinkedList, key: Callable, reverse: bool = False) -> None:
|
|
40
|
+
"""In-place sort in ascending order, equal objects are not swapped. Key can be applied to values and the list will be sorted based on the result of applying the key. Reverse will reverse the sort order."""
|
|
41
|
+
...
|
|
42
|
+
def __add__(self, Iterable: Iterable) -> DoublyLinkedList: ...
|
|
43
|
+
def __contains__(self, Any: Any) -> bool: ...
|
|
44
|
+
def __delitem__(self, Any: Any) -> None: ...
|
|
45
|
+
def __getitem__(self, Index: int) -> Any: ...
|
|
46
|
+
def __iadd__(self, Iterable: Iterable) -> Self: ...
|
|
47
|
+
def __len__(self) -> int: ...
|
|
48
|
+
def __setitem__(self, Index: int, Object: Any) -> None: ...
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: py-doubly-linked-list
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: A library adding doubly linked lists to python.
|
|
5
|
+
Author-email: Joshua Morningstar <joshuam18745@gmail.com>
|
|
6
|
+
Maintainer-email: Joshua Morningstar <joshuam18745@gmail.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
Project-URL: Documentation, https://github.com/JoshuaM176/PyDoublyLinkedList/blob/main/README.MD
|
|
9
|
+
Project-URL: Repository, https://github.com/JoshuaM176/PyDoublyLinkedList
|
|
10
|
+
Project-URL: Issues, https://github.com/JoshuaM176/PyDoublyLinkedList/issues
|
|
11
|
+
Project-URL: Changelog, https://github.com/JoshuaM176/PyDoublyLinkedList/blob/main/CHANGES
|
|
12
|
+
Keywords: doubly linked list,data structure
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
22
|
+
Requires-Python: >=3.9
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
License-File: README.MD
|
|
26
|
+
Dynamic: license-file
|
|
27
|
+
|
|
28
|
+
# py-doubly-linked-list
|
|
29
|
+
## Description
|
|
30
|
+
This library provides an implementation of a doubly linked list for python written with the c api. It is meant to be as close as possible to a regular list in its interface, meaning you can interact with it in almost all of the same ways you interact with a regular python list.
|
|
31
|
+
## Methods
|
|
32
|
+
- append
|
|
33
|
+
Append object to the end of the list. Set forward to false to append to the start.
|
|
34
|
+
```Python
|
|
35
|
+
doubly_linked_list.append(object: Any, forward: bool = True)
|
|
36
|
+
```
|
|
37
|
+
- clear
|
|
38
|
+
Remove all items from the list.
|
|
39
|
+
```Python
|
|
40
|
+
doubly_linked_list.clear()
|
|
41
|
+
```
|
|
42
|
+
- copy
|
|
43
|
+
Return a shallow copy of the list.
|
|
44
|
+
```Python
|
|
45
|
+
doubly_linked_list.copy()
|
|
46
|
+
```
|
|
47
|
+
- count
|
|
48
|
+
Return number of occurrences of value in the list.
|
|
49
|
+
```Python
|
|
50
|
+
doubly_linked_list.count(value: Any)
|
|
51
|
+
```
|
|
52
|
+
- extend
|
|
53
|
+
Extend list by appending elements from the iterable. Set forward to false to extend from the start.
|
|
54
|
+
```Python
|
|
55
|
+
doubly_linked_list.extend(iterable: Iterable, forward: bool = True)
|
|
56
|
+
```
|
|
57
|
+
- index
|
|
58
|
+
Return first index of value. Raises ValueError if the value is not present.
|
|
59
|
+
```Python
|
|
60
|
+
doubly_linked_list.index(value: Any, start: int, stop: int)
|
|
61
|
+
```
|
|
62
|
+
- insert
|
|
63
|
+
Insert object after index. Set forward to false to insert before index.
|
|
64
|
+
```Python
|
|
65
|
+
doubly_linked_list.insert(object: Any, index: int, forward: bool = True)
|
|
66
|
+
```
|
|
67
|
+
- pop
|
|
68
|
+
Remove and return item at index (default last). Raises IndexError if list is empty or index is out of range.
|
|
69
|
+
```Python
|
|
70
|
+
doubly_linked_list.pop(index: int = -1)
|
|
71
|
+
```
|
|
72
|
+
- remove
|
|
73
|
+
Remove first occurence of value. Raises ValueError if the value is not present.
|
|
74
|
+
```Python
|
|
75
|
+
doubly_linked_list.remove(value: Any)
|
|
76
|
+
```
|
|
77
|
+
- reverse
|
|
78
|
+
Reverse the order of the list.
|
|
79
|
+
```Python
|
|
80
|
+
doubly_linked_list.reverse()
|
|
81
|
+
```
|
|
82
|
+
- sort
|
|
83
|
+
In-place sort in ascending order, equal objects are not swapped. Key can be applied to values and the list will be sorted based on the result of applying the key. Reverse will reverse the sort order.
|
|
84
|
+
```Python
|
|
85
|
+
doubly_linked_list.sort(key: Callable = None, reverse: bool = False)
|
|
86
|
+
```
|
|
87
|
+
The DoublyLinkedList also supports:
|
|
88
|
+
- concatenation with other iterables
|
|
89
|
+
- in-place concatenation with other iterables
|
|
90
|
+
- indexing and assignment by index
|
|
91
|
+
- slicing
|
|
92
|
+
- iterating
|
|
93
|
+
|
|
94
|
+
Things that are not currently supported but might be in the future:
|
|
95
|
+
- assigning slices
|
|
96
|
+
- repeating, i.e list = [1] * 5 create a list with 1 repeated 5 times
|
|
97
|
+
|
|
98
|
+
# License
|
|
99
|
+
Copyright (c) 2025 Joshua A. Morningstar
|
|
100
|
+
|
|
101
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
102
|
+
|
|
103
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
104
|
+
|
|
105
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.MD
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/doubly_linked_list.c
|
|
5
|
+
src/py_doubly_linked_list/__init__.py
|
|
6
|
+
src/py_doubly_linked_list/__init__.pyi
|
|
7
|
+
src/py_doubly_linked_list.egg-info/PKG-INFO
|
|
8
|
+
src/py_doubly_linked_list.egg-info/SOURCES.txt
|
|
9
|
+
src/py_doubly_linked_list.egg-info/dependency_links.txt
|
|
10
|
+
src/py_doubly_linked_list.egg-info/top_level.txt
|
|
11
|
+
tests/test_build.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
py_doubly_linked_list
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from py_doubly_linked_list import DoublyLinkedList
|
|
2
|
+
import sys
|
|
3
|
+
import weakref
|
|
4
|
+
|
|
5
|
+
class DummyClass():
|
|
6
|
+
def __init__(self, id: int):
|
|
7
|
+
self.id = id
|
|
8
|
+
|
|
9
|
+
def __str__(self):
|
|
10
|
+
return str(self.id)
|
|
11
|
+
|
|
12
|
+
def create_test_list():
|
|
13
|
+
return DoublyLinkedList([DummyClass(0),DummyClass(1),DummyClass(2),DummyClass(3)])
|
|
14
|
+
|
|
15
|
+
def test_length():
|
|
16
|
+
test_list = create_test_list()
|
|
17
|
+
assert len(test_list) == 4
|
|
18
|
+
test_list.pop()
|
|
19
|
+
assert len(test_list) == 3
|
|
20
|
+
test_list.clear()
|
|
21
|
+
assert len(test_list) == 0
|
|
22
|
+
|
|
23
|
+
def test_dereferencing():
|
|
24
|
+
test_list = create_test_list()
|
|
25
|
+
reference = weakref.ref(test_list[0])
|
|
26
|
+
test_list.pop(0)
|
|
27
|
+
assert reference() is None
|
|
28
|
+
reference = weakref.ref(test_list[0])
|
|
29
|
+
test_list.clear()
|
|
30
|
+
assert reference() is None
|