@@ -14,14 +14,14 @@ public class SparseTable<T> {
14
14
private var defaultT : T
15
15
private var table : [ [ T ] ]
16
16
private var function : ( T , T ) -> T
17
-
17
+
18
18
public init ( array: [ T ] , function: @escaping ( T , T ) -> T , defaultT: T ) {
19
19
let N = array. count
20
20
let W = Int ( ceil ( log2 ( Double ( N) ) ) )
21
21
table = [ [ T] ] ( repeating: [ T] ( repeating: defaultT, count: N) , count: W)
22
22
self . function = function
23
23
self . defaultT = defaultT
24
-
24
+
25
25
for w in 0 ..< W {
26
26
for l in 0 ..< N {
27
27
if w == 0 {
@@ -35,7 +35,7 @@ public class SparseTable<T> {
35
35
}
36
36
}
37
37
}
38
-
38
+
39
39
public func query( from l: Int , until r: Int ) -> T {
40
40
let width = r - l
41
41
let N = table [ 0 ] . count
@@ -97,14 +97,14 @@ print("-------------------------------------------------------------------------
97
97
print ( " ---------------------------- EXAMPLE 4 ------------------------------------- " )
98
98
// An array of positive integers and we're repeatedly finding
99
99
// the gcd (greatest common divisor) over various ranges. The gcd operator is
100
- // associative and idempontent so we can use it with sparse tables
100
+ // associative and idempotent so we can use it with sparse tables
101
101
102
102
let posIntArray = [ 7 , 2 , 3 , 4 , 6 , 5 , 25 , 75 , 100 ]
103
103
func gcd( _ m: Int , _ n: Int ) -> Int {
104
104
var a = 0
105
105
var b = max ( m, n)
106
106
var r = min ( m, n)
107
-
107
+
108
108
while r != 0 {
109
109
a = b
110
110
b = r
@@ -127,7 +127,7 @@ print("------------------------------------------------------------------------\
127
127
print ( " ---------------------------- EXAMPLE 5 ------------------------------------- " )
128
128
// An array of nonnegative integers where for each integer we consider its binary representation.
129
129
// We're repeatedly finding the binary OR (|) over various ranges. The binary operator is
130
- // associative and idempontent so we can use it with sparse tables
130
+ // associative and idempotent so we can use it with sparse tables
131
131
132
132
let binArray = [ 0b1001 , 0b1100 , 0b0000 , 0b0001 , 0b0010 , 0b0100 , 0b0000 , 0b1111 ]
133
133
0 commit comments