spillsort 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.
@@ -0,0 +1,373 @@
1
+ # This file is automatically @generated by Cargo.
2
+ # It is not intended for manual editing.
3
+ version = 4
4
+
5
+ [[package]]
6
+ name = "autocfg"
7
+ version = "1.5.1"
8
+ source = "registry+https://github.com/rust-lang/crates.io-index"
9
+ checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
10
+
11
+ [[package]]
12
+ name = "bitflags"
13
+ version = "2.13.2"
14
+ source = "registry+https://github.com/rust-lang/crates.io-index"
15
+ checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06"
16
+
17
+ [[package]]
18
+ name = "cfg-if"
19
+ version = "1.0.5"
20
+ source = "registry+https://github.com/rust-lang/crates.io-index"
21
+ checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600"
22
+
23
+ [[package]]
24
+ name = "chacha20"
25
+ version = "0.10.2"
26
+ source = "registry+https://github.com/rust-lang/crates.io-index"
27
+ checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06"
28
+ dependencies = [
29
+ "cfg-if",
30
+ "cpufeatures",
31
+ "rand_core",
32
+ ]
33
+
34
+ [[package]]
35
+ name = "core_detect"
36
+ version = "1.0.0"
37
+ source = "registry+https://github.com/rust-lang/crates.io-index"
38
+ checksum = "7f8f80099a98041a3d1622845c271458a2d73e688351bf3cb999266764b81d48"
39
+
40
+ [[package]]
41
+ name = "cpufeatures"
42
+ version = "0.3.1"
43
+ source = "registry+https://github.com/rust-lang/crates.io-index"
44
+ checksum = "5ca28b0ae3115b884660db4118d803791fd6756b6e88f39c0f3f7859060d7566"
45
+ dependencies = [
46
+ "libc",
47
+ ]
48
+
49
+ [[package]]
50
+ name = "crossbeam-deque"
51
+ version = "0.8.8"
52
+ source = "registry+https://github.com/rust-lang/crates.io-index"
53
+ checksum = "622f3fc73690be383c7214310406f28a90e6edeadc3cea882f9d71e495b9711a"
54
+ dependencies = [
55
+ "crossbeam-epoch",
56
+ "crossbeam-utils",
57
+ ]
58
+
59
+ [[package]]
60
+ name = "crossbeam-epoch"
61
+ version = "0.9.21"
62
+ source = "registry+https://github.com/rust-lang/crates.io-index"
63
+ checksum = "dc74980687109a3b14c72fd458107bf0baa1da1a1a805e178d15501ba9b86d9d"
64
+ dependencies = [
65
+ "crossbeam-utils",
66
+ ]
67
+
68
+ [[package]]
69
+ name = "crossbeam-utils"
70
+ version = "0.8.23"
71
+ source = "registry+https://github.com/rust-lang/crates.io-index"
72
+ checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6"
73
+
74
+ [[package]]
75
+ name = "either"
76
+ version = "1.19.0"
77
+ source = "registry+https://github.com/rust-lang/crates.io-index"
78
+ checksum = "0e9c71c2167ca323c882b99918929403426e2373ea17242ff5653e0d5e1058be"
79
+
80
+ [[package]]
81
+ name = "errno"
82
+ version = "0.3.14"
83
+ source = "registry+https://github.com/rust-lang/crates.io-index"
84
+ checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
85
+ dependencies = [
86
+ "libc",
87
+ "windows-sys",
88
+ ]
89
+
90
+ [[package]]
91
+ name = "fastrand"
92
+ version = "2.5.0"
93
+ source = "registry+https://github.com/rust-lang/crates.io-index"
94
+ checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223"
95
+
96
+ [[package]]
97
+ name = "getrandom"
98
+ version = "0.4.3"
99
+ source = "registry+https://github.com/rust-lang/crates.io-index"
100
+ checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099"
101
+ dependencies = [
102
+ "cfg-if",
103
+ "libc",
104
+ "r-efi",
105
+ "rand_core",
106
+ ]
107
+
108
+ [[package]]
109
+ name = "heck"
110
+ version = "0.5.0"
111
+ source = "registry+https://github.com/rust-lang/crates.io-index"
112
+ checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
113
+
114
+ [[package]]
115
+ name = "libc"
116
+ version = "0.2.190"
117
+ source = "registry+https://github.com/rust-lang/crates.io-index"
118
+ checksum = "ce5d3ddc6d3fa000eb1536d85e147bfe31aacaba692ed6a876f95cb7c855be78"
119
+
120
+ [[package]]
121
+ name = "linux-raw-sys"
122
+ version = "0.12.1"
123
+ source = "registry+https://github.com/rust-lang/crates.io-index"
124
+ checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53"
125
+
126
+ [[package]]
127
+ name = "num-traits"
128
+ version = "0.2.19"
129
+ source = "registry+https://github.com/rust-lang/crates.io-index"
130
+ checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
131
+ dependencies = [
132
+ "autocfg",
133
+ ]
134
+
135
+ [[package]]
136
+ name = "once_cell"
137
+ version = "1.21.4"
138
+ source = "registry+https://github.com/rust-lang/crates.io-index"
139
+ checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
140
+
141
+ [[package]]
142
+ name = "portable-atomic"
143
+ version = "1.15.0"
144
+ source = "registry+https://github.com/rust-lang/crates.io-index"
145
+ checksum = "05c8b63e8d9609db387f0324918f81d68fe27748f084ef092fb35954d0539a85"
146
+
147
+ [[package]]
148
+ name = "proc-macro2"
149
+ version = "1.0.107"
150
+ source = "registry+https://github.com/rust-lang/crates.io-index"
151
+ checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
152
+ dependencies = [
153
+ "unicode-ident",
154
+ ]
155
+
156
+ [[package]]
157
+ name = "proptest"
158
+ version = "1.12.0"
159
+ source = "registry+https://github.com/rust-lang/crates.io-index"
160
+ checksum = "b8530004ccb15eae51c7e40009fbe317f341f804db54dc033eec1c50be28cfa0"
161
+ dependencies = [
162
+ "bitflags",
163
+ "chacha20",
164
+ "core_detect",
165
+ "num-traits",
166
+ "rand",
167
+ "rand_xorshift",
168
+ "regex-syntax",
169
+ "unarray",
170
+ ]
171
+
172
+ [[package]]
173
+ name = "pyo3"
174
+ version = "0.29.3"
175
+ source = "registry+https://github.com/rust-lang/crates.io-index"
176
+ checksum = "700d18fa267b73b9b521fd7e13580e2f446916f176cacee1ab63fcc8191f1655"
177
+ dependencies = [
178
+ "libc",
179
+ "once_cell",
180
+ "portable-atomic",
181
+ "pyo3-build-config",
182
+ "pyo3-ffi",
183
+ "pyo3-macros",
184
+ ]
185
+
186
+ [[package]]
187
+ name = "pyo3-build-config"
188
+ version = "0.29.3"
189
+ source = "registry+https://github.com/rust-lang/crates.io-index"
190
+ checksum = "7b3fc0c4d08f6bb10e71fe39dfb9e2f59c6eb6854e22ec8092f50c69a4499adb"
191
+ dependencies = [
192
+ "target-lexicon",
193
+ ]
194
+
195
+ [[package]]
196
+ name = "pyo3-ffi"
197
+ version = "0.29.3"
198
+ source = "registry+https://github.com/rust-lang/crates.io-index"
199
+ checksum = "dfc0b8e19df29aad7086cf977bb0c2a2f143e30567eb113e9cf72b62ca698330"
200
+ dependencies = [
201
+ "libc",
202
+ "pyo3-build-config",
203
+ ]
204
+
205
+ [[package]]
206
+ name = "pyo3-macros"
207
+ version = "0.29.3"
208
+ source = "registry+https://github.com/rust-lang/crates.io-index"
209
+ checksum = "6100e8a4b5eba53afaa5ed078364851a0b2499c553a44c31026b929049b49dc6"
210
+ dependencies = [
211
+ "proc-macro2",
212
+ "pyo3-macros-backend",
213
+ "quote",
214
+ "syn",
215
+ ]
216
+
217
+ [[package]]
218
+ name = "pyo3-macros-backend"
219
+ version = "0.29.3"
220
+ source = "registry+https://github.com/rust-lang/crates.io-index"
221
+ checksum = "6143877a16e82b5a727b7127ff4cd86858a24a28d745d72f43e6f227c7b1bdb3"
222
+ dependencies = [
223
+ "heck",
224
+ "proc-macro2",
225
+ "quote",
226
+ "syn",
227
+ ]
228
+
229
+ [[package]]
230
+ name = "quote"
231
+ version = "1.0.47"
232
+ source = "registry+https://github.com/rust-lang/crates.io-index"
233
+ checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
234
+ dependencies = [
235
+ "proc-macro2",
236
+ ]
237
+
238
+ [[package]]
239
+ name = "r-efi"
240
+ version = "6.0.0"
241
+ source = "registry+https://github.com/rust-lang/crates.io-index"
242
+ checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"
243
+
244
+ [[package]]
245
+ name = "rand"
246
+ version = "0.10.3"
247
+ source = "registry+https://github.com/rust-lang/crates.io-index"
248
+ checksum = "65c9fb96cbc91e3478eaae79a69fcd3f1ae4ad052e471fe6732fff548984b4af"
249
+ dependencies = [
250
+ "getrandom",
251
+ "rand_core",
252
+ ]
253
+
254
+ [[package]]
255
+ name = "rand_core"
256
+ version = "0.10.1"
257
+ source = "registry+https://github.com/rust-lang/crates.io-index"
258
+ checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69"
259
+
260
+ [[package]]
261
+ name = "rand_xorshift"
262
+ version = "0.5.0"
263
+ source = "registry+https://github.com/rust-lang/crates.io-index"
264
+ checksum = "60aa6af80be32871323012e02e6e65f8a7cc7890931ae421d217ad8fe0df2ccf"
265
+ dependencies = [
266
+ "rand_core",
267
+ ]
268
+
269
+ [[package]]
270
+ name = "rayon"
271
+ version = "1.12.0"
272
+ source = "registry+https://github.com/rust-lang/crates.io-index"
273
+ checksum = "fb39b166781f92d482534ef4b4b1b2568f42613b53e5b6c160e24cfbfa30926d"
274
+ dependencies = [
275
+ "either",
276
+ "rayon-core",
277
+ ]
278
+
279
+ [[package]]
280
+ name = "rayon-core"
281
+ version = "1.13.0"
282
+ source = "registry+https://github.com/rust-lang/crates.io-index"
283
+ checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91"
284
+ dependencies = [
285
+ "crossbeam-deque",
286
+ "crossbeam-utils",
287
+ ]
288
+
289
+ [[package]]
290
+ name = "regex-syntax"
291
+ version = "0.8.11"
292
+ source = "registry+https://github.com/rust-lang/crates.io-index"
293
+ checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4"
294
+
295
+ [[package]]
296
+ name = "rustix"
297
+ version = "1.1.5"
298
+ source = "registry+https://github.com/rust-lang/crates.io-index"
299
+ checksum = "891efababe418670775f199f0d233d84843c227a0949a883ce15b37c78d6629d"
300
+ dependencies = [
301
+ "bitflags",
302
+ "errno",
303
+ "libc",
304
+ "linux-raw-sys",
305
+ "windows-sys",
306
+ ]
307
+
308
+ [[package]]
309
+ name = "spillsort"
310
+ version = "0.1.0"
311
+ dependencies = [
312
+ "proptest",
313
+ "pyo3",
314
+ "rayon",
315
+ "tempfile",
316
+ ]
317
+
318
+ [[package]]
319
+ name = "syn"
320
+ version = "2.0.119"
321
+ source = "registry+https://github.com/rust-lang/crates.io-index"
322
+ checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297"
323
+ dependencies = [
324
+ "proc-macro2",
325
+ "quote",
326
+ "unicode-ident",
327
+ ]
328
+
329
+ [[package]]
330
+ name = "target-lexicon"
331
+ version = "0.13.5"
332
+ source = "registry+https://github.com/rust-lang/crates.io-index"
333
+ checksum = "adb6935a6f5c20170eeceb1a3835a49e12e19d792f6dd344ccc76a985ca5a6ca"
334
+
335
+ [[package]]
336
+ name = "tempfile"
337
+ version = "3.27.0"
338
+ source = "registry+https://github.com/rust-lang/crates.io-index"
339
+ checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd"
340
+ dependencies = [
341
+ "fastrand",
342
+ "getrandom",
343
+ "once_cell",
344
+ "rustix",
345
+ "windows-sys",
346
+ ]
347
+
348
+ [[package]]
349
+ name = "unarray"
350
+ version = "0.1.4"
351
+ source = "registry+https://github.com/rust-lang/crates.io-index"
352
+ checksum = "eaea85b334db583fe3274d12b4cd1880032beab409c0d774be044d4480ab9a94"
353
+
354
+ [[package]]
355
+ name = "unicode-ident"
356
+ version = "1.0.27"
357
+ source = "registry+https://github.com/rust-lang/crates.io-index"
358
+ checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
359
+
360
+ [[package]]
361
+ name = "windows-link"
362
+ version = "0.2.1"
363
+ source = "registry+https://github.com/rust-lang/crates.io-index"
364
+ checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
365
+
366
+ [[package]]
367
+ name = "windows-sys"
368
+ version = "0.61.2"
369
+ source = "registry+https://github.com/rust-lang/crates.io-index"
370
+ checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
371
+ dependencies = [
372
+ "windows-link",
373
+ ]
@@ -0,0 +1,46 @@
1
+ [package]
2
+ name = "spillsort"
3
+ version = "0.1.0"
4
+ edition = "2024"
5
+ rust-version = "1.89"
6
+ authors = ["Clint Valentine <valentine.clint@gmail.com>"]
7
+ license = "MIT"
8
+ description = "Sort more records than fit in memory: sorted runs spill to disk and merge as they are read"
9
+ repository = "https://github.com/clintval/spillsort"
10
+ documentation = "https://docs.rs/spillsort"
11
+ readme = "README.md"
12
+ keywords = ["sort", "external-sort", "merge", "spill", "python"]
13
+ categories = ["algorithms"]
14
+ include = ["/src/**/*.rs", "/Cargo.toml", "/Cargo.lock", "/README.md", "/LICENSE", "/NOTICE"]
15
+
16
+ [features]
17
+ default = []
18
+ python = ["dep:pyo3"]
19
+
20
+ [dependencies]
21
+ pyo3 = { version = "0.29", features = ["abi3-py311"], optional = true }
22
+ rayon = "1.11"
23
+ tempfile = "3"
24
+
25
+ [dev-dependencies]
26
+ proptest = { version = "1.11", default-features = false, features = ["std"] }
27
+
28
+ [lints.rust]
29
+ missing_docs = "warn"
30
+
31
+ [lints.clippy]
32
+ pedantic = { level = "warn", priority = -1 }
33
+ cast_possible_truncation = "allow"
34
+ cast_possible_wrap = "allow"
35
+ cast_sign_loss = "allow"
36
+ module_name_repetitions = "allow"
37
+ missing_errors_doc = "allow"
38
+ missing_panics_doc = "allow"
39
+ must_use_candidate = "allow"
40
+ similar_names = "allow"
41
+ decimal_bitwise_operands = "allow"
42
+ single_match_else = "allow"
43
+
44
+ [profile.release]
45
+ lto = "thin"
46
+ codegen-units = 1
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright © 2026 Clint Valentine
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.
spillsort-0.1.0/NOTICE ADDED
@@ -0,0 +1,13 @@
1
+ spillsort
2
+ Copyright © 2026 Clint Valentine
3
+
4
+ The design follows the Sorter of fgbio (https://github.com/fulcrumgenomics/fgbio,
5
+ MIT), which encodes items as they are added and holds bytes in memory, and the
6
+ SortingCollection of htsjdk (https://github.com/samtools/htsjdk, MIT), which
7
+ reads once and keeps records with equal keys in the order they were added. No
8
+ code is copied from either.
9
+
10
+ Sorting in parallel uses rayon (https://github.com/rayon-rs/rayon, MIT or
11
+ Apache-2.0), and temporary directories are made with tempfile
12
+ (https://github.com/Stebalien/tempfile, MIT or Apache-2.0). The Python bindings
13
+ use PyO3 (https://github.com/PyO3/pyo3, MIT or Apache-2.0).
@@ -0,0 +1,160 @@
1
+ Metadata-Version: 2.4
2
+ Name: spillsort
3
+ Version: 0.1.0
4
+ Classifier: Development Status :: 3 - Alpha
5
+ Classifier: Intended Audience :: Developers
6
+ Classifier: Intended Audience :: Science/Research
7
+ Classifier: Natural Language :: English
8
+ Classifier: Operating System :: OS Independent
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Programming Language :: Python :: 3.11
11
+ Classifier: Programming Language :: Python :: 3.12
12
+ Classifier: Programming Language :: Python :: 3.13
13
+ Classifier: Programming Language :: Python :: 3.14
14
+ Classifier: Programming Language :: Rust
15
+ Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
16
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
17
+ Classifier: Typing :: Typed
18
+ Requires-Dist: typing-extensions>=4.12
19
+ License-File: LICENSE
20
+ License-File: NOTICE
21
+ Summary: Sort more items than fit in memory: sorted runs spill to disk and merge as they are read.
22
+ Keywords: sort,external-sort,merge,spill,out-of-core,bioinformatics
23
+ Author-email: Clint Valentine <valentine.clint@gmail.com>
24
+ License-Expression: MIT
25
+ Requires-Python: >=3.11
26
+ Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
27
+ Project-URL: Bug Tracker, https://github.com/clintval/spillsort/issues
28
+ Project-URL: homepage, https://github.com/clintval/spillsort
29
+ Project-URL: repository, https://github.com/clintval/spillsort
30
+
31
+ # spillsort
32
+
33
+ [![Build Status](https://github.com/clintval/spillsort/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/clintval/spillsort/actions/workflows/tests.yml?query=branch%3Amain)
34
+ [![Python Versions](https://img.shields.io/badge/python-3.11_|_3.12_|_3.13_|_3.14-blue)](https://github.com/clintval/spillsort)
35
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/clintval/spillsort/blob/main/LICENSE)
36
+ [![Language](https://img.shields.io/badge/language-rust-dea588.svg)](https://www.rust-lang.org/)
37
+
38
+ Sort more items than fit in memory: sorted runs spill to disk and merge as they are read.
39
+
40
+ Install with pip or uv:
41
+
42
+ ```console
43
+ pip install spillsort
44
+ ```
45
+
46
+ Or add the Rust crate:
47
+
48
+ ```console
49
+ cargo add spillsort
50
+ ```
51
+
52
+ ## Introduction
53
+
54
+ This library sorts like `sorted(items, key=...)`, but in a fixed amount of memory.
55
+ Each item is keyed and encoded into bytes once, as it is added.
56
+ When the memory fills, a background thread sorts it and writes it to a temporary file while you keep adding.
57
+ Sorting merges those files with what is still in memory, so items come out in key order, and items with equal keys come out in the order they were added.
58
+ The engine is Rust, and it is also a Rust crate that needs no Python.
59
+
60
+ ## Quick Start
61
+
62
+ Give a key function and sort items, once:
63
+
64
+ ```python
65
+ from spillsort import Sorter
66
+
67
+ reads = [("chr2", 500, "read1"), ("chr1", 900, "read2"), ("chr1", 100, "read3"), ("chr1", 900, "read4")]
68
+
69
+ with Sorter(lambda read: (read[0], read[1])) as sorter:
70
+ ordered = list(sorter.sort(reads))
71
+
72
+ assert ordered == [("chr1", 100, "read3"), ("chr1", 900, "read2"), ("chr1", 900, "read4"), ("chr2", 500, "read1")]
73
+ ```
74
+
75
+ Add items one at a time as they arrive, bound the memory with `max_bytes`, sort on more threads with `threads`, and choose where to spill with `tmp_dir`:
76
+
77
+ ```python
78
+ import random
79
+ from tempfile import TemporaryDirectory
80
+
81
+ numbers = [random.random() for _ in range(100_000)]
82
+
83
+ with TemporaryDirectory() as tmp_dir, Sorter(max_bytes=2**20, threads=4, tmp_dir=tmp_dir) as sorter:
84
+ for number in numbers:
85
+ sorter.add(number)
86
+ ordered = list(sorter.sort())
87
+
88
+ assert ordered == sorted(numbers)
89
+ ```
90
+
91
+ Items are pickled while they wait unless a codec turns them into bytes and back:
92
+
93
+ ```python
94
+ class Line:
95
+ def encode(self, item: str, /) -> bytes:
96
+ return item.encode()
97
+
98
+ def decode(self, data: bytes, /) -> str:
99
+ return data.decode()
100
+
101
+ with Sorter(lambda line: int(line.split("\t")[1]), codec=Line()) as sorter:
102
+ lines = list(sorter.sort(["chr1\t300\n", "chr1\t20\n", "chr1\t1000\n"]))
103
+
104
+ assert lines == ["chr1\t20\n", "chr1\t300\n", "chr1\t1000\n"]
105
+ ```
106
+
107
+ If you already have the bytes, add them with their key, and no Python runs per item.
108
+ Write the sorted bytes to any binary file in large chunks, merged on another thread while the file writes:
109
+
110
+ ```python
111
+ import io
112
+
113
+ with Sorter() as sorter:
114
+ sorter.add_encoded(("chr2", 5), b"chr2\t5\t9\n")
115
+ sorter.add_encoded(("chr1", 70), b"chr1\t70\t80\n")
116
+ sorter.add_encoded(("chr1", 8), b"chr1\t8\t9\n")
117
+ out = io.BytesIO()
118
+ sorter.write_to(out)
119
+
120
+ assert out.getvalue() == b"chr1\t8\t9\nchr1\t70\t80\nchr2\t5\t9\n"
121
+ ```
122
+
123
+ From Rust, push keyed byte records and read them back merged:
124
+
125
+ ```rust
126
+ use spillsort::{Config, Sorter, key};
127
+
128
+ fn main() -> Result<(), spillsort::Error> {
129
+ let mut sorter = Sorter::new(Config::default().max_bytes(64 << 20))?;
130
+ for (refname, start) in [("chr2", 5), ("chr1", 70), ("chr1", 8)] {
131
+ let mut sort_key = Vec::new();
132
+ key::push_str(&mut sort_key, refname);
133
+ key::push_int(&mut sort_key, start);
134
+ sorter.push(&sort_key, format!("{refname}\t{start}\n").as_bytes())?;
135
+ }
136
+
137
+ let mut out = Vec::new();
138
+ sorter.finish()?.write_values(&mut out)?;
139
+ assert_eq!(out, b"chr1\t8\nchr1\t70\nchr2\t5\n");
140
+ Ok(())
141
+ }
142
+ ```
143
+
144
+ See the [API documentation](https://docs.rs/spillsort) for the crate.
145
+
146
+ ## Keys
147
+
148
+ Keys compare exactly as Python compares them, even ints and floats of any size, and NaN sorts last:
149
+
150
+ ```python
151
+ with Sorter() as sorter:
152
+ ordered = list(sorter.sort([2**64, float("nan"), 1.5, True, -0.5, 3]))
153
+
154
+ assert ordered[:-1] == [-0.5, True, 1.5, 3, 2**64]
155
+ ```
156
+
157
+ ## Development and Testing
158
+
159
+ See the [contributing guide](https://github.com/clintval/spillsort/blob/main/CONTRIBUTING.md) for more information.
160
+