Skip to content

Commit 68f7e67

Browse files
committed
Change from usize to u64
On [random-access-storage](https://github.com/datrs/random-access-storage) issue datrs/random-access-storage#6 has changed all types from `usize` into `u64` to be able to handle more than 4gbs on 32bits systems. > usize is 32 bits on a 32 bit system, so the storage would be limited to 4GB on such a system. When changing `random-access-storage` on `hypercore` (tracking: datrs/hypercore#100) one of the things I've noticed is that it wold be easier to also make `flat-tree` use `u64` to integrate with `random-access-storage`. Very likely, this also means that we would need to bump to use more than 32Gb storages on `flat-tree`. I've simply changed all declarations of usize into u64. All tests are passing.
1 parent f9ca2ef commit 68f7e67

File tree

2 files changed

+35
-35
lines changed

2 files changed

+35
-35
lines changed

src/iterator.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ use std::iter;
1313
/// Iterator over a flat-tree.
1414
#[derive(Debug)]
1515
pub struct Iterator {
16-
index: usize,
17-
offset: usize,
18-
factor: usize,
16+
index: u64,
17+
offset: u64,
18+
factor: u64,
1919
}
2020

2121
impl Iterator {
@@ -24,10 +24,10 @@ impl Iterator {
2424
/// ## Examples
2525
/// ```rust
2626
/// use flat_tree::Iterator;
27-
/// assert_eq!(Iterator::new(0).take(3).collect::<Vec<usize>>(), [2, 4, 6]);
27+
/// assert_eq!(Iterator::new(0).take(3).collect::<Vec<u64>>(), [2, 4, 6]);
2828
/// ```
2929
#[inline]
30-
pub fn new(index: usize) -> Self {
30+
pub fn new(index: u64) -> Self {
3131
let mut instance = Self {
3232
index: 0,
3333
offset: 0,
@@ -40,19 +40,19 @@ impl Iterator {
4040

4141
/// Get the current index.
4242
#[inline]
43-
pub fn index(&self) -> usize {
43+
pub fn index(&self) -> u64 {
4444
self.index
4545
}
4646

4747
/// Get the current offset.
4848
#[inline]
49-
pub fn offset(&self) -> usize {
49+
pub fn offset(&self) -> u64 {
5050
self.offset
5151
}
5252

5353
/// Get the current factor.
5454
#[inline]
55-
pub fn factor(&self) -> usize {
55+
pub fn factor(&self) -> u64 {
5656
self.factor
5757
}
5858

@@ -67,7 +67,7 @@ impl Iterator {
6767
/// assert_eq!(iter.next(), Some(4));
6868
/// ```
6969
#[inline]
70-
pub fn seek(&mut self, index: usize) {
70+
pub fn seek(&mut self, index: u64) {
7171
self.index = index;
7272
if is_odd(self.index) {
7373
self.offset = offset(index);
@@ -114,7 +114,7 @@ impl Iterator {
114114
/// assert_eq!(iter.prev(), 0);
115115
/// ```
116116
#[inline]
117-
pub fn prev(&mut self) -> usize {
117+
pub fn prev(&mut self) -> u64 {
118118
if self.offset == 0 {
119119
return self.index;
120120
}
@@ -132,7 +132,7 @@ impl Iterator {
132132
/// assert_eq!(flat_tree::Iterator::new(4).sibling(), 6);
133133
/// ```
134134
#[inline]
135-
pub fn sibling(&mut self) -> usize {
135+
pub fn sibling(&mut self) -> u64 {
136136
if self.is_left() {
137137
self.next().unwrap() // this is always safe
138138
} else {
@@ -149,7 +149,7 @@ impl Iterator {
149149
/// assert_eq!(flat_tree::Iterator::new(4).parent(), 5);
150150
/// ```
151151
#[inline]
152-
pub fn parent(&mut self) -> usize {
152+
pub fn parent(&mut self) -> u64 {
153153
if is_odd(self.offset) {
154154
self.index -= self.factor / 2;
155155
self.offset = (self.offset - 1) / 2;
@@ -172,7 +172,7 @@ impl Iterator {
172172
/// assert_eq!(flat_tree::Iterator::new(27).left_span(), 24);
173173
/// ```
174174
#[inline]
175-
pub fn left_span(&mut self) -> usize {
175+
pub fn left_span(&mut self) -> u64 {
176176
self.index = self.index + 1 - self.factor / 2;
177177
self.offset = self.index / 2;
178178
self.factor = 2;
@@ -190,7 +190,7 @@ impl Iterator {
190190
/// assert_eq!(flat_tree::Iterator::new(27).right_span(), 30);
191191
/// ```
192192
#[inline]
193-
pub fn right_span(&mut self) -> usize {
193+
pub fn right_span(&mut self) -> u64 {
194194
self.index = self.index + self.factor / 2 - 1;
195195
self.offset = self.index / 2;
196196
self.factor = 2;
@@ -206,7 +206,7 @@ impl Iterator {
206206
/// assert_eq!(flat_tree::Iterator::new(7).left_child(), 3);
207207
/// ```
208208
#[inline]
209-
pub fn left_child(&mut self) -> usize {
209+
pub fn left_child(&mut self) -> u64 {
210210
if self.factor == 2 {
211211
return self.index;
212212
}
@@ -225,7 +225,7 @@ impl Iterator {
225225
/// assert_eq!(flat_tree::Iterator::new(7).right_child(), 11);
226226
/// ```
227227
#[inline]
228-
pub fn right_child(&mut self) -> usize {
228+
pub fn right_child(&mut self) -> u64 {
229229
if self.factor == 2 {
230230
return self.index;
231231
}
@@ -237,7 +237,7 @@ impl Iterator {
237237
}
238238

239239
impl iter::Iterator for Iterator {
240-
type Item = usize;
240+
type Item = u64;
241241

242242
#[inline]
243243
fn next(&mut self) -> Option<Self::Item> {
@@ -255,7 +255,7 @@ impl Default for Iterator {
255255
}
256256

257257
#[inline]
258-
fn two_pow(n: usize) -> usize {
258+
fn two_pow(n: u64) -> u64 {
259259
if n < 31 {
260260
1 << n
261261
} else {

src/lib.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub use iterator::Iterator;
2121
/// assert_eq!(flat_tree::index(3, 1), 23);
2222
/// ```
2323
#[inline]
24-
pub const fn index(depth: usize, offset: usize) -> usize {
24+
pub const fn index(depth: u64, offset: u64) -> u64 {
2525
(offset << (depth + 1)) | ((1 << depth) - 1)
2626
}
2727

@@ -36,9 +36,9 @@ pub const fn index(depth: usize, offset: usize) -> usize {
3636
/// assert_eq!(flat_tree::depth(4), 0);
3737
/// ```
3838
#[inline]
39-
pub const fn depth(i: usize) -> usize {
39+
pub const fn depth(i: u64) -> u64 {
4040
// Count trailing `1`s of the binary representation of the number.
41-
(!i).trailing_zeros() as usize
41+
(!i).trailing_zeros() as u64
4242
}
4343

4444
/// Returns the offset of a node.
@@ -52,7 +52,7 @@ pub const fn depth(i: usize) -> usize {
5252
/// assert_eq!(flat_tree::offset(4), 2);
5353
/// ```
5454
#[inline]
55-
pub fn offset(i: usize) -> usize {
55+
pub fn offset(i: u64) -> u64 {
5656
let depth = self::depth(i);
5757
if is_even(i) {
5858
i / 2
@@ -72,7 +72,7 @@ pub fn offset(i: usize) -> usize {
7272
/// assert_eq!(flat_tree::parent(4), 5);
7373
/// ```
7474
#[inline]
75-
pub fn parent(i: usize) -> usize {
75+
pub fn parent(i: u64) -> u64 {
7676
let depth = self::depth(i);
7777
index(depth + 1, offset(i) >> 1)
7878
}
@@ -87,7 +87,7 @@ pub fn parent(i: usize) -> usize {
8787
/// assert_eq!(flat_tree::sibling(5), 1);
8888
/// ```
8989
#[inline]
90-
pub fn sibling(i: usize) -> usize {
90+
pub fn sibling(i: u64) -> u64 {
9191
let depth = self::depth(i);
9292
index(depth, offset(i) ^ 1)
9393
}
@@ -102,7 +102,7 @@ pub fn sibling(i: usize) -> usize {
102102
/// assert_eq!(flat_tree::uncle(5), 11);
103103
/// ```
104104
#[inline]
105-
pub fn uncle(i: usize) -> usize {
105+
pub fn uncle(i: u64) -> u64 {
106106
let depth = self::depth(i);
107107
index(depth + 1, offset(parent(i)) ^ 1)
108108
}
@@ -117,7 +117,7 @@ pub fn uncle(i: usize) -> usize {
117117
/// assert_eq!(flat_tree::children(9), Some((8, 10)));
118118
/// ```
119119
#[inline]
120-
pub fn children(i: usize) -> Option<(usize, usize)> {
120+
pub fn children(i: u64) -> Option<(u64, u64)> {
121121
let depth = self::depth(i);
122122
if is_even(i) {
123123
None
@@ -138,7 +138,7 @@ pub fn children(i: usize) -> Option<(usize, usize)> {
138138
/// assert_eq!(flat_tree::left_child(3), Some(1));
139139
/// ```
140140
#[inline]
141-
pub fn left_child(i: usize) -> Option<usize> {
141+
pub fn left_child(i: u64) -> Option<u64> {
142142
let depth = self::depth(i);
143143
if is_even(i) {
144144
None
@@ -159,7 +159,7 @@ pub fn left_child(i: usize) -> Option<usize> {
159159
/// ```
160160
// TODO: handle errors
161161
#[inline]
162-
pub fn right_child(i: usize) -> Option<usize> {
162+
pub fn right_child(i: u64) -> Option<u64> {
163163
let depth = self::depth(i);
164164
if is_even(i) {
165165
None
@@ -181,7 +181,7 @@ pub fn right_child(i: usize) -> Option<usize> {
181181
/// assert_eq!(flat_tree::right_span(27), 30);
182182
/// ```
183183
#[inline]
184-
pub fn right_span(i: usize) -> usize {
184+
pub fn right_span(i: u64) -> u64 {
185185
let depth = self::depth(i);
186186
if depth == 0 {
187187
i
@@ -201,7 +201,7 @@ pub fn right_span(i: usize) -> usize {
201201
/// assert_eq!(flat_tree::left_span(27), 24);
202202
/// ```
203203
#[inline]
204-
pub fn left_span(i: usize) -> usize {
204+
pub fn left_span(i: u64) -> u64 {
205205
let depth = self::depth(i);
206206
if depth == 0 {
207207
i
@@ -221,7 +221,7 @@ pub fn left_span(i: usize) -> usize {
221221
/// assert_eq!(flat_tree::spans(27), (24, 30));
222222
/// ```
223223
#[inline]
224-
pub fn spans(i: usize) -> (usize, usize) {
224+
pub fn spans(i: u64) -> (u64, u64) {
225225
(left_span(i), right_span(i))
226226
}
227227

@@ -237,7 +237,7 @@ pub fn spans(i: usize) -> (usize, usize) {
237237
/// assert_eq!(flat_tree::count(27), 7);
238238
/// ```
239239
#[inline]
240-
pub const fn count(i: usize) -> usize {
240+
pub const fn count(i: u64) -> u64 {
241241
let depth = self::depth(i);
242242
(2 << depth) - 1
243243
}
@@ -281,7 +281,7 @@ pub const fn count(i: usize) -> usize {
281281
/// assert_eq!(nodes, [7]);
282282
/// ```
283283
#[inline]
284-
pub fn full_roots(i: usize, nodes: &mut Vec<usize>) {
284+
pub fn full_roots(i: u64, nodes: &mut Vec<u64>) {
285285
assert!(
286286
is_even(i),
287287
format!(
@@ -308,12 +308,12 @@ pub fn full_roots(i: usize, nodes: &mut Vec<usize>) {
308308
}
309309

310310
#[inline]
311-
pub(crate) const fn is_even(num: usize) -> bool {
311+
pub(crate) const fn is_even(num: u64) -> bool {
312312
(num & 1) == 0
313313
}
314314

315315
#[inline]
316-
pub(crate) const fn is_odd(num: usize) -> bool {
316+
pub(crate) const fn is_odd(num: u64) -> bool {
317317
(num & 1) != 0
318318
}
319319

0 commit comments

Comments
 (0)