@@ -34,27 +34,54 @@ pure fn mul(x: uint, y: uint) -> uint { ret x * y; }
34
34
/* Function: div */
35
35
pure fn div ( x : uint , y : uint ) -> uint { ret x / y; }
36
36
37
- /**
38
- * Divide two numbers, return the result, rounded up.
39
- */
37
+ /* Function: div_ceil
38
+
39
+ Divide two numbers, return the result, rounded up.
40
+
41
+ Parameters:
42
+ x - an integer
43
+ y - an integer distinct from 0u
44
+
45
+ Return:
46
+ The smallest integer `q` such that `x/y <= q`.
47
+ */
40
48
pure fn div_ceil ( x : uint , y : uint ) -> uint {
41
49
let div = div ( x, y) ;
42
50
if x % y == 0 u { ret div; }
43
51
else { ret div + 1 u; }
44
52
}
45
53
46
- /**
47
- * Divide two numbers, return the result, rounded to the closest integer.
48
- */
54
+ /* Function: div_ceil
55
+
56
+ Divide two numbers, return the result, rounded to the closest integer.
57
+
58
+ Parameters:
59
+ x - an integer
60
+ y - an integer distinct from 0u
61
+
62
+ Return:
63
+ The integer `q` closest to `x/y`.
64
+ */
49
65
pure fn div_round ( x : uint , y : uint ) -> uint {
50
66
let div = div ( x, y) ;
51
67
if x % y * 2 u < y { ret div; }
52
68
else { ret div + 1 u; }
53
69
}
54
70
55
- /**
56
- * Divide two numbers, return the result, rounded down.
57
- */
71
+ /* Function: div_ceil
72
+
73
+ Divide two numbers, return the result, rounded down.
74
+
75
+ Parameters:
76
+ x - an integer
77
+ y - an integer distinct from 0u
78
+
79
+ Note: This is the same function as `div`.
80
+
81
+ Return:
82
+ The smallest integer `q` such that `x/y <= q`. This
83
+ is either `x/y` or `x/y + 1`.
84
+ */
58
85
pure fn div_floor ( x : uint , y : uint ) -> uint { ret x / y; }
59
86
60
87
/* Function: rem */
@@ -88,6 +115,31 @@ fn range(lo: uint, hi: uint, it: block(uint)) {
88
115
while i < hi { it ( i) ; i += 1 u; }
89
116
}
90
117
118
+ /*
119
+ Function: loop
120
+
121
+ Iterate over the range [`lo`..`hi`), or stop when requested
122
+
123
+ Parameters:
124
+ lo - The integer at which to start the loop (included)
125
+ hi - The integer at which to stop the loop (excluded)
126
+ it - A block to execute with each consecutive integer of the range.
127
+ Return `true` to continue, `false` to stop.
128
+
129
+ Returns:
130
+
131
+ `true` If execution proceeded correctly, `false` if it was interrupted,
132
+ that is if `it` returned `false` at any point.
133
+ */
134
+ fn loop ( lo : uint , hi : uint , it : block ( uint ) -> bool ) -> bool {
135
+ let i = lo;
136
+ while i < hi {
137
+ if ( !it ( i) ) { ret false ; }
138
+ i += 1 u;
139
+ }
140
+ ret true;
141
+ }
142
+
91
143
/*
92
144
Function: next_power_of_two
93
145
0 commit comments