core/cmp
skip to the library navigationShared: available on every enabled target.
Equality, hashing and ordering evidence, apart from the containers and algorithms that consume it.
The three concepts ship for these types:
| Type | equatable | hashable | ordered |
|---|---|---|---|
| the ten integer scalars | yes | yes | yes |
bool | yes | yes | no |
[]u8 | yes | yes | yes, by compare |
utf8, from core/text | yes | yes | yes |
time.duration and time.instant, from core/time | yes | yes | yes, by their counts |
net.address, from core/net | yes, as same_address, zone included | yes | no |
| floating-point types | no | no | no |
Every hash is keyed by a cmp.hash_seed, made by cmp.seed_from from two u64 words:
| Call | What it does |
|---|---|
cmp.seed_from | Make a seed from two words, SipHash's k0 and k1. |
cmp.hash_bytes | SipHash-1-3 of a byte slice under a seed. |
cmp.hash_word | The same of a u64's eight bytes, least significant first. |
The shipped hashes are these two. An integer hashes as hash_word of its bits widened to 64, a bool as the word 0 or 1, and []u8, utf8 and net.address as hash_bytes of the bytes their equality compares. They give the same result for a seed on every target, Cortex-M0 included. Whoever does not know the seed cannot tell which keys will collide. A key type of your own builds its hash from these with the seed it is given. SipHash is a keyed hash, not a cryptographic digest.
compare orders two byte strings as less, equal or greater. The first differing byte decides, and a proper prefix comes first. This is C's memcmp with the lengths taken into account, and it is the order ls gives names in the C locale. The ordered evidence of []u8 is the less of this order.
There is one register and no override. To read a shipped type another way, put it in a distinct wrapper that has its own evidence. A hashable key declares its equatable conformance as well.
The compiler does not prove the laws:
- equality must be an equivalence;
- equal keys must hash alike under one seed;
lessmust be a strict weak ordering.
Items
public equatable: type = concept (item: type)
Report whether two items are equal under a stable equivalence.
equal: (left: item, right: item) -> (yes: bool)
end equatable
Equality evidence. equal must be an equivalence relation, and stay one while an item is stored, for example as a map key.
Hash evidence, which composes equatable. Items that are equal must have equal hashes under the same seed, and both answers must stay stable while an item is stored. A hash should depend on the seed as well as the item, so that one who does not know the seed cannot choose items that collide: the hashes this module ships are SipHash-1-3 keyed by it, and a composite key builds its hash from hash_bytes or hash_word with the seed it is given. They are not cryptographic digests, and the seed is a secret only as long as the program keeps it one.
Although hashable composes equatable, [1340] still requires a concrete type to declare both conformances explicitly.
public ordered: type = concept (item: type)
Return true exactly when left precedes right under the strict weak ordering.
less: (left: item, right: item) -> (yes: bool)
end ordered
Strict ordering evidence. less must be irreflexive and transitive, with consistent equivalence classes, for the duration of a sort.
public hash_seed: type = seed_value
The key of a keyed hash: 128 bits that choose which of SipHash's functions hash_bytes and hash_word compute. Whoever does not know the seed cannot tell which keys will collide, so a map seeded from entropy cannot be filled with keys chosen to share one probe chain. Make one with seed_from; a hosted program draws one with random.hash_seed.
How two byte strings order: less, equal or greater.
public equal: atom
The two byte strings are the same bytes.
public greater: atom
The left bytes order after the right ones.
public less: atom
The left bytes order before the right ones.
public compare: (left: []u8, right: []u8) -> (result: order)
Order two byte strings as unsigned bytes, the first difference deciding, and a string that is a proper prefix of the other ordering first: C's memcmp with the lengths taken into account, and the order ls gives names in the C locale. []u8's ordered evidence is this order's less.
public hash_bytes: (value: []u8, seed: hash_seed) -> (result: u64)
SipHash-1-3 of value keyed by seed: Aumasson and Bernstein's SipHash with one compression round per eight-byte word and three finalization rounds, the variant Rust's and CPython's hash tables use. It is []u8's hash, and utf8's, and gives the same result on every target. It is not a cryptographic digest.
public hash_word: (value: u64, seed: hash_seed) -> (result: u64)
SipHash-1-3 keyed by seed of value's eight bytes, least significant first: what hash_bytes gives for those bytes, without storing them. Every integer scalar's hash is this word's.
public seed_from: (first: u64, second: u64) -> (seed: hash_seed)
The seed whose SipHash key is first and then second, each as its eight little-endian bytes: SipHash's k0 and k1. The same words give the same hashes on every target.
Example program
This complete program is one of the compiler's runtime tests. View source.
The program, 39 lines
import core/cmp -- The shipped evidence answers through the concepts alone, without a map -- or a sort to carry it. same: (item: type is cmp.equatable, left: item, right: item) -> (yes: bool) = yes = item.equal(left, right) end same digest: (item: type is cmp.hashable, value: item) -> (result: u64) = result = item.hash(value, test_seed()) end digest before: (item: type is cmp.ordered, left: item, right: item) -> (yes: bool) = yes = item.less(left, right) end before exercise: () -> (ok: bool) = ok = false bytes: []u8 = "ab" copy: []u8 = "ab" other: []u8 = "ba" return when not same(u32(7), u32(7)) or same(true, false) return when not same(bytes, copy) or same(bytes, other) return when digest(u16(9)) <> cmp.hash_word(9, test_seed()) or digest(bytes) == digest(other) ok = before(i32(-1), i32(0)) and not before(u8(2), u8(2)) end exercise public main: () -> (code: i32) = code = 1 ok := exercise() if ok then code = 42 end if end main -- A constant seed, so that every run hashes alike. test_seed: () -> (seed: cmp.hash_seed) = seed = cmp.seed_from(1, 2) end test_seed