arcweight 0.3.0

A high-performance, modular library for weighted finite state transducers with comprehensive examples and benchmarks
Documentation
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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
//! Native binary format for FST serialization.
//!
//! This module provides efficient binary serialization and deserialization of FSTs
//! using Rust's native serialization capabilities via serde and bincode. The format
//! is optimized for speed and type safety, preserving all FST properties exactly.
//!
//! # Features
//!
//! - **Type-safe:** Preserves exact semiring types and properties
//! - **Versioned:** Format includes version information for forward compatibility
//! - **Efficient:** Uses compact binary representation via bincode
//! - **Complete:** Serializes all FST data including structure and weights
//! - **Streaming:** Single-pass I/O minimizes memory usage
//!
//! # Format Specification
//!
//! The binary format uses little-endian byte ordering and consists of:
//!
//! ```text
//! +----------------+----------------+
//! | Magic (4B)     | Version (4B)   |  Header
//! +----------------+----------------+
//! | Num States (4B)| Start State(4B)|  Metadata
//! +----------------+----------------+
//! | State 0 Data   | ...            |  States
//! +----------------+----------------+
//! ```
//!
//! Each state contains:
//! - Final weight flag (1 byte): 0 = non-final, 1 = final
//! - Final weight (if final): bincode-encoded semiring weight
//! - Arc count (4 bytes): number of outgoing arcs
//! - Arc data: ilabel, olabel, weight, nextstate for each arc
//!
//! ## Header Constants
//!
//! - **Magic number:** `0x46535442` ("FSTB" in ASCII)
//! - **Version:** `1` (current)
//!
//! # Requirements
//!
//! This module requires the `serde` feature:
//!
//! ```toml
//! [dependencies]
//! arcweight = { version = "0.3", features = ["serde"] }
//! ```
//!
//! The semiring weight type must implement `serde::Serialize` and
//! `serde::Deserialize`.
//!
//! # Examples
//!
//! ## Basic Serialization
//!
//! ```
//! # #[cfg(feature = "serde")]
//! # {
//! use arcweight::prelude::*;
//! use arcweight::io::{write_binary, read_binary};
//! use std::io::Cursor;
//!
//! # fn example() -> Result<()> {
//! // Create and populate FST
//! let mut fst = VectorFst::<TropicalWeight>::new();
//! let s0 = fst.add_state();
//! let s1 = fst.add_state();
//! fst.set_start(s0);
//! fst.set_final(s1, TropicalWeight::one());
//! fst.add_arc(s0, Arc::new(1, 2, TropicalWeight::new(0.5), s1));
//!
//! // Write to in-memory buffer
//! let mut buffer = Vec::new();
//! write_binary(&fst, &mut buffer)?;
//!
//! // Read back from buffer
//! let mut cursor = Cursor::new(buffer);
//! let loaded: VectorFst<TropicalWeight> = read_binary(&mut cursor)?;
//!
//! assert_eq!(fst.num_states(), loaded.num_states());
//! # Ok(())
//! # }
//! # }
//! ```
//!
//! ## File I/O
//!
//! ```no_run
//! # #[cfg(feature = "serde")]
//! # {
//! use arcweight::prelude::*;
//! use arcweight::io::{write_binary, read_binary};
//! use std::fs::File;
//! use std::io::{BufReader, BufWriter};
//!
//! # fn example() -> Result<()> {
//! let fst = VectorFst::<TropicalWeight>::new();
//!
//! // Write to file with buffering for performance
//! let file = File::create("model.fstb")?;
//! let mut writer = BufWriter::new(file);
//! write_binary(&fst, &mut writer)?;
//!
//! // Read from file with buffering
//! let file = File::open("model.fstb")?;
//! let mut reader = BufReader::new(file);
//! let loaded: VectorFst<TropicalWeight> = read_binary(&mut reader)?;
//! # Ok(())
//! # }
//! # }
//! ```
//!
//! # Performance
//!
//! | Operation | Time Complexity | Space Complexity |
//! |-----------|-----------------|------------------|
//! | Write     | O(\|V\| + \|E\|)    | O(1) streaming   |
//! | Read      | O(\|V\| + \|E\|)    | O(\|V\| + \|E\|) output |
//!
//! For optimal performance:
//! - Use `BufReader`/`BufWriter` for file I/O
//! - Pre-allocate buffers when serializing to `Vec<u8>`
//!
//! # Compatibility
//!
//! - Files are portable across platforms with the same endianness
//! - Version 1 format is stable; future versions will be backward compatible
//! - Not compatible with OpenFST binary format (use [`read_openfst`](super::read_openfst)/[`write_openfst`](super::write_openfst)
//!   for interoperability)

#[cfg(feature = "serde")]
mod inner {
    use crate::arc::Arc;
    use crate::fst::{Fst, MutableFst, StateId};
    use crate::semiring::Semiring;
    use crate::{Error, Result};
    use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
    use std::io::{Read, Write};

    const MAGIC: u32 = 0x4653_5442; // "FSTB"
    const VERSION: u32 = 1;

    /// Writes an FST to binary format.
    ///
    /// Serializes the FST structure including all states, arcs, and weights to a
    /// compact binary representation. The format is versioned and includes a magic
    /// number for validation during deserialization.
    ///
    /// # Type Parameters
    ///
    /// * `W` - The semiring weight type, must implement `Semiring + Serialize`
    /// * `F` - The FST type, must implement `Fst<W>`
    /// * `Writer` - The output writer type, must implement `Write`
    ///
    /// # Arguments
    ///
    /// * `fst` - Reference to the FST to serialize
    /// * `writer` - Mutable reference to the output writer
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` on successful serialization.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Io`] if the writer encounters an I/O error.
    ///
    /// Returns [`Error::Serialization`] if weight
    /// serialization fails (e.g., for custom weight types with invalid data).
    ///
    /// # Complexity
    ///
    /// - **Time:** O(|V| + |E|) where |V| is the number of states and |E| is
    ///   the number of arcs
    /// - **Space:** O(1) additional space (streaming write)
    ///
    /// # Examples
    ///
    /// ```
    /// # #[cfg(feature = "serde")]
    /// # {
    /// use arcweight::prelude::*;
    /// use arcweight::io::write_binary;
    ///
    /// # fn example() -> Result<()> {
    /// let mut fst = VectorFst::<TropicalWeight>::new();
    /// let s0 = fst.add_state();
    /// fst.set_start(s0);
    /// fst.set_final(s0, TropicalWeight::one());
    ///
    /// let mut buffer = Vec::new();
    /// write_binary(&fst, &mut buffer)?;
    /// assert!(buffer.len() > 0);
    /// # Ok(())
    /// # }
    /// # }
    /// ```
    pub fn write_binary<W, F, Writer>(fst: &F, writer: &mut Writer) -> Result<()>
    where
        W: Semiring + serde::Serialize,
        F: Fst<W>,
        Writer: Write,
    {
        // write header
        writer.write_u32::<LittleEndian>(MAGIC)?;
        writer.write_u32::<LittleEndian>(VERSION)?;

        // write basic info
        writer.write_u32::<LittleEndian>(fst.num_states() as u32)?;
        writer.write_u32::<LittleEndian>(fst.start().unwrap_or(u32::MAX))?;

        // write states
        for state in fst.states() {
            // final weight
            if let Some(weight) = fst.final_weight(state) {
                writer.write_u8(1)?;
                write_weight(writer, weight)?;
            } else {
                writer.write_u8(0)?;
            }

            // arcs
            let num_arcs = fst.num_arcs(state);
            writer.write_u32::<LittleEndian>(num_arcs as u32)?;

            for arc in fst.arcs(state) {
                writer.write_u32::<LittleEndian>(arc.ilabel)?;
                writer.write_u32::<LittleEndian>(arc.olabel)?;
                write_weight(writer, &arc.weight)?;
                writer.write_u32::<LittleEndian>(arc.nextstate)?;
            }
        }

        Ok(())
    }

    /// Reads an FST from binary format.
    ///
    /// Deserializes an FST from the native binary format, reconstructing the
    /// complete FST structure including all states, arcs, and weights. Validates
    /// the magic number and version before parsing.
    ///
    /// # Type Parameters
    ///
    /// * `W` - The semiring weight type, must implement `Semiring + DeserializeOwned`
    /// * `M` - The mutable FST type to construct, must implement `MutableFst<W> + Default`
    /// * `Reader` - The input reader type, must implement `Read`
    ///
    /// # Arguments
    ///
    /// * `reader` - Mutable reference to the input reader
    ///
    /// # Returns
    ///
    /// Returns the deserialized FST on success.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Serialization`] if:
    /// - The magic number is invalid (not `0x46535442`)
    /// - The format version is unsupported
    /// - Weight deserialization fails
    ///
    /// Returns [`Error::Io`] if the reader encounters an I/O error.
    ///
    /// # Complexity
    ///
    /// - **Time:** O(|V| + |E|) where |V| is the number of states and |E| is
    ///   the number of arcs
    /// - **Space:** O(|V| + |E|) for the output FST
    ///
    /// # Correctness
    ///
    /// This function guarantees round-trip correctness:
    /// - `deserialize(serialize(fst)) == fst`
    /// - State IDs are preserved (created in order 0..n-1)
    /// - All weights are preserved exactly
    /// - Language is preserved: L(read(write(T))) = L(T)
    ///
    /// # Examples
    ///
    /// ```
    /// # #[cfg(feature = "serde")]
    /// # {
    /// use arcweight::prelude::*;
    /// use arcweight::io::{write_binary, read_binary};
    /// use std::io::Cursor;
    ///
    /// # fn example() -> Result<()> {
    /// let mut original = VectorFst::<TropicalWeight>::new();
    /// let s0 = original.add_state();
    /// original.set_start(s0);
    ///
    /// let mut buffer = Vec::new();
    /// write_binary(&original, &mut buffer)?;
    ///
    /// let mut cursor = Cursor::new(buffer);
    /// let loaded: VectorFst<TropicalWeight> = read_binary(&mut cursor)?;
    ///
    /// assert_eq!(original.num_states(), loaded.num_states());
    /// # Ok(())
    /// # }
    /// # }
    /// ```
    pub fn read_binary<W, M, Reader>(reader: &mut Reader) -> Result<M>
    where
        W: Semiring + serde::de::DeserializeOwned,
        M: MutableFst<W> + Default,
        Reader: Read,
    {
        // read header
        let magic = reader.read_u32::<LittleEndian>()?;
        if magic != MAGIC {
            return Err(Error::Serialization("Invalid magic number".into()));
        }

        let version = reader.read_u32::<LittleEndian>()?;
        if version != VERSION {
            return Err(Error::Serialization("Unsupported version".into()));
        }

        // read basic info
        let num_states = reader.read_u32::<LittleEndian>()? as usize;
        let start = reader.read_u32::<LittleEndian>()?;

        let mut fst = M::default();

        // create states
        for _ in 0..num_states {
            fst.add_state();
        }

        // set start
        if start != u32::MAX {
            fst.set_start(start);
        }

        // read states
        for state in 0..num_states {
            // final weight
            if reader.read_u8()? == 1 {
                let weight = read_weight(reader)?;
                fst.set_final(state as StateId, weight);
            }

            // arcs
            let num_arcs = reader.read_u32::<LittleEndian>()? as usize;
            for _ in 0..num_arcs {
                let ilabel = reader.read_u32::<LittleEndian>()?;
                let olabel = reader.read_u32::<LittleEndian>()?;
                let weight = read_weight(reader)?;
                let nextstate = reader.read_u32::<LittleEndian>()?;

                fst.add_arc(
                    state as StateId,
                    Arc::new(ilabel, olabel, weight, nextstate),
                );
            }
        }

        Ok(fst)
    }

    fn write_weight<W: Semiring + serde::Serialize>(
        writer: &mut impl Write,
        weight: &W,
    ) -> Result<()> {
        // this would need specialization per weight type
        let bytes = bincode::serde::encode_to_vec(weight, bincode::config::legacy())
            .map_err(|e| Error::Serialization(e.to_string()))?;
        writer.write_u32::<LittleEndian>(bytes.len() as u32)?;
        writer.write_all(&bytes)?;
        Ok(())
    }

    fn read_weight<W: Semiring + serde::de::DeserializeOwned>(reader: &mut impl Read) -> Result<W> {
        let len = reader.read_u32::<LittleEndian>()? as usize;
        let mut bytes = vec![0u8; len];
        reader.read_exact(&mut bytes)?;
        let (weight, _) = bincode::serde::decode_from_slice(&bytes, bincode::config::legacy())
            .map_err(|e| Error::Serialization(e.to_string()))?;
        Ok(weight)
    }
}

#[cfg(feature = "serde")]
pub use inner::{read_binary, write_binary};

#[cfg(all(test, feature = "serde"))]
mod tests {
    use super::*;
    use crate::prelude::*;
    use num_traits::One;
    use std::io::Cursor;

    #[test]
    fn test_write_read_binary_roundtrip() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();
        let s2 = fst.add_state();

        fst.set_start(s0);
        fst.set_final(s2, TropicalWeight::new(std::f32::consts::PI));

        fst.add_arc(s0, Arc::new(10, 20, TropicalWeight::new(2.5), s1));
        fst.add_arc(s1, Arc::new(30, 40, TropicalWeight::new(1.2), s2));
        fst.add_arc(s0, Arc::epsilon(TropicalWeight::new(0.1), s2));

        // Write to buffer
        let mut buffer = Vec::new();
        write_binary(&fst, &mut buffer).unwrap();

        // Read back from buffer
        let mut cursor = Cursor::new(buffer);
        let read_fst: VectorFst<TropicalWeight> =
            read_binary::<TropicalWeight, VectorFst<TropicalWeight>, _>(&mut cursor).unwrap();

        // Verify structure is preserved
        assert_eq!(read_fst.num_states(), fst.num_states());
        assert_eq!(read_fst.start(), fst.start());
        assert_eq!(read_fst.num_arcs_total(), fst.num_arcs_total());

        // Check final weights
        for state in fst.states() {
            let original_final = fst.final_weight(state);
            let read_final = read_fst.final_weight(state);

            match (original_final, read_final) {
                (Some(w1), Some(w2)) => assert_eq!(w1, w2),
                (None, None) => {}
                _ => panic!("Final weight mismatch for state {state}"),
            }
        }

        // Check arcs
        for state in fst.states() {
            let original_arcs: Vec<_> = fst.arcs(state).collect();
            let read_arcs: Vec<_> = read_fst.arcs(state).collect();

            assert_eq!(original_arcs.len(), read_arcs.len());

            for (orig, read) in original_arcs.iter().zip(read_arcs.iter()) {
                assert_eq!(orig.ilabel, read.ilabel);
                assert_eq!(orig.olabel, read.olabel);
                assert_eq!(orig.weight, read.weight);
                assert_eq!(orig.nextstate, read.nextstate);
            }
        }
    }

    #[test]
    fn test_binary_empty_fst() {
        let fst = VectorFst::<TropicalWeight>::new();

        let mut buffer = Vec::new();
        write_binary(&fst, &mut buffer).unwrap();

        let mut cursor = Cursor::new(buffer);
        let read_fst: VectorFst<TropicalWeight> =
            read_binary::<TropicalWeight, VectorFst<TropicalWeight>, _>(&mut cursor).unwrap();

        assert!(read_fst.is_empty());
        assert_eq!(read_fst.num_states(), 0);
        assert_eq!(read_fst.start(), None);
    }

    #[test]
    fn test_binary_format_efficiency() {
        let mut fst = VectorFst::<TropicalWeight>::new();

        // Create a larger FST to test efficiency
        for _i in 0..100 {
            fst.add_state();
        }

        fst.set_start(0);
        fst.set_final(99, TropicalWeight::one());

        for i in 0..99 {
            fst.add_arc(
                i,
                Arc::new(i + 1, i + 1, TropicalWeight::new(i as f32 * 0.1), i + 1),
            );
        }

        let mut binary_buffer = Vec::new();
        let mut text_buffer = Vec::new();

        write_binary(&fst, &mut binary_buffer).unwrap();
        crate::io::write_text(&fst, &mut text_buffer, None, None).unwrap();

        // Binary format should typically be more compact
        // (though this is not guaranteed for all cases)
        println!(
            "Binary size: {}, Text size: {}",
            binary_buffer.len(),
            text_buffer.len()
        );

        // Verify binary can be read back correctly
        let mut cursor = Cursor::new(binary_buffer);
        let read_fst: VectorFst<TropicalWeight> =
            read_binary::<TropicalWeight, VectorFst<TropicalWeight>, _>(&mut cursor).unwrap();

        assert_eq!(read_fst.num_states(), fst.num_states());
        assert_eq!(read_fst.num_arcs_total(), fst.num_arcs_total());
    }

    #[test]
    fn test_binary_format_single_state() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let s0 = fst.add_state();
        fst.set_start(s0);
        fst.set_final(s0, TropicalWeight::new(42.0));

        let mut buffer = Vec::new();
        write_binary(&fst, &mut buffer).unwrap();

        let mut cursor = Cursor::new(buffer);
        let read_fst: VectorFst<TropicalWeight> =
            read_binary::<TropicalWeight, VectorFst<TropicalWeight>, _>(&mut cursor).unwrap();

        assert_eq!(read_fst.num_states(), 1);
        assert_eq!(read_fst.start(), Some(s0));
        assert_eq!(read_fst.final_weight(s0), Some(&TropicalWeight::new(42.0)));
        assert_eq!(read_fst.num_arcs_total(), 0);
    }

    #[test]
    fn test_binary_format_self_loop() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let s0 = fst.add_state();
        fst.set_start(s0);
        fst.set_final(s0, TropicalWeight::one());
        fst.add_arc(s0, Arc::new(1, 1, TropicalWeight::new(0.5), s0)); // Self-loop

        let mut buffer = Vec::new();
        write_binary(&fst, &mut buffer).unwrap();

        let mut cursor = Cursor::new(buffer);
        let read_fst: VectorFst<TropicalWeight> =
            read_binary::<TropicalWeight, VectorFst<TropicalWeight>, _>(&mut cursor).unwrap();

        assert_eq!(read_fst.num_states(), 1);
        assert_eq!(read_fst.num_arcs_total(), 1);

        let arcs: Vec<_> = read_fst.arcs(s0).collect();
        assert_eq!(arcs.len(), 1);
        assert_eq!(arcs[0].nextstate, s0);
        assert_eq!(arcs[0].weight, TropicalWeight::new(0.5));
    }

    #[test]
    fn test_binary_format_no_start_state() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();
        fst.set_final(s1, TropicalWeight::one());
        fst.add_arc(s0, Arc::new(1, 1, TropicalWeight::one(), s1));
        // No start state set

        let mut buffer = Vec::new();
        write_binary(&fst, &mut buffer).unwrap();

        let mut cursor = Cursor::new(buffer);
        let read_fst: VectorFst<TropicalWeight> =
            read_binary::<TropicalWeight, VectorFst<TropicalWeight>, _>(&mut cursor).unwrap();

        assert_eq!(read_fst.num_states(), 2);
        assert_eq!(read_fst.start(), None);
        assert_eq!(read_fst.num_arcs_total(), 1);
    }

    #[test]
    fn test_binary_format_multiple_arcs_from_state() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();
        let s2 = fst.add_state();

        fst.set_start(s0);
        fst.set_final(s1, TropicalWeight::one());
        fst.set_final(s2, TropicalWeight::one());

        // Multiple arcs from s0
        fst.add_arc(s0, Arc::new(1, 1, TropicalWeight::new(1.0), s1));
        fst.add_arc(s0, Arc::new(2, 2, TropicalWeight::new(2.0), s2));
        fst.add_arc(s0, Arc::new(3, 3, TropicalWeight::new(3.0), s1));

        let mut buffer = Vec::new();
        write_binary(&fst, &mut buffer).unwrap();

        let mut cursor = Cursor::new(buffer);
        let read_fst: VectorFst<TropicalWeight> =
            read_binary::<TropicalWeight, VectorFst<TropicalWeight>, _>(&mut cursor).unwrap();

        assert_eq!(read_fst.num_states(), 3);
        assert_eq!(read_fst.num_arcs_total(), 3);
        assert_eq!(read_fst.num_arcs(s0), 3);

        let arcs: Vec<_> = read_fst.arcs(s0).collect();
        assert_eq!(arcs.len(), 3);

        // Check arc labels and weights are preserved
        let labels: Vec<u32> = arcs.iter().map(|arc| arc.ilabel).collect();
        assert!(labels.contains(&1));
        assert!(labels.contains(&2));
        assert!(labels.contains(&3));
    }

    #[test]
    fn test_binary_format_epsilon_arcs() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();

        fst.set_start(s0);
        fst.set_final(s1, TropicalWeight::one());
        fst.add_arc(s0, Arc::epsilon(TropicalWeight::new(0.1), s1));

        let mut buffer = Vec::new();
        write_binary(&fst, &mut buffer).unwrap();

        let mut cursor = Cursor::new(buffer);
        let read_fst: VectorFst<TropicalWeight> =
            read_binary::<TropicalWeight, VectorFst<TropicalWeight>, _>(&mut cursor).unwrap();

        let arcs: Vec<_> = read_fst.arcs(s0).collect();
        assert_eq!(arcs.len(), 1);
        assert_eq!(arcs[0].ilabel, 0);
        assert_eq!(arcs[0].olabel, 0);
        assert_eq!(arcs[0].weight, TropicalWeight::new(0.1));
    }

    #[test]
    fn test_binary_format_different_semirings() {
        // Test LogWeight
        let mut log_fst = VectorFst::<LogWeight>::new();
        let s0 = log_fst.add_state();
        let s1 = log_fst.add_state();

        log_fst.set_start(s0);
        log_fst.set_final(s1, LogWeight::new(std::f64::consts::E));
        log_fst.add_arc(s0, Arc::new(1, 2, LogWeight::new(1.414), s1));

        let mut buffer = Vec::new();
        write_binary(&log_fst, &mut buffer).unwrap();

        let mut cursor = Cursor::new(buffer);
        let read_fst: VectorFst<LogWeight> =
            read_binary::<LogWeight, VectorFst<LogWeight>, _>(&mut cursor).unwrap();

        assert_eq!(
            read_fst.final_weight(s1),
            Some(&LogWeight::new(std::f64::consts::E))
        );
        let arcs: Vec<_> = read_fst.arcs(s0).collect();
        assert_eq!(arcs[0].weight, LogWeight::new(1.414));

        // Test BooleanWeight
        let mut bool_fst = VectorFst::<BooleanWeight>::new();
        let s0 = bool_fst.add_state();
        let s1 = bool_fst.add_state();

        bool_fst.set_start(s0);
        bool_fst.set_final(s1, BooleanWeight::one());
        bool_fst.add_arc(s0, Arc::new(1, 1, BooleanWeight::zero(), s1));

        let mut buffer = Vec::new();
        write_binary(&bool_fst, &mut buffer).unwrap();

        let mut cursor = Cursor::new(buffer);
        let read_fst: VectorFst<BooleanWeight> =
            read_binary::<BooleanWeight, VectorFst<BooleanWeight>, _>(&mut cursor).unwrap();

        assert_eq!(read_fst.final_weight(s1), Some(&BooleanWeight::one()));
        let arcs: Vec<_> = read_fst.arcs(s0).collect();
        assert_eq!(arcs[0].weight, BooleanWeight::zero());
    }

    #[test]
    fn test_binary_format_large_labels() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();

        fst.set_start(s0);
        fst.set_final(s1, TropicalWeight::one());
        fst.add_arc(
            s0,
            Arc::new(u32::MAX - 1, u32::MAX, TropicalWeight::new(1000.0), s1),
        );

        let mut buffer = Vec::new();
        write_binary(&fst, &mut buffer).unwrap();

        let mut cursor = Cursor::new(buffer);
        let read_fst: VectorFst<TropicalWeight> =
            read_binary::<TropicalWeight, VectorFst<TropicalWeight>, _>(&mut cursor).unwrap();

        let arcs: Vec<_> = read_fst.arcs(s0).collect();
        assert_eq!(arcs.len(), 1);
        assert_eq!(arcs[0].ilabel, u32::MAX - 1);
        assert_eq!(arcs[0].olabel, u32::MAX);
        assert_eq!(arcs[0].weight, TropicalWeight::new(1000.0));
    }

    #[test]
    fn test_binary_format_large_finite_weights() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let s0 = fst.add_state();
        let s1 = fst.add_state();
        let s2 = fst.add_state();

        fst.set_start(s0);
        fst.set_final(s1, TropicalWeight::new(1_000_000.0)); // Large but finite weight
        fst.set_final(s2, TropicalWeight::one()); // Zero

        fst.add_arc(s0, Arc::new(1, 1, TropicalWeight::new(-1000.0), s1));
        fst.add_arc(s0, Arc::new(2, 2, TropicalWeight::new(1000.0), s2));

        let mut buffer = Vec::new();
        write_binary(&fst, &mut buffer).unwrap();

        let mut cursor = Cursor::new(buffer);
        let read_fst: VectorFst<TropicalWeight> =
            read_binary::<TropicalWeight, VectorFst<TropicalWeight>, _>(&mut cursor).unwrap();

        assert_eq!(
            read_fst.final_weight(s1),
            Some(&TropicalWeight::new(1_000_000.0))
        );
        assert_eq!(read_fst.final_weight(s2), Some(&TropicalWeight::one()));

        let arcs: Vec<_> = read_fst.arcs(s0).collect();
        assert_eq!(arcs.len(), 2);

        let arc1 = arcs.iter().find(|arc| arc.ilabel == 1).unwrap();
        let arc2 = arcs.iter().find(|arc| arc.ilabel == 2).unwrap();

        assert_eq!(arc1.weight, TropicalWeight::new(-1000.0));
        assert_eq!(arc2.weight, TropicalWeight::new(1000.0));
    }

    #[test]
    fn test_binary_format_linear_chain() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let states: Vec<_> = (0..5).map(|_| fst.add_state()).collect();

        fst.set_start(states[0]);
        fst.set_final(states[4], TropicalWeight::new(10.0));

        // Create linear chain: 0 -> 1 -> 2 -> 3 -> 4
        for i in 0..4 {
            fst.add_arc(
                states[i],
                Arc::new(
                    (i + 1) as u32,
                    (i + 1) as u32,
                    TropicalWeight::new((i + 1) as f32),
                    states[i + 1],
                ),
            );
        }

        let mut buffer = Vec::new();
        write_binary(&fst, &mut buffer).unwrap();

        let mut cursor = Cursor::new(buffer);
        let read_fst: VectorFst<TropicalWeight> =
            read_binary::<TropicalWeight, VectorFst<TropicalWeight>, _>(&mut cursor).unwrap();

        assert_eq!(read_fst.num_states(), 5);
        assert_eq!(read_fst.start(), Some(states[0]));
        assert_eq!(
            read_fst.final_weight(states[4]),
            Some(&TropicalWeight::new(10.0))
        );

        // Verify chain structure
        for i in 0..4 {
            let arcs: Vec<_> = read_fst.arcs(states[i]).collect();
            assert_eq!(arcs.len(), 1);
            assert_eq!(arcs[0].ilabel, (i + 1) as u32);
            assert_eq!(arcs[0].nextstate, states[i + 1]);
            assert_eq!(arcs[0].weight, TropicalWeight::new((i + 1) as f32));
        }
    }

    #[test]
    fn test_binary_format_complex_weights() {
        let mut fst = VectorFst::<TropicalWeight>::new();
        let s0 = fst.add_state();
        let _s1 = fst.add_state();

        fst.set_start(s0);

        // Test various weight values
        let weights = [
            0.0,
            1.0,
            -1.0,
            std::f32::consts::PI,
            std::f32::consts::E,
            0.123_456_8,
            1e-10,
            1e10,
            -1e-10,
            -1e10,
        ];

        for (i, &weight_val) in weights.iter().enumerate() {
            let state = fst.add_state();
            fst.set_final(state, TropicalWeight::new(weight_val));
            fst.add_arc(
                s0,
                Arc::new(i as u32, i as u32, TropicalWeight::new(weight_val), state),
            );
        }

        let mut buffer = Vec::new();
        write_binary(&fst, &mut buffer).unwrap();

        let mut cursor = Cursor::new(buffer);
        let read_fst: VectorFst<TropicalWeight> =
            read_binary::<TropicalWeight, VectorFst<TropicalWeight>, _>(&mut cursor).unwrap();

        // Verify all weights are preserved
        let arcs: Vec<_> = read_fst.arcs(s0).collect();
        for (i, &expected_weight) in weights.iter().enumerate() {
            let arc = arcs.iter().find(|arc| arc.ilabel == i as u32).unwrap();
            assert_eq!(arc.weight, TropicalWeight::new(expected_weight));

            let final_weight = read_fst.final_weight(arc.nextstate).unwrap();
            assert_eq!(*final_weight, TropicalWeight::new(expected_weight));
        }
    }
}