Imported Upstream version 1.7.5 upstream/1.7.5
authorDongHun Kwak <dh0128.kwak@samsung.com>
Tue, 18 Jan 2022 02:15:05 +0000 (11:15 +0900)
committerDongHun Kwak <dh0128.kwak@samsung.com>
Tue, 18 Jan 2022 02:15:05 +0000 (11:15 +0900)
165 files changed:
NEWS
README
configure
configure.ac
src/bin/fstencode-main.cc
src/bin/fstpush-main.cc
src/bin/fstrelabel-main.cc
src/bin/fstreplace-main.cc
src/bin/fstsymbols-main.cc
src/extensions/compact/Makefile.am
src/extensions/compact/Makefile.in
src/extensions/compress/Makefile.am
src/extensions/compress/Makefile.in
src/extensions/const/Makefile.am
src/extensions/const/Makefile.in
src/extensions/const/const16-fst.cc
src/extensions/const/const64-fst.cc
src/extensions/const/const8-fst.cc
src/extensions/far/Makefile.am
src/extensions/far/Makefile.in
src/extensions/linear/Makefile.am
src/extensions/linear/Makefile.in
src/extensions/lookahead/Makefile.am
src/extensions/lookahead/Makefile.in
src/extensions/mpdt/Makefile.am
src/extensions/mpdt/Makefile.in
src/extensions/mpdt/mpdtcompose-main.cc
src/extensions/mpdt/mpdtexpand-main.cc
src/extensions/mpdt/mpdtinfo-main.cc
src/extensions/mpdt/mpdtreverse-main.cc
src/extensions/mpdt/mpdtscript.cc
src/extensions/ngram/Makefile.am
src/extensions/ngram/Makefile.in
src/extensions/ngram/bitmap-index.cc
src/extensions/ngram/nthbit.cc
src/extensions/pdt/Makefile.am
src/extensions/pdt/Makefile.in
src/extensions/pdt/pdtcompose-main.cc
src/extensions/pdt/pdtexpand-main.cc
src/extensions/pdt/pdtinfo-main.cc
src/extensions/pdt/pdtreplace-main.cc
src/extensions/pdt/pdtreverse-main.cc
src/extensions/pdt/pdtscript.cc
src/extensions/pdt/pdtshortestpath-main.cc
src/extensions/python/Makefile.am
src/extensions/python/Makefile.in
src/extensions/python/cpywrapfst.pxd [moved from src/extensions/python/fst.pxd with 100% similarity]
src/extensions/python/pywrapfst.pxd
src/extensions/special/Makefile.am
src/extensions/special/Makefile.in
src/include/fst/accumulator.h
src/include/fst/add-on.h
src/include/fst/arc-arena.h
src/include/fst/arc-map.h
src/include/fst/arcsort.h
src/include/fst/bi-table.h
src/include/fst/cache.h
src/include/fst/closure.h
src/include/fst/compact-fst.h
src/include/fst/complement.h
src/include/fst/compose-filter.h
src/include/fst/compose.h
src/include/fst/concat.h
src/include/fst/connect.h
src/include/fst/const-fst.h
src/include/fst/determinize.h
src/include/fst/difference.h
src/include/fst/disambiguate.h
src/include/fst/edit-fst.h
src/include/fst/encode.h
src/include/fst/equivalent.h
src/include/fst/expanded-fst.h
src/include/fst/expander-cache.h
src/include/fst/expectation-weight.h
src/include/fst/extensions/compress/compress.h
src/include/fst/extensions/far/farscript.h
src/include/fst/extensions/linear/linear-fst-data-builder.h
src/include/fst/extensions/linear/linearscript.h
src/include/fst/extensions/mpdt/expand.h
src/include/fst/extensions/mpdt/info.h
src/include/fst/extensions/mpdt/mpdtscript.h
src/include/fst/extensions/ngram/bitmap-index.h
src/include/fst/extensions/ngram/ngram-fst.h
src/include/fst/extensions/ngram/nthbit.h
src/include/fst/extensions/pdt/expand.h
src/include/fst/extensions/pdt/info.h
src/include/fst/extensions/pdt/pdtscript.h
src/include/fst/extensions/pdt/shortest-path.h
src/include/fst/factor-weight.h
src/include/fst/filter-state.h
src/include/fst/float-weight.h
src/include/fst/fst.h
src/include/fst/generic-register.h
src/include/fst/icu.h
src/include/fst/intersect.h
src/include/fst/invert.h
src/include/fst/label-reachable.h
src/include/fst/lexicographic-weight.h
src/include/fst/lookahead-filter.h
src/include/fst/lookahead-matcher.h
src/include/fst/map.h
src/include/fst/mapped-file.h
src/include/fst/matcher-fst.h
src/include/fst/matcher.h
src/include/fst/memory.h
src/include/fst/minimize.h
src/include/fst/mutable-fst.h
src/include/fst/power-weight.h
src/include/fst/product-weight.h
src/include/fst/project.h
src/include/fst/properties.h
src/include/fst/push.h
src/include/fst/queue.h
src/include/fst/randgen.h
src/include/fst/rational.h
src/include/fst/register.h
src/include/fst/relabel.h
src/include/fst/replace-util.h
src/include/fst/replace.h
src/include/fst/reverse.h
src/include/fst/rmepsilon.h
src/include/fst/rmfinalepsilon.h
src/include/fst/script/arciterator-class.h
src/include/fst/script/encodemapper-class.h
src/include/fst/script/fstscript.h
src/include/fst/script/getters.h
src/include/fst/script/info-impl.h
src/include/fst/script/push.h
src/include/fst/script/randequivalent.h
src/include/fst/script/relabel.h
src/include/fst/script/replace.h
src/include/fst/script/rmepsilon.h
src/include/fst/script/script-impl.h
src/include/fst/script/shortest-path.h
src/include/fst/script/weight-class.h
src/include/fst/set-weight.h
src/include/fst/signed-log-weight.h
src/include/fst/sparse-power-weight.h
src/include/fst/state-map.h
src/include/fst/state-table.h
src/include/fst/statesort.h
src/include/fst/string-weight.h
src/include/fst/string.h
src/include/fst/symbol-table.h
src/include/fst/synchronize.h
src/include/fst/test-properties.h
src/include/fst/test/algo_test.h
src/include/fst/test/compactors.h
src/include/fst/tuple-weight.h
src/include/fst/union-weight.h
src/include/fst/union.h
src/include/fst/util.h
src/include/fst/vector-fst.h
src/include/fst/verify.h
src/include/fst/weight.h
src/lib/fst.cc
src/lib/mapped-file.cc
src/lib/properties.cc
src/lib/symbol-table.cc
src/lib/util.cc
src/script/Makefile.am
src/script/Makefile.in
src/script/fst-class.cc
src/script/weight-class.cc
src/test/fst_test.cc

diff --git a/NEWS b/NEWS
index b2bca5a..5e65965 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -1,9 +1,13 @@
 OpenFst: Release 1.7
-   * Removes harmful constexpr specifications in FAR (1.7.4)
+   * Removes unnecessary template parameters in constructors (1.7.5)
+   * Converts RmEpsilonFstOptions from class to struct (1.7.5)
+   * Eliminates redundant checks in Minimize (1.7.5)
+   * CompactFst is now templated on Compactor rather than ArcCompactor (1.7.4)
+   * Removes harmful constexpr specifications in the FAR extension (1.7.4)
    * Improved script API support for EncodeMapper (1.7.4)
    * New header format for the EncodeMapper (1.7.4)
-   * Imrproved C++17 compatibility shims (1.7.4)
-   * Rewrite of the compactor interface (1.7.4)
+   * Many cleanups to the n-gram extension (1.7.4)
+   * Improved C++17 compatibility shims (1.7.4)
    * Overloads Arc constructors with default weight argument (1.7.3)
    * Fixes RmEpsilon and Union property checking bugs (1.7.3)
    * Makes Isomorphic more robust to nondeterminism (1.7.3)
diff --git a/README b/README
index 57de1ef..15d352a 100644 (file)
--- a/README
+++ b/README
@@ -1,5 +1,3 @@
-OpenFst: Release 1.7.3.
-
 OpenFst is a library for constructing, combining, optimizing, and searching
 weighted finite-state transducers (FSTs).
 
index 8695775..95d67eb 100755 (executable)
--- a/configure
+++ b/configure
@@ -1,6 +1,6 @@
 #! /bin/sh
 # Guess values for system-dependent variables and create Makefiles.
-# Generated by GNU Autoconf 2.69 for OpenFst 1.7.4.
+# Generated by GNU Autoconf 2.69 for OpenFst 1.7.5.
 #
 # Report bugs to <help@www.openfst.org>.
 #
@@ -590,8 +590,8 @@ MAKEFLAGS=
 # Identity of this package.
 PACKAGE_NAME='OpenFst'
 PACKAGE_TARNAME='openfst'
-PACKAGE_VERSION='1.7.4'
-PACKAGE_STRING='OpenFst 1.7.4'
+PACKAGE_VERSION='1.7.5'
+PACKAGE_STRING='OpenFst 1.7.5'
 PACKAGE_BUGREPORT='help@www.openfst.org'
 PACKAGE_URL=''
 
@@ -1395,7 +1395,7 @@ if test "$ac_init_help" = "long"; then
   # Omit some internal or obsolete options to make the list less imposing.
   # This message is too long to be a string in the A/UX 3.1 sh.
   cat <<_ACEOF
-\`configure' configures OpenFst 1.7.4 to adapt to many kinds of systems.
+\`configure' configures OpenFst 1.7.5 to adapt to many kinds of systems.
 
 Usage: $0 [OPTION]... [VAR=VALUE]...
 
@@ -1466,7 +1466,7 @@ fi
 
 if test -n "$ac_init_help"; then
   case $ac_init_help in
-     short | recursive ) echo "Configuration of OpenFst 1.7.4:";;
+     short | recursive ) echo "Configuration of OpenFst 1.7.5:";;
    esac
   cat <<\_ACEOF
 
@@ -1598,7 +1598,7 @@ fi
 test -n "$ac_init_help" && exit $ac_status
 if $ac_init_version; then
   cat <<\_ACEOF
-OpenFst configure 1.7.4
+OpenFst configure 1.7.5
 generated by GNU Autoconf 2.69
 
 Copyright (C) 2012 Free Software Foundation, Inc.
@@ -2039,7 +2039,7 @@ cat >config.log <<_ACEOF
 This file contains any messages produced by compilers while
 running configure, to aid debugging if configure makes a mistake.
 
-It was created by OpenFst $as_me 1.7.4, which was
+It was created by OpenFst $as_me 1.7.5, which was
 generated by GNU Autoconf 2.69.  Invocation command line was
 
   $ $0 $@
@@ -2902,7 +2902,7 @@ fi
 
 # Define the identity of the package.
  PACKAGE='openfst'
- VERSION='1.7.4'
+ VERSION='1.7.5'
 
 
 cat >>confdefs.h <<_ACEOF
@@ -17551,7 +17551,7 @@ cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
 # report actual input values of CONFIG_FILES etc. instead of their
 # values after options handling.
 ac_log="
-This file was extended by OpenFst $as_me 1.7.4, which was
+This file was extended by OpenFst $as_me 1.7.5, which was
 generated by GNU Autoconf 2.69.  Invocation command line was
 
   CONFIG_FILES    = $CONFIG_FILES
@@ -17617,7 +17617,7 @@ _ACEOF
 cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
 ac_cs_config="`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`"
 ac_cs_version="\\
-OpenFst config.status 1.7.4
+OpenFst config.status 1.7.5
 configured by $0, generated by GNU Autoconf 2.69,
   with options \\"\$ac_cs_config\\"
 
index 5ad140a..f8777cf 100644 (file)
@@ -1,4 +1,4 @@
-AC_INIT([OpenFst], [1.7.4], [help@www.openfst.org])
+AC_INIT([OpenFst], [1.7.5], [help@www.openfst.org])
 AM_INIT_AUTOMAKE([foreign nostdinc -Wall -Werror subdir-objects])
 AM_PROG_AR
 
index 37effd7..5515405 100644 (file)
@@ -8,6 +8,7 @@
 #include <string>
 
 #include <fst/flags.h>
+#include <fst/types.h>
 #include <fst/script/decode.h>
 #include <fst/script/encode.h>
 #include <fst/script/getters.h>
index 03c5f74..0e747da 100644 (file)
@@ -9,6 +9,7 @@
 #include <string>
 
 #include <fst/flags.h>
+#include <fst/types.h>
 #include <fst/script/getters.h>
 #include <fst/script/push.h>
 
index 6e96872..5505575 100644 (file)
@@ -10,6 +10,7 @@
 #include <vector>
 
 #include <fst/flags.h>
+#include <fst/types.h>
 #include <fst/util.h>
 #include <fst/script/relabel.h>
 #include <fst/script/weight-class.h>
index debb8d6..932f6aa 100644 (file)
@@ -10,6 +10,7 @@
 #include <vector>
 
 #include <fst/flags.h>
+#include <fst/types.h>
 #include <fst/script/getters.h>
 #include <fst/script/replace.h>
 
index 511d9db..9064d67 100644 (file)
@@ -10,6 +10,7 @@
 #include <vector>
 
 #include <fst/flags.h>
+#include <fst/types.h>
 #include <fst/util.h>
 #include <fst/script/fst-class.h>
 #include <fst/script/verify.h>
index 93050f7..9df6f74 100644 (file)
@@ -7,7 +7,7 @@ libfst_LTLIBRARIES = compact8_acceptor-fst.la compact8_string-fst.la compact8_un
 lib_LTLIBRARIES = libfstcompact.la
 
 libfstcompact_la_SOURCES = compact8_acceptor-fst.cc compact8_string-fst.cc compact8_unweighted-fst.cc compact8_unweighted_acceptor-fst.cc compact8_weighted_string-fst.cc compact16_acceptor-fst.cc compact16_string-fst.cc compact16_unweighted-fst.cc compact16_unweighted_acceptor-fst.cc compact16_weighted_string-fst.cc compact64_acceptor-fst.cc compact64_string-fst.cc compact64_unweighted-fst.cc compact64_unweighted_acceptor-fst.cc compact64_weighted_string-fst.cc
-libfstcompact_la_LDFLAGS = -version-info 17:0:0
+libfstcompact_la_LDFLAGS = -version-info 18:0:0
 
 compact8_acceptor_fst_la_SOURCES = compact8_acceptor-fst.cc
 compact8_acceptor_fst_la_LDFLAGS = -avoid-version -module
index 268716f..12bf19e 100644 (file)
@@ -527,7 +527,7 @@ AM_CPPFLAGS = -I$(srcdir)/../../include $(ICU_CPPFLAGS)
 libfst_LTLIBRARIES = compact8_acceptor-fst.la compact8_string-fst.la compact8_unweighted-fst.la compact8_unweighted_acceptor-fst.la compact8_weighted_string-fst.la compact16_acceptor-fst.la compact16_string-fst.la compact16_unweighted-fst.la compact16_unweighted_acceptor-fst.la compact16_weighted_string-fst.la compact64_acceptor-fst.la compact64_string-fst.la compact64_unweighted-fst.la compact64_unweighted_acceptor-fst.la compact64_weighted_string-fst.la
 lib_LTLIBRARIES = libfstcompact.la
 libfstcompact_la_SOURCES = compact8_acceptor-fst.cc compact8_string-fst.cc compact8_unweighted-fst.cc compact8_unweighted_acceptor-fst.cc compact8_weighted_string-fst.cc compact16_acceptor-fst.cc compact16_string-fst.cc compact16_unweighted-fst.cc compact16_unweighted_acceptor-fst.cc compact16_weighted_string-fst.cc compact64_acceptor-fst.cc compact64_string-fst.cc compact64_unweighted-fst.cc compact64_unweighted_acceptor-fst.cc compact64_weighted_string-fst.cc
-libfstcompact_la_LDFLAGS = -version-info 17:0:0
+libfstcompact_la_LDFLAGS = -version-info 18:0:0
 compact8_acceptor_fst_la_SOURCES = compact8_acceptor-fst.cc
 compact8_acceptor_fst_la_LDFLAGS = -avoid-version -module
 compact8_string_fst_la_SOURCES = compact8_string-fst.cc
index 1994f42..012b02f 100644 (file)
@@ -13,7 +13,7 @@ endif
 
 if HAVE_SCRIPT
 libfstcompressscript_la_SOURCES = compressscript.cc
-libfstcompressscript_la_LDFLAGS = -version-info 17:0:0
+libfstcompressscript_la_LDFLAGS = -version-info 18:0:0
 libfstcompressscript_la_LIBADD = ../../script/libfstscript.la \
                                  ../../lib/libfst.la \
                                  -lz -lm $(DL_LIBS)
index 3d6b697..cb51e6f 100644 (file)
@@ -370,7 +370,7 @@ AM_CPPFLAGS = -I$(srcdir)/../../include $(ICU_CPPFLAGS)
 
 @HAVE_BIN_TRUE@fstcompress_SOURCES = fstcompress.cc fstcompress-main.cc
 @HAVE_SCRIPT_TRUE@libfstcompressscript_la_SOURCES = compressscript.cc
-@HAVE_SCRIPT_TRUE@libfstcompressscript_la_LDFLAGS = -version-info 17:0:0
+@HAVE_SCRIPT_TRUE@libfstcompressscript_la_LDFLAGS = -version-info 18:0:0
 @HAVE_SCRIPT_TRUE@libfstcompressscript_la_LIBADD = ../../script/libfstscript.la \
 @HAVE_SCRIPT_TRUE@                                 ../../lib/libfst.la \
 @HAVE_SCRIPT_TRUE@                                 -lz -lm $(DL_LIBS)
index 71d5109..0f96d62 100644 (file)
@@ -7,7 +7,7 @@ libfst_LTLIBRARIES = const8-fst.la const16-fst.la const64-fst.la
 lib_LTLIBRARIES = libfstconst.la
 
 libfstconst_la_SOURCES = const8-fst.cc const16-fst.cc const64-fst.cc
-libfstconst_la_LDFLAGS = -version-info 17:0:0
+libfstconst_la_LDFLAGS = -version-info 18:0:0
 
 const8_fst_la_SOURCES = const8-fst.cc
 const8_fst_la_LDFLAGS = -avoid-version -module
index f88f619..2acde5d 100644 (file)
@@ -371,7 +371,7 @@ AM_CPPFLAGS = -I$(srcdir)/../../include $(ICU_CPPFLAGS)
 libfst_LTLIBRARIES = const8-fst.la const16-fst.la const64-fst.la
 lib_LTLIBRARIES = libfstconst.la
 libfstconst_la_SOURCES = const8-fst.cc const16-fst.cc const64-fst.cc
-libfstconst_la_LDFLAGS = -version-info 17:0:0
+libfstconst_la_LDFLAGS = -version-info 18:0:0
 const8_fst_la_SOURCES = const8-fst.cc
 const8_fst_la_LDFLAGS = -avoid-version -module
 const16_fst_la_SOURCES = const16-fst.cc
index b6859fb..dfc8cfe 100644 (file)
@@ -1,8 +1,8 @@
 // See www.openfst.org for extensive documentation on this weighted
 // finite-state transducer library.
 
-#include <fst/fst.h>
 #include <fst/const-fst.h>
+#include <fst/fst.h>
 
 namespace fst {
 
index 9f059bf..63fb7b1 100644 (file)
@@ -1,8 +1,8 @@
 // See www.openfst.org for extensive documentation on this weighted
 // finite-state transducer library.
 
-#include <fst/fst.h>
 #include <fst/const-fst.h>
+#include <fst/fst.h>
 
 namespace fst {
 
index 04b8064..3bc2a53 100644 (file)
@@ -1,8 +1,8 @@
 // See www.openfst.org for extensive documentation on this weighted
 // finite-state transducer library.
 
-#include <fst/fst.h>
 #include <fst/const-fst.h>
+#include <fst/fst.h>
 
 namespace fst {
 
index a97fa0f..6ea4744 100644 (file)
@@ -7,13 +7,13 @@ lib_LTLIBRARIES = libfstfar.la
 endif
 
 libfstfar_la_SOURCES = sttable.cc stlist.cc
-libfstfar_la_LDFLAGS = -version-info 17:0:0
+libfstfar_la_LDFLAGS = -version-info 18:0:0
 libfstfar_la_LIBADD = ../../lib/libfst.la -lm $(DL_LIBS)
 
 if HAVE_SCRIPT
 libfstfarscript_la_SOURCES = far-class.cc farscript.cc getters.cc script-impl.cc \
-                             strings.cc
-libfstfarscript_la_LDFLAGS = -version-info 17:0:0
+                             strings.cc sttable.cc stlist.cc
+libfstfarscript_la_LDFLAGS = -version-info 18:0:0
 libfstfarscript_la_LIBADD = \
     libfstfar.la ../../script/libfstscript.la \
         ../../lib/libfst.la -lm $(DL_LIBS)
index 9d91943..681056e 100644 (file)
@@ -154,10 +154,10 @@ libfstfar_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CXX $(AM_LIBTOOLFLAGS) \
 @HAVE_SCRIPT_TRUE@     ../../script/libfstscript.la \
 @HAVE_SCRIPT_TRUE@     ../../lib/libfst.la $(am__DEPENDENCIES_1)
 am__libfstfarscript_la_SOURCES_DIST = far-class.cc farscript.cc \
-       getters.cc script-impl.cc strings.cc
+       getters.cc script-impl.cc strings.cc sttable.cc stlist.cc
 @HAVE_SCRIPT_TRUE@am_libfstfarscript_la_OBJECTS = far-class.lo \
 @HAVE_SCRIPT_TRUE@     farscript.lo getters.lo script-impl.lo \
-@HAVE_SCRIPT_TRUE@     strings.lo
+@HAVE_SCRIPT_TRUE@     strings.lo sttable.lo stlist.lo
 libfstfarscript_la_OBJECTS = $(am_libfstfarscript_la_OBJECTS)
 libfstfarscript_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CXX \
        $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=link $(CXXLD) \
@@ -449,12 +449,12 @@ AM_CPPFLAGS = -I$(srcdir)/../../include $(ICU_CPPFLAGS)
 @HAVE_SCRIPT_FALSE@lib_LTLIBRARIES = libfstfar.la
 @HAVE_SCRIPT_TRUE@lib_LTLIBRARIES = libfstfar.la libfstfarscript.la
 libfstfar_la_SOURCES = sttable.cc stlist.cc
-libfstfar_la_LDFLAGS = -version-info 17:0:0
+libfstfar_la_LDFLAGS = -version-info 18:0:0
 libfstfar_la_LIBADD = ../../lib/libfst.la -lm $(DL_LIBS)
 @HAVE_SCRIPT_TRUE@libfstfarscript_la_SOURCES = far-class.cc farscript.cc getters.cc script-impl.cc \
-@HAVE_SCRIPT_TRUE@                             strings.cc
+@HAVE_SCRIPT_TRUE@                             strings.cc sttable.cc stlist.cc
 
-@HAVE_SCRIPT_TRUE@libfstfarscript_la_LDFLAGS = -version-info 17:0:0
+@HAVE_SCRIPT_TRUE@libfstfarscript_la_LDFLAGS = -version-info 18:0:0
 @HAVE_SCRIPT_TRUE@libfstfarscript_la_LIBADD = \
 @HAVE_SCRIPT_TRUE@    libfstfar.la ../../script/libfstscript.la \
 @HAVE_SCRIPT_TRUE@        ../../lib/libfst.la -lm $(DL_LIBS)
index 27b3c56..05ed87c 100644 (file)
@@ -15,7 +15,7 @@ if HAVE_SCRIPT
 lib_LTLIBRARIES = libfstlinearscript.la
 
 libfstlinearscript_la_SOURCES = linearscript.cc
-libfstlinearscript_la_LDFLAGS = -version-info 17:0:0
+libfstlinearscript_la_LDFLAGS = -version-info 18:0:0
 libfstlinearscript_la_LIBADD = ../../script/libfstscript.la
 endif
 
index 7fd8656..961d9d5 100644 (file)
@@ -396,7 +396,7 @@ AM_CPPFLAGS = -I$(srcdir)/../../include $(ICU_CPPFLAGS)
 @HAVE_BIN_TRUE@fstloglinearapply_LDADD = libfstlinearscript.la ../../script/libfstscript.la
 @HAVE_SCRIPT_TRUE@lib_LTLIBRARIES = libfstlinearscript.la
 @HAVE_SCRIPT_TRUE@libfstlinearscript_la_SOURCES = linearscript.cc
-@HAVE_SCRIPT_TRUE@libfstlinearscript_la_LDFLAGS = -version-info 17:0:0
+@HAVE_SCRIPT_TRUE@libfstlinearscript_la_LDFLAGS = -version-info 18:0:0
 @HAVE_SCRIPT_TRUE@libfstlinearscript_la_LIBADD = ../../script/libfstscript.la
 libfst_LTLIBRARIES = linear_tagger-fst.la linear_classifier-fst.la
 linear_tagger_fst_la_SOURCES = linear-tagger-fst.cc
index 9372fc8..4a011f8 100644 (file)
@@ -9,7 +9,7 @@ lib_LTLIBRARIES = libfstlookahead.la
 
 libfstlookahead_la_SOURCES = arc_lookahead-fst.cc ilabel_lookahead-fst.cc \
                              olabel_lookahead-fst.cc
-libfstlookahead_la_LDFLAGS = -version-info 17:0:0
+libfstlookahead_la_LDFLAGS = -version-info 18:0:0
 
 arc_lookahead_fst_la_SOURCES = arc_lookahead-fst.cc
 arc_lookahead_fst_la_LDFLAGS = -avoid-version -module
index 122af87..51b7a0e 100644 (file)
@@ -382,7 +382,7 @@ lib_LTLIBRARIES = libfstlookahead.la
 libfstlookahead_la_SOURCES = arc_lookahead-fst.cc ilabel_lookahead-fst.cc \
                              olabel_lookahead-fst.cc
 
-libfstlookahead_la_LDFLAGS = -version-info 17:0:0
+libfstlookahead_la_LDFLAGS = -version-info 18:0:0
 arc_lookahead_fst_la_SOURCES = arc_lookahead-fst.cc
 arc_lookahead_fst_la_LDFLAGS = -avoid-version -module
 ilabel_lookahead_fst_la_SOURCES = ilabel_lookahead-fst.cc
index 8002e2d..88738ce 100644 (file)
@@ -20,7 +20,7 @@ endif
 if HAVE_SCRIPT
 lib_LTLIBRARIES = libfstmpdtscript.la
 libfstmpdtscript_la_SOURCES = mpdtscript.cc
-libfstmpdtscript_la_LDFLAGS = -version-info 17:0:0
+libfstmpdtscript_la_LDFLAGS = -version-info 18:0:0
 libfstmpdtscript_la_LIBADD = ../../script/libfstscript.la \
                              ../../lib/libfst.la -lm $(DL_LIBS)
 endif
index 1fa56bb..0de3702 100644 (file)
@@ -407,7 +407,7 @@ AM_CPPFLAGS = -I$(srcdir)/../../include $(ICU_CPPFLAGS)
 @HAVE_BIN_TRUE@mpdtreverse_SOURCES = mpdtreverse.cc mpdtreverse-main.cc
 @HAVE_SCRIPT_TRUE@lib_LTLIBRARIES = libfstmpdtscript.la
 @HAVE_SCRIPT_TRUE@libfstmpdtscript_la_SOURCES = mpdtscript.cc
-@HAVE_SCRIPT_TRUE@libfstmpdtscript_la_LDFLAGS = -version-info 17:0:0
+@HAVE_SCRIPT_TRUE@libfstmpdtscript_la_LDFLAGS = -version-info 18:0:0
 @HAVE_SCRIPT_TRUE@libfstmpdtscript_la_LIBADD = ../../script/libfstscript.la \
 @HAVE_SCRIPT_TRUE@                             ../../lib/libfst.la -lm $(DL_LIBS)
 
index ea50f0b..4a6f586 100644 (file)
@@ -10,6 +10,7 @@
 #include <vector>
 
 #include <fst/flags.h>
+#include <fst/types.h>
 #include <fst/log.h>
 #include <fst/extensions/mpdt/mpdtscript.h>
 #include <fst/extensions/mpdt/read_write_utils.h>
index 7cf91b9..f4b5810 100644 (file)
@@ -10,6 +10,7 @@
 #include <vector>
 
 #include <fst/flags.h>
+#include <fst/types.h>
 #include <fst/log.h>
 #include <fst/extensions/mpdt/mpdtscript.h>
 #include <fst/extensions/mpdt/read_write_utils.h>
index 5563f26..231055e 100644 (file)
@@ -11,6 +11,7 @@
 #include <vector>
 
 #include <fst/flags.h>
+#include <fst/types.h>
 #include <fst/log.h>
 #include <fst/extensions/mpdt/mpdtscript.h>
 #include <fst/extensions/mpdt/read_write_utils.h>
index 0a2bec3..88c4d79 100644 (file)
@@ -10,6 +10,7 @@
 #include <vector>
 
 #include <fst/flags.h>
+#include <fst/types.h>
 #include <fst/log.h>
 #include <fst/extensions/mpdt/mpdtscript.h>
 #include <fst/extensions/mpdt/read_write_utils.h>
index cc807f7..a33d449 100644 (file)
@@ -13,6 +13,7 @@
 #include <utility>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/extensions/mpdt/compose.h>
 #include <fst/extensions/mpdt/expand.h>
 #include <fst/extensions/mpdt/reverse.h>
index f7111b2..99ca7b3 100644 (file)
@@ -10,4 +10,4 @@ ngram_fst_la_SOURCES = bitmap-index.cc ngram-fst.cc nthbit.cc
 ngram_fst_la_LDFLAGS = -avoid-version -module
 
 libfstngram_la_SOURCES = bitmap-index.cc ngram-fst.cc nthbit.cc
-libfstngram_la_LDFLAGS = -version-info 17:0:0
+libfstngram_la_LDFLAGS = -version-info 18:0:0
index 5a2d5fa..dab5340 100644 (file)
@@ -355,7 +355,7 @@ lib_LTLIBRARIES = libfstngram.la
 ngram_fst_la_SOURCES = bitmap-index.cc ngram-fst.cc nthbit.cc
 ngram_fst_la_LDFLAGS = -avoid-version -module
 libfstngram_la_SOURCES = bitmap-index.cc ngram-fst.cc nthbit.cc
-libfstngram_la_LDFLAGS = -version-info 17:0:0
+libfstngram_la_LDFLAGS = -version-info 18:0:0
 all: all-am
 
 .SUFFIXES:
index 4b45f51..b44a7c6 100644 (file)
@@ -168,6 +168,10 @@ size_t BitmapIndex::get_index_ones_count(size_t array_index) const {
 }
 
 void BitmapIndex::BuildIndex(const uint64 *bits, size_t num_bits) {
+  // Primary counts are uint32s, so this is the most *set* bits we support
+  // for now.  Just check the number of *input* bits is less than this
+  // to keep things simple.
+  DCHECK_LT(num_bits, uint64{1} << 32);
   bits_ = bits;
   num_bits_ = num_bits;
   primary_index_.resize(primary_index_size());
index 10b249f..7317379 100644 (file)
@@ -3,12 +3,14 @@
 
 #include <fst/extensions/ngram/nthbit.h>
 
-#if defined(__arm__)  // 32-bit ARM
+#include <fst/types.h>
 
-// ARM64 is detected with __aarch64__.
+#if !defined(__BMI2__)  // BMI2 has everything in the header
+#if SIZE_MAX == UINT32_MAX
 
-// ARM32 has a poor implementation of __builtin_popcount, so any nth_bit
-// based on that is slow.  Use this table-based version instead.
+// 32-bit platforms will be slow when using 64-bit operations; use this
+// table-based version instead.  This only contains constant shifts, which
+// have been benchmarked to be fast.
 
 // These tables were generated using:
 //
@@ -213,100 +215,275 @@ static const uint8 nth_bit_bit_pos[8][256] = {
         255, 255, 255, 7,
     }};
 
-uint32 nth_bit(uint64 v, uint32 r) {
+uint32 nth_bit(const uint64 v, uint32 r) {
   // nth_bit uses 1-origin for r, but code below is more natural with 0-origin.
   r--;
-  uint32 next_v = v & 255;
-  uint32 v_in_byte = nth_bit_bit_count[next_v];
-  if (r < v_in_byte) return nth_bit_bit_pos[r][next_v];
-  r -= v_in_byte;
-  next_v = (v >> 8) & 255;
-  v_in_byte = nth_bit_bit_count[next_v];
-  if (r < v_in_byte) return 8 + nth_bit_bit_pos[r][next_v];
-  r -= v_in_byte;
-  next_v = (v >> 16) & 255;
-  v_in_byte = nth_bit_bit_count[next_v];
-  if (r < v_in_byte) return 16 + nth_bit_bit_pos[r][next_v];
-  r -= v_in_byte;
-  next_v = (v >> 24) & 255;
-  v_in_byte = nth_bit_bit_count[next_v];
-  if (r < v_in_byte) return 24 + nth_bit_bit_pos[r][next_v];
-  r -= v_in_byte;
-  next_v = (v >> 32) & 255;
-  v_in_byte = nth_bit_bit_count[next_v];
-  if (r < v_in_byte) return 32 + nth_bit_bit_pos[r][next_v];
-  r -= v_in_byte;
-  next_v = (v >> 40) & 255;
-  v_in_byte = nth_bit_bit_count[next_v];
-  if (r < v_in_byte) return 40 + nth_bit_bit_pos[r][next_v];
-  r -= v_in_byte;
-  next_v = (v >> 48) & 255;
-  v_in_byte = nth_bit_bit_count[next_v];
-  if (r < v_in_byte) return 48 + nth_bit_bit_pos[r][next_v];
-  r -= v_in_byte;
-  next_v = (v >> 56) & 255;
-  v_in_byte = nth_bit_bit_count[next_v];
-  if (r < v_in_byte) return 56 + nth_bit_bit_pos[r][next_v];
+  uint32 next_byte = v & 255;
+  uint32 byte_popcount = nth_bit_bit_count[next_byte];
+  if (r < byte_popcount) return nth_bit_bit_pos[r][next_byte];
+  r -= byte_popcount;
+  next_byte = (v >> 8) & 255;
+  byte_popcount = nth_bit_bit_count[next_byte];
+  if (r < byte_popcount) return 8 + nth_bit_bit_pos[r][next_byte];
+  r -= byte_popcount;
+  next_byte = (v >> 16) & 255;
+  byte_popcount = nth_bit_bit_count[next_byte];
+  if (r < byte_popcount) return 16 + nth_bit_bit_pos[r][next_byte];
+  r -= byte_popcount;
+  next_byte = (v >> 24) & 255;
+  byte_popcount = nth_bit_bit_count[next_byte];
+  if (r < byte_popcount) return 24 + nth_bit_bit_pos[r][next_byte];
+  r -= byte_popcount;
+  next_byte = (v >> 32) & 255;
+  byte_popcount = nth_bit_bit_count[next_byte];
+  if (r < byte_popcount) return 32 + nth_bit_bit_pos[r][next_byte];
+  r -= byte_popcount;
+  next_byte = (v >> 40) & 255;
+  byte_popcount = nth_bit_bit_count[next_byte];
+  if (r < byte_popcount) return 40 + nth_bit_bit_pos[r][next_byte];
+  r -= byte_popcount;
+  next_byte = (v >> 48) & 255;
+  byte_popcount = nth_bit_bit_count[next_byte];
+  if (r < byte_popcount) return 48 + nth_bit_bit_pos[r][next_byte];
+  r -= byte_popcount;
+  next_byte = (v >> 56) & 255;
+  byte_popcount = nth_bit_bit_count[next_byte];
+  if (r < byte_popcount) return 56 + nth_bit_bit_pos[r][next_byte];
   return -1;
 }
 
-#elif !defined(__BMI2__)
-// This table is generated using:
+#elif SIZE_MAX == UINT64_MAX  // 64-bit, non-BMI2
+// These tables are generated using:
 //
-//  for (size_t i = 0; i < 256; ++i) {
-//    uint32 offsets = 0;
-//    for (size_t b = 1; b <= 8; ++b) {
-//      uint32 offset = min<uint32>(nth_bit_scan(i, b), 8);
-//      offsets |= (offset << ((b - 1) << 2));
+//  constexpr uint64 kOnesStep8 = 0x0101010101010101;
+//  printf("const uint64 kPrefixSumOverflow[65] = {\n");
+//  for (int k = 0; k <= 64; ++k) {
+//    printf("  0x%" FST_LL_FORMAT "x,\n",  (0x80 - k) * kOnesStep8);
+//  }
+//  printf("};\n");
+//
+//  printf("const uint8 kSelectInByte[8 * 256] = {\n");
+//  for (int j = 0; j < 8; ++j) {
+//    for (int i = 0; i < 256; ++i) {
+//      if (i > 0) printf(" ");
+//      if (i % 16 == 0) printf("\n  ");
+//      const int k = findbitn(i, j);
+//      printf("%d,", k == -1 ? 0 : k);
 //    }
-//    bit_offset = offsets;
-//    printf("0x%x, ", bit_offset);
-//    if (i % 4 == 3) printf("\n");
+//    printf("\n");
 //  }
+//  printf("};\n");
 //
-const uint32 nth_bit_bit_offset[] = {
-    0x88888888, 0x88888880, 0x88888881, 0x88888810, 0x88888882, 0x88888820,
-    0x88888821, 0x88888210, 0x88888883, 0x88888830, 0x88888831, 0x88888310,
-    0x88888832, 0x88888320, 0x88888321, 0x88883210, 0x88888884, 0x88888840,
-    0x88888841, 0x88888410, 0x88888842, 0x88888420, 0x88888421, 0x88884210,
-    0x88888843, 0x88888430, 0x88888431, 0x88884310, 0x88888432, 0x88884320,
-    0x88884321, 0x88843210, 0x88888885, 0x88888850, 0x88888851, 0x88888510,
-    0x88888852, 0x88888520, 0x88888521, 0x88885210, 0x88888853, 0x88888530,
-    0x88888531, 0x88885310, 0x88888532, 0x88885320, 0x88885321, 0x88853210,
-    0x88888854, 0x88888540, 0x88888541, 0x88885410, 0x88888542, 0x88885420,
-    0x88885421, 0x88854210, 0x88888543, 0x88885430, 0x88885431, 0x88854310,
-    0x88885432, 0x88854320, 0x88854321, 0x88543210, 0x88888886, 0x88888860,
-    0x88888861, 0x88888610, 0x88888862, 0x88888620, 0x88888621, 0x88886210,
-    0x88888863, 0x88888630, 0x88888631, 0x88886310, 0x88888632, 0x88886320,
-    0x88886321, 0x88863210, 0x88888864, 0x88888640, 0x88888641, 0x88886410,
-    0x88888642, 0x88886420, 0x88886421, 0x88864210, 0x88888643, 0x88886430,
-    0x88886431, 0x88864310, 0x88886432, 0x88864320, 0x88864321, 0x88643210,
-    0x88888865, 0x88888650, 0x88888651, 0x88886510, 0x88888652, 0x88886520,
-    0x88886521, 0x88865210, 0x88888653, 0x88886530, 0x88886531, 0x88865310,
-    0x88886532, 0x88865320, 0x88865321, 0x88653210, 0x88888654, 0x88886540,
-    0x88886541, 0x88865410, 0x88886542, 0x88865420, 0x88865421, 0x88654210,
-    0x88886543, 0x88865430, 0x88865431, 0x88654310, 0x88865432, 0x88654320,
-    0x88654321, 0x86543210, 0x88888887, 0x88888870, 0x88888871, 0x88888710,
-    0x88888872, 0x88888720, 0x88888721, 0x88887210, 0x88888873, 0x88888730,
-    0x88888731, 0x88887310, 0x88888732, 0x88887320, 0x88887321, 0x88873210,
-    0x88888874, 0x88888740, 0x88888741, 0x88887410, 0x88888742, 0x88887420,
-    0x88887421, 0x88874210, 0x88888743, 0x88887430, 0x88887431, 0x88874310,
-    0x88887432, 0x88874320, 0x88874321, 0x88743210, 0x88888875, 0x88888750,
-    0x88888751, 0x88887510, 0x88888752, 0x88887520, 0x88887521, 0x88875210,
-    0x88888753, 0x88887530, 0x88887531, 0x88875310, 0x88887532, 0x88875320,
-    0x88875321, 0x88753210, 0x88888754, 0x88887540, 0x88887541, 0x88875410,
-    0x88887542, 0x88875420, 0x88875421, 0x88754210, 0x88887543, 0x88875430,
-    0x88875431, 0x88754310, 0x88875432, 0x88754320, 0x88754321, 0x87543210,
-    0x88888876, 0x88888760, 0x88888761, 0x88887610, 0x88888762, 0x88887620,
-    0x88887621, 0x88876210, 0x88888763, 0x88887630, 0x88887631, 0x88876310,
-    0x88887632, 0x88876320, 0x88876321, 0x88763210, 0x88888764, 0x88887640,
-    0x88887641, 0x88876410, 0x88887642, 0x88876420, 0x88876421, 0x88764210,
-    0x88887643, 0x88876430, 0x88876431, 0x88764310, 0x88876432, 0x88764320,
-    0x88764321, 0x87643210, 0x88888765, 0x88887650, 0x88887651, 0x88876510,
-    0x88887652, 0x88876520, 0x88876521, 0x88765210, 0x88887653, 0x88876530,
-    0x88876531, 0x88765310, 0x88876532, 0x88765320, 0x88765321, 0x87653210,
-    0x88887654, 0x88876540, 0x88876541, 0x88765410, 0x88876542, 0x88765420,
-    0x88765421, 0x87654210, 0x88876543, 0x88765430, 0x88765431, 0x87654310,
-    0x88765432, 0x87654320, 0x87654321, 0x76543210,
+namespace internal {
+
+// clang-format off
+const uint64 kPrefixSumOverflow[65] = {
+  0x8080808080808080,
+  0x7f7f7f7f7f7f7f7f,
+  0x7e7e7e7e7e7e7e7e,
+  0x7d7d7d7d7d7d7d7d,
+  0x7c7c7c7c7c7c7c7c,
+  0x7b7b7b7b7b7b7b7b,
+  0x7a7a7a7a7a7a7a7a,
+  0x7979797979797979,
+  0x7878787878787878,
+  0x7777777777777777,
+  0x7676767676767676,
+  0x7575757575757575,
+  0x7474747474747474,
+  0x7373737373737373,
+  0x7272727272727272,
+  0x7171717171717171,
+  0x7070707070707070,
+  0x6f6f6f6f6f6f6f6f,
+  0x6e6e6e6e6e6e6e6e,
+  0x6d6d6d6d6d6d6d6d,
+  0x6c6c6c6c6c6c6c6c,
+  0x6b6b6b6b6b6b6b6b,
+  0x6a6a6a6a6a6a6a6a,
+  0x6969696969696969,
+  0x6868686868686868,
+  0x6767676767676767,
+  0x6666666666666666,
+  0x6565656565656565,
+  0x6464646464646464,
+  0x6363636363636363,
+  0x6262626262626262,
+  0x6161616161616161,
+  0x6060606060606060,
+  0x5f5f5f5f5f5f5f5f,
+  0x5e5e5e5e5e5e5e5e,
+  0x5d5d5d5d5d5d5d5d,
+  0x5c5c5c5c5c5c5c5c,
+  0x5b5b5b5b5b5b5b5b,
+  0x5a5a5a5a5a5a5a5a,
+  0x5959595959595959,
+  0x5858585858585858,
+  0x5757575757575757,
+  0x5656565656565656,
+  0x5555555555555555,
+  0x5454545454545454,
+  0x5353535353535353,
+  0x5252525252525252,
+  0x5151515151515151,
+  0x5050505050505050,
+  0x4f4f4f4f4f4f4f4f,
+  0x4e4e4e4e4e4e4e4e,
+  0x4d4d4d4d4d4d4d4d,
+  0x4c4c4c4c4c4c4c4c,
+  0x4b4b4b4b4b4b4b4b,
+  0x4a4a4a4a4a4a4a4a,
+  0x4949494949494949,
+  0x4848484848484848,
+  0x4747474747474747,
+  0x4646464646464646,
+  0x4545454545454545,
+  0x4444444444444444,
+  0x4343434343434343,
+  0x4242424242424242,
+  0x4141414141414141,
+  0x4040404040404040
+};
+
+const uint8 kSelectInByte[8 * 256] = {
+  0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+  4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
+
+  0, 0, 0, 1, 0, 2, 2, 1, 0, 3, 3, 1, 3, 2, 2, 1,
+  0, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1,
+  0, 5, 5, 1, 5, 2, 2, 1, 5, 3, 3, 1, 3, 2, 2, 1,
+  5, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1,
+  0, 6, 6, 1, 6, 2, 2, 1, 6, 3, 3, 1, 3, 2, 2, 1,
+  6, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1,
+  6, 5, 5, 1, 5, 2, 2, 1, 5, 3, 3, 1, 3, 2, 2, 1,
+  5, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1,
+  0, 7, 7, 1, 7, 2, 2, 1, 7, 3, 3, 1, 3, 2, 2, 1,
+  7, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1,
+  7, 5, 5, 1, 5, 2, 2, 1, 5, 3, 3, 1, 3, 2, 2, 1,
+  5, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1,
+  7, 6, 6, 1, 6, 2, 2, 1, 6, 3, 3, 1, 3, 2, 2, 1,
+  6, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1,
+  6, 5, 5, 1, 5, 2, 2, 1, 5, 3, 3, 1, 3, 2, 2, 1,
+  5, 4, 4, 1, 4, 2, 2, 1, 4, 3, 3, 1, 3, 2, 2, 1,
+
+  0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 3, 0, 3, 3, 2,
+  0, 0, 0, 4, 0, 4, 4, 2, 0, 4, 4, 3, 4, 3, 3, 2,
+  0, 0, 0, 5, 0, 5, 5, 2, 0, 5, 5, 3, 5, 3, 3, 2,
+  0, 5, 5, 4, 5, 4, 4, 2, 5, 4, 4, 3, 4, 3, 3, 2,
+  0, 0, 0, 6, 0, 6, 6, 2, 0, 6, 6, 3, 6, 3, 3, 2,
+  0, 6, 6, 4, 6, 4, 4, 2, 6, 4, 4, 3, 4, 3, 3, 2,
+  0, 6, 6, 5, 6, 5, 5, 2, 6, 5, 5, 3, 5, 3, 3, 2,
+  6, 5, 5, 4, 5, 4, 4, 2, 5, 4, 4, 3, 4, 3, 3, 2,
+  0, 0, 0, 7, 0, 7, 7, 2, 0, 7, 7, 3, 7, 3, 3, 2,
+  0, 7, 7, 4, 7, 4, 4, 2, 7, 4, 4, 3, 4, 3, 3, 2,
+  0, 7, 7, 5, 7, 5, 5, 2, 7, 5, 5, 3, 5, 3, 3, 2,
+  7, 5, 5, 4, 5, 4, 4, 2, 5, 4, 4, 3, 4, 3, 3, 2,
+  0, 7, 7, 6, 7, 6, 6, 2, 7, 6, 6, 3, 6, 3, 3, 2,
+  7, 6, 6, 4, 6, 4, 4, 2, 6, 4, 4, 3, 4, 3, 3, 2,
+  7, 6, 6, 5, 6, 5, 5, 2, 6, 5, 5, 3, 5, 3, 3, 2,
+  6, 5, 5, 4, 5, 4, 4, 2, 5, 4, 4, 3, 4, 3, 3, 2,
+
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3,
+  0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 4, 0, 4, 4, 3,
+  0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 5, 0, 5, 5, 3,
+  0, 0, 0, 5, 0, 5, 5, 4, 0, 5, 5, 4, 5, 4, 4, 3,
+  0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 6, 0, 6, 6, 3,
+  0, 0, 0, 6, 0, 6, 6, 4, 0, 6, 6, 4, 6, 4, 4, 3,
+  0, 0, 0, 6, 0, 6, 6, 5, 0, 6, 6, 5, 6, 5, 5, 3,
+  0, 6, 6, 5, 6, 5, 5, 4, 6, 5, 5, 4, 5, 4, 4, 3,
+  0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 7, 0, 7, 7, 3,
+  0, 0, 0, 7, 0, 7, 7, 4, 0, 7, 7, 4, 7, 4, 4, 3,
+  0, 0, 0, 7, 0, 7, 7, 5, 0, 7, 7, 5, 7, 5, 5, 3,
+  0, 7, 7, 5, 7, 5, 5, 4, 7, 5, 5, 4, 5, 4, 4, 3,
+  0, 0, 0, 7, 0, 7, 7, 6, 0, 7, 7, 6, 7, 6, 6, 3,
+  0, 7, 7, 6, 7, 6, 6, 4, 7, 6, 6, 4, 6, 4, 4, 3,
+  0, 7, 7, 6, 7, 6, 6, 5, 7, 6, 6, 5, 6, 5, 5, 3,
+  7, 6, 6, 5, 6, 5, 5, 4, 6, 5, 5, 4, 5, 4, 4, 3,
+
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5,
+  0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 5, 0, 5, 5, 4,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6,
+  0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 6, 0, 6, 6, 4,
+  0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 6, 0, 6, 6, 5,
+  0, 0, 0, 6, 0, 6, 6, 5, 0, 6, 6, 5, 6, 5, 5, 4,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7,
+  0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 7, 0, 7, 7, 4,
+  0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 7, 0, 7, 7, 5,
+  0, 0, 0, 7, 0, 7, 7, 5, 0, 7, 7, 5, 7, 5, 5, 4,
+  0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 7, 0, 7, 7, 6,
+  0, 0, 0, 7, 0, 7, 7, 6, 0, 7, 7, 6, 7, 6, 6, 4,
+  0, 0, 0, 7, 0, 7, 7, 6, 0, 7, 7, 6, 7, 6, 6, 5,
+  0, 7, 7, 6, 7, 6, 6, 5, 7, 6, 6, 5, 6, 5, 5, 4,
+
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6,
+  0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 6, 0, 6, 6, 5,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7,
+  0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 7, 0, 7, 7, 5,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7,
+  0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 7, 0, 7, 7, 6,
+  0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 7, 0, 7, 7, 6,
+  0, 0, 0, 7, 0, 7, 7, 6, 0, 7, 7, 6, 7, 6, 6, 5,
+
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7,
+  0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 7, 0, 7, 7, 6,
+
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7
 };
-#endif  // !defined(__BMI2__) && !defined(__arm__)
+// clang-format on
+
+}  // namespace internal
+#endif  // 64-bit, non-BMI2
+#endif  // !defined(__BMI2__)
index 3b98616..70f7dbf 100644 (file)
@@ -24,7 +24,7 @@ endif
 if HAVE_SCRIPT
 lib_LTLIBRARIES = libfstpdtscript.la
 libfstpdtscript_la_SOURCES = getters.cc pdtscript.cc
-libfstpdtscript_la_LDFLAGS = -version-info 17:0:0
+libfstpdtscript_la_LDFLAGS = -version-info 18:0:0
 libfstpdtscript_la_LIBADD = ../../script/libfstscript.la \
                             ../../lib/libfst.la -lm $(DL_LIBS)
 endif
index 2cd6e0e..05122ba 100644 (file)
@@ -428,7 +428,7 @@ AM_CPPFLAGS = -I$(srcdir)/../../include $(ICU_CPPFLAGS)
 @HAVE_BIN_TRUE@pdtshortestpath_SOURCES = pdtshortestpath.cc pdtshortestpath-main.cc
 @HAVE_SCRIPT_TRUE@lib_LTLIBRARIES = libfstpdtscript.la
 @HAVE_SCRIPT_TRUE@libfstpdtscript_la_SOURCES = getters.cc pdtscript.cc
-@HAVE_SCRIPT_TRUE@libfstpdtscript_la_LDFLAGS = -version-info 17:0:0
+@HAVE_SCRIPT_TRUE@libfstpdtscript_la_LDFLAGS = -version-info 18:0:0
 @HAVE_SCRIPT_TRUE@libfstpdtscript_la_LIBADD = ../../script/libfstscript.la \
 @HAVE_SCRIPT_TRUE@                            ../../lib/libfst.la -lm $(DL_LIBS)
 
index 1250141..39f4afb 100644 (file)
@@ -10,6 +10,7 @@
 #include <vector>
 
 #include <fst/flags.h>
+#include <fst/types.h>
 #include <fst/log.h>
 #include <fst/extensions/pdt/getters.h>
 #include <fst/extensions/pdt/pdtscript.h>
index 3d0d767..8ba6937 100644 (file)
@@ -10,6 +10,7 @@
 #include <vector>
 
 #include <fst/flags.h>
+#include <fst/types.h>
 #include <fst/log.h>
 #include <fst/extensions/pdt/pdtscript.h>
 #include <fst/util.h>
index 3915b40..71a6bc0 100644 (file)
@@ -11,6 +11,7 @@
 #include <vector>
 
 #include <fst/flags.h>
+#include <fst/types.h>
 #include <fst/log.h>
 #include <fst/extensions/pdt/pdtscript.h>
 #include <fst/util.h>
index cc83dce..c983210 100644 (file)
@@ -9,6 +9,7 @@
 #include <vector>
 
 #include <fst/flags.h>
+#include <fst/types.h>
 #include <fst/extensions/pdt/getters.h>
 #include <fst/extensions/pdt/pdtscript.h>
 #include <fst/util.h>
index e882d99..988db91 100644 (file)
@@ -10,6 +10,7 @@
 #include <vector>
 
 #include <fst/flags.h>
+#include <fst/types.h>
 #include <fst/log.h>
 #include <fst/extensions/pdt/pdtscript.h>
 #include <fst/util.h>
index dd9896f..0a2bf61 100644 (file)
@@ -13,6 +13,7 @@
 #include <utility>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/extensions/pdt/compose.h>
 #include <fst/extensions/pdt/expand.h>
 #include <fst/extensions/pdt/replace.h>
index 6c38f79..7978dbb 100644 (file)
@@ -10,6 +10,7 @@
 #include <vector>
 
 #include <fst/flags.h>
+#include <fst/types.h>
 #include <fst/log.h>
 #include <fst/extensions/pdt/pdtscript.h>
 #include <fst/util.h>
index 36aa4d2..8427bef 100644 (file)
@@ -15,4 +15,4 @@ pywrapfst_la_LIBADD = ../far/libfstfarscript.la ../far/libfstfar.la \
                       -lm $(DL_LIBS) $(PYTHON_LIBS)
 
 # Exports the *.pxd/*.pxd source files.
-EXTRA_DIST = basictypes.pxd fst.pxd ios.pxd pywrapfst.pxd pywrapfst.pyx
+EXTRA_DIST = basictypes.pxd cpywrapfst.pxd ios.pxd pywrapfst.pxd pywrapfst.pyx
index fed0707..824f36c 100644 (file)
@@ -361,7 +361,7 @@ pywrapfst_la_LIBADD = ../far/libfstfarscript.la ../far/libfstfar.la \
 
 
 # Exports the *.pxd/*.pxd source files.
-EXTRA_DIST = basictypes.pxd fst.pxd ios.pxd pywrapfst.pxd pywrapfst.pyx
+EXTRA_DIST = basictypes.pxd cpywrapfst.pxd ios.pxd pywrapfst.pxd pywrapfst.pyx
 all: all-am
 
 .SUFFIXES:
index 80903ca..823d68d 100644 (file)
@@ -15,7 +15,7 @@ from libcpp.vector cimport vector
 
 from basictypes cimport *
 
-cimport fst as fst
+cimport cpywrapfst as fst
 
 from ios cimport ostream
 from ios cimport ofstream
index 114067c..2eae237 100644 (file)
@@ -15,7 +15,7 @@ libfst_LTLIBRARIES = phi-fst.la rho-fst.la sigma-fst.la
 lib_LTLIBRARIES = libfstspecial.la
 
 libfstspecial_la_SOURCES = phi-fst.cc rho-fst.cc sigma-fst.cc
-libfstspecial_la_LDFLAGS = -version-info 17:0:0
+libfstspecial_la_LDFLAGS = -version-info 18:0:0
 
 phi_fst_la_SOURCES = phi-fst.cc
 phi_fst_la_LDFLAGS = -avoid-version -module
index 72fbedf..c20d421 100644 (file)
@@ -388,7 +388,7 @@ AM_CPPFLAGS = -I$(srcdir)/../../include -I$(srcdir)/../../bin $(ICU_CPPFLAGS)
 libfst_LTLIBRARIES = phi-fst.la rho-fst.la sigma-fst.la
 lib_LTLIBRARIES = libfstspecial.la
 libfstspecial_la_SOURCES = phi-fst.cc rho-fst.cc sigma-fst.cc
-libfstspecial_la_LDFLAGS = -version-info 17:0:0
+libfstspecial_la_LDFLAGS = -version-info 18:0:0
 phi_fst_la_SOURCES = phi-fst.cc
 phi_fst_la_LDFLAGS = -avoid-version -module
 rho_fst_la_SOURCES = rho-fst.cc
index cd053d3..28220cb 100644 (file)
@@ -220,7 +220,7 @@ class FastLogAccumulator {
         state_weights_(nullptr),
         error_(false) {}
 
-  FastLogAccumulator(const FastLogAccumulator<Arc> &acc, bool safe = false)
+  FastLogAccumulator(const FastLogAccumulator &acc, bool safe = false)
       : to_log_weight_(),
         to_weight_(),
         arc_limit_(acc.arc_limit_),
@@ -471,7 +471,7 @@ class CacheLogAccumulator {
         s_(kNoStateId),
         error_(false) {}
 
-  CacheLogAccumulator(const CacheLogAccumulator<Arc> &acc, bool safe = false)
+  CacheLogAccumulator(const CacheLogAccumulator &acc, bool safe = false)
       : arc_limit_(acc.arc_limit_),
         fst_(acc.fst_ ? acc.fst_->Copy() : nullptr),
         data_(safe ? std::make_shared<CacheLogAccumulatorData<Arc>>(*acc.data_)
index 92399ca..92dd5d5 100644 (file)
 #include <string>
 #include <utility>
 
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/fst.h>
 
-
 namespace fst {
 
 // Identifies stream data as an add-on FST.
@@ -54,20 +54,18 @@ class AddOnPair {
 
   std::shared_ptr<A2> SharedSecond() const { return a2_; }
 
-  static AddOnPair<A1, A2> *Read(std::istream &istrm,
-                                 const FstReadOptions &opts) {
-    A1 *a1 = nullptr;
+  static AddOnPair *Read(std::istream &istrm, const FstReadOptions &opts) {
     bool have_addon1 = false;
     ReadType(istrm, &have_addon1);
-    if (have_addon1) a1 = A1::Read(istrm, opts);
+    std::unique_ptr<A1> a1;
+    if (have_addon1) a1 = fst::WrapUnique(A1::Read(istrm, opts));
 
-    A2 *a2 = nullptr;
     bool have_addon2 = false;
     ReadType(istrm, &have_addon2);
-    if (have_addon2) a2 = A2::Read(istrm, opts);
+    std::unique_ptr<A1> a2;
+    if (have_addon2) a2 = fst::WrapUnique(A2::Read(istrm, opts));
 
-    return new AddOnPair<A1, A2>(std::shared_ptr<A1>(a1),
-                                 std::shared_ptr<A2>(a2));
+    return new AddOnPair(std::move(a1), std::move(a2));
   }
 
   bool Write(std::ostream &ostrm, const FstWriteOptions &opts) const {
@@ -110,7 +108,7 @@ class AddOnImpl : public FstImpl<typename FST::Arc> {
   // We make a thread-safe copy of the FST by default since an FST
   // implementation is expected to not share mutable data between objects.
   AddOnImpl(const FST &fst, const std::string &type,
-            std::shared_ptr<T> t = std::shared_ptr<T>())
+            std::shared_ptr<T> t = nullptr)
       : fst_(fst, true), t_(std::move(t)) {
     SetType(type);
     SetProperties(fst_.Properties(kFstProperties, false));
@@ -121,7 +119,7 @@ class AddOnImpl : public FstImpl<typename FST::Arc> {
   // Conversion from const Fst<Arc> & to F always copies the underlying
   // implementation.
   AddOnImpl(const Fst<Arc> &fst, const std::string &type,
-            std::shared_ptr<T> t = std::shared_ptr<T>())
+            std::shared_ptr<T> t = nullptr)
       : fst_(fst), t_(std::move(t)) {
     SetType(type);
     SetProperties(fst_.Properties(kFstProperties, false));
@@ -131,8 +129,7 @@ class AddOnImpl : public FstImpl<typename FST::Arc> {
 
   // We make a thread-safe copy of the FST by default since an FST
   // implementation is expected to not share mutable data between objects.
-  AddOnImpl(const AddOnImpl<FST, T> &impl)
-      : fst_(impl.fst_, true), t_(impl.t_) {
+  AddOnImpl(const AddOnImpl &impl) : fst_(impl.fst_, true), t_(impl.t_) {
     SetType(impl.Type());
     SetProperties(fst_.Properties(kCopyProperties, false));
     SetInputSymbols(fst_.InputSymbols());
@@ -153,16 +150,14 @@ class AddOnImpl : public FstImpl<typename FST::Arc> {
 
   size_t NumStates() const { return fst_.NumStates(); }
 
-  static AddOnImpl<FST, T> *Read(std::istream &strm,
-                                 const FstReadOptions &opts) {
+  static AddOnImpl *Read(std::istream &strm, const FstReadOptions &opts) {
     FstReadOptions nopts(opts);
     FstHeader hdr;
     if (!nopts.header) {
       hdr.Read(strm, nopts.source);
       nopts.header = &hdr;
     }
-    std::unique_ptr<AddOnImpl<FST, T>> impl(
-        new AddOnImpl<FST, T>(nopts.header->FstType()));
+    std::unique_ptr<AddOnImpl> impl(new AddOnImpl(nopts.header->FstType()));
     if (!impl->ReadHeader(strm, nopts, kMinFileVersion, &hdr)) return nullptr;
     impl.reset();
     int32 magic_number = 0;
@@ -182,7 +177,7 @@ class AddOnImpl : public FstImpl<typename FST::Arc> {
       t = std::shared_ptr<T>(T::Read(strm, fopts));
       if (!t) return nullptr;
     }
-    return new AddOnImpl<FST, T>(*fst, nopts.header->FstType(), t);
+    return new AddOnImpl(*fst, nopts.header->FstType(), t);
   }
 
   bool Write(std::ostream &strm, const FstWriteOptions &opts) const {
index da0a3f5..fc060ac 100644 (file)
@@ -10,6 +10,7 @@
 #include <memory>
 #include <utility>
 
+#include <fst/types.h>
 #include <fst/fst.h>
 #include <fst/memory.h>
 #include <unordered_map>
@@ -145,7 +146,7 @@ class ArcArenaStateStore {
 
   class State {
    public:
-    Weight Final() const { return final_; }
+    Weight Final() const { return final_weight_; }
 
     size_t NumInputEpsilons() const { return niepsilons_; }
 
@@ -160,15 +161,15 @@ class ArcArenaStateStore {
     int* MutableRefCount() const { return nullptr; }
 
    private:
-    State(Weight weight, int32 niepsilons, int32 noepsilons, int32 narcs,
+    State(Weight final_weight, int32 niepsilons, int32 noepsilons, int32 narcs,
           const Arc *arcs)
-        : final_(std::move(weight)),
+        : final_weight_(std::move(final_weight)),
           niepsilons_(niepsilons),
           noepsilons_(noepsilons),
           narcs_(narcs),
           arcs_(arcs) {}
 
-    Weight final_;
+    Weight final_weight_;
     size_t niepsilons_;
     size_t noepsilons_;
     size_t narcs_;
@@ -193,7 +194,7 @@ class ArcArenaStateStore {
       if (arcs[i].olabel == 0) ++noepsilons;
     }
     states_.emplace_back(
-        State(builder.final_, niepsilons, noepsilons, narcs, arcs));
+        State(builder.final_weight_, niepsilons, noepsilons, narcs, arcs));
     // Places it in the cache.
     auto state = &states_.back();
     it.first->second = state;
@@ -208,10 +209,10 @@ class ArcArenaStateStore {
  private:
   class StateBuilder {
    public:
-    explicit StateBuilder(ArcArena<Arc>arena)
-       : arena_(arena), final_(Weight::Zero()), narcs_(0) {}
+    explicit StateBuilder(ArcArena<Arc> *arena)
+        : arena_(arena), final_weight_(Weight::Zero()), narcs_(0) {}
 
-    void SetFinal(Weight weight) { final_ = std::move(weight); }
+    void SetFinal(Weight weight) { final_weight_ = std::move(weight); }
 
     void ReserveArcs(size_t n) { arena_->ReserveArcs(n); }
 
@@ -224,7 +225,7 @@ class ArcArenaStateStore {
     friend class ArcArenaStateStore<Arc>;
 
     ArcArena<Arc> *arena_;
-    Weight final_;
+    Weight final_weight_;
     size_t narcs_;
   };
 
index e74a637..d39809d 100644 (file)
@@ -12,6 +12,7 @@
 #include <unordered_map>
 #include <utility>
 
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/cache.h>
@@ -540,12 +541,12 @@ class ArcMapFst : public ImplToFst<internal::ArcMapFstImpl<A, B, C>> {
             std::make_shared<Impl>(fst, mapper, ArcMapFstOptions())) {}
 
   // See Fst<>::Copy() for doc.
-  ArcMapFst(const ArcMapFst<A, B, C> &fst, bool safe = false)
+  ArcMapFst(const ArcMapFst &fst, bool safe = false)
       : ImplToFst<Impl>(fst, safe) {}
 
   // Get a copy of this ArcMapFst. See Fst<>::Copy() for further doc.
-  ArcMapFst<A, B, C> *Copy(bool safe = false) const override {
-    return new ArcMapFst<A, B, C>(*this, safe);
+  ArcMapFst *Copy(bool safe = false) const override {
+    return new ArcMapFst(*this, safe);
   }
 
   inline void InitStateIterator(StateIteratorData<B> *data) const override;
index e1260b6..8733c6d 100644 (file)
@@ -10,6 +10,8 @@
 #include <string>
 #include <vector>
 
+#include <fst/types.h>
+
 #include <fst/cache.h>
 #include <fst/state-map.h>
 #include <fst/test-properties.h>
@@ -124,11 +126,11 @@ class ArcSortFst : public StateMapFst<Arc, Arc, ArcSortMapper<Arc, Compare>> {
       : StateMapFst<Arc, Arc, Mapper>(fst, Mapper(fst, comp), opts) {}
 
   // See Fst<>::Copy() for doc.
-  ArcSortFst(const ArcSortFst<Arc, Compare> &fst, bool safe = false)
+  ArcSortFst(const ArcSortFst &fst, bool safe = false)
       : StateMapFst<Arc, Arc, Mapper>(fst, safe) {}
 
   // Gets a copy of this ArcSortFst. See Fst<>::Copy() for further doc.
-  ArcSortFst<Arc, Compare> *Copy(bool safe = false) const override {
+  ArcSortFst *Copy(bool safe = false) const override {
     return new ArcSortFst(*this, safe);
   }
 
index fa76f26..270362a 100644 (file)
@@ -15,6 +15,7 @@
 #include <unordered_set>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/log.h>
 #include <fst/memory.h>
 #include <unordered_set>
index 2aa3314..d85c1c1 100644 (file)
@@ -12,6 +12,7 @@
 #include <vector>
 
 #include <fst/flags.h>
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/vector-fst.h>
@@ -74,7 +75,7 @@ class CacheState {
 
   // Provides STL allocator for arcs.
   explicit CacheState(const ArcAllocator &alloc)
-      : final_(Weight::Zero()),
+      : final_weight_(Weight::Zero()),
         niepsilons_(0),
         noepsilons_(0),
         arcs_(alloc),
@@ -82,7 +83,7 @@ class CacheState {
         ref_count_(0) {}
 
   CacheState(const CacheState<A> &state, const ArcAllocator &alloc)
-      : final_(state.Final()),
+      : final_weight_(state.Final()),
         niepsilons_(state.NumInputEpsilons()),
         noepsilons_(state.NumOutputEpsilons()),
         arcs_(state.arcs_.begin(), state.arcs_.end(), alloc),
@@ -90,7 +91,7 @@ class CacheState {
         ref_count_(0) {}
 
   void Reset() {
-    final_ = Weight::Zero();
+    final_weight_ = Weight::Zero();
     niepsilons_ = 0;
     noepsilons_ = 0;
     ref_count_ = 0;
@@ -98,7 +99,7 @@ class CacheState {
     arcs_.clear();
   }
 
-  Weight Final() const { return final_; }
+  Weight Final() const { return final_weight_; }
 
   size_t NumInputEpsilons() const { return niepsilons_; }
 
@@ -117,7 +118,9 @@ class CacheState {
   // Accesses ref count; used by the caller.
   int RefCount() const { return ref_count_; }
 
-  void SetFinal(Weight weight = Weight::One()) { final_ = std::move(weight); }
+  void SetFinal(Weight weight = Weight::One()) {
+    final_weight_ = std::move(weight);
+  }
 
   void ReserveArcs(size_t n) { arcs_.reserve(n); }
 
@@ -209,7 +212,7 @@ class CacheState {
     if (arc.olabel == 0) ++noepsilons_;
   }
 
-  Weight final_;                         // Final weight.
+  Weight final_weight_;                  // Final weight.
   size_t niepsilons_;                    // # of input epsilons.
   size_t noepsilons_;                    // # of output epsilons.
   std::vector<Arc, ArcAllocator> arcs_;  // Arcs representation.
@@ -302,7 +305,7 @@ class VectorCacheStore {
 
   ~VectorCacheStore() { Clear(); }
 
-  VectorCacheStore<State> &operator=(const VectorCacheStore<State> &store) {
+  VectorCacheStore &operator=(const VectorCacheStore &store) {
     if (this != &store) {
       CopyStates(store);
       Reset();
@@ -428,7 +431,7 @@ class HashCacheStore {
 
   ~HashCacheStore() { Clear(); }
 
-  HashCacheStore<State> &operator=(const HashCacheStore<State> &store) {
+  HashCacheStore &operator=(const HashCacheStore &store) {
     if (this != &store) {
       CopyStates(store);
       Reset();
index efcc20c..1b303e8 100644 (file)
@@ -9,6 +9,8 @@
 #include <algorithm>
 #include <vector>
 
+#include <fst/types.h>
+
 #include <fst/mutable-fst.h>
 #include <fst/rational.h>
 
@@ -95,12 +97,12 @@ class ClosureFst : public RationalFst<A> {
   }
 
   // See Fst<>::Copy() for doc.
-  ClosureFst(const ClosureFst<Arc> &fst, bool safe = false)
+  ClosureFst(const ClosureFst &fst, bool safe = false)
       : RationalFst<A>(fst, safe) {}
 
   // Gets a copy of this ClosureFst. See Fst<>::Copy() for further doc.
-  ClosureFst<A> *Copy(bool safe = false) const override {
-    return new ClosureFst<A>(*this, safe);
+  ClosureFst *Copy(bool safe = false) const override {
+    return new ClosureFst(*this, safe);
   }
 
  private:
index e657cd2..d0f9560 100644 (file)
@@ -14,6 +14,7 @@
 #include <utility>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/cache.h>
@@ -24,7 +25,6 @@
 #include <fst/test-properties.h>
 #include <fst/util.h>
 
-
 namespace fst {
 
 struct CompactFstOptions : public CacheOptions {
@@ -47,7 +47,9 @@ struct CompactFstOptions : public CacheOptions {
 //                      shared_ptr<Compactor> compactor = nullptr);
 //   // Copy constructor.  Must make a thread-safe copy suitable for use by
 //   // by Fst::Copy(/*safe=*/true).  Only thread-unsafe data structures
-//   // need to be deeply copied.
+//   // need to be deeply copied.  Ideally, this constructor is O(1) and any
+//   // large structures are thread-safe and shared, while small ones may
+//   // need to be copied.
 //   Compactor(const Compactor &compactor);
 //   // Default constructor (optional, see comment below).
 //   Compactor();
@@ -179,10 +181,12 @@ struct CompactFstOptions : public CacheOptions {
 //   FSTERROR() << "Compactor: No default constructor";
 // }
 
-// Default implementation data for DefaultCompactor.  DefaultCompactStore
-// is thread-safe and can be shared between otherwise independent copies.
-// Only old-style ArcCompactors are supported because the DefaultCompactStore
-// constructors use the old API.
+// Default implementation data for DefaultCompactor.  Only old-style
+// ArcCompactors are supported because the DefaultCompactStore constructors
+// use the old API.
+//
+// DefaultCompact store is thread-compatible, but not thread-safe.
+// The copy constructor makes a thread-safe copy.
 //
 // The implementation contains two arrays: 'states_' and 'compacts_'.
 //
@@ -208,6 +212,9 @@ class DefaultCompactStore {
  public:
   DefaultCompactStore() = default;
 
+  // Makes a thread-safe copy. O(1).
+  DefaultCompactStore(const DefaultCompactStore &) = default;
+
   template <class Arc, class ArcCompactor>
   DefaultCompactStore(const Fst<Arc> &fst, const ArcCompactor &arc_compactor);
 
@@ -215,10 +222,7 @@ class DefaultCompactStore {
   DefaultCompactStore(const Iterator begin, const Iterator end,
                       const ArcCompactor &arc_compactor);
 
-  ~DefaultCompactStore() {
-    if (!states_region_) delete[] states_;
-    if (!compacts_region_) delete[] compacts_;
-  }
+  ~DefaultCompactStore() = default;
 
   template <class ArcCompactor>
   static DefaultCompactStore *Read(std::istream &strm,
@@ -253,9 +257,11 @@ class DefaultCompactStore {
   static const std::string &Type();
 
  private:
-  std::unique_ptr<MappedFile> states_region_;
-  std::unique_ptr<MappedFile> compacts_region_;
+  std::shared_ptr<MappedFile> states_region_;
+  std::shared_ptr<MappedFile> compacts_region_;
+  // Unowned pointer into states_region_.
   Unsigned *states_ = nullptr;
+  // Unowned pointer into compacts_region_.
   Element *compacts_ = nullptr;
   size_t nstates_  = 0;
   size_t ncompacts_ = 0;
@@ -280,9 +286,13 @@ DefaultCompactStore<Element, Unsigned>::DefaultCompactStore(
     if (fst.Final(s) != Weight::Zero()) ++nfinals;
   }
   if (arc_compactor.Size() == -1) {
-    states_ = new Unsigned[nstates_ + 1];
+    states_region_ = fst::WrapUnique(
+        MappedFile::Allocate(sizeof(states_[0]) * (nstates_ + 1)));
+    states_ = static_cast<Unsigned *>(states_region_->mutable_data());
     ncompacts_ = narcs_ + nfinals;
-    compacts_ = new Element[ncompacts_];
+    compacts_region_ = fst::WrapUnique(
+        MappedFile::Allocate(sizeof(compacts_[0]) * (ncompacts_)));
+    compacts_ = static_cast<Element *>(compacts_region_->mutable_data());
     states_[nstates_] = ncompacts_;
   } else {
     states_ = nullptr;
@@ -292,7 +302,9 @@ DefaultCompactStore<Element, Unsigned>::DefaultCompactStore(
       error_ = true;
       return;
     }
-    compacts_ = new Element[ncompacts_];
+    compacts_region_ = fst::WrapUnique(
+        MappedFile::Allocate(sizeof(compacts_[0]) * (ncompacts_)));
+    compacts_ = static_cast<Element *>(compacts_region_->mutable_data());
   }
   size_t pos = 0;
   size_t fpos = 0;
@@ -348,7 +360,9 @@ DefaultCompactStore<Element, Unsigned>::DefaultCompactStore(
     if (ncompacts_ == 0) return;
     start_ = 0;
     nstates_ = ncompacts_ / arc_compactor.Size();
-    compacts_ = new Element[ncompacts_];
+    compacts_region_ = fst::WrapUnique(
+        MappedFile::Allocate(sizeof(compacts_[0]) * (ncompacts_)));
+    compacts_ = static_cast<Element *>(compacts_region_->mutable_data());
     size_t i = 0;
     Iterator it = begin;
     for (; it != end; ++it, ++i) {
@@ -374,8 +388,12 @@ DefaultCompactStore<Element, Unsigned>::DefaultCompactStore(
       }
     }
     start_ = 0;
-    compacts_ = new Element[ncompacts_];
-    states_ = new Unsigned[nstates_ + 1];
+    compacts_region_ = fst::WrapUnique(
+        MappedFile::Allocate(sizeof(compacts_[0]) * (ncompacts_)));
+    compacts_ = static_cast<Element *>(compacts_region_->mutable_data());
+    states_region_ = fst::WrapUnique(
+        MappedFile::Allocate(sizeof(states_[0]) * (nstates_ + 1)));
+    states_ = static_cast<Unsigned *>(states_region_->mutable_data());
     states_[nstates_] = ncompacts_;
     size_t i = 0;
     size_t s = 0;
@@ -480,7 +498,8 @@ const std::string &DefaultCompactStore<Element, Unsigned>::Type() {
 template <class C, class U, class S> class DefaultCompactState;
 
 // Wraps an old-style arc compactor and a compact store as a new Fst compactor.
-// S must be thread-safe.
+// The copy constructors of AC and S must make thread-safe copies and should
+// be O(1).
 template <class AC, class U,
           class S /*= DefaultCompactStore<typename AC::Element, U>*/>
 class DefaultCompactor {
@@ -555,17 +574,28 @@ class DefaultCompactor {
       : DefaultCompactor(b, e, std::make_shared<ArcCompactor>()) {}
 
   // Copy constructor.  This makes a thread-safe copy, so requires that
-  // CompactStore is thread-safe.
+  // The ArcCompactor and CompactStore copy constructores make thread-safe
+  // copies.
   DefaultCompactor(const DefaultCompactor &compactor)
       : arc_compactor_(
-            std::make_shared<ArcCompactor>(*compactor.GetArcCompactor())),
-        compact_store_(compactor.SharedCompactStore()) {}
+            compactor.GetArcCompactor() == nullptr
+                ? nullptr
+                : std::make_shared<ArcCompactor>(*compactor.GetArcCompactor())),
+        compact_store_(compactor.GetCompactStore() == nullptr
+                           ? nullptr
+                           : std::make_shared<CompactStore>(
+                                 *compactor.GetCompactStore())) {}
 
   template <class OtherC>
   explicit DefaultCompactor(const DefaultCompactor<OtherC, U, S> &compactor)
       : arc_compactor_(
-            std::make_shared<ArcCompactor>(*compactor.GetArcCompactor())),
-        compact_store_(compactor.SharedCompactStore()) {}
+            compactor.GetArcCompactor() == nullptr
+                ? nullptr
+                : std::make_shared<ArcCompactor>(*compactor.GetArcCompactor())),
+        compact_store_(compactor.GetCompactStore() == nullptr
+                           ? nullptr
+                           : std::make_shared<CompactStore>(
+                                 *compactor.GetCompactStore())) {}
 
   StateId Start() const { return compact_store_->Start(); }
   StateId NumStates() const { return compact_store_->NumStates(); }
@@ -619,7 +649,7 @@ class DefaultCompactor {
   }
 
   const ArcCompactor *GetArcCompactor() const { return arc_compactor_.get(); }
-  CompactStore *GetCompactStore() const { return compact_store_.get(); }
+  const CompactStore *GetCompactStore() const { return compact_store_.get(); }
 
   std::shared_ptr<ArcCompactor> SharedArcCompactor() const {
     return arc_compactor_;
index fa097bf..cc4a6ee 100644 (file)
@@ -10,6 +10,7 @@
 #include <string>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/fst.h>
@@ -136,12 +137,12 @@ class ComplementFst : public ImplToFst<internal::ComplementFstImpl<A>> {
   }
 
   // See Fst<>::Copy() for doc.
-  ComplementFst(const ComplementFst<Arc> &fst, bool safe = false)
+  ComplementFst(const ComplementFst &fst, bool safe = false)
       : ImplToFst<Impl>(fst, safe) {}
 
   // Gets a copy of this FST. See Fst<>::Copy() for further doc.
-  ComplementFst<Arc> *Copy(bool safe = false) const override {
-    return new ComplementFst<Arc>(*this, safe);
+  ComplementFst *Copy(bool safe = false) const override {
+    return new ComplementFst(*this, safe);
   }
 
   inline void InitStateIterator(StateIteratorData<Arc> *data) const override;
index 7251e27..27700a0 100644 (file)
@@ -7,6 +7,8 @@
 #ifndef FST_COMPOSE_FILTER_H_
 #define FST_COMPOSE_FILTER_H_
 
+#include <fst/types.h>
+
 #include <fst/filter-state.h>
 #include <fst/fst-decl.h>  // For optional argument declarations
 #include <fst/fst.h>
@@ -96,7 +98,7 @@ class NullComposeFilter {
         fst1_(matcher1_->GetFst()),
         fst2_(matcher2_->GetFst()) {}
 
-  NullComposeFilter(const NullComposeFilter<M1, M2> &filter, bool safe = false)
+  NullComposeFilter(const NullComposeFilter &filter, bool safe = false)
       : matcher1_(filter.matcher1_->Copy(safe)),
         matcher2_(filter.matcher2_->Copy(safe)),
         fst1_(matcher1_->GetFst()),
@@ -529,7 +531,7 @@ class MultiEpsFilter {
       : filter_(fst1, fst2, matcher1, matcher2),
         keep_multi_eps_(keep_multi_eps) {}
 
-  MultiEpsFilter(const MultiEpsFilter<Filter> &filter, bool safe = false)
+  MultiEpsFilter(const MultiEpsFilter &filter, bool safe = false)
       : filter_(filter.filter_, safe),
         keep_multi_eps_(filter.keep_multi_eps_) {}
 
index a2a50c8..36dc586 100644 (file)
@@ -10,6 +10,7 @@
 #include <memory>
 #include <utility>
 
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/cache.h>
@@ -589,13 +590,13 @@ class ComposeFst
       : ImplToFst<Impl>(CreateBase2(fst1, fst2, opts)) {}
 
   // See Fst<>::Copy() for doc.
-  ComposeFst(const ComposeFst<A, CacheStore> &fst, bool safe = false)
+  ComposeFst(const ComposeFst &fst, bool safe = false)
       : ImplToFst<Impl>(safe ? std::shared_ptr<Impl>(fst.GetImpl()->Copy())
                              : fst.GetSharedImpl()) {}
 
   // Get a copy of this ComposeFst. See Fst<>::Copy() for further doc.
-  ComposeFst<A, CacheStore> *Copy(bool safe = false) const override {
-    return new ComposeFst<A, CacheStore>(*this, safe);
+  ComposeFst *Copy(bool safe = false) const override {
+    return new ComposeFst(*this, safe);
   }
 
   inline void InitStateIterator(StateIteratorData<Arc> *data) const override;
@@ -770,9 +771,8 @@ class ComposeFstMatcher : public MatcherBase<typename CacheStore::Arc> {
     if (match_type_ == MATCH_OUTPUT) std::swap(loop_.ilabel, loop_.olabel);
   }
 
-  ComposeFstMatcher<CacheStore, Filter, StateTable> *Copy(
-      bool safe = false) const override {
-    return new ComposeFstMatcher<CacheStore, Filter, StateTable>(*this, safe);
+  ComposeFstMatcher *Copy(bool safe = false) const override {
+    return new ComposeFstMatcher(*this, safe);
   }
 
   MatchType Type(bool test) const override {
index 8ff9227..71923d3 100644 (file)
@@ -9,6 +9,8 @@
 #include <algorithm>
 #include <vector>
 
+#include <fst/types.h>
+
 #include <fst/expanded-fst.h>
 #include <fst/mutable-fst.h>
 #include <fst/rational.h>
@@ -190,12 +192,12 @@ class ConcatFst : public RationalFst<A> {
   }
 
   // See Fst<>::Copy() for doc.
-  ConcatFst(const ConcatFst<Arc> &fst, bool safe = false)
+  ConcatFst(const ConcatFst &fst, bool safe = false)
       : RationalFst<Arc>(fst, safe) {}
 
   // Get a copy of this ConcatFst. See Fst<>::Copy() for further doc.
-  ConcatFst<Arc> *Copy(bool safe = false) const override {
-    return new ConcatFst<Arc>(*this, safe);
+  ConcatFst *Copy(bool safe = false) const override {
+    return new ConcatFst(*this, safe);
   }
 
  private:
index 4c98929..fd24990 100644 (file)
@@ -10,6 +10,8 @@
 #include <memory>
 #include <vector>
 
+#include <fst/types.h>
+
 #include <fst/dfs-visit.h>
 #include <fst/mutable-fst.h>
 #include <fst/union-find.h>
index 902f3d8..2ed05ed 100644 (file)
@@ -13,6 +13,7 @@
 
 // Google-only...
 // ...Google-only
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/expanded-fst.h>
@@ -65,7 +66,7 @@ class ConstFstImpl : public FstImpl<A> {
 
   StateId Start() const { return start_; }
 
-  Weight Final(StateId s) const { return states_[s].weight; }
+  Weight Final(StateId s) const { return states_[s].final_weight; }
 
   StateId NumStates() const { return nstates_; }
 
@@ -75,8 +76,7 @@ class ConstFstImpl : public FstImpl<A> {
 
   size_t NumOutputEpsilons(StateId s) const { return states_[s].noepsilons; }
 
-  static ConstFstImpl<Arc, Unsigned> *Read(std::istream &strm,
-                                           const FstReadOptions &opts);
+  static ConstFstImpl *Read(std::istream &strm, const FstReadOptions &opts);
 
   const Arc *Arcs(StateId s) const { return arcs_ + states_[s].pos; }
 
@@ -100,13 +100,13 @@ class ConstFstImpl : public FstImpl<A> {
 
   // States implemented by array *states_ below, arcs by (single) *arcs_.
   struct ConstState {
-    Weight weight;        // Final weight.
+    Weight final_weight;  // Final weight.
     Unsigned pos;         // Start of state's arcs in *arcs_.
     Unsigned narcs;       // Number of arcs (per state).
     Unsigned niepsilons;  // Number of input epsilons.
     Unsigned noepsilons;  // Number of output epsilons.
 
-    ConstState() : weight(Weight::Zero()) {}
+    ConstState() : final_weight(Weight::Zero()) {}
   };
 
   // Properties always true of this FST class.
@@ -159,13 +159,13 @@ ConstFstImpl<Arc, Unsigned>::ConstFstImpl(const Fst<Arc> &fst)
     ++nstates_;
     narcs_ += fst.NumArcs(siter.Value());
   }
-  states_region_.reset(MappedFile::Allocate(nstates_ * sizeof(*states_)));
-  arcs_region_.reset(MappedFile::Allocate(narcs_ * sizeof(*arcs_)));
-  states_ = reinterpret_cast<ConstState *>(states_region_->mutable_data());
-  arcs_ = reinterpret_cast<Arc *>(arcs_region_->mutable_data());
+  states_region_.reset(MappedFile::AllocateType<ConstState>(nstates_));
+  arcs_region_.reset(MappedFile::AllocateType<Arc>(narcs_));
+  states_ = static_cast<ConstState *>(states_region_->mutable_data());
+  arcs_ = static_cast<Arc *>(arcs_region_->mutable_data());
   size_t pos = 0;
   for (StateId s = 0; s < nstates_; ++s) {
-    states_[s].weight = fst.Final(s);
+    states_[s].final_weight = fst.Final(s);
     states_[s].pos = pos;
     states_[s].narcs = 0;
     states_[s].niepsilons = 0;
@@ -191,9 +191,7 @@ ConstFstImpl<Arc, Unsigned>::ConstFstImpl(const Fst<Arc> &fst)
 template <class Arc, class Unsigned>
 ConstFstImpl<Arc, Unsigned> *ConstFstImpl<Arc, Unsigned>::Read(
     std::istream &strm, const FstReadOptions &opts) {
-  using ConstState = typename ConstFstImpl<Arc, Unsigned>::ConstState;
-  std::unique_ptr<ConstFstImpl<Arc, Unsigned>> impl(
-      new ConstFstImpl<Arc, Unsigned>());
+  std::unique_ptr<ConstFstImpl> impl(new ConstFstImpl());
   FstHeader hdr;
   if (!impl->ReadHeader(strm, opts, kMinFileVersion, &hdr)) return nullptr;
   impl->start_ = hdr.Start();
@@ -215,7 +213,7 @@ ConstFstImpl<Arc, Unsigned> *ConstFstImpl<Arc, Unsigned>::Read(
     return nullptr;
   }
   impl->states_ =
-      reinterpret_cast<ConstState *>(impl->states_region_->mutable_data());
+      static_cast<ConstState *>(impl->states_region_->mutable_data());
   if ((hdr.GetFlags() & FstHeader::IS_ALIGNED) && !AlignInput(strm)) {
     LOG(ERROR) << "ConstFst::Read: Alignment failed: " << opts.source;
     return nullptr;
@@ -227,7 +225,7 @@ ConstFstImpl<Arc, Unsigned> *ConstFstImpl<Arc, Unsigned>::Read(
     LOG(ERROR) << "ConstFst::Read: Read failed: " << opts.source;
     return nullptr;
   }
-  impl->arcs_ = reinterpret_cast<Arc *>(impl->arcs_region_->mutable_data());
+  impl->arcs_ = static_cast<Arc *>(impl->arcs_region_->mutable_data());
   return impl.release();
 }
 
@@ -257,28 +255,25 @@ class ConstFst : public ImplToExpandedFst<internal::ConstFstImpl<A, Unsigned>> {
   explicit ConstFst(const Fst<Arc> &fst)
       : ImplToExpandedFst<Impl>(std::make_shared<Impl>(fst)) {}
 
-  ConstFst(const ConstFst<A, Unsigned> &fst, bool safe = false)
+  ConstFst(const ConstFst &fst, bool safe = false)
       : ImplToExpandedFst<Impl>(fst) {}
 
   // Gets a copy of this ConstFst. See Fst<>::Copy() for further doc.
-  ConstFst<A, Unsigned> *Copy(bool safe = false) const override {
-    return new ConstFst<A, Unsigned>(*this, safe);
+  ConstFst *Copy(bool safe = false) const override {
+    return new ConstFst(*this, safe);
   }
 
   // Reads a ConstFst from an input stream, returning nullptr on error.
-  static ConstFst<A, Unsigned> *Read(std::istream &strm,
-                                     const FstReadOptions &opts) {
+  static ConstFst *Read(std::istream &strm, const FstReadOptions &opts) {
     auto *impl = Impl::Read(strm, opts);
-    return impl ? new ConstFst<A, Unsigned>(std::shared_ptr<Impl>(impl))
-                : nullptr;
+    return impl ? new ConstFst(std::shared_ptr<Impl>(impl)) : nullptr;
   }
 
   // Read a ConstFst from a file; return nullptr on error; empty source reads
   // from standard input.
-  static ConstFst<A, Unsigned> *Read(const std::string &source) {
+  static ConstFst *Read(const std::string &source) {
     auto *impl = ImplToExpandedFst<Impl>::Read(source);
-    return impl ? new ConstFst<A, Unsigned>(std::shared_ptr<Impl>(impl))
-                : nullptr;
+    return impl ? new ConstFst(std::shared_ptr<Impl>(impl)) : nullptr;
   }
 
   bool Write(std::ostream &strm, const FstWriteOptions &opts) const override {
@@ -367,10 +362,10 @@ bool ConstFst<Arc, Unsigned>::WriteFst(const FST &fst, std::ostream &strm,
   }
   size_t pos = 0;
   size_t states = 0;
-  typename ConstFst<Arc, Unsigned>::ConstState state;
+  ConstState state;
   for (StateIterator<FST> siter(fst); !siter.Done(); siter.Next()) {
     const auto s = siter.Value();
-    state.weight = fst.Final(s);
+    state.final_weight = fst.Final(s);
     state.pos = pos;
     state.narcs = fst.NumArcs(s);
     state.niepsilons = fst.NumInputEpsilons(s);
index 1473908..d27a4d4 100644 (file)
@@ -14,6 +14,7 @@
 #include <utility>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/arc-map.h>
@@ -461,7 +462,7 @@ class DeterminizeFstImplBase : public CacheImpl<Arc> {
     SetOutputSymbols(fst.OutputSymbols());
   }
 
-  DeterminizeFstImplBase(const DeterminizeFstImplBase<Arc> &impl)
+  DeterminizeFstImplBase(const DeterminizeFstImplBase &impl)
       : CacheImpl<Arc>(impl), fst_(impl.fst_->Copy(true)) {
     SetType("determinize");
     SetProperties(impl.Properties(), kCopyProperties);
@@ -469,7 +470,7 @@ class DeterminizeFstImplBase : public CacheImpl<Arc> {
     SetOutputSymbols(impl.OutputSymbols());
   }
 
-  virtual DeterminizeFstImplBase<Arc> *Copy() const = 0;
+  virtual DeterminizeFstImplBase *Copy() const = 0;
 
   StateId Start() {
     if (!HasStart()) {
@@ -556,8 +557,7 @@ class DeterminizeFsaImpl : public DeterminizeFstImplBase<Arc> {
     if (out_dist_) out_dist_->clear();
   }
 
-  DeterminizeFsaImpl(
-      const DeterminizeFsaImpl<Arc, CommonDivisor, Filter, StateTable> &impl)
+  DeterminizeFsaImpl(const DeterminizeFsaImpl &impl)
       : DeterminizeFstImplBase<Arc>(impl),
         delta_(impl.delta_),
         in_dist_(nullptr),
@@ -570,10 +570,8 @@ class DeterminizeFsaImpl : public DeterminizeFstImplBase<Arc> {
     }
   }
 
-  DeterminizeFsaImpl<Arc, CommonDivisor, Filter, StateTable> *Copy()
-      const override {
-    return new DeterminizeFsaImpl<Arc, CommonDivisor, Filter, StateTable>(
-        *this);
+  DeterminizeFsaImpl *Copy() const override {
+    return new DeterminizeFsaImpl(*this);
   }
 
   uint64 Properties() const override { return Properties(kFstProperties); }
@@ -769,8 +767,7 @@ class DeterminizeFstImpl : public DeterminizeFstImplBase<Arc> {
     Init(GetFst(), opts.filter);
   }
 
-  DeterminizeFstImpl(
-      const DeterminizeFstImpl<Arc, G, CommonDivisor, Filter, StateTable> &impl)
+  DeterminizeFstImpl(const DeterminizeFstImpl &impl)
       : DeterminizeFstImplBase<Arc>(impl),
         delta_(impl.delta_),
         subsequential_label_(impl.subsequential_label_),
@@ -778,10 +775,8 @@ class DeterminizeFstImpl : public DeterminizeFstImplBase<Arc> {
     Init(GetFst(), nullptr);
   }
 
-  DeterminizeFstImpl<Arc, G, CommonDivisor, Filter, StateTable> *Copy()
-      const override {
-    return new DeterminizeFstImpl<Arc, G, CommonDivisor, Filter, StateTable>(
-        *this);
+  DeterminizeFstImpl *Copy() const override {
+    return new DeterminizeFstImpl(*this);
   }
 
   uint64 Properties() const override { return Properties(kFstProperties); }
@@ -896,13 +891,13 @@ class DeterminizeFst : public ImplToFst<internal::DeterminizeFstImplBase<A>> {
   }
 
   // See Fst<>::Copy() for doc.
-  DeterminizeFst(const DeterminizeFst<Arc> &fst, bool safe = false)
+  DeterminizeFst(const DeterminizeFst &fst, bool safe = false)
       : ImplToFst<Impl>(safe ? std::shared_ptr<Impl>(fst.GetImpl()->Copy())
                              : fst.GetSharedImpl()) {}
 
   // Get a copy of this DeterminizeFst. See Fst<>::Copy() for further doc.
-  DeterminizeFst<Arc> *Copy(bool safe = false) const override {
-    return new DeterminizeFst<Arc>(*this, safe);
+  DeterminizeFst *Copy(bool safe = false) const override {
+    return new DeterminizeFst(*this, safe);
   }
 
   inline void InitStateIterator(StateIteratorData<Arc> *data) const override;
index f073b0e..b7fbe9d 100644 (file)
@@ -73,12 +73,12 @@ class DifferenceFst : public ComposeFst<A> {
   }
 
   // See Fst<>::Copy() for doc.
-  DifferenceFst(const DifferenceFst<Arc> &fst, bool safe = false)
+  DifferenceFst(const DifferenceFst &fst, bool safe = false)
       : ComposeFst<Arc>(fst, safe) {}
 
   // Get a copy of this DifferenceFst. See Fst<>::Copy() for further doc.
-  DifferenceFst<Arc> *Copy(bool safe = false) const override {
-    return new DifferenceFst<Arc>(*this, safe);
+  DifferenceFst *Copy(bool safe = false) const override {
+    return new DifferenceFst(*this, safe);
   }
 
  private:
index bd01cce..c8f5481 100644 (file)
@@ -12,6 +12,8 @@
 #include <utility>
 #include <vector>
 
+#include <fst/types.h>
+
 #include <fst/arcsort.h>
 #include <fst/compose.h>
 #include <fst/connect.h>
index 0eb90b0..e146f5a 100644 (file)
@@ -29,6 +29,7 @@
 #include <unordered_map>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/cache.h>
@@ -69,8 +70,7 @@ class EditFstData {
 
   ~EditFstData() {}
 
-  static EditFstData<Arc, WrappedFstT, MutableFstT> *Read(
-      std::istream &strm, const FstReadOptions &opts);
+  static EditFstData *Read(std::istream &strm, const FstReadOptions &opts);
 
   bool Write(std::ostream &strm, const FstWriteOptions &opts) const {
     // Serializes all private data members of this class.
@@ -299,7 +299,7 @@ template <typename A, typename WrappedFstT, typename MutableFstT>
 EditFstData<A, WrappedFstT, MutableFstT> *
 EditFstData<A, WrappedFstT, MutableFstT>::Read(std::istream &strm,
                                                const FstReadOptions &opts) {
-  auto *data = new EditFstData<A, WrappedFstT, MutableFstT>();
+  auto *data = new EditFstData;
   // Next read in MutabelFstT machine that stores edits
   FstReadOptions edits_opts(opts);
   // Contained header was written out, so read it in.
@@ -423,8 +423,7 @@ class EditFstImpl : public FstImpl<A> {
     return wrapped_->NumStates() + data_->NumNewStates();
   }
 
-  static EditFstImpl<Arc, WrappedFstT, MutableFstT> *Read(
-      std::istream &strm, const FstReadOptions &opts);
+  static EditFstImpl *Read(std::istream &strm, const FstReadOptions &opts);
 
   bool Write(std::ostream &strm, const FstWriteOptions &opts) const {
     FstHeader hdr;
@@ -640,44 +639,37 @@ class EditFst : public ImplToMutableFst<
       : ImplToMutableFst<Impl>(std::make_shared<Impl>(fst)) {}
 
   // See Fst<>::Copy() for doc.
-  EditFst(const EditFst<Arc, WrappedFstT, MutableFstT> &fst, bool safe = false)
+  EditFst(const EditFst &fst, bool safe = false)
       : ImplToMutableFst<Impl>(fst, safe) {}
 
   ~EditFst() override {}
 
   // Gets a copy of this EditFst. See Fst<>::Copy() for further doc.
-  EditFst<Arc, WrappedFstT, MutableFstT> *Copy(
-      bool safe = false) const override {
-    return new EditFst<Arc, WrappedFstT, MutableFstT>(*this, safe);
+  EditFst *Copy(bool safe = false) const override {
+    return new EditFst(*this, safe);
   }
 
-  EditFst<Arc, WrappedFstT, MutableFstT> &operator=(
-      const EditFst<Arc, WrappedFstT, MutableFstT> &fst) {
+  EditFst &operator=(const EditFst &fst) {
     SetImpl(fst.GetSharedImpl());
     return *this;
   }
 
-  EditFst<Arc, WrappedFstT, MutableFstT> &operator=(
-      const Fst<Arc> &fst) override {
+  EditFst &operator=(const Fst<Arc> &fst) override {
     SetImpl(std::make_shared<Impl>(fst));
     return *this;
   }
 
   // Reads an EditFst from an input stream, returning nullptr on error.
-  static EditFst<Arc, WrappedFstT, MutableFstT> *Read(
-      std::istream &strm, const FstReadOptions &opts) {
+  static EditFst *Read(std::istream &strm, const FstReadOptions &opts) {
     auto *impl = Impl::Read(strm, opts);
-    return impl ? new EditFst<Arc>(std::shared_ptr<Impl>(impl)) : nullptr;
+    return impl ? new EditFst(std::shared_ptr<Impl>(impl)) : nullptr;
   }
 
   // Reads an EditFst from a file, returning nullptr on error. If the source
   // argument is an empty string, it reads from standard input.
-  static EditFst<Arc, WrappedFstT, MutableFstT> *Read(
-      const std::string &source) {
+  static EditFst *Read(const std::string &source) {
     auto *impl = ImplToExpandedFst<Impl, MutableFst<Arc>>::Read(source);
-    return impl ? new EditFst<Arc, WrappedFstT, MutableFstT>(
-                      std::shared_ptr<Impl>(impl))
-                : nullptr;
+    return impl ? new EditFst(std::shared_ptr<Impl>(impl)) : nullptr;
   }
 
   bool Write(std::ostream &strm, const FstWriteOptions &opts) const override {
index 2b19edd..f404543 100644 (file)
@@ -13,6 +13,7 @@
 #include <utility>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/log.h>
 #include <fst/arc-map.h>
 #include <fstream>
@@ -159,13 +160,6 @@ class EncodeTable {
     }
   }
 
-  // Given an arc, looks up its encoded label or returns kNoLabel if not found.
-  Label GetLabel(const Arc &arc) const {
-    const Triple triple(arc, flags_);
-    auto it = triple2label_.find(&triple);
-    return (it == triple2label_.end()) ? kNoLabel : it->second;
-  }
-
   // Given an encoded arc label, decodes back to input/output labels and costs.
   const Triple *Decode(Label label) const {
     if (label < 1 || label > triples_.size()) {
@@ -177,7 +171,7 @@ class EncodeTable {
 
   size_t Size() const { return triples_.size(); }
 
-  static EncodeTable<Arc> *Read(std::istream &strm, const std::string &source);
+  static EncodeTable *Read(std::istream &strm, const std::string &source);
 
   bool Write(std::ostream &strm, const std::string &source) const;
 
@@ -237,11 +231,7 @@ EncodeTable<Arc> *EncodeTable<Arc>::Read(std::istream &strm,
   const auto size = hdr.Size();
   auto table = fst::make_unique<EncodeTable>(flags);
   for (int64 i = 0; i < size; ++i) {
-    auto triple = fst::make_unique<Triple>();
-    ReadType(strm, &triple->ilabel);
-    ReadType(strm, &triple->olabel);
-    ReadType(strm, &triple->weight);
-    table->triples_.emplace_back(std::move(triple));
+    table->triples_.emplace_back(std::move(Triple::Read(strm)));
     table->triple2label_[table->triples_.back().get()] = table->triples_.size();
   }
   if (flags & kEncodeHasISymbols) {
@@ -358,14 +348,14 @@ class EncodeMapper {
 
   EncodeType Type() const { return type_; }
 
-  static EncodeMapper<Arc> *Read(std::istream &strm, const std::string &source,
-                                 EncodeType type = ENCODE) {
+  static EncodeMapper *Read(std::istream &strm, const std::string &source,
+                            EncodeType type = ENCODE) {
     auto *table = internal::EncodeTable<Arc>::Read(strm, source);
     return table ? new EncodeMapper(table->Flags(), type, table) : nullptr;
   }
 
-  static EncodeMapper<Arc> *Read(const std::string &source,
-                                 EncodeType type = ENCODE) {
+  static EncodeMapper *Read(const std::string &source,
+                            EncodeType type = ENCODE) {
     std::ifstream strm(source, std::ios_base::in | std::ios_base::binary);
     if (!strm) {
       LOG(ERROR) << "EncodeMapper: Can't open file: " << source;
@@ -503,11 +493,11 @@ class EncodeFst : public ArcMapFst<Arc, Arc, EncodeMapper<Arc>> {
       : ArcMapFst<Arc, Arc, Mapper>(fst, encoder, ArcMapFstOptions()) {}
 
   // See Fst<>::Copy() for doc.
-  EncodeFst(const EncodeFst<Arc> &fst, bool copy = false)
+  EncodeFst(const EncodeFst &fst, bool copy = false)
       : ArcMapFst<Arc, Arc, Mapper>(fst, copy) {}
 
   // Makes a copy of this EncodeFst. See Fst<>::Copy() for further doc.
-  EncodeFst<Arc> *Copy(bool safe = false) const override {
+  EncodeFst *Copy(bool safe = false) const override {
     if (safe) {
       FSTERROR() << "EncodeFst::Copy(true): Not allowed";
       GetImpl()->SetProperties(kError, kError);
@@ -534,7 +524,6 @@ class DecodeFst : public ArcMapFst<Arc, Arc, EncodeMapper<Arc>> {
  public:
   using Mapper = EncodeMapper<Arc>;
   using Impl = internal::ArcMapFstImpl<Arc, Arc, Mapper>;
-  using ImplToFst<Impl>::GetImpl;
 
   DecodeFst(const Fst<Arc> &fst, const Mapper &encoder)
       : ArcMapFst<Arc, Arc, Mapper>(fst, Mapper(encoder, DECODE),
@@ -544,15 +533,16 @@ class DecodeFst : public ArcMapFst<Arc, Arc, EncodeMapper<Arc>> {
   }
 
   // See Fst<>::Copy() for doc.
-  DecodeFst(const DecodeFst<Arc> &fst, bool safe = false)
+  DecodeFst(const DecodeFst &fst, bool safe = false)
       : ArcMapFst<Arc, Arc, Mapper>(fst, safe) {}
 
   // Makes a copy of this DecodeFst. See Fst<>::Copy() for further doc.
-  DecodeFst<Arc> *Copy(bool safe = false) const override {
+  DecodeFst *Copy(bool safe = false) const override {
     return new DecodeFst(*this, safe);
   }
 
  private:
+  using ImplToFst<Impl>::GetImpl;
   using ImplToFst<Impl>::GetMutableImpl;
 };
 
index fc81ecd..afde241 100644 (file)
@@ -11,6 +11,7 @@
 #include <unordered_map>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/encode.h>
index 0e73b10..3562e92 100644 (file)
@@ -31,11 +31,10 @@ class ExpandedFst : public Fst<A> {
   virtual StateId NumStates() const = 0;  // State count
 
   // Get a copy of this ExpandedFst. See Fst<>::Copy() for further doc.
-  ExpandedFst<Arc> *Copy(bool safe = false) const override = 0;
+  ExpandedFst *Copy(bool safe = false) const override = 0;
 
   // Read an ExpandedFst from an input stream; return NULL on error.
-  static ExpandedFst<Arc> *Read(std::istream &strm,
-                                const FstReadOptions &opts) {
+  static ExpandedFst *Read(std::istream &strm, const FstReadOptions &opts) {
     FstReadOptions ropts(opts);
     FstHeader hdr;
     if (ropts.header) {
@@ -57,12 +56,12 @@ class ExpandedFst : public Fst<A> {
     }
     auto *fst = reader(strm, ropts);
     if (!fst) return nullptr;
-    return static_cast<ExpandedFst<Arc> *>(fst);
+    return static_cast<ExpandedFst *>(fst);
   }
 
   // Read an ExpandedFst from a file; return NULL on error.
   // Empty source reads from standard input.
-  static ExpandedFst<Arc> *Read(const std::string &source) {
+  static ExpandedFst *Read(const std::string &source) {
     if (!source.empty()) {
       std::ifstream strm(source,
                               std::ios_base::in | std::ios_base::binary);
@@ -126,7 +125,7 @@ class ImplToExpandedFst : public ImplToFst<Impl, FST> {
   explicit ImplToExpandedFst(std::shared_ptr<Impl> impl)
       : ImplToFst<Impl, FST>(impl) {}
 
-  ImplToExpandedFst(const ImplToExpandedFst<Impl, FST> &fst, bool safe)
+  ImplToExpandedFst(const ImplToExpandedFst &fst, bool safe)
       : ImplToFst<Impl, FST>(fst, safe) {}
 
   static Impl *Read(std::istream &strm, const FstReadOptions &opts) {
index 36acae2..85dfbc9 100644 (file)
@@ -51,13 +51,13 @@ class SimpleVectorCacheState {
   using StateId = typename Arc::StateId;
 
   void Reset() {
-    final_ = Weight::Zero();
+    final_weight_ = Weight::Zero();
     niepsilons_ = 0;
     noepsilons_ = 0;
     arcs_.clear();
   }
 
-  Weight Final() const { return final_; }
+  Weight Final() const { return final_weight_; }
 
   size_t NumInputEpsilons() const { return niepsilons_; }
 
@@ -69,7 +69,7 @@ class SimpleVectorCacheState {
 
   const Arc *Arcs() const { return arcs_.empty() ? nullptr : &arcs_[0]; }
 
-  void SetFinal(Weight final) { final_ = final; }
+  void SetFinal(Weight weight) { final_weight_ = weight; }
 
   void ReserveArcs(size_t n) { arcs_.reserve(n); }
 
@@ -88,7 +88,7 @@ class SimpleVectorCacheState {
   int *MutableRefCount() const { return nullptr; }
 
  private:
-  Weight final_ = Weight::Zero();
+  Weight final_weight_ = Weight::Zero();
   size_t niepsilons_ = 0;  // Number of input epsilons.
   size_t noepsilons_ = 0;  // Number of output epsilons.
   std::vector<Arc> arcs_;
index ae4266f..768baed 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <string>
 
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/pair-weight.h>
@@ -76,7 +77,7 @@ class ExpectationWeight : public PairWeight<X1, X2> {
   }
 
   PairWeight<X1, X2> Quantize(float delta = kDelta) const {
-    return ExpectationWeight(PairWeight<X1, X2>::Quantize());
+    return ExpectationWeight(PairWeight<X1, X2>::Quantize(delta));
   }
 
   ReverseWeight Reverse() const {
index ce6048b..9abfb2b 100644 (file)
@@ -16,6 +16,7 @@
 #include <vector>
 
 #include <fst/compat.h>
+#include <fst/types.h>
 #include <fst/log.h>
 #include <fst/extensions/compress/elias.h>
 #include <fst/extensions/compress/gzfile.h>
index 396a1ed..da91e79 100644 (file)
@@ -15,8 +15,8 @@
 #include <fst/extensions/far/create.h>
 #include <fst/extensions/far/equal.h>
 #include <fst/extensions/far/extract.h>
-#include <fst/extensions/far/far.h>
 #include <fst/extensions/far/far-class.h>
+#include <fst/extensions/far/far.h>
 #include <fst/extensions/far/info.h>
 #include <fst/extensions/far/isomorphic.h>
 #include <fst/extensions/far/print-strings.h>
index b8ef763..1d4613f 100644 (file)
@@ -13,6 +13,7 @@
 #include <vector>
 
 #include <fst/compat.h>
+#include <fst/types.h>
 #include <fst/log.h>
 #include <fst/extensions/linear/linear-fst-data.h>
 #include <fst/fst.h>
index 94af369..0803ee0 100644 (file)
@@ -389,7 +389,7 @@ bool GetModelRecord(const std::string &model, std::istream &strm,  // NOLINT
   SplitAndPush<Arc>(fields[0], delim, fsyms, input_labels);
   SplitAndPush<Arc>(fields[1], delim, osyms, output_labels);
 
-  *weight = StrToWeight<typename Arc::Weight>(fields[2], model, *num_line);
+  *weight = StrToWeight<typename Arc::Weight>(fields[2]);
 
   GuessStartOrEnd<Arc>(input_labels, kNoLabel);
   GuessStartOrEnd<Arc>(output_labels, kNoLabel);
index 026c8f9..b7b0cb3 100644 (file)
@@ -8,6 +8,7 @@
 
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/extensions/mpdt/mpdt.h>
 #include <fst/extensions/pdt/paren.h>
 #include <fst/cache.h>
index ac45ab6..49172fc 100644 (file)
@@ -9,6 +9,7 @@
 #include <unordered_map>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/extensions/mpdt/mpdt.h>
 #include <fst/fst.h>
 
index fb4d757..a01cec1 100644 (file)
@@ -11,6 +11,7 @@
 #include <utility>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/log.h>
 #include <fst/compose.h>  // for ComposeOptions
 #include <fst/util.h>
index a6a8446..900d7d3 100644 (file)
@@ -8,6 +8,7 @@
 #include <vector>
 
 #include <fst/compat.h>
+#include <fst/types.h>
 
 // This class is a bitstring storage class with an index that allows
 // seeking to the Nth set or clear bit in time O(Log(N)) where N is
index 15e7f11..710605e 100644 (file)
@@ -17,6 +17,7 @@
 #include <vector>
 
 #include <fst/compat.h>
+#include <fst/types.h>
 #include <fst/log.h>
 #include <fst/extensions/ngram/bitmap-index.h>
 #include <fstream>
@@ -109,7 +110,7 @@ class NGramFstImpl : public FstImpl<A> {
     strm.read(reinterpret_cast<char *>(&num_final), sizeof(num_final));
     size_t size = Storage(num_states, num_futures, num_final);
     MappedFile *data_region = MappedFile::Allocate(size);
-    char *data = reinterpret_cast<char *>(data_region->mutable_data());
+    char *data = static_cast<char *>(data_region->mutable_data());
     // Copy num_states, num_futures and num_final back into data.
     memcpy(data, reinterpret_cast<char *>(&num_states), sizeof(num_states));
     memcpy(data + sizeof(num_states), reinterpret_cast<char *>(&num_futures),
@@ -596,7 +597,7 @@ NGramFstImpl<A>::NGramFstImpl(const Fst<A> &fst,
   Label label = kNoLabel;
   const size_t storage = Storage(num_states, num_futures, num_final);
   MappedFile *data_region = MappedFile::Allocate(storage);
-  char *data = reinterpret_cast<char *>(data_region->mutable_data());
+  char *data = static_cast<char *>(data_region->mutable_data());
   memset(data, 0, storage);
   size_t offset = 0;
   memcpy(data + offset, reinterpret_cast<char *>(&num_states),
index c6e6e48..7a1697a 100644 (file)
@@ -4,16 +4,12 @@
 #ifndef FST_EXTENSIONS_NGRAM_NTHBIT_H_
 #define FST_EXTENSIONS_NGRAM_NTHBIT_H_
 
-#include <fst/types.h>
-
-#if defined( __arm__)  // 32-bit ARM
+#include <cstdint>
 
-// Returns the position (0-63) of the r-th 1 bit in v.
-// 1 <= r <= CountOnes(v) <= 64.  Therefore, v must not be 0.
-uint32 nth_bit(uint64 v, uint32 r);
+#include <fst/types.h>
 
-#elif defined(__BMI2__)  // Intel Bit Manipulation Instruction Set 2
-// PDEP requires BMI2.
+#if defined(__BMI2__)  // Intel Bit Manipulation Instruction Set 2
+// PDEP requires BMI2; this is present starting with Haswell.
 
 #include <immintrin.h>
 
@@ -24,33 +20,66 @@ inline uint32 nth_bit(uint64 v, uint32 r) {
   return __builtin_ctzll(_pdep_u64(uint64{1} << (r - 1), v));
 }
 
-#else  // !defined(__BMI2__) && !defined(__arm__)
+#elif SIZE_MAX == UINT32_MAX
+// Detect 32-bit architectures via size_t.
+
+// Returns the position (0-63) of the r-th 1 bit in v.
+// 1 <= r <= CountOnes(v) <= 64.  Therefore, v must not be 0.
+uint32 nth_bit(uint64 v, uint32 r);
+
+#elif SIZE_MAX == UINT64_MAX
+// Default 64-bit version, used by ARM64 and Intel < Haswell.
 
-extern const uint32 nth_bit_bit_offset[];
+namespace internal {
+extern const uint64 kPrefixSumOverflow[65];
+extern const uint8 kSelectInByte[2048];
+}  // namespace internal
 
 // Returns the position (0-63) of the r-th 1 bit in v.
 // 1 <= r <= CountOnes(v) <= 64.  Therefore, v must not be 0.
-inline uint32 nth_bit(uint64 v, uint32 r) {
-  uint32 shift = 0;
-  uint32 c = __builtin_popcount(v & 0xffffffff);
-  uint32 mask = -(r > c);
-  r -= c & mask;
-  shift += (32 & mask);
-
-  c = __builtin_popcount((v >> shift) & 0xffff);
-  mask = -(r > c);
-  r -= c & mask;
-  shift += (16 & mask);
-
-  c = __builtin_popcount((v >> shift) & 0xff);
-  mask = -(r > c);
-  r -= c & mask;
-  shift += (8 & mask);
+//
+// This version is based on the paper "Broadword Implementation of
+// Rank/Select Queries" by Sebastiano Vigna, p. 5, Algorithm 2, with
+// improvements from "Optimized Succinct Data Structures for Massive Data"
+// by Gog & Petri, 2014.
+inline uint32 nth_bit(const uint64 v, const uint32 r) {
+  constexpr uint64 kOnesStep4 = 0x1111111111111111;
+  constexpr uint64 kOnesStep8 = 0x0101010101010101;
+  constexpr uint64 kMSBsStep8 = 0x80 * kOnesStep8;
 
+  uint64 s = v;
+  s = s - ((s >> 1) & (0x5 * kOnesStep4));
+  s = (s & (0x3 * kOnesStep4)) + ((s >> 2) & (0x3 * kOnesStep4));
+  s = (s + (s >> 4)) & (0xF * kOnesStep8);
+  // s now contains the byte-wise popcounts of v.
+
+  // byte_sums contains partial sums of the byte-wise popcounts.
+  // That is, byte i contains the popcounts of bytes <= i.
+  uint64 byte_sums = s * kOnesStep8;
+
+  // kPrefixSumOverflow[r] == (0x80 - r) * kOnesStep8, so the high bit is
+  // still set if byte_sums - r >= 0, or byte_sums >= r.  The first one set
+  // is in the byte with the sum larger than or equal to r (since r is 1-based),
+  // so this is the byte we need.
+  const uint64 b = (byte_sums + internal::kPrefixSumOverflow[r]) & kMSBsStep8;
+  // The first bit set is the high bit in the byte, so
+  // num_trailing_zeros == 8 * byte_nr + 7 and the byte number is the
+  // number of trailing zeros divided by 8.
+  const int byte_nr = __builtin_ctzll(b) >> 3;
+  const int shift = byte_nr << 3;
+  // The top byte contains the whole-word popcount; we never need that.
+  byte_sums <<= 8;
+  // Paper uses reinterpret_cast<uint8 *>; use shift/mask instead.
+  // Adjust for fact that r is 1-based.
+  const int rank_in_byte = r - 1 - (byte_sums >> shift) & 0xFF;
   return shift +
-         ((nth_bit_bit_offset[(v >> shift) & 0xff] >> ((r - 1) << 2)) & 0xf);
+         internal::kSelectInByte[(rank_in_byte << 8) + ((v >> shift) & 0xFF)];
 }
 
-#endif  // !defined(__BMI2__)
+#else
+
+#error Unrecognized architecture size
+
+#endif
 
 #endif  // FST_EXTENSIONS_NGRAM_NTHBIT_H_
index e21f2d8..9e179ee 100644 (file)
@@ -9,6 +9,7 @@
 #include <forward_list>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/log.h>
 #include <fst/extensions/pdt/paren.h>
 #include <fst/extensions/pdt/pdt.h>
index dee6e6f..1c566f9 100644 (file)
@@ -10,6 +10,7 @@
 #include <unordered_set>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/extensions/pdt/pdt.h>
 #include <fst/fst.h>
 
index 6dd8aa7..86467a4 100644 (file)
@@ -11,6 +11,7 @@
 #include <utility>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/log.h>
 #include <fst/extensions/pdt/compose.h>
 #include <fst/extensions/pdt/expand.h>
index 2ed7a30..a73bd3c 100644 (file)
@@ -11,6 +11,7 @@
 #include <utility>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/log.h>
 #include <fst/extensions/pdt/paren.h>
 #include <fst/extensions/pdt/pdt.h>
index 655194a..eff6ac1 100644 (file)
@@ -12,6 +12,7 @@
 #include <utility>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/cache.h>
@@ -460,12 +461,12 @@ class FactorWeightFst
       : ImplToFst<Impl>(std::make_shared<Impl>(fst, opts)) {}
 
   // See Fst<>::Copy() for doc.
-  FactorWeightFst(const FactorWeightFst<Arc, FactorIterator> &fst, bool copy)
+  FactorWeightFst(const FactorWeightFst &fst, bool copy)
       : ImplToFst<Impl>(fst, copy) {}
 
   // Get a copy of this FactorWeightFst. See Fst<>::Copy() for further doc.
-  FactorWeightFst<Arc, FactorIterator> *Copy(bool copy = false) const override {
-    return new FactorWeightFst<Arc, FactorIterator>(*this, copy);
+  FactorWeightFst *Copy(bool copy = false) const override {
+    return new FactorWeightFst(*this, copy);
   }
 
   inline void InitStateIterator(StateIteratorData<Arc> *data) const override;
index aba5217..544690c 100644 (file)
@@ -9,6 +9,8 @@
 #include <forward_list>
 #include <utility>
 
+#include <fst/types.h>
+
 #include <fst/fst-decl.h>  // For optional argument declarations
 #include <fst/fst.h>
 #include <fst/matcher.h>
index 54cba33..2a12cbb 100644 (file)
@@ -16,6 +16,8 @@
 #include <string>
 #include <type_traits>
 
+#include <fst/types.h>
+
 #include <fst/util.h>
 #include <fst/weight.h>
 
@@ -46,11 +48,11 @@ class FloatWeightTpl {
   constexpr FloatWeightTpl(T f) : value_(f) {}  // NOLINT
 
   // TODO(mjansche): Leave implicit once Android NDK r18 is the default.
-  FloatWeightTpl(const FloatWeightTpl<T> &) = default;
-  FloatWeightTpl(FloatWeightTpl<T> &&) noexcept = default;
+  FloatWeightTpl(const FloatWeightTpl &) = default;
+  FloatWeightTpl(FloatWeightTpl &&) noexcept = default;
 
-  FloatWeightTpl<T> &operator=(const FloatWeightTpl<T> &) = default;
-  FloatWeightTpl<T> &operator=(FloatWeightTpl<T> &&) noexcept = default;
+  FloatWeightTpl &operator=(const FloatWeightTpl &) = default;
+  FloatWeightTpl &operator=(FloatWeightTpl &&) noexcept = default;
 
   std::istream &Read(std::istream &strm) { return ReadType(strm, &value_); }
 
index 029be58..df55c90 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <fst/compat.h>
 #include <fst/flags.h>
+#include <fst/types.h>
 #include <fst/log.h>
 #include <fstream>
 
@@ -227,10 +228,10 @@ class Fst {
   // (3) If a MutableFst is copied and then mutated, then the original is
   // unmodified and vice versa (often by a copy-on-write on the initial
   // mutation, which may not be constant time).
-  virtual Fst<Arc> *Copy(bool safe = false) const = 0;
+  virtual Fst *Copy(bool safe = false) const = 0;
 
   // Reads an FST from an input stream; returns nullptr on error.
-  static Fst<Arc> *Read(std::istream &strm, const FstReadOptions &opts) {
+  static Fst *Read(std::istream &strm, const FstReadOptions &opts) {
     FstReadOptions ropts(opts);
     FstHeader hdr;
     if (ropts.header) {
@@ -251,7 +252,7 @@ class Fst {
 
   // Reads an FST from a file; returns nullptr on error. An empty source
   // results in reading from standard input.
-  static Fst<Arc> *Read(const std::string &source) {
+  static Fst *Read(const std::string &source) {
     if (!source.empty()) {
       std::ifstream strm(source,
                               std::ios_base::in | std::ios_base::binary);
@@ -653,7 +654,7 @@ class FstImpl {
 
   virtual ~FstImpl() {}
 
-  FstImpl &operator=(const FstImpl<Arc> &impl) {
+  FstImpl &operator=(const FstImpl &impl) {
     properties_ = impl.properties_;
     type_ = impl.type_;
     isymbols_ = impl.isymbols_ ? impl.isymbols_->Copy() : nullptr;
@@ -661,7 +662,7 @@ class FstImpl {
     return *this;
   }
 
-  FstImpl &operator=(FstImpl<Arc> &&impl) noexcept;
+  FstImpl &operator=(FstImpl &&impl) noexcept;
 
   const std::string &Type() const { return type_; }
 
@@ -918,7 +919,7 @@ class ImplToFst : public FST {
 
   // This constructor presumes there is a copy constructor for the
   // implementation that produces a thread-safe copy.
-  ImplToFst(const ImplToFst<Impl, FST> &fst, bool safe) {
+  ImplToFst(const ImplToFst &fst, bool safe) {
     if (safe) {
       impl_ = std::make_shared<Impl>(*(fst.impl_));
     } else {
@@ -928,19 +929,18 @@ class ImplToFst : public FST {
 
   ImplToFst() = delete;
 
-  ImplToFst(const ImplToFst<Impl, FST> &fst) : impl_(fst.impl_) {}
+  ImplToFst(const ImplToFst &fst) : impl_(fst.impl_) {}
 
-  ImplToFst(ImplToFst<Impl, FST> &&fst) noexcept
-      : impl_(std::move(fst.impl_)) {
+  ImplToFst(ImplToFst &&fst) noexcept : impl_(std::move(fst.impl_)) {
     fst.impl_ = std::make_shared<Impl>();
   }
 
-  ImplToFst<Impl, FST> &operator=(const ImplToFst<Impl, FST> &fst) {
+  ImplToFst &operator=(const ImplToFst &fst) {
     impl_ = fst.impl_;
     return *this;
   }
 
-  ImplToFst<Impl, FST> &operator=(ImplToFst<Impl, FST> &&fst) noexcept {
+  ImplToFst &operator=(ImplToFst &&fst) noexcept {
     if (this != &fst) {
       impl_ = std::move(fst.impl_);
       fst.impl_ = std::make_shared<Impl>();
index 1c27d40..9e98098 100644 (file)
@@ -11,7 +11,6 @@
 #include <map>
 #include <string>
 
-#include <fst/types.h>
 #include <fst/log.h>
 
 // Generic class representing a globally-stored correspondence between
index a682922..ddddda9 100644 (file)
@@ -13,6 +13,7 @@
 #include <sstream>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/log.h>
 
 namespace fst {
index fa66206..48a1c59 100644 (file)
@@ -79,12 +79,12 @@ class IntersectFst : public ComposeFst<A> {
   }
 
   // See Fst<>::Copy() for doc.
-  IntersectFst(const IntersectFst<Arc> &fst, bool safe = false)
+  IntersectFst(const IntersectFst &fst, bool safe = false)
       : ComposeFst<Arc>(fst, safe) {}
 
   // Get a copy of this IntersectFst. See Fst<>::Copy() for further doc.
-  IntersectFst<Arc> *Copy(bool safe = false) const override {
-    return new IntersectFst<Arc>(*this, safe);
+  IntersectFst *Copy(bool safe = false) const override {
+    return new IntersectFst(*this, safe);
   }
 
  private:
index bd243c6..b81a1e6 100644 (file)
@@ -6,6 +6,8 @@
 #ifndef FST_INVERT_H_
 #define FST_INVERT_H_
 
+#include <fst/types.h>
+
 #include <fst/arc-map.h>
 #include <fst/mutable-fst.h>
 
@@ -99,11 +101,11 @@ class InvertFst : public ArcMapFst<A, A, InvertMapper<A>> {
   }
 
   // See Fst<>::Copy() for doc.
-  InvertFst(const InvertFst<Arc> &fst, bool safe = false)
+  InvertFst(const InvertFst &fst, bool safe = false)
       : ArcMapFst<Arc, Arc, Mapper>(fst, safe) {}
 
   // Get a copy of this InvertFst. See Fst<>::Copy() for further doc.
-  InvertFst<Arc> *Copy(bool safe = false) const override {
+  InvertFst *Copy(bool safe = false) const override {
     return new InvertFst(*this, safe);
   }
 
index d409074..c917806 100644 (file)
@@ -11,6 +11,7 @@
 #include <utility>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/accumulator.h>
@@ -61,9 +62,9 @@ class LabelReachableData {
 
   Label FinalLabel() const { return final_label_; }
 
-  static LabelReachableData<Label> *Read(std::istream &istrm,
-                                         const FstReadOptions &opts) {
-    auto *data = new LabelReachableData<Label>();
+  static LabelReachableData *Read(std::istream &istrm,
+                                  const FstReadOptions &opts) {
+    auto *data = new LabelReachableData();
     ReadType(istrm, &data->reach_input_);
     ReadType(istrm, &data->keep_relabel_data_);
     data->have_relabel_data_ = data->keep_relabel_data_;
index 5f02664..4b229d4 100644 (file)
@@ -16,6 +16,7 @@
 #include <cstdlib>
 #include <string>
 
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/pair-weight.h>
index 657fe9a..cebeb75 100644 (file)
@@ -9,6 +9,7 @@
 
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/filter-state.h>
index 34c8429..52c5bfd 100644 (file)
@@ -12,6 +12,7 @@
 #include <vector>
 
 #include <fst/flags.h>
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/add-on.h>
@@ -48,7 +49,7 @@ namespace fst {
 //
 //   // If safe = true, the copy is thread-safe (except the lookahead FST is
 //   // preserved). See Fst<>::Copy() for further doc.
-//   LookaheadMatcher<FST> *Copy(bool safe = false) const override;
+//   LookaheadMatcher *Copy(bool safe = false) const override;
 
 //  // Below are methods for looking ahead for a match to a label and more
 //  // generally, to a rational set. Each returns false if there is definitely
@@ -180,12 +181,12 @@ class TrivialLookAheadMatcher
       : matcher_(fst, match_type) {}
 
   // This makes a copy of the FST.
-  TrivialLookAheadMatcher(const TrivialLookAheadMatcher<M> &lmatcher,
+  TrivialLookAheadMatcher(const TrivialLookAheadMatcher &lmatcher,
                           bool safe = false)
       : matcher_(lmatcher.matcher_, safe) {}
 
-  TrivialLookAheadMatcher<M> *Copy(bool safe = false) const override {
-    return new TrivialLookAheadMatcher<M>(*this, safe);
+  TrivialLookAheadMatcher *Copy(bool safe = false) const override {
+    return new TrivialLookAheadMatcher(*this, safe);
   }
 
   MatchType Type(bool test) const override { return matcher_.Type(test); }
@@ -256,7 +257,7 @@ class ArcLookAheadMatcher : public LookAheadMatcherBase<typename M::FST::Arc> {
   // This makes a copy of the FST.
   ArcLookAheadMatcher(
       const FST &fst, MatchType match_type,
-      std::shared_ptr<MatcherData> data = std::shared_ptr<MatcherData>())
+      std::shared_ptr<MatcherData> data = nullptr)
       : matcher_(fst, match_type),
         fst_(matcher_.GetFst()),
         lfst_(nullptr),
@@ -265,23 +266,22 @@ class ArcLookAheadMatcher : public LookAheadMatcherBase<typename M::FST::Arc> {
   // This doesn't copy the FST.
   ArcLookAheadMatcher(
       const FST *fst, MatchType match_type,
-      std::shared_ptr<MatcherData> data = std::shared_ptr<MatcherData>())
+      std::shared_ptr<MatcherData> data = nullptr)
       : matcher_(fst, match_type),
         fst_(matcher_.GetFst()),
         lfst_(nullptr),
         state_(kNoStateId) {}
 
   // This makes a copy of the FST.
-  ArcLookAheadMatcher(const ArcLookAheadMatcher<M, flags> &lmatcher,
-                      bool safe = false)
+  ArcLookAheadMatcher(const ArcLookAheadMatcher &lmatcher, bool safe = false)
       : matcher_(lmatcher.matcher_, safe),
         fst_(matcher_.GetFst()),
         lfst_(lmatcher.lfst_),
         state_(kNoStateId) {}
 
   // General matcher methods.
-  ArcLookAheadMatcher<M, flags> *Copy(bool safe = false) const override {
-    return new ArcLookAheadMatcher<M, flags>(*this, safe);
+  ArcLookAheadMatcher *Copy(bool safe = false) const override {
+    return new ArcLookAheadMatcher(*this, safe);
   }
 
   MatchType Type(bool test) const override { return matcher_.Type(test); }
@@ -316,9 +316,7 @@ class ArcLookAheadMatcher : public LookAheadMatcherBase<typename M::FST::Arc> {
 
   const MatcherData *GetData() const { return nullptr; }
 
-  std::shared_ptr<MatcherData> GetSharedData() const {
-    return std::shared_ptr<MatcherData>();
-  }
+  std::shared_ptr<MatcherData> GetSharedData() const { return nullptr; }
 
   // Look-ahead methods.
 
@@ -442,7 +440,7 @@ class LabelLookAheadMatcher
   // This makes a copy of the FST.
   LabelLookAheadMatcher(
       const FST &fst, MatchType match_type,
-      std::shared_ptr<MatcherData> data = std::shared_ptr<MatcherData>(),
+      std::shared_ptr<MatcherData> data = nullptr,
       Accumulator *accumulator = nullptr)
       : matcher_(fst, match_type),
         lfst_(nullptr),
@@ -454,7 +452,7 @@ class LabelLookAheadMatcher
   // This doesn't copy the FST.
   LabelLookAheadMatcher(
       const FST *fst, MatchType match_type,
-      std::shared_ptr<MatcherData> data = std::shared_ptr<MatcherData>(),
+      std::shared_ptr<MatcherData> data = nullptr,
       Accumulator *accumulator = nullptr)
       : matcher_(fst, match_type),
         lfst_(nullptr),
@@ -464,9 +462,8 @@ class LabelLookAheadMatcher
   }
 
   // This makes a copy of the FST.
-  LabelLookAheadMatcher(
-      const LabelLookAheadMatcher<M, flags, Accumulator, Reachable> &lmatcher,
-      bool safe = false)
+  LabelLookAheadMatcher(const LabelLookAheadMatcher &lmatcher,
+                        bool safe = false)
       : matcher_(lmatcher.matcher_, safe),
         lfst_(lmatcher.lfst_),
         label_reachable_(lmatcher.label_reachable_
@@ -475,10 +472,8 @@ class LabelLookAheadMatcher
         state_(kNoStateId),
         error_(lmatcher.error_) {}
 
-  LabelLookAheadMatcher<M, flags, Accumulator, Reachable> *Copy(
-      bool safe = false) const override {
-    return new LabelLookAheadMatcher<M, flags, Accumulator, Reachable>(*this,
-                                                                       safe);
+  LabelLookAheadMatcher *Copy(bool safe = false) const override {
+    return new LabelLookAheadMatcher(*this, safe);
   }
 
   MatchType Type(bool test) const override { return matcher_.Type(test); }
@@ -534,7 +529,7 @@ class LabelLookAheadMatcher
 
   std::shared_ptr<MatcherData> GetSharedData() const {
     return label_reachable_ ? label_reachable_->GetSharedData()
-                            : std::shared_ptr<MatcherData>();
+                            : nullptr;
   }
   // Checks if there is a matching (possibly super-final) transition at
   // (state_, s).
@@ -749,16 +744,15 @@ class LookAheadMatcher {
   }
 
   // This makes a copy of the FST.
-  LookAheadMatcher(const LookAheadMatcher<FST> &matcher, bool safe = false)
-      : base_(matcher.base_->Copy(safe)),
-        lookahead_(matcher.lookahead_) { }
+  LookAheadMatcher(const LookAheadMatcher &matcher, bool safe = false)
+      : base_(matcher.base_->Copy(safe)), lookahead_(matcher.lookahead_) {}
 
   // Takes ownership of base.
   explicit LookAheadMatcher(MatcherBase<Arc> *base)
       : base_(base), lookahead_(false) {}
 
-  LookAheadMatcher<FST> *Copy(bool safe = false) const {
-    return new LookAheadMatcher<FST>(*this, safe);
+  LookAheadMatcher *Copy(bool safe = false) const {
+    return new LookAheadMatcher(*this, safe);
   }
 
   MatchType Type(bool test) const { return base_->Type(test); }
index 5d663ae..a47a7f5 100644 (file)
@@ -58,11 +58,11 @@ class MapFst : public ArcMapFst<A, B, C> {
   MapFst(const Fst<A> &fst, C *mapper) : ArcMapFst<A, B, C>(fst, mapper) {}
 
   // See Fst<>::Copy() for doc.
-  MapFst(const MapFst<A, B, C> &fst, bool safe = false)
+  MapFst(const MapFst &fst, bool safe = false)
       : ArcMapFst<A, B, C>(fst, safe) {}
 
   // Get a copy of this MapFst. See Fst<>::Copy() for further doc.
-  MapFst<A, B, C> *Copy(bool safe = false) const override {
+  MapFst *Copy(bool safe = false) const override {
     return new MapFst(*this, safe);
   }
 };
index eadfdb1..c81e6e8 100644 (file)
@@ -49,12 +49,21 @@ class MappedFile {
   // factory function does not backoff to allocating and reading.
   static MappedFile *MapFromFileDescriptor(int fd, size_t pos, size_t size);
 
-  // Creates a MappedFile object with a new[]'ed block of memory of size. The
+  // Creates a MappedFile object with a new'ed block of memory of size. The
   // align argument can be used to specify a desired block alignment.
   // This is RECOMMENDED FOR INTERNAL USE ONLY as it may change in future
   // releases.
   static MappedFile *Allocate(size_t size, size_t align = kArchAlignment);
 
+  // Creates a MappedFile object with a new'ed block of memory with enough
+  // space for count elements of type T, correctly aligned for the type.
+  // This is RECOMMENDED FOR INTERNAL USE ONLY as it may change in future
+  // releases.
+  template <typename T>
+  static MappedFile *AllocateType(size_t count) {
+    return Allocate(sizeof(T) * count, alignof(T));
+  }
+
   // Creates a MappedFile object pointing to a borrowed reference to data. This
   // block of memory is not owned by the MappedFile object and will not be
   // freed. This is RECOMMENDED FOR INTERNAL USE ONLY, may change in future
index e7281ed..3f095e9 100644 (file)
@@ -9,6 +9,8 @@
 #include <memory>
 #include <string>
 
+#include <fst/types.h>
+
 #include <fst/add-on.h>
 #include <fst/const-fst.h>
 #include <fst/lookahead-matcher.h>
@@ -92,33 +94,25 @@ class MatcherFst : public ImplToExpandedFst<internal::AddOnImpl<F, Data>> {
       : ImplToExpandedFst<Impl>(CreateDataAndImpl(fst, Name)) {}
 
   // See Fst<>::Copy() for doc.
-  MatcherFst(const MatcherFst<FST, FstMatcher, Name, Init, Data> &fst,
-             bool safe = false)
+  MatcherFst(const MatcherFst &fst, bool safe = false)
       : ImplToExpandedFst<Impl>(fst, safe) {}
 
   // Get a copy of this MatcherFst. See Fst<>::Copy() for further doc.
-  MatcherFst<FST, FstMatcher, Name, Init, Data> *Copy(
-      bool safe = false) const override {
-    return new MatcherFst<FST, FstMatcher, Name, Init, Data>(*this, safe);
+  MatcherFst *Copy(bool safe = false) const override {
+    return new MatcherFst(*this, safe);
   }
 
   // Read a MatcherFst from an input stream; return nullptr on error
-  static MatcherFst<FST, M, Name, Init, Data> *Read(
-      std::istream &strm, const FstReadOptions &opts) {
+  static MatcherFst *Read(std::istream &strm, const FstReadOptions &opts) {
     auto *impl = Impl::Read(strm, opts);
-    return impl ? new MatcherFst<FST, FstMatcher, Name, Init, Data>(
-                      std::shared_ptr<Impl>(impl))
-                : nullptr;
+    return impl ? new MatcherFst(std::shared_ptr<Impl>(impl)) : nullptr;
   }
 
   // Read a MatcherFst from a file; return nullptr on error
   // Empty source reads from standard input
-  static MatcherFst<FST, FstMatcher, Name, Init, Data> *Read(
-      const std::string &source) {
+  static MatcherFst *Read(const std::string &source) {
     auto *impl = ImplToExpandedFst<Impl>::Read(source);
-    return impl ? new MatcherFst<FST, FstMatcher, Name, Init, Data>(
-                      std::shared_ptr<Impl>(impl))
-                : nullptr;
+    return impl ? new MatcherFst(std::shared_ptr<Impl>(impl)) : nullptr;
   }
 
   bool Write(std::ostream &strm, const FstWriteOptions &opts) const override {
@@ -226,9 +220,9 @@ class Matcher<MatcherFst<F, M, Name, Init>> {
   Matcher(const FST &fst, MatchType match_type)
       : matcher_(fst.InitMatcher(match_type)) {}
 
-  Matcher(const Matcher<FST> &matcher) : matcher_(matcher.matcher_->Copy()) {}
+  Matcher(const Matcher &matcher) : matcher_(matcher.matcher_->Copy()) {}
 
-  Matcher<FST> *Copy() const { return new Matcher<FST>(*this); }
+  Matcher *Copy() const { return new Matcher(*this); }
 
   MatchType Type(bool test) const { return matcher_->Type(test); }
 
@@ -263,12 +257,12 @@ class LookAheadMatcher<MatcherFst<F, M, Name, Init>> {
   LookAheadMatcher(const FST &fst, MatchType match_type)
       : matcher_(fst.InitMatcher(match_type)) {}
 
-  LookAheadMatcher(const LookAheadMatcher<FST> &matcher, bool safe = false)
+  LookAheadMatcher(const LookAheadMatcher &matcher, bool safe = false)
       : matcher_(matcher.matcher_->Copy(safe)) {}
 
   // General matcher methods.
-  LookAheadMatcher<FST> *Copy(bool safe = false) const {
-    return new LookAheadMatcher<FST>(*this, safe);
+  LookAheadMatcher *Copy(bool safe = false) const {
+    return new LookAheadMatcher(*this, safe);
   }
 
   MatchType Type(bool test) const { return matcher_->Type(test); }
index 465de19..2b80c68 100644 (file)
@@ -11,6 +11,7 @@
 #include <unordered_map>
 #include <utility>
 
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/mutable-fst.h>  // for all internal FST accessors.
@@ -631,7 +632,7 @@ class PhiMatcher : public MatcherBase<typename M::Arc> {
 
 
   // This makes a copy of the FST.
-  PhiMatcher(const PhiMatcher<M> &matcher, bool safe = false)
+  PhiMatcher(const PhiMatcher &matcher, bool safe = false)
       : matcher_(new M(*matcher.matcher_, safe)),
         match_type_(matcher.match_type_),
         phi_label_(matcher.phi_label_),
@@ -640,8 +641,8 @@ class PhiMatcher : public MatcherBase<typename M::Arc> {
         phi_loop_(matcher.phi_loop_),
         error_(matcher.error_) {}
 
-  PhiMatcher<M> *Copy(bool safe = false) const override {
-    return new PhiMatcher<M>(*this, safe);
+  PhiMatcher *Copy(bool safe = false) const override {
+    return new PhiMatcher(*this, safe);
   }
 
   MatchType Type(bool test) const override { return matcher_->Type(test); }
@@ -893,7 +894,7 @@ class RhoMatcher : public MatcherBase<typename M::Arc> {
                    matcher ? matcher : new M(fst, match_type)) { }
 
   // This makes a copy of the FST.
-  RhoMatcher(const RhoMatcher<M> &matcher, bool safe = false)
+  RhoMatcher(const RhoMatcher &matcher, bool safe = false)
       : matcher_(new M(*matcher.matcher_, safe)),
         match_type_(matcher.match_type_),
         rho_label_(matcher.rho_label_),
@@ -902,8 +903,8 @@ class RhoMatcher : public MatcherBase<typename M::Arc> {
         state_(kNoStateId),
         has_rho_(false) {}
 
-  RhoMatcher<M> *Copy(bool safe = false) const override {
-    return new RhoMatcher<M>(*this, safe);
+  RhoMatcher *Copy(bool safe = false) const override {
+    return new RhoMatcher(*this, safe);
   }
 
   MatchType Type(bool test) const override { return matcher_->Type(test); }
@@ -1085,7 +1086,7 @@ class SigmaMatcher : public MatcherBase<typename M::Arc> {
                      matcher ? matcher : new M(fst, match_type)) { }
 
   // This makes a copy of the FST.
-  SigmaMatcher(const SigmaMatcher<M> &matcher, bool safe = false)
+  SigmaMatcher(const SigmaMatcher &matcher, bool safe = false)
       : matcher_(new M(*matcher.matcher_, safe)),
         match_type_(matcher.match_type_),
         sigma_label_(matcher.sigma_label_),
@@ -1093,8 +1094,8 @@ class SigmaMatcher : public MatcherBase<typename M::Arc> {
         error_(matcher.error_),
         state_(kNoStateId) {}
 
-  SigmaMatcher<M> *Copy(bool safe = false) const override {
-    return new SigmaMatcher<M>(*this, safe);
+  SigmaMatcher *Copy(bool safe = false) const override {
+    return new SigmaMatcher(*this, safe);
   }
 
   MatchType Type(bool test) const override { return matcher_->Type(test); }
@@ -1264,7 +1265,7 @@ class MultiEpsMatcher {
   }
 
   // This makes a copy of the FST.
-  MultiEpsMatcher(const MultiEpsMatcher<M> &matcher, bool safe = false)
+  MultiEpsMatcher(const MultiEpsMatcher &matcher, bool safe = false)
       : matcher_(new M(*matcher.matcher_, safe)),
         flags_(matcher.flags_),
         own_matcher_(true),
@@ -1277,8 +1278,8 @@ class MultiEpsMatcher {
     if (own_matcher_) delete matcher_;
   }
 
-  MultiEpsMatcher<M> *Copy(bool safe = false) const {
-    return new MultiEpsMatcher<M>(*this, safe);
+  MultiEpsMatcher *Copy(bool safe = false) const {
+    return new MultiEpsMatcher(*this, safe);
   }
 
   MatchType Type(bool test) const { return matcher_->Type(test); }
@@ -1438,13 +1439,13 @@ class ExplicitMatcher : public MatcherBase<typename M::Arc> {
         error_(false) {}
 
   // This makes a copy of the FST.
-  ExplicitMatcher(const ExplicitMatcher<M> &matcher, bool safe = false)
+  ExplicitMatcher(const ExplicitMatcher &matcher, bool safe = false)
       : matcher_(new M(*matcher.matcher_, safe)),
         match_type_(matcher.match_type_),
         error_(matcher.error_) {}
 
-  ExplicitMatcher<M> *Copy(bool safe = false) const override {
-    return new ExplicitMatcher<M>(*this, safe);
+  ExplicitMatcher *Copy(bool safe = false) const override {
+    return new ExplicitMatcher(*this, safe);
   }
 
   MatchType Type(bool test) const override { return matcher_->Type(test); }
index c1f0bdd..552ca6c 100644 (file)
@@ -11,7 +11,6 @@
 #include <utility>
 #include <vector>
 
-#include <fst/types.h>
 #include <fst/log.h>
 #include <fstream>
 
@@ -295,7 +294,7 @@ class BlockAllocator {
 
   MemoryArenaCollection *arenas_;
 
-  BlockAllocator<T> operator=(const BlockAllocator<T> &);
+  BlockAllocator<T> operator=(const BlockAllocator &);
 };
 
 template <typename T, typename U>
@@ -423,7 +422,7 @@ class PoolAllocator {
 
   MemoryPoolCollection *pools_;
 
-  PoolAllocator<T> operator=(const PoolAllocator<T> &);
+  PoolAllocator<T> operator=(const PoolAllocator &);
 };
 
 template <typename T, typename U>
index 34f283c..c4d7d25 100644 (file)
@@ -13,6 +13,7 @@
 #include <utility>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/arcsort.h>
@@ -129,7 +130,7 @@ class CyclicMinimizer {
       size_t result = p2;
       size_t current_ilabel = kNoLabel;
       for (ArcIterator<Fst<Arc>> aiter(fst_, s); !aiter.Done(); aiter.Next()) {
-        Label this_ilabel = aiter.Value().ilabel;
+        const Label this_ilabel = aiter.Value().ilabel;
         if (this_ilabel != current_ilabel) {  // Ignores repeats.
           result = p1 * result + this_ilabel;
           current_ilabel = this_ilabel;
@@ -215,8 +216,8 @@ class CyclicMinimizer {
   void Initialize(const ExpandedFst<Arc> &fst) {
     // Constructs Tr.
     Reverse(fst, &Tr_);
-    ILabelCompare<RevArc> ilabel_comp;
-    ArcSort(&Tr_, ilabel_comp);
+    static const ILabelCompare<RevArc> icomp;
+    ArcSort(&Tr_, icomp);
     // Tells the partition how many elements to allocate. The first state in
     // Tr_ is super-final state.
     P_.Initialize(Tr_.NumStates() - 1);
@@ -231,7 +232,7 @@ class CyclicMinimizer {
     // Prepares priority queue: opens arc iterator for each state in C, and
     // inserts into priority queue.
     for (PartitionIterator<StateId> siter(P_, C); !siter.Done(); siter.Next()) {
-      StateId s = siter.Value();
+      const auto s = siter.Value();
       if (Tr_.NumArcs(s + 1)) {
         aiter_queue_->push(new ArcIterator<Fst<RevArc>>(Tr_, s + 1));
       }
@@ -445,21 +446,19 @@ void MergeStates(const Partition<typename Arc::StateId> &partition,
 }
 
 template <class Arc>
-void AcceptorMinimize(MutableFst<Arc> *fst,
-                      bool allow_acyclic_minimization = true) {
-  if (!(fst->Properties(kAcceptor | kUnweighted, true) ==
-        (kAcceptor | kUnweighted))) {
-    FSTERROR() << "FST is not an unweighted acceptor";
-    fst->SetProperties(kError, kError);
-    return;
-  }
+void AcceptorMinimize(MutableFst<Arc> *fst) {
   // Connects FST before minimization, handles disconnected states.
   Connect(fst);
-  if (fst->NumStates() == 0) return;
-  if (allow_acyclic_minimization && fst->Properties(kAcyclic, true)) {
+  if (fst->Start() == kNoStateId) return;
+  // The Revuz acyclic algorithm won't work for nondeterministic inputs, so if
+  // the input is nondeterministic, we force the use of the Hopcroft cyclic
+  // algorithm instead.
+  static constexpr auto revuz_props = kAcyclic | kIDeterministic;
+  if (fst->Properties(revuz_props, true) == revuz_props) {
     // Acyclic minimization (Revuz).
     VLOG(2) << "Acyclic minimization";
-    ArcSort(fst, ILabelCompare<Arc>());
+    static const ILabelCompare<Arc> comp;
+    ArcSort(fst, comp);
     AcyclicMinimizer<Arc> minimizer(*fst);
     MergeStates(minimizer.GetPartition(), fst);
   } else {
@@ -474,31 +473,27 @@ void AcceptorMinimize(MutableFst<Arc> *fst,
   ArcUniqueMapper<Arc> mapper(*fst);
   StateMap(fst, mapper);
 }
-
 }  // namespace internal
 
-// In place minimization of deterministic weighted automata and transducers,
-// and also non-deterministic ones if they use an idempotent semiring.
-// For transducers, if the 'sfst' argument is not null, the algorithm
-// produces a compact factorization of the minimal transducer.
+// In place minimization of deterministic weighted automata and transducers, and
+// also non-deterministic ones if they use an idempotent semiring. For
+// transducers, if the 'sfst' argument is not null, the algorithm produces a
+// compact factorization of the minimal transducer.
 //
-// In the acyclic deterministic case, we use an algorithm from Revuz that is
-// linear in the number of arcs (edges) in the machine.
+// In the acyclic deterministic case, we use an algorithm from Revuz; this has
+// complexity O(e).
 //
-// In the cyclic or non-deterministic case, we use the classical Hopcroft
+// In cyclic and non-deterministic cases, we use the classical Hopcroft
 // minimization (which was presented for the deterministic case but which
 // also works for non-deterministic FSTs); this has complexity O(e log v).
-//
 template <class Arc>
 void Minimize(MutableFst<Arc> *fst, MutableFst<Arc> *sfst = nullptr,
               float delta = kShortestDelta, bool allow_nondet = false) {
   using Weight = typename Arc::Weight;
-  const auto props = fst->Properties(
-      kAcceptor | kIDeterministic | kWeighted | kUnweighted, true);
-  bool allow_acyclic_minimization;
-  if (props & kIDeterministic) {
-    allow_acyclic_minimization = true;
-  } else {
+  static constexpr auto minimize_props =
+      kAcceptor | kIDeterministic | kWeighted | kUnweighted;
+  const auto props = fst->Properties(minimize_props, true);
+  if (!(props & kIDeterministic)) {
     // Our approach to minimization of non-deterministic FSTs will only work in
     // idempotent semirings---for non-deterministic inputs, a state could have
     // multiple transitions to states that will get merged, and we'd have to
@@ -514,26 +509,18 @@ void Minimize(MutableFst<Arc> *fst, MutableFst<Arc> *sfst = nullptr,
                  << "allow_nondet = false";
       return;
     }
-    // The Revuz algorithm won't work for nondeterministic inputs, so if the
-    // input is nondeterministic, we'll have to pass a bool saying not to use
-    // that algorithm. We check at this level rather than in AcceptorMinimize(),
-    // because it's possible that the FST at this level could be deterministic,
-    // but a harmless type of non-determinism could be introduced by Encode()
-    // (thanks to kEncodeWeights, if the FST has epsilons and has a final
-    // weight with weights equal to some epsilon arc.)
-    allow_acyclic_minimization = false;
   }
-  if (!(props & kAcceptor)) {  // Weighted transducer.
+  if ((props & kAcceptor) != kAcceptor) {  // Transducer.
     VectorFst<GallicArc<Arc, GALLIC_LEFT>> gfst;
     ArcMap(*fst, &gfst, ToGallicMapper<Arc, GALLIC_LEFT>());
     fst->DeleteStates();
     gfst.SetProperties(kAcceptor, kAcceptor);
     Push(&gfst, REWEIGHT_TO_INITIAL, delta);
     ArcMap(&gfst, QuantizeMapper<GallicArc<Arc, GALLIC_LEFT>>(delta));
-    EncodeMapper<GallicArc<Arc, GALLIC_LEFT>> encoder(
-        kEncodeLabels | kEncodeWeights, ENCODE);
+    EncodeMapper<GallicArc<Arc, GALLIC_LEFT>> encoder(kEncodeLabels |
+                                                      kEncodeWeights);
     Encode(&gfst, &encoder);
-    internal::AcceptorMinimize(&gfst, allow_acyclic_minimization);
+    internal::AcceptorMinimize(&gfst);
     Decode(&gfst, encoder);
     if (!sfst) {
       FactorWeightFst<GallicArc<Arc, GALLIC_LEFT>,
@@ -549,15 +536,17 @@ void Minimize(MutableFst<Arc> *fst, MutableFst<Arc> *sfst = nullptr,
       ArcMap(gfst, fst, &mapper);
       fst->SetOutputSymbols(sfst->InputSymbols());
     }
-  } else if (props & kWeighted) {  // Weighted acceptor.
+  } else if ((props & kWeighted) == kWeighted) {  // Weighted acceptor.
     Push(fst, REWEIGHT_TO_INITIAL, delta);
     ArcMap(fst, QuantizeMapper<Arc>(delta));
-    EncodeMapper<Arc> encoder(kEncodeLabels | kEncodeWeights, ENCODE);
+    // We encode labels even though this is already an acceptor because weight
+    // encoding gives us a transducer.
+    EncodeMapper<Arc> encoder(kEncodeLabels | kEncodeWeights);
     Encode(fst, &encoder);
-    internal::AcceptorMinimize(fst, allow_acyclic_minimization);
+    internal::AcceptorMinimize(fst);
     Decode(fst, encoder);
   } else {  // Unweighted acceptor.
-    internal::AcceptorMinimize(fst, allow_acyclic_minimization);
+    internal::AcceptorMinimize(fst);
   }
 }
 
index 5af7cff..e5266a0 100644 (file)
@@ -15,6 +15,7 @@
 #include <utility>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/log.h>
 #include <fstream>
 
@@ -37,7 +38,7 @@ class MutableFst : public ExpandedFst<A> {
 
   virtual MutableFst<Arc> &operator=(const Fst<Arc> &fst) = 0;
 
-  MutableFst<Arc> &operator=(const MutableFst<Arc> &fst) {
+  MutableFst &operator=(const MutableFst &fst) {
     return operator=(static_cast<const Fst<Arc> &>(fst));
   }
 
@@ -100,10 +101,10 @@ class MutableFst : public ExpandedFst<A> {
   virtual void SetOutputSymbols(const SymbolTable *osyms) = 0;
 
   // Gets a copy of this MutableFst. See Fst<>::Copy() for further doc.
-  MutableFst<A> *Copy(bool safe = false) const override = 0;
+  MutableFst *Copy(bool safe = false) const override = 0;
 
   // Reads a MutableFst from an input stream, returning nullptr on error.
-  static MutableFst<Arc> *Read(std::istream &strm, const FstReadOptions &opts) {
+  static MutableFst *Read(std::istream &strm, const FstReadOptions &opts) {
     FstReadOptions ropts(opts);
     FstHeader hdr;
     if (ropts.header) {
@@ -125,15 +126,15 @@ class MutableFst : public ExpandedFst<A> {
     }
     auto *fst = reader(strm, ropts);
     if (!fst) return nullptr;
-    return static_cast<MutableFst<Arc> *>(fst);
+    return static_cast<MutableFst *>(fst);
   }
 
   // Reads a MutableFst from a file; returns nullptr on error. An empty
   // source results in reading from standard input. If convert is true,
   // convert to a mutable FST subclass (given by convert_type) in the case
   // that the input FST is non-mutable.
-  static MutableFst<Arc> *Read(const std::string &source, bool convert = false,
-                               const std::string &convert_type = "vector") {
+  static MutableFst *Read(const std::string &source, bool convert = false,
+                          const std::string &convert_type = "vector") {
     if (convert == false) {
       if (!source.empty()) {
         std::ifstream strm(source,
@@ -150,7 +151,7 @@ class MutableFst : public ExpandedFst<A> {
       std::unique_ptr<Fst<Arc>> ifst(Fst<Arc>::Read(source));
       if (!ifst) return nullptr;
       if (ifst->Properties(kMutable, false)) {
-        return static_cast<MutableFst<Arc> *>(ifst.release());
+        return static_cast<MutableFst *>(ifst.release());
       } else {
         std::unique_ptr<Fst<Arc>> ofst(Convert(*ifst, convert_type));
         ifst.reset();
@@ -158,7 +159,7 @@ class MutableFst : public ExpandedFst<A> {
         if (!ofst->Properties(kMutable, false)) {
           LOG(ERROR) << "MutableFst: Bad convert type: " << convert_type;
         }
-        return static_cast<MutableFst<Arc> *>(ofst.release());
+        return static_cast<MutableFst *>(ofst.release());
       }
     }
   }
@@ -393,7 +394,7 @@ class ImplToMutableFst : public ImplToExpandedFst<Impl, FST> {
   explicit ImplToMutableFst(std::shared_ptr<Impl> impl)
       : ImplToExpandedFst<Impl, FST>(impl) {}
 
-  ImplToMutableFst(const ImplToMutableFst<Impl, FST> &fst, bool safe)
+  ImplToMutableFst(const ImplToMutableFst &fst, bool safe)
       : ImplToExpandedFst<Impl, FST>(fst, safe) {}
 
   void MutateCheck() {
index 86ecb22..0af03ad 100644 (file)
@@ -8,6 +8,8 @@
 
 #include <string>
 
+#include <fst/types.h>
+
 #include <fst/tuple-weight.h>
 #include <fst/weight.h>
 
index dc57d66..79fef33 100644 (file)
@@ -9,6 +9,8 @@
 #include <string>
 #include <utility>
 
+#include <fst/types.h>
+
 #include <fst/pair-weight.h>
 #include <fst/weight.h>
 
index 5a82cf1..6c519ab 100644 (file)
@@ -6,6 +6,8 @@
 #ifndef FST_PROJECT_H_
 #define FST_PROJECT_H_
 
+#include <fst/types.h>
+
 #include <fst/arc-map.h>
 #include <fst/mutable-fst.h>
 
@@ -119,11 +121,11 @@ class ProjectFst : public ArcMapFst<A, A, ProjectMapper<A>> {
   }
 
   // See Fst<>::Copy() for doc.
-  ProjectFst(const ProjectFst<A> &fst, bool safe = false)
+  ProjectFst(const ProjectFst &fst, bool safe = false)
       : ArcMapFst<A, A, ProjectMapper<A>>(fst, safe) {}
 
   // Gets a copy of this ProjectFst. See Fst<>::Copy() for further doc.
-  ProjectFst<A> *Copy(bool safe = false) const override {
+  ProjectFst *Copy(bool safe = false) const override {
     return new ProjectFst(*this, safe);
   }
 
index d974700..e76d61d 100644 (file)
@@ -11,6 +11,7 @@
 #include <vector>
 
 #include <fst/compat.h>
+#include <fst/types.h>
 
 namespace fst {
 
index f9d6253..a50332a 100644 (file)
@@ -9,6 +9,7 @@
 
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/arc-map.h>
index 449586a..ecadc1a 100644 (file)
@@ -13,6 +13,7 @@
 #include <utility>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/arcfilter.h>
index 87093df..c9106db 100644 (file)
@@ -17,6 +17,7 @@
 #include <utility>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/accumulator.h>
@@ -583,12 +584,12 @@ class RandGenFst
       : ImplToFst<Impl>(std::make_shared<Impl>(fst, opts)) {}
 
   // See Fst<>::Copy() for doc.
-  RandGenFst(const RandGenFst<FromArc, ToArc, Sampler> &fst, bool safe = false)
+  RandGenFst(const RandGenFst &fst, bool safe = false)
       : ImplToFst<Impl>(fst, safe) {}
 
   // Get a copy of this RandGenFst. See Fst<>::Copy() for further doc.
-  RandGenFst<FromArc, ToArc, Sampler> *Copy(bool safe = false) const override {
-    return new RandGenFst<FromArc, ToArc, Sampler>(*this, safe);
+  RandGenFst *Copy(bool safe = false) const override {
+    return new RandGenFst(*this, safe);
   }
 
   inline void InitStateIterator(StateIteratorData<ToArc> *data) const override;
index 29e9f40..f7d018c 100644 (file)
@@ -11,6 +11,8 @@
 #include <string>
 #include <vector>
 
+#include <fst/types.h>
+
 #include <fst/mutable-fst.h>
 #include <fst/replace.h>
 #include <fst/test-properties.h>
@@ -277,7 +279,7 @@ class RationalFst : public ImplToFst<internal::RationalFstImpl<A>> {
       : ImplToFst<Impl>(std::make_shared<Impl>(opts)) {}
 
   // See Fst<>::Copy() for doc.
-  RationalFst(const RationalFst<Arc> &fst, bool safe = false)
+  RationalFst(const RationalFst &fst, bool safe = false)
       : ImplToFst<Impl>(fst, safe) {}
 
  private:
index 4dcfb72..8dc41fd 100644 (file)
@@ -15,7 +15,6 @@
 #include <fst/util.h>
 
 
-#include <fst/types.h>
 #include <fst/log.h>
 
 namespace fst {
index b863289..844b550 100644 (file)
@@ -11,6 +11,7 @@
 #include <utility>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/cache.h>
@@ -390,12 +391,12 @@ class RelabelFst : public ImplToFst<internal::RelabelFstImpl<A>> {
                                                opts)) {}
 
   // See Fst<>::Copy() for doc.
-  RelabelFst(const RelabelFst<Arc> &fst, bool safe = false)
+  RelabelFst(const RelabelFst &fst, bool safe = false)
       : ImplToFst<Impl>(fst, safe) {}
 
   // Gets a copy of this RelabelFst. See Fst<>::Copy() for further doc.
-  RelabelFst<Arc> *Copy(bool safe = false) const override {
-    return new RelabelFst<Arc>(*this, safe);
+  RelabelFst *Copy(bool safe = false) const override {
+    return new RelabelFst(*this, safe);
   }
 
   void InitStateIterator(StateIteratorData<Arc> *data) const override;
index 0e26b08..8572fb4 100644 (file)
@@ -12,6 +12,7 @@
 #include <utility>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/connect.h>
index daf5322..3bf7910 100644 (file)
@@ -12,6 +12,7 @@
 #include <utility>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/cache.h>
@@ -975,14 +976,12 @@ class ReplaceFst
       : ImplToFst<Impl>(std::make_shared<Impl>(fst_array, opts)) {}
 
   // See Fst<>::Copy() for doc.
-  ReplaceFst(const ReplaceFst<Arc, StateTable, CacheStore> &fst,
-             bool safe = false)
+  ReplaceFst(const ReplaceFst &fst, bool safe = false)
       : ImplToFst<Impl>(fst, safe) {}
 
   // Get a copy of this ReplaceFst. See Fst<>::Copy() for further doc.
-  ReplaceFst<Arc, StateTable, CacheStore> *Copy(
-      bool safe = false) const override {
-    return new ReplaceFst<Arc, StateTable, CacheStore>(*this, safe);
+  ReplaceFst *Copy(bool safe = false) const override {
+    return new ReplaceFst(*this, safe);
   }
 
   inline void InitStateIterator(StateIteratorData<Arc> *data) const override;
@@ -1281,9 +1280,7 @@ class ReplaceFstMatcher : public MatcherBase<Arc> {
   }
 
   // This makes a copy of the FST.
-  ReplaceFstMatcher(
-      const ReplaceFstMatcher<Arc, StateTable, CacheStore> &matcher,
-      bool safe = false)
+  ReplaceFstMatcher(const ReplaceFstMatcher &matcher, bool safe = false)
       : owned_fst_(matcher.fst_.Copy(safe)),
         fst_(*owned_fst_),
         impl_(fst_.GetMutableImpl()),
@@ -1317,9 +1314,8 @@ class ReplaceFstMatcher : public MatcherBase<Arc> {
     }
   }
 
-  ReplaceFstMatcher<Arc, StateTable, CacheStore> *Copy(
-      bool safe = false) const override {
-    return new ReplaceFstMatcher<Arc, StateTable, CacheStore>(*this, safe);
+  ReplaceFstMatcher *Copy(bool safe = false) const override {
+    return new ReplaceFstMatcher(*this, safe);
   }
 
   MatchType Type(bool test) const override {
index 78614aa..649e368 100644 (file)
@@ -9,6 +9,8 @@
 #include <algorithm>
 #include <vector>
 
+#include <fst/types.h>
+
 #include <fst/cache.h>
 
 
index 70e512c..229f50b 100644 (file)
@@ -13,6 +13,7 @@
 #include <utility>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/arcfilter.h>
@@ -29,9 +30,8 @@
 namespace fst {
 
 template <class Arc, class Queue>
-class RmEpsilonOptions
+struct RmEpsilonOptions
     : public ShortestDistanceOptions<Arc, Queue, EpsilonArcFilter<Arc>> {
- public:
   using StateId = typename Arc::StateId;
   using Weight = typename Arc::Weight;
 
@@ -71,7 +71,7 @@ class RmEpsilonState {
 
   std::vector<Arc> &Arcs() { return arcs_; }
 
-  const Weight &Final() const { return final_; }
+  const Weight &Final() const { return final_weight_; }
 
   bool Error() const { return sd_state_.Error(); }
 
@@ -123,8 +123,8 @@ class RmEpsilonState {
   std::vector<bool> visited_;      // True if the state has been visited.
   std::forward_list<StateId> visited_states_;  // List of visited states.
   std::vector<Arc> arcs_;                      // Arcs of state being expanded.
-  Weight final_;       // Final weight of state being expanded.
-  StateId expand_id_;  // Unique ID for each call to Expand
+  Weight final_weight_;  // Final weight of state being expanded.
+  StateId expand_id_;    // Unique ID for each call to Expand
 
   RmEpsilonState(const RmEpsilonState &) = delete;
   RmEpsilonState &operator=(const RmEpsilonState &) = delete;
@@ -132,7 +132,7 @@ class RmEpsilonState {
 
 template <class Arc, class Queue>
 void RmEpsilonState<Arc, Queue>::Expand(typename Arc::StateId source) {
-  final_ = Weight::Zero();
+  final_weight_ = Weight::Zero();
   arcs_.clear();
   sd_state_.ShortestDistance(source);
   if (sd_state_.Error()) return;
@@ -169,7 +169,8 @@ void RmEpsilonState<Arc, Queue>::Expand(typename Arc::StateId source) {
         }
       }
     }
-    final_ = Plus(final_, Times((*distance_)[state], fst_.Final(state)));
+    final_weight_ =
+        Plus(final_weight_, Times((*distance_)[state], fst_.Final(state)));
   }
   while (!visited_states_.empty()) {
     visited_[visited_states_.front()] = false;
@@ -491,12 +492,12 @@ class RmEpsilonFst : public ImplToFst<internal::RmEpsilonFstImpl<A>> {
       : ImplToFst<Impl>(std::make_shared<Impl>(fst, opts)) {}
 
   // See Fst<>::Copy() for doc.
-  RmEpsilonFst(const RmEpsilonFst<Arc> &fst, bool safe = false)
+  RmEpsilonFst(const RmEpsilonFst &fst, bool safe = false)
       : ImplToFst<Impl>(fst, safe) {}
 
   // Get a copy of this RmEpsilonFst. See Fst<>::Copy() for further doc.
-  RmEpsilonFst<Arc> *Copy(bool safe = false) const override {
-    return new RmEpsilonFst<Arc>(*this, safe);
+  RmEpsilonFst *Copy(bool safe = false) const override {
+    return new RmEpsilonFst(*this, safe);
   }
 
   inline void InitStateIterator(StateIteratorData<Arc> *data) const override;
index 87e3a71..0953bcc 100644 (file)
@@ -9,6 +9,8 @@
 #include <unordered_set>
 #include <vector>
 
+#include <fst/types.h>
+
 #include <fst/connect.h>
 #include <fst/mutable-fst.h>
 
index 4336d7a..97dc608 100644 (file)
@@ -7,6 +7,7 @@
 #include <memory>
 #include <utility>
 
+#include <fst/types.h>
 #include <fst/fstlib.h>
 #include <fst/script/fst-class.h>
 
@@ -141,9 +142,7 @@ class MutableArcIteratorClassImpl
 
   void SetFlags(uint8 flags, uint8 mask) final { aiter_.SetFlags(flags, mask); }
 
-  void SetValue(const Arc &arc) { aiter_.SetValue(arc); }
-
-  void SetValue(const ArcClass &ac) final { aiter_.SetValue(ac.GetArc<Arc>()); }
+  void SetValue(const ArcClass &ac) final { SetValue(ac.GetArc<Arc>()); }
 
   // This is returned by value because it has not yet been constructed, and
   // is likely to participate in return-value optimization.
@@ -152,6 +151,8 @@ class MutableArcIteratorClassImpl
   ~MutableArcIteratorClassImpl() override {}
 
  private:
+  void SetValue(const Arc &arc) { aiter_.SetValue(arc); }
+
   MutableArcIterator<MutableFst<Arc>> aiter_;
 };
 
index 5796a71..e8dae66 100644 (file)
@@ -8,6 +8,7 @@
 #include <memory>
 #include <string>
 
+#include <fst/types.h>
 #include <fst/encode.h>
 #include <fst/generic-register.h>
 #include <fst/script/arc-class.h>
@@ -89,7 +90,7 @@ class EncodeMapperClassImpl : public EncodeMapperImplBase {
 
   ~EncodeMapperClassImpl() override {}
 
-  EncodeMapper<Arc> *GetImpl() const { return &mapper_; }
+  const EncodeMapper<Arc> *GetImpl() const { return &mapper_; }
 
   EncodeMapper<Arc> *GetImpl() { return &mapper_; }
 
@@ -261,7 +262,7 @@ struct EncodeMapperClassIORegistration {
 
 #define REGISTER_ENCODEMAPPER_CLASS(Arc)             \
   static EncodeMapperClassIORegistration::Registerer \
-      Class##_##Arc##_registerer(                    \
+      EncodeMapperClass_##Arc##_registerer(          \
           Arc::Type(),                               \
           EncodeMapperClassIORegistration::Entry(    \
               EncodeMapperClass::Read<Arc>, EncodeMapperClass::Create<Arc>));
index 791d292..052c7fd 100644 (file)
@@ -113,6 +113,7 @@ class AllFstOperationsRegisterer {
     REGISTER_FST_OPERATION(Info, Arc, InfoArgs);
     REGISTER_FST_OPERATION(Intersect, Arc, IntersectArgs);
     REGISTER_FST_OPERATION(Invert, Arc, MutableFstClass);
+    REGISTER_FST_OPERATION(Isomorphic, Arc, IsomorphicArgs);
   }
 
   void RegisterBatch2() {
index 7b3eb67..0b35d08 100644 (file)
@@ -9,6 +9,7 @@
 
 #include <string>
 
+#include <fst/types.h>
 #include <fst/log.h>
 #include <fst/compose.h>         // For ComposeFilter.
 #include <fst/determinize.h>     // For DeterminizeType.
index f0721cb..5f9c953 100644 (file)
@@ -11,6 +11,7 @@
 #include <string>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/connect.h>
 #include <fst/dfs-visit.h>
 #include <fst/fst.h>
@@ -307,8 +308,6 @@ class FstInfo {
   std::string arc_type_;
 };
 
-void InfoImpl(const FstInfo &fstinfo);
-
 }  // namespace fst
 
 #endif  // FST_SCRIPT_INFO_IMPL_H_
index 900f537..63a91a7 100644 (file)
@@ -6,6 +6,7 @@
 
 #include <tuple>
 
+#include <fst/types.h>
 #include <fst/push.h>
 #include <fst/script/fst-class.h>
 
index 225e1d5..3a1f4bf 100644 (file)
@@ -8,6 +8,7 @@
 #include <ctime>
 #include <tuple>
 
+#include <fst/types.h>
 #include <fst/randequivalent.h>
 #include <fst/script/arg-packs.h>
 #include <fst/script/fst-class.h>
index e4d0453..0232e20 100644 (file)
@@ -9,6 +9,7 @@
 #include <utility>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/relabel.h>
 #include <fst/script/fst-class.h>
 
index d447d9d..b30611e 100644 (file)
@@ -8,6 +8,7 @@
 #include <utility>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/replace.h>
 #include <fst/script/fst-class.h>
 
index 42986c8..724ff8b 100644 (file)
@@ -7,6 +7,7 @@
 #include <utility>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/queue.h>
 #include <fst/rmepsilon.h>
 #include <fst/script/fst-class.h>
index c09f888..4374360 100644 (file)
@@ -84,6 +84,7 @@
 #include <string>
 #include <utility>
 
+#include <fst/types.h>
 #include <fst/log.h>
 #include <fst/generic-register.h>
 #include <fst/script/fst-class.h>
@@ -108,11 +109,6 @@ class GenericOperationRegister
                              OperationSignature,
                              GenericOperationRegister<OperationSignature>> {
  public:
-  void RegisterOperation(const std::string &operation_name,
-                         const std::string &arc_type, OperationSignature op) {
-    this->SetEntry(std::make_pair(operation_name, arc_type), op);
-  }
-
   OperationSignature GetOperation(const std::string &operation_name,
                                   const std::string &arc_type) {
     return this->GetEntry(std::make_pair(operation_name, arc_type));
@@ -167,8 +163,7 @@ void Apply(const std::string &op_name, const std::string &arc_type,
   const auto op = OpReg::Register::GetRegister()->GetOperation(op_name,
                                                                arc_type);
   if (!op) {
-    FSTERROR() << "No operation found for " << op_name << " on "
-               << "arc type " << arc_type;
+    FSTERROR() << op_name << ": No operation found on arc type " << arc_type;
     return;
   }
   op(args);
@@ -182,8 +177,8 @@ namespace internal {
 template <class M, class N>
 bool ArcTypesMatch(const M &m, const N &n, const std::string &op_name) {
   if (m.ArcType() != n.ArcType()) {
-    FSTERROR() << "Arguments with non-matching arc types passed to "
-               << op_name << ":\t" << m.ArcType() << " and " << n.ArcType();
+    FSTERROR() << op_name << ": Arguments with non-matching arc types "
+               << m.ArcType() << " and " << n.ArcType();
     return false;
   }
   return true;
index e1e6633..531fa23 100644 (file)
@@ -7,6 +7,7 @@
 #include <memory>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/shortest-path.h>
 #include <fst/script/fst-class.h>
 #include <fst/script/shortest-distance.h>
index 7f98fae..8ca2ace 100644 (file)
@@ -186,13 +186,10 @@ std::ostream &operator<<(std::ostream &o, const WeightClass &c);
 
 // Registration for generic weight types.
 
-using StrToWeightImplBaseT = WeightImplBase *(*)(const std::string &str,
-                                                 const std::string &src,
-                                                 size_t nline);
+using StrToWeightImplBaseT = WeightImplBase *(*)(const std::string &str);
 
 template <class W>
-WeightImplBase *StrToWeightImplBase(const std::string &str,
-                                    const std::string &src, size_t nline) {
+WeightImplBase *StrToWeightImplBase(const std::string &str) {
   if (str == WeightClass::__ZERO__) {
     return new WeightClassImpl<W>(W::Zero());
   } else if (str == WeightClass::__ONE__) {
@@ -200,7 +197,7 @@ WeightImplBase *StrToWeightImplBase(const std::string &str,
   } else if (str == WeightClass::__NOWEIGHT__) {
     return new WeightClassImpl<W>(W::NoWeight());
   }
-  return new WeightClassImpl<W>(StrToWeight<W>(str, src, nline));
+  return new WeightClassImpl<W>(StrToWeight<W>(str));
 }
 
 class WeightClassRegister
index 6487524..cdba0d0 100644 (file)
@@ -14,6 +14,8 @@
 #include <string>
 #include <vector>
 
+#include <fst/types.h>
+
 #include <fst/union-weight.h>
 #include <fst/weight.h>
 
index 98f6f21..c34a756 100644 (file)
@@ -13,6 +13,8 @@
 
 #include <cstdlib>
 
+#include <fst/types.h>
+
 #include <fst/float-weight.h>
 #include <fst/pair-weight.h>
 #include <fst/product-weight.h>
index 3db3214..d7e17ee 100644 (file)
@@ -10,6 +10,8 @@
 #include <climits>
 #include <string>
 
+#include <fst/types.h>
+
 #include <fst/sparse-tuple-weight.h>
 #include <fst/weight.h>
 
index db99c01..3bdadde 100644 (file)
@@ -12,6 +12,7 @@
 #include <string>
 #include <utility>
 
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/arc-map.h>
@@ -345,12 +346,12 @@ class StateMapFst : public ImplToFst<internal::StateMapFstImpl<A, B, C>> {
             std::make_shared<Impl>(fst, mapper, StateMapFstOptions())) {}
 
   // See Fst<>::Copy() for doc.
-  StateMapFst(const StateMapFst<A, B, C> &fst, bool safe = false)
+  StateMapFst(const StateMapFst &fst, bool safe = false)
       : ImplToFst<Impl>(fst, safe) {}
 
   // Get a copy of this StateMapFst. See Fst<>::Copy() for further doc.
-  StateMapFst<A, B, C> *Copy(bool safe = false) const override {
-    return new StateMapFst<A, B, C>(*this, safe);
+  StateMapFst *Copy(bool safe = false) const override {
+    return new StateMapFst(*this, safe);
   }
 
   void InitStateIterator(StateIteratorData<B> *data) const override {
index d238e06..d7523ad 100644 (file)
@@ -10,6 +10,7 @@
 #include <utility>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/bi-table.h>
index 346c7d3..87c72ae 100644 (file)
@@ -9,6 +9,7 @@
 #include <algorithm>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/mutable-fst.h>
index 6dd38d0..75b9a5e 100644 (file)
@@ -11,6 +11,8 @@
 #include <string>
 #include <vector>
 
+#include <fst/types.h>
+
 #include <fst/product-weight.h>
 #include <fst/union-weight.h>
 #include <fst/weight.h>
index 490deda..3fb137f 100644 (file)
@@ -12,6 +12,7 @@
 #include <vector>
 
 #include <fst/flags.h>
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/compact-fst.h>
index 16f8c64..13e3a4f 100644 (file)
@@ -9,6 +9,7 @@
 #include <functional>
 #include <ios>
 #include <iostream>
+#include <iterator>
 #include <memory>
 #include <sstream>
 #include <string>
@@ -18,6 +19,7 @@
 
 #include <fst/compat.h>
 #include <fst/flags.h>
+#include <fst/types.h>
 #include <fst/log.h>
 #include <fstream>
 #include <map>
@@ -290,6 +292,8 @@ class SymbolTableImpl final : public MutableSymbolTableImpl {
 
 }  // namespace internal
 
+class SymbolTableIterator;
+
 // Symbol (string) to integer (and reverse) mapping.
 //
 // The SymbolTable implements the mappings of labels to strings and reverse.
@@ -303,6 +307,8 @@ class SymbolTableImpl final : public MutableSymbolTableImpl {
 class SymbolTable {
  public:
   using SymbolType = internal::SymbolTableImpl::SymbolType;
+  using iterator = SymbolTableIterator;
+  using const_iterator = iterator;
 
   // Constructs symbol table with an optional name.
   explicit SymbolTable(const std::string &name = "<unspecified>")
@@ -434,6 +440,11 @@ class SymbolTable {
   // Dumps a text representation of the symbol table.
   bool WriteText(const std::string &source) const;
 
+  const_iterator begin() const;
+  const_iterator end() const;
+  const_iterator cbegin() const;
+  const_iterator cend() const;
+
  protected:
   explicit SymbolTable(std::shared_ptr<internal::SymbolTableImplBase> impl)
       : impl_(impl) {}
@@ -460,14 +471,43 @@ class SymbolTable {
   std::shared_ptr<internal::SymbolTableImplBase> impl_;
 };
 
+namespace internal {
+
+// TODO(wolfsonkin): Consider adding std::tie and structured bindings support.
+class SymbolTableIteratorItem {
+ public:
+  explicit SymbolTableIteratorItem(const SymbolTable &table, ssize_t pos)
+      : table_(table), key_(table.GetNthKey(pos)) {}
+
+  // Return the label of the current symbol.
+  int64 Label() const { return key_; }
+
+  // Return the string of the current symbol.
+  // TODO(wolfsonkin): Consider adding caching
+  std::string Symbol() const { return table_.Find(key_); }
+
+ private:
+  // Sets this item to the pos'th element in the symbol table
+  void SetPosition(ssize_t pos) { key_ = table_.GetNthKey(pos); }
+
+  friend class fst::SymbolTableIterator;
+  const SymbolTable &table_;
+  int64 key_;
+};
+
+}  // namespace internal
+
 // Iterator class for symbols in a symbol table.
 class SymbolTableIterator {
  public:
+  using iterator_category = std::input_iterator_tag;
+  using value_type = internal::SymbolTableIteratorItem;
+  using difference_type = std::ptrdiff_t;
+  using pointer = const value_type *const;
+  using reference = const value_type &;
+
   explicit SymbolTableIterator(const SymbolTable &table)
-      : table_(table),
-        pos_(0),
-        nsymbols_(table.NumSymbols()),
-        key_(table.GetNthKey(0)) {}
+      : SymbolTableIterator(table, 0) {}
 
   ~SymbolTableIterator() {}
 
@@ -475,28 +515,54 @@ class SymbolTableIterator {
   bool Done() const { return (pos_ == nsymbols_); }
 
   // Return the key of the current symbol.
-  int64 Value() const { return key_; }
+  int64 Value() const { return iter_item_.Label(); }
 
   // Return the string of the current symbol.
-  std::string Symbol() const { return table_.Find(key_); }
+  std::string Symbol() const { return iter_item_.Symbol(); }
 
   // Advances iterator.
   void Next() {
     ++pos_;
-    if (pos_ < nsymbols_) key_ = table_.GetNthKey(pos_);
+    if (pos_ < nsymbols_) iter_item_.SetPosition(pos_);
+  }
+
+  SymbolTableIterator &operator++() {
+    Next();
+    return *this;
+  }
+
+  SymbolTableIterator operator++(int) {
+    SymbolTableIterator retval = *this;
+    ++(*this);
+    return retval;
+  }
+
+  bool operator==(const SymbolTableIterator &other) const {
+    return (pos_ == other.pos_);
+  }
+
+  bool operator!=(const SymbolTableIterator &other) const {
+    return !(*this == other);
   }
 
+  reference operator*() { return iter_item_; }
+
+  pointer operator->() const { return &iter_item_; }
+
   // Resets iterator.
   void Reset() {
     pos_ = 0;
-    key_ = table_.GetNthKey(0);
+    iter_item_.SetPosition(pos_);
   }
 
  private:
-  const SymbolTable &table_;
+  explicit SymbolTableIterator(const SymbolTable &table, ssize_t pos)
+      : pos_(pos), nsymbols_(table.NumSymbols()), iter_item_(table, pos) {}
+  friend class SymbolTable;
+
   ssize_t pos_;
   size_t nsymbols_;
-  int64 key_;
+  value_type iter_item_;
 };
 
 // Relabels a symbol table as specified by the input vector of pairs
index cc0d9a4..251c5cf 100644 (file)
@@ -12,6 +12,8 @@
 #include <unordered_set>
 #include <vector>
 
+#include <fst/types.h>
+
 #include <fst/cache.h>
 #include <fst/test-properties.h>
 
@@ -333,12 +335,12 @@ class SynchronizeFst : public ImplToFst<internal::SynchronizeFstImpl<A>> {
       : ImplToFst<Impl>(std::make_shared<Impl>(fst, opts)) {}
 
   // See Fst<>::Copy() for doc.
-  SynchronizeFst(const SynchronizeFst<Arc> &fst, bool safe = false)
+  SynchronizeFst(const SynchronizeFst &fst, bool safe = false)
       : ImplToFst<Impl>(fst, safe) {}
 
   // Gets a copy of this SynchronizeFst. See Fst<>::Copy() for further doc.
-  SynchronizeFst<Arc> *Copy(bool safe = false) const override {
-    return new SynchronizeFst<Arc>(*this, safe);
+  SynchronizeFst *Copy(bool safe = false) const override {
+    return new SynchronizeFst(*this, safe);
   }
 
   inline void InitStateIterator(StateIteratorData<Arc> *data) const override;
index 677ed01..eafcb10 100644 (file)
@@ -9,6 +9,7 @@
 #include <unordered_set>
 
 #include <fst/flags.h>
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/connect.h>
index e528da6..9d36677 100644 (file)
@@ -6,6 +6,7 @@
 #ifndef FST_TEST_ALGO_TEST_H_
 #define FST_TEST_ALGO_TEST_H_
 
+#include <fst/types.h>
 #include <fst/log.h>
 #include <fst/fstlib.h>
 #include <fst/test/rand-fst.h>
index e259acf..1c0546d 100644 (file)
@@ -8,6 +8,7 @@
 
 #include <type_traits>
 
+#include <fst/types.h>
 #include <fst/arc.h>
 #include <fst/fst.h>
 #include <fst/vector-fst.h>
index c8ecab7..ee39f48 100644 (file)
@@ -13,6 +13,7 @@
 #include <vector>
 
 #include <fst/flags.h>
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/weight.h>
index da68dc4..e05a03f 100644 (file)
@@ -15,6 +15,8 @@
 #include <string>
 #include <utility>
 
+#include <fst/types.h>
+
 #include <fst/weight.h>
 
 
index 25c7875..77fb18f 100644 (file)
@@ -10,6 +10,8 @@
 #include <utility>
 #include <vector>
 
+#include <fst/types.h>
+
 #include <fst/expanded-fst.h>
 #include <fst/mutable-fst.h>
 #include <fst/rational.h>
@@ -129,12 +131,12 @@ class UnionFst : public RationalFst<A> {
   }
 
   // See Fst<>::Copy() for doc.
-  UnionFst(const UnionFst<Arc> &fst, bool safe = false)
+  UnionFst(const UnionFst &fst, bool safe = false)
       : RationalFst<Arc>(fst, safe) {}
 
   // Gets a copy of this UnionFst. See Fst<>::Copy() for further doc.
-  UnionFst<Arc> *Copy(bool safe = false) const override {
-    return new UnionFst<Arc>(*this, safe);
+  UnionFst *Copy(bool safe = false) const override {
+    return new UnionFst(*this, safe);
   }
 
  private:
index 789c621..5ea9c35 100644 (file)
@@ -6,6 +6,7 @@
 #ifndef FST_UTIL_H_
 #define FST_UTIL_H_
 
+#include <array>
 #include <iostream>
 #include <iterator>
 #include <list>
@@ -59,7 +60,7 @@ inline std::istream &ReadType(std::istream &strm, T *t) {
 inline std::istream &ReadType(std::istream &strm, std::string *s) {  // NOLINT
   s->clear();
   int32 ns = 0;
-  strm.read(reinterpret_cast<char *>(&ns), sizeof(ns));
+  ReadType(strm, &ns);
   for (int32 i = 0; i < ns; ++i) {
     char c;
     strm.read(&c, 1);
@@ -114,6 +115,12 @@ std::istream &ReadContainerType(std::istream &strm, C *c, ReserveFn reserve) {
 }
 }  // namespace internal
 
+template <class T, size_t N>
+std::istream &ReadType(std::istream &strm, std::array<T, N> *c) {
+  for (auto &v : *c) ReadType(strm, &v);
+  return strm;
+}
+
 template <class... T>
 std::istream &ReadType(std::istream &strm, std::vector<T...> *c) {
   return internal::ReadContainerType(
@@ -168,7 +175,7 @@ inline std::ostream &WriteType(std::ostream &strm, const T t) {
 inline std::ostream &WriteType(std::ostream &strm,  // NOLINT
                                const std::string &s) {
   int32 ns = s.size();
-  strm.write(reinterpret_cast<const char *>(&ns), sizeof(ns));
+  WriteType(strm, ns);
   return strm.write(s.data(), ns);
 }
 
@@ -203,17 +210,27 @@ inline std::ostream &WriteType(std::ostream &strm,
 
 namespace internal {
 template <class C>
-std::ostream &WriteContainer(std::ostream &strm, const C &c) {
-  const int64 n = c.size();
-  WriteType(strm, n);
+std::ostream &WriteSequence(std::ostream &strm, const C &c) {
   for (const auto &e : c) {
     WriteType(strm, e);
   }
   return strm;
 }
 
+template <class C>
+std::ostream &WriteContainer(std::ostream &strm, const C &c) {
+  const int64 n = c.size();
+  WriteType(strm, n);
+  WriteSequence(strm, c);
+  return strm;
+}
 }  // namespace internal
 
+template <class T, size_t N>
+std::ostream &WriteType(std::ostream &strm, const std::array<T, N> &c) {
+  return internal::WriteSequence(strm, c);
+}
+
 template <typename... T>
 std::ostream &WriteType(std::ostream &strm, const std::vector<T...> &c) {
   return internal::WriteContainer(strm, c);
@@ -250,14 +267,12 @@ int64 StrToInt64(const std::string &s, const std::string &source, size_t nline,
                  bool allow_negative, bool *error = nullptr);
 
 template <typename Weight>
-Weight StrToWeight(const std::string &s, const std::string &source,
-                   size_t nline) {
+Weight StrToWeight(const std::string &s) {
   Weight w;
   std::istringstream strm(s);
   strm >> w;
   if (!strm) {
-    FSTERROR() << "StrToWeight: Bad weight = \"" << s
-               << "\", source = " << source << ", line = " << nline;
+    FSTERROR() << "StrToWeight: Bad weight: " << s;
     return Weight::NoWeight();
   }
   return w;
index 1af1f1b..b3e3f8f 100644 (file)
@@ -11,6 +11,7 @@
 #include <utility>
 #include <vector>
 
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/fst-decl.h>  // For optional argument declarations
@@ -40,22 +41,25 @@ class VectorState {
 
   // Provide STL allocator for arcs.
   explicit VectorState(const ArcAllocator &alloc)
-      : final_(Weight::Zero()), niepsilons_(0), noepsilons_(0), arcs_(alloc) {}
+      : final_weight_(Weight::Zero()),
+        niepsilons_(0),
+        noepsilons_(0),
+        arcs_(alloc) {}
 
   VectorState(const VectorState<A, M> &state, const ArcAllocator &alloc)
-      : final_(state.Final()),
+      : final_weight_(state.Final()),
         niepsilons_(state.NumInputEpsilons()),
         noepsilons_(state.NumOutputEpsilons()),
         arcs_(state.arcs_.begin(), state.arcs_.end(), alloc) {}
 
   void Reset() {
-    final_ = Weight::Zero();
+    final_weight_ = Weight::Zero();
     niepsilons_ = 0;
     noepsilons_ = 0;
     arcs_.clear();
   }
 
-  Weight Final() const { return final_; }
+  Weight Final() const { return final_weight_; }
 
   size_t NumInputEpsilons() const { return niepsilons_; }
 
@@ -71,7 +75,7 @@ class VectorState {
 
   void ReserveArcs(size_t n) { arcs_.reserve(n); }
 
-  void SetFinal(Weight weight) { final_ = std::move(weight); }
+  void SetFinal(Weight weight) { final_weight_ = std::move(weight); }
 
   void SetNumInputEpsilons(size_t n) { niepsilons_ = n; }
 
@@ -134,7 +138,7 @@ class VectorState {
     if (arc.olabel == 0) ++noepsilons_;
   }
 
-  Weight final_;                       // Final weight.
+  Weight final_weight_;                // Final weight.
   size_t niepsilons_;                  // # of input epsilons
   size_t noepsilons_;                  // # of output epsilons
   std::vector<A, ArcAllocator> arcs_;  // Arc container.
@@ -160,10 +164,10 @@ class VectorFstBaseImpl : public FstImpl<typename S::Arc> {
 
   // Copying is not permitted.
   VectorFstBaseImpl(const VectorFstBaseImpl<S> &) = delete;
-  VectorFstBaseImpl<S> &operator=(const VectorFstBaseImpl<S> &) = delete;
+  VectorFstBaseImpl &operator=(const VectorFstBaseImpl &) = delete;
 
   // Moving is permitted.
-  VectorFstBaseImpl(VectorFstBaseImpl<S> &&impl) noexcept
+  VectorFstBaseImpl(VectorFstBaseImpl &&impl) noexcept
       : FstImpl<typename S::Arc>(),
         states_(std::move(impl.states_)),
         start_(impl.start_) {
@@ -171,7 +175,7 @@ class VectorFstBaseImpl : public FstImpl<typename S::Arc> {
     impl.start_ = kNoStateId;
   }
 
-  VectorFstBaseImpl<S> &operator=(VectorFstBaseImpl<S> &&impl) noexcept {
+  VectorFstBaseImpl &operator=(VectorFstBaseImpl &&impl) noexcept {
     for (auto *state : states_) {
       State::Destroy(state, &state_alloc_);
     }
@@ -344,7 +348,7 @@ class VectorFstImpl : public VectorFstBaseImpl<S> {
 
   explicit VectorFstImpl(const Fst<Arc> &fst);
 
-  static VectorFstImpl<S> *Read(std::istream &strm, const FstReadOptions &opts);
+  static VectorFstImpl *Read(std::istream &strm, const FstReadOptions &opts);
 
   void SetStart(StateId state) {
     BaseImpl::SetStart(state);
@@ -457,7 +461,7 @@ VectorFstImpl<S>::VectorFstImpl(const Fst<Arc> &fst) {
 template <class S>
 VectorFstImpl<S> *VectorFstImpl<S>::Read(std::istream &strm,
                                          const FstReadOptions &opts) {
-  std::unique_ptr<VectorFstImpl<S>> impl(new VectorFstImpl());
+  std::unique_ptr<VectorFstImpl> impl(new VectorFstImpl());
   FstHeader hdr;
   if (!impl->ReadHeader(strm, opts, kMinFileVersion, &hdr)) return nullptr;
   impl->BaseImpl::SetStart(hdr.Start());
@@ -523,21 +527,21 @@ class VectorFst : public ImplToMutableFst<internal::VectorFstImpl<S>> {
   explicit VectorFst(const Fst<Arc> &fst)
       : ImplToMutableFst<Impl>(std::make_shared<Impl>(fst)) {}
 
-  VectorFst(const VectorFst<Arc, State> &fst, bool safe = false)
+  VectorFst(const VectorFst &fst, bool safe = false)
       : ImplToMutableFst<Impl>(fst) {}
 
-  VectorFst(VectorFst<Arc, State> &&) noexcept;
+  VectorFst(VectorFst &&) noexcept;
 
   // Get a copy of this VectorFst. See Fst<>::Copy() for further doc.
-  VectorFst<Arc, State> *Copy(bool safe = false) const override {
-    return new VectorFst<Arc, State>(*this, safe);
+  VectorFst *Copy(bool safe = false) const override {
+    return new VectorFst(*this, safe);
   }
 
-  VectorFst<Arc, State> &operator=(const VectorFst<Arc, State> &) = default;
+  VectorFst &operator=(const VectorFst &) = default;
 
-  VectorFst<Arc, State> &operator=(VectorFst<Arc, State> &&) noexcept;
+  VectorFst &operator=(VectorFst &&) noexcept;
 
-  VectorFst<Arc, State> &operator=(const Fst<Arc> &fst) override {
+  VectorFst &operator=(const Fst<Arc> &fst) override {
     if (this != &fst) SetImpl(std::make_shared<Impl>(fst));
     return *this;
   }
@@ -549,19 +553,16 @@ class VectorFst : public ImplToMutableFst<internal::VectorFstImpl<S>> {
   }
 
   // Reads a VectorFst from an input stream, returning nullptr on error.
-  static VectorFst<Arc, State> *Read(std::istream &strm,
-                                     const FstReadOptions &opts) {
+  static VectorFst *Read(std::istream &strm, const FstReadOptions &opts) {
     auto *impl = Impl::Read(strm, opts);
-    return impl ? new VectorFst<Arc, State>(std::shared_ptr<Impl>(impl))
-                : nullptr;
+    return impl ? new VectorFst(std::shared_ptr<Impl>(impl)) : nullptr;
   }
 
   // Read a VectorFst from a file, returning nullptr on error; empty source
   // reads from standard input.
-  static VectorFst<Arc, State> *Read(const std::string &source) {
+  static VectorFst *Read(const std::string &source) {
     auto *impl = ImplToExpandedFst<Impl, MutableFst<Arc>>::Read(source);
-    return impl ? new VectorFst<Arc, State>(std::shared_ptr<Impl>(impl))
-                : nullptr;
+    return impl ? new VectorFst(std::shared_ptr<Impl>(impl)) : nullptr;
   }
 
   bool Write(std::ostream &strm, const FstWriteOptions &opts) const override {
@@ -601,12 +602,11 @@ class VectorFst : public ImplToMutableFst<internal::VectorFstImpl<S>> {
 };
 
 template <class Arc, class State>
-inline VectorFst<Arc, State>::VectorFst(
-    VectorFst<Arc, State> &&fst) noexcept = default;
+inline VectorFst<Arc, State>::VectorFst(VectorFst &&fst) noexcept = default;
 
 template <class Arc, class State>
 inline VectorFst<Arc, State> &VectorFst<Arc, State>::operator=(
-    VectorFst<Arc, State> &&fst) noexcept = default;
+    VectorFst &&fst) noexcept = default;
 
 // Writes FST to file in Vector format, potentially with a pass over the machine
 // before writing to compute number of states.
index 83ae7b8..b6074ae 100644 (file)
@@ -6,6 +6,7 @@
 #ifndef FST_VERIFY_H_
 #define FST_VERIFY_H_
 
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/fst.h>
index 236c541..81e8301 100644 (file)
@@ -14,6 +14,7 @@
 #include <utility>
 
 #include <fst/compat.h>
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fst/util.h>
index 0c1147e..c32c234 100644 (file)
@@ -8,6 +8,7 @@
 #include <sstream>
 
 #include <fst/flags.h>
+#include <fst/types.h>
 #include <fst/log.h>
 #include <fst/matcher-fst.h>  // declarations of *_lookahead_fst_type
 
index fa9f8ce..97e2c3f 100644 (file)
@@ -109,7 +109,7 @@ MappedFile *MappedFile::Allocate(size_t size, size_t align) {
     // Use std::aligned_alloc() when C++17 is allowed.
     char *buffer = static_cast<char *>(operator new(size + align));
     uintptr_t address = reinterpret_cast<uintptr_t>(buffer);
-    region.offset = kArchAlignment - (address % align);
+    region.offset = align - (address % align);
     region.data = buffer + region.offset;
   }
   region.mmap = nullptr;
index bd3dd92..ecc1616 100644 (file)
@@ -10,6 +10,8 @@
 #include <string>
 #include <vector>
 
+#include <fst/types.h>
+
 namespace fst {
 
 // These functions determine the properties associated with the FST result of
index 785e17a..48a620f 100644 (file)
@@ -6,6 +6,7 @@
 #include <fst/symbol-table.h>
 
 #include <fst/flags.h>
+#include <fst/types.h>
 #include <fst/log.h>
 
 #include <fstream>
@@ -395,6 +396,18 @@ bool SymbolTable::WriteText(const std::string &source) const {
   }
 }
 
+SymbolTable::const_iterator SymbolTable::begin() const {
+  return SymbolTable::const_iterator(*this, 0);
+}
+
+SymbolTable::const_iterator SymbolTable::end() const {
+  return SymbolTable::const_iterator(*this, this->NumSymbols());
+}
+
+SymbolTable::const_iterator SymbolTable::cbegin() const { return begin(); }
+
+SymbolTable::const_iterator SymbolTable::cend() const { return end(); }
+
 bool CompatSymbols(const SymbolTable *syms1, const SymbolTable *syms2,
                    bool warning) {
   // Flag can explicitly override this check.
index 438c023..2eb0a41 100644 (file)
@@ -10,6 +10,7 @@
 #include <string>
 
 #include <fst/flags.h>
+#include <fst/types.h>
 #include <fst/log.h>
 #include <fst/mapped-file.h>
 
index 9522f91..494d40b 100644 (file)
@@ -13,5 +13,5 @@ shortest-distance.cc shortest-path.cc stateiterator-class.cc synchronize.cc \
 text-io.cc topsort.cc union.cc weight-class.cc verify.cc
 
 libfstscript_la_LIBADD = ../lib/libfst.la -lm $(DL_LIBS)
-libfstscript_la_LDFLAGS = -version-info 17:0:0
+libfstscript_la_LDFLAGS = -version-info 18:0:0
 endif
index 362b922..e7edfe9 100644 (file)
@@ -407,7 +407,7 @@ AM_CPPFLAGS = -I$(srcdir)/../include $(ICU_CPPFLAGS)
 @HAVE_SCRIPT_TRUE@text-io.cc topsort.cc union.cc weight-class.cc verify.cc
 
 @HAVE_SCRIPT_TRUE@libfstscript_la_LIBADD = ../lib/libfst.la -lm $(DL_LIBS)
-@HAVE_SCRIPT_TRUE@libfstscript_la_LDFLAGS = -version-info 17:0:0
+@HAVE_SCRIPT_TRUE@libfstscript_la_LDFLAGS = -version-info 18:0:0
 all: all-am
 
 .SUFFIXES:
index 3ef73f4..2ee3b98 100644 (file)
@@ -82,8 +82,8 @@ FstClass *FstClass::Read(std::istream &istrm, const std::string &source) {
 bool FstClass::WeightTypesMatch(const WeightClass &weight,
                                 const std::string &op_name) const {
   if (WeightType() != weight.Type()) {
-    FSTERROR() << "FST and weight with non-matching weight types passed to "
-               << op_name << ": " << WeightType() << " and " << weight.Type();
+    FSTERROR() << op_name << ": FST and weight with non-matching weight types: "
+               << WeightType() << " and " << weight.Type();
     return false;
   }
   return true;
index ea525c0..70e0ef4 100644 (file)
@@ -12,14 +12,14 @@ REGISTER_FST_WEIGHT(Log64Arc::Weight);
 
 WeightClass::WeightClass(const std::string &weight_type,
                          const std::string &weight_str) {
-  WeightClassRegister *reg = WeightClassRegister::GetRegister();
-  StrToWeightImplBaseT stw = reg->GetEntry(weight_type);
+  static const auto *reg = WeightClassRegister::GetRegister();
+  const auto stw = reg->GetEntry(weight_type);
   if (!stw) {
-    FSTERROR() << "Unknown weight type: " << weight_type;
+    FSTERROR() << "WeightClass: Unknown weight type: " << weight_type;
     impl_.reset();
     return;
   }
-  impl_.reset(stw(weight_str, "WeightClass", 0));
+  impl_.reset(stw(weight_str));
 }
 
 constexpr char WeightClass::__ZERO__[];
@@ -42,8 +42,8 @@ bool WeightClass::WeightTypesMatch(const WeightClass &lhs,
                                    const WeightClass &rhs,
                                    const std::string &op_name) {
   if (lhs.Type() != rhs.Type()) {
-    FSTERROR() << "Weights with non-matching types passed to " << op_name
-               << ": " << lhs.Type() << " and " << rhs.Type();
+    FSTERROR() << op_name << ": Weights with non-matching types: " << lhs.Type()
+               << " and " << rhs.Type();
     return false;
   }
   return true;
index a9d7c5e..08a329a 100644 (file)
@@ -6,6 +6,7 @@
 #include <fst/test/fst_test.h>
 
 #include <fst/flags.h>
+#include <fst/types.h>
 #include <fst/log.h>
 #include <fst/compact-fst.h>
 #include <fst/const-fst.h>