|
1 | 1 | (define (make-compact-array size)
|
2 |
| - (cond ((< size #xFF) |
3 |
| - (let ((bv (make-bytevector (+ size 2) #xFF))) |
| 2 | + (cond ((fx<? size #xFF) |
| 3 | + (let ((bv (make-bytevector (fx+ size 2) #xFF))) |
4 | 4 | (bytevector-u8-set! bv 0 1)
|
5 | 5 | (bytevector-u8-set! bv 1 size)
|
6 | 6 | bv))
|
7 |
| - ((< size #xFFFF) |
8 |
| - (let ((bv (make-bytevector (* 2 (+ size 2)) #xFF))) |
| 7 | + ((fx<? size #xFFFF) |
| 8 | + (let ((bv (make-bytevector (fxarithmetic-shift-left (fx+ size 2) 1) #xFF))) |
9 | 9 | (bytevector-u8-set! bv 0 2)
|
10 | 10 | (bytevector-u16-native-set! bv 2 size)
|
11 | 11 | bv))
|
12 |
| - ((< size #xFFFFFFFF) |
13 |
| - (let ((bv (make-bytevector (* 4 (+ size 2)) #xFF))) |
| 12 | + ((fx<? size #xFFFFFFFF) |
| 13 | + (let ((bv (make-bytevector (fxarithmetic-shift-left (fx+ size 2) 2) #xFF))) |
14 | 14 | (bytevector-u8-set! bv 0 4)
|
15 | 15 | (bytevector-u32-native-set! bv 4 size)
|
16 | 16 | bv))
|
17 | 17 | (else
|
18 |
| - (let ((bv (make-bytevector (* 8 (+ size 2)) #xFF))) |
| 18 | + (let ((bv (make-bytevector (fxarithmetic-shift-left (fx+ size 2) 3) #xFF))) |
19 | 19 | (bytevector-u8-set! bv 0 8)
|
20 | 20 | (bytevector-u64-native-set! bv 8 size)
|
21 | 21 | bv))))
|
22 | 22 |
|
23 | 23 | (define (compact-array-ref sa idx)
|
24 | 24 | (define (max-to x n) (if (eqv? x n) #f x))
|
25 |
| - (assert (bytevector? sa)) |
26 | 25 | (case (bytevector-u8-ref sa 0)
|
27 |
| - ((1) (max-to (bytevector-u8-ref sa (+ idx 2)) #xFF)) |
28 |
| - ((2) (max-to (bytevector-u16-native-ref sa (* (+ idx 2) 2)) #xFFFF)) |
29 |
| - ((4) (max-to (bytevector-u32-native-ref sa (* (+ idx 2) 4)) #xFFFFFFFF)) |
30 |
| - ((8) (max-to (bytevector-u64-native-ref sa (* (+ idx 2) 8)) #xFFFFFFFFFFFFFFFF)) |
31 |
| - (else (assertion-violation 'compact-array-ref "not a compact array" sa)))) |
| 26 | + ((1) (max-to (bytevector-u8-ref sa (fx+ idx 2)) #xFF)) |
| 27 | + ((2) (max-to (bytevector-u16-native-ref sa (fxarithmetic-shift-left (fx+ idx 2) 1)) #xFFFF)) |
| 28 | + ((4) (max-to (bytevector-u32-native-ref sa (fxarithmetic-shift-left (fx+ idx 2) 2)) #xFFFFFFFF)) |
| 29 | + ((8) (max-to (bytevector-u64-native-ref sa (fxarithmetic-shift-left (fx+ idx 2) 3)) #xFFFFFFFFFFFFFFFF)))) |
32 | 30 |
|
33 | 31 | (define (compact-array-set? sa idx)
|
34 | 32 | (not (not (compact-array-ref sa idx))))
|
35 | 33 |
|
36 | 34 | (define (compact-array-set! sa idx val)
|
37 |
| - (define (check-size n) |
38 |
| - (if (>= val n) (assertion-violation 'compact-array-set! "can't store value in compact array, try upgrading it"))) |
39 |
| - (assert (bytevector? sa)) |
40 | 35 | (case (bytevector-u8-ref sa 0)
|
41 |
| - ((1) (check-size #xFF) (bytevector-u8-set! sa (+ idx 2) val)) |
42 |
| - ((2) (check-size #xFFFF) (bytevector-u16-native-set! sa (* (+ idx 2) 2) val)) |
43 |
| - ((4) (check-size #xFFFFFFFF) (bytevector-u32-native-set! sa (* (+ idx 2) 4) val)) |
44 |
| - ((8) (check-size #xFFFFFFFFFFFFFFFF) (bytevector-u64-native-set! sa (* (+ idx 2) 8) val)) |
45 |
| - (else (assertion-violation 'compact-array-set! "not a compact array" sa)))) |
| 36 | + ((1) (bytevector-u8-set! sa (fx+ idx 2) val)) |
| 37 | + ((2) (bytevector-u16-native-set! sa (fxarithmetic-shift-left (fx+ idx 2) 1) val)) |
| 38 | + ((4) (bytevector-u32-native-set! sa (fxarithmetic-shift-left (fx+ idx 2) 2) val)) |
| 39 | + ((8) (fxarithmetic-shift-left (fx+ idx 2) 3)))) |
46 | 40 |
|
47 | 41 | (define (compact-array-delete! sa idx)
|
48 |
| - (assert (bytevector? sa)) |
49 | 42 | (case (bytevector-u8-ref sa 0)
|
50 |
| - ((1) (bytevector-u8-set! sa (+ idx 2) #xFF)) |
51 |
| - ((2) (bytevector-u16-native-set! sa (* (+ idx 2) 2) #xFFFF)) |
52 |
| - ((4) (bytevector-u32-native-set! sa (* (+ idx 2) 4) #xFFFFFFFF)) |
53 |
| - ((8) (bytevector-u64-native-set! sa (* (+ idx 2) 8) #xFFFFFFFFFFFFFFFF)) |
54 |
| - (else (assertion-violation 'compact-array-delete! "not a compact array" sa)))) |
| 43 | + ((1) (bytevector-u8-set! sa (fx+ idx 2) #xFF)) |
| 44 | + ((2) (bytevector-u16-native-set! sa (fxarithmetic-shift-left (fx+ idx 2) 1) #xFFFF)) |
| 45 | + ((4) (bytevector-u32-native-set! sa (fxarithmetic-shift-left (fx+ idx 2) 2) #xFFFFFFFF)) |
| 46 | + ((8) (bytevector-u64-native-set! sa (fxarithmetic-shift-left (fx+ idx 2) 3) #xFFFFFFFFFFFFFFFF)))) |
55 | 47 |
|
56 | 48 | (define (compact-array-clear! sa)
|
57 |
| - (assert (bytevector? sa)) |
58 | 49 | (let ((elt-size (bytevector-u8-ref sa 0)))
|
59 | 50 | (bytevector-fill! sa #xFF)
|
60 | 51 | (bytevector-u8-set! sa 0 elt-size)
|
61 | 52 | (case elt-size
|
62 |
| - ((1) (bytevector-u8-set! sa 1 (- 2 (bytevector-length sa)))) |
| 53 | + ((1) (bytevector-u8-set! sa 1 (fx- (bytevector-length sa) 2))) |
63 | 54 | ((2) (bytevector-u16-native-set! sa 2
|
64 |
| - (- 2 (div (bytevector-length sa) 2)))) |
| 55 | + (fx- (fxarithmetic-shift-right (bytevector-length sa) 1) 2))) |
65 | 56 | ((4) (bytevector-u32-native-set! sa 4
|
66 |
| - (- 2 (div (bytevector-length sa) 4)))) |
| 57 | + (fx- (fxarithmetic-shift-right (bytevector-length sa) 2) 2))) |
67 | 58 | ((8) (bytevector-u32-native-set! sa 8
|
68 |
| - (- 2 (div (bytevector-length sa) 8))))))) |
| 59 | + (fx- (fxarithmetic-shift-right (bytevector-length sa) 3) 2)))))) |
69 | 60 |
|
70 | 61 | (define (compact-array-copy sa) (bytevector-copy sa))
|
71 | 62 |
|
72 | 63 | (define (compact-array-length sa)
|
73 |
| - (assert (bytevector? sa)) |
74 | 64 | (case (bytevector-u8-ref sa 0)
|
75 | 65 | ((1) (bytevector-u8-ref sa 1))
|
76 | 66 | ((2) (bytevector-u16-native-ref sa 2))
|
77 | 67 | ((4) (bytevector-u32-native-ref sa 4))
|
78 |
| - ((8) (bytevector-u64-native-ref sa 8)) |
79 |
| - (else (assertion-violation 'compact-array-ref "not a compact array" sa)))) |
| 68 | + ((8) (bytevector-u64-native-ref sa 8)))) |
0 commit comments