forked from covscript/covscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_table.csp
50 lines (50 loc) · 964 Bytes
/
hash_table.csp
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
package hash_table
struct type_impl
var bucket_count=new number
var table=new array
var bucket_func=[](val)->math.abs(to_integer(runtime.hash(val)))%this.bucket_count
function insert(dat)
var bucket=bucket_func(dat.first())
if table.at(bucket)==null
table.at(bucket)=gcnew list
end
var lst=table.at(bucket)
foreach it:*lst
if it.first()==dat.first()
it.second()=dat.second()
return
end
end
lst->push_back(dat)
end
function get(key)
var bucket=bucket_func(key)
var lst=table.at(bucket)
if lst==null
return null
end
foreach it:*lst
if it.first()==key
return it.second()
end
end
return null
end
function print()
foreach lst:table
if lst!=null
foreach p:*lst
system.out.println(to_string(p.first())+":"+to_string(p.second()))
end
end
end
end
end
function create(bc)
var ht=gcnew type_impl
ht->bucket_count=bc
for i=1,i<=bc,++i
ht->table.push_back(null)
end
return ht
end