Made the zigzag encoding examples compile and run (#421)

Made the zigzag encoding examples compile and run
This commit is contained in:
Trangar 2021-10-25 11:24:50 +02:00 committed by GitHub
parent b30805dde2
commit 73616a3d62
1 changed files with 19 additions and 4 deletions

View File

@ -102,24 +102,39 @@ impl<E, I, A> Configuration<E, I, A> {
/// ///
/// The zigzag algorithm is defined as follows: /// The zigzag algorithm is defined as follows:
/// ///
/// ```ignore /// ```rust
/// # type Signed = i32;
/// # type Unsigned = u32;
/// fn zigzag(v: Signed) -> Unsigned { /// fn zigzag(v: Signed) -> Unsigned {
/// match v { /// match v {
/// 0 => 0, /// 0 => 0,
/// v if v < 0 => |v| * 2 - 1 /// // To avoid the edge case of Signed::min_value()
/// v if v > 0 => v * 2 /// // !n is equal to `-n - 1`, so this is:
/// // !n * 2 + 1 = 2(-n - 1) + 1 = -2n - 2 + 1 = -2n - 1
/// v if v < 0 => !(v as Unsigned) * 2 - 1,
/// v if v > 0 => (v as Unsigned) * 2,
/// # _ => unreachable!()
/// } /// }
/// } /// }
/// ``` /// ```
/// ///
/// And works such that: /// And works such that:
/// ///
/// ```ignore /// ```rust
/// # let zigzag = |n: i64| -> u64 {
/// # match n {
/// # 0 => 0,
/// # v if v < 0 => !(v as u64) * 2 + 1,
/// # v if v > 0 => (v as u64) * 2,
/// # _ => unreachable!(),
/// # }
/// # };
/// assert_eq!(zigzag(0), 0); /// assert_eq!(zigzag(0), 0);
/// assert_eq!(zigzag(-1), 1); /// assert_eq!(zigzag(-1), 1);
/// assert_eq!(zigzag(1), 2); /// assert_eq!(zigzag(1), 2);
/// assert_eq!(zigzag(-2), 3); /// assert_eq!(zigzag(-2), 3);
/// assert_eq!(zigzag(2), 4); /// assert_eq!(zigzag(2), 4);
/// // etc
/// assert_eq!(zigzag(i64::min_value()), u64::max_value()); /// assert_eq!(zigzag(i64::min_value()), u64::max_value());
/// ``` /// ```
/// ///