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
|
local bucketlib = require "cratera.lib.bucket"
local Bucketable = bucketlib.Bucketable
local Bucket = bucketlib.Bucket
local Struct = Struct
local Point = mkstruct(function(_struct, x, y)
return {x, y}
end, {
__eq = function(a, b)
return a[Struct] == b[Struct] and a[1] == b[1] and a[2] == b[2]
end
})
Point[Bucketable] = {}
-- FIXME we really need that 'function Point:[Bucketable].bucket(bucket)' syntax...
local pointbucket = Point[Bucketable]
function pointbucket:bucket(bucket)
bucket:[Bucket].put_number(self[1])
bucket:[Bucket].put_number(self[2])
end
local map = bucketlib.BucketingMap()
map:put(Point(0, 1), "hello")
map:put(Point(2, 3), "cratera")
map:put(Point(4, 5), "world")
assert(map._buckets[2])
assert(map._buckets[3])
assert(map._buckets[4])
assert(map:get(Point(0, 1)) == "hello")
assert(map:get(Point(2, 3)) == "cratera")
assert(map:get(Point(4, 5)) == "world")
|