sicada 0.1.0

A weighted finite-state transducer (WFST) library, file-compatible with OpenFst
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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
//! An immutable FST held in two contiguous blocks.
//!
//! Port of OpenFst's `const-fst.h`. The states and the arcs each live in one
//! block, which lets a file be mapped rather than parsed: the bytes on disk are
//! the bytes in memory. The layout of what goes in those blocks is
//! therefore part of the file format; see [`ConstState`] and
//! [`ArcTpl`](crate::arc::ArcTpl).

use std::fmt::Debug;
use std::fs::File;
use std::io::{Read, Seek, Write};
use std::marker::PhantomData;
use std::path::Path;

use crate::AtomicRc;
use crate::algorithms::test_properties::{cached_properties, check_properties};
use crate::arc::{Arc, ArcLabel, ArcStateId};
use crate::error::OpenFstError;
use crate::fst::{
    ContiguousArcsFst, ExpandedFst, FileReadMode, Fst, FstReadOptions, FstWriteOptions,
    NO_STATE_ID, PropertyCache, read_fst_header, write_fst_header,
};
use crate::fst_header::{FstHeader, flags};
use crate::fst_type::FstType;
use crate::fsts::compact_fst::Unsigned;
use crate::properties::{
    K_COPY_PROPERTIES, K_EXPANDED, K_MUTABLE, K_UNWEIGHTED_CYCLES, K_WEIGHTED_CYCLES,
};
use crate::symbol_table::SymbolTable;
use crate::utils::io::{CountingWriter, align_input};
use crate::utils::mapped_file::{ARCH_ALIGNMENT, MappedFile};
use crate::weight::Weight as _;

/// The properties every `ConstFst` has, whatever it holds.
const K_STATIC_PROPERTIES: u64 = K_EXPANDED;

/// Internal representation of a state in a ConstFst.
/// Note: Must be `#[repr(C)]` because it's directly mapped from byte arrays.
#[repr(C)]
#[derive(Clone, Debug, PartialEq)]
pub struct ConstState<W, U> {
    pub final_weight: W,
    pub pos: U,
    pub narcs: U,
    pub niepsilons: U,
    pub noepsilons: U,
}

impl<W: crate::weight::Weight, U: Unsigned> Default for ConstState<W, U> {
    #[inline(always)]
    fn default() -> Self {
        Self {
            final_weight: W::zero(),
            pos: U::default(),
            narcs: U::default(),
            niepsilons: U::default(),
            noepsilons: U::default(),
        }
    }
}

/// Simple concrete immutable FST whose states and arcs are each stored in single contiguous memory regions.
/// Backed by `MappedFile` to allow true zero-copy loading from disk.
pub struct ConstFst<'a, A: Arc, U: Unsigned> {
    states_region: MappedFile<'a>,
    arcs_region: MappedFile<'a>,
    nstates: usize,
    start: Option<A::StateId>,
    properties: PropertyCache,
    input_symbols: Option<AtomicRc<SymbolTable>>,
    output_symbols: Option<AtomicRc<SymbolTable>>,
    _marker: PhantomData<(A, U)>,
}

impl<'a, A: Arc, U: Unsigned> ConstFst<'a, A, U> {
    /// Creates a ConstFst from any generic Fst by fully expanding and copying its states and arcs.
    pub fn from_fst<F: Fst<A>>(fst: &F) -> Result<Self, OpenFstError> {
        let mut nstates = 0;
        let mut narcs = 0;

        for s in fst.states() {
            nstates += 1;
            narcs += fst.num_arcs(s);
        }

        let mut states_region = MappedFile::allocate_type::<ConstState<A::Weight, U>>(nstates)?;
        let mut arcs_region = MappedFile::allocate_type::<A>(narcs)?;

        if nstates > 0 {
            let states_slice = unsafe {
                std::slice::from_raw_parts_mut(
                    states_region.as_mut_slice().unwrap().as_mut_ptr()
                        as *mut ConstState<A::Weight, U>,
                    nstates,
                )
            };

            let arcs_slice = if narcs > 0 {
                unsafe {
                    std::slice::from_raw_parts_mut(
                        arcs_region.as_mut_slice().unwrap().as_mut_ptr() as *mut A,
                        narcs,
                    )
                }
            } else {
                &mut []
            };

            let mut pos = 0;
            for (s_idx, s) in fst.states().enumerate() {
                let final_weight = fst.final_weight(s);
                let mut narcs_state = 0;
                let mut niepsilons = 0;
                let mut noepsilons = 0;

                for arc in fst.arcs(s) {
                    if arc.ilabel() == A::Label::epsilon() {
                        niepsilons += 1;
                    }
                    if arc.olabel() == A::Label::epsilon() {
                        noepsilons += 1;
                    }
                    arcs_slice[pos] = arc;
                    pos += 1;
                    narcs_state += 1;
                }

                states_slice[s_idx] = ConstState {
                    final_weight,
                    pos: U::from_usize(pos - narcs_state),
                    narcs: U::from_usize(narcs_state),
                    niepsilons: U::from_usize(niepsilons),
                    noepsilons: U::from_usize(noepsilons),
                };
            }
        }

        // A mutable FST keeps its bits up to date, so asking it settles them.
        // An immutable one may have come from a file written before the cycle
        // properties existed, so those are left out of what must already be
        // known, but stay in what a scan, if one happens, will settle.
        let properties = if (fst.properties(K_MUTABLE, false) & K_MUTABLE) != 0 {
            fst.properties(K_COPY_PROPERTIES, true)
        } else {
            check_properties(
                fst,
                K_COPY_PROPERTIES & !K_WEIGHTED_CYCLES & !K_UNWEIGHTED_CYCLES,
                K_COPY_PROPERTIES,
                false,
            )
        };

        Ok(Self {
            states_region,
            arcs_region,
            nstates,
            start: fst.start(),
            properties: PropertyCache::new(properties | K_STATIC_PROPERTIES),
            input_symbols: fst.input_symbols(),
            output_symbols: fst.output_symbols(),
            _marker: PhantomData,
        })
    }

    #[inline(always)]
    fn states_slice(&self) -> &[ConstState<A::Weight, U>] {
        if self.nstates == 0 {
            return &[];
        }
        unsafe {
            std::slice::from_raw_parts(
                self.states_region.as_ref().as_ptr() as *const ConstState<A::Weight, U>,
                self.nstates,
            )
        }
    }

    #[inline(always)]
    fn arcs_slice_internal(&self) -> &[A] {
        let size_in_arcs = self.arcs_region.as_ref().len() / std::mem::size_of::<A>();
        if size_in_arcs == 0 {
            return &[];
        }
        unsafe {
            std::slice::from_raw_parts(self.arcs_region.as_ref().as_ptr() as *const A, size_in_arcs)
        }
    }
}

/// Version written for a file whose regions are aligned.
///
/// Upstream numbers these the other way round to how they read: version 1 is
/// the *aligned* layout and version 2 the unaligned one, because alignment came
/// first and was later made optional.
const ALIGNED_FILE_VERSION: i32 = 1;
/// Version written for a file whose regions are not aligned.
const FILE_VERSION: i32 = 2;
/// The oldest version that can still be read.
const MIN_FILE_VERSION: i32 = 1;
/// Refuses a header claiming more states than could be stored, before that
/// count is used to size anything.
const MAX_STATES: i64 = 0x0010_0000_0000_0000;
/// As [`MAX_STATES`], for arcs.
const MAX_ARCS: i64 = 0x0010_0000_0000_0000;

impl<A: Arc, U: Unsigned> ConstFst<'static, A, U> {
    /// Reads a `ConstFst` from a stream.
    ///
    /// The regions are read into memory. Use
    /// [`read_from_file`](Self::read_from_file) to map them instead.
    pub fn read<R: Read + Seek>(
        reader: &mut R,
        opts: &FstReadOptions,
    ) -> Result<Self, OpenFstError> {
        Self::read_regions(reader, opts, |reader, aligned, size| {
            if aligned {
                align_input(reader, ARCH_ALIGNMENT as u64)?;
            }
            let mut region = MappedFile::allocate(size, ARCH_ALIGNMENT)?;
            reader.read_exact(
                region
                    .as_mut_slice()
                    .expect("a freshly allocated region is writable"),
            )?;
            Ok(region)
        })
    }

    /// Reads a `ConstFst` from a file, mapping its regions where it can.
    ///
    /// Mapping is advisory: a region that does not begin on an aligned offset
    /// cannot be viewed as the packed structures stored here, so it is read
    /// instead. That is why a file can be written aligned at all.
    pub fn read_from_file(
        path: impl AsRef<Path>,
        opts: &FstReadOptions,
    ) -> Result<Self, OpenFstError> {
        let mut file = File::open(path.as_ref())?;
        let opts = FstReadOptions {
            source: path.as_ref().display().to_string(),
            ..opts.clone()
        };
        let memorymap = opts.mode == FileReadMode::Map;
        Self::read_regions(&mut file, &opts, |file, aligned, size| {
            if aligned {
                align_input(file, ARCH_ALIGNMENT as u64)?;
            }
            Ok(MappedFile::map_or_read(file, memorymap, size)?)
        })
    }

    /// The shared body of the two readers: everything but how a region is
    /// obtained.
    fn read_regions<R, F>(
        reader: &mut R,
        opts: &FstReadOptions,
        mut region: F,
    ) -> Result<Self, OpenFstError>
    where
        R: Read + Seek,
        F: FnMut(&mut R, bool, usize) -> Result<MappedFile<'static>, OpenFstError>,
    {
        let read = read_fst_header::<A, _>(
            reader,
            opts,
            Self::fst_type_name().as_str(),
            MIN_FILE_VERSION,
        )?;
        let header = read.header;

        let bad =
            |message: String| OpenFstError::InvalidFstHeader(format!("{}: {message}", opts.source));
        if header.num_states < 0 || header.num_states > MAX_STATES {
            return Err(bad(format!(
                "invalid number of states: {}",
                header.num_states
            )));
        }
        if header.num_arcs < 0 || header.num_arcs > MAX_ARCS {
            return Err(bad(format!("invalid number of arcs: {}", header.num_arcs)));
        }
        let nstates = header.num_states as usize;
        let narcs = header.num_arcs as usize;

        // Version 1 predates the flag, and was always aligned.
        let aligned =
            header.version == ALIGNED_FILE_VERSION || header.flags & flags::IS_ALIGNED != 0;

        let states_region = region(
            reader,
            aligned,
            nstates * std::mem::size_of::<ConstState<A::Weight, U>>(),
        )?;
        let arcs_region = region(reader, aligned, narcs * std::mem::size_of::<A>())?;

        let start = if header.start == NO_STATE_ID as i64 {
            None
        } else {
            if header.start < 0 || header.start >= header.num_states {
                return Err(bad(format!(
                    "invalid start state {} for an FST with {nstates} states",
                    header.start
                )));
            }
            Some(A::StateId::from_usize(header.start as usize))
        };

        let fst = Self {
            states_region,
            arcs_region,
            nstates,
            start,
            properties: PropertyCache::new(header.properties | K_EXPANDED),
            input_symbols: read.isymbols,
            output_symbols: read.osymbols,
            _marker: PhantomData,
        };

        // Each state names a range of the arc array; a range reaching past it
        // would be read out of bounds by every later access.
        for (s, state) in fst.states_slice().iter().enumerate() {
            let pos = state.pos.as_usize();
            let narcs_s = state.narcs.as_usize();
            if pos > narcs || narcs_s > narcs - pos {
                return Err(bad(format!("state {s} arc range out of bounds")));
            }
        }

        if opts.verify {
            for arc in fst.arcs_slice_internal() {
                let nextstate = arc.nextstate();
                if nextstate == A::StateId::no_state() {
                    return Err(bad("disallowed next state".to_string()));
                }
                if nextstate < A::StateId::from_usize(0) {
                    return Err(bad("next state is negative".to_string()));
                }
                if nextstate.as_usize() >= nstates {
                    return Err(bad(format!(
                        "next state {} is larger than the number of states {nstates}",
                        nextstate.as_usize()
                    )));
                }
            }
        }

        Ok(fst)
    }
}

impl<A: Arc, U: Unsigned> ConstFst<'_, A, U> {
    /// The name this FST goes by in a file, which depends on the width of the
    /// arc indices.
    fn fst_type_name() -> FstType {
        if std::mem::size_of::<U>() == std::mem::size_of::<u32>() {
            FstType::CONST_32
        } else {
            FstType::CONST_64
        }
    }

    /// Writes any FST out in this format.
    ///
    /// SICADA-DIVERGE: upstream rewrites the header afterwards with the state
    /// and arc counts it actually saw, seeking back to do it, and falls back to
    /// counting them up front when the stream cannot seek. Counting first is
    /// what this always does: it costs one more pass over an FST that already
    /// knows its size, and in exchange the output is correct on a pipe and the
    /// writer needs no `Seek`.
    pub fn write_fst<F: Fst<A>, W: Write>(
        fst: &F,
        writer: &mut W,
        opts: &FstWriteOptions,
    ) -> Result<(), OpenFstError> {
        let mut nstates = 0i64;
        let mut narcs = 0i64;
        for s in fst.states() {
            nstates += 1;
            narcs += fst.num_arcs(s) as i64;
        }

        let header = FstHeader {
            fst_type: Self::fst_type_name().as_str().to_string(),
            arc_type: A::type_name().as_str().to_string(),
            version: if opts.align {
                ALIGNED_FILE_VERSION
            } else {
                FILE_VERSION
            },
            // Overwritten by `write_fst_header` from the tables it writes.
            flags: 0,
            properties: fst.properties(K_COPY_PROPERTIES, true) | K_STATIC_PROPERTIES,
            start: fst
                .start()
                .map_or(NO_STATE_ID as i64, |s| s.as_usize() as i64),
            num_states: nstates,
            num_arcs: narcs,
        };
        let isymbols = fst.input_symbols();
        let osymbols = fst.output_symbols();
        // Counted rather than seeked: alignment needs the file offset, and this
        // way it works on a stream that cannot report one.
        let mut writer = CountingWriter::new(writer, 0);
        write_fst_header(
            &mut writer,
            opts,
            &header,
            isymbols.as_deref(),
            osymbols.as_deref(),
        )?;

        // The states and arcs go out as blocks of memory, which is why their
        // layout is pinned; see `ConstState` and `ArcTpl`.
        let mut states = Vec::with_capacity(nstates as usize);
        let mut pos = U::from_usize(0);
        for s in fst.states() {
            let narcs_s = fst.num_arcs(s);
            states.push(ConstState {
                final_weight: fst.final_weight(s),
                pos,
                narcs: U::from_usize(narcs_s),
                niepsilons: U::from_usize(fst.num_input_epsilons(s)),
                noepsilons: U::from_usize(fst.num_output_epsilons(s)),
            });
            pos = U::from_usize(pos.as_usize() + narcs_s);
        }
        if opts.align {
            writer.align(ARCH_ALIGNMENT as u64)?;
        }
        writer.write_all(as_bytes(&states))?;

        if opts.align {
            writer.align(ARCH_ALIGNMENT as u64)?;
        }
        let mut arcs = Vec::with_capacity(narcs as usize);
        for s in fst.states() {
            arcs.extend(fst.arcs(s));
        }
        writer.write_all(as_bytes(&arcs))?;
        Ok(())
    }

    /// Writes this FST out.
    pub fn write<W: Write>(
        &self,
        writer: &mut W,
        opts: &FstWriteOptions,
    ) -> Result<(), OpenFstError> {
        Self::write_fst(self, writer, opts)
    }
}

/// A slice seen as the bytes it occupies.
///
/// # Safety
///
/// `T` must be `#[repr(C)]` with no uninitialized padding that would be written
/// out. Both types this is used for, `ConstState` and the arc type, are laid out
/// so that every byte belongs to a field.
fn as_bytes<T>(values: &[T]) -> &[u8] {
    // SAFETY: the slice is valid for `len * size_of::<T>()` bytes, and `u8` has
    // no alignment requirement or invalid values, so any byte pattern reads
    // back.
    unsafe {
        std::slice::from_raw_parts(values.as_ptr() as *const u8, std::mem::size_of_val(values))
    }
}

pub struct ConstStateIter<S: ArcStateId> {
    nstates: usize,
    s: usize,
    _phantom: PhantomData<S>,
}

impl<S: ArcStateId> Iterator for ConstStateIter<S> {
    type Item = S;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        if self.s < self.nstates {
            let s = S::from_usize(self.s);
            self.s += 1;
            Some(s)
        } else {
            None
        }
    }
}

pub struct ConstArcIter<'a, A: Arc> {
    arcs: &'a [A],
    pos: usize,
}

impl<'a, A: Arc> Clone for ConstArcIter<'a, A> {
    fn clone(&self) -> Self {
        Self {
            arcs: self.arcs,
            pos: self.pos,
        }
    }
}

impl<'a, A: Arc> Iterator for ConstArcIter<'a, A> {
    type Item = A;

    #[inline(always)]
    fn next(&mut self) -> Option<Self::Item> {
        if self.pos < self.arcs.len() {
            let item = self.arcs[self.pos].clone();
            self.pos += 1;
            Some(item)
        } else {
            None
        }
    }
}

impl<'a, A: Arc, U: Unsigned> Fst<A> for ConstFst<'a, A, U> {
    type StateIter<'b>
        = ConstStateIter<A::StateId>
    where
        Self: 'b;
    type ArcIter<'b>
        = ConstArcIter<'b, A>
    where
        Self: 'b;

    #[inline]
    fn start(&self) -> Option<A::StateId> {
        self.start
    }

    #[inline]
    fn final_weight(&self, state: A::StateId) -> A::Weight {
        let s = state.as_usize();
        if s < self.nstates {
            self.states_slice()[s].final_weight.clone()
        } else {
            A::Weight::zero()
        }
    }

    #[inline]
    fn num_arcs(&self, state: A::StateId) -> usize {
        let s = state.as_usize();
        if s < self.nstates {
            self.states_slice()[s].narcs.as_usize()
        } else {
            0
        }
    }

    #[inline]
    fn num_input_epsilons(&self, state: A::StateId) -> usize {
        let s = state.as_usize();
        if s < self.nstates {
            self.states_slice()[s].niepsilons.as_usize()
        } else {
            0
        }
    }

    #[inline]
    fn num_output_epsilons(&self, state: A::StateId) -> usize {
        let s = state.as_usize();
        if s < self.nstates {
            self.states_slice()[s].noepsilons.as_usize()
        } else {
            0
        }
    }

    #[inline]
    fn num_states_if_known(&self) -> Option<usize> {
        Some(self.nstates)
    }

    #[inline]
    fn properties(&self, mask: u64, test: bool) -> u64 {
        cached_properties(self, &self.properties, mask, test)
    }

    #[inline]
    fn fst_type(&self) -> &str {
        if std::mem::size_of::<U>() != 4 {
            FstType::CONST_64.as_str()
        } else {
            FstType::CONST_32.as_str()
        }
    }

    #[inline]
    fn input_symbols(&self) -> Option<AtomicRc<SymbolTable>> {
        self.input_symbols.clone()
    }

    #[inline]
    fn output_symbols(&self) -> Option<AtomicRc<SymbolTable>> {
        self.output_symbols.clone()
    }

    #[inline]
    fn states<'b>(&'b self) -> Self::StateIter<'b> {
        ConstStateIter {
            nstates: self.nstates,
            s: 0,
            _phantom: PhantomData,
        }
    }

    #[inline]
    fn arcs<'b>(&'b self, state: A::StateId) -> Self::ArcIter<'b> {
        let s = state.as_usize();
        let arcs = if s < self.nstates {
            let st = &self.states_slice()[s];
            let start = st.pos.as_usize();
            let len = st.narcs.as_usize();
            &self.arcs_slice_internal()[start..start + len]
        } else {
            &[]
        };
        ConstArcIter { arcs, pos: 0 }
    }
}

impl<'a, A: Arc, U: Unsigned> ExpandedFst<A> for ConstFst<'a, A, U> {
    #[inline]
    fn num_states(&self) -> usize {
        self.nstates
    }
}

impl<'a, A: Arc, U: Unsigned> ContiguousArcsFst<A> for ConstFst<'a, A, U> {
    /// Returns a contiguous slice of arcs leaving the given state in O(1) time.
    #[inline]
    fn arcs_slice(&self, state: A::StateId) -> &[A] {
        let s = state.as_usize();
        if s < self.nstates {
            let st = &self.states_slice()[s];
            let start = st.pos.as_usize();
            let len = st.narcs.as_usize();
            &self.arcs_slice_internal()[start..start + len]
        } else {
            &[]
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::arc::StdArc;
    use crate::float_weight::TropicalWeight;
    use crate::fst::MutableFst;
    use crate::vector_fst::StdVectorFst;
    use std::io::Cursor;

    #[test]
    fn test_const_fst_basic() {
        let mut vfst = StdVectorFst::new();
        let s0 = vfst.add_state();
        let s1 = vfst.add_state();

        vfst.set_start(s0);
        vfst.set_final(s1, TropicalWeight(1.0));

        vfst.add_arc(s0, StdArc::new(1, 2, TropicalWeight(0.5), s1));
        vfst.add_arc(s0, StdArc::new(0, 0, TropicalWeight(0.2), s0)); // Epsilon loop

        // Use u32 as the unsigned index type
        let cfst = ConstFst::<StdArc, u32>::from_fst(&vfst).unwrap();

        assert_eq!(cfst.fst_type(), "const");
        assert_eq!(cfst.start(), Some(0));
        assert_eq!(cfst.num_states(), 2);
        assert_eq!(cfst.final_weight(1).value(), 1.0);
        assert_eq!(cfst.final_weight(0), TropicalWeight::zero());

        assert_eq!(cfst.num_arcs(0), 2);
        assert_eq!(cfst.num_arcs(1), 0);

        // Epsilon counts test
        assert_eq!(cfst.num_input_epsilons(0), 1);
        assert_eq!(cfst.num_output_epsilons(0), 1);

        // Contiguous slice access test
        let arcs_s0 = cfst.arcs_slice(0);
        assert_eq!(arcs_s0.len(), 2);
        assert_eq!(arcs_s0[0].ilabel(), 1);
        assert_eq!(arcs_s0[0].olabel(), 2);
        assert_eq!(arcs_s0[0].weight().value(), 0.5);
        assert_eq!(arcs_s0[0].nextstate(), 1);
    }

    #[test]
    fn test_const_fst_64() {
        let mut vfst = StdVectorFst::new();
        vfst.add_state();
        vfst.set_start(0);

        // Use u64 as the unsigned index type
        let cfst = ConstFst::<StdArc, u64>::from_fst(&vfst).unwrap();
        assert_eq!(cfst.fst_type(), "const64");
    }

    #[test]
    fn test_const_fst_empty() {
        let vfst = StdVectorFst::new();
        let cfst = ConstFst::<StdArc, u32>::from_fst(&vfst).unwrap();

        assert_eq!(cfst.num_states(), 0);
        assert_eq!(cfst.start(), None);
    }

    /// The layout of what goes on disk, checked against what OpenFst produces.
    ///
    /// From `tests/oracles/const-fst-golden.cc`. A `ConstFst` file is the
    /// arrays of these two structures written out as blocks of memory, so a
    /// changed size, alignment or field offset is a changed file format.
    #[test]
    fn the_on_disk_structures_are_laid_out_as_openfst_lays_them_out() {
        use std::mem::{align_of, offset_of, size_of};

        type Arc32 = StdArc;
        assert_eq!(size_of::<Arc32>(), 16);
        assert_eq!(align_of::<Arc32>(), 4);
        assert_eq!(offset_of!(Arc32, ilabel), 0);
        assert_eq!(offset_of!(Arc32, olabel), 4);
        assert_eq!(offset_of!(Arc32, weight), 8);
        assert_eq!(offset_of!(Arc32, nextstate), 12);

        type State32 = ConstState<TropicalWeight, u32>;
        assert_eq!(size_of::<State32>(), 20);
        assert_eq!(align_of::<State32>(), 4);
        assert_eq!(offset_of!(State32, final_weight), 0);
        assert_eq!(offset_of!(State32, pos), 4);
        assert_eq!(offset_of!(State32, narcs), 8);
        assert_eq!(offset_of!(State32, niepsilons), 12);
        assert_eq!(offset_of!(State32, noepsilons), 16);

        type State64 = ConstState<TropicalWeight, u64>;
        assert_eq!(size_of::<State64>(), 40);
        assert_eq!(align_of::<State64>(), 8);
        assert_eq!(offset_of!(State64, final_weight), 0);
        assert_eq!(offset_of!(State64, pos), 8);
        assert_eq!(offset_of!(State64, narcs), 16);
        assert_eq!(offset_of!(State64, niepsilons), 24);
        assert_eq!(offset_of!(State64, noepsilons), 32);
    }

    fn hex(bytes: &[u8]) -> String {
        bytes.iter().map(|b| format!("{b:02x}")).collect()
    }

    /// 0 -> 1 -> 2, with 2 final.
    fn chain() -> StdVectorFst {
        let mut fst = StdVectorFst::new();
        for _ in 0..3 {
            fst.add_state();
        }
        fst.set_start(0);
        fst.add_arc(0, StdArc::new(1, 1, TropicalWeight::one(), 1));
        fst.add_arc(1, StdArc::new(2, 2, TropicalWeight::one(), 2));
        fst.set_final(2, TropicalWeight::one());
        fst
    }

    /// The exact bytes of the states and arcs blocks, against the ones OpenFst
    /// writes for the same FST.
    #[test]
    fn the_state_and_arc_blocks_are_the_bytes_openfst_writes() {
        const GOLDEN_STATES: &str = concat!(
            "0000807f00000000010000000000000000000000",
            "0000807f01000000010000000000000000000000",
            "0000000002000000000000000000000000000000",
        );
        const GOLDEN_ARCS: &str =
            "0100000001000000000000000100000002000000020000000000000002000000";

        let mut bytes = Vec::new();
        ConstFst::<StdArc, u32>::write_fst(&chain(), &mut bytes, &FstWriteOptions::default())
            .unwrap();

        // Everything after the header is the two blocks, back to back.
        let mut header = Vec::new();
        let read = FstHeader::read(&mut Cursor::new(bytes.clone())).unwrap();
        read.write(&mut header).unwrap();
        let body = &bytes[header.len()..];

        let states_len = 3 * std::mem::size_of::<ConstState<TropicalWeight, u32>>();
        assert_eq!(hex(&body[..states_len]), GOLDEN_STATES);
        assert_eq!(hex(&body[states_len..]), GOLDEN_ARCS);
    }

    #[test]
    fn a_const_fst_round_trips_through_a_stream() {
        let source = chain();
        let mut bytes = Vec::new();
        ConstFst::<StdArc, u32>::write_fst(&source, &mut bytes, &FstWriteOptions::default())
            .unwrap();

        let read =
            ConstFst::<StdArc, u32>::read(&mut Cursor::new(bytes), &FstReadOptions::default())
                .unwrap();

        assert_eq!(read.start(), Some(0));
        assert_eq!(read.num_states(), 3);
        assert_eq!(read.final_weight(2), TropicalWeight::one());
        assert_eq!(read.final_weight(0), TropicalWeight::zero());
        for state in 0..3 {
            let want: Vec<_> = source.arcs(state).collect();
            let got: Vec<_> = read.arcs(state).collect();
            assert_eq!(got, want, "state {state}");
        }
    }

    /// Writing aligned pads the blocks so a reader can map them; the contents
    /// have to survive the padding.
    #[test]
    fn an_aligned_file_round_trips_and_is_padded() {
        let source = chain();
        let opts = FstWriteOptions {
            align: true,
            ..Default::default()
        };
        let mut aligned = Vec::new();
        ConstFst::<StdArc, u32>::write_fst(&source, &mut aligned, &opts).unwrap();

        let mut plain = Vec::new();
        ConstFst::<StdArc, u32>::write_fst(&source, &mut plain, &FstWriteOptions::default())
            .unwrap();
        assert!(aligned.len() > plain.len(), "no padding was written");

        let header = FstHeader::read(&mut Cursor::new(aligned.clone())).unwrap();
        assert_eq!(header.version, ALIGNED_FILE_VERSION);
        assert_ne!(header.flags & flags::IS_ALIGNED, 0);

        let read =
            ConstFst::<StdArc, u32>::read(&mut Cursor::new(aligned), &FstReadOptions::default())
                .unwrap();
        assert_eq!(read.num_states(), 3);
        let arcs: Vec<_> = read.arcs(0).collect();
        assert_eq!(arcs, source.arcs(0).collect::<Vec<_>>());
    }

    /// Version 1 predates the alignment flag and was always aligned, so it has
    /// to be read as aligned whatever the flags say.
    #[test]
    fn version_one_is_read_as_aligned_even_without_the_flag() {
        let opts = FstWriteOptions {
            align: true,
            ..Default::default()
        };
        let mut bytes = Vec::new();
        ConstFst::<StdArc, u32>::write_fst(&chain(), &mut bytes, &opts).unwrap();

        // Clear the flag, leaving only the version to say so.
        let mut header = FstHeader::read(&mut Cursor::new(bytes.clone())).unwrap();
        let header_len = {
            let mut probe = Vec::new();
            header.write(&mut probe).unwrap();
            probe.len()
        };
        assert_eq!(header.version, ALIGNED_FILE_VERSION);
        header.flags &= !flags::IS_ALIGNED;
        let mut rewritten = Vec::new();
        header.write(&mut rewritten).unwrap();
        rewritten.extend_from_slice(&bytes[header_len..]);

        let read =
            ConstFst::<StdArc, u32>::read(&mut Cursor::new(rewritten), &FstReadOptions::default())
                .unwrap();
        assert_eq!(read.num_states(), 3);
        assert_eq!(read.arcs(0).next().unwrap().ilabel(), 1);
    }

    #[test]
    fn a_file_that_lies_about_its_shape_is_refused() {
        let mut bytes = Vec::new();
        ConstFst::<StdArc, u32>::write_fst(&chain(), &mut bytes, &FstWriteOptions::default())
            .unwrap();

        let rewrite = |edit: &dyn Fn(&mut FstHeader)| {
            let mut header = FstHeader::read(&mut Cursor::new(bytes.clone())).unwrap();
            let header_len = {
                let mut probe = Vec::new();
                header.write(&mut probe).unwrap();
                probe.len()
            };
            edit(&mut header);
            let mut out = Vec::new();
            header.write(&mut out).unwrap();
            out.extend_from_slice(&bytes[header_len..]);
            ConstFst::<StdArc, u32>::read(&mut Cursor::new(out), &FstReadOptions::default())
        };

        assert!(rewrite(&|h| h.num_states = -1).is_err(), "negative states");
        assert!(
            rewrite(&|h| h.num_states = i64::MAX).is_err(),
            "absurd states"
        );
        assert!(rewrite(&|h| h.num_arcs = -1).is_err(), "negative arcs");
        assert!(rewrite(&|h| h.start = 9).is_err(), "start past the end");
        assert!(rewrite(&|h| h.start = -7).is_err(), "negative start");
        assert!(
            rewrite(&|h| h.fst_type = "vector".to_string()).is_err(),
            "wrong type"
        );
        assert!(
            rewrite(&|h| h.arc_type = "log".to_string()).is_err(),
            "wrong arc type"
        );
        assert!(
            rewrite(&|h| h.version = MIN_FILE_VERSION - 1).is_err(),
            "old version"
        );
        // Unchanged, as a control.
        assert!(rewrite(&|_| {}).is_ok());
    }

    /// With `verify`, an arc pointing at a state that does not exist is caught
    /// at read time rather than at the first traversal.
    #[test]
    fn an_arc_leading_outside_the_fst_is_caught_when_verifying() {
        let mut bytes = Vec::new();
        ConstFst::<StdArc, u32>::write_fst(&chain(), &mut bytes, &FstWriteOptions::default())
            .unwrap();

        // The last four bytes are the final arc's nextstate.
        let len = bytes.len();
        bytes[len - 4..].copy_from_slice(&9i32.to_le_bytes());

        assert!(
            ConstFst::<StdArc, u32>::read(
                &mut Cursor::new(bytes.clone()),
                &FstReadOptions::default()
            )
            .is_err()
        );
        // Without verifying it is let through, as upstream also does.
        let opts = FstReadOptions {
            verify: false,
            ..Default::default()
        };
        assert!(ConstFst::<StdArc, u32>::read(&mut Cursor::new(bytes), &opts).is_ok());
    }

    /// The symbol tables sit between the header and the blocks; a reader that
    /// skipped them would decode the states out of the middle of a table.
    #[test]
    fn symbol_tables_survive_the_round_trip() {
        let mut table = SymbolTable::new("input".to_string());
        table.add_symbol("<eps>", 0);
        table.add_symbol("a", 1);
        table.add_symbol("b", 2);

        let mut source = chain();
        source.set_input_symbols(Some(AtomicRc::new(table)));

        let mut bytes = Vec::new();
        ConstFst::<StdArc, u32>::write_fst(&source, &mut bytes, &FstWriteOptions::default())
            .unwrap();
        let read =
            ConstFst::<StdArc, u32>::read(&mut Cursor::new(bytes), &FstReadOptions::default())
                .unwrap();

        assert_eq!(read.num_states(), 3);
        let symbols = read.input_symbols().expect("the table came back");
        assert_eq!(symbols.find_symbol(1), Some("a"));
        assert_eq!(symbols.find_symbol(2), Some("b"));
        assert!(read.output_symbols().is_none());
        assert_eq!(read.arcs(0).next().unwrap().ilabel(), 1);
    }

    /// Reading through a file is the path that can map rather than copy.
    #[test]
    fn a_const_fst_round_trips_through_a_file() {
        use std::io::Write as _;

        let source = chain();
        let mut bytes = Vec::new();
        ConstFst::<StdArc, u32>::write_fst(
            &source,
            &mut bytes,
            &FstWriteOptions {
                align: true,
                ..Default::default()
            },
        )
        .unwrap();

        let mut file = tempfile::NamedTempFile::new().unwrap();
        file.write_all(&bytes).unwrap();
        file.flush().unwrap();

        for mode in [FileReadMode::Read, FileReadMode::Map] {
            let read = ConstFst::<StdArc, u32>::read_from_file(
                file.path(),
                &FstReadOptions::default().mode(mode),
            )
            .unwrap();
            assert_eq!(read.num_states(), 3, "{mode:?}");
            assert_eq!(read.start(), Some(0));
            for state in 0..3 {
                assert_eq!(
                    read.arcs(state).collect::<Vec<_>>(),
                    source.arcs(state).collect::<Vec<_>>(),
                    "{mode:?} state {state}"
                );
            }
        }
    }
}