Lax489179.IntegerEncoding
Encoding signed integers and missing distances
concepts/Lax489179/IntegerEncoding.lean · lax-489179
No public endorsements yet.
Loading review…
Sign in with ORCIDConcept map
Definition
Signed integers are stored in natural-number words using , , , , and so on. For an optional integer, zero denotes absence and an integer is encoded by its signed code plus one. This separates a missing edge or infinite distance from every finite weight, including zero and negative weights.
Lean source view on GitHub
| 1 | import Mathlib.Data.Int.Basic |
| 2 | |
| 3 | /-! |
| 4 | --- |
| 5 | title: Encoding signed integers and missing distances |
| 6 | type: definition |
| 7 | --- |
| 8 | Signed integers are stored in natural-number words using |
| 9 | , , , , and so on. |
| 10 | For an optional integer, zero denotes absence and an integer is encoded |
| 11 | by its signed code plus one. This separates a missing edge or infinite |
| 12 | distance from every finite weight, including zero and negative weights. |
| 13 | -/ |
| 14 | |
| 15 | namespace Lax489179.IntegerEncoding |
| 16 | |
| 17 | def encodeInt : ℤ → ℕ |
| 18 | | .ofNat n => 2 * n |
| 19 | | .negSucc n => 2 * n + 1 |
| 20 | |
| 21 | def decodeInt (n : ℕ) : ℤ := |
| 22 | if n % 2 = 0 then .ofNat (n / 2) else .negSucc (n / 2) |
| 23 | |
| 24 | def encodeOption : Option ℤ → ℕ |
| 25 | | none => 0 |
| 26 | | some z => encodeInt z + 1 |
| 27 | |
| 28 | def decodeOption : ℕ → Option ℤ |
| 29 | | 0 => none |
| 30 | | n + 1 => some (decodeInt n) |
| 31 | |
| 32 | end Lax489179.IntegerEncoding |
| 33 |
Builds on
none
From Mathlib
Community review
Discussion
Ask a question or add context. Endorsements and structured flags are kept in the review panel above; your ORCID profile must share a public name.
0 comments