forked from kennytm/ozdss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinearDictionary.oz
83 lines (74 loc) · 1.8 KB
/
LinearDictionary.oz
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
functor
export
new: LDNew
condGet: LDCondGet
condExchange: LDCondExchange
condPut: LDCondPut
put: LDPut
remove: LDRemove
length: LDLength
define
% A generic dictionary that can accept any keys with an equality operator.
% This dictionary is O(N) in all operations. Please GenericDictionary if a
% strict weak ordering can be defined.
fun {LDNew}
nil
end
fun {LDCondGet LD Key DefValue}
case LD
of K#V|Tail then
if K == Key then
V
else
{LDCondGet Tail Key DefValue}
end
else
DefValue
end
end
fun {LDPut LD Key Value}
case LD
of K#V|Tail then
if K == Key then
K#Value|Tail
else
K#V|{LDPut Tail Key Value}
end
else
[Key#Value]
end
end
fun {LDCondExchange LD Key DefValue ?OldValue NewValue}
case LD
of K#V|Tail then
if K == Key then
OldValue = V
K#NewValue|Tail
else
K#V|{LDCondExchange Tail Key DefValue ?OldValue NewValue}
end
else
OldValue = DefValue
[Key#NewValue]
end
end
fun {LDCondPut LD Key ?OldValue NewValue ?Existing}
case LD
of K#V|Tail then
if K == Key then
Existing = true
OldValue = V
K#NewValue|Tail
else
K#V|{LDCondPut Tail Key ?OldValue NewValue ?Existing}
end
else
Existing = false
[Key#NewValue]
end
end
fun {LDRemove LD Key}
{Filter LD fun {$ K#_} K \= Key end}
end
LDLength = Length
end