linkedset 0.1.0__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.
- linkedset-0.1.0/.github/dependabot.yml +19 -0
- linkedset-0.1.0/.github/workflows/ci.yml +38 -0
- linkedset-0.1.0/.github/workflows/docs.yml +43 -0
- linkedset-0.1.0/.github/workflows/publish.yml +42 -0
- linkedset-0.1.0/.gitignore +174 -0
- linkedset-0.1.0/.vscode/settings.json +4 -0
- linkedset-0.1.0/LICENSE +21 -0
- linkedset-0.1.0/PKG-INFO +199 -0
- linkedset-0.1.0/README.md +168 -0
- linkedset-0.1.0/docs/Makefile +16 -0
- linkedset-0.1.0/docs/api.md +8 -0
- linkedset-0.1.0/docs/conf.py +52 -0
- linkedset-0.1.0/docs/index.md +20 -0
- linkedset-0.1.0/linkedset/__init__.py +606 -0
- linkedset-0.1.0/linkedset/py.typed +0 -0
- linkedset-0.1.0/pyproject.toml +48 -0
- linkedset-0.1.0/tests/test_linkedset.py +496 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
version: 2
|
|
2
|
+
updates:
|
|
3
|
+
- package-ecosystem: github-actions
|
|
4
|
+
directory: /
|
|
5
|
+
schedule:
|
|
6
|
+
interval: weekly
|
|
7
|
+
groups:
|
|
8
|
+
github-actions:
|
|
9
|
+
patterns:
|
|
10
|
+
- "*"
|
|
11
|
+
|
|
12
|
+
- package-ecosystem: pip
|
|
13
|
+
directory: /
|
|
14
|
+
schedule:
|
|
15
|
+
interval: weekly
|
|
16
|
+
groups:
|
|
17
|
+
python-dependencies:
|
|
18
|
+
patterns:
|
|
19
|
+
- "*"
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [main]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
test:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
strategy:
|
|
13
|
+
fail-fast: false
|
|
14
|
+
matrix:
|
|
15
|
+
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
|
|
16
|
+
steps:
|
|
17
|
+
- uses: actions/checkout@v7
|
|
18
|
+
- uses: actions/setup-python@v6
|
|
19
|
+
with:
|
|
20
|
+
python-version: ${{ matrix.python-version }}
|
|
21
|
+
- name: Install
|
|
22
|
+
run: python -m pip install -e ".[dev]"
|
|
23
|
+
- name: Test
|
|
24
|
+
run: python -m pytest -q
|
|
25
|
+
|
|
26
|
+
lint:
|
|
27
|
+
runs-on: ubuntu-latest
|
|
28
|
+
steps:
|
|
29
|
+
- uses: actions/checkout@v7
|
|
30
|
+
- uses: actions/setup-python@v6
|
|
31
|
+
with:
|
|
32
|
+
python-version: "3.12"
|
|
33
|
+
- name: Install ruff
|
|
34
|
+
run: python -m pip install ruff
|
|
35
|
+
- name: Ruff check
|
|
36
|
+
run: python -m ruff check .
|
|
37
|
+
- name: Ruff format check
|
|
38
|
+
run: python -m ruff format --check .
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
name: Docs
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [main]
|
|
8
|
+
|
|
9
|
+
permissions:
|
|
10
|
+
contents: read
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
build:
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v7
|
|
17
|
+
- uses: actions/setup-python@v6
|
|
18
|
+
with:
|
|
19
|
+
python-version: "3.12"
|
|
20
|
+
- name: Install
|
|
21
|
+
run: python -m pip install -e ".[docs]"
|
|
22
|
+
- name: Build docs
|
|
23
|
+
run: python -m sphinx -b html -W --keep-going docs docs/_build/html
|
|
24
|
+
- name: Upload Pages artifact
|
|
25
|
+
if: github.ref == 'refs/heads/main'
|
|
26
|
+
uses: actions/upload-pages-artifact@v5
|
|
27
|
+
with:
|
|
28
|
+
path: docs/_build/html
|
|
29
|
+
|
|
30
|
+
deploy:
|
|
31
|
+
if: github.ref == 'refs/heads/main'
|
|
32
|
+
needs: build
|
|
33
|
+
runs-on: ubuntu-latest
|
|
34
|
+
permissions:
|
|
35
|
+
pages: write
|
|
36
|
+
id-token: write
|
|
37
|
+
environment:
|
|
38
|
+
name: github-pages
|
|
39
|
+
url: ${{ steps.deployment.outputs.page_url }}
|
|
40
|
+
steps:
|
|
41
|
+
- name: Deploy to GitHub Pages
|
|
42
|
+
id: deployment
|
|
43
|
+
uses: actions/deploy-pages@v5
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
build:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
steps:
|
|
11
|
+
- uses: actions/checkout@v7
|
|
12
|
+
- uses: actions/setup-python@v6
|
|
13
|
+
with:
|
|
14
|
+
python-version: "3.12"
|
|
15
|
+
- name: Build sdist and wheel
|
|
16
|
+
run: |
|
|
17
|
+
python -m pip install --upgrade build
|
|
18
|
+
python -m build
|
|
19
|
+
- name: Check metadata
|
|
20
|
+
run: |
|
|
21
|
+
python -m pip install --upgrade twine
|
|
22
|
+
python -m twine check dist/*
|
|
23
|
+
- uses: actions/upload-artifact@v7
|
|
24
|
+
with:
|
|
25
|
+
name: dist
|
|
26
|
+
path: dist/
|
|
27
|
+
|
|
28
|
+
publish:
|
|
29
|
+
needs: build
|
|
30
|
+
runs-on: ubuntu-latest
|
|
31
|
+
environment:
|
|
32
|
+
name: pypi
|
|
33
|
+
url: https://pypi.org/project/linkedset/
|
|
34
|
+
permissions:
|
|
35
|
+
id-token: write # Required for Trusted Publishing (OIDC)
|
|
36
|
+
steps:
|
|
37
|
+
- uses: actions/download-artifact@v8
|
|
38
|
+
with:
|
|
39
|
+
name: dist
|
|
40
|
+
path: dist/
|
|
41
|
+
- name: Publish to PyPI
|
|
42
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py,cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
#uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
#poetry.lock
|
|
109
|
+
|
|
110
|
+
# pdm
|
|
111
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
112
|
+
#pdm.lock
|
|
113
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
114
|
+
# in version control.
|
|
115
|
+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
|
116
|
+
.pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
121
|
+
__pypackages__/
|
|
122
|
+
|
|
123
|
+
# Celery stuff
|
|
124
|
+
celerybeat-schedule
|
|
125
|
+
celerybeat.pid
|
|
126
|
+
|
|
127
|
+
# SageMath parsed files
|
|
128
|
+
*.sage.py
|
|
129
|
+
|
|
130
|
+
# Environments
|
|
131
|
+
.env
|
|
132
|
+
.venv
|
|
133
|
+
env/
|
|
134
|
+
venv/
|
|
135
|
+
ENV/
|
|
136
|
+
env.bak/
|
|
137
|
+
venv.bak/
|
|
138
|
+
|
|
139
|
+
# Spyder project settings
|
|
140
|
+
.spyderproject
|
|
141
|
+
.spyproject
|
|
142
|
+
|
|
143
|
+
# Rope project settings
|
|
144
|
+
.ropeproject
|
|
145
|
+
|
|
146
|
+
# mkdocs documentation
|
|
147
|
+
/site
|
|
148
|
+
|
|
149
|
+
# mypy
|
|
150
|
+
.mypy_cache/
|
|
151
|
+
.dmypy.json
|
|
152
|
+
dmypy.json
|
|
153
|
+
|
|
154
|
+
# Pyre type checker
|
|
155
|
+
.pyre/
|
|
156
|
+
|
|
157
|
+
# pytype static type analyzer
|
|
158
|
+
.pytype/
|
|
159
|
+
|
|
160
|
+
# Cython debug symbols
|
|
161
|
+
cython_debug/
|
|
162
|
+
|
|
163
|
+
# PyCharm
|
|
164
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
165
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
166
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
167
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
168
|
+
#.idea/
|
|
169
|
+
|
|
170
|
+
# Ruff stuff:
|
|
171
|
+
.ruff_cache/
|
|
172
|
+
|
|
173
|
+
# PyPI configuration file
|
|
174
|
+
.pypirc
|
linkedset-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Justin Chu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
linkedset-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: linkedset
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Ordered set robust against mutation during iteration in Python
|
|
5
|
+
Project-URL: Homepage, https://github.com/justinchuby/linkedset
|
|
6
|
+
Project-URL: Documentation, https://justinchuby.github.io/linkedset/
|
|
7
|
+
Project-URL: Repository, https://github.com/justinchuby/linkedset
|
|
8
|
+
Project-URL: Issues, https://github.com/justinchuby/linkedset/issues
|
|
9
|
+
Author: Justin Chu
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: data-structure,iteration,linked-list,ordered-set
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
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: Typing :: Typed
|
|
22
|
+
Requires-Python: >=3.9
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: pytest>=7; extra == 'dev'
|
|
25
|
+
Requires-Dist: ruff; extra == 'dev'
|
|
26
|
+
Provides-Extra: docs
|
|
27
|
+
Requires-Dist: furo; extra == 'docs'
|
|
28
|
+
Requires-Dist: myst-parser; extra == 'docs'
|
|
29
|
+
Requires-Dist: sphinx>=7; extra == 'docs'
|
|
30
|
+
Description-Content-Type: text/markdown
|
|
31
|
+
|
|
32
|
+
# linkedset
|
|
33
|
+
|
|
34
|
+
An ordered set that is robust against mutation during iteration, implemented in pure Python.
|
|
35
|
+
|
|
36
|
+
`DoublyLinkedSet` behaves like an ordered set backed by a doubly linked list. Inserting
|
|
37
|
+
and removing elements is `O(1)`, and — unlike Python's built-in `list` or `set` — you can
|
|
38
|
+
safely add, remove, or move elements *while iterating over the container*.
|
|
39
|
+
|
|
40
|
+
It implements both `collections.abc.Sequence` (ordered, indexable) and
|
|
41
|
+
`collections.abc.MutableSet` (set algebra and in-place updates).
|
|
42
|
+
|
|
43
|
+
📖 **Documentation**: <https://justinchuby.github.io/linkedset/>
|
|
44
|
+
|
|
45
|
+
## Installation
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
pip install linkedset
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Or install from source:
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
git clone https://github.com/justinchuby/linkedset
|
|
55
|
+
cd linkedset
|
|
56
|
+
pip install -e ".[dev]"
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Usage
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
from linkedset import DoublyLinkedSet
|
|
63
|
+
|
|
64
|
+
s = DoublyLinkedSet(["a", "b", "c"])
|
|
65
|
+
|
|
66
|
+
s.append("d") # -> a, b, c, d
|
|
67
|
+
s.insert_after("a", ["x"]) # -> a, x, b, c, d
|
|
68
|
+
s.insert_before("b", ["y"]) # -> a, x, y, b, c, d
|
|
69
|
+
s.remove("c") # -> a, x, y, b, d
|
|
70
|
+
|
|
71
|
+
print(s[0]) # "a" (O(1))
|
|
72
|
+
print(s[-1]) # "d" (O(1))
|
|
73
|
+
print(list(s)) # ['a', 'x', 'y', 'b', 'd']
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Safe mutation during iteration
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
s = DoublyLinkedSet(["a", "b", "c"])
|
|
80
|
+
for x in s:
|
|
81
|
+
if x == "a":
|
|
82
|
+
s.insert_after("a", ["d"]) # inserted after current -> iterated
|
|
83
|
+
s.remove("b") # removed before reached -> skipped
|
|
84
|
+
# Iterated: a, d, c
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Iteration rules:
|
|
88
|
+
|
|
89
|
+
- Elements inserted **after** the current node **are** iterated over.
|
|
90
|
+
- Elements inserted **before** the current node are **not** iterated over in the current pass.
|
|
91
|
+
- If the current node is moved to a different location, iteration continues from the node
|
|
92
|
+
that followed it at its *original* location.
|
|
93
|
+
|
|
94
|
+
Per-element mutation (`add`, `remove`, `discard`, `append`, `insert`, `clear`, …) is safe
|
|
95
|
+
during iteration. The **global reorders** `reverse()` and `rotate()` are *not* — calling them
|
|
96
|
+
mid-iteration may cause an in-progress iterator to skip or repeat elements.
|
|
97
|
+
|
|
98
|
+
### Set operations
|
|
99
|
+
|
|
100
|
+
Because it is a `MutableSet`, the usual set algebra works and returns a new
|
|
101
|
+
`DoublyLinkedSet` (order preserved):
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
a = DoublyLinkedSet(["a", "b", "c"])
|
|
105
|
+
b = DoublyLinkedSet(["c", "d"])
|
|
106
|
+
|
|
107
|
+
a | b # union -> a, b, c, d
|
|
108
|
+
a & b # intersection -> c
|
|
109
|
+
a - b # difference -> a, b
|
|
110
|
+
a ^ b # symmetric -> a, b, d
|
|
111
|
+
|
|
112
|
+
a.add("x") # idempotent add (no-op if already present)
|
|
113
|
+
a.discard("z") # remove if present, never raises
|
|
114
|
+
a.pop() # remove and return the last element (list-style; pass an index too)
|
|
115
|
+
a |= b # in-place update
|
|
116
|
+
|
|
117
|
+
# `==` is order-sensitive, because the set is ordered:
|
|
118
|
+
DoublyLinkedSet(["a", "b"]) == DoublyLinkedSet(["a", "b"]) # True
|
|
119
|
+
DoublyLinkedSet(["a", "b"]) == DoublyLinkedSet(["b", "a"]) # False
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Deque- and list-style methods
|
|
123
|
+
|
|
124
|
+
Because it is ordered, it also offers the familiar `deque`/`list` mutators (all keeping the
|
|
125
|
+
set's uniqueness and identity semantics):
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
s = DoublyLinkedSet(["a", "b", "c"])
|
|
129
|
+
|
|
130
|
+
s.appendleft("z") # -> z, a, b, c
|
|
131
|
+
s.extendleft(["x", "y"])# -> y, x, z, a, b, c (prepended, reversed like deque)
|
|
132
|
+
s.popleft() # removes and returns "y"
|
|
133
|
+
s.pop() # removes and returns the last element ("c")
|
|
134
|
+
s.pop(1) # removes and returns the element at index 1
|
|
135
|
+
s.insert(1, "q") # insert before index 1 (index clamped like list.insert)
|
|
136
|
+
s.rotate(1) # rotate right; negative rotates left
|
|
137
|
+
s.reverse() # reverse in place
|
|
138
|
+
s2 = s.copy() # shallow copy, order preserved
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## Semantics
|
|
142
|
+
|
|
143
|
+
- Membership and set operations are based on object **identity** (`id(value)`), not equality.
|
|
144
|
+
Two distinct objects that compare equal are treated as different elements.
|
|
145
|
+
- `==` is **order-sensitive** (it is an *ordered* set): equal only when the same elements, by
|
|
146
|
+
identity, appear in the same order. Instances are **not hashable** (mutable set).
|
|
147
|
+
Ordering/subset comparisons (`<`, `<=`, `>`, `>=`) are **not supported** (they raise
|
|
148
|
+
`TypeError`), since a subset relation would be ambiguous next to order-sensitive equality;
|
|
149
|
+
use the set algebra (`&`, `|`, `-`, `^`) or `isdisjoint()` instead.
|
|
150
|
+
- `None` is not a valid value.
|
|
151
|
+
- Accessing by index is `O(n)`, except the ends (`s[0]`, `s[-1]`) which are `O(1)`.
|
|
152
|
+
|
|
153
|
+
## Complexity
|
|
154
|
+
|
|
155
|
+
`DoublyLinkedSet` is a doubly linked list paired with a `dict` mapping each element's
|
|
156
|
+
`id()` to its list node. That combination gives set-like `O(1)` membership and endpoint
|
|
157
|
+
mutation, while preserving order and safe mutation during iteration.
|
|
158
|
+
|
|
159
|
+
Let `n` be the size of the set (and `m` the size of the other operand for binary set
|
|
160
|
+
operations).
|
|
161
|
+
|
|
162
|
+
| Operation | Complexity | Notes |
|
|
163
|
+
| --- | --- | --- |
|
|
164
|
+
| `x in s`, `s.count(x)` | `O(1)` | `dict` lookup by `id(x)` |
|
|
165
|
+
| `len(s)` | `O(1)` | length is tracked, not counted |
|
|
166
|
+
| `s.append(x)`, `s.add(x)`, `s.appendleft(x)` | `O(1)` | insert at a known end |
|
|
167
|
+
| `s.remove(x)`, `s.discard(x)` | `O(1)` | unlink the node, no shifting |
|
|
168
|
+
| `s.extend(xs)`, `s.extendleft(xs)` | `O(k)` | `k = len(xs)`; `O(1)` per element |
|
|
169
|
+
| `s.insert_after(v, xs)`, `s.insert_before(v, xs)` | `O(k)` | `k = len(xs)`; `O(1)` per element |
|
|
170
|
+
| `s.pop()`, `s.popleft()` | `O(1)` | remove from an end |
|
|
171
|
+
| `s.pop(i)`, `s.insert(i, x)` | `O(\|i\|)` | walks to the index from the nearer end |
|
|
172
|
+
| `s[0]`, `s[-1]` | `O(1)` | endpoints are reachable directly |
|
|
173
|
+
| `s[i]` | `O(\|i\|)` | walks from the nearer end |
|
|
174
|
+
| `s.index(x)` | `O(n)` | linear scan for position |
|
|
175
|
+
| `s.rotate(n)` | `O(n mod len(s))` | relink only, no node churn |
|
|
176
|
+
| `s.reverse()`, `s.copy()`, `s.clear()` | `O(n)` | |
|
|
177
|
+
| iteration, `reversed(s)`, `s == other` | `O(n)` | |
|
|
178
|
+
| `s[a:b]` (slice) | `O(n)` | materialises a tuple |
|
|
179
|
+
| `s \| t`, `s & t`, `s - t`, `s ^ t` | `O(n + m)` | each membership test is `O(1)` |
|
|
180
|
+
| `s.isdisjoint(t)` | `O(n)` | one pass with `O(1)` lookups |
|
|
181
|
+
|
|
182
|
+
Space is `O(n)`: every element is wrapped in a small link node and referenced once from the
|
|
183
|
+
`id`-keyed index.
|
|
184
|
+
|
|
185
|
+
Mutating during iteration stays `O(1)` per operation. Removed nodes are unlinked from their
|
|
186
|
+
neighbours immediately, so a traversal never pays to skip over dead nodes — the only cost is
|
|
187
|
+
following the `next` pointer you already hold.
|
|
188
|
+
|
|
189
|
+
## Development
|
|
190
|
+
|
|
191
|
+
```bash
|
|
192
|
+
pip install -e ".[dev]"
|
|
193
|
+
python -m pytest # run tests
|
|
194
|
+
python -m ruff check # lint
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## License
|
|
198
|
+
|
|
199
|
+
MIT. Portions derived from the ONNX Project Contributors (Apache-2.0); see `linkedset/__init__.py`.
|