-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaabtable.m
63 lines (55 loc) · 1.49 KB
/
aabtable.m
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
55
56
57
58
59
60
61
62
63
const VectorOfAny <- Vector.of[Any]
const AABTable <- class AABTable [size : Integer]
var currentsize : Integer <- size
var lefts : VectorOfAny <- VectorOfAny.create[currentsize]
var rights : VectorOfAny <- VectorOfAny.create[currentsize]
var count : Integer <- 0
export operation Insert [left : Any, right : Any]
if count == currentsize then self.grow end if
lefts[count] <- left
rights[count] <- right
count <- count + 1
end Insert
operation grow
const oldrights <- rights
const oldlefts <- lefts
const oldsize <- currentsize
currentsize <- currentsize * 2
rights <- VectorOfAny.create[currentsize]
lefts <- VectorOfAny.create[currentsize]
var h : Integer <- 0
loop
exit when h == count
lefts[h] <- oldlefts[h]
rights[h] <- oldrights[h]
h <- h + 1
end loop
end grow
export function Lookup [left : Any, right : Any] -> [answer : Boolean]
var h : Integer <- 0
loop
exit when h = count
if lefts[h] == left and right == rights[h] then
answer <- true
return
end if
h <- h + 1
end loop
answer <- false
end Lookup
export operation Forget[left : Any, right : Any]
var h : Integer <- 0
count <- count - 1
loop
exit when lefts[h] == left and rights[h] == right
h <- h + 1
end loop
loop
exit when h = count
lefts[h] <- lefts[h + 1]
rights[h] <- rights[h + 1]
h <- h + 1
end loop
end Forget
end AABTable
export AABTable