Expand description
This crate contains many utilities that are used by the
malachite-nz and
malachite-q crates. These utilities
include
- Traits that wrap functions from the standard library, like
CheckedAdd. - Traits that give extra functionality to primitive types, like
Gcd,FloorSqrt, andBitAccess. - Iterator-producing functions that let you generate values for testing. Here’s an example of
an iterator that produces all pairs of
u32s:use malachite_base::num::exhaustive::exhaustive_unsigneds; use malachite_base::tuples::exhaustive::exhaustive_pairs_from_single; let mut pairs = exhaustive_pairs_from_single(exhaustive_unsigneds::<u32>()); assert_eq!( pairs.take(20).collect::<Vec<_>>(), &[ (0, 0), (0, 1), (1, 0), (1, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 0), (2, 1), (3, 0), (3, 1), (2, 2), (2, 3), (3, 2), (3, 3), (0, 4), (0, 5), (1, 4), (1, 5) ] ); - The
RoundingModeenum, which allows you to specify the rounding behavior of various functions. - The
NiceFloatwrapper, which provides alternative implementations ofEq,Ord, andDisplayfor floating-point values which are in some ways nicer than the defaults.
§Complexity conventions
Most functions in Malachite come with a “Worst-case complexity” section stating time and
additional-memory bounds, like $T(n) = O(n \log n \log\log n)$ and $M(n) = O(n)$, along with a
line defining each variable. The model is a word RAM: time counts operations on machine words
(an operation on any primitive type up to u128 compiles to a bounded number of native
instructions, so it counts as one step), and additional memory counts words allocated beyond
the inputs and the output.
Since primitive-integer inputs are bounded, every function on them technically runs in constant time. The complexity sections are written to be more informative than that:
- “Constant time and additional memory” means the operation count is bounded independently of the type’s width and of the inputs’ values.
- Otherwise, the bound is written in terms of variables, describing how the work would scale
if the same algorithm were instantiated at an arbitrarily large width. For example,
mod_powfor primitive types is documented as $T(n) = O(n)$, where $n$ isexp.significant_bits(): the work scales with the exponent’s bit length. Every such variable is bounded by the type’s width, so these bounds may be read as constants; the variable form tells you what the constant depends on.
§Demos and benchmarks
This crate comes with a bin target that can be used for running demos and benchmarks.
- Almost all of the public functions in this crate have an associated demo. Running a demo
shows you a function’s behavior on a large number of inputs. For example, to demo the
mod_powfunction onu32s, you can use the following command:This command uses thecargo run --features bin_build --release -- -l 10000 -m exhaustive -d demo_mod_pow_u32exhaustivemode, which generates every possible input, generally starting with the simplest input and progressing to more complex ones. Another mode israndom. The-lflag specifies how many inputs should be generated. - You can use a similar command to run benchmarks. The following command benchmarks various
GCD algorithms for
u64s:This creates a file called gcd-bench.gp. You can use gnuplot to create an SVG from it like so:cargo run --features bin_build --release -- -l 1000000 -m random -b \ benchmark_gcd_algorithms_u64 -o gcd-bench.gpgnuplot -e "set terminal svg; l \"gcd-bench.gp\"" > gcd-bench.svg
The list of available demos and benchmarks is not documented anywhere; you must find them by
browsing through
bin_util/demo_and_bench.
§Features
test_build: A large proportion of the code in this crate is only used for testing. For a typical user, building this code would result in an unnecessarily long compilation time and an unnecessarily large binary. Much of it is also used for testingmalachite-nzandmalachite-q, so it can’t just be confined to thetestsdirectory. My solution is to only build this code when thetest_buildfeature is enabled. If you want to run unit tests, you must enabletest_build. However, doctests don’t require it, since they only test the public interface.bin_build: This feature is used to build the code for demos and benchmarks, which also takes a long time to build. Enabling this feature also enablestest_build.
Modules§
- bools
- Functions for working with
bools. - chars
- Functions for working with
chars. - comparison
- Macros and traits related to comparing values.
- foer_
sequences FoerSequence, a type representing a sequence that is finite or eventually repeating (which is what “foer” abbreviates), just like the digits of a rational number.- iterators
- Functions and adaptors for iterators.
- named
- The
Namedtrait, for getting a type’s name. - nevers
Never, a type that cannot be instantiated.- num
- Functions for working with primitive integers and floats.
- options
- Functions for working with
Orderings. - orderings
- Functions for working with
Options. - platform
- random
- Functions for generating random values.
- rounding_
modes RoundingMode, an enum used to specify rounding behavior.- sets
- Functions for working with
HashSets andBTreeSets. - slices
- Functions for working with slices.
- strings
- Functions for working with
Strings. - tuples
- Functions for working with tuples.
- unions
- Unions (sum types). These are essentially generic enums.
- vecs
- Functions for working with
Vecs.
Macros§
- custom_
tuples - Defines custom exhaustive tuple generators.
- exhaustive_
ordered_ unique_ tuples - Defines exhaustive ordered unique tuple generators.
- exhaustive_
tuples - Defines exhaustive tuple generators.
- exhaustive_
tuples_ 1_ input - Defines exhaustive tuple generators that generate tuples from a single iterator.
- exhaustive_
unions - Defines exhaustive union generators.
- exhaustive_
unique_ tuples - Defines lexicographic unique tuple generators.
- exhaustive_
vecs_ fixed_ length - Defines exhaustive fixed-length
Vecgenerators. - fma
- gmp_
format - Formats values according to a GMP-style
printfformat string, asgmp_formatdoes, taking the values as ordinary arguments:gmp_format!("%Zd of %d", n, k). - impl_
named - Automatically implements
Namedfor a type. - lex_
custom_ tuples - Defines custom lexicographic tuple generators.
- lex_
ordered_ unique_ tuples - Defines lexicographic ordered unique tuple generators.
- lex_
tuples - Defines lexicographic tuple generators.
- lex_
unique_ tuples - Defines lexicographic unique tuple generators.
- lex_
vecs_ fixed_ length - Defines lexicographic fixed-length
Vecgenerators. - max
- Computes the maximum of a list of expressions.
- min
- Computes the minimum of a list of expressions.
- random_
custom_ tuples - Defines custom random tuple generators.
- random_
ordered_ unique_ tuples - Defines random ordered unique tuple generators.
- random_
tuples - Defines random tuple generators.
- random_
unions - Defines random union generators.
- random_
unique_ tuples - Defines random unique tuple generators.
- random_
vecs_ fixed_ length - Defines random fixed-length
Vecgenerators. - round_
even - split_
into_ chunks - Splits an immutable slice into adjacent immutable chunks.
- split_
into_ chunks_ mut - Splits a mutable slice into adjacent mutable chunks.
- union_
struct - Defines unions.