-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.yl
More file actions
54 lines (49 loc) · 848 Bytes
/
Copy pathmod.yl
File metadata and controls
54 lines (49 loc) · 848 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**********************************************************************
AUTHOR: Kenneth Pollick <me@kennethpollick.com>
COPYRIGHT: 2022-2026 Kenneth Pollick
DATE: 2026-05-09
**********************************************************************/
type mod
[
(natural) n
inherit (sdt)
]
(
(type) t
)
{
(t) val
/***
ctor(dt#0 d)
{
if (n < 2)
err "Type mod must have a size abstraction greater than one";
this.val = d % n;
}
***/
algorithm unwrap
static
[
return (t)
]
(
(THIS) this
)
{
return this.val
}
algorithm add
static
[
return (THIS)
]
(
(THIS) this
(t) r
)
{
// don't need to modulo beforehand if n is a power of 2 (might remove for dynamically allocated numbers or separate implementations)
constant dt#0 rp = ternary{(popcount(n) == 1), r, (r % n)};
return ctor(this.val + rp);
}
}