1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
use ;
use ;
use crate::;
thread_local!
/// Computes the maximum possible compressed buffer size in bytes for `len` f64 values.
/// Callers can allocate a destination buffer of this size to guarantee no overflow.
///
/// # Arguments
/// * `len` - Number of `f64` values to compress.
///
/// # Returns
/// Maximum required destination buffer capacity in bytes.
///
/// ---
///
/// 计算 `len` 个 f64 浮点数值在最差情况下所需的最大压缩缓冲区字节大小。
/// 调用方可预先分配该大小的目标缓冲区,确保绝不发生容量不足错误。
///
/// # 参数
/// * `len` - 待压缩的 `f64` 元素个数。
///
/// # 返回值
/// 所需的最大目标缓冲区字节容量。
pub extern "C"
/// Computes the maximum possible compressed buffer size in bytes for `len` f32 values.
/// Callers can allocate a destination buffer of this size to guarantee no overflow.
///
/// # Arguments
/// * `len` - Number of `f32` values to compress.
///
/// # Returns
/// Maximum required destination buffer capacity in bytes.
///
/// ---
///
/// 计算 `len` 个 f32 浮点数值在最差情况下所需的最大压缩缓冲区字节大小。
/// 调用方可预先分配该大小的目标缓冲区,确保绝不发生容量不足错误。
///
/// # 参数
/// * `len` - 待压缩的 `f32` 元素个数。
///
/// # 返回值
/// 所需的最大目标缓冲区字节容量。
pub extern "C"
/// Reads the element count from the compressed byte buffer in O(1) time without decompressing.
///
/// # Arguments
/// * `src` - Pointer to the compressed byte buffer.
/// * `src_len` - Length of the compressed byte buffer.
///
/// # Returns
/// The number of floating-point values contained in the compressed buffer, or `0` on error.
///
/// # Safety
/// * `src` must point to at least `src_len` readable bytes.
///
/// ---
///
/// 从压缩字节缓冲区快速读取元素总数(O(1) 复杂度,零内存分配,无需解压任何有效载荷数据)。
///
/// # 参数
/// * `src` - 压缩字节缓冲区指针。
/// * `src_len` - 压缩字节缓冲区的有效长度。
///
/// # 返回值
/// 压缩块中所包含的浮点元素总数;若数据损坏或无法解析则返回 `0`。
///
/// # 安全性保证 (Safety)
/// * `src` 必须指向至少 `src_len` 字节可读的有效内存。
pub unsafe extern "C"
/// Resets the cached model parameters for the thread-local double-precision (f64) encoder.
/// Subsequent calls to `fastalp_compress_cached_f64` will re-sample parameters.
///
/// ---
///
/// 重置当前线程局部双精度 (f64) 状态化编码器的已缓存模型参数。
/// 后续调用 `fastalp_compress_cached_f64` 时将重新进行参数探测与采样。
pub extern "C"
/// Resets the cached model parameters for the thread-local single-precision (f32) encoder.
/// Subsequent calls to `fastalp_compress_cached_f32` will re-sample parameters.
///
/// ---
///
/// 重置当前线程局部单精度 (f32) 状态化编码器的已缓存模型参数。
/// 后续调用 `fastalp_compress_cached_f32` 时将重新进行参数探测与采样。
pub extern "C"
/// Compresses an array of f64 floating-point values with dynamic parameter sampling.
///
/// # Arguments
/// * `src` - Pointer to the source `f64` array.
/// * `len` - Number of `f64` elements in the source array.
/// * `dst` - Pointer to the destination byte buffer.
/// * `dst_cap` - Capacity of the destination buffer in bytes.
///
/// # Returns
/// The number of compressed bytes written to `dst`, or `0` if an error occurs or `dst_cap` is insufficient.
///
/// # Safety
/// * `src` must point to at least `len` valid, aligned `f64` values.
/// * `dst` must point to writable memory of at least `dst_cap` bytes.
/// * `src` and `dst` must not overlap or be null.
///
/// ---
///
/// 压缩 f64 浮点数组(包含动态模型参数采样探测)。
///
/// # 参数
/// * `src` - 源 `f64` 浮点数组指针。
/// * `len` - 源数组中 `f64` 元素的个数。
/// * `dst` - 目标字节缓冲区指针。
/// * `dst_cap` - 目标字节缓冲区的最大容量。
///
/// # 返回值
/// 实际写入目标缓冲区的压缩字节数;若发生错误或目标容量不足则返回 `0`。
///
/// # 安全性保证 (Safety)
/// * `src` 必须指向至少包含 `len` 个有效且内存对齐的 `f64` 元素。
/// * `dst` 必须指向至少具有 `dst_cap` 字节可写容量的有效内存区域。
/// * `src` 与 `dst` 指针不得重叠且不得为空。
pub unsafe extern "C"
/// Compresses an array of f64 floating-point values by reusing cached parameters from the thread-local encoder.
/// Skips sampling overhead, suitable for high-throughput streaming pipelines of stationary data.
///
/// # Arguments
/// * `src` - Pointer to the source `f64` array.
/// * `len` - Number of `f64` elements in the source array.
/// * `dst` - Pointer to the destination byte buffer.
/// * `dst_cap` - Capacity of the destination buffer in bytes.
///
/// # Returns
/// The number of compressed bytes written to `dst`, or `0` if an error occurs or `dst_cap` is insufficient.
///
/// # Safety
/// * `src` must point to at least `len` valid, aligned `f64` values.
/// * `dst` must point to writable memory of at least `dst_cap` bytes.
/// * `src` and `dst` must not overlap or be null.
///
/// ---
///
/// 复用线程局部编码器已缓存的模型参数压缩 f64 浮点数组。
/// 跳过重复采样开销,直接执行核心编码内核,适用于平稳时序数据的高吞吐流式批量压缩。
///
/// # 参数
/// * `src` - 源 `f64` 浮点数组指针。
/// * `len` - 源数组中 `f64` 元素的个数。
/// * `dst` - 目标字节缓冲区指针。
/// * `dst_cap` - 目标字节缓冲区的最大容量。
///
/// # 返回值
/// 实际写入目标缓冲区的压缩字节数;若发生错误或目标容量不足则返回 `0`。
///
/// # 安全性保证 (Safety)
/// * `src` 必须指向至少包含 `len` 个有效且内存对齐的 `f64` 元素。
/// * `dst` 必须指向至少具有 `dst_cap` 字节可写容量的有效内存区域。
/// * `src` 与 `dst` 指针不得重叠且不得为空。
pub unsafe extern "C"
/// Decompresses a byte buffer into an array of f64 floating-point values.
///
/// # Arguments
/// * `src` - Pointer to the compressed byte buffer.
/// * `src_len` - Length of the compressed byte buffer.
/// * `dst` - Pointer to the destination `f64` array.
/// * `dst_cap` - Maximum number of `f64` elements that `dst` can hold.
///
/// # Returns
/// The number of decompressed `f64` values written to `dst`, or `0` on error or insufficient capacity.
///
/// # Safety
/// * `src` must point to at least `src_len` readable bytes.
/// * `dst` must point to writable memory for at least `dst_cap` aligned `f64` elements.
/// * `src` and `dst` must not overlap or be null.
///
/// ---
///
/// 解压字节缓冲区至 f64 浮点数组。
///
/// # 参数
/// * `src` - 压缩字节缓冲区指针。
/// * `src_len` - 压缩字节缓冲区的有效长度。
/// * `dst` - 目标 `f64` 浮点数组指针。
/// * `dst_cap` - 目标数组最多可容纳的 `f64` 元素个数。
///
/// # 返回值
/// 实际解压出的 `f64` 浮点元素个数;若数据损坏、解析失败或目标容量不足则返回 `0`。
///
/// # 安全性保证 (Safety)
/// * `src` 必须指向至少 `src_len` 字节可读的有效内存。
/// * `dst` 必须指向至少可容纳 `dst_cap` 个内存对齐 `f64` 元素的可写内存。
/// * `src` 与 `dst` 指针不得重叠且不得为空。
pub unsafe extern "C"
/// Compresses an array of f32 floating-point values with dynamic parameter sampling.
///
/// # Arguments
/// * `src` - Pointer to the source `f32` array.
/// * `len` - Number of `f32` elements in the source array.
/// * `dst` - Pointer to the destination byte buffer.
/// * `dst_cap` - Capacity of the destination buffer in bytes.
///
/// # Returns
/// The number of compressed bytes written to `dst`, or `0` if an error occurs or `dst_cap` is insufficient.
///
/// # Safety
/// * `src` must point to at least `len` valid, aligned `f32` values.
/// * `dst` must point to writable memory of at least `dst_cap` bytes.
/// * `src` and `dst` must not overlap or be null.
///
/// ---
///
/// 压缩 f32 浮点数组(包含动态模型参数采样探测)。
///
/// # 参数
/// * `src` - 源 `f32` 浮点数组指针。
/// * `len` - 源数组中 `f32` 元素的个数。
/// * `dst` - 目标字节缓冲区指针。
/// * `dst_cap` - 目标字节缓冲区的最大容量。
///
/// # 返回值
/// 实际写入目标缓冲区的压缩字节数;若发生错误或目标容量不足则返回 `0`。
///
/// # 安全性保证 (Safety)
/// * `src` 必须指向至少包含 `len` 个有效且内存对齐的 `f32` 元素。
/// * `dst` 必须指向至少具有 `dst_cap` 字节可写容量的有效内存区域。
/// * `src` 与 `dst` 指针不得重叠且不得为空。
pub unsafe extern "C"
/// Compresses an array of f32 floating-point values by reusing cached parameters from the thread-local encoder.
/// Skips sampling overhead, suitable for high-throughput streaming pipelines of stationary data.
///
/// # Arguments
/// * `src` - Pointer to the source `f32` array.
/// * `len` - Number of `f32` elements in the source array.
/// * `dst` - Pointer to the destination byte buffer.
/// * `dst_cap` - Capacity of the destination buffer in bytes.
///
/// # Returns
/// The number of compressed bytes written to `dst`, or `0` if an error occurs or `dst_cap` is insufficient.
///
/// # Safety
/// * `src` must point to at least `len` valid, aligned `f32` values.
/// * `dst` must point to writable memory of at least `dst_cap` bytes.
/// * `src` and `dst` must not overlap or be null.
///
/// ---
///
/// 复用线程局部编码器已缓存的模型参数压缩 f32 浮点数组。
/// 跳过重复采样开销,直接执行核心编码内核,适用于平稳时序数据的高吞吐流式批量压缩。
///
/// # 参数
/// * `src` - 源 `f32` 浮点数组指针。
/// * `len` - 源数组中 `f32` 元素的个数。
/// * `dst` - 目标字节缓冲区指针。
/// * `dst_cap` - 目标字节缓冲区的最大容量。
///
/// # 返回值
/// 实际写入目标缓冲区的压缩字节数;若发生错误或目标容量不足则返回 `0`。
///
/// # 安全性保证 (Safety)
/// * `src` 必须指向至少包含 `len` 个有效且内存对齐的 `f32` 元素。
/// * `dst` 必须指向至少具有 `dst_cap` 字节可写容量的有效内存区域。
/// * `src` 与 `dst` 指针不得重叠且不得为空。
pub unsafe extern "C"
/// Decompresses a byte buffer into an array of f32 floating-point values.
///
/// # Arguments
/// * `src` - Pointer to the compressed byte buffer.
/// * `src_len` - Length of the compressed byte buffer.
/// * `dst` - Pointer to the destination `f32` array.
/// * `dst_cap` - Maximum number of `f32` elements that `dst` can hold.
///
/// # Returns
/// The number of decompressed `f32` values written to `dst`, or `0` on error or insufficient capacity.
///
/// # Safety
/// * `src` must point to at least `src_len` readable bytes.
/// * `dst` must point to writable memory for at least `dst_cap` aligned `f32` elements.
/// * `src` and `dst` must not overlap or be null.
///
/// ---
///
/// 解压字节缓冲区至 f32 浮点数组。
///
/// # 参数
/// * `src` - 压缩字节缓冲区指针。
/// * `src_len` - 压缩字节缓冲区的有效长度。
/// * `dst` - 目标 `f32` 浮点数组指针。
/// * `dst_cap` - 目标数组最多可容纳的 `f32` 元素个数。
///
/// # 返回值
/// 实际解压出的 `f32` 浮点元素个数;若数据损坏、解析失败或目标容量不足则返回 `0`。
///
/// # 安全性保证 (Safety)
/// * `src` 必须指向至少 `src_len` 字节可读的有效内存。
/// * `dst` 必须指向至少可容纳 `dst_cap` 个内存对齐 `f32` 元素的可写内存。
/// * `src` 与 `dst` 指针不得重叠且不得为空。
pub unsafe extern "C"
/// Opaque handle for a stateful double-precision (f64) encoder instance.
/// Useful when multiple encoders are needed across different threads or streams without TLS.
///
/// ---
///
/// 双精度 (f64) 状态化独立编码器句柄。
/// 适用于多线程、多流并发或不依赖 TLS 的场景。
/// Creates a new stateful f64 encoder handle on the heap.
/// Caller is responsible for releasing it using `fastalp_encoder_f64_free`.
///
/// # Returns
/// A pointer to the newly allocated encoder, or null on allocation failure.
///
/// ---
///
/// 在堆上创建新的状态化 f64 独立编码器实例句柄。
/// 调用方需负责调用 `fastalp_encoder_f64_free` 进行内存释放。
///
/// # 返回值
/// 指向新创建编码器的指针;若内存分配失败则返回空指针。
pub extern "C"
/// Frees a stateful f64 encoder instance created by `fastalp_encoder_f64_new`.
///
/// # Safety
/// `enc` must be a valid pointer returned by `fastalp_encoder_f64_new` that has not been freed.
///
/// ---
///
/// 释放由 `fastalp_encoder_f64_new` 创建的 f64 独立编码器实例。
///
/// # 安全性保证 (Safety)
/// `enc` 必须是由 `fastalp_encoder_f64_new` 返回且尚未被释放的有效指针。
pub unsafe extern "C"
/// Resets cached parameters in a stateful f64 encoder handle.
///
/// # Safety
/// `enc` must point to a valid `FastAlpEncoderF64` instance.
///
/// ---
///
/// 重置 f64 独立编码器句柄中的已缓存模型参数。
///
/// # 安全性保证 (Safety)
/// `enc` 必须指向有效的 `FastAlpEncoderF64` 实例。
pub unsafe extern "C"
/// Compresses an array of f64 values using a stateful encoder handle, reusing cached parameters.
///
/// # Arguments
/// * `enc` - Pointer to the `FastAlpEncoderF64` instance.
/// * `src` - Pointer to the source `f64` array.
/// * `len` - Number of `f64` values to compress.
/// * `dst` - Pointer to destination buffer.
/// * `dst_cap` - Capacity of destination buffer in bytes.
///
/// # Returns
/// Number of bytes written, or `0` on error or insufficient capacity.
///
/// # Safety
/// `enc`, `src`, and `dst` must be valid and non-null.
///
/// ---
///
/// 使用 f64 独立编码器句柄压缩浮点数组(复用已缓存模型参数)。
///
/// # 参数
/// * `enc` - 编码器句柄指针。
/// * `src` - 源 `f64` 浮点数组指针。
/// * `len` - 待压缩元素个数。
/// * `dst` - 目标字节缓冲区指针。
/// * `dst_cap` - 目标缓冲区字节容量。
///
/// # 返回值
/// 实际写入字节数;出错或容量不足时返回 `0`。
///
/// # 安全性保证 (Safety)
/// `enc`、`src` 与 `dst` 必须有效且非空。
pub unsafe extern "C"
/// Opaque handle for a stateful single-precision (f32) encoder instance.
/// Useful when multiple encoders are needed across different threads or streams without TLS.
///
/// ---
///
/// 单精度 (f32) 状态化独立编码器句柄。
/// 适用于多线程、多流并发或不依赖 TLS 的场景。
/// Creates a new stateful f32 encoder handle on the heap.
/// Caller is responsible for releasing it using `fastalp_encoder_f32_free`.
///
/// # Returns
/// A pointer to the newly allocated encoder, or null on allocation failure.
///
/// ---
///
/// 在堆上创建新的状态化 f32 独立编码器实例句柄。
/// 调用方需负责调用 `fastalp_encoder_f32_free` 进行内存释放。
///
/// # 返回值
/// 指向新创建编码器的指针;若内存分配失败则返回空指针。
pub extern "C"
/// Frees a stateful f32 encoder instance created by `fastalp_encoder_f32_new`.
///
/// # Safety
/// `enc` must be a valid pointer returned by `fastalp_encoder_f32_new` that has not been freed.
///
/// ---
///
/// 释放由 `fastalp_encoder_f32_new` 创建的 f32 独立编码器实例。
///
/// # 安全性保证 (Safety)
/// `enc` 必须是由 `fastalp_encoder_f32_new` 返回且尚未被释放的有效指针。
pub unsafe extern "C"
/// Resets cached parameters in a stateful f32 encoder handle.
///
/// # Safety
/// `enc` must point to a valid `FastAlpEncoderF32` instance.
///
/// ---
///
/// 重置 f32 独立编码器句柄中的已缓存模型参数。
///
/// # 安全性保证 (Safety)
/// `enc` 必须指向有效的 `FastAlpEncoderF32` 实例。
pub unsafe extern "C"
/// Compresses an array of f32 values using a stateful encoder handle, reusing cached parameters.
///
/// # Arguments
/// * `enc` - Pointer to the `FastAlpEncoderF32` instance.
/// * `src` - Pointer to the source `f32` array.
/// * `len` - Number of `f32` values to compress.
/// * `dst` - Pointer to destination buffer.
/// * `dst_cap` - Capacity of destination buffer in bytes.
///
/// # Returns
/// Number of bytes written, or `0` on error or insufficient capacity.
///
/// # Safety
/// `enc`, `src`, and `dst` must be valid and non-null.
///
/// ---
///
/// 使用 f32 独立编码器句柄压缩浮点数组(复用已缓存模型参数)。
///
/// # 参数
/// * `enc` - 编码器句柄指针。
/// * `src` - 源 `f32` 浮点数组指针。
/// * `len` - 待压缩元素个数。
/// * `dst` - 目标字节缓冲区指针。
/// * `dst_cap` - 目标缓冲区字节容量。
///
/// # 返回值
/// 实际写入字节数;出错或容量不足时返回 `0`。
///
/// # 安全性保证 (Safety)
/// `enc`、`src` 与 `dst` 必须有效且非空。
pub unsafe extern "C"